You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by ro...@apache.org on 2019/06/18 08:27:57 UTC
[james-project] branch master updated (e738cd3 -> b60420a)
This is an automated email from the ASF dual-hosted git repository.
rouazana pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git.
from e738cd3 Merge remote-tracking branch 'trantienduchn/JAMES-2621'
new 3c79a9f JAMES-1975 Move JDKIM mailet as a server mailet
new 44b992a JAMES-1975 Solve some basic inteliJ warnings
new e7c3770 JAMES-1975 Fix checkstyle issues in DKIM mailet code
new 3accc70 JAMES-1975 Clean james-server-mailet-dkim dependencies
new a40c41a JAMES-1975 Make JDKIM mailets available in guice products
new 2d5add9 JAMES-1975 Use ${james.groupId} in DKIM mailet POM
new 5baaf6b JAMES-1975 Move snapshot repository declaration to parent POM
new 5cff614 JAMES-1975 Update dkim to JUnit 5 and new mailet API
new b0cbfae JAMES-1975 Use released version of DKIM
new 30a07f2 JAMES-1975 Replace not-yet-commons-ssl by BouncyCastle in DKIM
new b8f9199 JAMES-1975 Refactor DKIM
new 0371a57 JAMES-1975 Use file for privateKey in DKIMSign
new 0ea10a6 JAMES-1975 Fix Checkstyle on DKIM
new b60420a JAMES-1975 Be able to set either a privateKey of a privateKeyFilepath in DKIM mailet
The 14 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
pom.xml | 5 +
server/container/guice/guice-common/pom.xml | 4 +
server/mailet/dkim/pom.xml | 139 +++++++++
.../james/jdkim/mailets/CRLFOutputStream.java | 159 ++++++++++
.../apache/james/jdkim/mailets/ConvertTo7Bit.java} | 56 ++--
.../org/apache/james/jdkim/mailets/DKIMSign.java | 215 +++++++++++++
.../org/apache/james/jdkim/mailets/DKIMVerify.java | 127 ++++++++
.../jdkim/mailets/HeaderSkippingOutputStream.java | 75 +++++
.../james/jdkim/mailets/MimeMessageHeaders.java | 78 +++++
.../apache/james/jdkim/mailets/DKIMSignTest.java | 345 +++++++++++++++++++++
.../apache/james/jdkim/mailets/DKIMVerifyTest.java | 124 ++++++++
.../mailet/dkim/src/test/resources/test-dkim.pem | 15 +
server/pom.xml | 14 +-
13 files changed, 1321 insertions(+), 35 deletions(-)
create mode 100644 server/mailet/dkim/pom.xml
create mode 100644 server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/CRLFOutputStream.java
copy server/mailet/{mailets/src/main/java/org/apache/james/transport/mailets/remote/delivery/Converter7Bit.java => dkim/src/main/java/org/apache/james/jdkim/mailets/ConvertTo7Bit.java} (57%)
create mode 100644 server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMSign.java
create mode 100644 server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMVerify.java
create mode 100644 server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/HeaderSkippingOutputStream.java
create mode 100644 server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/MimeMessageHeaders.java
create mode 100644 server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMSignTest.java
create mode 100644 server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMVerifyTest.java
create mode 100644 server/mailet/dkim/src/test/resources/test-dkim.pem
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org
[james-project] 05/14: JAMES-1975 Make JDKIM mailets available in
guice products
Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rouazana pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git
commit a40c41a908731ea4bf55f81a77e4cf30432859c9
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Fri Aug 31 18:39:27 2018 +0700
JAMES-1975 Make JDKIM mailets available in guice products
---
pom.xml | 5 +++++
server/container/guice/guice-common/pom.xml | 4 ++++
2 files changed, 9 insertions(+)
diff --git a/pom.xml b/pom.xml
index d4905b0..670f5c2 100644
--- a/pom.xml
+++ b/pom.xml
@@ -1550,6 +1550,11 @@
</dependency>
<dependency>
<groupId>${james.groupId}</groupId>
+ <artifactId>james-server-mailet-dkim</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>${james.groupId}</groupId>
<artifactId>james-server-mailets</artifactId>
<version>${project.version}</version>
</dependency>
diff --git a/server/container/guice/guice-common/pom.xml b/server/container/guice/guice-common/pom.xml
index fd3dd31..036ada1 100644
--- a/server/container/guice/guice-common/pom.xml
+++ b/server/container/guice/guice-common/pom.xml
@@ -108,6 +108,10 @@
</dependency>
<dependency>
<groupId>${james.groupId}</groupId>
+ <artifactId>james-server-mailet-dkim</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>${james.groupId}</groupId>
<artifactId>james-server-mailets</artifactId>
</dependency>
<dependency>
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org
[james-project] 10/14: JAMES-1975 Replace not-yet-commons-ssl by
BouncyCastle in DKIM
Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rouazana pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 30a07f29d8c6ece689c590d2372c23ede8136da2
Author: Gautier DI FOLCO <gd...@linagora.com>
AuthorDate: Tue May 21 16:11:51 2019 +0200
JAMES-1975 Replace not-yet-commons-ssl by BouncyCastle in DKIM
---
server/mailet/dkim/pom.xml | 13 +++--
.../org/apache/james/jdkim/mailets/DKIMSign.java | 56 +++++++++++++++++-----
2 files changed, 53 insertions(+), 16 deletions(-)
diff --git a/server/mailet/dkim/pom.xml b/server/mailet/dkim/pom.xml
index 3c26fd0..3d17acb 100644
--- a/server/mailet/dkim/pom.xml
+++ b/server/mailet/dkim/pom.xml
@@ -107,16 +107,19 @@
</exclusions>
</dependency>
<dependency>
- <groupId>ca.juliusdavies</groupId>
- <artifactId>not-yet-commons-ssl</artifactId>
- <version>0.3.11</version>
- </dependency>
- <dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
+ <groupId>org.bouncycastle</groupId>
+ <artifactId>bcpkix-jdk15on</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.bouncycastle</groupId>
+ <artifactId>bcprov-jdk15on</artifactId>
+ </dependency>
+ <dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
diff --git a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMSign.java b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMSign.java
index f27fbe4..65c9007 100644
--- a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMSign.java
+++ b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMSign.java
@@ -20,12 +20,18 @@
package org.apache.james.jdkim.mailets;
import java.io.ByteArrayInputStream;
+import java.io.FileReader;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
import java.io.OutputStream;
-import java.security.GeneralSecurityException;
+import java.security.KeyFactory;
+import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
+import java.security.Security;
import java.security.spec.InvalidKeySpecException;
+import java.security.spec.RSAPrivateCrtKeySpec;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
@@ -34,7 +40,7 @@ import javax.mail.Header;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
-import org.apache.commons.ssl.PKCS8Key;
+import org.apache.commons.io.IOUtils;
import org.apache.james.jdkim.DKIMSigner;
import org.apache.james.jdkim.api.BodyHasher;
import org.apache.james.jdkim.api.Headers;
@@ -42,6 +48,13 @@ import org.apache.james.jdkim.api.SignatureRecord;
import org.apache.james.jdkim.exceptions.PermFailException;
import org.apache.mailet.Mail;
import org.apache.mailet.base.GenericMailet;
+import org.bouncycastle.jce.provider.JCERSAPrivateCrtKey;
+import org.bouncycastle.openssl.PEMDecryptorProvider;
+import org.bouncycastle.openssl.PEMEncryptedKeyPair;
+import org.bouncycastle.openssl.PEMKeyPair;
+import org.bouncycastle.openssl.PEMParser;
+import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
+import org.bouncycastle.openssl.jcajce.JcePEMDecryptorProviderBuilder;
/**
* This mailet sign a message using the DKIM protocol
@@ -86,14 +99,14 @@ public class DKIMSign extends GenericMailet {
/**
* @return the signatureTemplate
*/
- protected String getSignatureTemplate() {
+ private String getSignatureTemplate() {
return signatureTemplate;
}
/**
* @return the privateKey
*/
- protected PrivateKey getPrivateKey() {
+ private PrivateKey getPrivateKey() {
return privateKey;
}
@@ -102,18 +115,16 @@ public class DKIMSign extends GenericMailet {
String privateKeyString = getInitParameter("privateKey");
String privateKeyPassword = getInitParameter("privateKeyPassword", null);
forceCRLF = getInitParameter("forceCRLF", true);
+
try {
- PKCS8Key pkcs8 = new PKCS8Key(new ByteArrayInputStream(
- privateKeyString.getBytes()),
- privateKeyPassword != null ? privateKeyPassword
- .toCharArray() : null);
- privateKey = pkcs8.getPrivateKey();
+ char[] passphrase = privateKeyPassword != null ? privateKeyPassword.toCharArray() : null;
+ privateKey = extractPrivateKey(new ByteArrayInputStream(privateKeyString.getBytes()), passphrase);
} catch (NoSuchAlgorithmException e) {
throw new MessagingException("Unknown private key algorythm: " + e.getMessage(), e);
} catch (InvalidKeySpecException e) {
throw new MessagingException("PrivateKey should be in base64 encoded PKCS8 (der) format: " + e.getMessage(), e);
- } catch (GeneralSecurityException e) {
- throw new MessagingException("General security exception: " + e.getMessage(), e);
+ } catch (IOException e) {
+ throw new MessagingException("Problem during reading: " + e.getMessage(), e);
}
}
@@ -170,4 +181,27 @@ public class DKIMSign extends GenericMailet {
}
}
+ private PrivateKey extractPrivateKey(InputStream rawKey, char[] passphrase) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
+ Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
+
+ PEMParser pemParser = new PEMParser(new InputStreamReader(rawKey));
+ Object pemObject = pemParser.readObject();
+ JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
+ KeyPair keyPair;
+ if (pemObject instanceof PEMEncryptedKeyPair)
+ {
+ PEMEncryptedKeyPair pemEncryptedKeyPair = (PEMEncryptedKeyPair) pemObject;
+ PEMDecryptorProvider decProv = new JcePEMDecryptorProviderBuilder().build(passphrase);
+ keyPair = converter.getKeyPair(pemEncryptedKeyPair.decryptKeyPair(decProv));
+ }
+ else
+ {
+ keyPair = converter.getKeyPair((PEMKeyPair) pemObject);
+ }
+
+ KeyFactory keyFac = KeyFactory.getInstance("RSA");
+ RSAPrivateCrtKeySpec privateKeySpec = keyFac.getKeySpec(keyPair.getPrivate(), RSAPrivateCrtKeySpec.class);
+
+ return keyFac.generatePrivate(privateKeySpec);
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org
[james-project] 11/14: JAMES-1975 Refactor DKIM
Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rouazana pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git
commit b8f91998eae0488aea4ca4032262185f56f3222f
Author: Gautier DI FOLCO <gd...@linagora.com>
AuthorDate: Tue May 21 16:46:40 2019 +0200
JAMES-1975 Refactor DKIM
---
.../org/apache/james/jdkim/mailets/DKIMSign.java | 43 ++++++++--------
.../org/apache/james/jdkim/mailets/DKIMVerify.java | 10 +++-
.../james/jdkim/mailets/MimeMessageHeaders.java | 57 +++++++++++++---------
3 files changed, 61 insertions(+), 49 deletions(-)
diff --git a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMSign.java b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMSign.java
index 65c9007..4989f4a 100644
--- a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMSign.java
+++ b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMSign.java
@@ -32,14 +32,15 @@ import java.security.PrivateKey;
import java.security.Security;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPrivateCrtKeySpec;
-import java.util.Enumeration;
-import java.util.LinkedList;
+import java.util.Collections;
import java.util.List;
import javax.mail.Header;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
+import com.github.fge.lambdas.Throwing;
+import com.google.common.collect.ImmutableList;
import org.apache.commons.io.IOUtils;
import org.apache.james.jdkim.DKIMSigner;
import org.apache.james.jdkim.api.BodyHasher;
@@ -142,10 +143,14 @@ public class DKIMSign extends GenericMailet {
os = new CRLFOutputStream(os);
}
message.writeTo(os);
- bhj.getOutputStream().close();
} catch (IOException e) {
- throw new MessagingException("Exception calculating bodyhash: "
- + e.getMessage(), e);
+ throw new MessagingException("Exception calculating bodyhash: " + e.getMessage(), e);
+ } finally {
+ try {
+ bhj.getOutputStream().close();
+ } catch (IOException e) {
+ throw new MessagingException("Exception calculating bodyhash: " + e.getMessage(), e);
+ }
}
String signatureHeader = signer.sign(headers, bhj);
// Unfortunately JavaMail does not give us a method to add headers
@@ -153,32 +158,22 @@ public class DKIMSign extends GenericMailet {
// message.addHeaderLine(signatureHeader);
prependHeader(message, signatureHeader);
} catch (PermFailException e) {
- throw new MessagingException("PermFail while signing: "
- + e.getMessage(), e);
+ throw new MessagingException("PermFail while signing: " + e.getMessage(), e);
}
}
private void prependHeader(MimeMessage message, String signatureHeader)
throws MessagingException {
- List<String> prevHeader = new LinkedList<>();
- // read all the headers
- for (Enumeration<String> e = message.getAllHeaderLines(); e.hasMoreElements();) {
- String headerLine = e.nextElement();
- prevHeader.add(headerLine);
- }
- // remove all the headers
- for (Enumeration<Header> e = message.getAllHeaders(); e.hasMoreElements();) {
- Header header = e.nextElement();
- message.removeHeader(header.getName());
- }
- // add our header
+ List<String> prevHeader = Collections.list(message.getAllHeaderLines());
+ Collections.list(message.getAllHeaders())
+ .stream()
+ .map(Header::getName)
+ .forEach(Throwing.consumer(message::removeHeader).sneakyThrow());
+
message.addHeaderLine(signatureHeader);
- // add the remaining headers using "addHeaderLine" that won't alter the
- // insertion order.
- for (String header : prevHeader) {
- message.addHeaderLine(header);
- }
+ prevHeader
+ .forEach(Throwing.consumer(message::addHeaderLine).sneakyThrow());
}
private PrivateKey extractPrivateKey(InputStream rawKey, char[] passphrase) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
diff --git a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMVerify.java b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMVerify.java
index a98c12b..e0db937 100644
--- a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMVerify.java
+++ b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMVerify.java
@@ -106,12 +106,20 @@ public class DKIMVerify extends GenericMailet {
os = new CRLFOutputStream(os);
}
message.writeTo(os);
- bh.getOutputStream().close();
}
} catch (IOException e) {
throw new MessagingException("Exception calculating bodyhash: "
+ e.getMessage(), e);
+ } finally {
+ try {
+ if (bh != null) {
+ bh.getOutputStream().close();
+ }
+ } catch (IOException e) {
+ throw new MessagingException("Exception calculating bodyhash: "
+ + e.getMessage(), e);
+ }
}
return verifier.verify(bh);
}
diff --git a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/MimeMessageHeaders.java b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/MimeMessageHeaders.java
index 6a975a2..3eb3dbf 100644
--- a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/MimeMessageHeaders.java
+++ b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/MimeMessageHeaders.java
@@ -19,44 +19,45 @@
package org.apache.james.jdkim.mailets;
-import java.util.Enumeration;
-import java.util.HashMap;
-import java.util.LinkedList;
import java.util.List;
-import java.util.Map;
+import java.util.Locale;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
+import org.apache.commons.lang3.tuple.Pair;
import org.apache.james.jdkim.api.Headers;
+import com.github.fge.lambdas.Throwing;
+import com.github.steveash.guavate.Guavate;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableListMultimap;
+import com.google.common.collect.Iterators;
+import com.google.common.collect.Streams;
+
/**
* An adapter to let DKIMSigner read headers from MimeMessage
*/
final class MimeMessageHeaders implements Headers {
- private final Map<String, List<String>> headers;
+ private final ImmutableListMultimap<String, String> headers;
private final List<String> fields;
public MimeMessageHeaders(MimeMessage message) throws MessagingException {
- headers = new HashMap<>();
- fields = new LinkedList<>();
- for (Enumeration<String> e = message.getAllHeaderLines(); e.hasMoreElements();) {
- String head = e.nextElement();
- int p = head.indexOf(':');
- if (p <= 0) {
- throw new MessagingException("Bad header line: " + head);
- }
- String headerName = head.substring(0, p).trim();
- String headerNameLC = headerName.toLowerCase();
- fields.add(headerName);
- List<String> strings = headers.get(headerNameLC);
- if (strings == null) {
- strings = new LinkedList<>();
- headers.put(headerNameLC, strings);
- }
- strings.add(head);
- }
+ ImmutableList<Pair<String, String>> headsAndLines = Streams.stream(Iterators.forEnumeration(message.getAllHeaderLines()))
+ .map(Throwing.function(this::extractHeaderLine).sneakyThrow())
+ .collect(Guavate.toImmutableList());
+
+ fields = headsAndLines
+ .stream()
+ .map(Pair::getKey)
+ .collect(Guavate.toImmutableList());
+
+ headers = headsAndLines
+ .stream()
+ .collect(Guavate.toImmutableListMultimap(
+ pair -> pair.getKey().toLowerCase(Locale.US),
+ Pair::getValue));
}
public List<String> getFields() {
@@ -64,6 +65,14 @@ final class MimeMessageHeaders implements Headers {
}
public List<String> getFields(String name) {
- return headers.get(name.toLowerCase());
+ return headers.get(name.toLowerCase(Locale.US));
+ }
+
+ private Pair<String, String> extractHeaderLine(String header) throws MessagingException {
+ int fieldSeperatorPosition = header.indexOf(':');
+ if (fieldSeperatorPosition <= 0) {
+ throw new MessagingException("Bad header line: " + header);
+ }
+ return Pair.of(header.substring(0, fieldSeperatorPosition).trim(), header);
}
}
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org
[james-project] 14/14: JAMES-1975 Be able to set either a
privateKey of a privateKeyFilepath in DKIM mailet
Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rouazana pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git
commit b60420aaff18e5e3b7c2186bb69eadbd477553f0
Author: Gautier DI FOLCO <gd...@linagora.com>
AuthorDate: Wed May 22 12:06:19 2019 +0200
JAMES-1975 Be able to set either a privateKey of a privateKeyFilepath in DKIM mailet
---
.../org/apache/james/jdkim/mailets/DKIMSign.java | 87 +++++++++++++++-------
.../apache/james/jdkim/mailets/DKIMSignTest.java | 12 +--
2 files changed, 67 insertions(+), 32 deletions(-)
diff --git a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMSign.java b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMSign.java
index 3917f2e..b3aec11 100644
--- a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMSign.java
+++ b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMSign.java
@@ -19,6 +19,7 @@
package org.apache.james.jdkim.mailets;
+import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@@ -32,6 +33,7 @@ import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPrivateCrtKeySpec;
import java.util.Collections;
import java.util.List;
+import java.util.Optional;
import javax.mail.Header;
import javax.mail.MessagingException;
@@ -58,19 +60,44 @@ import com.github.fge.lambdas.Throwing;
* This mailet sign a message using the DKIM protocol
* If the privateKey is encoded using a password then you can pass
* the password as privateKeyPassword parameter.
- *
- * Sample configuration:
- *
+ *
+ * Sample configuration with inlined private key:
+ *
* <pre><code>
* <mailet match="All" class="DKIMSign">
* <signatureTemplate>v=1; s=selector; d=example.com; h=from:to:received:received; a=rsa-sha256; bh=; b=;</signatureTemplate>
- * <privateKey>dkim-signing.pem</privateKey>
+ * <privateKey>
+ * -----BEGIN RSA PRIVATE KEY-----
+ * MIICXAIBAAKBgQDYDaYKXzwVYwqWbLhmuJ66aTAN8wmDR+rfHE8HfnkSOax0oIoT
+ * M5zquZrTLo30870YMfYzxwfB6j/Nz3QdwrUD/t0YMYJiUKyWJnCKfZXHJBJ+yfRH
+ * r7oW+UW3cVo9CG2bBfIxsInwYe175g9UjyntJpWueqdEIo1c2bhv9Mp66QIDAQAB
+ * AoGBAI8XcwnZi0Sq5N89wF+gFNhnREFo3rsJDaCY8iqHdA5DDlnr3abb/yhipw0I
+ * /1HlgC6fIG2oexXOXFWl+USgqRt1kTt9jXhVFExg8mNko2UelAwFtsl8CRjVcYQO
+ * cedeH/WM/mXjg2wUqqZenBmlKlD6vNb70jFJeVaDJ/7n7j8BAkEA9NkH2D4Zgj/I
+ * OAVYccZYH74+VgO0e7VkUjQk9wtJ2j6cGqJ6Pfj0roVIMUWzoBb8YfErR8l6JnVQ
+ * bfy83gJeiQJBAOHk3ow7JjAn8XuOyZx24KcTaYWKUkAQfRWYDFFOYQF4KV9xLSEt
+ * ycY0kjsdxGKDudWcsATllFzXDCQF6DTNIWECQEA52ePwTjKrVnLTfCLEG4OgHKvl
+ * Zud4amthwDyJWoMEH2ChNB2je1N4JLrABOE+hk+OuoKnKAKEjWd8f3Jg/rkCQHj8
+ * mQmogHqYWikgP/FSZl518jV48Tao3iXbqvU9Mo2T6yzYNCCqIoDLFWseNVnCTZ0Q
+ * b+IfiEf1UeZVV5o4J+ECQDatNnS3V9qYUKjj/krNRD/U0+7eh8S2ylLqD3RlSn9K
+ * tYGRMgAtUXtiOEizBH6bd/orzI9V9sw8yBz+ZqIH25Q=
+ * -----END RSA PRIVATE KEY-----
+ * </privateKey>
* </mailet>
* </code></pre>
- *
- * By default the mailet assume that Javamail will convert LF to CRLF when sending
- * so will compute the hash using converted newlines. If you don't want this
- * behaviout then set forceCRLF attribute to false.
+ *
+ * Sample configuration with file-provided private key:
+ *
+ * <pre><code>
+ * <mailet match="All" class="DKIMSign">
+ * <signatureTemplate>v=1; s=selector; d=example.com; h=from:to:received:received; a=rsa-sha256; bh=; b=;</signatureTemplate>
+ * <privateKeyFilepath>dkim-signing.pem</privateKeyFilepath>
+ * </mailet>
+ * </code></pre>
+ *
+ * By default the mailet assume that Javamail will convert LF to CRLF when sending
+ * so will compute the hash using converted newlines. If you don't want this
+ * behaviour then set forceCRLF attribute to false.
*/
public class DKIMSign extends GenericMailet {
@@ -94,13 +121,18 @@ public class DKIMSign extends GenericMailet {
public void init() throws MessagingException {
signatureTemplate = getInitParameter("signatureTemplate");
- String privateKeyFilePath = getInitParameter("privateKey");
- String privateKeyPassword = getInitParameter("privateKeyPassword", null);
+ Optional<String> privateKeyPassword = getInitParameterAsOptional("privateKeyPassword");
forceCRLF = getInitParameter("forceCRLF", true);
try {
- char[] passphrase = privateKeyPassword != null ? privateKeyPassword.toCharArray() : null;
- privateKey = extractPrivateKey(ClassLoader.getSystemResourceAsStream(privateKeyFilePath), passphrase);
+ char[] passphrase = privateKeyPassword.map(String::toCharArray).orElse(null);
+ InputStream pem = getInitParameterAsOptional("privateKey")
+ .map(String::getBytes)
+ .map(ByteArrayInputStream::new)
+ .map(byteArrayInputStream -> (InputStream) byteArrayInputStream)
+ .orElseGet(Throwing.supplier(() -> ClassLoader.getSystemResourceAsStream(getInitParameter("privateKeyFilepath"))).sneakyThrow());
+
+ privateKey = extractPrivateKey(pem, passphrase);
} catch (NoSuchAlgorithmException e) {
throw new MessagingException("Unknown private key algorythm: " + e.getMessage(), e);
} catch (InvalidKeySpecException e) {
@@ -160,21 +192,24 @@ public class DKIMSign extends GenericMailet {
private PrivateKey extractPrivateKey(InputStream rawKey, char[] passphrase) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
- PEMParser pemParser = new PEMParser(new InputStreamReader(rawKey));
- Object pemObject = pemParser.readObject();
- JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
- KeyPair keyPair;
- if (pemObject instanceof PEMEncryptedKeyPair) {
- PEMEncryptedKeyPair pemEncryptedKeyPair = (PEMEncryptedKeyPair) pemObject;
- PEMDecryptorProvider decProv = new JcePEMDecryptorProviderBuilder().build(passphrase);
- keyPair = converter.getKeyPair(pemEncryptedKeyPair.decryptKeyPair(decProv));
- } else {
- keyPair = converter.getKeyPair((PEMKeyPair) pemObject);
- }
+ try (InputStreamReader pemReader = new InputStreamReader(rawKey)) {
+ try (PEMParser pemParser = new PEMParser(pemReader)) {
+ Object pemObject = pemParser.readObject();
+ JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
+ KeyPair keyPair;
+ if (pemObject instanceof PEMEncryptedKeyPair) {
+ PEMEncryptedKeyPair pemEncryptedKeyPair = (PEMEncryptedKeyPair) pemObject;
+ PEMDecryptorProvider decProv = new JcePEMDecryptorProviderBuilder().build(passphrase);
+ keyPair = converter.getKeyPair(pemEncryptedKeyPair.decryptKeyPair(decProv));
+ } else {
+ keyPair = converter.getKeyPair((PEMKeyPair) pemObject);
+ }
- KeyFactory keyFac = KeyFactory.getInstance("RSA");
- RSAPrivateCrtKeySpec privateKeySpec = keyFac.getKeySpec(keyPair.getPrivate(), RSAPrivateCrtKeySpec.class);
+ KeyFactory keyFac = KeyFactory.getInstance("RSA");
+ RSAPrivateCrtKeySpec privateKeySpec = keyFac.getKeySpec(keyPair.getPrivate(), RSAPrivateCrtKeySpec.class);
- return keyFac.generatePrivate(privateKeySpec);
+ return keyFac.generatePrivate(privateKeySpec);
+ }
+ }
}
}
diff --git a/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMSignTest.java b/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMSignTest.java
index 3ccefa7..305ff5f 100644
--- a/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMSignTest.java
+++ b/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMSignTest.java
@@ -66,7 +66,7 @@ public class DKIMSignTest {
.setProperty(
"signatureTemplate",
"v=1; s=selector; d=example.com; h=from:to:received:received; a=rsa-sha256; bh=; b=;")
- .setProperty("privateKey", TESTING_PEM_FILE)
+ .setProperty("privateKeyFilepath", TESTING_PEM_FILE)
.build();
mailet.init(mci);
@@ -115,7 +115,7 @@ public class DKIMSignTest {
.setProperty(
"signatureTemplate",
"v=1; t=" + ((System.currentTimeMillis() / 1000) + 1000) + "; s=selector; d=example.com; h=from:to:received:received; a=rsa-sha256; bh=; b=;")
- .setProperty("privateKey", TESTING_PEM_FILE)
+ .setProperty("privateKeyFilepath", TESTING_PEM_FILE)
.build();
mailet.init(mci);
@@ -161,7 +161,7 @@ public class DKIMSignTest {
.setProperty(
"signatureTemplate",
"v=1; t=; s=selector; d=example.com; h=from:to:received:received; a=rsa-sha256; bh=; b=;")
- .setProperty("privateKey", TESTING_PEM_FILE)
+ .setProperty("privateKeyFilepath", TESTING_PEM_FILE)
.build();
mailet.init(mci);
@@ -214,7 +214,7 @@ public class DKIMSignTest {
.setProperty(
"signatureTemplate",
"v=1; s=selector; d=example.com; h=from:to:received:received; a=rsa-sha256; bh=; b=;")
- .setProperty("privateKey", TESTING_PEM_FILE)
+ .setProperty("privateKeyFilepath", TESTING_PEM_FILE)
.build();
mailet.init(mci);
@@ -261,7 +261,7 @@ public class DKIMSignTest {
.setProperty(
"signatureTemplate",
"v=1; s=selector; d=example.com; h=from:to:received:received; a=rsa-sha256; bh=; b=;")
- .setProperty("privateKey", TESTING_PEM_FILE)
+ .setProperty("privateKeyFilepath", TESTING_PEM_FILE)
.build();
Mail mail = FakeMail.builder()
@@ -309,7 +309,7 @@ public class DKIMSignTest {
.setProperty(
"signatureTemplate",
"v=1; s=selector; d=example.com; h=from:to:received:received; a=rsa-sha256; bh=; b=;")
- .setProperty("privateKey", TESTING_PEM_FILE)
+ .setProperty("privateKeyFilepath", TESTING_PEM_FILE)
.build();
Mail mail = FakeMail.builder()
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org
[james-project] 08/14: JAMES-1975 Update dkim to JUnit 5 and new
mailet API
Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rouazana pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 5cff6146f9c16ea31ebc9219d8322f44fbfb2430
Author: Gautier DI FOLCO <gd...@linagora.com>
AuthorDate: Tue May 21 14:07:31 2019 +0200
JAMES-1975 Update dkim to JUnit 5 and new mailet API
---
server/mailet/dkim/pom.xml | 57 ++++++++++++++++++----
.../org/apache/james/jdkim/mailets/DKIMVerify.java | 17 ++++---
.../apache/james/jdkim/mailets/DKIMSignTest.java | 34 ++++++-------
.../apache/james/jdkim/mailets/DKIMVerifyTest.java | 40 ++++++++-------
4 files changed, 98 insertions(+), 50 deletions(-)
diff --git a/server/mailet/dkim/pom.xml b/server/mailet/dkim/pom.xml
index 3dfbbf6..1db8160 100644
--- a/server/mailet/dkim/pom.xml
+++ b/server/mailet/dkim/pom.xml
@@ -56,13 +56,25 @@
<version>${jdkim.version}</version>
<exclusions>
<exclusion>
- <groupId>org.apache.james</groupId>
+ <groupId>${james.groupId}</groupId>
+ <artifactId>apache-mailet</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>${james.groupId}</groupId>
<artifactId>apache-mime4j-core</artifactId>
</exclusion>
<exclusion>
- <groupId>org.apache.james</groupId>
+ <groupId>${james.groupId}</groupId>
<artifactId>apache-mime4j-dom</artifactId>
</exclusion>
+ <exclusion>
+ <groupId>org.apache.geronimo.javamail</groupId>
+ <artifactId>geronimo-javamail_1.4_mail</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>log4j</groupId>
+ <artifactId>log4j</artifactId>
+ </exclusion>
</exclusions>
</dependency>
<dependency>
@@ -73,25 +85,52 @@
<scope>test</scope>
<exclusions>
<exclusion>
- <groupId>org.apache.james</groupId>
+ <groupId>${james.groupId}</groupId>
+ <artifactId>apache-mailet</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>${james.groupId}</groupId>
<artifactId>apache-mime4j-core</artifactId>
</exclusion>
<exclusion>
- <groupId>org.apache.james</groupId>
+ <groupId>${james.groupId}</groupId>
<artifactId>apache-mime4j-dom</artifactId>
</exclusion>
+ <exclusion>
+ <groupId>org.apache.geronimo.javamail</groupId>
+ <artifactId>geronimo-javamail_1.4_mail</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>log4j</groupId>
+ <artifactId>log4j</artifactId>
+ </exclusion>
</exclusions>
</dependency>
<dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <scope>test</scope>
- </dependency>
- <dependency>
<groupId>ca.juliusdavies</groupId>
<artifactId>not-yet-commons-ssl</artifactId>
<version>0.3.11</version>
</dependency>
+ <dependency>
+ <groupId>org.assertj</groupId>
+ <artifactId>assertj-core</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter-engine</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.junit.platform</groupId>
+ <artifactId>junit-platform-launcher</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.junit.vintage</groupId>
+ <artifactId>junit-vintage-engine</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
diff --git a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMVerify.java b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMVerify.java
index baddb09..a98c12b 100644
--- a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMVerify.java
+++ b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMVerify.java
@@ -27,11 +27,15 @@ import java.util.Optional;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
+import com.google.common.annotations.VisibleForTesting;
import org.apache.james.jdkim.DKIMVerifier;
import org.apache.james.jdkim.api.BodyHasher;
import org.apache.james.jdkim.api.Headers;
import org.apache.james.jdkim.api.SignatureRecord;
import org.apache.james.jdkim.exceptions.FailException;
+import org.apache.mailet.Attribute;
+import org.apache.mailet.AttributeName;
+import org.apache.mailet.AttributeValue;
import org.apache.mailet.Mail;
import org.apache.mailet.base.GenericMailet;
@@ -50,9 +54,9 @@ import org.apache.mailet.base.GenericMailet;
*/
public class DKIMVerify extends GenericMailet {
- public static final String DKIM_AUTH_RESULT_ATTRIBUTE = "jDKIM.AUTHRESULT";
+ public static final AttributeName DKIM_AUTH_RESULT = AttributeName.of("jDKIM.AUTHRESULT");
- protected DKIMVerifier verifier = null;
+ @VisibleForTesting DKIMVerifier verifier = null;
private boolean forceCRLF;
@Override
@@ -67,7 +71,7 @@ public class DKIMVerify extends GenericMailet {
List<SignatureRecord> res = verify(verifier, message, forceCRLF);
if (res == null || res.isEmpty()) {
// neutral
- mail.setAttribute(DKIM_AUTH_RESULT_ATTRIBUTE, "neutral (no signatures)");
+ mail.setAttribute(new Attribute(DKIM_AUTH_RESULT, AttributeValue.of("neutral (no signatures)")));
} else {
// pass
StringBuilder msg = new StringBuilder();
@@ -78,18 +82,19 @@ public class DKIMVerify extends GenericMailet {
msg.append(rec.getIdentity().toString());
msg.append(")");
}
- mail.setAttribute(DKIM_AUTH_RESULT_ATTRIBUTE, msg.toString());
+ mail.setAttribute(new Attribute(DKIM_AUTH_RESULT, AttributeValue.of(msg.toString())));
}
} catch (FailException e) {
// fail
String relatedRecordIdentity = Optional.ofNullable(e.getRelatedRecordIdentity())
.map(value -> "identity" + value + ":")
.orElse("");
- mail.setAttribute(DKIM_AUTH_RESULT_ATTRIBUTE, "fail (" + relatedRecordIdentity + e.getMessage() + ")");
+ mail.setAttribute(new Attribute(DKIM_AUTH_RESULT, AttributeValue.of("fail (" + relatedRecordIdentity + e.getMessage() + ")")));
}
}
- protected static List<SignatureRecord> verify(DKIMVerifier verifier, MimeMessage message, boolean forceCRLF)
+ @VisibleForTesting
+ static List<SignatureRecord> verify(DKIMVerifier verifier, MimeMessage message, boolean forceCRLF)
throws MessagingException, FailException {
Headers headers = new MimeMessageHeaders(message);
BodyHasher bh = verifier.newBodyHasher(headers);
diff --git a/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMSignTest.java b/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMSignTest.java
index 731941a..ee69b4c 100644
--- a/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMSignTest.java
+++ b/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMSignTest.java
@@ -19,8 +19,8 @@
package org.apache.james.jdkim.mailets;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Fail.fail;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@@ -45,8 +45,8 @@ import org.apache.mailet.Mailet;
import org.apache.mailet.base.test.FakeMail;
import org.apache.mailet.base.test.FakeMailContext;
import org.apache.mailet.base.test.FakeMailetConfig;
-import org.junit.Assert;
-import org.junit.Test;
+
+import org.junit.jupiter.api.Test;
public class DKIMSignTest {
@@ -68,7 +68,7 @@ public class DKIMSignTest {
private static final FakeMailContext FAKE_MAIL_CONTEXT = FakeMailContext.defaultContext();
@Test
- public void testDKIMSign() throws MessagingException, IOException,
+ void testDKIMSign() throws MessagingException, IOException,
FailException {
String message = "Received: by 10.XX.XX.12 with SMTP id dfgskldjfhgkljsdfhgkljdhfg;\r\n\tTue, 06 Oct 2009 07:37:34 -0700 (PDT)\r\nReturn-Path: <bo...@example.com>\r\nReceived: from example.co.uk (example.co.uk [XX.XXX.125.19])\r\n\tby mx.example.com with ESMTP id dgdfgsdfgsd.97.2009.10.06.07.37.32;\r\n\tTue, 06 Oct 2009 07:37:32 -0700 (PDT)\r\nFrom: apache@bago.org\r\nTo: apache@bago.org\r\n\r\nbody\r\nprova\r\n";
@@ -111,13 +111,13 @@ public class DKIMSignTest {
MockPublicKeyRecordRetriever mockPublicKeyRecordRetriever)
throws MessagingException, FailException {
List<SignatureRecord> signs = DKIMVerify.verify(new DKIMVerifier(mockPublicKeyRecordRetriever), new MimeMessage(Session.getDefaultInstance(new Properties()), new ByteArrayInputStream(rawMessage.toByteArray())), true);
- assertNotNull(signs);
- assertEquals(1, signs.size());
+ assertThat(signs).hasSize(1);
+
return signs;
}
@Test
- public void testDKIMSignFuture() throws MessagingException, IOException,
+ void testDKIMSignFuture() throws MessagingException, IOException,
FailException {
String message = "Received: by 10.XX.XX.12 with SMTP id dfgskldjfhgkljsdfhgkljdhfg;\r\n\tTue, 06 Oct 2009 07:37:34 -0700 (PDT)\r\nReturn-Path: <bo...@example.com>\r\nReceived: from example.co.uk (example.co.uk [XX.XXX.125.19])\r\n\tby mx.example.com with ESMTP id dgdfgsdfgsd.97.2009.10.06.07.37.32;\r\n\tTue, 06 Oct 2009 07:37:32 -0700 (PDT)\r\nFrom: apache@bago.org\r\nTo: apache@bago.org\r\n\r\nbody\r\nprova\r\n";
@@ -155,7 +155,7 @@ public class DKIMSignTest {
"selector", "example.com");
try {
verify(rawMessage, mockPublicKeyRecordRetriever);
- Assert.fail("Expecting signature to be ignored");
+ fail("Expecting signature to be ignored");
} catch (PermFailException e) {
// signature ignored, so fail for missing signatures.
}
@@ -163,7 +163,7 @@ public class DKIMSignTest {
@Test
- public void testDKIMSignTime() throws MessagingException, IOException,
+ void testDKIMSignTime() throws MessagingException, IOException,
FailException {
String message = "Received: by 10.XX.XX.12 with SMTP id dfgskldjfhgkljsdfhgkljdhfg;\r\n\tTue, 06 Oct 2009 07:37:34 -0700 (PDT)\r\nReturn-Path: <bo...@example.com>\r\nReceived: from example.co.uk (example.co.uk [XX.XXX.125.19])\r\n\tby mx.example.com with ESMTP id dgdfgsdfgsd.97.2009.10.06.07.37.32;\r\n\tTue, 06 Oct 2009 07:37:32 -0700 (PDT)\r\nFrom: apache@bago.org\r\nTo: apache@bago.org\r\n\r\nbody\r\nprova\r\n";
@@ -204,15 +204,15 @@ public class DKIMSignTest {
List<SignatureRecord> rs = verify(rawMessage, mockPublicKeyRecordRetriever);
// check we have a valued signatureTimestamp
- Assert.assertNotNull(rs.get(0).getSignatureTimestamp());
+ assertThat(rs.get(0).getSignatureTimestamp()).isNotNull();
long ref = System.currentTimeMillis() / 1000;
// Chech that the signature timestamp is in the past 60 seconds.
- Assert.assertTrue(rs.get(0).getSignatureTimestamp() <= ref);
- Assert.assertTrue(rs.get(0).getSignatureTimestamp() >= ref - 60);
+ assertThat(rs.get(0).getSignatureTimestamp() <= ref).isTrue();
+ assertThat(rs.get(0).getSignatureTimestamp() >= ref - 60).isTrue();
}
@Test
- public void testDKIMSignMessageAsText() throws MessagingException,
+ void testDKIMSignMessageAsText() throws MessagingException,
IOException, FailException {
MimeMessage mm = new MimeMessage(Session
.getDefaultInstance(new Properties()));
@@ -256,7 +256,7 @@ public class DKIMSignTest {
}
@Test
- public void testDKIMSignMessageAsObjectConvertedTo7Bit()
+ void testDKIMSignMessageAsObjectConvertedTo7Bit()
throws MessagingException, IOException, FailException {
MimeMessage mm = new MimeMessage(Session
.getDefaultInstance(new Properties()));
@@ -304,7 +304,7 @@ public class DKIMSignTest {
}
@Test
- public void testDKIMSignMessageAsObjectNotConverted()
+ void testDKIMSignMessageAsObjectNotConverted()
throws MessagingException, IOException, FailException {
MimeMessage mm = new MimeMessage(Session
.getDefaultInstance(new Properties()));
@@ -350,7 +350,7 @@ public class DKIMSignTest {
"selector", "example.com");
try {
verify(rawMessage, mockPublicKeyRecordRetriever);
- Assert.fail("Expected PermFail");
+ fail("Expected PermFail");
} catch (PermFailException e) {
}
diff --git a/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMVerifyTest.java b/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMVerifyTest.java
index 2011965..27650ab 100644
--- a/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMVerifyTest.java
+++ b/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMVerifyTest.java
@@ -19,7 +19,10 @@
package org.apache.james.jdkim.mailets;
+import static org.assertj.core.api.Assertions.assertThat;
+
import java.io.ByteArrayInputStream;
+import java.util.Optional;
import java.util.Properties;
import javax.mail.Session;
@@ -27,65 +30,66 @@ import javax.mail.internet.MimeMessage;
import org.apache.james.jdkim.DKIMVerifier;
import org.apache.james.jdkim.MockPublicKeyRecordRetriever;
+import org.apache.mailet.AttributeUtils;
import org.apache.mailet.Mail;
import org.apache.mailet.Mailet;
import org.apache.mailet.base.test.FakeMail;
import org.apache.mailet.base.test.FakeMailContext;
import org.apache.mailet.base.test.FakeMailetConfig;
-import org.junit.Assert;
-import org.junit.Test;
+
+import org.junit.jupiter.api.Test;
public class DKIMVerifyTest {
@Test
- public void testDKIMVerifyPass() throws Exception {
+ void testDKIMVerifyPass() throws Exception {
String message = "DKIM-Signature: v=1; d=example.com; t=1284762805; b=ZFfwSIzTQM7k9syRnl9VfQh0/dr99euvBe1gn/DiTrnEZjxyjzQBD2MMvowVdbHpPMtSjtCtehU9zZ3urXmj5iHKujpEkP92FEKinzElkQ2eT2zoxdg1zByPHsKPX+KjrBespAJcO2k052aOK5kIBFxpQumP4aiW7ZklBKSWMBk=; s=selector; a=rsa-sha256; bh=rHOD7fd9xnNxK7OSl5ellpQVF14NNFbOIizqtUMhnio=; h=from:to:received:received;\r\n"
+ "Received: by 10.XX.XX.12 with SMTP id dfgskldjfhgkljsdfhgkljdhfg;\r\n\tTue, 06 Oct 2009 07:37:34 -0700 (PDT)\r\nReturn-Path: <bo...@example.com>\r\nReceived: from example.co.uk (example.co.uk [XX.XXX.125.19])\r\n\tby mx.example.com with ESMTP id dgdfgsdfgsd.97.2009.10.06.07.37.32;\r\n\tTue, 06 Oct 2009 07:37:32 -0700 (PDT)\r\nFrom: apache@bago.org\r\nTo: apache@bago.org\r\n\r\nbody\r\nprova\r\n";
Mail mail = process(message);
- String attr = (String) mail.getAttribute(DKIMVerify.DKIM_AUTH_RESULT_ATTRIBUTE);
- Assert.assertNotNull(attr);
- Assert.assertTrue(attr.startsWith("pass"));
+ Optional<String> attr = AttributeUtils.getValueAndCastFromMail(mail, DKIMVerify.DKIM_AUTH_RESULT, String.class);
+ assertThat(attr)
+ .hasValueSatisfying(result -> assertThat(result).startsWith("pass"));
}
@Test
- public void testDKIMVerifyFail() throws Exception {
+ void testDKIMVerifyFail() throws Exception {
// altered message body
String message = "DKIM-Signature: v=1; d=example.com; t=1284762805; b=ZFfwSIzTQM7k9syRnl9VfQh0/dr99euvBe1gn/DiTrnEZjxyjzQBD2MMvowVdbHpPMtSjtCtehU9zZ3urXmj5iHKujpEkP92FEKinzElkQ2eT2zoxdg1zByPHsKPX+KjrBespAJcO2k052aOK5kIBFxpQumP4aiW7ZklBKSWMBk=; s=selector; a=rsa-sha256; bh=rHOD7fd9xnNxK7OSl5ellpQVF14NNFbOIizqtUMhnio=; h=from:to:received:received;\r\n"
+ "Received: by 10.XX.XX.12 with SMTP id dfgskldjfhgkljsdfhgkljdhfg;\r\n\tTue, 06 Oct 2009 07:37:34 -0700 (PDT)\r\nReturn-Path: <bo...@example.com>\r\nReceived: from example.co.uk (example.co.uk [XX.XXX.125.19])\r\n\tby mx.example.com with ESMTP id dgdfgsdfgsd.97.2009.10.06.07.37.32;\r\n\tTue, 06 Oct 2009 07:37:32 -0700 (PDT)\r\nFrom: apache@bago.org\r\nTo: apache@bago.org\r\n\r\nbody\r\nprova altered\r\n";
Mail mail = process(message);
- String attr = (String) mail.getAttribute(DKIMVerify.DKIM_AUTH_RESULT_ATTRIBUTE);
- Assert.assertNotNull(attr);
- Assert.assertTrue(attr.startsWith("fail"));
+ Optional<String> attr = AttributeUtils.getValueAndCastFromMail(mail, DKIMVerify.DKIM_AUTH_RESULT, String.class);
+ assertThat(attr)
+ .hasValueSatisfying(result -> assertThat(result).startsWith("fail"));
}
@Test
- public void testDKIMVerifyFailInvalid() throws Exception {
+ void testDKIMVerifyFailInvalid() throws Exception {
// invalid version v=2
String message = "DKIM-Signature: v=2; d=example.com; t=1284762805; b=ZFfwSIzTQM7k9syRnl9VfQh0/dr99euvBe1gn/DiTrnEZjxyjzQBD2MMvowVdbHpPMtSjtCtehU9zZ3urXmj5iHKujpEkP92FEKinzElkQ2eT2zoxdg1zByPHsKPX+KjrBespAJcO2k052aOK5kIBFxpQumP4aiW7ZklBKSWMBk=; s=selector; a=rsa-sha256; bh=rHOD7fd9xnNxK7OSl5ellpQVF14NNFbOIizqtUMhnio=; h=from:to:received:received;\r\n"
+ "Received: by 10.XX.XX.12 with SMTP id dfgskldjfhgkljsdfhgkljdhfg;\r\n\tTue, 06 Oct 2009 07:37:34 -0700 (PDT)\r\nReturn-Path: <bo...@example.com>\r\nReceived: from example.co.uk (example.co.uk [XX.XXX.125.19])\r\n\tby mx.example.com with ESMTP id dgdfgsdfgsd.97.2009.10.06.07.37.32;\r\n\tTue, 06 Oct 2009 07:37:32 -0700 (PDT)\r\nFrom: apache@bago.org\r\nTo: apache@bago.org\r\n\r\nbody\r\nprova\r\n";
Mail mail = process(message);
- String attr = (String) mail.getAttribute(DKIMVerify.DKIM_AUTH_RESULT_ATTRIBUTE);
- Assert.assertNotNull(attr);
- Assert.assertTrue(attr.startsWith("fail"));
+ Optional<String> attr = AttributeUtils.getValueAndCastFromMail(mail, DKIMVerify.DKIM_AUTH_RESULT, String.class);
+ assertThat(attr)
+ .hasValueSatisfying(result -> assertThat(result).startsWith("fail"));
}
@Test
- public void testDKIMVerifyNeutral() throws Exception {
+ void testDKIMVerifyNeutral() throws Exception {
// no signatures!
String message = ""
+ "Received: by 10.XX.XX.12 with SMTP id dfgskldjfhgkljsdfhgkljdhfg;\r\n\tTue, 06 Oct 2009 07:37:34 -0700 (PDT)\r\nReturn-Path: <bo...@example.com>\r\nReceived: from example.co.uk (example.co.uk [XX.XXX.125.19])\r\n\tby mx.example.com with ESMTP id dgdfgsdfgsd.97.2009.10.06.07.37.32;\r\n\tTue, 06 Oct 2009 07:37:32 -0700 (PDT)\r\nFrom: apache@bago.org\r\nTo: apache@bago.org\r\n\r\nbody\r\nprova altered\r\n";
Mail mail = process(message);
- String attr = (String) mail.getAttribute(DKIMVerify.DKIM_AUTH_RESULT_ATTRIBUTE);
- Assert.assertNotNull(attr);
- Assert.assertTrue(attr.startsWith("neutral"));
+ Optional<String> attr = AttributeUtils.getValueAndCastFromMail(mail, DKIMVerify.DKIM_AUTH_RESULT, String.class);
+ assertThat(attr)
+ .hasValueSatisfying(result -> assertThat(result).startsWith("neutral"));
}
private Mail process(String message) throws Exception {
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org
[james-project] 09/14: JAMES-1975 Use released version of DKIM
Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rouazana pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git
commit b0cbfaef197f8271905d03d69982089609aaa8bd
Author: Gautier DI FOLCO <gd...@linagora.com>
AuthorDate: Tue May 21 16:11:34 2019 +0200
JAMES-1975 Use released version of DKIM
---
pom.xml | 12 ------------
server/mailet/dkim/pom.xml | 2 +-
2 files changed, 1 insertion(+), 13 deletions(-)
diff --git a/pom.xml b/pom.xml
index a8ed295..670f5c2 100644
--- a/pom.xml
+++ b/pom.xml
@@ -2754,18 +2754,6 @@
</dependencies>
</dependencyManagement>
- <repositories>
- <!-- apache.snapshot is needed for james-project SNAPSHOT, see JAMES-1470 -->
- <repository>
- <releases>
- <enabled>false</enabled>
- </releases>
- <id>apache.snapshots</id>
- <name>Apache Snapshot Repository</name>
- <url>http://repository.apache.org/snapshots</url>
- </repository>
- </repositories>
-
<build>
<pluginManagement>
<plugins>
diff --git a/server/mailet/dkim/pom.xml b/server/mailet/dkim/pom.xml
index 1db8160..3c26fd0 100644
--- a/server/mailet/dkim/pom.xml
+++ b/server/mailet/dkim/pom.xml
@@ -33,7 +33,7 @@
<description>Mailet adapter for the JDKIM library</description>
<properties>
- <jdkim.version>0.3-SNAPSHOT</jdkim.version>
+ <jdkim.version>0.2</jdkim.version>
</properties>
<dependencies>
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org
[james-project] 01/14: JAMES-1975 Move JDKIM mailet as a server
mailet
Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rouazana pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 3c79a9fc019c8ffd5d5167d8daf60f6b22b272d6
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Fri Aug 31 17:39:28 2018 +0700
JAMES-1975 Move JDKIM mailet as a server mailet
We introduce it in a separate maven module.
All code was just copied.
---
server/mailet/dkim/pom.xml | 106 ++++++
.../james/jdkim/mailets/CRLFOutputStream.java | 161 +++++++++
.../apache/james/jdkim/mailets/ConvertTo7Bit.java | 77 +++++
.../org/apache/james/jdkim/mailets/DKIMSign.java | 173 ++++++++++
.../org/apache/james/jdkim/mailets/DKIMVerify.java | 108 ++++++
.../jdkim/mailets/HeaderSkippingOutputStream.java | 71 ++++
.../james/jdkim/mailets/MimeMessageHeaders.java | 70 ++++
.../apache/james/jdkim/mailets/DKIMSignTest.java | 361 +++++++++++++++++++++
.../apache/james/jdkim/mailets/DKIMVerifyTest.java | 127 ++++++++
server/pom.xml | 2 +
10 files changed, 1256 insertions(+)
diff --git a/server/mailet/dkim/pom.xml b/server/mailet/dkim/pom.xml
new file mode 100644
index 0000000..4d4076a
--- /dev/null
+++ b/server/mailet/dkim/pom.xml
@@ -0,0 +1,106 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.james</groupId>
+ <artifactId>james-server</artifactId>
+ <version>3.4.0-SNAPSHOT</version>
+ <relativePath>../../pom.xml</relativePath>
+ </parent>
+
+ <artifactId>james-server-mailet-dkim</artifactId>
+ <name>Apache James :: Server :: Mailet :: DKIM</name>
+
+ <description>Mailet adapter for the JDKIM library</description>
+
+ <properties>
+ <jdkim.version>0.3-SNAPSHOT</jdkim.version>
+ </properties>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.james</groupId>
+ <artifactId>apache-mailet-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.james</groupId>
+ <artifactId>apache-mailet-base</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.james</groupId>
+ <artifactId>apache-mailet-test</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.james.jdkim</groupId>
+ <artifactId>apache-jdkim-library</artifactId>
+ <version>${jdkim.version}</version>
+ <exclusions>
+ <exclusion>
+ <groupId>org.apache.james</groupId>
+ <artifactId>apache-mime4j-core</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>org.apache.james</groupId>
+ <artifactId>apache-mime4j-dom</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.james.jdkim</groupId>
+ <artifactId>apache-jdkim-library</artifactId>
+ <version>${jdkim.version}</version>
+ <type>test-jar</type>
+ <scope>test</scope>
+ <exclusions>
+ <exclusion>
+ <groupId>org.apache.james</groupId>
+ <artifactId>apache-mime4j-core</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>org.apache.james</groupId>
+ <artifactId>apache-mime4j-dom</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ <dependency>
+ <groupId>commons-logging</groupId>
+ <artifactId>commons-logging</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>ca.juliusdavies</groupId>
+ <artifactId>not-yet-commons-ssl</artifactId>
+ <version>0.3.11</version>
+ </dependency>
+ <dependency>
+ <groupId>commons-codec</groupId>
+ <artifactId>commons-codec</artifactId>
+ <version>1.7</version>
+ </dependency>
+ </dependencies>
+
+</project>
diff --git a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/CRLFOutputStream.java b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/CRLFOutputStream.java
new file mode 100644
index 0000000..b686b72
--- /dev/null
+++ b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/CRLFOutputStream.java
@@ -0,0 +1,161 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+
+package org.apache.james.jdkim.mailets;
+
+import java.io.FilterOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+/**
+ * A Filter for use with SMTP or other protocols in which lines must end with
+ * CRLF. Converts every "isolated" occourency of \r or \n with \r\n
+ *
+ * RFC 2821 #2.3.7 mandates that line termination is CRLF, and that CR and LF
+ * must not be transmitted except in that pairing. If we get a naked LF, convert
+ * to CRLF.
+ *
+ */
+public class CRLFOutputStream extends FilterOutputStream {
+
+ /**
+ * Counter for number of last (0A or 0D).
+ */
+ protected int statusLast;
+
+ protected final static int LAST_WAS_OTHER = 0;
+
+ protected final static int LAST_WAS_CR = 1;
+
+ protected final static int LAST_WAS_LF = 2;
+
+ protected boolean startOfLine = true;
+
+ /**
+ * Constructor that wraps an OutputStream.
+ *
+ * @param out
+ * the OutputStream to be wrapped
+ */
+ public CRLFOutputStream(OutputStream out) {
+ super(out);
+ statusLast = LAST_WAS_LF; // we already assume a CRLF at beginning
+ // (otherwise TOP would not work correctly
+ // !)
+ }
+
+ /**
+ * Writes a byte to the stream Fixes any naked CR or LF to the RFC 2821
+ * mandated CFLF pairing.
+ *
+ * @param b
+ * the byte to write
+ *
+ * @throws IOException
+ * if an error occurs writing the byte
+ */
+ public void write(int b) throws IOException {
+ switch (b) {
+ case '\r':
+ out.write('\r');
+ out.write('\n');
+ startOfLine = true;
+ statusLast = LAST_WAS_CR;
+ break;
+ case '\n':
+ if (statusLast != LAST_WAS_CR) {
+ out.write('\r');
+ out.write('\n');
+ startOfLine = true;
+ }
+ statusLast = LAST_WAS_LF;
+ break;
+ default:
+ // we're no longer at the start of a line
+ out.write(b);
+ startOfLine = false;
+ statusLast = LAST_WAS_OTHER;
+ break;
+ }
+ }
+
+ /**
+ * Provides an extension point for ExtraDotOutputStream to be able to add
+ * dots at the beginning of new lines.
+ *
+ * @see java.io.FilterOutputStream#write(byte[], int, int)
+ */
+ protected void writeChunk(byte buffer[], int offset, int length)
+ throws IOException {
+ out.write(buffer, offset, length);
+ }
+
+ /**
+ * @see java.io.FilterOutputStream#write(byte[], int, int)
+ */
+ public synchronized void write(byte buffer[], int offset, int length)
+ throws IOException {
+ /* optimized */
+ int lineStart = offset;
+ for (int i = offset; i < length + offset; i++) {
+ switch (buffer[i]) {
+ case '\r':
+ // CR case. Write down the last line
+ // and position the new lineStart at the next char
+ writeChunk(buffer, lineStart, i - lineStart);
+ out.write('\r');
+ out.write('\n');
+ startOfLine = true;
+ lineStart = i + 1;
+ statusLast = LAST_WAS_CR;
+ break;
+ case '\n':
+ if (statusLast != LAST_WAS_CR) {
+ writeChunk(buffer, lineStart, i - lineStart);
+ out.write('\r');
+ out.write('\n');
+ startOfLine = true;
+ }
+ lineStart = i + 1;
+ statusLast = LAST_WAS_LF;
+ break;
+ default:
+ statusLast = LAST_WAS_OTHER;
+ }
+ }
+ if (length + offset > lineStart) {
+ writeChunk(buffer, lineStart, length + offset - lineStart);
+ startOfLine = false;
+ }
+ }
+
+ /**
+ * Ensure that the stream is CRLF terminated.
+ *
+ * @throws IOException
+ * if an error occurs writing the byte
+ */
+ public void checkCRLFTerminator() throws IOException {
+ if (statusLast == LAST_WAS_OTHER) {
+ out.write('\r');
+ out.write('\n');
+ statusLast = LAST_WAS_CR;
+ }
+ }
+}
diff --git a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/ConvertTo7Bit.java b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/ConvertTo7Bit.java
new file mode 100644
index 0000000..53a4e01
--- /dev/null
+++ b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/ConvertTo7Bit.java
@@ -0,0 +1,77 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+
+package org.apache.james.jdkim.mailets;
+
+import java.io.IOException;
+
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeMessage;
+import javax.mail.internet.MimeMultipart;
+import javax.mail.internet.MimePart;
+
+import org.apache.mailet.Mail;
+import org.apache.mailet.base.GenericMailet;
+
+/**
+ * Make sure the message stream is 7bit. Every 8bit part is encoded to
+ * quoted-printable or base64 and the message is saved.
+ */
+public class ConvertTo7Bit extends GenericMailet {
+
+ public void service(Mail mail) throws MessagingException {
+ MimeMessage message = mail.getMessage();
+ try {
+ convertTo7Bit(message);
+ message.saveChanges();
+ } catch (IOException e) {
+ throw new MessagingException("IOException converting message to 7bit: " + e.getMessage(), e);
+ }
+ }
+
+ /**
+ * Converts a message to 7 bit.
+ */
+ private void convertTo7Bit(MimePart part) throws MessagingException, IOException {
+ if (part.isMimeType("multipart/*")) {
+ MimeMultipart parts = (MimeMultipart) part.getContent();
+ int count = parts.getCount();
+ for (int i = 0; i < count; i++) {
+ convertTo7Bit((MimePart) parts.getBodyPart(i));
+ }
+ } else if ("8bit".equals(part.getEncoding())) {
+ // The content may already be in encoded the form (likely with mail
+ // created from a stream). In that case, just changing the encoding
+ // to quoted-printable will mangle the result when this is
+ // transmitted.
+ // We must first convert the content into its native format, set it
+ // back, and only THEN set the transfer encoding to force the
+ // content to be encoded appropriately.
+
+ // if the part doesn't contain text it will be base64 encoded.
+ String contentTransferEncoding = part.isMimeType("text/*") ? "quoted-printable" : "base64";
+ part.setContent(part.getContent(), part.getContentType());
+ part.setHeader("Content-Transfer-Encoding", contentTransferEncoding);
+ part.addHeader("X-MIME-Autoconverted", "from 8bit to "
+ + contentTransferEncoding + " by "
+ + getMailetContext().getServerInfo());
+ }
+ }
+
+}
diff --git a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMSign.java b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMSign.java
new file mode 100644
index 0000000..57cc2b0
--- /dev/null
+++ b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMSign.java
@@ -0,0 +1,173 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+
+package org.apache.james.jdkim.mailets;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.security.GeneralSecurityException;
+import java.security.NoSuchAlgorithmException;
+import java.security.PrivateKey;
+import java.security.spec.InvalidKeySpecException;
+import java.util.Enumeration;
+import java.util.LinkedList;
+import java.util.List;
+
+import javax.mail.Header;
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeMessage;
+
+import org.apache.commons.ssl.PKCS8Key;
+import org.apache.james.jdkim.DKIMSigner;
+import org.apache.james.jdkim.api.BodyHasher;
+import org.apache.james.jdkim.api.Headers;
+import org.apache.james.jdkim.api.SignatureRecord;
+import org.apache.james.jdkim.exceptions.PermFailException;
+import org.apache.mailet.Mail;
+import org.apache.mailet.base.GenericMailet;
+
+/**
+ * This mailet sign a message using the DKIM protocol
+ * If the privateKey is encoded using a password then you can pass
+ * the password as privateKeyPassword parameter.
+ *
+ * Sample configuration:
+ *
+ * <pre><code>
+ * <mailet match="All" class="DKIMSign">
+ * <signatureTemplate>v=1; s=selector; d=example.com; h=from:to:received:received; a=rsa-sha256; bh=; b=;</signatureTemplate>
+ * <privateKey>
+ * -----BEGIN RSA PRIVATE KEY-----
+ * MIICXAIBAAKBgQDYDaYKXzwVYwqWbLhmuJ66aTAN8wmDR+rfHE8HfnkSOax0oIoT
+ * M5zquZrTLo30870YMfYzxwfB6j/Nz3QdwrUD/t0YMYJiUKyWJnCKfZXHJBJ+yfRH
+ * r7oW+UW3cVo9CG2bBfIxsInwYe175g9UjyntJpWueqdEIo1c2bhv9Mp66QIDAQAB
+ * AoGBAI8XcwnZi0Sq5N89wF+gFNhnREFo3rsJDaCY8iqHdA5DDlnr3abb/yhipw0I
+ * /1HlgC6fIG2oexXOXFWl+USgqRt1kTt9jXhVFExg8mNko2UelAwFtsl8CRjVcYQO
+ * cedeH/WM/mXjg2wUqqZenBmlKlD6vNb70jFJeVaDJ/7n7j8BAkEA9NkH2D4Zgj/I
+ * OAVYccZYH74+VgO0e7VkUjQk9wtJ2j6cGqJ6Pfj0roVIMUWzoBb8YfErR8l6JnVQ
+ * bfy83gJeiQJBAOHk3ow7JjAn8XuOyZx24KcTaYWKUkAQfRWYDFFOYQF4KV9xLSEt
+ * ycY0kjsdxGKDudWcsATllFzXDCQF6DTNIWECQEA52ePwTjKrVnLTfCLEG4OgHKvl
+ * Zud4amthwDyJWoMEH2ChNB2je1N4JLrABOE+hk+OuoKnKAKEjWd8f3Jg/rkCQHj8
+ * mQmogHqYWikgP/FSZl518jV48Tao3iXbqvU9Mo2T6yzYNCCqIoDLFWseNVnCTZ0Q
+ * b+IfiEf1UeZVV5o4J+ECQDatNnS3V9qYUKjj/krNRD/U0+7eh8S2ylLqD3RlSn9K
+ * tYGRMgAtUXtiOEizBH6bd/orzI9V9sw8yBz+ZqIH25Q=
+ * -----END RSA PRIVATE KEY-----
+ * </privateKey>
+ * </mailet>
+ * </code></pre>
+ *
+ * By default the mailet assume that Javamail will convert LF to CRLF when sending
+ * so will compute the hash using converted newlines. If you don't want this
+ * behaviout then set forceCRLF attribute to false.
+ */
+public class DKIMSign extends GenericMailet {
+
+ private String signatureTemplate;
+ private PrivateKey privateKey;
+ private boolean forceCRLF;
+
+ /**
+ * @return the signatureTemplate
+ */
+ protected String getSignatureTemplate() {
+ return signatureTemplate;
+ }
+
+ /**
+ * @return the privateKey
+ */
+ protected PrivateKey getPrivateKey() {
+ return privateKey;
+ }
+
+ public void init() throws MessagingException {
+ signatureTemplate = getInitParameter("signatureTemplate");
+ String privateKeyString = getInitParameter("privateKey");
+ String privateKeyPassword = getInitParameter("privateKeyPassword", null);
+ forceCRLF = getInitParameter("forceCRLF", true);
+ try {
+ PKCS8Key pkcs8 = new PKCS8Key(new ByteArrayInputStream(
+ privateKeyString.getBytes()),
+ privateKeyPassword != null ? privateKeyPassword
+ .toCharArray() : null);
+ privateKey = pkcs8.getPrivateKey();
+ } catch (NoSuchAlgorithmException e) {
+ throw new MessagingException("Unknown private key algorythm: " + e.getMessage(), e);
+ } catch (InvalidKeySpecException e) {
+ throw new MessagingException("PrivateKey should be in base64 encoded PKCS8 (der) format: " + e.getMessage(), e);
+ } catch (GeneralSecurityException e) {
+ throw new MessagingException("General security exception: " + e.getMessage(), e);
+ }
+ }
+
+ public void service(Mail mail) throws MessagingException {
+ DKIMSigner signer = new DKIMSigner(getSignatureTemplate(), getPrivateKey());
+ SignatureRecord signRecord = signer
+ .newSignatureRecordTemplate(getSignatureTemplate());
+ try {
+ BodyHasher bhj = signer.newBodyHasher(signRecord);
+ MimeMessage message = mail.getMessage();
+ Headers headers = new MimeMessageHeaders(message);
+ try {
+ OutputStream os = new HeaderSkippingOutputStream(bhj.getOutputStream());
+ if (forceCRLF) {
+ os = new CRLFOutputStream(os);
+ }
+ message.writeTo(os);
+ bhj.getOutputStream().close();
+ } catch (IOException e) {
+ throw new MessagingException("Exception calculating bodyhash: "
+ + e.getMessage(), e);
+ }
+ String signatureHeader = signer.sign(headers, bhj);
+ // Unfortunately JavaMail does not give us a method to add headers
+ // on top.
+ // message.addHeaderLine(signatureHeader);
+ prependHeader(message, signatureHeader);
+ } catch (PermFailException e) {
+ throw new MessagingException("PermFail while signing: "
+ + e.getMessage(), e);
+ }
+
+ }
+
+ private void prependHeader(MimeMessage message, String signatureHeader)
+ throws MessagingException {
+ List<String> prevHeader = new LinkedList<String>();
+ // read all the headers
+ for (Enumeration<String> e = message.getAllHeaderLines(); e.hasMoreElements();) {
+ String headerLine = e.nextElement();
+ prevHeader.add(headerLine);
+ }
+ // remove all the headers
+ for (Enumeration<Header> e = message.getAllHeaders(); e.hasMoreElements();) {
+ Header header = e.nextElement();
+ message.removeHeader(header.getName());
+ }
+ // add our header
+ message.addHeaderLine(signatureHeader);
+ // add the remaining headers using "addHeaderLine" that won't alter the
+ // insertion order.
+ for (String header : prevHeader) {
+ message.addHeaderLine(header);
+ }
+ }
+
+}
diff --git a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMVerify.java b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMVerify.java
new file mode 100644
index 0000000..ca72876
--- /dev/null
+++ b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMVerify.java
@@ -0,0 +1,108 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+
+package org.apache.james.jdkim.mailets;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.List;
+
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeMessage;
+
+import org.apache.james.jdkim.DKIMVerifier;
+import org.apache.james.jdkim.api.BodyHasher;
+import org.apache.james.jdkim.api.Headers;
+import org.apache.james.jdkim.api.SignatureRecord;
+import org.apache.james.jdkim.exceptions.FailException;
+import org.apache.mailet.Mail;
+import org.apache.mailet.base.GenericMailet;
+
+/**
+ * This mailet verify a message using the DKIM protocol
+ *
+ * Sample configuration:
+ * <pre><code>
+ * <mailet match="All" class="DKIMVerify">
+ * </mailet>
+ * </code></pre>
+ *
+ * By default the mailet assume that Javamail will use LF instead of CRLF
+ * so it will verify the hash using converted newlines. If you don't want this
+ * behaviout then set forceCRLF attribute to false.
+ */
+public class DKIMVerify extends GenericMailet {
+
+ public static final String DKIM_AUTH_RESULT_ATTRIBUTE = "jDKIM.AUTHRESULT";
+
+ protected DKIMVerifier verifier = null;
+ private boolean forceCRLF;
+
+ @Override
+ public void init() throws MessagingException {
+ verifier = new DKIMVerifier();
+ forceCRLF = getInitParameter("forceCRLF", true);
+ }
+
+ public void service(Mail mail) throws MessagingException {
+ try {
+ MimeMessage message = mail.getMessage();
+ List<SignatureRecord> res = verify(verifier, message, forceCRLF);
+ if (res == null || res.isEmpty()) {
+ // neutral
+ mail.setAttribute(DKIM_AUTH_RESULT_ATTRIBUTE, "neutral (no signatures)");
+ } else {
+ // pass
+ StringBuilder msg = new StringBuilder();
+ msg.append("pass");
+ for (SignatureRecord rec : res) {
+ msg.append(" (");
+ msg.append("identity ");
+ msg.append(rec.getIdentity().toString());
+ msg.append(")");
+ }
+ mail.setAttribute(DKIM_AUTH_RESULT_ATTRIBUTE, msg.toString());
+ }
+ } catch (FailException e) {
+ // fail
+ mail.setAttribute(DKIM_AUTH_RESULT_ATTRIBUTE, "fail ("+(e.getRelatedRecordIdentity() != null ? "identity "+ e.getRelatedRecordIdentity() + ": " : "")+e.getMessage()+")");
+ }
+
+ }
+
+ protected static List<SignatureRecord> verify(DKIMVerifier verifier, MimeMessage message, boolean forceCRLF)
+ throws MessagingException, FailException {
+ Headers headers = new MimeMessageHeaders(message);
+ BodyHasher bh = verifier.newBodyHasher(headers);
+ try {
+ if (bh != null) {
+ OutputStream os = new HeaderSkippingOutputStream(bh
+ .getOutputStream());
+ if (forceCRLF) os = new CRLFOutputStream(os);
+ message.writeTo(os);
+ bh.getOutputStream().close();
+ }
+
+ } catch (IOException e) {
+ throw new MessagingException("Exception calculating bodyhash: "
+ + e.getMessage(), e);
+ }
+ return verifier.verify(bh);
+ }
+}
diff --git a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/HeaderSkippingOutputStream.java b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/HeaderSkippingOutputStream.java
new file mode 100644
index 0000000..16e7f25
--- /dev/null
+++ b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/HeaderSkippingOutputStream.java
@@ -0,0 +1,71 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+
+package org.apache.james.jdkim.mailets;
+
+import java.io.FilterOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+/**
+ * Ignore writes until the given sequence is found.
+ */
+public class HeaderSkippingOutputStream extends FilterOutputStream {
+
+ boolean inHeaders = true;
+ final byte[] skipTo = "\r\n\r\n".getBytes();
+ int pos = 0;
+
+ public HeaderSkippingOutputStream(OutputStream out) {
+ super(out);
+ }
+
+ public void write(byte[] b, int off, int len) throws IOException {
+ if (inHeaders) {
+ for (int i = off; i < off + len; i++) {
+ if (b[i] == skipTo[pos]) {
+ pos++;
+ if (pos == skipTo.length) {
+ inHeaders = false;
+ if (len - i - 1 > 0)
+ out.write(b, i + 1, len - i - 1);
+ break;
+ }
+ } else
+ pos = 0;
+ }
+ } else {
+ out.write(b, off, len);
+ }
+ }
+
+ public void write(int b) throws IOException {
+ if (inHeaders) {
+ if (skipTo[pos] == b) {
+ pos++;
+ if (pos == skipTo.length)
+ inHeaders = false;
+ } else
+ pos = 0;
+ } else {
+ out.write(b);
+ }
+ }
+
+}
diff --git a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/MimeMessageHeaders.java b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/MimeMessageHeaders.java
new file mode 100644
index 0000000..3d2a639
--- /dev/null
+++ b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/MimeMessageHeaders.java
@@ -0,0 +1,70 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+
+package org.apache.james.jdkim.mailets;
+
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeMessage;
+
+import org.apache.james.jdkim.api.Headers;
+
+/**
+ * An adapter to let DKIMSigner read headers from MimeMessage
+ */
+final class MimeMessageHeaders implements Headers {
+
+ private final Map<String, List<String>> headers;
+ private final List<String> fields;
+
+ public MimeMessageHeaders(MimeMessage message)
+ throws MessagingException {
+ headers = new HashMap<String, List<String>>();
+ fields = new LinkedList<String>();
+ for (Enumeration<String> e = message.getAllHeaderLines(); e
+ .hasMoreElements();) {
+ String head = e.nextElement();
+ int p = head.indexOf(':');
+ if (p <= 0)
+ throw new MessagingException("Bad header line: " + head);
+ String headerName = head.substring(0, p).trim();
+ String headerNameLC = headerName.toLowerCase();
+ fields.add(headerName);
+ List<String> strings = headers.get(headerNameLC);
+ if (strings == null) {
+ strings = new LinkedList<String>();
+ headers.put(headerNameLC, strings);
+ }
+ strings.add(head);
+ }
+ }
+
+ public List<String> getFields() {
+ return fields;
+ }
+
+ public List<String> getFields(String name) {
+ return headers.get(name.toLowerCase());
+ }
+}
\ No newline at end of file
diff --git a/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMSignTest.java b/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMSignTest.java
new file mode 100644
index 0000000..83ea4d6
--- /dev/null
+++ b/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMSignTest.java
@@ -0,0 +1,361 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+
+package org.apache.james.jdkim.mailets;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.List;
+import java.util.Properties;
+
+import javax.mail.Address;
+import javax.mail.MessagingException;
+import javax.mail.Session;
+import javax.mail.internet.InternetAddress;
+import javax.mail.internet.MimeMessage;
+import javax.mail.internet.MimeMessage.RecipientType;
+
+import org.apache.james.jdkim.DKIMVerifier;
+import org.apache.james.jdkim.MockPublicKeyRecordRetriever;
+import org.apache.james.jdkim.api.SignatureRecord;
+import org.apache.james.jdkim.exceptions.FailException;
+import org.apache.james.jdkim.exceptions.PermFailException;
+import org.apache.mailet.Mail;
+import org.apache.mailet.Mailet;
+import org.apache.mailet.base.test.FakeMail;
+import org.apache.mailet.base.test.FakeMailContext;
+import org.apache.mailet.base.test.FakeMailetConfig;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class DKIMSignTest {
+
+ private static final String TESTING_PEM = "-----BEGIN RSA PRIVATE KEY-----\r\n" +
+ "MIICXAIBAAKBgQDYDaYKXzwVYwqWbLhmuJ66aTAN8wmDR+rfHE8HfnkSOax0oIoT\r\n" +
+ "M5zquZrTLo30870YMfYzxwfB6j/Nz3QdwrUD/t0YMYJiUKyWJnCKfZXHJBJ+yfRH\r\n" +
+ "r7oW+UW3cVo9CG2bBfIxsInwYe175g9UjyntJpWueqdEIo1c2bhv9Mp66QIDAQAB\r\n" +
+ "AoGBAI8XcwnZi0Sq5N89wF+gFNhnREFo3rsJDaCY8iqHdA5DDlnr3abb/yhipw0I\r\n" +
+ "/1HlgC6fIG2oexXOXFWl+USgqRt1kTt9jXhVFExg8mNko2UelAwFtsl8CRjVcYQO\r\n" +
+ "cedeH/WM/mXjg2wUqqZenBmlKlD6vNb70jFJeVaDJ/7n7j8BAkEA9NkH2D4Zgj/I\r\n" +
+ "OAVYccZYH74+VgO0e7VkUjQk9wtJ2j6cGqJ6Pfj0roVIMUWzoBb8YfErR8l6JnVQ\r\n" +
+ "bfy83gJeiQJBAOHk3ow7JjAn8XuOyZx24KcTaYWKUkAQfRWYDFFOYQF4KV9xLSEt\r\n" +
+ "ycY0kjsdxGKDudWcsATllFzXDCQF6DTNIWECQEA52ePwTjKrVnLTfCLEG4OgHKvl\r\n" +
+ "Zud4amthwDyJWoMEH2ChNB2je1N4JLrABOE+hk+OuoKnKAKEjWd8f3Jg/rkCQHj8\r\n" +
+ "mQmogHqYWikgP/FSZl518jV48Tao3iXbqvU9Mo2T6yzYNCCqIoDLFWseNVnCTZ0Q\r\n" +
+ "b+IfiEf1UeZVV5o4J+ECQDatNnS3V9qYUKjj/krNRD/U0+7eh8S2ylLqD3RlSn9K\r\n" +
+ "tYGRMgAtUXtiOEizBH6bd/orzI9V9sw8yBz+ZqIH25Q=\r\n" +
+ "-----END RSA PRIVATE KEY-----\r\n";
+ private static final FakeMailContext FAKE_MAIL_CONTEXT = FakeMailContext.defaultContext();
+
+ @Test
+ public void testDKIMSign() throws MessagingException, IOException,
+ FailException {
+ String message = "Received: by 10.XX.XX.12 with SMTP id dfgskldjfhgkljsdfhgkljdhfg;\r\n\tTue, 06 Oct 2009 07:37:34 -0700 (PDT)\r\nReturn-Path: <bo...@example.com>\r\nReceived: from example.co.uk (example.co.uk [XX.XXX.125.19])\r\n\tby mx.example.com with ESMTP id dgdfgsdfgsd.97.2009.10.06.07.37.32;\r\n\tTue, 06 Oct 2009 07:37:32 -0700 (PDT)\r\nFrom: apache@bago.org\r\nTo: apache@bago.org\r\n\r\nbody\r\nprova\r\n";
+
+ Mailet mailet = new DKIMSign();
+
+ FakeMailetConfig mci = FakeMailetConfig.builder()
+ .mailetName("Test")
+ .mailetContext(FAKE_MAIL_CONTEXT)
+ .setProperty(
+ "signatureTemplate",
+ "v=1; s=selector; d=example.com; h=from:to:received:received; a=rsa-sha256; bh=; b=;")
+ .setProperty("privateKey", TESTING_PEM)
+ .build();
+
+ mailet.init(mci);
+
+ Mail mail = FakeMail.builder()
+ .name("test")
+ .mimeMessage(new MimeMessage(Session
+ .getDefaultInstance(new Properties()),
+ new ByteArrayInputStream(message.getBytes())))
+ .build();
+
+ mailet.service(mail);
+
+ Mailet m7bit = new ConvertTo7Bit();
+ m7bit.init(mci);
+ m7bit.service(mail);
+
+ ByteArrayOutputStream rawMessage = new ByteArrayOutputStream();
+ mail.getMessage().writeTo(rawMessage);
+
+ MockPublicKeyRecordRetriever mockPublicKeyRecordRetriever = new MockPublicKeyRecordRetriever(
+ "v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDYDaYKXzwVYwqWbLhmuJ66aTAN8wmDR+rfHE8HfnkSOax0oIoTM5zquZrTLo30870YMfYzxwfB6j/Nz3QdwrUD/t0YMYJiUKyWJnCKfZXHJBJ+yfRHr7oW+UW3cVo9CG2bBfIxsInwYe175g9UjyntJpWueqdEIo1c2bhv9Mp66QIDAQAB;",
+ "selector", "example.com");
+ verify(rawMessage, mockPublicKeyRecordRetriever);
+ }
+
+ private List<SignatureRecord> verify(ByteArrayOutputStream rawMessage,
+ MockPublicKeyRecordRetriever mockPublicKeyRecordRetriever)
+ throws MessagingException, FailException {
+ List<SignatureRecord> signs = DKIMVerify.verify(new DKIMVerifier(mockPublicKeyRecordRetriever), new MimeMessage(Session.getDefaultInstance(new Properties()), new ByteArrayInputStream(rawMessage.toByteArray())), true);
+ assertNotNull(signs);
+ assertEquals(1, signs.size());
+ return signs;
+ }
+
+ @Test
+ public void testDKIMSignFuture() throws MessagingException, IOException,
+ FailException {
+ String message = "Received: by 10.XX.XX.12 with SMTP id dfgskldjfhgkljsdfhgkljdhfg;\r\n\tTue, 06 Oct 2009 07:37:34 -0700 (PDT)\r\nReturn-Path: <bo...@example.com>\r\nReceived: from example.co.uk (example.co.uk [XX.XXX.125.19])\r\n\tby mx.example.com with ESMTP id dgdfgsdfgsd.97.2009.10.06.07.37.32;\r\n\tTue, 06 Oct 2009 07:37:32 -0700 (PDT)\r\nFrom: apache@bago.org\r\nTo: apache@bago.org\r\n\r\nbody\r\nprova\r\n";
+
+ Mailet mailet = new DKIMSign();
+
+ FakeMailetConfig mci = FakeMailetConfig.builder()
+ .mailetName("Test")
+ .mailetContext(FAKE_MAIL_CONTEXT)
+ .setProperty(
+ "signatureTemplate",
+ "v=1; t=" + ((System.currentTimeMillis() / 1000) + 1000) + "; s=selector; d=example.com; h=from:to:received:received; a=rsa-sha256; bh=; b=;")
+ .setProperty("privateKey", TESTING_PEM)
+ .build();
+
+ mailet.init(mci);
+
+ Mail mail = FakeMail.builder()
+ .name("test")
+ .mimeMessage(new MimeMessage(Session
+ .getDefaultInstance(new Properties()),
+ new ByteArrayInputStream(message.getBytes())))
+ .build();
+
+ mailet.service(mail);
+
+ Mailet m7bit = new ConvertTo7Bit();
+ m7bit.init(mci);
+ m7bit.service(mail);
+
+ ByteArrayOutputStream rawMessage = new ByteArrayOutputStream();
+ mail.getMessage().writeTo(rawMessage);
+
+ MockPublicKeyRecordRetriever mockPublicKeyRecordRetriever = new MockPublicKeyRecordRetriever(
+ "v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDYDaYKXzwVYwqWbLhmuJ66aTAN8wmDR+rfHE8HfnkSOax0oIoTM5zquZrTLo30870YMfYzxwfB6j/Nz3QdwrUD/t0YMYJiUKyWJnCKfZXHJBJ+yfRHr7oW+UW3cVo9CG2bBfIxsInwYe175g9UjyntJpWueqdEIo1c2bhv9Mp66QIDAQAB;",
+ "selector", "example.com");
+ try {
+ verify(rawMessage, mockPublicKeyRecordRetriever);
+ Assert.fail("Expecting signature to be ignored");
+ } catch (PermFailException e) {
+ // signature ignored, so fail for missing signatures.
+ }
+ }
+
+
+ @Test
+ public void testDKIMSignTime() throws MessagingException, IOException,
+ FailException {
+ String message = "Received: by 10.XX.XX.12 with SMTP id dfgskldjfhgkljsdfhgkljdhfg;\r\n\tTue, 06 Oct 2009 07:37:34 -0700 (PDT)\r\nReturn-Path: <bo...@example.com>\r\nReceived: from example.co.uk (example.co.uk [XX.XXX.125.19])\r\n\tby mx.example.com with ESMTP id dgdfgsdfgsd.97.2009.10.06.07.37.32;\r\n\tTue, 06 Oct 2009 07:37:32 -0700 (PDT)\r\nFrom: apache@bago.org\r\nTo: apache@bago.org\r\n\r\nbody\r\nprova\r\n";
+
+ Mailet mailet = new DKIMSign();
+
+ FakeMailetConfig mci = FakeMailetConfig.builder()
+ .mailetName("Test")
+ .mailetContext(FAKE_MAIL_CONTEXT)
+ .setProperty(
+ "signatureTemplate",
+ "v=1; t=; s=selector; d=example.com; h=from:to:received:received; a=rsa-sha256; bh=; b=;")
+ .setProperty("privateKey", TESTING_PEM)
+ .build();
+
+ mailet.init(mci);
+
+ Mail mail = FakeMail.builder()
+ .name("test")
+ .mimeMessage(new MimeMessage(Session
+ .getDefaultInstance(new Properties()),
+ new ByteArrayInputStream(message.getBytes())))
+ .build();
+
+ mailet.service(mail);
+
+ Mailet m7bit = new ConvertTo7Bit();
+ m7bit.init(mci);
+ m7bit.service(mail);
+
+ ByteArrayOutputStream rawMessage = new ByteArrayOutputStream();
+ mail.getMessage().writeTo(rawMessage);
+
+ MockPublicKeyRecordRetriever mockPublicKeyRecordRetriever = new MockPublicKeyRecordRetriever(
+ "v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDYDaYKXzwVYwqWbLhmuJ66aTAN8wmDR+rfHE8HfnkSOax0oIoTM5zquZrTLo30870YMfYzxwfB6j/Nz3QdwrUD/t0YMYJiUKyWJnCKfZXHJBJ+yfRHr7oW+UW3cVo9CG2bBfIxsInwYe175g9UjyntJpWueqdEIo1c2bhv9Mp66QIDAQAB;",
+ "selector", "example.com");
+ verify(rawMessage, mockPublicKeyRecordRetriever);
+
+ List<SignatureRecord> rs = verify(rawMessage, mockPublicKeyRecordRetriever);
+
+ // check we have a valued signatureTimestamp
+ Assert.assertNotNull(rs.get(0).getSignatureTimestamp());
+ long ref = System.currentTimeMillis() / 1000;
+ // Chech that the signature timestamp is in the past 60 seconds.
+ Assert.assertTrue(rs.get(0).getSignatureTimestamp() <= ref);
+ Assert.assertTrue(rs.get(0).getSignatureTimestamp() >= ref - 60);
+ }
+
+ @Test
+ public void testDKIMSignMessageAsText() throws MessagingException,
+ IOException, FailException {
+ MimeMessage mm = new MimeMessage(Session
+ .getDefaultInstance(new Properties()));
+ mm.addFrom(new Address[]{new InternetAddress("io@bago.org")});
+ mm.addRecipient(RecipientType.TO, new InternetAddress("io@bago.org"));
+ mm.setText("An 8bit encoded body with \u20ACuro symbol.", "ISO-8859-15");
+
+ Mailet mailet = new DKIMSign();
+
+ FakeMailetConfig mci = FakeMailetConfig.builder()
+ .mailetName("Test")
+ .mailetContext(FAKE_MAIL_CONTEXT)
+ .setProperty(
+ "signatureTemplate",
+ "v=1; s=selector; d=example.com; h=from:to:received:received; a=rsa-sha256; bh=; b=;")
+ .setProperty("privateKey", TESTING_PEM)
+ .build();
+
+ mailet.init(mci);
+
+ Mail mail = FakeMail.builder()
+ .name("test")
+ .mimeMessage(mm)
+ .build();
+
+ Mailet m7bit = new ConvertTo7Bit();
+ m7bit.init(mci);
+
+ mailet.service(mail);
+
+ m7bit.service(mail);
+
+ ByteArrayOutputStream rawMessage = new ByteArrayOutputStream();
+ mail.getMessage().writeTo(rawMessage);
+
+ MockPublicKeyRecordRetriever mockPublicKeyRecordRetriever = new MockPublicKeyRecordRetriever(
+ "v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDYDaYKXzwVYwqWbLhmuJ66aTAN8wmDR+rfHE8HfnkSOax0oIoTM5zquZrTLo30870YMfYzxwfB6j/Nz3QdwrUD/t0YMYJiUKyWJnCKfZXHJBJ+yfRHr7oW+UW3cVo9CG2bBfIxsInwYe175g9UjyntJpWueqdEIo1c2bhv9Mp66QIDAQAB;",
+ "selector", "example.com");
+
+ verify(rawMessage, mockPublicKeyRecordRetriever);
+ }
+
+ @Test
+ public void testDKIMSignMessageAsObjectConvertedTo7Bit()
+ throws MessagingException, IOException, FailException {
+ MimeMessage mm = new MimeMessage(Session
+ .getDefaultInstance(new Properties()));
+ mm.addFrom(new Address[]{new InternetAddress("io@bago.org")});
+ mm.addRecipient(RecipientType.TO, new InternetAddress("io@bago.org"));
+ mm.setContent("An 8bit encoded body with \u20ACuro symbol.",
+ "text/plain; charset=iso-8859-15");
+ mm.setHeader("Content-Transfer-Encoding", "8bit");
+ mm.saveChanges();
+
+ FakeMailContext FakeMailContext = FAKE_MAIL_CONTEXT;
+ FakeMailContext.getServerInfo();
+
+ FakeMailetConfig mci = FakeMailetConfig.builder()
+ .mailetName("Test")
+ .mailetContext(FAKE_MAIL_CONTEXT)
+ .setProperty(
+ "signatureTemplate",
+ "v=1; s=selector; d=example.com; h=from:to:received:received; a=rsa-sha256; bh=; b=;")
+ .setProperty("privateKey", TESTING_PEM)
+ .build();
+
+ Mail mail = FakeMail.builder()
+ .name("test")
+ .mimeMessage(mm)
+ .build();
+
+ Mailet mailet = new DKIMSign();
+ mailet.init(mci);
+
+ Mailet m7bit = new ConvertTo7Bit();
+ m7bit.init(mci);
+ m7bit.service(mail);
+
+ mailet.service(mail);
+
+ m7bit.service(mail);
+
+ ByteArrayOutputStream rawMessage = new ByteArrayOutputStream();
+ mail.getMessage().writeTo(rawMessage);
+
+ MockPublicKeyRecordRetriever mockPublicKeyRecordRetriever = new MockPublicKeyRecordRetriever(
+ "v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDYDaYKXzwVYwqWbLhmuJ66aTAN8wmDR+rfHE8HfnkSOax0oIoTM5zquZrTLo30870YMfYzxwfB6j/Nz3QdwrUD/t0YMYJiUKyWJnCKfZXHJBJ+yfRHr7oW+UW3cVo9CG2bBfIxsInwYe175g9UjyntJpWueqdEIo1c2bhv9Mp66QIDAQAB;",
+ "selector", "example.com");
+ verify(rawMessage, mockPublicKeyRecordRetriever);
+ }
+
+ @Test
+ public void testDKIMSignMessageAsObjectNotConverted()
+ throws MessagingException, IOException, FailException {
+ MimeMessage mm = new MimeMessage(Session
+ .getDefaultInstance(new Properties()));
+ mm.addFrom(new Address[]{new InternetAddress("io@bago.org")});
+ mm.addRecipient(RecipientType.TO, new InternetAddress("io@bago.org"));
+ mm.setContent("An 8bit encoded body with \u20ACuro symbol.",
+ "text/plain; charset=iso-8859-15");
+ mm.setHeader("Content-Transfer-Encoding", "8bit");
+ mm.saveChanges();
+
+ FakeMailContext FakeMailContext = FAKE_MAIL_CONTEXT;
+ FakeMailContext.getServerInfo();
+
+ FakeMailetConfig mci = FakeMailetConfig.builder()
+ .mailetName("Test")
+ .mailetContext(FAKE_MAIL_CONTEXT)
+ .setProperty(
+ "signatureTemplate",
+ "v=1; s=selector; d=example.com; h=from:to:received:received; a=rsa-sha256; bh=; b=;")
+ .setProperty("privateKey", TESTING_PEM)
+ .build();
+
+ Mail mail = FakeMail.builder()
+ .name("test")
+ .mimeMessage(mm)
+ .build();
+
+ Mailet mailet = new DKIMSign();
+ mailet.init(mci);
+
+ Mailet m7bit = new ConvertTo7Bit();
+ m7bit.init(mci);
+ // m7bit.service(mail);
+
+ mailet.service(mail);
+
+ m7bit.service(mail);
+
+ ByteArrayOutputStream rawMessage = new ByteArrayOutputStream();
+ mail.getMessage().writeTo(rawMessage);
+
+ MockPublicKeyRecordRetriever mockPublicKeyRecordRetriever = new MockPublicKeyRecordRetriever(
+ "v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDYDaYKXzwVYwqWbLhmuJ66aTAN8wmDR+rfHE8HfnkSOax0oIoTM5zquZrTLo30870YMfYzxwfB6j/Nz3QdwrUD/t0YMYJiUKyWJnCKfZXHJBJ+yfRHr7oW+UW3cVo9CG2bBfIxsInwYe175g9UjyntJpWueqdEIo1c2bhv9Mp66QIDAQAB;",
+ "selector", "example.com");
+ try {
+ verify(rawMessage, mockPublicKeyRecordRetriever);
+ Assert.fail("Expected PermFail");
+ } catch (PermFailException e) {
+
+ }
+ }
+
+}
diff --git a/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMVerifyTest.java b/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMVerifyTest.java
new file mode 100644
index 0000000..5ee901f
--- /dev/null
+++ b/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMVerifyTest.java
@@ -0,0 +1,127 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+
+package org.apache.james.jdkim.mailets;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.util.Properties;
+
+import javax.mail.MessagingException;
+import javax.mail.Session;
+import javax.mail.internet.MimeMessage;
+
+import org.apache.james.jdkim.DKIMVerifier;
+import org.apache.james.jdkim.MockPublicKeyRecordRetriever;
+import org.apache.james.jdkim.exceptions.FailException;
+import org.apache.mailet.Mail;
+import org.apache.mailet.Mailet;
+import org.apache.mailet.base.test.FakeMail;
+import org.apache.mailet.base.test.FakeMailContext;
+import org.apache.mailet.base.test.FakeMailetConfig;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class DKIMVerifyTest {
+
+ @Test
+ public void testDKIMVerifyPass() throws MessagingException, IOException,
+ FailException {
+ String message = "DKIM-Signature: v=1; d=example.com; t=1284762805; b=ZFfwSIzTQM7k9syRnl9VfQh0/dr99euvBe1gn/DiTrnEZjxyjzQBD2MMvowVdbHpPMtSjtCtehU9zZ3urXmj5iHKujpEkP92FEKinzElkQ2eT2zoxdg1zByPHsKPX+KjrBespAJcO2k052aOK5kIBFxpQumP4aiW7ZklBKSWMBk=; s=selector; a=rsa-sha256; bh=rHOD7fd9xnNxK7OSl5ellpQVF14NNFbOIizqtUMhnio=; h=from:to:received:received;\r\n"
+ + "Received: by 10.XX.XX.12 with SMTP id dfgskldjfhgkljsdfhgkljdhfg;\r\n\tTue, 06 Oct 2009 07:37:34 -0700 (PDT)\r\nReturn-Path: <bo...@example.com>\r\nReceived: from example.co.uk (example.co.uk [XX.XXX.125.19])\r\n\tby mx.example.com with ESMTP id dgdfgsdfgsd.97.2009.10.06.07.37.32;\r\n\tTue, 06 Oct 2009 07:37:32 -0700 (PDT)\r\nFrom: apache@bago.org\r\nTo: apache@bago.org\r\n\r\nbody\r\nprova\r\n";
+
+ Mail mail = process(message);
+
+ String attr = (String) mail.getAttribute(DKIMVerify.DKIM_AUTH_RESULT_ATTRIBUTE);
+ Assert.assertNotNull(attr);
+ Assert.assertTrue(attr.startsWith("pass"));
+ }
+
+ @Test
+ public void testDKIMVerifyFail() throws MessagingException, IOException,
+ FailException {
+ // altered message body
+ String message = "DKIM-Signature: v=1; d=example.com; t=1284762805; b=ZFfwSIzTQM7k9syRnl9VfQh0/dr99euvBe1gn/DiTrnEZjxyjzQBD2MMvowVdbHpPMtSjtCtehU9zZ3urXmj5iHKujpEkP92FEKinzElkQ2eT2zoxdg1zByPHsKPX+KjrBespAJcO2k052aOK5kIBFxpQumP4aiW7ZklBKSWMBk=; s=selector; a=rsa-sha256; bh=rHOD7fd9xnNxK7OSl5ellpQVF14NNFbOIizqtUMhnio=; h=from:to:received:received;\r\n"
+ + "Received: by 10.XX.XX.12 with SMTP id dfgskldjfhgkljsdfhgkljdhfg;\r\n\tTue, 06 Oct 2009 07:37:34 -0700 (PDT)\r\nReturn-Path: <bo...@example.com>\r\nReceived: from example.co.uk (example.co.uk [XX.XXX.125.19])\r\n\tby mx.example.com with ESMTP id dgdfgsdfgsd.97.2009.10.06.07.37.32;\r\n\tTue, 06 Oct 2009 07:37:32 -0700 (PDT)\r\nFrom: apache@bago.org\r\nTo: apache@bago.org\r\n\r\nbody\r\nprova altered\r\n";
+
+ Mail mail = process(message);
+
+ String attr = (String) mail.getAttribute(DKIMVerify.DKIM_AUTH_RESULT_ATTRIBUTE);
+ Assert.assertNotNull(attr);
+ Assert.assertTrue(attr.startsWith("fail"));
+ }
+
+ @Test
+ public void testDKIMVerifyFailInvalid() throws MessagingException, IOException,
+ FailException {
+ // invalid version v=2
+ String message = "DKIM-Signature: v=2; d=example.com; t=1284762805; b=ZFfwSIzTQM7k9syRnl9VfQh0/dr99euvBe1gn/DiTrnEZjxyjzQBD2MMvowVdbHpPMtSjtCtehU9zZ3urXmj5iHKujpEkP92FEKinzElkQ2eT2zoxdg1zByPHsKPX+KjrBespAJcO2k052aOK5kIBFxpQumP4aiW7ZklBKSWMBk=; s=selector; a=rsa-sha256; bh=rHOD7fd9xnNxK7OSl5ellpQVF14NNFbOIizqtUMhnio=; h=from:to:received:received;\r\n"
+ + "Received: by 10.XX.XX.12 with SMTP id dfgskldjfhgkljsdfhgkljdhfg;\r\n\tTue, 06 Oct 2009 07:37:34 -0700 (PDT)\r\nReturn-Path: <bo...@example.com>\r\nReceived: from example.co.uk (example.co.uk [XX.XXX.125.19])\r\n\tby mx.example.com with ESMTP id dgdfgsdfgsd.97.2009.10.06.07.37.32;\r\n\tTue, 06 Oct 2009 07:37:32 -0700 (PDT)\r\nFrom: apache@bago.org\r\nTo: apache@bago.org\r\n\r\nbody\r\nprova\r\n";
+
+ Mail mail = process(message);
+
+ String attr = (String) mail.getAttribute(DKIMVerify.DKIM_AUTH_RESULT_ATTRIBUTE);
+ Assert.assertNotNull(attr);
+ Assert.assertTrue(attr.startsWith("fail"));
+ }
+
+ @Test
+ public void testDKIMVerifyNeutral() throws MessagingException, IOException,
+ FailException {
+ // no signatures!
+ String message = ""
+ + "Received: by 10.XX.XX.12 with SMTP id dfgskldjfhgkljsdfhgkljdhfg;\r\n\tTue, 06 Oct 2009 07:37:34 -0700 (PDT)\r\nReturn-Path: <bo...@example.com>\r\nReceived: from example.co.uk (example.co.uk [XX.XXX.125.19])\r\n\tby mx.example.com with ESMTP id dgdfgsdfgsd.97.2009.10.06.07.37.32;\r\n\tTue, 06 Oct 2009 07:37:32 -0700 (PDT)\r\nFrom: apache@bago.org\r\nTo: apache@bago.org\r\n\r\nbody\r\nprova altered\r\n";
+
+ Mail mail = process(message);
+
+ String attr = (String) mail.getAttribute(DKIMVerify.DKIM_AUTH_RESULT_ATTRIBUTE);
+ Assert.assertNotNull(attr);
+ Assert.assertTrue(attr.startsWith("neutral"));
+ }
+
+ private Mail process(String message) throws MessagingException {
+ Mailet mailet = new DKIMVerify() {
+
+ @Override
+ public void init() throws MessagingException {
+ verifier = new DKIMVerifier(new MockPublicKeyRecordRetriever(
+ "v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDYDaYKXzwVYwqWbLhmuJ66aTAN8wmDR+rfHE8HfnkSOax0oIoTM5zquZrTLo30870YMfYzxwfB6j/Nz3QdwrUD/t0YMYJiUKyWJnCKfZXHJBJ+yfRHr7oW+UW3cVo9CG2bBfIxsInwYe175g9UjyntJpWueqdEIo1c2bhv9Mp66QIDAQAB;",
+ "selector", "example.com"));
+ }
+
+ };
+
+ FakeMailetConfig mci = FakeMailetConfig.builder()
+ .mailetName("Test")
+ .mailetContext(FakeMailContext.defaultContext())
+ .build();
+
+ mailet.init(mci);
+
+ Mail mail = FakeMail.builder()
+ .name("test")
+ .mimeMessage(new MimeMessage(Session
+ .getDefaultInstance(new Properties()),
+ new ByteArrayInputStream(message.getBytes())))
+ .build();
+
+ mailet.service(mail);
+ return mail;
+ }
+}
diff --git a/server/pom.xml b/server/pom.xml
index 07e2ae2..aafbbb4 100644
--- a/server/pom.xml
+++ b/server/pom.xml
@@ -72,6 +72,8 @@
<module>dns-service/dnsservice-test</module>
<module>mailet/integration-testing</module>
+
+ <module>mailet/dkim</module>
<module>mailet/mailetcontainer-api</module>
<module>mailet/mailetcontainer-camel</module>
<module>mailet/mailets</module>
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org
[james-project] 03/14: JAMES-1975 Fix checkstyle issues in DKIM
mailet code
Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rouazana pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git
commit e7c3770a3cf837a503a0429b100cd062603e9933
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Fri Aug 31 17:58:59 2018 +0700
JAMES-1975 Fix checkstyle issues in DKIM mailet code
---
.../james/jdkim/mailets/CRLFOutputStream.java | 22 +++++-----
.../org/apache/james/jdkim/mailets/DKIMSign.java | 30 +++++++-------
.../org/apache/james/jdkim/mailets/DKIMVerify.java | 47 ++++++++++++----------
.../jdkim/mailets/HeaderSkippingOutputStream.java | 18 +++++----
.../james/jdkim/mailets/MimeMessageHeaders.java | 9 ++---
5 files changed, 66 insertions(+), 60 deletions(-)
diff --git a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/CRLFOutputStream.java b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/CRLFOutputStream.java
index b686b72..91f8579 100644
--- a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/CRLFOutputStream.java
+++ b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/CRLFOutputStream.java
@@ -33,23 +33,21 @@ import java.io.OutputStream;
*
*/
public class CRLFOutputStream extends FilterOutputStream {
+ private static final int LAST_WAS_OTHER = 0;
+ private static final int LAST_WAS_CR = 1;
+ private static final int LAST_WAS_LF = 2;
/**
* Counter for number of last (0A or 0D).
*/
protected int statusLast;
- protected final static int LAST_WAS_OTHER = 0;
-
- protected final static int LAST_WAS_CR = 1;
-
- protected final static int LAST_WAS_LF = 2;
protected boolean startOfLine = true;
/**
* Constructor that wraps an OutputStream.
- *
+ *
* @param out
* the OutputStream to be wrapped
*/
@@ -63,10 +61,10 @@ public class CRLFOutputStream extends FilterOutputStream {
/**
* Writes a byte to the stream Fixes any naked CR or LF to the RFC 2821
* mandated CFLF pairing.
- *
+ *
* @param b
* the byte to write
- *
+ *
* @throws IOException
* if an error occurs writing the byte
*/
@@ -98,10 +96,10 @@ public class CRLFOutputStream extends FilterOutputStream {
/**
* Provides an extension point for ExtraDotOutputStream to be able to add
* dots at the beginning of new lines.
- *
+ *
* @see java.io.FilterOutputStream#write(byte[], int, int)
*/
- protected void writeChunk(byte buffer[], int offset, int length)
+ protected void writeChunk(byte[] buffer, int offset, int length)
throws IOException {
out.write(buffer, offset, length);
}
@@ -109,7 +107,7 @@ public class CRLFOutputStream extends FilterOutputStream {
/**
* @see java.io.FilterOutputStream#write(byte[], int, int)
*/
- public synchronized void write(byte buffer[], int offset, int length)
+ public synchronized void write(byte[] buffer, int offset, int length)
throws IOException {
/* optimized */
int lineStart = offset;
@@ -147,7 +145,7 @@ public class CRLFOutputStream extends FilterOutputStream {
/**
* Ensure that the stream is CRLF terminated.
- *
+ *
* @throws IOException
* if an error occurs writing the byte
*/
diff --git a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMSign.java b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMSign.java
index a51b672..f27fbe4 100644
--- a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMSign.java
+++ b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMSign.java
@@ -83,19 +83,19 @@ public class DKIMSign extends GenericMailet {
private PrivateKey privateKey;
private boolean forceCRLF;
- /**
- * @return the signatureTemplate
- */
- protected String getSignatureTemplate() {
- return signatureTemplate;
- }
+ /**
+ * @return the signatureTemplate
+ */
+ protected String getSignatureTemplate() {
+ return signatureTemplate;
+ }
- /**
- * @return the privateKey
- */
- protected PrivateKey getPrivateKey() {
- return privateKey;
- }
+ /**
+ * @return the privateKey
+ */
+ protected PrivateKey getPrivateKey() {
+ return privateKey;
+ }
public void init() throws MessagingException {
signatureTemplate = getInitParameter("signatureTemplate");
@@ -126,9 +126,9 @@ public class DKIMSign extends GenericMailet {
MimeMessage message = mail.getMessage();
Headers headers = new MimeMessageHeaders(message);
try {
- OutputStream os = new HeaderSkippingOutputStream(bhj.getOutputStream());
- if (forceCRLF) {
- os = new CRLFOutputStream(os);
+ OutputStream os = new HeaderSkippingOutputStream(bhj.getOutputStream());
+ if (forceCRLF) {
+ os = new CRLFOutputStream(os);
}
message.writeTo(os);
bhj.getOutputStream().close();
diff --git a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMVerify.java b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMVerify.java
index c3a4f14..baddb09 100644
--- a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMVerify.java
+++ b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMVerify.java
@@ -22,6 +22,7 @@ package org.apache.james.jdkim.mailets;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
+import java.util.Optional;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
@@ -81,28 +82,32 @@ public class DKIMVerify extends GenericMailet {
}
} catch (FailException e) {
// fail
- mail.setAttribute(DKIM_AUTH_RESULT_ATTRIBUTE, "fail ("+(e.getRelatedRecordIdentity() != null ? "identity "+ e.getRelatedRecordIdentity() + ": " : "")+e.getMessage()+")");
+ String relatedRecordIdentity = Optional.ofNullable(e.getRelatedRecordIdentity())
+ .map(value -> "identity" + value + ":")
+ .orElse("");
+ mail.setAttribute(DKIM_AUTH_RESULT_ATTRIBUTE, "fail (" + relatedRecordIdentity + e.getMessage() + ")");
}
-
}
- protected static List<SignatureRecord> verify(DKIMVerifier verifier, MimeMessage message, boolean forceCRLF)
- throws MessagingException, FailException {
- Headers headers = new MimeMessageHeaders(message);
- BodyHasher bh = verifier.newBodyHasher(headers);
- try {
- if (bh != null) {
- OutputStream os = new HeaderSkippingOutputStream(bh
- .getOutputStream());
- if (forceCRLF) os = new CRLFOutputStream(os);
- message.writeTo(os);
- bh.getOutputStream().close();
- }
-
- } catch (IOException e) {
- throw new MessagingException("Exception calculating bodyhash: "
- + e.getMessage(), e);
- }
- return verifier.verify(bh);
- }
+ protected static List<SignatureRecord> verify(DKIMVerifier verifier, MimeMessage message, boolean forceCRLF)
+ throws MessagingException, FailException {
+ Headers headers = new MimeMessageHeaders(message);
+ BodyHasher bh = verifier.newBodyHasher(headers);
+ try {
+ if (bh != null) {
+ OutputStream os = new HeaderSkippingOutputStream(bh
+ .getOutputStream());
+ if (forceCRLF) {
+ os = new CRLFOutputStream(os);
+ }
+ message.writeTo(os);
+ bh.getOutputStream().close();
+ }
+
+ } catch (IOException e) {
+ throw new MessagingException("Exception calculating bodyhash: "
+ + e.getMessage(), e);
+ }
+ return verifier.verify(bh);
+ }
}
diff --git a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/HeaderSkippingOutputStream.java b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/HeaderSkippingOutputStream.java
index 16e7f25..5ccfbb0 100644
--- a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/HeaderSkippingOutputStream.java
+++ b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/HeaderSkippingOutputStream.java
@@ -28,9 +28,9 @@ import java.io.OutputStream;
*/
public class HeaderSkippingOutputStream extends FilterOutputStream {
- boolean inHeaders = true;
- final byte[] skipTo = "\r\n\r\n".getBytes();
- int pos = 0;
+ private final byte[] skipTo = "\r\n\r\n".getBytes();
+ private boolean inHeaders = true;
+ private int pos = 0;
public HeaderSkippingOutputStream(OutputStream out) {
super(out);
@@ -43,12 +43,14 @@ public class HeaderSkippingOutputStream extends FilterOutputStream {
pos++;
if (pos == skipTo.length) {
inHeaders = false;
- if (len - i - 1 > 0)
+ if (len - i - 1 > 0) {
out.write(b, i + 1, len - i - 1);
+ }
break;
}
- } else
+ } else {
pos = 0;
+ }
}
} else {
out.write(b, off, len);
@@ -59,10 +61,12 @@ public class HeaderSkippingOutputStream extends FilterOutputStream {
if (inHeaders) {
if (skipTo[pos] == b) {
pos++;
- if (pos == skipTo.length)
+ if (pos == skipTo.length) {
inHeaders = false;
- } else
+ }
+ } else {
pos = 0;
+ }
} else {
out.write(b);
}
diff --git a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/MimeMessageHeaders.java b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/MimeMessageHeaders.java
index 2972aa8..6a975a2 100644
--- a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/MimeMessageHeaders.java
+++ b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/MimeMessageHeaders.java
@@ -38,16 +38,15 @@ final class MimeMessageHeaders implements Headers {
private final Map<String, List<String>> headers;
private final List<String> fields;
- public MimeMessageHeaders(MimeMessage message)
- throws MessagingException {
+ public MimeMessageHeaders(MimeMessage message) throws MessagingException {
headers = new HashMap<>();
fields = new LinkedList<>();
- for (Enumeration<String> e = message.getAllHeaderLines(); e
- .hasMoreElements();) {
+ for (Enumeration<String> e = message.getAllHeaderLines(); e.hasMoreElements();) {
String head = e.nextElement();
int p = head.indexOf(':');
- if (p <= 0)
+ if (p <= 0) {
throw new MessagingException("Bad header line: " + head);
+ }
String headerName = head.substring(0, p).trim();
String headerNameLC = headerName.toLowerCase();
fields.add(headerName);
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org
[james-project] 06/14: JAMES-1975 Use ${james.groupId} in DKIM
mailet POM
Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rouazana pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 2d5add9cdf05026861a40d787accc76b5cfe51d8
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Fri Aug 31 19:20:14 2018 +0700
JAMES-1975 Use ${james.groupId} in DKIM mailet POM
---
server/mailet/dkim/pom.xml | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/server/mailet/dkim/pom.xml b/server/mailet/dkim/pom.xml
index 8955c5f..3dfbbf6 100644
--- a/server/mailet/dkim/pom.xml
+++ b/server/mailet/dkim/pom.xml
@@ -38,15 +38,15 @@
<dependencies>
<dependency>
- <groupId>org.apache.james</groupId>
+ <groupId>${james.groupId}</groupId>
<artifactId>apache-mailet-api</artifactId>
</dependency>
<dependency>
- <groupId>org.apache.james</groupId>
+ <groupId>${james.groupId}</groupId>
<artifactId>apache-mailet-base</artifactId>
</dependency>
<dependency>
- <groupId>org.apache.james</groupId>
+ <groupId>${james.groupId}</groupId>
<artifactId>apache-mailet-test</artifactId>
<scope>test</scope>
</dependency>
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org
[james-project] 13/14: JAMES-1975 Fix Checkstyle on DKIM
Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rouazana pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 0ea10a6fed6a4c2efefee8d1b7435abb77afbd18
Author: Gautier DI FOLCO <gd...@linagora.com>
AuthorDate: Wed May 22 09:15:42 2019 +0200
JAMES-1975 Fix Checkstyle on DKIM
---
.../java/org/apache/james/jdkim/mailets/DKIMSign.java | 16 +++++-----------
.../java/org/apache/james/jdkim/mailets/DKIMVerify.java | 3 ++-
2 files changed, 7 insertions(+), 12 deletions(-)
diff --git a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMSign.java b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMSign.java
index c9f8f84..3917f2e 100644
--- a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMSign.java
+++ b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMSign.java
@@ -19,8 +19,6 @@
package org.apache.james.jdkim.mailets;
-import java.io.ByteArrayInputStream;
-import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@@ -39,9 +37,6 @@ import javax.mail.Header;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
-import com.github.fge.lambdas.Throwing;
-import com.google.common.collect.ImmutableList;
-import org.apache.commons.io.IOUtils;
import org.apache.james.jdkim.DKIMSigner;
import org.apache.james.jdkim.api.BodyHasher;
import org.apache.james.jdkim.api.Headers;
@@ -49,7 +44,7 @@ import org.apache.james.jdkim.api.SignatureRecord;
import org.apache.james.jdkim.exceptions.PermFailException;
import org.apache.mailet.Mail;
import org.apache.mailet.base.GenericMailet;
-import org.bouncycastle.jce.provider.JCERSAPrivateCrtKey;
+
import org.bouncycastle.openssl.PEMDecryptorProvider;
import org.bouncycastle.openssl.PEMEncryptedKeyPair;
import org.bouncycastle.openssl.PEMKeyPair;
@@ -57,6 +52,8 @@ import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
import org.bouncycastle.openssl.jcajce.JcePEMDecryptorProviderBuilder;
+import com.github.fge.lambdas.Throwing;
+
/**
* This mailet sign a message using the DKIM protocol
* If the privateKey is encoded using a password then you can pass
@@ -167,14 +164,11 @@ public class DKIMSign extends GenericMailet {
Object pemObject = pemParser.readObject();
JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
KeyPair keyPair;
- if (pemObject instanceof PEMEncryptedKeyPair)
- {
+ if (pemObject instanceof PEMEncryptedKeyPair) {
PEMEncryptedKeyPair pemEncryptedKeyPair = (PEMEncryptedKeyPair) pemObject;
PEMDecryptorProvider decProv = new JcePEMDecryptorProviderBuilder().build(passphrase);
keyPair = converter.getKeyPair(pemEncryptedKeyPair.decryptKeyPair(decProv));
- }
- else
- {
+ } else {
keyPair = converter.getKeyPair((PEMKeyPair) pemObject);
}
diff --git a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMVerify.java b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMVerify.java
index e0db937..92e6015 100644
--- a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMVerify.java
+++ b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMVerify.java
@@ -27,7 +27,6 @@ import java.util.Optional;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
-import com.google.common.annotations.VisibleForTesting;
import org.apache.james.jdkim.DKIMVerifier;
import org.apache.james.jdkim.api.BodyHasher;
import org.apache.james.jdkim.api.Headers;
@@ -39,6 +38,8 @@ import org.apache.mailet.AttributeValue;
import org.apache.mailet.Mail;
import org.apache.mailet.base.GenericMailet;
+import com.google.common.annotations.VisibleForTesting;
+
/**
* This mailet verify a message using the DKIM protocol
*
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org
[james-project] 07/14: JAMES-1975 Move snapshot repository
declaration to parent POM
Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rouazana pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 5baaf6be75e474021724f8b21a0a0dcd258f4821
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Tue Sep 4 10:47:55 2018 +0700
JAMES-1975 Move snapshot repository declaration to parent POM
---
pom.xml | 12 ++++++++++++
server/pom.xml | 12 ------------
2 files changed, 12 insertions(+), 12 deletions(-)
diff --git a/pom.xml b/pom.xml
index 670f5c2..a8ed295 100644
--- a/pom.xml
+++ b/pom.xml
@@ -2754,6 +2754,18 @@
</dependencies>
</dependencyManagement>
+ <repositories>
+ <!-- apache.snapshot is needed for james-project SNAPSHOT, see JAMES-1470 -->
+ <repository>
+ <releases>
+ <enabled>false</enabled>
+ </releases>
+ <id>apache.snapshots</id>
+ <name>Apache Snapshot Repository</name>
+ <url>http://repository.apache.org/snapshots</url>
+ </repository>
+ </repositories>
+
<build>
<pluginManagement>
<plugins>
diff --git a/server/pom.xml b/server/pom.xml
index aafbbb4..0d82ea6 100644
--- a/server/pom.xml
+++ b/server/pom.xml
@@ -111,18 +111,6 @@
<productName>Apache-James Mail Server</productName>
</properties>
- <repositories>
- <!-- apache.snapshot is needed for james-project SNAPSHOT, see JAMES-1470 -->
- <repository>
- <releases>
- <enabled>false</enabled>
- </releases>
- <id>apache.snapshots</id>
- <name>Apache Snapshot Repository</name>
- <url>http://repository.apache.org/snapshots</url>
- </repository>
- </repositories>
-
<build>
<pluginManagement>
<plugins>
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org
[james-project] 12/14: JAMES-1975 Use file for privateKey in
DKIMSign
Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rouazana pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 0371a57a28b1c14e9cb451f179a80b4d0c2d7a40
Author: Gautier DI FOLCO <gd...@linagora.com>
AuthorDate: Tue May 21 17:02:34 2019 +0200
JAMES-1975 Use file for privateKey in DKIMSign
---
.../org/apache/james/jdkim/mailets/DKIMSign.java | 22 +++--------------
.../apache/james/jdkim/mailets/DKIMSignTest.java | 28 ++++++----------------
.../mailet/dkim/src/test/resources/test-dkim.pem | 15 ++++++++++++
3 files changed, 25 insertions(+), 40 deletions(-)
diff --git a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMSign.java b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMSign.java
index 4989f4a..c9f8f84 100644
--- a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMSign.java
+++ b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMSign.java
@@ -67,23 +67,7 @@ import org.bouncycastle.openssl.jcajce.JcePEMDecryptorProviderBuilder;
* <pre><code>
* <mailet match="All" class="DKIMSign">
* <signatureTemplate>v=1; s=selector; d=example.com; h=from:to:received:received; a=rsa-sha256; bh=; b=;</signatureTemplate>
- * <privateKey>
- * -----BEGIN RSA PRIVATE KEY-----
- * MIICXAIBAAKBgQDYDaYKXzwVYwqWbLhmuJ66aTAN8wmDR+rfHE8HfnkSOax0oIoT
- * M5zquZrTLo30870YMfYzxwfB6j/Nz3QdwrUD/t0YMYJiUKyWJnCKfZXHJBJ+yfRH
- * r7oW+UW3cVo9CG2bBfIxsInwYe175g9UjyntJpWueqdEIo1c2bhv9Mp66QIDAQAB
- * AoGBAI8XcwnZi0Sq5N89wF+gFNhnREFo3rsJDaCY8iqHdA5DDlnr3abb/yhipw0I
- * /1HlgC6fIG2oexXOXFWl+USgqRt1kTt9jXhVFExg8mNko2UelAwFtsl8CRjVcYQO
- * cedeH/WM/mXjg2wUqqZenBmlKlD6vNb70jFJeVaDJ/7n7j8BAkEA9NkH2D4Zgj/I
- * OAVYccZYH74+VgO0e7VkUjQk9wtJ2j6cGqJ6Pfj0roVIMUWzoBb8YfErR8l6JnVQ
- * bfy83gJeiQJBAOHk3ow7JjAn8XuOyZx24KcTaYWKUkAQfRWYDFFOYQF4KV9xLSEt
- * ycY0kjsdxGKDudWcsATllFzXDCQF6DTNIWECQEA52ePwTjKrVnLTfCLEG4OgHKvl
- * Zud4amthwDyJWoMEH2ChNB2je1N4JLrABOE+hk+OuoKnKAKEjWd8f3Jg/rkCQHj8
- * mQmogHqYWikgP/FSZl518jV48Tao3iXbqvU9Mo2T6yzYNCCqIoDLFWseNVnCTZ0Q
- * b+IfiEf1UeZVV5o4J+ECQDatNnS3V9qYUKjj/krNRD/U0+7eh8S2ylLqD3RlSn9K
- * tYGRMgAtUXtiOEizBH6bd/orzI9V9sw8yBz+ZqIH25Q=
- * -----END RSA PRIVATE KEY-----
- * </privateKey>
+ * <privateKey>dkim-signing.pem</privateKey>
* </mailet>
* </code></pre>
*
@@ -113,13 +97,13 @@ public class DKIMSign extends GenericMailet {
public void init() throws MessagingException {
signatureTemplate = getInitParameter("signatureTemplate");
- String privateKeyString = getInitParameter("privateKey");
+ String privateKeyFilePath = getInitParameter("privateKey");
String privateKeyPassword = getInitParameter("privateKeyPassword", null);
forceCRLF = getInitParameter("forceCRLF", true);
try {
char[] passphrase = privateKeyPassword != null ? privateKeyPassword.toCharArray() : null;
- privateKey = extractPrivateKey(new ByteArrayInputStream(privateKeyString.getBytes()), passphrase);
+ privateKey = extractPrivateKey(ClassLoader.getSystemResourceAsStream(privateKeyFilePath), passphrase);
} catch (NoSuchAlgorithmException e) {
throw new MessagingException("Unknown private key algorythm: " + e.getMessage(), e);
} catch (InvalidKeySpecException e) {
diff --git a/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMSignTest.java b/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMSignTest.java
index ee69b4c..3ccefa7 100644
--- a/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMSignTest.java
+++ b/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMSignTest.java
@@ -50,21 +50,7 @@ import org.junit.jupiter.api.Test;
public class DKIMSignTest {
- private static final String TESTING_PEM = "-----BEGIN RSA PRIVATE KEY-----\r\n" +
- "MIICXAIBAAKBgQDYDaYKXzwVYwqWbLhmuJ66aTAN8wmDR+rfHE8HfnkSOax0oIoT\r\n" +
- "M5zquZrTLo30870YMfYzxwfB6j/Nz3QdwrUD/t0YMYJiUKyWJnCKfZXHJBJ+yfRH\r\n" +
- "r7oW+UW3cVo9CG2bBfIxsInwYe175g9UjyntJpWueqdEIo1c2bhv9Mp66QIDAQAB\r\n" +
- "AoGBAI8XcwnZi0Sq5N89wF+gFNhnREFo3rsJDaCY8iqHdA5DDlnr3abb/yhipw0I\r\n" +
- "/1HlgC6fIG2oexXOXFWl+USgqRt1kTt9jXhVFExg8mNko2UelAwFtsl8CRjVcYQO\r\n" +
- "cedeH/WM/mXjg2wUqqZenBmlKlD6vNb70jFJeVaDJ/7n7j8BAkEA9NkH2D4Zgj/I\r\n" +
- "OAVYccZYH74+VgO0e7VkUjQk9wtJ2j6cGqJ6Pfj0roVIMUWzoBb8YfErR8l6JnVQ\r\n" +
- "bfy83gJeiQJBAOHk3ow7JjAn8XuOyZx24KcTaYWKUkAQfRWYDFFOYQF4KV9xLSEt\r\n" +
- "ycY0kjsdxGKDudWcsATllFzXDCQF6DTNIWECQEA52ePwTjKrVnLTfCLEG4OgHKvl\r\n" +
- "Zud4amthwDyJWoMEH2ChNB2je1N4JLrABOE+hk+OuoKnKAKEjWd8f3Jg/rkCQHj8\r\n" +
- "mQmogHqYWikgP/FSZl518jV48Tao3iXbqvU9Mo2T6yzYNCCqIoDLFWseNVnCTZ0Q\r\n" +
- "b+IfiEf1UeZVV5o4J+ECQDatNnS3V9qYUKjj/krNRD/U0+7eh8S2ylLqD3RlSn9K\r\n" +
- "tYGRMgAtUXtiOEizBH6bd/orzI9V9sw8yBz+ZqIH25Q=\r\n" +
- "-----END RSA PRIVATE KEY-----\r\n";
+ private static final String TESTING_PEM_FILE = "test-dkim.pem";
private static final FakeMailContext FAKE_MAIL_CONTEXT = FakeMailContext.defaultContext();
@Test
@@ -80,7 +66,7 @@ public class DKIMSignTest {
.setProperty(
"signatureTemplate",
"v=1; s=selector; d=example.com; h=from:to:received:received; a=rsa-sha256; bh=; b=;")
- .setProperty("privateKey", TESTING_PEM)
+ .setProperty("privateKey", TESTING_PEM_FILE)
.build();
mailet.init(mci);
@@ -129,7 +115,7 @@ public class DKIMSignTest {
.setProperty(
"signatureTemplate",
"v=1; t=" + ((System.currentTimeMillis() / 1000) + 1000) + "; s=selector; d=example.com; h=from:to:received:received; a=rsa-sha256; bh=; b=;")
- .setProperty("privateKey", TESTING_PEM)
+ .setProperty("privateKey", TESTING_PEM_FILE)
.build();
mailet.init(mci);
@@ -175,7 +161,7 @@ public class DKIMSignTest {
.setProperty(
"signatureTemplate",
"v=1; t=; s=selector; d=example.com; h=from:to:received:received; a=rsa-sha256; bh=; b=;")
- .setProperty("privateKey", TESTING_PEM)
+ .setProperty("privateKey", TESTING_PEM_FILE)
.build();
mailet.init(mci);
@@ -228,7 +214,7 @@ public class DKIMSignTest {
.setProperty(
"signatureTemplate",
"v=1; s=selector; d=example.com; h=from:to:received:received; a=rsa-sha256; bh=; b=;")
- .setProperty("privateKey", TESTING_PEM)
+ .setProperty("privateKey", TESTING_PEM_FILE)
.build();
mailet.init(mci);
@@ -275,7 +261,7 @@ public class DKIMSignTest {
.setProperty(
"signatureTemplate",
"v=1; s=selector; d=example.com; h=from:to:received:received; a=rsa-sha256; bh=; b=;")
- .setProperty("privateKey", TESTING_PEM)
+ .setProperty("privateKey", TESTING_PEM_FILE)
.build();
Mail mail = FakeMail.builder()
@@ -323,7 +309,7 @@ public class DKIMSignTest {
.setProperty(
"signatureTemplate",
"v=1; s=selector; d=example.com; h=from:to:received:received; a=rsa-sha256; bh=; b=;")
- .setProperty("privateKey", TESTING_PEM)
+ .setProperty("privateKey", TESTING_PEM_FILE)
.build();
Mail mail = FakeMail.builder()
diff --git a/server/mailet/dkim/src/test/resources/test-dkim.pem b/server/mailet/dkim/src/test/resources/test-dkim.pem
new file mode 100644
index 0000000..935e62a
--- /dev/null
+++ b/server/mailet/dkim/src/test/resources/test-dkim.pem
@@ -0,0 +1,15 @@
+-----BEGIN RSA PRIVATE KEY-----
+MIICXAIBAAKBgQDYDaYKXzwVYwqWbLhmuJ66aTAN8wmDR+rfHE8HfnkSOax0oIoT
+M5zquZrTLo30870YMfYzxwfB6j/Nz3QdwrUD/t0YMYJiUKyWJnCKfZXHJBJ+yfRH
+r7oW+UW3cVo9CG2bBfIxsInwYe175g9UjyntJpWueqdEIo1c2bhv9Mp66QIDAQAB
+AoGBAI8XcwnZi0Sq5N89wF+gFNhnREFo3rsJDaCY8iqHdA5DDlnr3abb/yhipw0I
+/1HlgC6fIG2oexXOXFWl+USgqRt1kTt9jXhVFExg8mNko2UelAwFtsl8CRjVcYQO
+cedeH/WM/mXjg2wUqqZenBmlKlD6vNb70jFJeVaDJ/7n7j8BAkEA9NkH2D4Zgj/I
+OAVYccZYH74+VgO0e7VkUjQk9wtJ2j6cGqJ6Pfj0roVIMUWzoBb8YfErR8l6JnVQ
+bfy83gJeiQJBAOHk3ow7JjAn8XuOyZx24KcTaYWKUkAQfRWYDFFOYQF4KV9xLSEt
+ycY0kjsdxGKDudWcsATllFzXDCQF6DTNIWECQEA52ePwTjKrVnLTfCLEG4OgHKvl
+Zud4amthwDyJWoMEH2ChNB2je1N4JLrABOE+hk+OuoKnKAKEjWd8f3Jg/rkCQHj8
+mQmogHqYWikgP/FSZl518jV48Tao3iXbqvU9Mo2T6yzYNCCqIoDLFWseNVnCTZ0Q
+b+IfiEf1UeZVV5o4J+ECQDatNnS3V9qYUKjj/krNRD/U0+7eh8S2ylLqD3RlSn9K
+tYGRMgAtUXtiOEizBH6bd/orzI9V9sw8yBz+ZqIH25Q=
+-----END RSA PRIVATE KEY-----
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org
[james-project] 02/14: JAMES-1975 Solve some basic inteliJ warnings
Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rouazana pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 44b992ad0abdd62c6fb5136a6f174f0c8ffaed2a
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Fri Aug 31 17:42:37 2018 +0700
JAMES-1975 Solve some basic inteliJ warnings
---
.../java/org/apache/james/jdkim/mailets/DKIMSign.java | 2 +-
.../org/apache/james/jdkim/mailets/DKIMVerify.java | 2 +-
.../james/jdkim/mailets/MimeMessageHeaders.java | 6 +++---
.../org/apache/james/jdkim/mailets/DKIMSignTest.java | 6 ++----
.../apache/james/jdkim/mailets/DKIMVerifyTest.java | 19 ++++++-------------
5 files changed, 13 insertions(+), 22 deletions(-)
diff --git a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMSign.java b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMSign.java
index 57cc2b0..a51b672 100644
--- a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMSign.java
+++ b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMSign.java
@@ -150,7 +150,7 @@ public class DKIMSign extends GenericMailet {
private void prependHeader(MimeMessage message, String signatureHeader)
throws MessagingException {
- List<String> prevHeader = new LinkedList<String>();
+ List<String> prevHeader = new LinkedList<>();
// read all the headers
for (Enumeration<String> e = message.getAllHeaderLines(); e.hasMoreElements();) {
String headerLine = e.nextElement();
diff --git a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMVerify.java b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMVerify.java
index ca72876..c3a4f14 100644
--- a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMVerify.java
+++ b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/DKIMVerify.java
@@ -55,7 +55,7 @@ public class DKIMVerify extends GenericMailet {
private boolean forceCRLF;
@Override
- public void init() throws MessagingException {
+ public void init() {
verifier = new DKIMVerifier();
forceCRLF = getInitParameter("forceCRLF", true);
}
diff --git a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/MimeMessageHeaders.java b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/MimeMessageHeaders.java
index 3d2a639..2972aa8 100644
--- a/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/MimeMessageHeaders.java
+++ b/server/mailet/dkim/src/main/java/org/apache/james/jdkim/mailets/MimeMessageHeaders.java
@@ -40,8 +40,8 @@ final class MimeMessageHeaders implements Headers {
public MimeMessageHeaders(MimeMessage message)
throws MessagingException {
- headers = new HashMap<String, List<String>>();
- fields = new LinkedList<String>();
+ headers = new HashMap<>();
+ fields = new LinkedList<>();
for (Enumeration<String> e = message.getAllHeaderLines(); e
.hasMoreElements();) {
String head = e.nextElement();
@@ -53,7 +53,7 @@ final class MimeMessageHeaders implements Headers {
fields.add(headerName);
List<String> strings = headers.get(headerNameLC);
if (strings == null) {
- strings = new LinkedList<String>();
+ strings = new LinkedList<>();
headers.put(headerNameLC, strings);
}
strings.add(head);
diff --git a/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMSignTest.java b/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMSignTest.java
index 83ea4d6..731941a 100644
--- a/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMSignTest.java
+++ b/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMSignTest.java
@@ -267,8 +267,7 @@ public class DKIMSignTest {
mm.setHeader("Content-Transfer-Encoding", "8bit");
mm.saveChanges();
- FakeMailContext FakeMailContext = FAKE_MAIL_CONTEXT;
- FakeMailContext.getServerInfo();
+ FAKE_MAIL_CONTEXT.getServerInfo();
FakeMailetConfig mci = FakeMailetConfig.builder()
.mailetName("Test")
@@ -316,8 +315,7 @@ public class DKIMSignTest {
mm.setHeader("Content-Transfer-Encoding", "8bit");
mm.saveChanges();
- FakeMailContext FakeMailContext = FAKE_MAIL_CONTEXT;
- FakeMailContext.getServerInfo();
+ FAKE_MAIL_CONTEXT.getServerInfo();
FakeMailetConfig mci = FakeMailetConfig.builder()
.mailetName("Test")
diff --git a/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMVerifyTest.java b/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMVerifyTest.java
index 5ee901f..2011965 100644
--- a/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMVerifyTest.java
+++ b/server/mailet/dkim/src/test/java/org/apache/james/jdkim/mailets/DKIMVerifyTest.java
@@ -20,16 +20,13 @@
package org.apache.james.jdkim.mailets;
import java.io.ByteArrayInputStream;
-import java.io.IOException;
import java.util.Properties;
-import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import org.apache.james.jdkim.DKIMVerifier;
import org.apache.james.jdkim.MockPublicKeyRecordRetriever;
-import org.apache.james.jdkim.exceptions.FailException;
import org.apache.mailet.Mail;
import org.apache.mailet.Mailet;
import org.apache.mailet.base.test.FakeMail;
@@ -41,8 +38,7 @@ import org.junit.Test;
public class DKIMVerifyTest {
@Test
- public void testDKIMVerifyPass() throws MessagingException, IOException,
- FailException {
+ public void testDKIMVerifyPass() throws Exception {
String message = "DKIM-Signature: v=1; d=example.com; t=1284762805; b=ZFfwSIzTQM7k9syRnl9VfQh0/dr99euvBe1gn/DiTrnEZjxyjzQBD2MMvowVdbHpPMtSjtCtehU9zZ3urXmj5iHKujpEkP92FEKinzElkQ2eT2zoxdg1zByPHsKPX+KjrBespAJcO2k052aOK5kIBFxpQumP4aiW7ZklBKSWMBk=; s=selector; a=rsa-sha256; bh=rHOD7fd9xnNxK7OSl5ellpQVF14NNFbOIizqtUMhnio=; h=from:to:received:received;\r\n"
+ "Received: by 10.XX.XX.12 with SMTP id dfgskldjfhgkljsdfhgkljdhfg;\r\n\tTue, 06 Oct 2009 07:37:34 -0700 (PDT)\r\nReturn-Path: <bo...@example.com>\r\nReceived: from example.co.uk (example.co.uk [XX.XXX.125.19])\r\n\tby mx.example.com with ESMTP id dgdfgsdfgsd.97.2009.10.06.07.37.32;\r\n\tTue, 06 Oct 2009 07:37:32 -0700 (PDT)\r\nFrom: apache@bago.org\r\nTo: apache@bago.org\r\n\r\nbody\r\nprova\r\n";
@@ -54,8 +50,7 @@ public class DKIMVerifyTest {
}
@Test
- public void testDKIMVerifyFail() throws MessagingException, IOException,
- FailException {
+ public void testDKIMVerifyFail() throws Exception {
// altered message body
String message = "DKIM-Signature: v=1; d=example.com; t=1284762805; b=ZFfwSIzTQM7k9syRnl9VfQh0/dr99euvBe1gn/DiTrnEZjxyjzQBD2MMvowVdbHpPMtSjtCtehU9zZ3urXmj5iHKujpEkP92FEKinzElkQ2eT2zoxdg1zByPHsKPX+KjrBespAJcO2k052aOK5kIBFxpQumP4aiW7ZklBKSWMBk=; s=selector; a=rsa-sha256; bh=rHOD7fd9xnNxK7OSl5ellpQVF14NNFbOIizqtUMhnio=; h=from:to:received:received;\r\n"
+ "Received: by 10.XX.XX.12 with SMTP id dfgskldjfhgkljsdfhgkljdhfg;\r\n\tTue, 06 Oct 2009 07:37:34 -0700 (PDT)\r\nReturn-Path: <bo...@example.com>\r\nReceived: from example.co.uk (example.co.uk [XX.XXX.125.19])\r\n\tby mx.example.com with ESMTP id dgdfgsdfgsd.97.2009.10.06.07.37.32;\r\n\tTue, 06 Oct 2009 07:37:32 -0700 (PDT)\r\nFrom: apache@bago.org\r\nTo: apache@bago.org\r\n\r\nbody\r\nprova altered\r\n";
@@ -68,8 +63,7 @@ public class DKIMVerifyTest {
}
@Test
- public void testDKIMVerifyFailInvalid() throws MessagingException, IOException,
- FailException {
+ public void testDKIMVerifyFailInvalid() throws Exception {
// invalid version v=2
String message = "DKIM-Signature: v=2; d=example.com; t=1284762805; b=ZFfwSIzTQM7k9syRnl9VfQh0/dr99euvBe1gn/DiTrnEZjxyjzQBD2MMvowVdbHpPMtSjtCtehU9zZ3urXmj5iHKujpEkP92FEKinzElkQ2eT2zoxdg1zByPHsKPX+KjrBespAJcO2k052aOK5kIBFxpQumP4aiW7ZklBKSWMBk=; s=selector; a=rsa-sha256; bh=rHOD7fd9xnNxK7OSl5ellpQVF14NNFbOIizqtUMhnio=; h=from:to:received:received;\r\n"
+ "Received: by 10.XX.XX.12 with SMTP id dfgskldjfhgkljsdfhgkljdhfg;\r\n\tTue, 06 Oct 2009 07:37:34 -0700 (PDT)\r\nReturn-Path: <bo...@example.com>\r\nReceived: from example.co.uk (example.co.uk [XX.XXX.125.19])\r\n\tby mx.example.com with ESMTP id dgdfgsdfgsd.97.2009.10.06.07.37.32;\r\n\tTue, 06 Oct 2009 07:37:32 -0700 (PDT)\r\nFrom: apache@bago.org\r\nTo: apache@bago.org\r\n\r\nbody\r\nprova\r\n";
@@ -82,8 +76,7 @@ public class DKIMVerifyTest {
}
@Test
- public void testDKIMVerifyNeutral() throws MessagingException, IOException,
- FailException {
+ public void testDKIMVerifyNeutral() throws Exception {
// no signatures!
String message = ""
+ "Received: by 10.XX.XX.12 with SMTP id dfgskldjfhgkljsdfhgkljdhfg;\r\n\tTue, 06 Oct 2009 07:37:34 -0700 (PDT)\r\nReturn-Path: <bo...@example.com>\r\nReceived: from example.co.uk (example.co.uk [XX.XXX.125.19])\r\n\tby mx.example.com with ESMTP id dgdfgsdfgsd.97.2009.10.06.07.37.32;\r\n\tTue, 06 Oct 2009 07:37:32 -0700 (PDT)\r\nFrom: apache@bago.org\r\nTo: apache@bago.org\r\n\r\nbody\r\nprova altered\r\n";
@@ -95,11 +88,11 @@ public class DKIMVerifyTest {
Assert.assertTrue(attr.startsWith("neutral"));
}
- private Mail process(String message) throws MessagingException {
+ private Mail process(String message) throws Exception {
Mailet mailet = new DKIMVerify() {
@Override
- public void init() throws MessagingException {
+ public void init() {
verifier = new DKIMVerifier(new MockPublicKeyRecordRetriever(
"v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDYDaYKXzwVYwqWbLhmuJ66aTAN8wmDR+rfHE8HfnkSOax0oIoTM5zquZrTLo30870YMfYzxwfB6j/Nz3QdwrUD/t0YMYJiUKyWJnCKfZXHJBJ+yfRHr7oW+UW3cVo9CG2bBfIxsInwYe175g9UjyntJpWueqdEIo1c2bhv9Mp66QIDAQAB;",
"selector", "example.com"));
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org
[james-project] 04/14: JAMES-1975 Clean james-server-mailet-dkim
dependencies
Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rouazana pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 3accc706f915d21856d837950c3cfbab3e91971a
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Fri Aug 31 18:02:12 2018 +0700
JAMES-1975 Clean james-server-mailet-dkim dependencies
---
server/mailet/dkim/pom.xml | 9 ---------
1 file changed, 9 deletions(-)
diff --git a/server/mailet/dkim/pom.xml b/server/mailet/dkim/pom.xml
index 4d4076a..8955c5f 100644
--- a/server/mailet/dkim/pom.xml
+++ b/server/mailet/dkim/pom.xml
@@ -83,10 +83,6 @@
</exclusions>
</dependency>
<dependency>
- <groupId>commons-logging</groupId>
- <artifactId>commons-logging</artifactId>
- </dependency>
- <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
@@ -96,11 +92,6 @@
<artifactId>not-yet-commons-ssl</artifactId>
<version>0.3.11</version>
</dependency>
- <dependency>
- <groupId>commons-codec</groupId>
- <artifactId>commons-codec</artifactId>
- <version>1.7</version>
- </dependency>
</dependencies>
</project>
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org