Skip to content
This repository has been archived by the owner on Jan 6, 2022. It is now read-only.

support specifying the encryption algorithm #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.markenwerk</groupId>
<!--<groupId>net.markenwerk</groupId>-->
<groupId>com.vnomicscorp</groupId>
<artifactId>utils-mail-smime</artifactId>
<version>1.0.8</version>
<!-- 1.0.8 is the original version of this branched project. The next number is the vnomics specific build number-->
<version>1.0.8.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>${project.groupId}:${project.artifactId}</name>
Expand Down Expand Up @@ -292,4 +294,4 @@
</build>
</profile>
</profiles>
</project>
</project>
31 changes: 27 additions & 4 deletions src/main/java/net/markenwerk/utils/mail/smime/SmimeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import javax.mail.internet.MimePart;

import org.bouncycastle.asn1.ASN1EncodableVector;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.cms.AttributeTable;
import org.bouncycastle.asn1.cms.IssuerAndSerialNumber;
import org.bouncycastle.asn1.smime.SMIMECapabilitiesAttribute;
Expand Down Expand Up @@ -133,12 +134,34 @@ private static void updateMailcapCommandMap() {
* @return The new S/MIME encrypted {@link MimeMessage}.
*/
public static MimeMessage encrypt(Session session, MimeMessage mimeMessage, X509Certificate certificate) {
return encrypt(session, mimeMessage, certificate, CMSAlgorithm.DES_EDE3_CBC);
}

/**
* Encrypts a MIME message and yields a new S/MIME encrypted MIME message.
*
* @param session
* The {@link Session} that is used in conjunction with the
* original {@link MimeMessage}.
* @param mimeMessage
* The original {@link MimeMessage} to be encrypted.
* @param certificate
* The {@link X509Certificate} used to obtain the
* {@link PublicKey} to encrypt the original message with.
* @param cmsAlgorithm
* The {@link ASN1ObjectIdentifier} cooresponding to the CMS Algorithm
* used to encrypt the email. This is typically a constant found
* in {@link CMSAlgorithm}
*
* @return The new S/MIME encrypted {@link MimeMessage}.
*/
public static MimeMessage encrypt(Session session, MimeMessage mimeMessage, X509Certificate certificate, ASN1ObjectIdentifier cmsAlgorithm) {
try {
MimeMessage encryptedMimeMessage = new MimeMessage(session);
copyHeaders(mimeMessage, encryptedMimeMessage);

SMIMEEnvelopedGenerator generator = prepareGenerator(certificate);
OutputEncryptor encryptor = prepareEncryptor();
OutputEncryptor encryptor = prepareEncryptor(cmsAlgorithm);

MimeBodyPart encryptedMimeBodyPart = generator.generate(mimeMessage, encryptor);
copyContent(encryptedMimeBodyPart, encryptedMimeMessage);
Expand All @@ -164,7 +187,7 @@ public static MimeMessage encrypt(Session session, MimeMessage mimeMessage, X509
public static MimeBodyPart encrypt(MimeBodyPart mimeBodyPart, X509Certificate certificate) {
try {
SMIMEEnvelopedGenerator generator = prepareGenerator(certificate);
OutputEncryptor encryptor = prepareEncryptor();
OutputEncryptor encryptor = prepareEncryptor(CMSAlgorithm.DES_EDE3_CBC);

MimeBodyPart encryptedMimeBodyPart = generator.generate(mimeBodyPart, encryptor);
return encryptedMimeBodyPart;
Expand Down Expand Up @@ -202,8 +225,8 @@ private static SMIMEEnvelopedGenerator prepareGenerator(X509Certificate certific
return generator;
}

private static OutputEncryptor prepareEncryptor() throws CMSException {
return new JceCMSContentEncryptorBuilder(CMSAlgorithm.DES_EDE3_CBC).setProvider(
private static OutputEncryptor prepareEncryptor(ASN1ObjectIdentifier cmsAlgorithm) throws CMSException {
return new JceCMSContentEncryptorBuilder(cmsAlgorithm).setProvider(
BouncyCastleProvider.PROVIDER_NAME).build();
}

Expand Down