Skip to content

Commit 7c7c8c2

Browse files
committed
Initialise the content AlgorithmParameterGenerator with the caller's SecureRandom on the general path in JceCMSContentEncryptorBuilder, not only for RC2, so setSecureRandom drives the IV/nonce for AES-GCM and every other content-encryption algorithm.
1 parent e6b360d commit 7c7c8c2

5 files changed

Lines changed: 108 additions & 0 deletions

File tree

‎docs/releasenotes.html‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ <h2>2.0 Release History</h2>
2323
Date:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2026, TBD
2424
<h3>2.1.2 Defects Fixed</h3>
2525
<ul>
26+
<li>The SecureRandom supplied to org.bouncycastle.cms.jcajce.JceCMSContentEncryptorBuilder.setSecureRandom() did not drive the content IV / nonce for any algorithm other than RC2. EnvelopedDataHelper.generateParameters passed the caller's SecureRandom to the AlgorithmParameterGenerator only in the RC2_CBC branch; every other content-encryption algorithm - AES-CBC, AES-GCM, AES-CCM, Camellia, ARIA, SEED and the rest - reached pGen.generateParameters() on an uninitialised generator, so the IV / nonce was drawn from a default SecureRandom and setSecureRandom() was silently ignored (the builder's javadoc states that random is used for IV/nonce generation). The generator is now initialised with the supplied random on the general path as well, so a caller who provides a specific randomness source - for a controlled or FIPS-approved DRBG, say - has it honoured for the content IV / nonce. The session-key generation path was unaffected and already used the supplied random.</li>
2627
<li>The bcmail module descriptor did not declare its javax.mail/javax.activation dependences, so a modular (module-path) consumer of the jar hit IllegalAccessError/module-resolution failures when the S/MIME classes touched the mail API. The descriptor now requires them optionally (requires static) under all four module names those libraries are known by - the automatic names mail and activation carried by the javax.mail:mail / javax.activation:activation artifacts, and the explicit names java.mail and java.activation carried by the newer com.sun.mail / com.sun.activation ones - a hard requires on any one name would break users of the others (github #2389).</li>
2728
<li>Four type-coercion helpers in the OER / IEEE 1609.2 (ITS) decoder tested the wrong type in the identity fast path that lets a getInstance() factory return an argument that is already of the target type. org.bouncycastle.oer.its.ieee1609dot2.basetypes.UINT32.getInstance and org.bouncycastle.oer.its.etsi102941.basetypes.Version.getInstance guarded on UINT8 - a sibling of UINT32 under UintBase, and unrelated to Version - so passing a UINT8 threw ClassCastException, while passing an actual UINT32 or Version missed the fast path and fell through to ASN1Integer.getInstance, which rejects them: neither factory accepted its own type. org.bouncycastle.oer.its.etsi103097.EtsiTs103097DataEncryptedUnicast.getInstance guarded on its sibling EtsiTs103097DataEncrypted and then cast to the unicast type, so an EtsiTs103097DataEncrypted threw ClassCastException. org.bouncycastle.oer.OEROptional.getObject(Class) called value.getClass().isInstance(type) with the arguments transposed, which is always false because the argument is a java.lang.Class, so the cast path was dead and every optional field was resolved reflectively, failing with IllegalStateException for a target type with no static getInstance. Each guard now names the type it returns, matching the sibling UINT8 / UINT16 / UINT64 and EtsiTs103097DataEncrypted factories (github #2373).</li>
2829
<li>DefaultAlgorithmNameFinder and DefaultSignatureNameFinder had no entries at all for the ShangMi algorithms, so an SM2 signature AlgorithmIdentifier that DefaultSignatureAlgorithmIdentifierFinder itself produces came back named only by its OID string - getAlgorithmName(GMObjectIdentifiers.sm2sign_with_sm3) returned "1.2.156.10197.1.501" and hasAlgorithmName returned false. Both finders now name sm2sign_with_sm3 as SM3WITHSM2 and sm2sign_with_sha256 as SHA256WITHSM2, and DefaultAlgorithmNameFinder additionally names the sm3 digest. All three resolve through the BC provider, as Signature and MessageDigest respectively. The remaining GM arc - the SM4 cipher modes, the sm2encrypt variants, and the SM1 / SM6 / SSF33 ciphers BC does not implement - is still unnamed (github #2377).</li>

‎pkix/src/main/java/org/bouncycastle/cms/jcajce/EnvelopedDataHelper.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,11 @@ AlgorithmParameters generateParameters(ASN1ObjectIdentifier encryptionOID, Secre
631631
throw new CMSException("parameters generation error: " + e, e);
632632
}
633633
}
634+
else
635+
{
636+
// without this the generator uses a default SecureRandom for the IV/nonce, ignoring rand
637+
pGen.init(getStrengthInBits(encKey), rand);
638+
}
634639

635640
return pGen.generateParameters();
636641
}
@@ -644,6 +649,14 @@ AlgorithmParameters generateParameters(ASN1ObjectIdentifier encryptionOID, Secre
644649
}
645650
}
646651

652+
// strength is ignored by the symmetric parameter generators; the fall-back only avoids an NPE for a key with no encoding (e.g. a hardware key)
653+
private static int getStrengthInBits(SecretKey encKey)
654+
{
655+
byte[] keyBytes = encKey.getEncoded();
656+
657+
return (keyBytes != null) ? keyBytes.length * 8 : 256;
658+
}
659+
647660
AlgorithmIdentifier getAlgorithmIdentifier(ASN1ObjectIdentifier encryptionOID, AlgorithmParameters params)
648661
throws CMSException
649662
{

‎pkix/src/main/jdk1.1/org/bouncycastle/cms/jcajce/EnvelopedDataHelper.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,11 @@ AlgorithmParameters generateParameters(ASN1ObjectIdentifier encryptionOID, Secre
552552
throw new CMSException("parameters generation error: " + e, e);
553553
}
554554
}
555+
else
556+
{
557+
// without this the generator uses a default SecureRandom for the IV/nonce, ignoring rand
558+
pGen.init(getStrengthInBits(encKey), rand);
559+
}
555560

