Skip to content

Commit 44ef6fe

Browse files
committed
8236671: NullPointerException in JKS keystore
Reviewed-by: clanger Backport-of: 276a1bf
1 parent 240ef44 commit 44ef6fe

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

src/java.base/share/classes/com/sun/crypto/provider/JceKeyStore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ public void engineSetKeyEntry(String alias, Key key, char[] password,
291291
}
292292

293293
} catch (Exception e) {
294-
throw new KeyStoreException(e.getMessage());
294+
throw new KeyStoreException(e.getMessage(), e);
295295
}
296296
}
297297
}

src/java.base/share/classes/sun/security/provider/JavaKeyStore.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,9 @@ public void engineSetKeyEntry(String alias, Key key, char[] password,
263263
if (!(key instanceof java.security.PrivateKey)) {
264264
throw new KeyStoreException("Cannot store non-PrivateKeys");
265265
}
266+
if (password == null) {
267+
throw new KeyStoreException("password can't be null");
268+
}
266269
try {
267270
synchronized(entries) {
268271
KeyEntry entry = new KeyEntry();

test/jdk/java/security/KeyStore/TestKeyStoreBasic.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -40,7 +40,7 @@
4040

4141
/*
4242
* @test
43-
* @bug 8048621 8133090 8167371
43+
* @bug 8048621 8133090 8167371 8236671
4444
* @summary Test basic operations with keystores (jks, jceks, pkcs12)
4545
* @author Yu-Ching Valerie PENG
4646
*/
@@ -181,6 +181,17 @@ public void runTest(String provider) throws Exception {
181181
// create an empty key store
182182
ks.load(null, null);
183183

184+
// unit test - test with null password
185+
try {
186+
ks.setKeyEntry(ALIAS_HEAD, privateKey, null,
187+
new Certificate[] { cert });
188+
} catch (KeyStoreException e) {
189+
if (!e.getMessage().contains("password can\'t be null")) {
190+
throw new RuntimeException("Unexpected message:" + e.getMessage());
191+
}
192+
// expected
193+
}
194+
184195
// store the secret keys
185196
for (int j = 0; j < numEntries; j++) {
186197
ks.setKeyEntry(ALIAS_HEAD + j, privateKey, PASSWDK,

0 commit comments

Comments
 (0)