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