Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/security/KeyStore/EntryMethods.java
38811 views
1
/*
2
* Copyright (c) 2003, 2004, 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
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
import sun.security.provider.*;
36
37
public class EntryMethods
38
extends Provider
39
implements KeyStore.Entry
40
{
41
42
private static FileInputStream pre15fis;
43
private static char[] password = {'f', 'o', 'o', 'b', 'a', 'r'};
44
private static char[] badPwd = {'b', 'a', 'd', 'p', 'w', 'd'};
45
46
public static class FooProtect implements KeyStore.ProtectionParameter { }
47
public static class FooParameter implements KeyStore.LoadStoreParameter {
48
public KeyStore.ProtectionParameter getProtectionParameter() {
49
return null;
50
}
51
}
52
53
public static class FooEntry implements KeyStore.Entry { }
54
55
public EntryMethods() throws Exception {
56
super("EntryMethods", 0.0, "EntryMethods");
57
58
pre15fis = new FileInputStream
59
(System.getProperty("test.src") + "/EntryMethods.pre15.keystore");
60
61
AccessController.doPrivileged(new PrivilegedAction() {
62
public Object run() {
63
put("KeyStore.Pre15KeyStore", "EntryMethods$Pre15");
64
put("KeyStore.Post15KeyStore", "EntryMethods$Post15");
65
put("KeyStore.UnrecoverableKeyStore",
66
"EntryMethods$UnrecoverableKS");
67
return null;
68
}
69
});
70
}
71
72
public static void main(String[] args) throws Exception {
73
74
EntryMethods entry = new EntryMethods();
75
76
// test pre-JDK1.5 KeyStore throws UnsupportedOperationExceptions
77
// for new methods
78
KeyStore pre15KS = KeyStore.getInstance("Pre15KeyStore", entry);
79
testPre15(pre15KS);
80
81
// test post-JDK1.5 KeyStore does right thing with new methods
82
KeyStore post15KS = KeyStore.getInstance("Post15KeyStore", entry);
83
testPost15(post15KS);
84
85
// test post-JDK1.5 KeyStore can throw new UnrecoverableEntryException
86
KeyStore uKS = KeyStore.getInstance("UnrecoverableKeyStore", entry);
87
testUnrecoverable(uKS);
88
}
89
90
private static void testPre15(KeyStore ks) throws Exception {
91
92
int tNum = 1;
93
KeyStore.Entry e = null;
94
95
// TEST load null param
96
ks.load((KeyStore.LoadStoreParameter)null);
97
System.out.println("[Pre1.5] test " + tNum++ + " passed");
98
99
100
// TEST load random param
101
try {
102
ks.load(new FooParameter());
103
throw new SecurityException("[Pre1.5] test " + tNum + " failed");
104
} catch (UnsupportedOperationException uoe) {
105
System.out.println("[Pre1.5] test " + tNum++ + " passed");
106
}
107
108
109
// TEST store random param
110
ks.load(pre15fis, password);
111
112
// setup for later user
113
KeyStore.Entry pkeNew = ks.getEntry("privkey",
114
new KeyStore.PasswordProtection(password));
115
KeyStore.Entry tceNew = ks.getEntry("trustedcert", null);
116
117
try {
118
ks.store(new FooParameter());
119
throw new SecurityException("[Pre1.5] test " + tNum + " failed");
120
} catch (UnsupportedOperationException uoe) {
121
// good
122
System.out.println("[Pre1.5] test " + tNum++ + " passed");
123
}
124
125
126
// TEST store null param
127
try {
128
ks.store(null);
129
throw new SecurityException("[Pre1.5] test " + tNum + " failed");
130
} catch (UnsupportedOperationException uoe) {
131
// good
132
System.out.println("[Pre1.5] test " + tNum++ + " passed");
133
}
134
135
136
// TEST getEntry with alias/protParam - use invalid alias
137
e = ks.getEntry("notPresent",
138
new KeyStore.PasswordProtection(password));
139
if (e == null) {
140
System.out.println("[Pre1.5] test " + tNum++ + " passed");
141
} else {
142
throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +
143
"expected null entry returned");
144
}
145
146
147
// TEST getEntry with alias/null protParam - get private key
148
try {
149
e = ks.getEntry("privkey", null);
150
throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +
151
"expected UnrecoverableEntryException");
152
} catch (UnrecoverableEntryException uee) {
153
System.out.println("[Pre1.5] test " + tNum++ + " passed");
154
}
155
156
157
// TEST getEntry with alias/bad password - get private key
158
try {
159
e = ks.getEntry("privkey",
160
new KeyStore.PasswordProtection(badPwd));
161
throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +
162
"expected UnrecoverableEntryException");
163
} catch (UnrecoverableEntryException uee) {
164
System.out.println("[Pre1.5] test " + tNum++ + " passed");
165
}
166
167
168
// TEST getEntry with alias/unknown protection - get private key
169
try {
170
e = ks.getEntry("privkey", new FooProtect());
171
throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +
172
"expected UnsupportedOperationException");
173
} catch (UnsupportedOperationException uoe) {
174
System.out.println("[Pre1.5] test " + tNum++ + " passed");
175
}
176
177
178
// TEST getEntry with alias/protParam - get private key
179
e = ks.getEntry("privkey", new KeyStore.PasswordProtection(password));
180
if (e instanceof KeyStore.PrivateKeyEntry) {
181
System.out.println("[Pre1.5] test " + tNum++ + " passed");
182
} else {
183
throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +
184
"expected PrivateKeyEntry");
185
}
186
187
188
// TEST getEntry with alias/null protParam - get trusted cert
189
e = ks.getEntry("trustedcert", null);
190
if (e instanceof KeyStore.TrustedCertificateEntry) {
191
System.out.println("[Pre1.5] test " + tNum++ + " passed");
192
} else {
193
throw new SecurityException("[Pre1.5] test " + tNum + " failed");
194
}
195
196
197
// TEST getEntry with alias/non-null protParam - get trusted cert
198
try {
199
e = ks.getEntry("trustedcert",
200
new KeyStore.PasswordProtection(password));
201
throw new SecurityException("[Pre1.5] test " + tNum + " failed");
202
} catch (UnsupportedOperationException uoe) {
203
System.out.println("[Pre1.5] test " + tNum++ + " passed");
204
}
205
206
207
// TEST setEntry with alias/entry/protParam - use invalid alias
208
try {
209
ks.setEntry("foo", new FooEntry(),
210
new KeyStore.PasswordProtection(password));
211
throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +
212
"expected KeyStoreException");
213
} catch (KeyStoreException kse) {
214
// good
215
System.out.println("[Pre1.5] test " + tNum++ + " passed");
216
}
217
218
219
// TEST setEntry with alias/entry/null protParam - set private key
220
try {
221
ks.setEntry("newPrivKey", pkeNew, null);
222
throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +
223
"expected KeyStoreException");
224
} catch (KeyStoreException kse) {
225
System.out.println("[Pre1.5] test " + tNum++ + " passed");
226
}
227
228
229
// TEST setEntry with alias/entry/random protParam - set private key
230
try {
231
ks.setEntry("newPrivKey", pkeNew, new FooProtect());
232
throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +
233
"expected KeyStoreException");
234
} catch (KeyStoreException kse) {
235
System.out.println("[Pre1.5] test " + tNum++ + " passed");
236
}
237
238
239
// TEST setEntry with alias/entry/protParam - set private key
240
ks.setEntry("newPrivKey", pkeNew,
241
new KeyStore.PasswordProtection(password));
242
e = ks.getEntry("newPrivKey",
243
new KeyStore.PasswordProtection(password));
244
if (e instanceof KeyStore.PrivateKeyEntry) {
245
System.out.println("[Pre1.5] test " + tNum++ + " passed");
246
} else {
247
throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +
248
"expected PrivateKeyEntry");
249
}
250
251
252
// TEST setEntry with alias/entry/non null protParam - set trusted cert
253
try {
254
ks.setEntry("newTrustedcert", tceNew,
255
new KeyStore.PasswordProtection(password));
256
throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +
257
"expected KeyStoreException");
258
} catch (KeyStoreException kse) {
259
// good
260
System.out.println("[Pre1.5] test " + tNum++ + " passed");
261
}
262
263
264
// TEST setEntry with alias/entry/null protParam - set trusted cert
265
ks.setEntry("newTrustedcert", tceNew, null);
266
e = ks.getEntry("newTrustedcert", null);
267
if (e instanceof KeyStore.TrustedCertificateEntry) {
268
System.out.println("[Pre1.5] test " + tNum++ + " passed");
269
} else {
270
throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +
271
"expected TrustedCertificateEntry");
272
}
273
274
275
// TEST entryInstanceOf - invalid alias
276
if (ks.entryInstanceOf("foo", EntryMethods.class) == false) {
277
System.out.println("[Pre1.5] test " + tNum++ + " passed");
278
} else {
279
throw new SecurityException("[Pre1.5] test " + tNum + " failed");
280
}
281
282
283
// TEST entryInstanceOf - false case
284
if (ks.entryInstanceOf("privkey", EntryMethods.class) == false) {
285
System.out.println("[Pre1.5] test " + tNum++ + " passed");
286
} else {
287
throw new SecurityException("[Pre1.5] test " + tNum + " failed");
288
}
289
290
291
// TEST entryInstanceOf - true case, trustedcert entry
292
if (ks.entryInstanceOf("trustedcert",
293
KeyStore.TrustedCertificateEntry.class)) {
294
System.out.println("[Pre1.5] test " + tNum++ + " passed");
295
} else {
296
throw new SecurityException("[Pre1.5] test " + tNum + " failed");
297
}
298
299
300
// TEST entryInstanceOf - true case, private key entry
301
if (ks.entryInstanceOf("privkey",
302
KeyStore.PrivateKeyEntry.class)) {
303
System.out.println("[Pre1.5] test " + tNum++ + " passed");
304
} else {
305
throw new SecurityException("[Pre1.5] test " + tNum + " failed");
306
}
307
308
}
309
310
private static void testPost15(KeyStore ks) throws Exception {
311
312
KeyStore.Entry e = null;
313
314
ks.load(new EntryMethods.FooParameter());
315
ks.store(new EntryMethods.FooParameter());
316
317
e = ks.getEntry("foo", new KeyStore.PasswordProtection(password));
318
if (!(e instanceof EntryMethods.FooEntry)) {
319
throw new SecurityException
320
("testPost15 getEntry(String, ProtParm) " +
321
"expected EntryMethods.FooEntry returned");
322
}
323
324
ks.setEntry("foo", new EntryMethods.FooEntry(),
325
new KeyStore.PasswordProtection(password));
326
327
if (!ks.entryInstanceOf("foo", KeyStore.PrivateKeyEntry.class)) {
328
throw new SecurityException
329
("testPost15 entryInstanceOf(String, Class) " +
330
"expected true returned");
331
}
332
333
System.out.println("[Post1.5] tests all passed");
334
}
335
336
private static void testUnrecoverable(KeyStore ks) throws Exception {
337
ks.load(new EntryMethods.FooParameter());
338
try {
339
ks.getEntry("foo", new KeyStore.PasswordProtection(password));
340
throw new SecurityException
341
("UnrecoverableEntryException not thrown for " +
342
"getEntry(String, ProtectionParam)");
343
} catch (UnrecoverableEntryException uee) {
344
// good
345
System.out.println("[UnrecoverableEntry] test passed");
346
}
347
}
348
349
public static class Pre15 extends KeyStoreSpi {
350
351
private static KeyStoreSpi jks = getJKS();
352
353
// javac does not allow direct access to class (javac bug?)
354
// use reflection instead
355
private static KeyStoreSpi getJKS() {
356
try {
357
Class clazz = Class.forName("sun.security.provider.JavaKeyStore$JKS");
358
return (KeyStoreSpi)clazz.newInstance();
359
} catch (Exception e) {
360
e.printStackTrace();
361
throw new RuntimeException(e);
362
}
363
}
364
365
public Key engineGetKey(String alias, char[] password)
366
throws NoSuchAlgorithmException, UnrecoverableKeyException {
367
return jks.engineGetKey(alias, password);
368
}
369
370
public java.security.cert.Certificate[] engineGetCertificateChain
371
(String alias) {
372
return jks.engineGetCertificateChain(alias);
373
}
374
375
public java.security.cert.Certificate engineGetCertificate
376
(String alias) {
377
return jks.engineGetCertificate(alias);
378
}
379
380
public Date engineGetCreationDate(String alias) {
381
return jks.engineGetCreationDate(alias);
382
}
383
384
public void engineSetKeyEntry(String alias, Key key,
385
char[] password,
386
java.security.cert.Certificate[] chain)
387
throws KeyStoreException {
388
jks.engineSetKeyEntry(alias, key, password, chain);
389
}
390
391
public void engineSetKeyEntry(String alias, byte[] key,
392
java.security.cert.Certificate[] chain)
393
throws KeyStoreException {
394
jks.engineSetKeyEntry(alias, key, chain);
395
}
396
397
public void engineSetCertificateEntry(String alias,
398
java.security.cert.Certificate cert)
399
throws KeyStoreException {
400
jks.engineSetCertificateEntry(alias, cert);
401
}
402
403
public void engineDeleteEntry(String alias)
404
throws KeyStoreException {
405
jks.engineDeleteEntry(alias);
406
}
407
408
public Enumeration engineAliases() {
409
return jks.engineAliases();
410
}
411
412
public boolean engineContainsAlias(String alias) {
413
return jks.engineContainsAlias(alias);
414
}
415
416
public int engineSize() {
417
return jks.engineSize();
418
}
419
420
public boolean engineIsKeyEntry(String alias) {
421
return jks.engineIsKeyEntry(alias);
422
}
423
424
public boolean engineIsCertificateEntry(String alias) {
425
return jks.engineIsCertificateEntry(alias);
426
}
427
428
public String engineGetCertificateAlias
429
(java.security.cert.Certificate cert) {
430
return jks.engineGetCertificateAlias(cert);
431
}
432
433
public void engineStore(OutputStream stream, char[] password)
434
throws IOException, NoSuchAlgorithmException, CertificateException {
435
jks.engineStore(stream, password);
436
}
437
438
public void engineLoad(InputStream stream, char[] password)
439
throws IOException, NoSuchAlgorithmException, CertificateException {
440
jks.engineLoad(stream, password);
441
}
442
}
443
444
public static class Post15 extends Pre15 {
445
446
public void engineStore(KeyStore.LoadStoreParameter parameter)
447
throws IOException, NoSuchAlgorithmException, CertificateException {
448
if (!(parameter instanceof EntryMethods.FooParameter)) {
449
throw new IOException("Post15 engineStore method expected " +
450
"FooParameter");
451
}
452
}
453
454
public void engineLoad(KeyStore.LoadStoreParameter parameter)
455
throws IOException, NoSuchAlgorithmException, CertificateException {
456
if (!(parameter instanceof EntryMethods.FooParameter)) {
457
throw new IOException("Post15 engineLoadFrom method expected " +
458
"FooParameter");
459
}
460
}
461
462
public KeyStore.Entry engineGetEntry(String alias,
463
KeyStore.ProtectionParameter protectionParam)
464
throws UnrecoverableEntryException {
465
if (!alias.equals("foo")) {
466
throw new SecurityException
467
("Post15 engineGetEntry(String, ProtectionParam) " +
468
"expected [foo] alias");
469
}
470
KeyStore.PasswordProtection pwdParam =
471
(KeyStore.PasswordProtection)protectionParam;
472
if (pwdParam.getPassword().length != 6) {
473
throw new SecurityException
474
("Post15 engineGetEntry(String, ProtectionParam) " +
475
"expected [foobar] password");
476
}
477
478
return new EntryMethods.FooEntry();
479
}
480
481
public void engineSetEntry(String alias, KeyStore.Entry entry,
482
KeyStore.ProtectionParameter protectionParam) {
483
if (!alias.equals("foo") ||
484
!(entry instanceof EntryMethods.FooEntry)) {
485
throw new SecurityException
486
("Post15 engineSetEntry(String, entry, ProtParm) " +
487
"expected [foo] alias and EntryMethods.FooEntry");
488
}
489
490
KeyStore.PasswordProtection pwdParam =
491
(KeyStore.PasswordProtection)protectionParam;
492
if (pwdParam.getPassword().length != 6) {
493
throw new SecurityException
494
("Post15 engineSetEntry(String, entry, ProtParm) " +
495
"expected [foobar] password");
496
}
497
}
498
499
public boolean engineEntryInstanceOf(String alias,
500
Class<? extends KeyStore.Entry> entryClass)
501
{
502
if (!alias.equals("foo") ||
503
entryClass != KeyStore.PrivateKeyEntry.class) {
504
throw new SecurityException
505
("Post15 engineEntryInstanceOf(String, Class) " +
506
"expected [foo] alias " +
507
"and [KeyStore.PrivateKeyEntry] class");
508
}
509
return true;
510
}
511
}
512
513
public static class UnrecoverableKS extends Post15 {
514
public KeyStore.Entry engineGetEntry(String alias,
515
KeyStore.ProtectionParameter protectionParam)
516
throws UnrecoverableEntryException {
517
throw new UnrecoverableEntryException();
518
}
519
}
520
}
521
522