Skip to content

Commit 2524f46

Browse files
author
gefeili
committed
Destroy the SecretWithEncapsulation in the ML-KEM, HQC, NTRU, NTRU Prime and SM9 javax.crypto.KEM encapsulators once the encapsulation and shared secret have been taken from it, as the sibling Cipher services and the ISO FrodoKEM and Classic McEliece encapsulators already do - getEncapsulation() and getSecret() hand back clones, so KdfUtil clearing the secret clone left the mechanism's own session key live in the heap for the lifetime of the KEM.Encapsulator.
1 parent e6b360d commit 2524f46

6 files changed

Lines changed: 81 additions & 25 deletions

File tree

‎docs/releasenotes.html‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ <h3>2.1.2 Defects Fixed</h3>
124124
<li>The parameter-set specific SLH-DSA Signature services were not bound to their parameter set at all: every one of the twenty four algorithm names, and their OIDs, was registered as an alias of the unparameterised "SLH-DSA" or "HASH-SLH-DSA" service, so the name a caller asked for had no effect on which keys were accepted. Signature.getInstance("SLH-DSA-SHAKE-256S") would sign quite happily with an SLH-DSA-SHA2-128F key, producing a SHA2-128F signature, and likewise across every other pair. This matters to a caller using the algorithm name as a policy gate - a service that means to sign or verify only at a chosen parameter set - which had no way to tell that the name was being ignored. Each name and OID is now registered against an SPI bound to its own parameter set, applying the same rule described for ML-DSA above: a pure key is accepted by the pre-hash service of its own parameter set, a pre-hash key is refused by the pure service, and a key of any other parameter set is refused either way. <b>This is a behavioural change for a caller that relied on a parameter-set named SLH-DSA Signature accepting a key of a different parameter set</b>, which now raises InvalidKeyException; the unparameterised "SLH-DSA" and "HASH-SLH-DSA" services name no parameter set and are unchanged, so they remain the way to work with a key whose parameter set is not known in advance. Note also that the pre-hash OID table was in a different order from the algorithm names it is now index-matched against, which is corrected here - while everything aliased to a single service the ordering could not be observed.</li>
125125
<li>Signature.setParameter(...) on any of the ML-DSA or SLH-DSA services threw NullPointerException when called before initSign / initVerify, out of a method declared to throw InvalidAlgorithmParameterException. The context carried by a ContextParameterSpec is applied by re-initialising the underlying signer with the key the Signature holds, and BaseDeterministicOrRandomSignature.engineSetParameter went straight to that re-initialisation without checking there was a key to re-initialise with. The context may now be set either side of initialisation, as the RSASSA-PSS parameters may be: setting it on a signature that already has a key re-initialises the signer at once as before, and setting it beforehand records it to be applied when the key arrives. Initialising no longer discards a context set beforehand, which it previously did by resetting the context to empty - so a caller who set one first and would have had it silently dropped now gets the signature they asked for. Setting a context after initialisation, which is what the existing tests and examples do, produces exactly the signatures it did before, and setting one in the middle of an update is still refused with a ProviderException. Note the context now also survives a subsequent re-initialisation of the same Signature object, again as the RSASSA-PSS parameters do (github #2396).</li>
126126
<li>An OCSP response carrying no nextUpdate could be cached and reused as though it stated a validity interval, so a response could go on answering for a certificate after the responder had newer information about it - after a revocation, in particular. Any such reuse was bounded only by garbage collection rather than by an interval: OcspCache holds each responder's response map through a WeakReference, itself in a WeakHashMap, and nothing else refers to that map once the call returns, so entries last until the next collection. That is unpredictable rather than long - short on a busy JVM, potentially much longer on a large heap that collects rarely - and it is not a window the responder or the caller had any say in. RFC 6960 sec. 4.2.2.1 says the opposite of what that assumes: "if nextUpdate is not set, the responder is indicating that newer revocation information is available all the time", which is a statement that there is no interval to reuse the response over, not that it never expires. OcspCache now separates the two questions it had been asking with one method: a response arriving from the responder is accepted as before, whether or not it states a nextUpdate, while only a response that states one may be served from the cache afterwards. Nothing is rejected that was previously accepted - a responder that omits nextUpdate simply costs another request per validation, which is what "available all the time" asks for. Additionally, both the cached and the caller-supplied (stapled) paths now apply RFC 6960 sec. 4.2.2.1's other freshness rule, "responses whose thisUpdate time is later than the local system time SHOULD be considered unreliable", which neither had checked: a response dated ahead of the time being validated for by more than a 15 minute clock-skew allowance is treated as unreliable, raising "OCSP response not yet valid" on the stapled path.</li>
127+
<li>None of the javax.crypto.KEM encapsulators destroyed the SecretWithEncapsulation the mechanism handed them, so each left the mechanism's own session key live in the heap for as long as the KEM.Encapsulator object was reachable - ML-KEM, HQC, NTRU, NTRU Prime and SM9 were all affected. Clearing the derived key was not enough to cover it: SecretWithEncapsulationImpl.getSecret() and getEncapsulation() hand back clones of their internal arrays, so KdfUtil.makeKeyBytes clearing the secret clone it is passed never reached the session key inside, and only destroy() does. Each encapsulator now takes the encapsulation and the shared secret and then destroys the SecretWithEncapsulation in a finally block, as the sibling Cipher services in the same packages already did, and as the ISO/IEC 18033-2 FrodoKEM and Classic McEliece encapsulators added in this release do. The order matters, since destroy() clears the encapsulation as well as the session key. Nothing observable changes for a caller - the shared secret and encapsulation returned are the same as before, and are the caller's own copies.</li>
127128
</ul>
128129

129130
<h3>2.1.3 Additional Features and Functionality</h3>

‎prov/src/main/jdk17/org/bouncycastle/jcajce/provider/asymmetric/mlkem/MLKEMEncapsulatorSpi.java‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import javax.crypto.KEMSpi;
88
import javax.crypto.SecretKey;
99
import javax.crypto.spec.SecretKeySpec;
10+
import javax.security.auth.DestroyFailedException;
1011

1112
import org.bouncycastle.crypto.SecretWithEncapsulation;
1213
import org.bouncycastle.jcajce.spec.KTSParameterSpec;
@@ -56,19 +57,29 @@ else if (!algorithm.equals(keyAlgName))
5657

5758
SecretWithEncapsulation secEnc = kemGen.generateEncapsulated(publicKey.getKeyParams());
5859

59-
byte[] encapsulation = secEnc.getEncapsulation();
60-
61-
byte[] kemSecret = secEnc.getSecret();
62-
byte[] kdfSecret = KdfUtil.makeKeyBytes(parameterSpec, kemSecret);
63-
60+
byte[] kdfSecret = null;
6461
try
6562
{
63+
// getEncapsulation()/getSecret() hand back clones, so the originals have to be
64+
// destroyed as well - KdfUtil.makeKeyBytes only clears the secret clone it is passed.
65+
byte[] encapsulation = secEnc.getEncapsulation();
66+
67+
kdfSecret = KdfUtil.makeKeyBytes(parameterSpec, secEnc.getSecret());
68+
6669
SecretKey secretKey = new SecretKeySpec(kdfSecret, from, to - from, algorithm);
6770
return new KEM.Encapsulated(secretKey, encapsulation, null);
6871
}
6972
finally
7073
{
7174
Arrays.clear(kdfSecret);
75+
try
76+
{
77+
secEnc.destroy();
78+
}
79+
catch (DestroyFailedException e)
80+
{
81+
// ignore
82+
}
7283
}
7384
}
7485