556561
return pGen.generateParameters();
557562
}
@@ -561,6 +566,14 @@ AlgorithmParameters generateParameters(ASN1ObjectIdentifier encryptionOID, Secre
561566
}
562567
}
563568

569+
// strength is ignored by the symmetric parameter generators; the fall-back only avoids an NPE for a key with no encoding (e.g. a hardware key)
570+
private static int getStrengthInBits(SecretKey encKey)
571+
{
572+
byte[] keyBytes = encKey.getEncoded();
573+
574+
return (keyBytes != null) ? keyBytes.length * 8 : 256;
575+
}
576+
564577
AlgorithmIdentifier getAlgorithmIdentifier(ASN1ObjectIdentifier encryptionOID, AlgorithmParameters params)
565578
throws CMSException
566579
{

‎pkix/src/test/java/org/bouncycastle/cms/test/AllTests.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public static Test suite()
2424
suite.addTest(NewCompressedDataTest.suite());
2525
suite.addTest(NewSignedDataTest.suite());
2626
suite.addTest(NewEnvelopedDataTest.suite());
27+
suite.addTestSuite(ContentEncryptorRandomTest.class);
2728
suite.addTestSuite(FrodoKEMEnvelopedDataTest.class);
2829
suite.addTestSuite(KEMUkmEnvelopedDataTest.class);
2930
suite.addTestSuite(CompositeKEMEnvelopedDataTest.class);
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package org.bouncycastle.cms.test;
2+
3+
import java.security.SecureRandom;
4+
import java.security.Security;
5+
6+
import junit.framework.TestCase;
7+
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
8+
import org.bouncycastle.asn1.ASN1OctetString;
9+
import org.bouncycastle.asn1.ASN1Sequence;
10+
import org.bouncycastle.cms.CMSAlgorithm;
11+
import org.bouncycastle.cms.jcajce.JceCMSContentEncryptorBuilder;
12+
import org.bouncycastle.jce.provider.BouncyCastleProvider;
13+
import org.bouncycastle.operator.OutputEncryptor;
14+
import org.bouncycastle.util.Arrays;
15+
import org.bouncycastle.util.test.FixedSecureRandom;
16+
17+
/**
18+
* The SecureRandom passed to {@link JceCMSContentEncryptorBuilder#setSecureRandom} has to drive the
19+
* content IV / nonce generation, not just RC2. Previously it reached only the RC2 branch of
20+
* EnvelopedDataHelper.generateParameters, so for every other algorithm - including AES-GCM - the IV
21+
* came from a default SecureRandom and setSecureRandom() was silently ignored.
22+
*/
23+
public class ContentEncryptorRandomTest
24+
extends TestCase
25+
{
26+
public void setUp()
27+
{
28+
if (Security.getProvider("BC") == null)
29+
{
30+
Security.addProvider(new BouncyCastleProvider());
31+
}
32+
}
33+
34+
public void testSuppliedRandomDrivesGcmNonce()
35+
throws Exception
36+
{
37+
byte[] nonce = new byte[12];
38+
for (int i = 0; i != nonce.length; i++)
39+
{
40+
nonce[i] = (byte)(0xC0 + i);
41+
}
42+
43+
// the parameters for AES-GCM are a SEQUENCE whose first element is the nonce OCTET STRING
44+
ASN1Sequence params = ASN1Sequence.getInstance(gcmParameters(CMSAlgorithm.AES256_GCM, nonce));
45+
46+
assertTrue("supplied SecureRandom did not drive the GCM nonce",
47+
Arrays.areEqual(nonce, ASN1OctetString.getInstance(params.getObjectAt(0)).getOctets()));
48+
}
49+
50+
public void testSuppliedRandomDrivesCbcIV()
51+
throws Exception
52+
{
53+
byte[] iv = new byte[16];
54+
for (int i = 0; i != iv.length; i++)
55+
{
56+
iv[i] = (byte)(0xA0 + i);
57+
}
58+
59+
// the parameters for AES-CBC are the IV as a bare OCTET STRING
60+
byte[] emitted = ASN1OctetString.getInstance(
61+
gcmParameters(CMSAlgorithm.AES256_CBC, iv)).getOctets();
62+
63+
assertTrue("supplied SecureRandom did not drive the CBC IV", Arrays.areEqual(iv, emitted));
64+
}
65+
66+
private static org.bouncycastle.asn1.ASN1Encodable gcmParameters(ASN1ObjectIdentifier alg, byte[] ivMaterial)
67+
throws Exception
68+
{
69+
// a supplied key means the SecureRandom is consumed only for the IV / nonce, so a
70+
// FixedSecureRandom emitting exactly ivMaterial makes the emitted parameters deterministic
71+
SecureRandom fixed = new FixedSecureRandom(ivMaterial);
72+
73+
OutputEncryptor enc = new JceCMSContentEncryptorBuilder(alg)
74+
.setProvider("BC")
75+
.setSecureRandom(fixed)
76+
.build(new byte[32]);
77+
78+
return enc.getAlgorithmIdentifier().getParameters();
79+
}
80+
}

0 commit comments

Comments
 (0)