Skip to content

Commit f9c89f4

Browse files
committed
minor refactoring, added contributed SMIMEEnvelopedUtil, added support for encryption check on SMIMEAuthEnvelopedData messages to SMIMEToolkit.
1 parent 0106cb2 commit f9c89f4

File tree

6 files changed

+221
-126
lines changed

6 files changed

+221
-126
lines changed

mail/src/main/java/org/bouncycastle/mail/smime/SMIMEAuthEnvelopedGenerator.java

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
package org.bouncycastle.mail.smime;
22

3-
import org.bouncycastle.asn1.ASN1EncodableVector;
4-
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
5-
import org.bouncycastle.cms.*;
6-
import org.bouncycastle.operator.OutputAEADEncryptor;
7-
import org.bouncycastle.operator.OutputEncryptor;
3+
import java.io.IOException;
4+
import java.io.OutputStream;
5+
import java.security.AccessController;
6+
import java.security.PrivilegedAction;
87

98
import javax.activation.CommandMap;
109
import javax.activation.MailcapCommandMap;
1110
import javax.mail.MessagingException;
1211
import javax.mail.internet.MimeBodyPart;
13-
import java.io.IOException;
14-
import java.io.OutputStream;
15-
import java.security.AccessController;
16-
import java.security.PrivilegedAction;
12+
13+
import org.bouncycastle.asn1.ASN1EncodableVector;
14+
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
15+
import org.bouncycastle.cms.CMSAuthEnvelopedDataGenerator;
16+
import org.bouncycastle.cms.CMSAuthEnvelopedDataStreamGenerator;
17+
import org.bouncycastle.cms.CMSException;
18+
import org.bouncycastle.cms.RecipientInfoGenerator;
19+
import org.bouncycastle.operator.OutputAEADEncryptor;
20+
import org.bouncycastle.operator.OutputEncryptor;
1721

