Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/tools/keytool/ProbingFailure.java
41152 views
1
/*
2
* Copyright (c) 2018, 2019, 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
26
* @bug 8214100
27
* @summary use of keystore probing results in unnecessary exception thrown
28
* @library /test/lib
29
* @compile -XDignore.symbol.file ProbingFailure.java
30
* @run main ProbingFailure
31
*/
32
33
import jdk.test.lib.SecurityTools;
34
import jdk.test.lib.process.OutputAnalyzer;
35
36
import java.io.IOException;
37
import java.io.InputStream;
38
import java.io.OutputStream;
39
import java.security.Key;
40
import java.security.KeyStore;
41
import java.security.KeyStoreException;
42
import java.security.KeyStoreSpi;
43
import java.security.NoSuchAlgorithmException;
44
import java.security.Provider;
45
import java.security.UnrecoverableKeyException;
46
import java.security.cert.Certificate;
47
import java.security.cert.CertificateException;
48
import java.util.Date;
49
import java.util.Enumeration;
50
51
public class ProbingFailure {
52
53
public static void main(String[] args) throws Exception {
54
55
// genkeypair
56
kt("-genkeypair -keystore mks -alias a -dname CN=A -keyalg DSA -storetype MYKS")
57
.shouldHaveExitValue(0);
58
59
// list
60
kt("-list -keystore mks -storetype MYKS")
61
.shouldHaveExitValue(0);
62
63
kt("-list -keystore mks")
64
.shouldHaveExitValue(1)
65
.shouldContain("Unrecognized keystore format");
66
67
// importkeystore
68
kt("-importkeystore -srckeystore mks -srcstoretype MYKS -destkeystore p12")
69
.shouldHaveExitValue(0);
70
71
kt("-importkeystore -srckeystore mks -destkeystore p12a")
72
.shouldHaveExitValue(1)
73
.shouldContain("Unrecognized keystore format");
74
75
// in-place importkeystore
76
kt("-importkeystore -srckeystore mks -srcstoretype MYKS -destkeystore mks -deststoretype myks")
77
.shouldContain("The original keystore \"mks\" is backed up")
78
.shouldHaveExitValue(0);
79
80
kt("-importkeystore -srckeystore mks -srcstoretype MYKS -destkeystore mks")
81
.shouldContain("Migrated \"mks\" to PKCS12")
82
.shouldHaveExitValue(0);
83
84
kt("-importkeystore -srckeystore p12 -destkeystore p12 -deststoretype MYKS")
85
.shouldContain("Migrated \"p12\" to MYKS")
86
.shouldHaveExitValue(0);
87
}
88
89
static OutputAnalyzer kt(String cmd) throws Exception {
90
return SecurityTools.keytool(
91
"-storepass changeit -keypass changeit -debug "
92
+ "-srcstorepass changeit -deststorepass changeit "
93
+ "-providerclass ProbingFailure$MyProvider "
94
+ "-providerpath " + System.getProperty("test.classes")
95
+ " " + cmd);
96
}
97
98
public static class MyProvider extends Provider {
99
public MyProvider() {
100
super("MP", "1.0", "My Provider");
101
put("KeyStore.MYKS", "ProbingFailure$MyKS");
102
}
103
}
104
105
// The MYKS keystore prepends a zero byte before a PKCS12 file
106
// and does not support probing.
107
public static class MyKS extends KeyStoreSpi {
108
109
KeyStore ks;
110
111
public MyKS() {
112
try {
113
ks = KeyStore.getInstance("PKCS12");
114
} catch (Exception e) {
115
throw new RuntimeException(e);
116
}
117
}
118
119
@Override
120
public Key engineGetKey(String alias, char[] password)
121
throws NoSuchAlgorithmException, UnrecoverableKeyException {
122
try {
123
return ks.getKey(alias, password);
124
} catch (KeyStoreException e) {
125
throw new RuntimeException(e);
126
}
127
}
128
129
@Override
130
public Certificate[] engineGetCertificateChain(String alias) {
131
try {
132
return ks.getCertificateChain(alias);
133
} catch (KeyStoreException e) {
134
throw new RuntimeException(e);
135
}
136
}
137
138
@Override
139
public Certificate engineGetCertificate(String alias) {
140
try {
141
return ks.getCertificate(alias);
142
} catch (KeyStoreException e) {
143
throw new RuntimeException(e);
144
}
145
}
146
147
@Override
148
public Date engineGetCreationDate(String alias) {
149
try {
150
return ks.getCreationDate(alias);
151
} catch (KeyStoreException e) {
152
throw new RuntimeException(e);
153
}
154
}
155
156
@Override
157
public void engineSetKeyEntry(String alias, Key key, char[] password,
158
Certificate[] chain) throws KeyStoreException {
159
ks.setKeyEntry(alias, key, password, chain);
160
}
161
162
@Override
163
public void engineSetKeyEntry(String alias, byte[] key,
164
Certificate[] chain) throws KeyStoreException {
165
ks.setKeyEntry(alias, key, chain);
166
}
167
168
@Override
169
public void engineSetCertificateEntry(String alias, Certificate cert)
170
throws KeyStoreException {
171
ks.setCertificateEntry(alias, cert);
172
}
173
174
@Override
175
public void engineDeleteEntry(String alias) throws KeyStoreException {
176
ks.deleteEntry(alias);
177
}
178
179
@Override
180
public Enumeration<String> engineAliases() {
181
try {
182
return ks.aliases();
183
} catch (KeyStoreException e) {
184
throw new RuntimeException(e);
185
}
186
}
187
188
@Override
189
public boolean engineContainsAlias(String alias) {
190
try {
191
return ks.containsAlias(alias);
192
} catch (KeyStoreException e) {
193
throw new RuntimeException(e);
194
}
195
}
196
197
@Override
198
public int engineSize() {
199
try {
200
return ks.size();
201
} catch (KeyStoreException e) {
202
throw new RuntimeException(e);
203
}
204
}
205
206
@Override
207
public boolean engineIsKeyEntry(String alias) {
208
try {
209
return ks.isKeyEntry(alias);
210
} catch (KeyStoreException e) {
211
throw new RuntimeException(e);
212
}
213
}
214
215
@Override
216
public boolean engineIsCertificateEntry(String alias) {
217
try {
218
return ks.isCertificateEntry(alias);
219
} catch (KeyStoreException e) {
220
throw new RuntimeException(e);
221
}
222
}
223
224
@Override
225
public String engineGetCertificateAlias(Certificate cert) {
226
try {
227
return ks.getCertificateAlias(cert);
228
} catch (KeyStoreException e) {
229
throw new RuntimeException(e);
230
}
231
}
232
233
@Override
234
public void engineStore(OutputStream stream, char[] password)
235
throws IOException, NoSuchAlgorithmException, CertificateException {
236
stream.write(0);
237
try {
238
ks.store(stream, password);
239
} catch (KeyStoreException e) {
240
throw new RuntimeException(e);
241
}
242
}
243
244
@Override
245
public void engineLoad(InputStream stream, char[] password)
246
throws IOException, NoSuchAlgorithmException, CertificateException {
247
if (stream != null) {
248
stream.read();
249
}
250
ks.load(stream, password);
251
}
252
253
@Override
254
public boolean engineProbe(InputStream stream) {
255
return false;
256
}
257
}
258
}
259
260