Skip to content

Commit 2bc7208

Browse files
author
gefeili
committed
Address the review of the KdfUtil KEM helpers. makeSecretKey promised the passed in secret was erased on every exit, but its argument-check throws sat before the erasing try/finally, so a null algorithm name left a live session secret in the caller's buffer - and that path was reachable through the provider: SM9 is the one KEM that does not route its spec through resolveKemSpec, a KTSParameterSpec built with a null algorithm name is constructible, and the JDK's default "Generic" request is then substituted with the null and hit the NPE after the secret had been extracted. The checks now sit inside the try, whose finally clears the secret on every exit including those throws, and SM9KEMSpi rejects a spec with no key algorithm name at construction with the declared InvalidAlgorithmParameterException, as resolveKemSpec does for every other family. Cover the helpers directly in a new KdfUtilTest - the provider's own services pre-validate everything they pass in, so the guards and the erase-on-failure contract are reachable only by an outside caller and no provider-driven test can exercise them - and add the null-name spec rejection to SM9KEM17Test on both the encapsulate and decapsulate paths.
1 parent 501bcd0 commit 2bc7208

5 files changed

Lines changed: 209 additions & 15 deletions

File tree

‎prov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/util/KdfUtil.java‎

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -133,23 +133,24 @@ else if (!isSupportedKdf(ktsSpec.getKdfAlgorithm()))
133133
public static SecretKey makeSecretKey(KTSParameterSpec parameterSpec, byte[] kemSecret,
134134
int from, int to, String algorithm)
135135
{
136-
if (parameterSpec == null)
137-
{
138-
throw new NullPointerException("'parameterSpec' cannot be null");
139-
}
140-
if (kemSecret == null)
141-
{
142-
throw new NullPointerException("'kemSecret' cannot be null");
143-
}
144-
if (algorithm == null)
145-
{
146-
throw new NullPointerException("'algorithm' cannot be null");
147-
}
148-
149-
// erased on every exit, throws included, matching makeKeyBytes' contract - the clear is
150-
// idempotent, so makeKeyBytes having already erased it on the derivation path is harmless
136+
// erased on every exit, the argument-check throws included, matching makeKeyBytes' contract -
137+
// Arrays.clear is null safe, and the clear is idempotent, so makeKeyBytes having already
138+
// erased it on the derivation path is harmless
151139
try
152140
{
141+
if (parameterSpec == null)
142+
{
143+
throw new NullPointerException("'parameterSpec' cannot be null");
144+
}
145+
if (kemSecret == null)
146+
{
147+
throw new NullPointerException("'kemSecret' cannot be null");
148+
}
149+
if (algorithm == null)
150+
{
151+
throw new NullPointerException("'algorithm' cannot be null");
152+
}
153+
153154
// the same count makeKeyBytes will derive. Checked before deriving, and spelled out
154155
// rather than using Objects.checkFromToIndex, which is newer than this tree's source
155156
// floor allows.

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ else if (!(spec instanceof KTSParameterSpec))
4646
{
4747
throw new InvalidAlgorithmParameterException("SM9-KEM can only accept KTSParameterSpec");
4848
}
49+
else if (((KTSParameterSpec)spec).getKeyAlgorithmName() == null)
50+
{
51+
// SM9 sizes its secret from the spec itself and so does not go through
52+
// KdfUtil.resolveKemSpec, which rejects this for every other KEM: a null name would be
53+
// substituted for a "Generic" request and only fail deep inside the derivation.
54+
throw new InvalidAlgorithmParameterException("KTSParameterSpec has no key algorithm name");
55+
}
4956

5057
return new SM9EncapsulatorSpi(bcPublicKey, (KTSParameterSpec)spec, secureRandom);
5158
}
@@ -68,6 +75,13 @@ else if (!(spec instanceof KTSParameterSpec))
6875
{
6976
throw new InvalidAlgorithmParameterException("SM9-KEM can only accept KTSParameterSpec");
7077
}
78+
else if (((KTSParameterSpec)spec).getKeyAlgorithmName() == null)
79+
{
80+
// SM9 sizes its secret from the spec itself and so does not go through
81+
// KdfUtil.resolveKemSpec, which rejects this for every other KEM: a null name would be
82+
// substituted for a "Generic" request and only fail deep inside the derivation.
83+
throw new InvalidAlgorithmParameterException("KTSParameterSpec has no key algorithm name");
84+
}
7185

7286
return new SM9DecapsulatorSpi(bcPrivateKey, (KTSParameterSpec)spec);
7387
}

