Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/security/KeyStore/EntryMethods.java
41149 views
1
/*
2
* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
* @test 1.5, 03/06/24
26
* @bug 4850376 8130850 8130181
27
* @summary Provide generic storage KeyStore storage facilities
28
*/
29
30
import java.security.*;
31
import java.security.cert.*;
32
import java.util.*;
33
import java.io.*;
34
35
public class EntryMethods
36
extends Provider
37
implements KeyStore.Entry
38
{
39
40
private static FileInputStream pre15fis;
41
private static char[] password = {'f', 'o', 'o', 'b', 'a', 'r'};
42
private static char[] badPwd = {'b', 'a', 'd', 'p', 'w', 'd'};
43
44
public static class FooProtect implements KeyStore.ProtectionParameter { }
45
public static class FooParameter implements KeyStore.LoadStoreParameter {
46
public KeyStore.ProtectionParameter getProtectionParameter() {
47
return null;
48
}
49
}
50
51
public static class MyLoadStoreParameter
52
implements KeyStore.LoadStoreParameter {
53
54
private KeyStore.ProtectionParameter protection;
55
56
MyLoadStoreParameter(KeyStore.ProtectionParameter protection) {
57
this.protection = protection;
58
}
59
60
public KeyStore.ProtectionParameter getProtectionParameter() {
61
return protection;
62
}
63
}
64
65
public static class FooEntry implements KeyStore.Entry { }
66
67
public EntryMethods() throws Exception {
68
super("EntryMethods", "0.0", "EntryMethods");
69
70
pre15fis = new FileInputStream
71
(System.getProperty("test.src") + "/EntryMethods.pre15.keystore");
72
73
AccessController.doPrivileged(new PrivilegedAction() {
74
public Object run() {
75
put("KeyStore.Pre15KeyStore", "EntryMethods$Pre15");
76
put("KeyStore.Post15KeyStore", "EntryMethods$Post15");
77
put("KeyStore.UnrecoverableKeyStore",
78
"EntryMethods$UnrecoverableKS");
79
return null;
80
}
81
});
82
}
83
84
public static void main(String[] args) throws Exception {
85
86
EntryMethods entry = new EntryMethods();
87
88
// test pre-JDK1.5 KeyStore throws UnsupportedOperationExceptions
89
// for new methods
90
KeyStore pre15KS = KeyStore.getInstance("Pre15KeyStore", entry);
91
testPre15(pre15KS);
92
93
// test post-JDK1.5 KeyStore does right thing with new methods
94
KeyStore post15KS = KeyStore.getInstance("Post15KeyStore", entry);
95
testPost15(post15KS);
96
97
// test post-JDK1.5 KeyStore can throw new UnrecoverableEntryException
98
KeyStore uKS = KeyStore.getInstance("UnrecoverableKeyStore", entry);
99
testUnrecoverable(uKS);
100
}
101
102
private static void testPre15(KeyStore ks) throws Exception {
103
104
int tNum = 1;
105
KeyStore.Entry e = null;
106
107
// TEST load null param
108
ks.load((KeyStore.LoadStoreParameter)null);
109
System.out.println("[Pre1.5] test " + tNum++ + " passed");
110
111
112
// TEST load random param
113
try {
114
ks.load(new FooParameter());
115
throw new SecurityException("[Pre1.5] test " + tNum + " failed");
116
} catch (UnsupportedOperationException uoe) {
117
System.out.println("[Pre1.5] test " + tNum++ + " passed");
118
} catch (NoSuchAlgorithmException nsae) {
119
System.out.println("[Pre1.5] test " + tNum++ + " passed");
120
}
121
122
123
// TEST load custom param
124
ks.load(new MyLoadStoreParameter(
125
new KeyStore.PasswordProtection(password)));
126
System.out.println("[Pre1.5] test " + tNum++ + " passed");
127
128
129
// TEST store random param
130
ks.load(pre15fis, password);
131
132
// setup for later user
133
KeyStore.Entry pkeNew = ks.getEntry("privkey",
134
new KeyStore.PasswordProtection(password));
135
KeyStore.Entry tceNew = ks.getEntry("trustedcert", null);
136
137
try {
138
ks.store(new FooParameter());
139
throw new SecurityException("[Pre1.5] test " + tNum + " failed");
140
} catch (UnsupportedOperationException uoe) {
141
// good
142
System.out.println("[Pre1.5] test " + tNum++ + " passed");
143
}
144
145
146
// TEST store null param
147
try {
148
ks.store(null);
149
throw new SecurityException("[Pre1.5] test " + tNum + " failed");
150
} catch (UnsupportedOperationException uoe) {
151
// good
152
System.out.println("[Pre1.5] test " + tNum++ + " passed");
153
}
154
155
156
// TEST getEntry with alias/protParam - use invalid alias
157
e = ks.getEntry("notPresent",
158
new KeyStore.PasswordProtection(password));
159
if (e == null) {
160
System.out.println("[Pre1.5] test " + tNum++ + " passed");
161
} else {
162
throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +
163
"expected null entry returned");
164
}
165
166
167
// TEST getEntry with alias/null protParam - get private key
168
try {
169
e = ks.getEntry("privkey", null);
170
throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +
171
"expected UnrecoverableEntryException");
172
} catch (UnrecoverableEntryException uee) {
173
System.out.println("[Pre1.5] test " + tNum++ + " passed");
174
}
175
176
177
// TEST getEntry with alias/bad password - get private key
178
try {
179
e = ks.getEntry("privkey",
180
new KeyStore.PasswordProtection(badPwd));
181
throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +
182
"expected UnrecoverableEntryException");
183
} catch (UnrecoverableEntryException uee) {
184
System.out.println("[Pre1.5] test " + tNum++ + " passed");
185
}
186
187
188
// TEST getEntry with alias/unknown protection - get private key
189
try {
190
e = ks.getEntry("privkey", new FooProtect());
191
throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +
192
"expected UnsupportedOperationException");
193
} catch (UnsupportedOperationException uoe) {
194
System.out.println("[Pre1.5] test " + tNum++ + " passed");
195
}
196
197
198
// TEST getEntry with alias/protParam - get private key
199
e = ks.getEntry("privkey", new KeyStore.PasswordProtection(password));
200
if (e instanceof KeyStore.PrivateKeyEntry) {
201
System.out.println("[Pre1.5] test " + tNum++ + " passed");
202
} else {
203
throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +
204
"expected PrivateKeyEntry");
205
}
206
207
208
// TEST getEntry with alias/null protParam - get trusted cert
209
e = ks.getEntry("trustedcert", null);
210
if (e instanceof KeyStore.TrustedCertificateEntry) {
211
System.out.println("[Pre1.5] test " + tNum++ + " passed");
212
} else {
213
throw new SecurityException("[Pre1.5] test " + tNum + " failed");
214
}
215
216
217
// TEST getEntry with alias/non-null protParam - get trusted cert
218
try {
219
e = ks.getEntry("trustedcert",
220
new KeyStore.PasswordProtection(password));
221
throw new SecurityException("[Pre1.5] test " + tNum + " failed");
222
} catch (UnsupportedOperationException uoe) {
223
System.out.println("[Pre1.5] test " + tNum++ + " passed");
224
}
225
226
227
// TEST setEntry with alias/entry/protParam - use invalid alias
228
try {
229
ks.setEntry("foo", new FooEntry(),
230
new KeyStore.PasswordProtection(password));
231
throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +
232
"expected KeyStoreException");
233
} catch (KeyStoreException kse) {
234
// good
235
System.out.println("[Pre1.5] test " + tNum++ + " passed");
236
}
237
238
239
// TEST setEntry with alias/entry/null protParam - set private key
240
try {
241
ks.setEntry("newPrivKey", pkeNew, null);
242
throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +
243
"expected KeyStoreException");
244
} catch (KeyStoreException kse) {
245
System.out.println("[Pre1.5] test " + tNum++ + " passed");
246
}
247
248
249
// TEST setEntry with alias/entry/random protParam - set private key
250
try {
251
ks.setEntry("newPrivKey", pkeNew, new FooProtect());
252
throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +
253
"expected KeyStoreException");
254
} catch (KeyStoreException kse) {
255
System.out.println("[Pre1.5] test " + tNum++ + " passed");
256
}
257
258
259
// TEST setEntry with alias/entry/protParam - set private key
260
ks.setEntry("newPrivKey", pkeNew,
261
new KeyStore.PasswordProtection(password));
262
e = ks.getEntry("newPrivKey",
263
new KeyStore.PasswordProtection(password));
264
if (e instanceof KeyStore.PrivateKeyEntry) {
265
System.out.println("[Pre1.5] test " + tNum++ + " passed");
266
} else {
267
throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +
268
"expected PrivateKeyEntry");
269
}
270
271
272
// TEST setEntry with alias/entry/non null protParam - set trusted cert
273
try {
274
ks.setEntry("newTrustedcert", tceNew,
275
new KeyStore.PasswordProtection(password));
276
throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +
277
"expected KeyStoreException");
278
} catch (KeyStoreException kse) {
279
// good
280
System.out.println("[Pre1.5] test " + tNum++ + " passed");
281
}
282
283
284
// TEST setEntry with alias/entry/null protParam - set trusted cert
285
ks.setEntry("newTrustedcert", tceNew, null);
286
e = ks.getEntry("newTrustedcert", null);
287
if (e instanceof KeyStore.TrustedCertificateEntry) {
288
System.out.println("[Pre1.5] test " + tNum++ + " passed");
289
} else {
290
throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +
291
"expected TrustedCertificateEntry");
292
}
293
294
295
// TEST entryInstanceOf - invalid alias
296
if (ks.entryInstanceOf("foo", EntryMethods.class) == false) {
297
System.out.println("[Pre1.5] test " + tNum++ + " passed");
298
} else {
299
throw new SecurityException("[Pre1.5] test " + tNum + " failed");
300
}
301
302
303
// TEST entryInstanceOf - false case
304
if (ks.entryInstanceOf("privkey", EntryMethods.class) == false) {
305
System.out.println("[Pre1.5] test " + tNum++ + " passed");
306
} else {
307
throw new SecurityException("[Pre1.5] test " + tNum + " failed");
308
}
309
310
311
// TEST entryInstanceOf - true case, trustedcert entry
312
if (ks.entryInstanceOf("trustedcert",
313
KeyStore.TrustedCertificateEntry.class)) {
314
System.out.println("[Pre1.5] test " + tNum++ + " passed");
315
} else {
316
throw new SecurityException("[Pre1.5] test " + tNum + " failed");
317
}
318
319
320
// TEST entryInstanceOf - true case, private key entry
321
if (ks.entryInstanceOf("privkey",
322
KeyStore.PrivateKeyEntry.class)) {
323
System.out.println("[Pre1.5] test " + tNum++ + " passed");
324
} else {
325
throw new SecurityException("[Pre1.5] test " + tNum + " failed");
326
}
327
328
}
329
330
private static void testPost15(KeyStore ks) throws Exception {
331
332
KeyStore.Entry e = null;
333
334
ks.load(new EntryMethods.FooParameter());
335
ks.store(new EntryMethods.FooParameter());
336
337
e = ks.getEntry("foo", new KeyStore.PasswordProtection(password));
338
if (!(e instanceof EntryMethods.FooEntry)) {
339
throw new SecurityException
340
("testPost15 getEntry(String, ProtParm) " +
341
"expected EntryMethods.FooEntry returned");
342
}
343
344
ks.setEntry("foo", new EntryMethods.FooEntry(),
345
new KeyStore.PasswordProtection(password));
346
347
if (!ks.entryInstanceOf("foo", KeyStore.PrivateKeyEntry.class)) {
348
throw new SecurityException
349
("testPost15 entryInstanceOf(String, Class) " +
350
"expected true returned");
351
}
352
353
System.out.println("[Post1.5] tests all passed");
354
}
355
356
private static void testUnrecoverable(KeyStore ks) throws Exception {
357
ks.load(new EntryMethods.FooParameter());
358
try {
359
ks.getEntry("foo", new KeyStore.PasswordProtection(password));
360
throw new SecurityException
361
("UnrecoverableEntryException not thrown for " +
362
"getEntry(String, ProtectionParam)");
363
} catch (UnrecoverableEntryException uee) {
364
// good
365
System.out.println("[UnrecoverableEntry] test passed");
366
}
367
}
368
369
public static class Pre15 extends KeyStoreSpi {
370
371
private static KeyStore jks = getJKS();
372
373
private static KeyStore getJKS() {
374
try {
375
return (KeyStore) KeyStore.getInstance("JKS");
376
} catch (Exception e) {
377
e.printStackTrace();
378
throw new RuntimeException(e);
379
}
380
}
381
public Pre15() {
382
}
383
384
public Key engineGetKey(String alias, char[] password)
385
throws NoSuchAlgorithmException, UnrecoverableKeyException {
386
try {
387
return jks.getKey(alias, password);
388
} catch (KeyStoreException ke) {
389
throw new RuntimeException("Unexpected exception", ke);
390
}
391
}
392
393
public java.security.cert.Certificate[] engineGetCertificateChain
394
(String alias) {
395
try {
396
return jks.getCertificateChain(alias);
397
} catch (KeyStoreException ke) {
398
throw new RuntimeException("Unexpected exception", ke);
399
}
400
}
401
402
public java.security.cert.Certificate engineGetCertificate
403
(String alias) {
404
try {
405
return jks.getCertificate(alias);
406
} catch (KeyStoreException ke) {
407
throw new RuntimeException("Unexpected exception", ke);
408
}
409
}
410
411
public Date engineGetCreationDate(String alias) {
412
try {
413
return jks.getCreationDate(alias);
414
} catch (KeyStoreException ke) {
415
throw new RuntimeException("Unexpected exception", ke);
416
}
417
}
418
419
public void engineSetKeyEntry(String alias, Key key,
420
char[] password,
421
java.security.cert.Certificate[] chain)
422
throws KeyStoreException {
423
jks.setKeyEntry(alias, key, password, chain);
424
}
425
426
public void engineSetKeyEntry(String alias, byte[] key,
427
java.security.cert.Certificate[] chain)
428
throws KeyStoreException {
429
jks.setKeyEntry(alias, key, chain);
430
}
431
432
public void engineSetCertificateEntry(String alias,
433
java.security.cert.Certificate cert)
434
throws KeyStoreException {
435
jks.setCertificateEntry(alias, cert);
436
}
437
438
public void engineDeleteEntry(String alias)
439
throws KeyStoreException {
440
jks.deleteEntry(alias);
441
}
442
443
public Enumeration engineAliases() {
444
try {
445
return jks.aliases();
446
} catch (KeyStoreException ke) {
447
throw new RuntimeException("Unexpected exception", ke);
448
}
449
450
}
451
452
public boolean engineContainsAlias(String alias) {
453
try {
454
return jks.containsAlias(alias);
455
} catch (KeyStoreException ke) {
456
throw new RuntimeException("Unexpected exception", ke);
457
}
458
}
459
460
public int engineSize() {
461
try {
462
return jks.size();
463
} catch (KeyStoreException ke) {
464
throw new RuntimeException("Unexpected exception", ke);
465
}
466
}
467
468
public boolean engineIsKeyEntry(String alias) {
469
try {
470
return jks.isKeyEntry(alias);
471
} catch (KeyStoreException ke) {
472
throw new RuntimeException("Unexpected exception", ke);
473
}
474
}
475
476
public boolean engineIsCertificateEntry(String alias) {
477
try {
478
return jks.isCertificateEntry(alias);
479
} catch (KeyStoreException ke) {
480
throw new RuntimeException("Unexpected exception", ke);
481
}
482
}
483
484
public String engineGetCertificateAlias
485
(java.security.cert.Certificate cert) {
486
try {
487
return jks.getCertificateAlias(cert);
488
} catch (KeyStoreException ke) {
489
throw new RuntimeException("Unexpected exception", ke);
490
}
491
}
492
493
public void engineStore(OutputStream stream, char[] password)
494
throws IOException, NoSuchAlgorithmException, CertificateException {
495
try {
496
jks.store(stream, password);
497
} catch (KeyStoreException ke) {
498
throw new RuntimeException("Unexpected exception", ke);
499
}
500
}
501
502
public void engineLoad(InputStream stream, char[] password)
503
throws IOException, NoSuchAlgorithmException, CertificateException {
504
jks.load(stream, password);
505
}
506
}
507
508
public static class Post15 extends Pre15 {
509
510
public void engineStore(KeyStore.LoadStoreParameter parameter)
511
throws IOException, NoSuchAlgorithmException, CertificateException {
512
if (!(parameter instanceof EntryMethods.FooParameter)) {
513
throw new IOException("Post15 engineStore method expected " +
514
"FooParameter");
515
}
516
}
517
518
public void engineLoad(KeyStore.LoadStoreParameter parameter)
519
throws IOException, NoSuchAlgorithmException, CertificateException {
520
if (!(parameter instanceof EntryMethods.FooParameter)) {
521
throw new IOException("Post15 engineLoadFrom method expected " +
522
"FooParameter");
523
}
524
}
525
526
public KeyStore.Entry engineGetEntry(String alias,
527
KeyStore.ProtectionParameter protectionParam)
528
throws UnrecoverableEntryException {
529
if (!alias.equals("foo")) {
530
throw new SecurityException
531
("Post15 engineGetEntry(String, ProtectionParam) " +
532
"expected [foo] alias");
533
}
534
KeyStore.PasswordProtection pwdParam =
535
(KeyStore.PasswordProtection)protectionParam;
536
if (pwdParam.getPassword().length != 6) {
537
throw new SecurityException
538
("Post15 engineGetEntry(String, ProtectionParam) " +
539
"expected [foobar] password");
540
}
541
542
return new EntryMethods.FooEntry();
543
}
544
545
public void engineSetEntry(String alias, KeyStore.Entry entry,
546
KeyStore.ProtectionParameter protectionParam) {
547
if (!alias.equals("foo") ||
548
!(entry instanceof EntryMethods.FooEntry)) {
549
throw new SecurityException
550
("Post15 engineSetEntry(String, entry, ProtParm) " +
551
"expected [foo] alias and EntryMethods.FooEntry");
552
}
553
554
KeyStore.PasswordProtection pwdParam =
555
(KeyStore.PasswordProtection)protectionParam;
556
if (pwdParam.getPassword().length != 6) {
557
throw new SecurityException
558
("Post15 engineSetEntry(String, entry, ProtParm) " +
559
"expected [foobar] password");
560
}
561
}
562
563
public boolean engineEntryInstanceOf(String alias,
564
Class<? extends KeyStore.Entry> entryClass)
565
{
566
if (!alias.equals("foo") ||
567
entryClass != KeyStore.PrivateKeyEntry.class) {
568
throw new SecurityException
569
("Post15 engineEntryInstanceOf(String, Class) " +
570
"expected [foo] alias " +
571
"and [KeyStore.PrivateKeyEntry] class");
572
}
573
return true;
574
}
575
}
576
577
public static class UnrecoverableKS extends Post15 {
578
public KeyStore.Entry engineGetEntry(String alias,
579
KeyStore.ProtectionParameter protectionParam)
580
throws UnrecoverableEntryException {
581
throw new UnrecoverableEntryException();
582
}
583
}
584
}
585
586