Skip to content

Commit 276a1bf

Browse files
committed
8236671: NullPointerException in JKS keystore
Reviewed-by: hchao, xuelei
1 parent e9370a1 commit 276a1bf

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
@@ -293,7 +293,7 @@ public void engineSetKeyEntry(String alias, Key key, char[] password,
293293
}
294294

295295
} catch (Exception e) {
296-
throw new KeyStoreException(e.getMessage());
296+
throw new KeyStoreException(e.getMessage(), e);
297297
}
298298
}
299299
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,9 @@ public void engineSetKeyEntry(String alias, Key key, char[] password,
281281
if (!(key instanceof java.security.PrivateKey)) {
282282
throw new KeyStoreException("Cannot store non-PrivateKeys");
283283
}
284+
if (password == null) {
285+
throw new KeyStoreException("password can't be null");
286+
}
284287
try {
285288
synchronized(entries) {
286289
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, 2020, 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
*/
@@ -163,6 +163,17 @@ public void runTest(String provider) throws Exception {
163163
// create an empty key store
164164
ks.load(null, null);
165165

166+
// unit test - test with null password
167+
try {
168+
ks.setKeyEntry(ALIAS_HEAD, privateKey, null,
169+
new Certificate[] { cert });
170+
} catch (KeyStoreException e) {
171+
if (!e.getMessage().contains("password can\'t be null")) {
172+
throw new RuntimeException("Unexpected message:" + e.getMessage());
173+
}
174+
// expected
175+
}
176+
166177
// store the secret keys
167178
for (int j = 0; j < numEntries; j++) {
168179
ks.setKeyEntry(ALIAS_HEAD + j, privateKey, PASSWDK,

0 commit comments

Comments
 (0)