‎prov/src/test/java/org/bouncycastle/jcajce/provider/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

2525
suite.addTestSuite(ECAlgorithmParametersTest.class);
2626
suite.addTestSuite(GeneralKeyTest.class);
27+
suite.addTestSuite(KdfUtilTest.class);
2728
suite.addTestSuite(LEATest.class);
2829
suite.addTestSuite(HybridRandomProviderTest.class);
2930
suite.addTestSuite(PrivateConstructorTest.class);
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
package org.bouncycastle.jcajce.provider.test;
2+
3+
import java.security.InvalidAlgorithmParameterException;
4+
5+
import javax.crypto.SecretKey;
6+
7+
import junit.framework.TestCase;
8+
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
9+
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
10+
import org.bouncycastle.jcajce.provider.asymmetric.util.KdfUtil;
11+
import org.bouncycastle.jcajce.spec.KTSParameterSpec;
12+
import org.bouncycastle.util.Arrays;
13+
14+
/**
15+
* Direct coverage of the public KEM helpers on KdfUtil - resolveKemSpec, resolveAlgorithm and
16+
* makeSecretKey. The provider's own KEM services pre-validate everything they pass in, so the
17+
* guards here are reachable only by an outside caller, and a provider-driven test can never
18+
* exercise them; the erase-on-every-exit contract in particular is asserted here for each failure
19+
* path, since a caller who hits one must not be left holding a live secret the javadoc says was
20+
* erased.
21+
*/
22+
public class KdfUtilTest
23+
extends TestCase
24+
{
25+
private static byte[] secret()
26+
{
27+
byte[] secret = new byte[32];
28+
Arrays.fill(secret, (byte)0x5a);
29+
return secret;
30+
}
31+
32+
private static boolean isErased(byte[] secret)
33+
{
34+
return Arrays.areAllZeroes(secret, 0, secret.length);
35+
}
36+
37+
public void testMakeSecretKeyHappyPath()
38+
{
39+
KTSParameterSpec spec = new KTSParameterSpec.Builder("AES", 256).build();
40+
byte[] secret = secret();
41+
42+
SecretKey key = KdfUtil.makeSecretKey(spec, secret, 0, 16, "AES");
43+
44+
assertEquals("AES", key.getAlgorithm());
45+
assertEquals(16, key.getEncoded().length);
46+
assertTrue("input secret not erased", isErased(secret));
47+
48+
// the same derivation over the same secret bytes gives the same key, and a later window
49+
// of it differs from the leading one
50+
SecretKey again = KdfUtil.makeSecretKey(spec, secret(), 0, 16, "AES");
51+
assertTrue(Arrays.areEqual(key.getEncoded(), again.getEncoded()));
52+
SecretKey tail = KdfUtil.makeSecretKey(spec, secret(), 16, 32, "AES");
53+
assertFalse(Arrays.areEqual(key.getEncoded(), tail.getEncoded()));
54+
}
55+
56+
/**
57+
* The erase happens on every exit, the failure paths included - a caller who hits an exception
58+
* must not be left holding a live secret.
59+
*/
60+
public void testMakeSecretKeyErasesOnEveryExit()
61+
{
62+
KTSParameterSpec spec = new KTSParameterSpec.Builder("AES", 256).build();
63+
64+
// null algorithm name
65+
byte[] secret = secret();
66+
try
67+
{
68+
KdfUtil.makeSecretKey(spec, secret, 0, 16, null);
69+
fail("null algorithm accepted");
70+
}
71+
catch (NullPointerException expected)
72+
{
73+
assertTrue("secret not erased on the null-algorithm path", isErased(secret));
74+
}
75+
76+
// out-of-range request, refused before deriving
77+
secret = secret();
78+
try
79+
{
80+
KdfUtil.makeSecretKey(spec, secret, 0, 999, "AES");
81+
fail("out-of-range request accepted");
82+
}
83+
catch (IllegalArgumentException expected)
84+
{
85+
assertTrue("secret not erased on the range path", isErased(secret));
86+
}
87+
88+
// a KDF the derivation itself refuses
89+
KTSParameterSpec badKdf = new KTSParameterSpec.Builder("AES", 256)
90+
.withKdfAlgorithm(new AlgorithmIdentifier(new ASN1ObjectIdentifier("1.2.3.4"))).build();
91+
secret = secret();
92+
try
93+
{
94+
KdfUtil.makeSecretKey(badKdf, secret, 0, 16, "AES");
95+
fail("unknown KDF accepted");
96+
}
97+
catch (IllegalArgumentException expected)
98+
{
99+
assertTrue("secret not erased on the derivation path", isErased(secret));
100+
}
101+
}
102+
103+
public void testResolveKemSpecGuards()
104+
throws Exception
105+
{
106+
// a bad session key size is the caller's own error, not a spec failure
107+
try
108+
{
109+
KdfUtil.resolveKemSpec(null, "TEST", "test-set", 100);
110+
fail("non-whole-byte session key size accepted");
111+
}
112+
catch (IllegalArgumentException expected)
113+
{
114+
}
115+
116+
// null spec resolves to the mechanism's own secret, no KDF
117+
KTSParameterSpec resolved = KdfUtil.resolveKemSpec(null, "TEST", "test-set", 192);
118+
assertEquals("Generic", resolved.getKeyAlgorithmName());
119+
assertEquals(192, resolved.getKeySize());
120+
assertNull(resolved.getKdfAlgorithm());
121+
122+
// a KDF-less request above the session key is refused, naming the set and both sizes
123+
try
124+
{
125+
KdfUtil.resolveKemSpec(new KTSParameterSpec.Builder("AES", 256).withNoKdf().build(),
126+
"TEST", "test-set", 192);
127+
fail("unsatisfiable KDF-less spec accepted");
128+
}
129+
catch (InvalidAlgorithmParameterException expected)
130+
{
131+
assertEquals("no KDF specified and test-set produces a 192 bit secret, 256 requested",
132+
expected.getMessage());
133+
}
134+
}
135+
136+
public void testResolveAlgorithm()
137+
{
138+
KTSParameterSpec generic = new KTSParameterSpec.Builder("Generic", 256).build();
139+
KTSParameterSpec aes = new KTSParameterSpec.Builder("AES", 256).build();
140+
141+
assertEquals("AES", KdfUtil.resolveAlgorithm(generic, "AES"));
142+
assertEquals("AES", KdfUtil.resolveAlgorithm(aes, "Generic"));
143+
assertEquals("AES", KdfUtil.resolveAlgorithm(aes, "AES"));
144+
145+
try
146+
{
147+
KdfUtil.resolveAlgorithm(aes, "AES-KWP");
148+
fail("algorithm mismatch accepted");
149+
}
150+
catch (UnsupportedOperationException expected)
151+
{
152+
assertEquals("AES does not match AES-KWP", expected.getMessage());
153+
}
154+
}
155+
}

