author | valeriep |
Fri, 29 Mar 2019 00:39:49 +0000 | |
changeset 54333 | 2a29e62446bd |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
54333
2a29e62446bd
8220016: Clean up redundant RSA services in the SunJSSE provider
valeriep
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
import java.io.*; |
|
25 |
import java.util.*; |
|
26 |
||
27 |
import java.security.KeyStore; |
|
28 |
import java.security.KeyStoreException; |
|
29 |
import java.security.KeyFactory; |
|
30 |
import java.security.KeyPairGenerator; |
|
31 |
import java.security.KeyPair; |
|
32 |
import java.security.SecureRandom; |
|
33 |
import java.security.AuthProvider; |
|
34 |
import java.security.PrivateKey; |
|
35 |
import java.security.Provider; |
|
36 |
import java.security.ProviderException; |
|
37 |
import java.security.Signature; |
|
38 |
import java.security.Security; |
|
39 |
||
40 |
import java.security.cert.*; |
|
41 |
import java.security.spec.*; |
|
42 |
import java.security.interfaces.*; |
|
43 |
||
44 |
import javax.crypto.SecretKey; |
|
45 |
||
46 |
import javax.security.auth.Subject; |
|
47 |
import javax.security.auth.login.LoginException; |
|
48 |
||
49 |
import com.sun.security.auth.module.*; |
|
50 |
import com.sun.security.auth.callback.*; |
|
51 |
||
52 |
||
53 |
public class Basic extends PKCS11Test { |
|
54 |
||
55 |
private static final char SEP = File.separatorChar; |
|
56 |
||
57 |
private static String DIR = System.getProperty("DIR"); |
|
58 |
private static char[] tokenPwd; |
|
59 |
private static final char[] ibuttonPwd = |
|
60 |
new char[0]; |
|
61 |
private static final char[] activcardPwd = |
|
62 |
new char[] { '1', '1', '2', '2', '3', '3' }; |
|
63 |
private static final char[] nssPwd = |
|
64 |
new char[] { 't', 'e', 's', 't', '1', '2' }; |
|
65 |
private static final char[] solarisPwd = |
|
66 |
new char[] { 'p', 'i', 'n' }; |
|
67 |
private static final char[] sca1000Pwd = |
|
68 |
new char[] { 'p', 'a', 's', 's', 'w', 'o', 'r', 'd' }; |
|
69 |
private static final char[] sPwd = { 'f', 'o', 'o' }; |
|
70 |
||
71 |
private static SecretKey sk1; |
|
72 |
private static SecretKey sk2; |
|
73 |
private static SecretKey sk3; |
|
74 |
private static SecretKey sk4; |
|
75 |
||
76 |
private static RSAPrivateCrtKey pk1; |
|
77 |
private static PrivateKey pk2; |
|
78 |
private static PrivateKey pk3; |
|
79 |
||
80 |
private static Certificate[] chain1; |
|
81 |
private static Certificate[] chain2; |
|
82 |
private static Certificate[] chain3; |
|
83 |
private static Certificate[] chain4; |
|
84 |
||
85 |
private static X509Certificate randomCert; |
|
86 |
||
87 |
private static KeyStore ks; |
|
88 |
private static final String KS_TYPE = "PKCS11"; |
|
89 |
private static Provider provider; |
|
90 |
||
91 |
private static class FooEntry implements KeyStore.Entry { } |
|
92 |
||
93 |
private static class P11SecretKey implements SecretKey { |
|
94 |
String alg; |
|
95 |
int length; |
|
96 |
public P11SecretKey(String alg, int length) { |
|
97 |
this.alg = alg; |
|
98 |
this.length = length; |
|
99 |
} |
|
100 |
public String getAlgorithm() { return alg; } |
|
101 |
public String getFormat() { return "raw"; } |
|
102 |
public byte[] getEncoded() { return new byte[length/8]; } |
|
103 |
} |
|
104 |
||
105 |
public static void main(String[] args) throws Exception { |
|
37353
e8e684b89c81
8153545: sun/security/pkcs11/Provider/Login.sh fails on Linux
amjiang
parents:
31270
diff
changeset
|
106 |
main(new Basic(), args); |
2 | 107 |
} |
108 |
||
109 |
public void main(Provider p) throws Exception { |
|
110 |
||
111 |
this.provider = p; |
|
112 |
||
113 |
// get private keys |
|
54333
2a29e62446bd
8220016: Clean up redundant RSA services in the SunJSSE provider
valeriep
parents:
47216
diff
changeset
|
114 |
KeyFactory kf = KeyFactory.getInstance("RSA"); |
2 | 115 |
KeyFactory dsaKf = KeyFactory.getInstance("DSA", "SUN"); |
116 |
||
117 |
ObjectInputStream ois1 = new ObjectInputStream |
|
118 |
(new FileInputStream(new File(DIR, "pk1.key"))); |
|
119 |
byte[] keyBytes = (byte[])ois1.readObject(); |
|
120 |
ois1.close(); |
|
121 |
PrivateKey tmpKey = |
|
122 |
kf.generatePrivate(new PKCS8EncodedKeySpec(keyBytes)); |
|
123 |
pk1 = (RSAPrivateCrtKey)tmpKey; |
|
124 |
||
125 |
ObjectInputStream ois2 = new ObjectInputStream |
|
126 |
(new FileInputStream(new File(DIR, "pk2.key"))); |
|
127 |
keyBytes = (byte[])ois2.readObject(); |
|
128 |
ois2.close(); |
|
129 |
pk2 = kf.generatePrivate(new PKCS8EncodedKeySpec(keyBytes)); |
|
130 |
||
131 |
ObjectInputStream ois3 = new ObjectInputStream |
|
132 |
(new FileInputStream(new File(DIR, "pk3.key"))); |
|
133 |
keyBytes = (byte[])ois3.readObject(); |
|
134 |
pk3 = kf.generatePrivate(new PKCS8EncodedKeySpec(keyBytes)); |
|
135 |
ois3.close(); |
|
136 |
||
137 |
// get cert chains for private keys |
|
138 |
CertificateFactory cf = CertificateFactory.getInstance("X.509", "SUN"); |
|
139 |
Certificate caCert = (X509Certificate)cf.generateCertificate |
|
140 |
(new FileInputStream(new File(DIR, "ca.cert"))); |
|
141 |
Certificate ca2Cert = (X509Certificate)cf.generateCertificate |
|
142 |
(new FileInputStream(new File(DIR, "ca2.cert"))); |
|
143 |
Certificate pk1cert = (X509Certificate)cf.generateCertificate |
|
144 |
(new FileInputStream(new File(DIR, "pk1.cert"))); |
|
145 |
Certificate pk1cert2 = (X509Certificate)cf.generateCertificate |
|
146 |
(new FileInputStream(new File(DIR, "pk1.cert2"))); |
|
147 |
Certificate pk2cert = (X509Certificate)cf.generateCertificate |
|
148 |
(new FileInputStream(new File(DIR, "pk2.cert"))); |
|
149 |
Certificate pk3cert = (X509Certificate)cf.generateCertificate |
|
150 |
(new FileInputStream(new File(DIR, "pk3.cert"))); |
|
151 |
chain1 = new Certificate[] { pk1cert, caCert }; |
|
152 |
chain2 = new Certificate[] { pk2cert, caCert }; |
|
153 |
chain3 = new Certificate[] { pk3cert, caCert }; |
|
154 |
chain4 = new Certificate[] { pk1cert2, ca2Cert }; |
|
155 |
||
156 |
// create secret keys |
|
157 |
sk1 = new P11SecretKey("DES", 64); |
|
158 |
sk2 = new P11SecretKey("DESede", 192); |
|
159 |
sk3 = new P11SecretKey("AES", 128); |
|
160 |
sk4 = new P11SecretKey("RC4", 128); |
|
161 |
||
162 |
// read randomCert |
|
163 |
randomCert = (X509Certificate)cf.generateCertificate |
|
164 |
(new FileInputStream(new File(DIR, "random.cert"))); |
|
165 |
||
166 |
doTest(); |
|
167 |
} |
|
168 |
||
169 |
private static void doTest() throws Exception { |
|
170 |
||
171 |
String token = System.getProperty("TOKEN"); |
|
172 |
String test = System.getProperty("TEST"); |
|
173 |
||
174 |
if (token == null || token.length() == 0) { |
|
175 |
throw new Exception("token arg required"); |
|
176 |
} |
|
177 |
if (test == null || test.length() == 0) { |
|
178 |
throw new Exception("test arg required"); |
|
179 |
} |
|
180 |
||
181 |
if ("ibutton".equals(token)) { |
|
182 |
tokenPwd = ibuttonPwd; |
|
183 |
} else if ("activcard".equals(token)) { |
|
184 |
tokenPwd = activcardPwd; |
|
185 |
} else if ("nss".equals(token)) { |
|
186 |
tokenPwd = nssPwd; |
|
187 |
} else if ("sca1000".equals(token)) { |
|
188 |
tokenPwd = sca1000Pwd; |
|
189 |
} else if ("solaris".equals(token)) { |
|
190 |
tokenPwd = solarisPwd; |
|
191 |
} |
|
192 |
||
193 |
if ("list".equals(test)) { |
|
194 |
Basic.list(); |
|
195 |
} else if ("basic".equals(test)) { |
|
196 |
||
197 |
int testnum = 1; |
|
198 |
||
199 |
if ("ibutton".equals(token)) { |
|
200 |
// pkey and setAttribute |
|
201 |
testnum = Basic.pkey(testnum); |
|
202 |
testnum = Basic.setAttribute(testnum); |
|
203 |
} else if ("activcard".equals(token)) { |
|
204 |
// sign |
|
205 |
testnum = Basic.signAlias(testnum, null); |
|
206 |
} else if ("nss".equals(token)) { |
|
207 |
// setAttribute, pkey, sign |
|
208 |
testnum = Basic.setAttribute(testnum); |
|
209 |
testnum = Basic.pkey(testnum); |
|
210 |
testnum = Basic.sign(testnum); |
|
211 |
testnum = Basic.copy(testnum); |
|
212 |
} else if ("solaris".equals(token)) { |
|
213 |
testnum = Basic.setAttribute(testnum); |
|
214 |
testnum = Basic.pkey(testnum); |
|
215 |
testnum = Basic.sign(testnum); |
|
216 |
testnum = Basic.skey(testnum); |
|
217 |
testnum = Basic.copy(testnum); |
|
218 |
} else if ("sca1000".equals(token)) { |
|
219 |
// setAttribute, pkey, sign, skey, copy |
|
220 |
testnum = Basic.setAttribute(testnum); |
|
221 |
testnum = Basic.pkey(testnum); |
|
222 |
testnum = Basic.sign(testnum); |
|
223 |
testnum = Basic.skey(testnum); |
|
224 |
testnum = Basic.copy(testnum); |
|
225 |
} |
|
226 |
||
227 |
} else if ("pkey".equals(test)) { |
|
228 |
Basic.pkey(1); |
|
229 |
} else if ("skey".equals(test)) { |
|
230 |
Basic.skey(1); |
|
231 |
} else if ("setAttribute".equals(test)) { |
|
232 |
Basic.setAttribute(1); |
|
233 |
} else if ("copy".equals(test)) { |
|
234 |
Basic.copy(1); |
|
235 |
} else if ("sign".equals(test)) { |
|
236 |
Basic.sign(1); |
|
237 |
} else if ("module".equals(test)) { |
|
238 |
Basic.module(); |
|
239 |
} else if ("nss-extended".equals(test)) { |
|
240 |
||
241 |
// this only works if NSS_TEST is set to true in P11KeyStore.java |
|
242 |
||
243 |
int testnum = 1; |
|
244 |
testnum = Basic.setAttribute(testnum); |
|
245 |
testnum = Basic.pkey(testnum); |
|
246 |
testnum = Basic.sign(testnum); |
|
247 |
testnum = Basic.extended(testnum); |
|
248 |
} else { |
|
249 |
System.out.println("unrecognized command"); |
|
250 |
} |
|
251 |
} |
|
252 |
||
253 |
private static int sign(int testnum) throws Exception { |
|
254 |
if (ks == null) { |
|
255 |
ks = KeyStore.getInstance(KS_TYPE, provider); |
|
256 |
ks.load(null, tokenPwd); |
|
257 |
} |
|
258 |
if (!ks.containsAlias("pk1")) { |
|
259 |
ks.setKeyEntry("pk1", pk1, null, chain1); |
|
260 |
} |
|
261 |
System.out.println("test " + testnum++ + " passed"); |
|
262 |
||
263 |
return signAlias(testnum, "pk1"); |
|
264 |
} |
|
265 |
||
266 |
private static int signAlias(int testnum, String alias) throws Exception { |
|
267 |
||
268 |
if (ks == null) { |
|
269 |
ks = KeyStore.getInstance(KS_TYPE, provider); |
|
270 |
ks.load(null, tokenPwd); |
|
271 |
} |
|
272 |
||
273 |
if (alias == null) { |
|
274 |
Enumeration enu = ks.aliases(); |
|
275 |
if (enu.hasMoreElements()) { |
|
276 |
alias = (String)enu.nextElement(); |
|
277 |
} |
|
278 |
} |
|
279 |
||
280 |
PrivateKey pkey = (PrivateKey)ks.getKey(alias, null); |
|
281 |
if ("RSA".equals(pkey.getAlgorithm())) { |
|
282 |
System.out.println("got [" + alias + "] signing key: " + pkey); |
|
283 |
} else { |
|
284 |
throw new SecurityException |
|
285 |
("expected RSA, got " + pkey.getAlgorithm()); |
|
286 |
} |
|
287 |
||
288 |
Signature s = Signature.getInstance("MD5WithRSA", ks.getProvider()); |
|
289 |
s.initSign(pkey); |
|
290 |
System.out.println("initialized signature object with key"); |
|
291 |
s.update("hello".getBytes()); |
|
292 |
System.out.println("signature object updated with [hello] bytes"); |
|
293 |
||
294 |
byte[] signed = s.sign(); |
|
295 |
System.out.println("received signature " + signed.length + |
|
296 |
" bytes in length"); |
|
297 |
||
298 |
Signature v = Signature.getInstance("MD5WithRSA", ks.getProvider()); |
|
299 |
v.initVerify(ks.getCertificate(alias)); |
|
300 |
v.update("hello".getBytes()); |
|
301 |
v.verify(signed); |
|
302 |
System.out.println("signature verified"); |
|
303 |
System.out.println("test " + testnum++ + " passed"); |
|
304 |
||
305 |
return testnum; |
|
306 |
} |
|
307 |
||
308 |
private static int copy(int testnum) throws Exception { |
|
309 |
||
310 |
if (ks == null) { |
|
311 |
ks = KeyStore.getInstance(KS_TYPE, provider); |
|
312 |
ks.load(null, tokenPwd); |
|
313 |
} |
|
314 |
||
315 |
KeyFactory kf = KeyFactory.getInstance("RSA", provider); |
|
316 |
PrivateKey pkSession = (PrivateKey)kf.translateKey(pk3); |
|
317 |
System.out.println("pkSession = " + pkSession); |
|
318 |
ks.setKeyEntry("pkSession", pkSession, null, chain3); |
|
319 |
||
320 |
KeyStore.PrivateKeyEntry pke = |
|
321 |
(KeyStore.PrivateKeyEntry)ks.getEntry("pkSession", null); |
|
322 |
System.out.println("pkSession = " + pke.getPrivateKey()); |
|
323 |
Certificate[] chain = pke.getCertificateChain(); |
|
324 |
if (chain.length != chain3.length) { |
|
325 |
throw new SecurityException("received chain not correct length"); |
|
326 |
} |
|
327 |
for (int i = 0; i < chain.length; i++) { |
|
328 |
if (!chain[i].equals(chain3[i])) { |
|
329 |
throw new SecurityException("received chain not equal"); |
|
330 |
} |
|
331 |
} |
|
332 |
||
333 |
System.out.println("test " + testnum++ + " passed"); |
|
334 |
||
335 |
return testnum; |
|
336 |
} |
|
337 |
||
338 |
private static void list() throws Exception { |
|
339 |
int testnum = 1; |
|
340 |
||
341 |
ks = KeyStore.getInstance(KS_TYPE, provider); |
|
342 |
||
343 |
// check instance |
|
344 |
if (ks.getProvider() instanceof java.security.AuthProvider) { |
|
345 |
System.out.println("keystore provider instance of AuthProvider"); |
|
346 |
System.out.println("test " + testnum++ + " passed"); |
|
347 |
} else { |
|
348 |
throw new SecurityException("did not get AuthProvider KeyStore"); |
|
349 |
} |
|
350 |
||
351 |
// load |
|
352 |
ks.load(null, tokenPwd); |
|
353 |
System.out.println("test " + testnum++ + " passed"); |
|
354 |
||
355 |
// aliases |
|
356 |
Enumeration enu = ks.aliases(); |
|
357 |
int count = 0; |
|
358 |
while (enu.hasMoreElements()) { |
|
359 |
count++; |
|
360 |
System.out.println("alias " + |
|
361 |
count + |
|
362 |
" = " + |
|
363 |
(String)enu.nextElement()); |
|
364 |
} |
|
365 |
} |
|
366 |
||
367 |
private static void module() throws Exception { |
|
368 |
||
369 |
// perform Security.addProvider of P11 provider |
|
31270
e6470b24700d
7191662: JCE providers should be located via ServiceLoader
valeriep
parents:
30820
diff
changeset
|
370 |
Security.addProvider(getSunPKCS11(System.getProperty("CUSTOM_P11_CONFIG"))); |
2 | 371 |
|
372 |
String KS_PROVIDER = "SunPKCS11-" + System.getProperty("TOKEN"); |
|
373 |
||
374 |
KeyStoreLoginModule m = new KeyStoreLoginModule(); |
|
375 |
Subject s = new Subject(); |
|
376 |
Map options = new HashMap(); |
|
377 |
options.put("keyStoreURL", "NONE"); |
|
378 |
options.put("keyStoreType", KS_TYPE); |
|
379 |
options.put("keyStoreProvider", KS_PROVIDER); |
|
380 |
options.put("debug", "true"); |
|
381 |
m.initialize(s, new TextCallbackHandler(), new HashMap(), options); |
|
382 |
m.login(); |
|
383 |
m.commit(); |
|
384 |
System.out.println("authenticated subject = " + s); |
|
385 |
m.logout(); |
|
386 |
System.out.println("authenticated subject = " + s); |
|
387 |
} |
|
388 |
||
389 |
/** |
|
390 |
* SCA1000 does not handle extended secret key tests |
|
391 |
* . Blowfish (CKR_TEMPLATE_INCOMPLETE) |
|
392 |
* . AES (CKR_TEMPLATE_INCOMPLETE) |
|
393 |
* . RC4 (CKR_ATTRIBUTE_TYPE_INVALID) |
|
394 |
* so do this instead |
|
395 |
*/ |
|
396 |
private static int skey(int testnum) throws Exception { |
|
397 |
if (ks == null) { |
|
398 |
ks = KeyStore.getInstance(KS_TYPE, provider); |
|
399 |
ks.load(null, tokenPwd); |
|
400 |
} |
|
401 |
||
402 |
// delete all old aliases |
|
403 |
Enumeration enu = ks.aliases(); |
|
404 |
int count = 0; |
|
405 |
while (enu.hasMoreElements()) { |
|
406 |
String next = (String)enu.nextElement(); |
|
407 |
ks.deleteEntry(next); |
|
408 |
System.out.println("deleted entry for: " + next); |
|
409 |
} |
|
410 |
||
411 |
// set good ske 1 |
|
412 |
ks.setKeyEntry("sk1", sk1, null, null); |
|
413 |
System.out.println("test " + testnum++ + " passed"); |
|
414 |
||
415 |
// set good ske 2 |
|
416 |
ks.setKeyEntry("sk2", sk2, null, null); |
|
417 |
System.out.println("test " + testnum++ + " passed"); |
|
418 |
||
419 |
// getEntry good ske 1 |
|
420 |
KeyStore.SecretKeyEntry ske = |
|
421 |
(KeyStore.SecretKeyEntry)ks.getEntry("sk1", null); |
|
422 |
if ("DES".equals(ske.getSecretKey().getAlgorithm())) { |
|
423 |
System.out.println("test " + testnum++ + " passed"); |
|
424 |
} else { |
|
425 |
throw new SecurityException |
|
426 |
("expected DES, got " + ske.getSecretKey().getAlgorithm()); |
|
427 |
} |
|
428 |
||
429 |
// getEntry good ske 2 |
|
430 |
ske = (KeyStore.SecretKeyEntry)ks.getEntry("sk2", null); |
|
431 |
if ("DESede".equals(ske.getSecretKey().getAlgorithm())) { |
|
432 |
System.out.println("test " + testnum++ + " passed"); |
|
433 |
} else { |
|
434 |
throw new SecurityException |
|
435 |
("expected DESede, got " + ske.getSecretKey().getAlgorithm()); |
|
436 |
} |
|
437 |
||
438 |
// getKey good ske 1 |
|
439 |
SecretKey skey = (SecretKey)ks.getKey("sk1", null); |
|
440 |
if ("DES".equals(skey.getAlgorithm())) { |
|
441 |
System.out.println("test " + testnum++ + " passed"); |
|
442 |
} else { |
|
443 |
throw new SecurityException |
|
444 |
("expected DES, got " + skey.getAlgorithm()); |
|
445 |
} |
|
446 |
||
447 |
// getKey good ske 2 |
|
448 |
skey = (SecretKey)ks.getKey("sk2", null); |
|
449 |
if ("DESede".equals(skey.getAlgorithm())) { |
|
450 |
System.out.println("test " + testnum++ + " passed"); |
|
451 |
} else { |
|
452 |
throw new SecurityException |
|
453 |
("expected DESede, got " + skey.getAlgorithm()); |
|
454 |
} |
|
455 |
||
456 |
// aliases |
|
457 |
enu = ks.aliases(); |
|
458 |
count = 0; |
|
459 |
while (enu.hasMoreElements()) { |
|
460 |
count++; |
|
461 |
System.out.println("alias " + |
|
462 |
count + |
|
463 |
" = " + |
|
464 |
(String)enu.nextElement()); |
|
465 |
} |
|
466 |
if (count == 2) { |
|
467 |
System.out.println("test " + testnum++ + " passed"); |
|
468 |
} else { |
|
469 |
throw new SecurityException("expected 2 aliases"); |
|
470 |
} |
|
471 |
||
472 |
// size |
|
473 |
if (ks.size() == 2) { |
|
474 |
System.out.println("test " + testnum++ + " passed"); |
|
475 |
} else { |
|
476 |
throw new SecurityException("expected size 2"); |
|
477 |
} |
|
478 |
||
479 |
// isCertificateEntry sk1 |
|
480 |
if (!ks.isCertificateEntry("sk1")) { |
|
481 |
System.out.println("test " + testnum++ + " passed"); |
|
482 |
} else { |
|
483 |
throw new SecurityException("expected ske"); |
|
484 |
} |
|
485 |
||
486 |
// isKeyEntry sk1 |
|
487 |
if (ks.isKeyEntry("sk1")) { |
|
488 |
System.out.println("test " + testnum++ + " passed"); |
|
489 |
} else { |
|
490 |
throw new SecurityException("expected ske"); |
|
491 |
} |
|
492 |
||
493 |
// entryInstanceOf sk2 |
|
494 |
if (ks.entryInstanceOf("sk2", KeyStore.SecretKeyEntry.class)) { |
|
495 |
System.out.println("test " + testnum++ + " passed"); |
|
496 |
} else { |
|
497 |
throw new SecurityException("expected ske"); |
|
498 |
} |
|
499 |
||
500 |
return testnum; |
|
501 |
} |
|
502 |
||
503 |
private static int setAttribute(int testnum) throws Exception { |
|
504 |
||
505 |
if (ks == null) { |
|
506 |
ks = KeyStore.getInstance(KS_TYPE, provider); |
|
507 |
ks.load(null, tokenPwd); |
|
508 |
} |
|
509 |
||
510 |
if (!ks.containsAlias("pk1")) { |
|
511 |
// set good pke 1 |
|
512 |
ks.setKeyEntry("pk1", pk1, null, chain1); |
|
513 |
System.out.println("test " + testnum++ + " passed"); |
|
514 |
} |
|
515 |
||
516 |
// delete all old aliases except pk1 |
|
517 |
Enumeration enu = ks.aliases(); |
|
518 |
int count = 0; |
|
519 |
while (enu.hasMoreElements()) { |
|
520 |
String next = (String)enu.nextElement(); |
|
521 |
if (!"pk1".equals(next)) { |
|
522 |
ks.deleteEntry(next); |
|
523 |
System.out.println("deleted entry for: " + next); |
|
524 |
} |
|
525 |
} |
|
526 |
||
527 |
KeyStore.PrivateKeyEntry pke = |
|
528 |
(KeyStore.PrivateKeyEntry)ks.getEntry("pk1", null); |
|
529 |
System.out.println("pk1 = " + pke.getPrivateKey()); |
|
530 |
Certificate[] chain = pke.getCertificateChain(); |
|
531 |
if (chain.length != chain1.length) { |
|
532 |
throw new SecurityException("received chain not correct length"); |
|
533 |
} |
|
534 |
for (int i = 0; i < chain.length; i++) { |
|
535 |
if (!chain[i].equals(chain1[i])) { |
|
536 |
throw new SecurityException("received chain not equal"); |
|
537 |
} |
|
538 |
} |
|
539 |
System.out.println("test " + testnum++ + " passed"); |
|
540 |
||
541 |
/** |
|
542 |
* test change alias only |
|
543 |
*/ |
|
544 |
||
545 |
// test C_SetAttribute |
|
546 |
PrivateKey pkey = pke.getPrivateKey(); |
|
547 |
ks.setEntry("pk1SA", |
|
548 |
new KeyStore.PrivateKeyEntry(pkey, chain1), |
|
549 |
null); |
|
550 |
System.out.println("test " + testnum++ + " passed"); |
|
551 |
||
552 |
// aliases |
|
553 |
enu = ks.aliases(); |
|
554 |
count = 0; |
|
555 |
String newAlias = null; |
|
556 |
while (enu.hasMoreElements()) { |
|
557 |
count++; |
|
558 |
newAlias = (String)enu.nextElement(); |
|
559 |
System.out.println("alias " + |
|
560 |
count + |
|
561 |
" = " + |
|
562 |
newAlias); |
|
563 |
} |
|
564 |
if (count == 1 && "pk1SA".equals(newAlias)) { |
|
565 |
System.out.println("test " + testnum++ + " passed"); |
|
566 |
} else { |
|
567 |
throw new SecurityException("expected 1 alias"); |
|
568 |
} |
|
569 |
||
570 |
// size |
|
571 |
if (ks.size() == 1) { |
|
572 |
System.out.println("test " + testnum++ + " passed"); |
|
573 |
} else { |
|
574 |
throw new SecurityException("expected size 1"); |
|
575 |
} |
|
576 |
||
577 |
pke = (KeyStore.PrivateKeyEntry)ks.getEntry("pk1", null); |
|
578 |
if (pke != null) { |
|
579 |
throw new SecurityException("expected not to find pk1"); |
|
580 |
} |
|
581 |
System.out.println("test " + testnum++ + " passed"); |
|
582 |
||
583 |
pke = (KeyStore.PrivateKeyEntry)ks.getEntry("pk1SA", null); |
|
584 |
System.out.println("pk1SA = " + pke.getPrivateKey()); |
|
585 |
chain = pke.getCertificateChain(); |
|
586 |
if (chain.length != chain1.length) { |
|
587 |
throw new SecurityException("received chain not correct length"); |
|
588 |
} |
|
589 |
for (int i = 0; i < chain.length; i++) { |
|
590 |
if (!chain[i].equals(chain1[i])) { |
|
591 |
throw new SecurityException("received chain not equal"); |
|
592 |
} |
|
593 |
} |
|
594 |
System.out.println("test " + testnum++ + " passed"); |
|
595 |
||
596 |
/** |
|
597 |
* test change cert chain |
|
598 |
*/ |
|
599 |
||
600 |
pkey = pke.getPrivateKey(); |
|
601 |
ks.setEntry("pk1SA-2", |
|
602 |
new KeyStore.PrivateKeyEntry(pkey, chain4), |
|
603 |
null); |
|
604 |
System.out.println("test " + testnum++ + " passed"); |
|
605 |
||
606 |
// aliases |
|
607 |
enu = ks.aliases(); |
|
608 |
count = 0; |
|
609 |
newAlias = null; |
|
610 |
while (enu.hasMoreElements()) { |
|
611 |
count++; |
|
612 |
newAlias = (String)enu.nextElement(); |
|
613 |
System.out.println("alias " + |
|
614 |
count + |
|
615 |
" = " + |
|
616 |
newAlias); |
|
617 |
} |
|
618 |
if (count == 1 && "pk1SA-2".equals(newAlias)) { |
|
619 |
System.out.println("test " + testnum++ + " passed"); |
|
620 |
} else { |
|
621 |
throw new SecurityException("expected 1 alias"); |
|
622 |
} |
|
623 |
||
624 |
// size |
|
625 |
if (ks.size() == 1) { |
|
626 |
System.out.println("test " + testnum++ + " passed"); |
|
627 |
} else { |
|
628 |
throw new SecurityException("expected size 1"); |
|
629 |
} |
|
630 |
||
631 |
pke = (KeyStore.PrivateKeyEntry)ks.getEntry("pk1SA", null); |
|
632 |
if (pke != null) { |
|
633 |
throw new SecurityException("expected not to find pk1SA"); |
|
634 |
} |
|
635 |
System.out.println("test " + testnum++ + " passed"); |
|
636 |
||
637 |
pke = (KeyStore.PrivateKeyEntry)ks.getEntry("pk1SA-2", null); |
|
638 |
System.out.println("pk1SA-2 = " + pke.getPrivateKey()); |
|
639 |
chain = pke.getCertificateChain(); |
|
640 |
if (chain.length != chain4.length) { |
|
641 |
throw new SecurityException("received chain not correct length"); |
|
642 |
} |
|
643 |
for (int i = 0; i < chain.length; i++) { |
|
644 |
if (!chain[i].equals(chain4[i])) { |
|
645 |
throw new SecurityException("received chain not equal"); |
|
646 |
} |
|
647 |
} |
|
648 |
System.out.println("test " + testnum++ + " passed"); |
|
649 |
||
650 |
return testnum; |
|
651 |
} |
|
652 |
||
653 |
private static int pkey(int testnum) throws Exception { |
|
654 |
||
655 |
if (ks == null) { |
|
656 |
ks = KeyStore.getInstance(KS_TYPE, provider); |
|
657 |
ks.load(null, tokenPwd); |
|
658 |
System.out.println("test " + testnum++ + " passed"); |
|
659 |
} |
|
660 |
||
661 |
// check instance |
|
662 |
if (ks.getProvider() instanceof java.security.AuthProvider) { |
|
663 |
System.out.println("keystore provider instance of AuthProvider"); |
|
664 |
System.out.println("test " + testnum++ + " passed"); |
|
665 |
} else { |
|
666 |
throw new SecurityException("did not get AuthProvider KeyStore"); |
|
667 |
} |
|
668 |
||
669 |
// delete all old aliases |
|
670 |
Enumeration enu = ks.aliases(); |
|
671 |
int count = 0; |
|
672 |
while (enu.hasMoreElements()) { |
|
673 |
String next = (String)enu.nextElement(); |
|
674 |
ks.deleteEntry(next); |
|
675 |
System.out.println("deleted entry for: " + next); |
|
676 |
} |
|
677 |
||
678 |
// set good pke 1 |
|
679 |
ks.setKeyEntry("pk1", pk1, null, chain1); |
|
680 |
System.out.println("test " + testnum++ + " passed"); |
|
681 |
||
682 |
// set good pke 2 |
|
683 |
ks.setEntry("pk2", |
|
684 |
new KeyStore.PrivateKeyEntry(pk2, chain2), |
|
685 |
null); |
|
686 |
System.out.println("test " + testnum++ + " passed"); |
|
687 |
||
688 |
// getEntry good pke 1 |
|
689 |
KeyStore.PrivateKeyEntry pke = |
|
690 |
(KeyStore.PrivateKeyEntry)ks.getEntry("pk1", null); |
|
691 |
System.out.println("pk1 = " + pke.getPrivateKey()); |
|
692 |
Certificate[] chain = pke.getCertificateChain(); |
|
693 |
if (chain.length != chain1.length) { |
|
694 |
throw new SecurityException("received chain not correct length"); |
|
695 |
} |
|
696 |
for (int i = 0; i < chain.length; i++) { |
|
697 |
if (!chain[i].equals(chain1[i])) { |
|
698 |
throw new SecurityException("received chain not equal"); |
|
699 |
} |
|
700 |
} |
|
701 |
||
702 |
// getKey good pke 1 |
|
703 |
PrivateKey pkey = (PrivateKey)ks.getKey("pk1", null); |
|
704 |
System.out.println("pk1 = " + pkey); |
|
705 |
if ("RSA".equals(pkey.getAlgorithm())) { |
|
706 |
System.out.println("test " + testnum++ + " passed"); |
|
707 |
} else { |
|
708 |
throw new SecurityException |
|
709 |
("expected RSA, got " + pkey.getAlgorithm()); |
|
710 |
} |
|
711 |
||
712 |
// getCertificate chain chain 1 |
|
713 |
chain = ks.getCertificateChain("pk1"); |
|
714 |
if (chain.length != chain1.length) { |
|
715 |
throw new SecurityException("received chain not correct length"); |
|
716 |
} |
|
717 |
for (int i = 0; i < chain.length; i++) { |
|
718 |
if (!chain[i].equals(chain1[i])) { |
|
719 |
throw new SecurityException("received chain not equal"); |
|
720 |
} |
|
721 |
} |
|
722 |
System.out.println("test " + testnum++ + " passed"); |
|
723 |
||
724 |
// getEntry good pke 2 |
|
725 |
pke = (KeyStore.PrivateKeyEntry)ks.getEntry("pk2", null); |
|
726 |
if ("RSA".equals(pke.getPrivateKey().getAlgorithm())) { |
|
727 |
System.out.println("test " + testnum++ + " passed"); |
|
728 |
} else { |
|
729 |
throw new SecurityException |
|
730 |
("expected RSA, got " + pke.getPrivateKey().getAlgorithm()); |
|
731 |
} |
|
732 |
System.out.println("pk2 = " + pke.getPrivateKey()); |
|
733 |
chain = pke.getCertificateChain(); |
|
734 |
if (chain.length != chain2.length) { |
|
735 |
throw new SecurityException("received chain not correct length"); |
|
736 |
} |
|
737 |
for (int i = 0; i < chain.length; i++) { |
|
738 |
if (!chain[i].equals(chain2[i])) { |
|
739 |
throw new SecurityException("received chain not equal"); |
|
740 |
} |
|
741 |
} |
|
742 |
||
743 |
// getKey good pke 2 |
|
744 |
pkey = (PrivateKey)ks.getKey("pk2", null); |
|
745 |
if ("RSA".equals(pkey.getAlgorithm())) { |
|
746 |
System.out.println("test " + testnum++ + " passed"); |
|
747 |
} else { |
|
748 |
throw new SecurityException |
|
749 |
("expected RSA, got " + pkey.getAlgorithm()); |
|
750 |
} |
|
751 |
||
752 |
// getCertificate chain chain 2 |
|
753 |
chain = ks.getCertificateChain("pk2"); |
|
754 |
if (chain.length != chain2.length) { |
|
755 |
throw new SecurityException("received chain not correct length"); |
|
756 |
} |
|
757 |
for (int i = 0; i < chain.length; i++) { |
|
758 |
if (!chain[i].equals(chain2[i])) { |
|
759 |
throw new SecurityException("received chain not equal"); |
|
760 |
} |
|
761 |
} |
|
762 |
System.out.println("test " + testnum++ + " passed"); |
|
763 |
||
764 |
// aliases |
|
765 |
enu = ks.aliases(); |
|
766 |
count = 0; |
|
767 |
while (enu.hasMoreElements()) { |
|
768 |
count++; |
|
769 |
System.out.println("alias " + |
|
770 |
count + |
|
771 |
" = " + |
|
772 |
(String)enu.nextElement()); |
|
773 |
} |
|
774 |
if (count == 2) { |
|
775 |
System.out.println("test " + testnum++ + " passed"); |
|
776 |
} else { |
|
777 |
throw new SecurityException("expected 2 aliases"); |
|
778 |
} |
|
779 |
||
780 |
// size |
|
781 |
if (ks.size() == 2) { |
|
782 |
System.out.println("test " + testnum++ + " passed"); |
|
783 |
} else { |
|
784 |
throw new SecurityException("expected size 2"); |
|
785 |
} |
|
786 |
||
787 |
// getCertificate |
|
788 |
if (ks.getCertificate("pk1").equals(chain1[0])) { |
|
789 |
System.out.println("test " + testnum++ + " passed"); |
|
790 |
} else { |
|
791 |
throw new SecurityException("expected certificate pk1 end entity"); |
|
792 |
} |
|
793 |
||
794 |
// containsAlias |
|
795 |
if (ks.containsAlias("pk1") && ks.containsAlias("pk2") && |
|
796 |
!ks.containsAlias("foobar") && |
|
797 |
!ks.containsAlias("pk1.2") && !ks.containsAlias("pk2.2")) { |
|
798 |
System.out.println("test " + testnum++ + " passed"); |
|
799 |
} else { |
|
800 |
throw new SecurityException("unexpected aliases encountered"); |
|
801 |
} |
|
802 |
||
803 |
// isKeyEntry |
|
804 |
if (ks.isKeyEntry("pk1") && ks.isKeyEntry("pk2") && |
|
805 |
!ks.isKeyEntry("foobar")) { |
|
806 |
System.out.println("test " + testnum++ + " passed"); |
|
807 |
} else { |
|
808 |
throw new SecurityException("isKeyEntry failed"); |
|
809 |
} |
|
810 |
||
811 |
// isCertificateEntry |
|
812 |
if (!ks.isCertificateEntry("foobar") && |
|
813 |
!ks.isCertificateEntry("pk1") && !ks.isCertificateEntry("pk2")) { |
|
814 |
System.out.println("test " + testnum++ + " passed"); |
|
815 |
} else { |
|
816 |
throw new SecurityException("isCertificateEntry failed"); |
|
817 |
} |
|
818 |
||
819 |
// getCertificateAlias |
|
820 |
if (ks.getCertificateAlias(chain1[0]).equals("pk1") && |
|
821 |
ks.getCertificateAlias(chain2[0]).equals("pk2") && |
|
822 |
ks.getCertificateAlias(randomCert) == null) { |
|
823 |
System.out.println("test " + testnum++ + " passed"); |
|
824 |
} else { |
|
825 |
throw new SecurityException("getCertificateAlias failed"); |
|
826 |
} |
|
827 |
||
828 |
if (ks.entryInstanceOf("pk1", KeyStore.PrivateKeyEntry.class) && |
|
829 |
ks.entryInstanceOf("pk2", KeyStore.PrivateKeyEntry.class) && |
|
830 |
!ks.entryInstanceOf("pk1", KeyStore.TrustedCertificateEntry.class) && |
|
831 |
!ks.entryInstanceOf("pk2", KeyStore.TrustedCertificateEntry.class) && |
|
832 |
!ks.entryInstanceOf("foobar", KeyStore.TrustedCertificateEntry.class) && |
|
833 |
!ks.entryInstanceOf("foobar", KeyStore.PrivateKeyEntry.class)) { |
|
834 |
System.out.println("test " + testnum++ + " passed"); |
|
835 |
} else { |
|
836 |
throw new SecurityException("entryInstanceOf failed"); |
|
837 |
} |
|
838 |
||
839 |
ks.deleteEntry("pk2"); |
|
840 |
if (ks.containsAlias("pk1") && !ks.containsAlias("pk2")) { |
|
841 |
System.out.println("test " + testnum++ + " passed"); |
|
842 |
} else { |
|
843 |
throw new SecurityException("deleteEntry failed"); |
|
844 |
} |
|
845 |
||
846 |
// getEntry good pke 1 |
|
847 |
pke = (KeyStore.PrivateKeyEntry)ks.getEntry("pk1", null); |
|
848 |
System.out.println("pk1 = " + pke.getPrivateKey()); |
|
849 |
chain = pke.getCertificateChain(); |
|
850 |
if (chain.length != chain1.length) { |
|
851 |
throw new SecurityException("received chain not correct length"); |
|
852 |
} |
|
853 |
for (int i = 0; i < chain.length; i++) { |
|
854 |
if (!chain[i].equals(chain1[i])) { |
|
855 |
throw new SecurityException("received chain not equal"); |
|
856 |
} |
|
857 |
} |
|
858 |
System.out.println("test " + testnum++ + " passed"); |
|
859 |
||
860 |
// aliases |
|
861 |
enu = ks.aliases(); |
|
862 |
count = 0; |
|
863 |
while (enu.hasMoreElements()) { |
|
864 |
count++; |
|
865 |
System.out.println("alias " + |
|
866 |
count + |
|
867 |
" = " + |
|
868 |
(String)enu.nextElement()); |
|
869 |
} |
|
870 |
if (count == 1) { |
|
871 |
System.out.println("test " + testnum++ + " passed"); |
|
872 |
} else { |
|
873 |
throw new SecurityException("expected 1 alias"); |
|
874 |
} |
|
875 |
||
876 |
// size |
|
877 |
if (ks.size() == 1) { |
|
878 |
System.out.println("test " + testnum++ + " passed"); |
|
879 |
} else { |
|
880 |
throw new SecurityException("expected size 1"); |
|
881 |
} |
|
882 |
||
883 |
return testnum; |
|
884 |
} |
|
885 |
||
886 |
private static int extended(int testnum) throws Exception { |
|
887 |
||
888 |
// setEntry unknown entry type |
|
889 |
try { |
|
890 |
ks.setEntry("foo", new FooEntry(), null); |
|
891 |
throw new SecurityException("setEntry should have failed"); |
|
892 |
} catch (KeyStoreException kse) { |
|
893 |
System.out.println("test " + testnum++ + " passed"); |
|
894 |
} |
|
895 |
||
896 |
// getEntry random foo |
|
897 |
if (ks.getEntry("foo", null) != null) { |
|
898 |
throw new SecurityException("expected null entry"); |
|
899 |
} else { |
|
900 |
System.out.println("test " + testnum++ + " passed"); |
|
901 |
} |
|
902 |
||
903 |
// set good ske 1 |
|
904 |
ks.setKeyEntry("sk1", sk1, null, null); |
|
905 |
System.out.println("test " + testnum++ + " passed"); |
|
906 |
||
907 |
// set good ske 2 |
|
908 |
ks.setKeyEntry("sk2", sk2, null, null); |
|
909 |
System.out.println("test " + testnum++ + " passed"); |
|
910 |
||
911 |
// set good ske 3 |
|
912 |
ks.setEntry("sk3", |
|
913 |
new KeyStore.SecretKeyEntry(sk3), |
|
914 |
null); |
|
915 |
System.out.println("test " + testnum++ + " passed"); |
|
916 |
||
917 |
// set good ske 4 |
|
918 |
ks.setEntry("sk4", |
|
919 |
new KeyStore.SecretKeyEntry(sk4), |
|
920 |
null); |
|
921 |
System.out.println("test " + testnum++ + " passed"); |
|
922 |
||
923 |
// getEntry good ske 1 |
|
924 |
KeyStore.SecretKeyEntry ske = |
|
925 |
(KeyStore.SecretKeyEntry)ks.getEntry("sk1", null); |
|
926 |
if ("DES".equals(ske.getSecretKey().getAlgorithm())) { |
|
927 |
System.out.println("test " + testnum++ + " passed"); |
|
928 |
} else { |
|
929 |
throw new SecurityException |
|
930 |
("expected DES, got " + ske.getSecretKey().getAlgorithm()); |
|
931 |
} |
|
932 |
||
933 |
// getEntry good ske 2 |
|
934 |
ske = (KeyStore.SecretKeyEntry)ks.getEntry("sk2", null); |
|
935 |
if ("DESede".equals(ske.getSecretKey().getAlgorithm())) { |
|
936 |
System.out.println("test " + testnum++ + " passed"); |
|
937 |
} else { |
|
938 |
throw new SecurityException |
|
939 |
("expected DESede, got " + ske.getSecretKey().getAlgorithm()); |
|
940 |
} |
|
941 |
||
942 |
// getEntry good ske 3 |
|
943 |
ske = (KeyStore.SecretKeyEntry)ks.getEntry("sk3", null); |
|
944 |
if ("AES".equals(ske.getSecretKey().getAlgorithm())) { |
|
945 |
System.out.println("test " + testnum++ + " passed"); |
|
946 |
} else { |
|
947 |
throw new SecurityException |
|
948 |
("expected AES, got " + ske.getSecretKey().getAlgorithm()); |
|
949 |
} |
|
950 |
||
951 |
// getEntry good ske 4 |
|
952 |
ske = (KeyStore.SecretKeyEntry)ks.getEntry("sk4", null); |
|
953 |
if ("ARCFOUR".equals(ske.getSecretKey().getAlgorithm())) { |
|
954 |
System.out.println("test " + testnum++ + " passed"); |
|
955 |
} else { |
|
956 |
throw new SecurityException |
|
957 |
("expected ARCFOUR, got " + ske.getSecretKey().getAlgorithm()); |
|
958 |
} |
|
959 |
||
960 |
// getKey good ske 1 |
|
961 |
SecretKey skey = (SecretKey)ks.getKey("sk1", null); |
|
962 |
if ("DES".equals(skey.getAlgorithm())) { |
|
963 |
System.out.println("test " + testnum++ + " passed"); |
|
964 |
} else { |
|
965 |
throw new SecurityException |
|
966 |
("expected DES, got " + skey.getAlgorithm()); |
|
967 |
} |
|
968 |
||
969 |
// getKey good ske 2 |
|
970 |
skey = (SecretKey)ks.getKey("sk2", null); |
|
971 |
if ("DESede".equals(skey.getAlgorithm())) { |
|
972 |
System.out.println("test " + testnum++ + " passed"); |
|
973 |
} else { |
|
974 |
throw new SecurityException |
|
975 |
("expected DESede, got " + skey.getAlgorithm()); |
|
976 |
} |
|
977 |
||
978 |
// getKey good ske 3 |
|
979 |
skey = (SecretKey)ks.getKey("sk3", null); |
|
980 |
if ("AES".equals(skey.getAlgorithm())) { |
|
981 |
System.out.println("test " + testnum++ + " passed"); |
|
982 |
} else { |
|
983 |
throw new SecurityException |
|
984 |
("expected AES, got " + skey.getAlgorithm()); |
|
985 |
} |
|
986 |
||
987 |
// getKey good ske 4 |
|
988 |
skey = (SecretKey)ks.getKey("sk4", null); |
|
989 |
if ("ARCFOUR".equals(skey.getAlgorithm())) { |
|
990 |
System.out.println("test " + testnum++ + " passed"); |
|
991 |
} else { |
|
992 |
throw new SecurityException |
|
993 |
("expected ARCFOUR, got " + skey.getAlgorithm()); |
|
994 |
} |
|
995 |
||
996 |
// aliases |
|
997 |
Enumeration enu = ks.aliases(); |
|
998 |
int count = 0; |
|
999 |
while (enu.hasMoreElements()) { |
|
1000 |
count++; |
|
1001 |
System.out.println("alias " + |
|
1002 |
count + |
|
1003 |
" = " + |
|
1004 |
(String)enu.nextElement()); |
|
1005 |
} |
|
1006 |
if (count == 5) { |
|
1007 |
System.out.println("test " + testnum++ + " passed"); |
|
1008 |
} else { |
|
1009 |
throw new SecurityException("expected 5 aliases"); |
|
1010 |
} |
|
1011 |
||
1012 |
// size |
|
1013 |
if (ks.size() == 5) { |
|
1014 |
System.out.println("test " + testnum++ + " passed"); |
|
1015 |
} else { |
|
1016 |
throw new SecurityException("expected size 5"); |
|
1017 |
} |
|
1018 |
||
1019 |
// set good pke 2 |
|
1020 |
ks.setEntry("pk2", |
|
1021 |
new KeyStore.PrivateKeyEntry(pk2, chain2), |
|
1022 |
null); |
|
1023 |
System.out.println("test " + testnum++ + " passed"); |
|
1024 |
||
1025 |
// set good pke 3 |
|
1026 |
ks.setEntry("pk3", |
|
1027 |
new KeyStore.PrivateKeyEntry(pk3, chain3), |
|
1028 |
null); |
|
1029 |
System.out.println("test " + testnum++ + " passed"); |
|
1030 |
||
1031 |
// getEntry good pke 1 |
|
1032 |
KeyStore.PrivateKeyEntry pke = |
|
1033 |
(KeyStore.PrivateKeyEntry)ks.getEntry("pk1", null); |
|
1034 |
System.out.println("pk1 = " + pke.getPrivateKey()); |
|
1035 |
Certificate[] chain = pke.getCertificateChain(); |
|
1036 |
if (chain.length != chain1.length) { |
|
1037 |
throw new SecurityException("received chain not correct length"); |
|
1038 |
} |
|
1039 |
for (int i = 0; i < chain.length; i++) { |
|
1040 |
if (!chain[i].equals(chain1[i])) { |
|
1041 |
throw new SecurityException("received chain not equal"); |
|
1042 |
} |
|
1043 |
} |
|
1044 |
||
1045 |
// getEntry good pke 2 |
|
1046 |
pke = (KeyStore.PrivateKeyEntry)ks.getEntry("pk2", null); |
|
1047 |
System.out.println("pk2 = " + pke.getPrivateKey()); |
|
1048 |
chain = pke.getCertificateChain(); |
|
1049 |
if (chain.length != chain2.length) { |
|
1050 |
throw new SecurityException("received chain not correct length"); |
|
1051 |
} |
|
1052 |
for (int i = 0; i < chain.length; i++) { |
|
1053 |
if (!chain[i].equals(chain2[i])) { |
|
1054 |
throw new SecurityException("received chain not equal"); |
|
1055 |
} |
|
1056 |
} |
|
1057 |
||
1058 |
// getEntry good pke 3 |
|
1059 |
pke = (KeyStore.PrivateKeyEntry)ks.getEntry("pk3", null); |
|
1060 |
System.out.println("pk3 = " + pke.getPrivateKey()); |
|
1061 |
chain = pke.getCertificateChain(); |
|
1062 |
if (chain.length != chain3.length) { |
|
1063 |
throw new SecurityException("received chain not correct length"); |
|
1064 |
} |
|
1065 |
for (int i = 0; i < chain.length; i++) { |
|
1066 |
if (!chain[i].equals(chain3[i])) { |
|
1067 |
throw new SecurityException("received chain not equal"); |
|
1068 |
} |
|
1069 |
} |
|
1070 |
||
1071 |
// getKey good pke 1 |
|
1072 |
PrivateKey pkey = (PrivateKey)ks.getKey("pk1", null); |
|
1073 |
if ("RSA".equals(pkey.getAlgorithm())) { |
|
1074 |
System.out.println("test " + testnum++ + " passed"); |
|
1075 |
} else { |
|
1076 |
throw new SecurityException |
|
1077 |
("expected RSA, got " + pkey.getAlgorithm()); |
|
1078 |
} |
|
1079 |
||
1080 |
// getCertificate chain chain 1 |
|
1081 |
chain = ks.getCertificateChain("pk1"); |
|
1082 |
if (chain.length != chain1.length) { |
|
1083 |
throw new SecurityException("received chain not correct length"); |
|
1084 |
} |
|
1085 |
for (int i = 0; i < chain.length; i++) { |
|
1086 |
if (!chain[i].equals(chain1[i])) { |
|
1087 |
throw new SecurityException("received chain not equal"); |
|
1088 |
} |
|
1089 |
} |
|
1090 |
||
1091 |
// getKey good pke 2 |
|
1092 |
pkey = (PrivateKey)ks.getKey("pk2", null); |
|
1093 |
if ("RSA".equals(pkey.getAlgorithm())) { |
|
1094 |
System.out.println("test " + testnum++ + " passed"); |
|
1095 |
} else { |
|
1096 |
throw new SecurityException |
|
1097 |
("expected RSA, got " + pkey.getAlgorithm()); |
|
1098 |
} |
|
1099 |
||
1100 |
// getCertificate chain chain 2 |
|
1101 |
chain = ks.getCertificateChain("pk2"); |
|
1102 |
if (chain.length != chain2.length) { |
|
1103 |
throw new SecurityException("received chain not correct length"); |
|
1104 |
} |
|
1105 |
for (int i = 0; i < chain.length; i++) { |
|
1106 |
if (!chain[i].equals(chain2[i])) { |
|
1107 |
throw new SecurityException("received chain not equal"); |
|
1108 |
} |
|
1109 |
} |
|
1110 |
||
1111 |
// getKey good pke 3 |
|
1112 |
pkey = (PrivateKey)ks.getKey("pk3", null); |
|
1113 |
if ("RSA".equals(pkey.getAlgorithm())) { |
|
1114 |
System.out.println("test " + testnum++ + " passed"); |
|
1115 |
} else { |
|
1116 |
throw new SecurityException |
|
1117 |
("expected RSA, got " + pkey.getAlgorithm()); |
|
1118 |
} |
|
1119 |
||
1120 |
// getCertificate chain chain 3 |
|
1121 |
chain = ks.getCertificateChain("pk3"); |
|
1122 |
if (chain.length != chain3.length) { |
|
1123 |
throw new SecurityException("received chain not correct length"); |
|
1124 |
} |
|
1125 |
for (int i = 0; i < chain.length; i++) { |
|
1126 |
if (!chain[i].equals(chain3[i])) { |
|
1127 |
throw new SecurityException("received chain not equal"); |
|
1128 |
} |
|
1129 |
} |
|
1130 |
||
1131 |
// aliases |
|
1132 |
enu = ks.aliases(); |
|
1133 |
count = 0; |
|
1134 |
while (enu.hasMoreElements()) { |
|
1135 |
count++; |
|
1136 |
System.out.println("alias " + |
|
1137 |
count + |
|
1138 |
" = " + |
|
1139 |
(String)enu.nextElement()); |
|
1140 |
} |
|
1141 |
if (count == 7) { |
|
1142 |
System.out.println("test " + testnum++ + " passed"); |
|
1143 |
} else { |
|
1144 |
throw new SecurityException("expected 7 aliases"); |
|
1145 |
} |
|
1146 |
||
1147 |
// size |
|
1148 |
if (ks.size() == 7) { |
|
1149 |
System.out.println("test " + testnum++ + " passed"); |
|
1150 |
} else { |
|
1151 |
throw new SecurityException("expected size 7"); |
|
1152 |
} |
|
1153 |
||
1154 |
// getCertificate good chain 1 |
|
1155 |
if (ks.getCertificate("pk1").equals(chain1[0])) { |
|
1156 |
System.out.println("test " + testnum++ + " passed"); |
|
1157 |
} else { |
|
1158 |
throw new SecurityException("retrieved cert not equal"); |
|
1159 |
} |
|
1160 |
||
1161 |
// getCertificate good chain 3 |
|
1162 |
if (ks.getCertificate("pk3").equals(chain3[0])) { |
|
1163 |
System.out.println("test " + testnum++ + " passed"); |
|
1164 |
} else { |
|
1165 |
throw new SecurityException("retrieved cert not equal"); |
|
1166 |
} |
|
1167 |
||
1168 |
// getKey good ske 1 |
|
1169 |
skey = (SecretKey)ks.getKey("sk1", null); |
|
1170 |
if ("DES".equals(skey.getAlgorithm())) { |
|
1171 |
System.out.println("test " + testnum++ + " passed"); |
|
1172 |
} else { |
|
1173 |
throw new SecurityException |
|
1174 |
("expected DES, got " + skey.getAlgorithm()); |
|
1175 |
} |
|
1176 |
||
1177 |
// getKey good ske 4 |
|
1178 |
skey = (SecretKey)ks.getKey("sk4", null); |
|
1179 |
if ("ARCFOUR".equals(skey.getAlgorithm())) { |
|
1180 |
System.out.println("test " + testnum++ + " passed"); |
|
1181 |
} else { |
|
1182 |
throw new SecurityException |
|
1183 |
("expected ARCFOUR, got " + skey.getAlgorithm()); |
|
1184 |
} |
|
1185 |
||
1186 |
// getKey good pke 1 |
|
1187 |
pkey = (PrivateKey)ks.getKey("pk1", null); |
|
1188 |
if ("RSA".equals(pkey.getAlgorithm())) { |
|
1189 |
System.out.println("test " + testnum++ + " passed"); |
|
1190 |
} else { |
|
1191 |
throw new SecurityException |
|
1192 |
("expected RSA, got " + pkey.getAlgorithm()); |
|
1193 |
} |
|
1194 |
||
1195 |
// getKey good pke 3 |
|
1196 |
pkey = (PrivateKey)ks.getKey("pk3", null); |
|
1197 |
if ("RSA".equals(pkey.getAlgorithm())) { |
|
1198 |
System.out.println("test " + testnum++ + " passed"); |
|
1199 |
} else { |
|
1200 |
throw new SecurityException |
|
1201 |
("expected RSA, got " + pkey.getAlgorithm()); |
|
1202 |
} |
|
1203 |
||
1204 |
// contains alias |
|
1205 |
if (!ks.containsAlias("pk1") || |
|
1206 |
!ks.containsAlias("pk2") || |
|
1207 |
!ks.containsAlias("pk3") || |
|
1208 |
!ks.containsAlias("sk1") || |
|
1209 |
!ks.containsAlias("sk2") || |
|
1210 |
!ks.containsAlias("sk3") || |
|
1211 |
!ks.containsAlias("sk4")) { |
|
1212 |
throw new SecurityException("did not contain all aliases"); |
|
1213 |
} |
|
1214 |
System.out.println("test " + testnum++ + " passed"); |
|
1215 |
||
1216 |
// getCertificateAlias pk1 |
|
1217 |
if (ks.getCertificateAlias(chain1[0]).equals("pk1")) { |
|
1218 |
System.out.println("test " + testnum++ + " passed"); |
|
1219 |
} else { |
|
1220 |
throw new SecurityException("expected cert pk1"); |
|
1221 |
} |
|
1222 |
||
1223 |
// getCertificateAlias pk3 |
|
1224 |
if (ks.getCertificateAlias(chain3[0]).equals("pk3")) { |
|
1225 |
System.out.println("test " + testnum++ + " passed"); |
|
1226 |
} else { |
|
1227 |
throw new SecurityException("expected cert pk3"); |
|
1228 |
} |
|
1229 |
||
1230 |
// isCertificateEntry pk1 |
|
1231 |
if (!ks.isCertificateEntry("pk1")) { |
|
1232 |
System.out.println("test " + testnum++ + " passed"); |
|
1233 |
} else { |
|
1234 |
throw new SecurityException("expected pke"); |
|
1235 |
} |
|
1236 |
||
1237 |
// isCertificateEntry pk3 |
|
1238 |
if (!ks.isCertificateEntry("pk3")) { |
|
1239 |
System.out.println("test " + testnum++ + " passed"); |
|
1240 |
} else { |
|
1241 |
throw new SecurityException("expected pke"); |
|
1242 |
} |
|
1243 |
||
1244 |
// isCertificateEntry sk1 |
|
1245 |
if (!ks.isCertificateEntry("sk1")) { |
|
1246 |
System.out.println("test " + testnum++ + " passed"); |
|
1247 |
} else { |
|
1248 |
throw new SecurityException("expected ske"); |
|
1249 |
} |
|
1250 |
||
1251 |
// isCertificateEntry sk4 |
|
1252 |
if (!ks.isCertificateEntry("sk4")) { |
|
1253 |
System.out.println("test " + testnum++ + " passed"); |
|
1254 |
} else { |
|
1255 |
throw new SecurityException("expected ske"); |
|
1256 |
} |
|
1257 |
||
1258 |
// isKeyEntry pk1 |
|
1259 |
if (ks.isKeyEntry("pk1")) { |
|
1260 |
System.out.println("test " + testnum++ + " passed"); |
|
1261 |
} else { |
|
1262 |
throw new SecurityException("expected pke"); |
|
1263 |
} |
|
1264 |
||
1265 |
// isKeyEntry pk3 |
|
1266 |
if (ks.isKeyEntry("pk3")) { |
|
1267 |
System.out.println("test " + testnum++ + " passed"); |
|
1268 |
} else { |
|
1269 |
throw new SecurityException("expected pke"); |
|
1270 |
} |
|
1271 |
||
1272 |
// isKeyEntry sk1 |
|
1273 |
if (ks.isKeyEntry("sk1")) { |
|
1274 |
System.out.println("test " + testnum++ + " passed"); |
|
1275 |
} else { |
|
1276 |
throw new SecurityException("expected ske"); |
|
1277 |
} |
|
1278 |
||
1279 |
// isKeyEntry sk4 |
|
1280 |
if (ks.isKeyEntry("sk4")) { |
|
1281 |
System.out.println("test " + testnum++ + " passed"); |
|
1282 |
} else { |
|
1283 |
throw new SecurityException("expected ske"); |
|
1284 |
} |
|
1285 |
||
1286 |
// isCertificateEntry random foo |
|
1287 |
if (!ks.isCertificateEntry("foo")) { |
|
1288 |
System.out.println("test " + testnum++ + " passed"); |
|
1289 |
} else { |
|
1290 |
throw new SecurityException("expected foo"); |
|
1291 |
} |
|
1292 |
||
1293 |
// isKeyEntry random foo |
|
1294 |
if (!ks.isKeyEntry("foo")) { |
|
1295 |
System.out.println("test " + testnum++ + " passed"); |
|
1296 |
} else { |
|
1297 |
throw new SecurityException("expected foo"); |
|
1298 |
} |
|
1299 |
||
1300 |
// entryInstanceOf pk1 |
|
1301 |
if (!ks.entryInstanceOf |
|
1302 |
("pk1", KeyStore.TrustedCertificateEntry.class)) { |
|
1303 |
System.out.println("test " + testnum++ + " passed"); |
|
1304 |
} else { |
|
1305 |
throw new SecurityException("expected tce"); |
|
1306 |
} |
|
1307 |
||
1308 |
// entryInstanceOf pk3 |
|
1309 |
if (!ks.entryInstanceOf |
|
1310 |
("pk3", KeyStore.TrustedCertificateEntry.class)) { |
|
1311 |
System.out.println("test " + testnum++ + " passed"); |
|
1312 |
} else { |
|
1313 |
throw new SecurityException("expected tce"); |
|
1314 |
} |
|
1315 |
||
1316 |
// entryInstanceOf sk1 |
|
1317 |
if (!ks.entryInstanceOf |
|
1318 |
("sk1", KeyStore.TrustedCertificateEntry.class)) { |
|
1319 |
System.out.println("test " + testnum++ + " passed"); |
|
1320 |
} else { |
|
1321 |
throw new SecurityException("expected tce"); |
|
1322 |
} |
|
1323 |
||
1324 |
// entryInstanceOf sk4 |
|
1325 |
if (!ks.entryInstanceOf |
|
1326 |
("sk4", KeyStore.TrustedCertificateEntry.class)) { |
|
1327 |
System.out.println("test " + testnum++ + " passed"); |
|
1328 |
} else { |
|
1329 |
throw new SecurityException("expected tce"); |
|
1330 |
} |
|
1331 |
||
1332 |
// entryInstanceOf pk1 |
|
1333 |
if (ks.entryInstanceOf("pk1", KeyStore.PrivateKeyEntry.class)) { |
|
1334 |
System.out.println("test " + testnum++ + " passed"); |
|
1335 |
} else { |
|
1336 |
throw new SecurityException("expected pke"); |
|
1337 |
} |
|
1338 |
||
1339 |
// entryInstanceOf pk3 |
|
1340 |
if (ks.entryInstanceOf("pk3", KeyStore.PrivateKeyEntry.class)) { |
|
1341 |
System.out.println("test " + testnum++ + " passed"); |
|
1342 |
} else { |
|
1343 |
throw new SecurityException("expected pke"); |
|
1344 |
} |
|
1345 |
||
1346 |
// entryInstanceOf sk1 |
|
1347 |
if (!ks.entryInstanceOf("sk1", KeyStore.PrivateKeyEntry.class)) { |
|
1348 |
System.out.println("test " + testnum++ + " passed"); |
|
1349 |
} else { |
|
1350 |
throw new SecurityException("expected pke"); |
|
1351 |
} |
|
1352 |
||
1353 |
// entryInstanceOf sk4 |
|
1354 |
if (!ks.entryInstanceOf("sk4", KeyStore.PrivateKeyEntry.class)) { |
|
1355 |
System.out.println("test " + testnum++ + " passed"); |
|
1356 |
} else { |
|
1357 |
throw new SecurityException("expected pke"); |
|
1358 |
} |
|
1359 |
||
1360 |
// entryInstanceOf sk1 |
|
1361 |
if (ks.entryInstanceOf("sk1", KeyStore.SecretKeyEntry.class)) { |
|
1362 |
System.out.println("test " + testnum++ + " passed"); |
|
1363 |
} else { |
|
1364 |
throw new SecurityException("expected ske"); |
|
1365 |
} |
|
1366 |
||
1367 |
// entryInstanceOf sk4 |
|
1368 |
if (ks.entryInstanceOf("sk4", KeyStore.SecretKeyEntry.class)) { |
|
1369 |
System.out.println("test " + testnum++ + " passed"); |
|
1370 |
} else { |
|
1371 |
throw new SecurityException("expected ske"); |
|
1372 |
} |
|
1373 |
||
1374 |
// entryInstanceOf pk1 |
|
1375 |
if (!ks.entryInstanceOf("pk1", KeyStore.SecretKeyEntry.class)) { |
|
1376 |
System.out.println("test " + testnum++ + " passed"); |
|
1377 |
} else { |
|
1378 |
throw new SecurityException("expected ske"); |
|
1379 |
} |
|
1380 |
||
1381 |
// entryInstanceOf pk3 |
|
1382 |
if (!ks.entryInstanceOf("pk3", KeyStore.SecretKeyEntry.class)) { |
|
1383 |
System.out.println("test " + testnum++ + " passed"); |
|
1384 |
} else { |
|
1385 |
throw new SecurityException("expected ske"); |
|
1386 |
} |
|
1387 |
||
1388 |
// getEntry random foobar |
|
1389 |
if (ks.getEntry("foobar", null) != null) { |
|
1390 |
throw new SecurityException("expected null entry"); |
|
1391 |
} else { |
|
1392 |
System.out.println("test " + testnum++ + " passed"); |
|
1393 |
} |
|
1394 |
||
1395 |
// deleteEntry |
|
1396 |
ks.deleteEntry("pk1"); |
|
1397 |
ks.deleteEntry("pk3"); |
|
1398 |
ks.deleteEntry("sk2"); |
|
1399 |
ks.deleteEntry("sk3"); |
|
1400 |
System.out.println("test " + testnum++ + " passed"); |
|
1401 |
||
1402 |
// aliases |
|
1403 |
enu = ks.aliases(); |
|
1404 |
count = 0; |
|
1405 |
while (enu.hasMoreElements()) { |
|
1406 |
count++; |
|
1407 |
System.out.println("alias " + |
|
1408 |
count + |
|
1409 |
" = " + |
|
1410 |
(String)enu.nextElement()); |
|
1411 |
} |
|
1412 |
if (count == 3) { |
|
1413 |
System.out.println("test " + testnum++ + " passed"); |
|
1414 |
} else { |
|
1415 |
throw new SecurityException("expected 3 aliases"); |
|
1416 |
} |
|
1417 |
||
1418 |
// size |
|
1419 |
if (ks.size() == 3) { |
|
1420 |
System.out.println("test " + testnum++ + " passed"); |
|
1421 |
} else { |
|
1422 |
throw new SecurityException("expected size 6"); |
|
1423 |
} |
|
1424 |
||
1425 |
// entryInstanceOf sk1 |
|
1426 |
if (!ks.entryInstanceOf("sk1", KeyStore.PrivateKeyEntry.class)) { |
|
1427 |
System.out.println("test " + testnum++ + " passed"); |
|
1428 |
} else { |
|
1429 |
throw new SecurityException("expected pke"); |
|
1430 |
} |
|
1431 |
||
1432 |
// entryInstanceOf sk4 |
|
1433 |
if (!ks.entryInstanceOf("sk4", KeyStore.PrivateKeyEntry.class)) { |
|
1434 |
System.out.println("test " + testnum++ + " passed"); |
|
1435 |
} else { |
|
1436 |
throw new SecurityException("expected pke"); |
|
1437 |
} |
|
1438 |
||
1439 |
// entryInstanceOf pk2 |
|
1440 |
if (ks.entryInstanceOf("pk2", KeyStore.PrivateKeyEntry.class)) { |
|
1441 |
System.out.println("test " + testnum++ + " passed"); |
|
1442 |
} else { |
|
1443 |
throw new SecurityException("expected pke"); |
|
1444 |
} |
|
1445 |
System.out.println("test " + testnum++ + " passed"); |
|
1446 |
||
1447 |
return testnum; |
|
1448 |
} |
|
1449 |
} |