Skip to content

Commit 5037a78

Browse files
committed
Repair the BCPQC KeyStoreTest and register it in AllTests so it actually runs: store the stateful XMSSMT root key only after its last certificate signature, as BKS and BCFKS snapshot the encoding at setKeyEntry time, and assert the reloaded keystore rather than querying the in-memory one again.
1 parent 40c928f commit 5037a78

2 files changed

Lines changed: 12 additions & 7 deletions

File tree

‎prov/src/test/java/org/bouncycastle/pqc/jcajce/provider/test/AllTests.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public static Test suite()
8585
suite.addTestSuite(NTRUPlusTest.class);
8686
suite.addTestSuite(NTRUPlusKeyPairGeneratorTest.class);
8787
suite.addTestSuite(SmaugTTest.class);
88+
suite.addTestSuite(KeyStoreTest.class);
8889

8990
return new BCTestSetup(suite);
9091
}

‎prov/src/test/java/org/bouncycastle/pqc/jcajce/provider/test/KeyStoreTest.java‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,7 @@ private void tryKeyStore(String format)
108108
X509Certificate rootCA = createPQSelfSignedCert(nameBuilder.build(), "SHA256WITHXMSSMT", kp);
109109
X509Certificate[] chain = new X509Certificate[1];
110110
chain[0] = rootCA;
111-
// store root private key
112111
String alias1 = "xmssmt private";
113-
store.setKeyEntry(alias1, kp.getPrivate(), password.toCharArray(), chain);
114112
// store root certificate
115113
store.setCertificateEntry("root ca", rootCA);
116114

@@ -151,15 +149,19 @@ private void tryKeyStore(String format)
151149
String alias2 = "private key 1";
152150
String alias3 = "private key 2";
153151

154-
// store private keys
152+
// store private keys - the XMSSMT root key only now, after its last signature: the
153+
// key is stateful, each signature advances it, and BKS/BCFKS snapshot the encoding
154+
// at setKeyEntry time, so an entry stored before the certificates were signed would
155+
// no longer compare equal to the live key below
156+
store.setKeyEntry(alias1, kp.getPrivate(), password.toCharArray(), chain);
155157
store.setKeyEntry(alias2, ntruKp.getPrivate(), password.toCharArray(), chain1);
156158
store.setKeyEntry(alias3, sphincsKp.getPrivate(), password.toCharArray(), chain2);
157159

158160
// store certificates
159161
store.setCertificateEntry("cert 1", cert1);
160162
store.setCertificateEntry("cert 2", cert2);
161163

162-
// can't restore keys from keystore
164+
// the entries as held by the in-memory store
163165
Key k1 = store.getKey(alias1, password.toCharArray());
164166

165167
assertEquals(kp.getPrivate(), k1);
@@ -180,15 +182,17 @@ private void tryKeyStore(String format)
180182

181183
bcStore.load(new ByteArrayInputStream(bOut.toByteArray()), "fred".toCharArray());
182184

183-
k1 = store.getKey(alias1, password.toCharArray());
185+
// and the entries recovered from the persisted form - this used to query the
186+
// in-memory store again, so the round trip through the stored bytes went unasserted
187+
k1 = bcStore.getKey(alias1, password.toCharArray());
184188

185189
assertEquals(kp.getPrivate(), k1);
186190

187-
k2 = store.getKey(alias2, password.toCharArray());
191+
k2 = bcStore.getKey(alias2, password.toCharArray());
188192

189193
assertEquals(ntruKp.getPrivate(), k2);
190194

191-
k3 = store.getKey(alias3, password.toCharArray());
195+
k3 = bcStore.getKey(alias3, password.toCharArray());
192196

193197
assertEquals(sphincsKp.getPrivate(), k3);
194198
}

0 commit comments

Comments
 (0)