‎prov/src/test/jdk17/org/bouncycastle/jce/provider/test/SM9KEM17Test.java‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,29 @@ public void testGuards()
151151
catch (InvalidAlgorithmParameterException expected)
152152
{
153153
}
154+
155+
// a spec with no key algorithm name is refused at construction - SM9 does not go
156+
// through KdfUtil.resolveKemSpec, so it carries this check itself; without it the null
157+
// is substituted for a "Generic" request and fails deep inside the derivation
158+
KTSParameterSpec nullName = new KTSParameterSpec.Builder(null, 256).withNoKdf().build();
159+
try
160+
{
161+
kem.newEncapsulator(bobPublic, nullName, null);
162+
fail("encapsulator accepted a spec with no key algorithm name");
163+
}
164+
catch (InvalidAlgorithmParameterException expected)
165+
{
166+
}
167+
168+
try
169+
{
170+
kem.newDecapsulator(((SM9EncMasterPrivateKey)master.getPrivate())
171+
.generateUserKeyPair(bobIdentity, SM9EncMasterPrivateKeyParameters.HID).getPrivate(), nullName);
172+
fail("decapsulator accepted a spec with no key algorithm name");
173+
}
174+
catch (InvalidAlgorithmParameterException expected)
175+
{
176+
}
154177
}
155178

156179
/**

0 commit comments

Comments
 (0)