‎prov/src/main/jdk17/org/bouncycastle/jcajce/provider/asymmetric/sm9/SM9EncapsulatorSpi.java‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import javax.crypto.KEMSpi;
88
import javax.crypto.SecretKey;
99
import javax.crypto.spec.SecretKeySpec;
10+
import javax.security.auth.DestroyFailedException;
1011

1112
import org.bouncycastle.crypto.SecretWithEncapsulation;
1213
import org.bouncycastle.crypto.kems.SM9KEMGenerator;
@@ -59,19 +60,29 @@ else if (!algorithm.equals(keyAlgName))
5960

6061
SecretWithEncapsulation secEnc = kemGen.generateEncapsulated(publicKey.getKeyParameters());
6162

62-
byte[] encapsulation = secEnc.getEncapsulation();
63-
64-
byte[] kemSecret = secEnc.getSecret();
65-
byte[] kdfSecret = KdfUtil.makeKeyBytes(parameterSpec, kemSecret);
66-
63+
byte[] kdfSecret = null;
6764
try
6865
{
66+
// getEncapsulation()/getSecret() hand back clones, so the originals have to be
67+
// destroyed as well - KdfUtil.makeKeyBytes only clears the secret clone it is passed.
68+
byte[] encapsulation = secEnc.getEncapsulation();
69+
70+
kdfSecret = KdfUtil.makeKeyBytes(parameterSpec, secEnc.getSecret());
71+
6972
SecretKey secretKey = new SecretKeySpec(kdfSecret, from, to - from, algorithm);
7073
return new KEM.Encapsulated(secretKey, encapsulation, null);
7174
}
7275
finally
7376
{
7477
Arrays.clear(kdfSecret);
78+
try
79+
{
80+
secEnc.destroy();
81+
}
82+
catch (DestroyFailedException e)
83+
{
84+
// ignore
85+
}
7586
}
7687
}
7788

‎prov/src/main/jdk17/org/bouncycastle/pqc/jcajce/provider/hqc/HQCEncapsulatorSpi.java‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import javax.crypto.KEMSpi;
88
import javax.crypto.SecretKey;
99
import javax.crypto.spec.SecretKeySpec;
10+
import javax.security.auth.DestroyFailedException;
1011

1112
import org.bouncycastle.crypto.SecretWithEncapsulation;
1213
import org.bouncycastle.jcajce.spec.KTSParameterSpec;
@@ -56,19 +57,29 @@ else if (!algorithm.equals(keyAlgName))
5657

