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/TestKeyStoreEntry.java
38811 views
1
/*
2
* Copyright (c) 2001, 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
import static java.lang.System.out;
25
26
import java.io.FileInputStream;
27
import java.io.FileOutputStream;
28
import java.security.Key;
29
import java.security.KeyStore;
30
import java.security.Provider;
31
import java.security.Security;
32
import javax.crypto.KeyGenerator;
33
import javax.crypto.SecretKey;
34
35
/*
36
* @test
37
* @bug 8048621
38
* @summary Test the basic operations of KeyStore entry, provided by SunJCE
39
* (jceks), and SunPKCS11-Solaris(PKCS11KeyStore)
40
* @author Yu-Ching Valerie PENG
41
*/
42
43
public class TestKeyStoreEntry {
44
private static final char[] PASSWDK = new char[] {
45
't', 'e', 'r', 'c', 'e', 's'
46
};
47
private static final char[] PASSWDF = new String("guardian Angel")
48
.toCharArray();
49
private static final String[] KS_ALGOS = {
50
"DES", "DESede", "Blowfish"
51
};
52
private static final int NUM_ALGOS = KS_ALGOS.length;
53
54
private static final String[] KS_TYPE = {
55
"jks", "jceks", "pkcs12", "PKCS11KeyStore"
56
};
57
private static final String[] PRO_TYPE = {
58
"SUN", "SunJCE", "SunJSSE", "SunPKCS11-Solaris"
59
};
60
61
private final SecretKey[] sks = new SecretKey[NUM_ALGOS];
62
63
TestKeyStoreEntry() throws Exception {
64
// generate secret keys which are to be stored in the jce
65
// key store object
66
KeyGenerator[] kgs = new KeyGenerator[NUM_ALGOS];
67
for (int i = 0; i < NUM_ALGOS; i++) {
68
kgs[i] = KeyGenerator.getInstance(KS_ALGOS[i], "SunJCE");
69
sks[i] = kgs[i].generateKey();
70
}
71
72
}
73
74
public static void main(String args[]) throws Exception {
75
TestKeyStoreEntry jstest = new TestKeyStoreEntry();
76
jstest.run();
77
}
78
79
public void run() throws Exception {
80
81
Provider[] providers = Security.getProviders();
82
for (Provider p: providers) {
83
String prvName = p.getName();
84
if (prvName.startsWith("SunJCE")
85
|| prvName.startsWith("SunPKCS11-Solaris")) {
86
try {
87
runTest(p);
88
out.println("Test with provider " + p.getName() + ""
89
+ " passed");
90
91
} catch (java.security.KeyStoreException e) {
92
if (prvName.startsWith("SunPKCS11-Solaris")) {
93
out.println("KeyStoreException is expected because "
94
+ "PKCS11KeyStore is invalid keystore type.");
95
e.printStackTrace();
96
} else {
97
throw e;
98
}
99
}
100
}
101
}
102
}
103
104
public void runTest(Provider p) throws Exception {
105
try (FileOutputStream fos = new FileOutputStream("jceks");
106
FileInputStream fis = new FileInputStream("jceks");) {
107
108
KeyStore ks = KeyStore.getInstance("jceks", p);
109
// create an empty key store
110
ks.load(null, null);
111
112
// store the secret keys
113
String aliasHead = new String("secretKey");
114
for (int j = 0; j < NUM_ALGOS; j++) {
115
ks.setKeyEntry(aliasHead + j, sks[j], PASSWDK, null);
116
}
117
118
// write the key store out to a file
119
ks.store(fos, PASSWDF);
120
// wipe clean the existing key store
121
for (int k = 0; k < NUM_ALGOS; k++) {
122
ks.deleteEntry(aliasHead + k);
123
}
124
if (ks.size() != 0) {
125
throw new RuntimeException("ERROR: re-initialization failed");
126
}
127
128
// reload the key store with the file
129
ks.load(fis, PASSWDF);
130
131
// check the integrity/validaty of the key store
132
Key temp = null;
133
String alias = null;
134
if (ks.size() != NUM_ALGOS) {
135
throw new RuntimeException("ERROR: wrong number of key"
136
+ " entries");
137
}
138
139
for (int m = 0; m < ks.size(); m++) {
140
alias = aliasHead + m;
141
temp = ks.getKey(alias, PASSWDK);
142
// compare the keys
143
if (!temp.equals(sks[m])) {
144
throw new RuntimeException("ERROR: key comparison (" + m
145
+ ") failed");
146
}
147
// check the type of key
148
if (ks.isCertificateEntry(alias) || !ks.isKeyEntry(alias)) {
149
throw new RuntimeException("ERROR: type identification ("
150
+ m + ") failed");
151
}
152
}
153
}
154
}
155
156
}
157
158