1822
/**
1923
* General class for generating a pkcs7-mime message using AEAD algorithm.
20-
*
24+
* <p>
2125
* A simple example of usage.
2226
*
2327
* <pre>
@@ -34,11 +38,11 @@
3438
public class SMIMEAuthEnvelopedGenerator
3539
extends SMIMEEnvelopedGenerator
3640
{
37-
public static final String AES128_GCM = CMSAuthEnvelopedDataGenerator.AES128_GCM;
38-
public static final String AES192_GCM = CMSAuthEnvelopedDataGenerator.AES192_GCM;
39-
public static final String AES256_GCM = CMSAuthEnvelopedDataGenerator.AES256_GCM;
41+
public static final String AES128_GCM = CMSAuthEnvelopedDataGenerator.AES128_GCM;
42+
public static final String AES192_GCM = CMSAuthEnvelopedDataGenerator.AES192_GCM;
43+
public static final String AES256_GCM = CMSAuthEnvelopedDataGenerator.AES256_GCM;
4044

41-
private static final String AUTH_ENCRYPTED_CONTENT_TYPE = "application/pkcs7-mime; name=\"smime.p7m\"; smime-type=authEnveloped-data";
45+
static final String AUTH_ENVELOPED_DATA_CONTENT_TYPE = "application/pkcs7-mime; name=\"smime.p7m\"; smime-type=authEnveloped-data";
4246

4347
final private AuthEnvelopedGenerator authFact;
4448

@@ -90,19 +94,21 @@ public void setBerEncodeRecipients(
9094
/**
9195
* return encrypted content type for enveloped data.
9296
*/
93-
protected String getEncryptedContentType() {
94-
return AUTH_ENCRYPTED_CONTENT_TYPE;
97+
protected String getEncryptedContentType()
98+
{
99+
return AUTH_ENVELOPED_DATA_CONTENT_TYPE;
95100
}
96101

97102
/**
98103
* return content encryptor.
99104
*/
100105
protected SMIMEStreamingProcessor getContentEncryptor(
101-
MimeBodyPart content,
102-
OutputEncryptor encryptor)
103-
throws SMIMEException
106+
MimeBodyPart content,
107+
OutputEncryptor encryptor)
108+
throws SMIMEException
104109
{
105-
if (encryptor instanceof OutputAEADEncryptor) {
110+
if (encryptor instanceof OutputAEADEncryptor)
111+
{
106112
return new ContentEncryptor(content, (OutputAEADEncryptor)encryptor);
107113
}
108114
// this would happen if the encryption algorithm is not AEAD algorithm
@@ -113,12 +119,12 @@ private static class AuthEnvelopedGenerator
113119
extends CMSAuthEnvelopedDataStreamGenerator
114120
{
115121
private ASN1ObjectIdentifier dataType;
116-
private ASN1EncodableVector recipientInfos;
122+
private ASN1EncodableVector recipientInfos;
117123

118124
protected OutputStream open(
119125
ASN1ObjectIdentifier dataType,
120-
OutputStream out,
121-
ASN1EncodableVector recipientInfos,
126+
OutputStream out,
127+
ASN1EncodableVector recipientInfos,
122128
OutputAEADEncryptor encryptor)
123129
throws IOException
124130
{
@@ -130,31 +136,31 @@ protected OutputStream open(
130136

131137
OutputStream regenerate(
132138
OutputStream out,
133-
OutputAEADEncryptor encryptor)
139+
OutputAEADEncryptor encryptor)
134140
throws IOException
135141
{
136142
return super.open(dataType, out, recipientInfos, encryptor);
137143
}
138144
}
139145

140146
private class ContentEncryptor
141-
implements SMIMEStreamingProcessor
147+
implements SMIMEStreamingProcessor
142148
{
143149
private final MimeBodyPart _content;
144150
private OutputAEADEncryptor _encryptor;
145151

146152
private boolean _firstTime = true;
147153

148154
ContentEncryptor(
149-
MimeBodyPart content,
150-
OutputAEADEncryptor encryptor)
155+
MimeBodyPart content,
156+
OutputAEADEncryptor encryptor)
151157
{
152158
_content = content;
153159
_encryptor = encryptor;
154160
}
155161

156162
public void write(OutputStream out)
157-
throws IOException
163+
throws IOException
158164
{
159165
OutputStream encrypted;
160166

mail/src/main/java/org/bouncycastle/mail/smime/SMIMEEnvelopedGenerator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ public class SMIMEEnvelopedGenerator
6262
public static final String SEED_WRAP = CMSEnvelopedDataGenerator.SEED_WRAP;
6363

6464
public static final String ECDH_SHA1KDF = CMSEnvelopedDataGenerator.ECDH_SHA1KDF;
65-
66-
private static final String ENCRYPTED_CONTENT_TYPE = "application/pkcs7-mime; name=\"smime.p7m\"; smime-type=enveloped-data";
6765

66+
static final String ENVELOPED_DATA_CONTENT_TYPE = "application/pkcs7-mime; name=\"smime.p7m\"; smime-type=enveloped-data";
67+
6868
private EnvelopedGenerator fact;
6969

7070
static
@@ -116,7 +116,7 @@ public void setBerEncodeRecipients(
116116
* return encrypted content type for enveloped data.
117117
*/
118118
protected String getEncryptedContentType() {
119-
return ENCRYPTED_CONTENT_TYPE;
119+
return ENVELOPED_DATA_CONTENT_TYPE;
120120
}
121121

122122
/**
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.bouncycastle.mail.smime;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
import javax.mail.MessagingException;
7+
import javax.mail.internet.MimeBodyPart;
8+
9+
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
10+
import org.bouncycastle.asn1.nist.NISTObjectIdentifiers;
11+
import org.bouncycastle.cms.CMSException;
12+
import org.bouncycastle.cms.RecipientInformationStore;
13+
14+
public class SMIMEEnvelopedUtil
15+
{
16+
private static Set<ASN1ObjectIdentifier> authOIDs = new HashSet<ASN1ObjectIdentifier>();
17+
18+
static
19+
{
20+
authOIDs.add(NISTObjectIdentifiers.id_aes128_GCM);
21+
authOIDs.add(NISTObjectIdentifiers.id_aes128_GCM);
22+
authOIDs.add(NISTObjectIdentifiers.id_aes128_GCM);
23+
}
24+
25+
/**
26+
* Parse the passed in MimeMessage extracting the RecipientInfos from it.
27+
*
28+
* @param message the message to be parsed.
29+
* @return the RecipientInformation store for the passed in message.
30+
* @throws MessagingException
31+
* @throws CMSException
32+
*/
33+
public static RecipientInformationStore getRecipientInfos(MimeBodyPart message) throws MessagingException, CMSException
34+
{
35+
if(message.getContentType().equals(SMIMEAuthEnvelopedGenerator.AUTH_ENVELOPED_DATA_CONTENT_TYPE))
36+
{
37+
return new SMIMEAuthEnveloped(message).getRecipientInfos();
38+
}
39+
return new SMIMEEnveloped(message).getRecipientInfos();
40+
}
41+
42+
/**
43+
* Utility method which will return an SMIMEEnvelopedGenerator or an
44+
* SMIMEAuthEnvelopedGenerator as appropriate for the algorithm OID passed in.
45+
*
46+
* @param algorithm algorithm OID
47+
* @return a SMIME Enveloped Generator class.
48+
*/
49+
public static SMIMEEnvelopedGenerator createGenerator(ASN1ObjectIdentifier algorithm)
50+
{
51+
if (authOIDs.contains(algorithm))
52+
{
53+
return new SMIMEAuthEnvelopedGenerator();
54+
}
55+
return new SMIMEEnvelopedGenerator();
56+
}
57+
}

mail/src/main/java/org/bouncycastle/mail/smime/SMIMEToolkit.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ public SMIMEToolkit(DigestCalculatorProvider digestCalculatorProvider)
5454
public boolean isEncrypted(Part message)
5555
throws MessagingException
5656
{
57-
return message.getHeader("Content-Type")[0].equals("application/pkcs7-mime; name=\"smime.p7m\"; smime-type=enveloped-data");
57+
String mainContentType = message.getHeader("Content-Type")[0];
58+
59+
return mainContentType.equals(SMIMEEnvelopedGenerator.ENVELOPED_DATA_CONTENT_TYPE)
60+
|| mainContentType.equals(SMIMEAuthEnvelopedGenerator.AUTH_ENVELOPED_DATA_CONTENT_TYPE);
5861
}
5962

6063
/**

0 commit comments

Comments
 (0)