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