Skip to content

Commit 9a3d295

Browse files
Merge branch 'bcgit:main' into main
2 parents 8b604e4 + b6d5bd9 commit 9a3d295

17 files changed

Lines changed: 89 additions & 278 deletions

File tree

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

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

33
import java.util.HashSet;
4-
import java.util.Set;
54

65
import javax.mail.MessagingException;
76
import javax.mail.internet.MimeBodyPart;
@@ -13,13 +12,13 @@
1312

1413
public class SMIMEEnvelopedUtil
1514
{
16-
private static Set<ASN1ObjectIdentifier> authOIDs = new HashSet<ASN1ObjectIdentifier>();
15+
private static final HashSet<ASN1ObjectIdentifier> AUTH_OIDS = new HashSet<ASN1ObjectIdentifier>();
1716

1817
static
1918
{
20-
authOIDs.add(NISTObjectIdentifiers.id_aes128_GCM);
21-
authOIDs.add(NISTObjectIdentifiers.id_aes128_GCM);
22-
authOIDs.add(NISTObjectIdentifiers.id_aes128_GCM);
19+
AUTH_OIDS.add(NISTObjectIdentifiers.id_aes128_GCM);
20+
AUTH_OIDS.add(NISTObjectIdentifiers.id_aes192_GCM);
21+
AUTH_OIDS.add(NISTObjectIdentifiers.id_aes256_GCM);
2322
}
2423

2524
/**
@@ -32,7 +31,7 @@ public class SMIMEEnvelopedUtil
3231
*/
3332
public static RecipientInformationStore getRecipientInfos(MimeBodyPart message) throws MessagingException, CMSException
3433
{
35-
if(message.getContentType().equals(SMIMEAuthEnvelopedGenerator.AUTH_ENVELOPED_DATA_CONTENT_TYPE))
34+
if (message.getContentType().equals(SMIMEAuthEnvelopedGenerator.AUTH_ENVELOPED_DATA_CONTENT_TYPE))
3635
{
3736
return new SMIMEAuthEnveloped(message).getRecipientInfos();
3837
}
@@ -48,7 +47,7 @@ public static RecipientInformationStore getRecipientInfos(MimeBodyPart message)
4847
*/
4948
public static SMIMEEnvelopedGenerator createGenerator(ASN1ObjectIdentifier algorithm)
5049
{
51-
if (authOIDs.contains(algorithm))
50+
if (AUTH_OIDS.contains(algorithm))
5251
{
5352
return new SMIMEAuthEnvelopedGenerator();
5453
}

pkix/src/main/java/org/bouncycastle/cms/CMSAuthEnvelopedDataStreamGenerator.java

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.io.IOException;
44
import java.io.OutputStream;
5-
import java.util.Collections;
65

76
import org.bouncycastle.asn1.ASN1EncodableVector;
87
import org.bouncycastle.asn1.ASN1Integer;
@@ -57,17 +56,6 @@ public void setBEREncodeRecipients(
5756
_berEncodeRecipientSet = berEncodeRecipientSet;
5857
}
5958

60-
private OutputStream doOpen(
61-
ASN1ObjectIdentifier dataType,
62-
OutputStream out,
63-
OutputAEADEncryptor encryptor)
64-
throws IOException, CMSException
65-
{
66-
ASN1EncodableVector recipientInfos = CMSUtils.getRecipentInfos(encryptor.getKey(), recipientInfoGenerators);
67-
68-
return open(dataType, out, recipientInfos, encryptor);
69-
}
70-
7159
protected OutputStream open(
7260
ASN1ObjectIdentifier dataType,
7361
OutputStream out,
@@ -124,22 +112,41 @@ protected OutputStream open(
124112
}
125113

126114
/**
127-
* generate an enveloped object that contains an CMS Enveloped Data object using the given encryptor.
115+
* Generate authenticated-enveloped-data using the given encryptor, and marking the encapsulated
116+
* bytes as being of type DATA.
117+
* <p>
118+
* <b>Stream handling note:</b> Closing the returned stream finalizes the CMS structure but <b>does
119+
* not close</b> the underlying output stream. The caller remains responsible for managing the
120+
* lifecycle of {@code out}.
121+
*
122+
* @param out the output stream to write the CMS structure to
123+
* @param encryptor the cipher to use for encryption
124+
* @return an output stream that writes encrypted and authenticated content
125+
*/
126+
public OutputStream open(OutputStream out, OutputAEADEncryptor encryptor) throws CMSException, IOException
127+
{
128+
return open(CMSObjectIdentifiers.data, out, encryptor);
129+
}
130+
131+
/**
132+
* Generate authenticated-enveloped-data using the given encryptor, and marking the encapsulated
133+
* bytes as being of the passed in type.
128134
* <p>
129135
* <b>Stream handling note:</b> Closing the returned stream finalizes the CMS structure but
130136
* <b>does not close</b> the underlying output stream. The caller remains responsible for
131137
* managing the lifecycle of {@code out}.
132138
*
139+
* @param dataType the type of the data being written to the object.
133140
* @param out the output stream to write the CMS structure to
134141
* @param encryptor the cipher to use for encryption
135142
* @return an output stream that writes encrypted and authenticated content
136143
*/
137-
public OutputStream open(
138-
OutputStream out,
139-
OutputAEADEncryptor encryptor)
144+
public OutputStream open(ASN1ObjectIdentifier dataType, OutputStream out, OutputAEADEncryptor encryptor)
140145
throws CMSException, IOException
141146
{
142-
return doOpen(new ASN1ObjectIdentifier(CMSObjectIdentifiers.data.getId()), out, encryptor);
147+
ASN1EncodableVector recipientInfos = CMSUtils.getRecipentInfos(encryptor.getKey(), recipientInfoGenerators);
148+
149+
return open(dataType, out, recipientInfos, encryptor);
143150
}
144151

145152
private class CMSAuthEnvelopedDataOutputStream
@@ -206,7 +213,7 @@ public void close()
206213

207214
_envGen.addObject(new DEROctetString(_encryptor.getMAC()));
208215

209-
CMSUtils.addAttriSetToGenerator(_envGen, unauthAttrsGenerator, 2, Collections.EMPTY_MAP);
216+
CMSUtils.addAttriSetToGenerator(_envGen, unauthAttrsGenerator, 2, CMSUtils.getEmptyParameters());
210217

211218
_envGen.close();
212219
_cGen.close();

pkix/src/main/java/org/bouncycastle/cms/CMSAuthEnvelopedGenerator.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,4 @@ public void setOriginatorInfo(OriginatorInformation originatorInfo)
4343
{
4444
this.originatorInfo = originatorInfo.toASN1Structure();
4545
}
46-
47-
/**
48-
* Add a generator to produce the recipient info required.
49-
*
50-
* @param recipientGenerator a generator of a recipient info object.
51-
*/
52-
public void addRecipientInfoGenerator(RecipientInfoGenerator recipientGenerator)
53-
{
54-
recipientInfoGenerators.add(recipientGenerator);
55-
}
5646
}

pkix/src/main/java/org/bouncycastle/cms/CMSAuthenticatedDataStreamGenerator.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ public class CMSAuthenticatedDataStreamGenerator
4747
// private Object _unprotectedAttributes = null;
4848
private int bufferSize;
4949
private boolean berEncodeRecipientSet;
50-
private MacCalculator macCalculator;
5150

5251
/**
5352
* base constructor
@@ -133,8 +132,6 @@ public OutputStream open(
133132
DigestCalculator digestCalculator)
134133
throws CMSException
135134
{
136-
this.macCalculator = macCalculator;
137-
138135
try
139136
{
140137
ASN1EncodableVector recipientInfos = CMSUtils.getRecipentInfos(macCalculator.getKey(), recipientInfoGenerators);
@@ -254,7 +251,11 @@ public void close()
254251

255252
if (digestCalculator != null)
256253
{
257-
parameters = Collections.unmodifiableMap(getBaseParameters(contentType, digestCalculator.getAlgorithmIdentifier(), macCalculator.getAlgorithmIdentifier(), digestCalculator.getDigest()));
254+
AlgorithmIdentifier digestAlgID = digestCalculator.getAlgorithmIdentifier();
255+
AlgorithmIdentifier macAlgID = macCalculator.getAlgorithmIdentifier();
256+
257+
parameters = Collections.unmodifiableMap(
258+
getBaseParameters(contentType, digestAlgID, macAlgID, digestCalculator.getDigest()));
258259

259260
if (authGen == null)
260261
{
@@ -273,7 +274,7 @@ public void close()
273274
}
274275
else
275276
{
276-
parameters = Collections.EMPTY_MAP;
277+
parameters = CMSUtils.getEmptyParameters();
277278
}
278279

279280
envGen.addObject(new DEROctetString(macCalculator.getMac()));

pkix/src/main/java/org/bouncycastle/cms/CMSEnvelopedDataStreamGenerator.java

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.io.IOException;
44
import java.io.OutputStream;
5-
import java.util.Collections;
65

76
import org.bouncycastle.asn1.ASN1EncodableVector;
87
import org.bouncycastle.asn1.ASN1Integer;
@@ -77,17 +76,6 @@ private ASN1Integer getVersion(ASN1EncodableVector recipientInfos)
7776
return new ASN1Integer(EnvelopedData.calculateVersion(originatorInfo, new DLSet(recipientInfos), null));
7877
}
7978

80-
private OutputStream doOpen(
81-
ASN1ObjectIdentifier dataType,
82-
OutputStream out,
83-
OutputEncryptor encryptor)
84-
throws IOException, CMSException
85-
{
86-
ASN1EncodableVector recipientInfos = CMSUtils.getRecipentInfos(encryptor.getKey(), recipientInfoGenerators);
87-
88-
return open(dataType, out, recipientInfos, encryptor);
89-
}
90-
9179
protected OutputStream open(
9280
ASN1ObjectIdentifier dataType,
9381
OutputStream out,
@@ -147,26 +135,22 @@ protected OutputStream open(
147135
* generate an enveloped object that contains an CMS Enveloped Data
148136
* object using the given encryptor.
149137
*/
150-
public OutputStream open(
151-
OutputStream out,
152-
OutputEncryptor encryptor)
153-
throws CMSException, IOException
138+
public OutputStream open(OutputStream out, OutputEncryptor encryptor) throws CMSException, IOException
154139
{
155-
return doOpen(new ASN1ObjectIdentifier(CMSObjectIdentifiers.data.getId()), out, encryptor);
140+
return open(CMSObjectIdentifiers.data, out, encryptor);
156141
}
157142

158143
/**
159144
* generate an enveloped object that contains an CMS Enveloped Data
160145
* object using the given encryptor and marking the data as being of the passed
161146
* in type.
162147
*/
163-
public OutputStream open(
164-
ASN1ObjectIdentifier dataType,
165-
OutputStream out,
166-
OutputEncryptor encryptor)
148+
public OutputStream open(ASN1ObjectIdentifier dataType, OutputStream out, OutputEncryptor encryptor)
167149
throws CMSException, IOException
168150
{
169-
return doOpen(dataType, out, encryptor);
151+
ASN1EncodableVector recipientInfos = CMSUtils.getRecipentInfos(encryptor.getKey(), recipientInfoGenerators);
152+
153+
return open(dataType, out, recipientInfos, encryptor);
170154
}
171155

172156
private class CmsEnvelopedDataOutputStream
@@ -229,7 +213,7 @@ public void close()
229213
}
230214
_eiGen.close();
231215

232-
CMSUtils.addAttriSetToGenerator(_envGen, unprotectedAttributeGenerator, 1, Collections.EMPTY_MAP);
216+
CMSUtils.addAttriSetToGenerator(_envGen, unprotectedAttributeGenerator, 1, CMSUtils.getEmptyParameters());
233217

234218
_envGen.close();
235219
_cGen.close();

pkix/src/main/java/org/bouncycastle/cms/CMSUtils.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ static ASN1Set processAuthAttrSet(CMSAttributeTableGenerator authAttrsGenerator,
492492
ASN1Set authenticatedAttrSet = null;
493493
if (authAttrsGenerator != null)
494494
{
495-
AttributeTable attrTable = authAttrsGenerator.getAttributes(Collections.EMPTY_MAP);
495+
AttributeTable attrTable = authAttrsGenerator.getAttributes(getEmptyParameters());
496496

497497
authenticatedAttrSet = new DERSet(attrTable.toASN1EncodableVector());
498498
encryptor.getAADStream().write(authenticatedAttrSet.getEncoded(ASN1Encoding.DER));
@@ -521,12 +521,12 @@ static AttributeTable getAttributesTable(ASN1SetParser set)
521521

522522
static ASN1Set getAttrDLSet(CMSAttributeTableGenerator gen)
523523
{
524-
return (gen != null) ? new DLSet(gen.getAttributes(Collections.EMPTY_MAP).toASN1EncodableVector()) : null;
524+
return (gen != null) ? new DLSet(gen.getAttributes(getEmptyParameters()).toASN1EncodableVector()) : null;
525525
}
526526

527527
static ASN1Set getAttrBERSet(CMSAttributeTableGenerator gen)
528528
{
529-
return (gen != null) ? new BERSet(gen.getAttributes(Collections.EMPTY_MAP).toASN1EncodableVector()) : null;
529+
return (gen != null) ? new BERSet(gen.getAttributes(getEmptyParameters()).toASN1EncodableVector()) : null;
530530
}
531531

532532
static byte[] encodeObj(
@@ -540,4 +540,9 @@ static byte[] encodeObj(
540540

541541
return null;
542542
}
543+
544+
static Map getEmptyParameters()
545+
{
546+
return Collections.EMPTY_MAP;
547+
}
543548
}

pkix/src/main/jdk1.1/org/bouncycastle/cms/CMSAbsentContent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class CMSAbsentContent
1717

1818
public CMSAbsentContent()
1919
{
20-
this(new ASN1ObjectIdentifier(CMSObjectIdentifiers.data.getId()));
20+
this(CMSObjectIdentifiers.data);
2121
}
2222

2323
public CMSAbsentContent(

pkix/src/main/jdk1.1/org/bouncycastle/cms/CMSProcessableByteArray.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class CMSProcessableByteArray
2121
public CMSProcessableByteArray(
2222
byte[] bytes)
2323
{
24-
this(new ASN1ObjectIdentifier(CMSObjectIdentifiers.data.getId()), bytes);
24+
this(CMSObjectIdentifiers.data, bytes);
2525
}
2626

2727
public CMSProcessableByteArray(

0 commit comments

Comments
 (0)