5758
SecretWithEncapsulation secEnc = kemGen.generateEncapsulated(publicKey.getKeyParams());
5859

59-
byte[] encapsulation = secEnc.getEncapsulation();
60-
61-
byte[] kemSecret = secEnc.getSecret();
62-
byte[] kdfSecret = KdfUtil.makeKeyBytes(parameterSpec, kemSecret);
63-
60+
byte[] kdfSecret = null;
6461
try
6562
{
63+
// getEncapsulation()/getSecret() hand back clones, so the originals have to be
64+
// destroyed as well - KdfUtil.makeKeyBytes only clears the secret clone it is passed.
65+
byte[] encapsulation = secEnc.getEncapsulation();
66+
67+
kdfSecret = KdfUtil.makeKeyBytes(parameterSpec, secEnc.getSecret());
68+
6669
SecretKey secretKey = new SecretKeySpec(kdfSecret, from, to - from, algorithm);
6770
return new KEM.Encapsulated(secretKey, encapsulation, null);
6871
}
6972
finally
7073
{
7174
Arrays.clear(kdfSecret);
75+
try
76+
{
77+
secEnc.destroy();
78+
}
79+
catch (DestroyFailedException e)
80+
{
81+
// ignore
82+
}
7283
}
7384
}
7485

‎prov/src/main/jdk17/org/bouncycastle/pqc/jcajce/provider/ntru/NTRUEncapsulatorSpi.java‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import javax.crypto.KEMSpi;
88
import javax.crypto.SecretKey;
99
import javax.crypto.spec.SecretKeySpec;
10+
import javax.security.auth.DestroyFailedException;
1011

1112
import org.bouncycastle.crypto.SecretWithEncapsulation;
1213
import org.bouncycastle.jcajce.spec.KTSParameterSpec;
@@ -56,19 +57,29 @@ else if (!algorithm.equals(keyAlgName))
5657

5758
SecretWithEncapsulation secEnc = kemGen.generateEncapsulated(publicKey.getKeyParams());
5859

59-
byte[] encapsulation = secEnc.getEncapsulation();
60-
61-
byte[] kemSecret = secEnc.getSecret();
62-
byte[] kdfSecret = KdfUtil.makeKeyBytes(parameterSpec, kemSecret);
63-
60+
byte[] kdfSecret = null;
6461
try
6562
{
63+
// getEncapsulation()/getSecret() hand back clones, so the originals have to be
64+
// destroyed as well - KdfUtil.makeKeyBytes only clears the secret clone it is passed.
65+
byte[] encapsulation = secEnc.getEncapsulation();
66+
67+
kdfSecret = KdfUtil.makeKeyBytes(parameterSpec, secEnc.getSecret());
68+
6669
SecretKey secretKey = new SecretKeySpec(kdfSecret, from, to - from, algorithm);
6770
return new KEM.Encapsulated(secretKey, encapsulation, null);
6871
}
6972
finally
7073
{
7174
Arrays.clear(kdfSecret);
75+
try
76+
{
77+
secEnc.destroy();
78+
}
79+
catch (DestroyFailedException e)
80+
{
81+
// ignore
82+
}
7283
}
7384
}
7485

‎prov/src/main/jdk17/org/bouncycastle/pqc/jcajce/provider/ntruprime/SNTRUPrimeEncapsulatorSpi.java‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import javax.crypto.KEMSpi;
88
import javax.crypto.SecretKey;
99
import javax.crypto.spec.SecretKeySpec;
10+
import javax.security.auth.DestroyFailedException;
1011

1112
import org.bouncycastle.crypto.SecretWithEncapsulation;
1213
import org.bouncycastle.jcajce.spec.KTSParameterSpec;
@@ -56,19 +57,29 @@ else if (!algorithm.equals(keyAlgName))
5657

5758
SecretWithEncapsulation secEnc = kemGen.generateEncapsulated(publicKey.getKeyParams());
5859

59-
byte[] encapsulation = secEnc.getEncapsulation();
60-
61-
byte[] kemSecret = secEnc.getSecret();
62-
byte[] kdfSecret = KdfUtil.makeKeyBytes(parameterSpec, kemSecret);
63-
60+
byte[] kdfSecret = null;
6461
try
6562
{
63+
// getEncapsulation()/getSecret() hand back clones, so the originals have to be
64+
// destroyed as well - KdfUtil.makeKeyBytes only clears the secret clone it is passed.
65+
byte[] encapsulation = secEnc.getEncapsulation();
66+
67+
kdfSecret = KdfUtil.makeKeyBytes(parameterSpec, secEnc.getSecret());
68+
6669
SecretKey secretKey = new SecretKeySpec(kdfSecret, from, to - from, algorithm);
6770
return new KEM.Encapsulated(secretKey, encapsulation, null);
6871
}
6972
finally
7073
{
7174
Arrays.clear(kdfSecret);
75+
try
76+
{
77+
secEnc.destroy();
78+
}
79+
catch (DestroyFailedException e)
80+
{
81+
// ignore
82+
}
7283
}
7384
}
7485

0 commit comments

Comments
 (0)