Skip to content

Commit

Permalink
#498: Finally implement builder API to configure S/MIME signing and/o…
Browse files Browse the repository at this point in the history
…r encryption algorithms, and actually use it. Added tests to test some variations, including both RSA (already present) and DSA (added new key for this in pkcs store).
  • Loading branch information
bbottema committed Mar 20, 2024
1 parent c958f53 commit 0c681f2
Show file tree
Hide file tree
Showing 34 changed files with 889 additions and 509 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.simplejavamail.api.email.config.DkimConfig;
import org.simplejavamail.api.email.config.SmimeEncryptionConfig;
import org.simplejavamail.api.email.config.SmimeSigningConfig;
import org.simplejavamail.api.internal.smimesupport.model.PlainSmimeDetails;
import org.simplejavamail.api.mailer.config.Pkcs12Config;
import org.simplejavamail.internal.config.EmailProperty;
import org.simplejavamail.internal.util.MiscUtil;

import java.io.InputStream;
import java.io.File;
import java.io.Serializable;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
Expand Down Expand Up @@ -192,17 +192,17 @@ public class Email implements Serializable {
private final DkimConfig dkimConfig;

/**
* @see EmailPopulatingBuilder#signWithSmime(Pkcs12Config)
* @see EmailPopulatingBuilder#signWithSmime(InputStream, String, String, String)
* @see EmailPopulatingBuilder#encryptWithSmime(SmimeEncryptionConfig)
* @see EmailPopulatingBuilder#encryptWithSmime(File, String, String)
*/
private final X509Certificate x509CertificateForSmimeEncryption;
private final SmimeEncryptionConfig smimeEncryptionConfig;

/**
* @see EmailPopulatingBuilder#encryptWithSmime(X509Certificate)
* @see EmailPopulatingBuilder#encryptWithSmime(InputStream)
* @see EmailPopulatingBuilder#signWithSmime(SmimeSigningConfig)
* @see EmailPopulatingBuilder#signWithSmime(File, String, String, String, String)
*/
// data source is not serializable, so transient
private final transient Pkcs12Config pkcs12ConfigForSmimeSigning;
private transient final SmimeSigningConfig smimeSigningConfig;

/**
* @see EmailPopulatingBuilder#getSmimeSignedEmail()
Expand Down Expand Up @@ -274,8 +274,8 @@ public Email(@NotNull final EmailPopulatingBuilder builder) {
emailToForward = builder.getEmailToForward();
originalSmimeDetails = builder.getOriginalSmimeDetails();
sentDate = builder.getSentDate();
x509CertificateForSmimeEncryption = builder.getX509CertificateForSmimeEncryption();
pkcs12ConfigForSmimeSigning = builder.getPkcs12ConfigForSmimeSigning();
smimeEncryptionConfig = builder.getSmimeEncryptionConfig();
smimeSigningConfig = builder.getSmimeSigningConfig();
dkimConfig = builder.getDkimConfig();
}

Expand Down Expand Up @@ -335,18 +335,18 @@ public String toString() {
s += ",\n\tforwardingEmail=true";
}

if (smimeSignedEmail != null || pkcs12ConfigForSmimeSigning != null
|| x509CertificateForSmimeEncryption != null || !(originalSmimeDetails instanceof PlainSmimeDetails)) {
if (smimeSignedEmail != null || smimeSigningConfig != null
|| smimeEncryptionConfig != null || !(originalSmimeDetails instanceof PlainSmimeDetails)) {
s += ",\n\tsmime details: {\n";
s += "\t----------------------\n";
if (smimeSignedEmail != null) {
s += "\t\tsmimeSignedEmail=" + smimeSignedEmail + ",\n";
}
if (pkcs12ConfigForSmimeSigning != null) {
s += "\t\tpkcs12ConfigForSmimeSigning=" + pkcs12ConfigForSmimeSigning + ",\n";
if (smimeSigningConfig != null) {
s += "\t\tsmimeSigningConfig=" + smimeSigningConfig + ",\n";
}
if (x509CertificateForSmimeEncryption != null) {
s += "\t\tx509CertificateForSmimeEncryption=" + x509CertificateForSmimeEncryption;
if (smimeEncryptionConfig != null) {
s += "\t\tsmimeEncryptionConfig=" + smimeEncryptionConfig;
}
s += "\t\toriginalSmimeDetails=" + originalSmimeDetails + "\n";
s += "\t----------------------\n\t}";
Expand Down Expand Up @@ -600,23 +600,22 @@ public Map<String, Collection<String>> getHeaders() {
public DkimConfig getDkimConfig() {
return dkimConfig;
}

/**
* @see EmailPopulatingBuilder#signWithSmime(Pkcs12Config)
* @see EmailPopulatingBuilder#signWithSmime(InputStream, String, String, String)
* @see EmailPopulatingBuilder#encryptWithSmime(SmimeEncryptionConfig)
* @see EmailPopulatingBuilder#encryptWithSmime(File, String, String)
*/
@Nullable
public X509Certificate getX509CertificateForSmimeEncryption() {
return x509CertificateForSmimeEncryption;
public SmimeEncryptionConfig getSmimeEncryptionConfig() {
return smimeEncryptionConfig;
}

/**
* @see EmailPopulatingBuilder#encryptWithSmime(X509Certificate)
* @see EmailPopulatingBuilder#encryptWithSmime(InputStream)
* @see EmailPopulatingBuilder#signWithSmime(SmimeSigningConfig)
* @see EmailPopulatingBuilder#signWithSmime(File, String, String, String, String)
*/
@Nullable
public Pkcs12Config getPkcs12ConfigForSmimeSigning() {
return pkcs12ConfigForSmimeSigning;
public SmimeSigningConfig getSmimeSigningConfig() {
return smimeSigningConfig;
}

/**
Expand Down
Loading

0 comments on commit 0c681f2

Please sign in to comment.