Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/provider/KeyStore/DKSTest.java
38853 views
1
/*
2
* Copyright (c) 2013, 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
* see ./DKSTest.sh
26
*/
27
28
import java.io.*;
29
import java.net.*;
30
import java.security.*;
31
import java.security.KeyStore;
32
import java.security.cert.*;
33
import java.security.cert.Certificate;
34
import java.util.*;
35
36
// Load and store entries in domain keystores
37
38
public class DKSTest {
39
40
private static final String TEST_SRC = System.getProperty("test.src");
41
private static final String USER_DIR = System.getProperty("user.dir");
42
private static final String CERT = TEST_SRC + "/../../pkcs12/trusted.pem";
43
private static final String CONFIG = "file://" + TEST_SRC + "/domains.cfg";
44
private static final Map<String, KeyStore.ProtectionParameter> PASSWORDS =
45
new HashMap<String, KeyStore.ProtectionParameter>() {{
46
put("keystore",
47
new KeyStore.PasswordProtection("test123".toCharArray()));
48
put("policy_keystore",
49
new KeyStore.PasswordProtection(
50
"Alias.password".toCharArray()));
51
put("pw_keystore",
52
new KeyStore.PasswordProtection("test12".toCharArray()));
53
put("eckeystore1",
54
new KeyStore.PasswordProtection("password".toCharArray()));
55
put("eckeystore2",
56
new KeyStore.PasswordProtection("password".toCharArray()));
57
put("truststore",
58
new KeyStore.PasswordProtection("changeit".toCharArray()));
59
put("empty",
60
new KeyStore.PasswordProtection("passphrase".toCharArray()));
61
}};
62
63
private static final Map<String, KeyStore.ProtectionParameter>
64
WRONG_PASSWORDS = new HashMap<String, KeyStore.ProtectionParameter>() {{
65
put("policy_keystore",
66
new KeyStore.PasswordProtection(
67
"wrong".toCharArray()));
68
put("pw_keystore",
69
new KeyStore.PasswordProtection("wrong".toCharArray()));
70
put("eckeystore1",
71
new KeyStore.PasswordProtection("wrong".toCharArray()));
72
put("eckeystore2",
73
new KeyStore.PasswordProtection("wrong".toCharArray()));
74
}};
75
76
public static void main(String[] args) throws Exception {
77
/*
78
* domain keystore: keystores with wrong passwords
79
*/
80
try {
81
URI config = new URI(CONFIG + "#keystores");
82
KeyStore ks = KeyStore.getInstance("DKS");
83
ks.load(new DomainLoadStoreParameter(config, WRONG_PASSWORDS));
84
throw new RuntimeException("Expected exception not thrown");
85
} catch (IOException e) {
86
System.out.println("Expected exception: " + e);
87
if (!causedBy(e, UnrecoverableKeyException.class)) {
88
e.printStackTrace(System.out);
89
throw new RuntimeException("Unexpected cause");
90
}
91
System.out.println("Expected cause: " + e);
92
}
93
94
/*
95
* domain keystore: system
96
*/
97
URI config = new URI(CONFIG + "#system");
98
int cacertsCount;
99
int expected;
100
KeyStore keystore = KeyStore.getInstance("DKS");
101
// load entries
102
keystore.load(new DomainLoadStoreParameter(config, PASSWORDS));
103
cacertsCount = expected = keystore.size();
104
System.out.println("\nLoading domain keystore: " + config + "\t[" +
105
expected + " entries]");
106
checkEntries(keystore, expected);
107
108
/*
109
* domain keystore: system_plus
110
*/
111
config = new URI(CONFIG + "#system_plus");
112
expected = cacertsCount + 1;
113
keystore = KeyStore.getInstance("DKS");
114
// load entries
115
keystore.load(new DomainLoadStoreParameter(config, PASSWORDS));
116
System.out.println("\nLoading domain keystore: " + config + "\t[" +
117
expected + " entries]");
118
checkEntries(keystore, expected);
119
120
/*
121
* domain keystore: system_env
122
*/
123
config = new URI(CONFIG + "#system_env");
124
expected = 1 + cacertsCount;
125
keystore = KeyStore.getInstance("DKS");
126
// load entries
127
keystore.load(
128
new DomainLoadStoreParameter(config,
129
Collections.<String, KeyStore.ProtectionParameter>emptyMap()));
130
System.out.println("\nLoading domain keystore: " + config + "\t[" +
131
expected + " entries]");
132
checkEntries(keystore, expected);
133
134
/*
135
* domain keystore: empty
136
*/
137
KeyStore empty = KeyStore.getInstance("JKS");
138
empty.load(null, null);
139
140
try (OutputStream outStream =
141
new FileOutputStream(new File(USER_DIR, "empty.jks"))) {
142
empty.store(outStream, "passphrase".toCharArray());
143
}
144
config = new URI(CONFIG + "#empty");
145
expected = 0;
146
keystore = KeyStore.getInstance("DKS");
147
// load entries
148
keystore.load(new DomainLoadStoreParameter(config, PASSWORDS));
149
System.out.println("\nLoading domain keystore: " + config + "\t[" +
150
expected + " entries]");
151
checkEntries(keystore, expected);
152
153
/*
154
* domain keystore: keystores
155
*/
156
config = new URI(CONFIG + "#keystores");
157
expected = 2 + 1 + 1 + 1;
158
keystore = KeyStore.getInstance("DKS");
159
// load entries
160
keystore.load(new DomainLoadStoreParameter(config, PASSWORDS));
161
System.out.println("\nLoading domain keystore: " + config + "\t[" +
162
expected + " entries]");
163
checkEntries(keystore, expected);
164
// set a new trusted certificate entry
165
Certificate cert = loadCertificate(CERT);
166
String alias = "pw_keystore tmp-cert";
167
System.out.println("Setting new trusted certificate entry: " + alias);
168
keystore.setEntry(alias,
169
new KeyStore.TrustedCertificateEntry(cert), null);
170
expected++;
171
// store entries
172
config = new URI(CONFIG + "#keystores_tmp");
173
System.out.println("Storing domain keystore: " + config + "\t[" +
174
expected + " entries]");
175
keystore.store(new DomainLoadStoreParameter(config, PASSWORDS));
176
keystore = KeyStore.getInstance("DKS");
177
// reload entries
178
keystore.load(new DomainLoadStoreParameter(config, PASSWORDS));
179
System.out.println("Reloading domain keystore: " + config + "\t[" +
180
expected + " entries]");
181
checkEntries(keystore, expected);
182
// get the new trusted certificate entry
183
System.out.println("Getting new trusted certificate entry: " + alias);
184
if (!keystore.isCertificateEntry(alias)) {
185
throw new Exception("Error: cannot retrieve certificate entry: " +
186
alias);
187
}
188
keystore.setEntry(alias,
189
new KeyStore.TrustedCertificateEntry(cert), null);
190
}
191
192
private static void checkEntries(KeyStore keystore, int expected)
193
throws Exception {
194
int i = 0;
195
for (String alias : Collections.list(keystore.aliases())) {
196
System.out.print(".");
197
i++;
198
}
199
System.out.println();
200
if (expected != i) {
201
throw new Exception("Error: unexpected entry count in keystore: " +
202
"loaded=" + i + ", expected=" + expected);
203
}
204
}
205
206
private static Certificate loadCertificate(String certFile)
207
throws Exception {
208
X509Certificate cert = null;
209
try (FileInputStream certStream = new FileInputStream(certFile)) {
210
CertificateFactory factory =
211
CertificateFactory.getInstance("X.509");
212
return factory.generateCertificate(certStream);
213
}
214
}
215
216
// checks if an exception was caused by specified exception class
217
private static boolean causedBy(Exception e, Class klass) {
218
Throwable cause = e;
219
while ((cause = cause.getCause()) != null) {
220
if (cause.getClass().equals(klass)) {
221
return true;
222
}
223
}
224
return false;
225
}
226
}
227
228