提交 ff10ce00 编写于 作者: V vinnie

8079129: NullPointerException in PKCS#12 Keystore in PKCS12KeyStore.java

Reviewed-by: weijun
上级 32526fdf
/* /*
* Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -1600,23 +1600,22 @@ public final class PKCS12KeyStore extends KeyStoreSpi { ...@@ -1600,23 +1600,22 @@ public final class PKCS12KeyStore extends KeyStoreSpi {
Entry entry = entries.get(alias); Entry entry = entries.get(alias);
// certificate chain // certificate chain
int chainLen = 1; Certificate[] certs;
Certificate[] certs = null;
if (entry instanceof PrivateKeyEntry) { if (entry instanceof PrivateKeyEntry) {
PrivateKeyEntry keyEntry = (PrivateKeyEntry) entry; PrivateKeyEntry keyEntry = (PrivateKeyEntry) entry;
if (keyEntry.chain == null) { if (keyEntry.chain != null) {
chainLen = 0; certs = keyEntry.chain;
} else { } else {
chainLen = keyEntry.chain.length; certs = new Certificate[0];
} }
certs = keyEntry.chain;
} else if (entry instanceof CertEntry) { } else if (entry instanceof CertEntry) {
certs = new Certificate[]{((CertEntry) entry).cert}; certs = new Certificate[]{((CertEntry) entry).cert};
} else {
certs = new Certificate[0];
} }
for (int i = 0; i < chainLen; i++) { for (int i = 0; i < certs.length; i++) {
// create SafeBag of Type CertBag // create SafeBag of Type CertBag
DerOutputStream safeBag = new DerOutputStream(); DerOutputStream safeBag = new DerOutputStream();
safeBag.putOID(CertBag_OID); safeBag.putOID(CertBag_OID);
......
/* /*
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2013, 2015 Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -23,12 +23,14 @@ ...@@ -23,12 +23,14 @@
/* /*
* @test * @test
* @bug 8005408 * @bug 8005408 8079129
* @summary KeyStore API enhancements * @summary KeyStore API enhancements
*/ */
import java.io.*; import java.io.*;
import java.security.*; import java.security.*;
import java.security.cert.*;
import java.security.cert.Certificate;
import java.util.*; import java.util.*;
import javax.crypto.*; import javax.crypto.*;
import javax.crypto.spec.*; import javax.crypto.spec.*;
...@@ -39,7 +41,9 @@ public class StoreSecretKeyTest { ...@@ -39,7 +41,9 @@ public class StoreSecretKeyTest {
private final static String DIR = System.getProperty("test.src", "."); private final static String DIR = System.getProperty("test.src", ".");
private static final char[] PASSWORD = "passphrase".toCharArray(); private static final char[] PASSWORD = "passphrase".toCharArray();
private static final String KEYSTORE = "keystore.p12"; private static final String KEYSTORE = "keystore.p12";
private static final String ALIAS = "my secret key"; private static final String CERT = DIR + "/trusted.pem";
private static final String ALIAS = "my trusted cert";
private static final String ALIAS2 = "my secret key";
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
...@@ -56,8 +60,13 @@ public class StoreSecretKeyTest { ...@@ -56,8 +60,13 @@ public class StoreSecretKeyTest {
KeyStore keystore = KeyStore.getInstance("PKCS12"); KeyStore keystore = KeyStore.getInstance("PKCS12");
keystore.load(null, null); keystore.load(null, null);
// Set entry // Set trusted certificate entry
Certificate cert = loadCertificate(CERT);
keystore.setEntry(ALIAS, keystore.setEntry(ALIAS,
new KeyStore.TrustedCertificateEntry(cert), null);
// Set secret key entry
keystore.setEntry(ALIAS2,
new KeyStore.SecretKeyEntry(generateSecretKey("AES", 128)), new KeyStore.SecretKeyEntry(generateSecretKey("AES", 128)),
new KeyStore.PasswordProtection(PASSWORD)); new KeyStore.PasswordProtection(PASSWORD));
...@@ -73,7 +82,7 @@ public class StoreSecretKeyTest { ...@@ -73,7 +82,7 @@ public class StoreSecretKeyTest {
" entries"); " entries");
} }
KeyStore.Entry entry = keystore.getEntry(ALIAS, KeyStore.Entry entry = keystore.getEntry(ALIAS2,
new KeyStore.PasswordProtection(PASSWORD)); new KeyStore.PasswordProtection(PASSWORD));
System.out.println("Retrieved entry: " + entry); System.out.println("Retrieved entry: " + entry);
...@@ -101,4 +110,14 @@ public class StoreSecretKeyTest { ...@@ -101,4 +110,14 @@ public class StoreSecretKeyTest {
generator.init(size); generator.init(size);
return generator.generateKey(); return generator.generateKey();
} }
private static Certificate loadCertificate(String certFile)
throws Exception {
X509Certificate cert = null;
try (FileInputStream certStream = new FileInputStream(certFile)) {
CertificateFactory factory =
CertificateFactory.getInstance("X.509");
return factory.generateCertificate(certStream);
}
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册