Skip to content

Commit 16b8f87

Browse files
committed
Reproduce a JournalingSecureRandom encryption session by regenerating the content IV from the replayed randomness rather than reusing the first encryption's AlgorithmIdentifier, now that setSecureRandom drives the IV and it is recorded in the transcript.
1 parent 77632a5 commit 16b8f87

3 files changed

Lines changed: 5 additions & 7 deletions

File tree

‎docs/releasenotes.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ <h2>2.0 Release History</h2>
2424
<h3>2.1.2 Defects Fixed</h3>
2525
<ul>
2626
<li>The MLS implementation did not bind an X.509 credential to the LeafNode's signature_key. LeafNode.verify() checked a leaf's signature against the signature_key declared in the leaf itself, while the X.509 credential's certificate chain was stored but never parsed or checked, so the certificate's public key was never required to match signature_key (RFC 9420 sec. 5.3). A leaf could therefore carry one party's certificate while being signed by an unrelated key and still be accepted under that party's identity through KeyPackage.verify() and the Group leaf-validation path. LeafNode.verify() now requires the end-entity certificate's subject public key, in the cipher suite's signature encoding, to equal signature_key for an X.509 credential, and rejects the leaf otherwise - including an empty chain or a certificate whose key type does not match the cipher suite; certificate-chain and identity validation to a trust anchor remain the application's responsibility per RFC 9420 sec. 5.3.1. A public org.bouncycastle.mls.codec.Certificate(byte[]) constructor and a Credential.getCertificates() accessor are added so callers can build and inspect X.509 credentials. Basic credentials are unaffected.</li>
27-
<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>
27+
<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. Because the content IV / nonce now comes from the supplied SecureRandom, the org.bouncycastle.crypto.util JournalingSecureRandom / JournaledAlgorithm reproducible-encryption support records it in the transcript: a resumed session reproduces the IV / nonce by regenerating it from the replayed randomness - build the resuming encryptor from the content-algorithm OID - rather than by reusing the AlgorithmIdentifier captured from the first encryption, which no longer keeps the transcript aligned.</li>
2828
<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>
2929
<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>
3030
<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>

‎mail/src/test/java/org/bouncycastle/mail/smime/test/JournalingSecureRandomEncryptTest.java‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import junit.framework.TestCase;
2121
import org.bouncycastle.asn1.x500.X500Name;
22-
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
2322
import org.bouncycastle.cert.X509v3CertificateBuilder;
2423
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
2524
import org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder;
@@ -141,9 +140,6 @@ public void testEncryptFullMessage()
141140

142141
OutputEncryptor encryptor = new JceCMSContentEncryptorBuilder(CMSAlgorithm.AES128_CBC).setProvider(BC).setSecureRandom(journaling_1).build();
143142

144-
AlgorithmIdentifier alg_id = encryptor.getAlgorithmIdentifier();
145-
146-
147143
SMIMEEnvelopedWriter envWrt = envBldr_i.build(out_i, encryptor);
148144

149145
OutputStream envOut = envWrt.getContentStream();
@@ -172,7 +168,8 @@ public void testEncryptFullMessage()
172168
// specify encryption certificate
173169
envBldr_f.addRecipientInfoGenerator(new JceKeyTransRecipientInfoGenerator(_encrCert, wrapper_f).setProvider(BC));
174170

175-
OutputEncryptor encryptor_2 = new JceCMSContentEncryptorBuilder(alg_id).setSecureRandom(journaling_2).build();
171+
// regenerate the IV from the replayed journaling random (build from the OID, not the first round's captured AlgorithmIdentifier) so the IV draw stays aligned with the recorded transcript
172+
OutputEncryptor encryptor_2 = new JceCMSContentEncryptorBuilder(CMSAlgorithm.AES128_CBC).setProvider(BC).setSecureRandom(journaling_2).build();
176173

177174
SMIMEEnvelopedWriter envWrt_f = envBldr_f.build(out_f, encryptor_2);
178175

‎pkix/src/test/java/org/bouncycastle/mime/test/TestJournalingSecureRandomEncrypt.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ public static ByteArrayOutputStream encryptWithSession(byte[] input_bytes, X509C
165165

166166
envBldr_i.setBufferSize(20);
167167

168-
OutputEncryptor encryptor = new JceCMSContentEncryptorBuilder(journaledAlgorithm.getAlgorithmIdentifier()).setProvider(BC)
168+
// regenerate the IV from the replayed journaling random (build from the algorithm OID, not the stored IV-bearing AlgorithmIdentifier) so the IV draw stays aligned with the recorded transcript
169+
OutputEncryptor encryptor = new JceCMSContentEncryptorBuilder(new AlgorithmIdentifier(journaledAlgorithm.getAlgorithmIdentifier().getAlgorithm())).setProvider(BC)
169170
.setSecureRandom(journaledAlgorithm.getJournalingSecureRandom()).build();
170171

171172
SMIMEEnvelopedWriter envWrt = envBldr_i.build(out_i, encryptor);

0 commit comments

Comments
 (0)