28243
|
1 |
/*
|
|
2 |
* Copyright (c) 2014, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
* @test
|
|
26 |
* @bug 8044445
|
|
27 |
* @summary test new methods from JEP-229: Create PKCS12 Keystores by Default
|
|
28 |
*/
|
|
29 |
|
|
30 |
import java.io.*;
|
|
31 |
import java.security.*;
|
|
32 |
import java.security.KeyStore.*;
|
|
33 |
import java.security.cert.*;
|
|
34 |
import javax.crypto.*;
|
|
35 |
import javax.security.auth.callback.*;
|
|
36 |
|
|
37 |
public class ProbeKeystores {
|
|
38 |
private static final char[] PASSWORD = "changeit".toCharArray();
|
|
39 |
private static final char[] BAD_PASSWORD = "badpasword".toCharArray();
|
|
40 |
private static final String DIR = System.getProperty("test.src", ".");
|
|
41 |
private static final String CERT_FILE = "trusted.pem";
|
|
42 |
|
|
43 |
public static final void main(String[] args) throws Exception {
|
|
44 |
try {
|
|
45 |
test();
|
|
46 |
} finally {
|
|
47 |
cleanup();
|
|
48 |
}
|
|
49 |
}
|
|
50 |
|
|
51 |
private static final void test() throws Exception {
|
|
52 |
cleanup();
|
|
53 |
|
|
54 |
// Testing empty keystores
|
|
55 |
|
|
56 |
init("empty.jks", "JKS");
|
|
57 |
init("empty.jceks", "JCEKS");
|
|
58 |
init("empty.p12", "PKCS12");
|
|
59 |
|
|
60 |
load("empty.jks", "JKS");
|
|
61 |
load("empty.jceks", "JCEKS");
|
|
62 |
load("empty.p12", "PKCS12");
|
|
63 |
load("empty.jks", "PKCS12"); // test compatibility mode
|
|
64 |
load("empty.p12", "JKS"); // test compatibility mode
|
|
65 |
load("empty.jks", "PKCS12", true); // test without compatibility mode
|
|
66 |
load("empty.jks", "JKS", false); // test without compatibility mode
|
|
67 |
load("empty.p12", "JKS", true); // test without compatibility mode
|
|
68 |
load("empty.p12", "PKCS12", false); // test without compatibility mode
|
|
69 |
|
|
70 |
probe("empty.jks", "JKS");
|
|
71 |
probe("empty.jceks", "JCEKS");
|
|
72 |
probe("empty.p12", "PKCS12");
|
|
73 |
|
|
74 |
build("empty.jks", "JKS", true);
|
|
75 |
build("empty.jks", "JKS", false);
|
|
76 |
build("empty.jceks", "JCEKS", true);
|
|
77 |
build("empty.jceks", "JCEKS", false);
|
|
78 |
build("empty.p12", "PKCS12", true);
|
|
79 |
build("empty.p12", "PKCS12", false);
|
|
80 |
|
|
81 |
// Testing keystores containing an X.509 certificate
|
|
82 |
|
|
83 |
X509Certificate cert = loadCertificate(CERT_FILE);
|
|
84 |
init("onecert.jks", "JKS", cert);
|
|
85 |
init("onecert.jceks", "JCEKS", cert);
|
|
86 |
init("onecert.p12", "PKCS12", cert);
|
|
87 |
|
|
88 |
load("onecert.jks", "JKS");
|
|
89 |
load("onecert.jceks", "JCEKS");
|
|
90 |
load("onecert.p12", "PKCS12");
|
|
91 |
load("onecert.jks", "PKCS12"); // test compatibility mode
|
|
92 |
load("onecert.p12", "JKS"); // test compatibility mode
|
|
93 |
load("onecert.jks", "PKCS12", true); // test without compatibility mode
|
|
94 |
load("onecert.jks", "JKS", false); // test without compatibility mode
|
|
95 |
load("onecert.p12", "JKS", true); // test without compatibility mode
|
|
96 |
load("onecert.p12", "PKCS12", false); // test without compatibility mode
|
|
97 |
|
|
98 |
probe("onecert.jks", "JKS");
|
|
99 |
probe("onecert.jceks", "JCEKS");
|
|
100 |
probe("onecert.p12", "PKCS12");
|
|
101 |
|
|
102 |
build("onecert.jks", "JKS", true);
|
|
103 |
build("onecert.jks", "JKS", false);
|
|
104 |
build("onecert.jceks", "JCEKS", true);
|
|
105 |
build("onecert.jceks", "JCEKS", false);
|
|
106 |
build("onecert.p12", "PKCS12", true);
|
|
107 |
build("onecert.p12", "PKCS12", false);
|
|
108 |
|
|
109 |
// Testing keystores containing a secret key
|
|
110 |
|
|
111 |
SecretKey key = generateSecretKey("AES", 128);
|
|
112 |
init("onekey.jceks", "JCEKS", key);
|
|
113 |
init("onekey.p12", "PKCS12", key);
|
|
114 |
|
|
115 |
load("onekey.jceks", "JCEKS");
|
|
116 |
load("onekey.p12", "PKCS12");
|
|
117 |
load("onekey.p12", "JKS"); // test compatibility mode
|
|
118 |
load("onekey.p12", "JKS", true); // test without compatibility mode
|
|
119 |
load("onekey.p12", "PKCS12", false); // test without compatibility mode
|
|
120 |
|
|
121 |
probe("onekey.jceks", "JCEKS");
|
|
122 |
probe("onekey.p12", "PKCS12");
|
|
123 |
|
|
124 |
build("onekey.jceks", "JCEKS", true);
|
|
125 |
build("onekey.jceks", "JCEKS", false);
|
|
126 |
build("onekey.p12", "PKCS12", true);
|
|
127 |
build("onekey.p12", "PKCS12", false);
|
|
128 |
|
|
129 |
System.out.println("OK.");
|
|
130 |
}
|
|
131 |
|
|
132 |
private static void cleanup() {
|
|
133 |
new File("empty.jks").delete();
|
|
134 |
new File("empty.jceks").delete();
|
|
135 |
new File("empty.p12").delete();
|
|
136 |
new File("onecert.jks").delete();
|
|
137 |
new File("onecert.jceks").delete();
|
|
138 |
new File("onecert.p12").delete();
|
|
139 |
new File("onekey.jceks").delete();
|
|
140 |
new File("onekey.p12").delete();
|
|
141 |
}
|
|
142 |
|
|
143 |
// Instantiate an empty keystore using the supplied keystore type
|
|
144 |
private static void init(String file, String type) throws Exception {
|
|
145 |
KeyStore ks = KeyStore.getInstance(type);
|
|
146 |
ks.load(null, null);
|
|
147 |
try (OutputStream stream = new FileOutputStream(DIR + "/" + file)) {
|
|
148 |
ks.store(stream, PASSWORD);
|
|
149 |
}
|
|
150 |
System.out.println("Created a " + type + " keystore named '" + file + "'");
|
|
151 |
}
|
|
152 |
|
|
153 |
// Instantiate a keystore using the supplied keystore type & create an entry
|
|
154 |
private static void init(String file, String type, X509Certificate cert)
|
|
155 |
throws Exception {
|
|
156 |
KeyStore ks = KeyStore.getInstance(type);
|
|
157 |
ks.load(null, null);
|
|
158 |
ks.setEntry("mycert", new KeyStore.TrustedCertificateEntry(cert), null);
|
|
159 |
try (OutputStream stream = new FileOutputStream(DIR + "/" + file)) {
|
|
160 |
ks.store(stream, PASSWORD);
|
|
161 |
}
|
|
162 |
System.out.println("Created a " + type + " keystore named '" + file + "'");
|
|
163 |
}
|
|
164 |
|
|
165 |
// Instantiate a keystore using the supplied keystore type & create an entry
|
|
166 |
private static void init(String file, String type, SecretKey key)
|
|
167 |
throws Exception {
|
|
168 |
KeyStore ks = KeyStore.getInstance(type);
|
|
169 |
ks.load(null, null);
|
|
170 |
ks.setEntry("mykey", new KeyStore.SecretKeyEntry(key),
|
|
171 |
new PasswordProtection(PASSWORD));
|
|
172 |
try (OutputStream stream = new FileOutputStream(DIR + "/" + file)) {
|
|
173 |
ks.store(stream, PASSWORD);
|
|
174 |
}
|
|
175 |
System.out.println("Created a " + type + " keystore named '" + file + "'");
|
|
176 |
}
|
|
177 |
|
|
178 |
// Instantiate a keystore by probing the supplied file for the keystore type
|
|
179 |
private static void probe(String file, String type) throws Exception {
|
|
180 |
// First try with the correct password
|
|
181 |
KeyStore ks = KeyStore.getInstance(new File(DIR, file), PASSWORD);
|
|
182 |
if (!type.equalsIgnoreCase(ks.getType())) {
|
|
183 |
throw new Exception("ERROR: expected a " + type + " keystore, " +
|
|
184 |
"got a " + ks.getType() + " keystore instead");
|
|
185 |
} else {
|
|
186 |
System.out.println("Probed a " + type + " keystore named '" + file + "'");
|
|
187 |
}
|
|
188 |
|
|
189 |
// Next try with an incorrect password
|
|
190 |
try {
|
|
191 |
ks = KeyStore.getInstance(new File(DIR, file), BAD_PASSWORD);
|
|
192 |
throw new Exception("ERROR: expected an exception but got success");
|
|
193 |
} catch (IOException e) {
|
|
194 |
System.out.println("Failed to load a " + type + " keystore named '" + file + "' (as expected)");
|
|
195 |
}
|
|
196 |
}
|
|
197 |
|
|
198 |
// Instantiate a keystore by probing the supplied file for the keystore type
|
|
199 |
private static void build(String file, String type, boolean usePassword)
|
|
200 |
throws Exception {
|
|
201 |
|
|
202 |
Builder builder;
|
|
203 |
if (usePassword) {
|
|
204 |
builder = Builder.newInstance(new File(DIR, file),
|
|
205 |
new PasswordProtection(PASSWORD));
|
|
206 |
} else {
|
|
207 |
builder = Builder.newInstance(new File(DIR, file),
|
|
208 |
new CallbackHandlerProtection(new DummyHandler()));
|
|
209 |
}
|
|
210 |
KeyStore ks = builder.getKeyStore();
|
|
211 |
if (!type.equalsIgnoreCase(ks.getType())) {
|
|
212 |
throw new Exception("ERROR: expected a " + type + " keystore, " +
|
|
213 |
"got a " + ks.getType() + " keystore instead");
|
|
214 |
} else {
|
|
215 |
System.out.println("Built a " + type + " keystore named '" + file + "'");
|
|
216 |
}
|
|
217 |
}
|
|
218 |
|
|
219 |
// Load the keystore entries
|
|
220 |
private static void load(String file, String type) throws Exception {
|
|
221 |
KeyStore ks = KeyStore.getInstance(type);
|
|
222 |
try (InputStream stream = new FileInputStream(DIR + "/" + file)) {
|
|
223 |
ks.load(stream, PASSWORD);
|
|
224 |
}
|
|
225 |
if (!type.equalsIgnoreCase(ks.getType())) {
|
|
226 |
throw new Exception("ERROR: expected a " + type + " keystore, " +
|
|
227 |
"got a " + ks.getType() + " keystore instead");
|
|
228 |
} else {
|
|
229 |
System.out.println("Loaded a " + type + " keystore named '" + file + "'");
|
|
230 |
}
|
|
231 |
}
|
|
232 |
|
|
233 |
// Load the keystore entries (with compatibility mode disabled)
|
|
234 |
private static void load(String file, String type, boolean expectFailure)
|
|
235 |
throws Exception {
|
|
236 |
Security.setProperty("keystore.type.compat", "false");
|
|
237 |
try {
|
|
238 |
load(file, type);
|
|
239 |
if (expectFailure) {
|
|
240 |
throw new Exception("ERROR: expected load to fail but it didn't");
|
|
241 |
}
|
|
242 |
} catch (IOException e) {
|
|
243 |
if (expectFailure) {
|
|
244 |
System.out.println("Failed to load a " + type + " keystore named '" + file + "' (as expected)");
|
|
245 |
} else {
|
|
246 |
throw e;
|
|
247 |
}
|
|
248 |
} finally {
|
|
249 |
Security.setProperty("keystore.type.compat", "true");
|
|
250 |
}
|
|
251 |
}
|
|
252 |
|
|
253 |
// Read an X.509 certificate from the supplied file
|
|
254 |
private static X509Certificate loadCertificate(String certFile)
|
|
255 |
throws Exception {
|
|
256 |
X509Certificate cert = null;
|
|
257 |
try (FileInputStream certStream =
|
|
258 |
new FileInputStream(DIR + "/" + certFile)) {
|
|
259 |
CertificateFactory factory =
|
|
260 |
CertificateFactory.getInstance("X.509");
|
|
261 |
return (X509Certificate) factory.generateCertificate(certStream);
|
|
262 |
}
|
|
263 |
}
|
|
264 |
|
|
265 |
// Generate a secret key using the supplied algorithm name and key size
|
|
266 |
private static SecretKey generateSecretKey(String algorithm, int size)
|
|
267 |
throws NoSuchAlgorithmException {
|
|
268 |
KeyGenerator generator = KeyGenerator.getInstance(algorithm);
|
|
269 |
generator.init(size);
|
|
270 |
return generator.generateKey();
|
|
271 |
}
|
|
272 |
|
|
273 |
private static class DummyHandler implements CallbackHandler {
|
|
274 |
public void handle(Callback[] callbacks)
|
|
275 |
throws IOException, UnsupportedCallbackException {
|
|
276 |
System.out.println("** Callbackhandler invoked");
|
|
277 |
for (int i = 0; i < callbacks.length; i++) {
|
|
278 |
Callback cb = callbacks[i];
|
|
279 |
if (cb instanceof PasswordCallback) {
|
|
280 |
PasswordCallback pcb = (PasswordCallback)cb;
|
|
281 |
pcb.setPassword(PASSWORD);
|
|
282 |
break;
|
|
283 |
}
|
|
284 |
}
|
|
285 |
}
|
|
286 |
}
|
|
287 |
}
|