Skip to content

Commit

Permalink
#52 Bug when using File with dkim signing
Browse files Browse the repository at this point in the history
  • Loading branch information
bbottema committed Nov 7, 2016
1 parent e4963a0 commit fe10642
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
31 changes: 13 additions & 18 deletions src/main/java/org/simplejavamail/email/Email.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.util.ByteArrayDataSource;
import java.io.*;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -73,6 +75,7 @@ public class Email {
*/
private boolean applyDKIMSignature = false;
private InputStream dkimPrivateKeyInputStream;
private File dkimPrivateKeyFile; // supported seperately, so we don't have to do resource management ourselves for the InputStream
private String signingDomain;
private String selector;

Expand Down Expand Up @@ -106,26 +109,14 @@ public Email() {
}

/**
* @see #signWithDomainKey(InputStream, String, String)
* As {@link #signWithDomainKey(InputStream, String, String)}, but with a File reference that is later read as {@code InputStream}.
*/
@SuppressWarnings("WeakerAccess")
public void signWithDomainKey(final File dkimPrivateKeyFile, final String signingDomain, final String selector) {
FileInputStream dkimPrivateKeyInputStream = null;
try {
dkimPrivateKeyInputStream = new FileInputStream(dkimPrivateKeyFile);
signWithDomainKey(dkimPrivateKeyInputStream, signingDomain, selector);
} catch (final FileNotFoundException e) {
throw new EmailException(format(EmailException.DKIM_ERROR_INVALID_FILE, dkimPrivateKeyFile), e);
} finally {
if (dkimPrivateKeyInputStream != null) {
try {
dkimPrivateKeyInputStream.close();
} catch (final IOException e) {
//noinspection ThrowFromFinallyBlock
throw new EmailException(format(EmailException.DKIM_ERROR_UNCLOSABLE_INPUTSTREAM, e.getMessage()), e);
}
}
}
this.applyDKIMSignature = true;
this.dkimPrivateKeyFile = dkimPrivateKeyFile;
this.signingDomain = signingDomain;
this.selector = selector;
}

/**
Expand Down Expand Up @@ -342,6 +333,10 @@ public InputStream getDkimPrivateKeyInputStream() {
return dkimPrivateKeyInputStream;
}

public File getDkimPrivateKeyFile() {
return dkimPrivateKeyFile;
}

public String getSigningDomain() {
return signingDomain;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,16 @@ static String determineResourceName(AttachmentResource attachmentResource, boole
*/
public static MimeMessage signMessageWithDKIM(final MimeMessage message, final Email email) {
try {
final DkimSigner dkimSigner = new DkimSigner(email.getSigningDomain(), email.getSelector(),
email.getDkimPrivateKeyInputStream());
final DkimSigner dkimSigner;
if (email.getDkimPrivateKeyFile() != null) {
// InputStream is managed by Dkim library
dkimSigner = new DkimSigner(email.getSigningDomain(), email.getSelector(),
email.getDkimPrivateKeyFile());
} else {
// InputStream is managed by SimpleJavaMail user
dkimSigner = new DkimSigner(email.getSigningDomain(), email.getSelector(),
email.getDkimPrivateKeyInputStream());
}
dkimSigner.setIdentity(email.getFromRecipient().getAddress());
dkimSigner.setHeaderCanonicalization(Canonicalization.SIMPLE);
dkimSigner.setBodyCanonicalization(Canonicalization.RELAXED);
Expand Down

0 comments on commit fe10642

Please sign in to comment.