2
|
1 |
/*
|
|
2 |
* Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/**
|
|
25 |
* @test
|
|
26 |
* @bug 6269847
|
|
27 |
* @summary store a NSS PKCS11 PrivateKeyEntry to JKS KeyStore throws confusing NPE
|
|
28 |
* @author Wang Weijun
|
|
29 |
* @library ..
|
|
30 |
*/
|
|
31 |
|
|
32 |
import java.util.*;
|
|
33 |
|
|
34 |
import java.security.*;
|
|
35 |
import java.security.KeyStore.*;
|
|
36 |
import java.security.cert.*;
|
|
37 |
|
|
38 |
public class JksSetPrivateKey extends SecmodTest {
|
|
39 |
|
|
40 |
public static void main(String[] args) throws Exception {
|
|
41 |
if (initSecmod() == false) {
|
|
42 |
return;
|
|
43 |
}
|
|
44 |
|
|
45 |
String configName = BASE + SEP + "nss.cfg";
|
|
46 |
Provider p = getSunPKCS11(configName);
|
|
47 |
|
|
48 |
System.out.println(p);
|
|
49 |
Security.addProvider(p);
|
|
50 |
KeyStore ks = KeyStore.getInstance("PKCS11", p);
|
|
51 |
ks.load(null, password);
|
|
52 |
Collection<String> aliases = new TreeSet<String>(Collections.list(ks.aliases()));
|
|
53 |
System.out.println("entries: " + aliases.size());
|
|
54 |
System.out.println(aliases);
|
|
55 |
|
|
56 |
PrivateKey privateKey = (PrivateKey)ks.getKey(keyAlias, password);
|
|
57 |
System.out.println(privateKey);
|
|
58 |
|
|
59 |
X509Certificate[] chain = (X509Certificate[])ks.getCertificateChain(keyAlias);
|
|
60 |
|
|
61 |
KeyStore jks = KeyStore.getInstance("JKS");
|
|
62 |
jks.load(null, null);
|
|
63 |
|
|
64 |
try {
|
|
65 |
jks.setKeyEntry("k1", privateKey, "changeit".toCharArray(), chain);
|
|
66 |
throw new Exception("No, an NSS PrivateKey shouldn't be extractable and put inside a JKS keystore");
|
|
67 |
} catch (KeyStoreException e) {
|
|
68 |
System.err.println(e);; // This is OK
|
|
69 |
}
|
|
70 |
|
|
71 |
try {
|
|
72 |
jks.setKeyEntry("k2", new DummyPrivateKey(), "changeit".toCharArray(), chain);
|
|
73 |
throw new Exception("No, non-PKCS#8 key shouldn't be put inside a KeyStore");
|
|
74 |
} catch (KeyStoreException e) {
|
|
75 |
System.err.println(e);; // This is OK
|
|
76 |
}
|
|
77 |
System.out.println("OK");
|
|
78 |
|
|
79 |
try {
|
|
80 |
jks.setKeyEntry("k3", new DummyPrivateKey2(), "changeit".toCharArray(), chain);
|
|
81 |
throw new Exception("No, not-extractble key shouldn't be put inside a KeyStore");
|
|
82 |
} catch (KeyStoreException e) {
|
|
83 |
System.err.println(e);; // This is OK
|
|
84 |
}
|
|
85 |
System.out.println("OK");
|
|
86 |
}
|
|
87 |
}
|
|
88 |
|
|
89 |
class DummyPrivateKey implements PrivateKey {
|
|
90 |
public String getAlgorithm() {
|
|
91 |
return "DUMMY";
|
|
92 |
}
|
|
93 |
|
|
94 |
public String getFormat() {
|
|
95 |
return "DUMMY";
|
|
96 |
}
|
|
97 |
|
|
98 |
public byte[] getEncoded() {
|
|
99 |
return "DUMMY".getBytes();
|
|
100 |
}
|
|
101 |
}
|
|
102 |
|
|
103 |
class DummyPrivateKey2 implements PrivateKey {
|
|
104 |
public String getAlgorithm() {
|
|
105 |
return "DUMMY";
|
|
106 |
}
|
|
107 |
|
|
108 |
public String getFormat() {
|
|
109 |
return "PKCS#8";
|
|
110 |
}
|
|
111 |
|
|
112 |
public byte[] getEncoded() {
|
|
113 |
return null;
|
|
114 |
}
|
|
115 |
}
|