Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/tools/keytool/ExtOptionCamelCase.java
41152 views
1
/*
2
* Copyright (c) 2019, 2021, 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 8231950 8257497
27
* @summary keytool -ext camel-case shorthand not working
28
* @modules java.base/sun.security.tools.keytool
29
* java.base/sun.security.tools.keytool:open
30
* java.base/sun.security.util
31
* java.base/sun.security.x509
32
* @compile -XDignore.symbol.file ExtOptionCamelCase.java
33
* @run main ExtOptionCamelCase
34
*/
35
36
import sun.security.tools.keytool.Main;
37
import sun.security.util.DerValue;
38
import sun.security.x509.BasicConstraintsExtension;
39
import sun.security.x509.CertificateExtensions;
40
import sun.security.x509.Extension;
41
import sun.security.x509.KeyIdentifier;
42
import sun.security.x509.KeyUsageExtension;
43
44
import java.io.ByteArrayOutputStream;
45
import java.lang.reflect.Constructor;
46
import java.lang.reflect.InvocationTargetException;
47
import java.lang.reflect.Method;
48
import java.security.KeyPairGenerator;
49
import java.security.PublicKey;
50
import java.util.List;
51
52
public class ExtOptionCamelCase {
53
static Method createV3Extensions;
54
static Constructor<Main> ctor;
55
static PublicKey pk;
56
static Method oneOf;
57
58
public static void main(String[] args) throws Exception {
59
60
prepare();
61
62
// Unseen ext name
63
testCreateFail("abc");
64
65
// camelCase match, both cases work
66
testCreate("bc", BasicConstraintsExtension.class);
67
testCreate("BC", BasicConstraintsExtension.class);
68
69
// Prefix match
70
testCreate("BasicC", BasicConstraintsExtension.class);
71
72
// Ambiguous, digitalSignature or dataEncipherment?
73
testCreateFail("ku=d");
74
75
// prefix match
76
testCreate("ku:c=dig", KeyUsageExtension.class,
77
x -> x.get(KeyUsageExtension.DIGITAL_SIGNATURE));
78
79
// camelCase match
80
testCreate("ku=kE", KeyUsageExtension.class,
81
x -> x.get(KeyUsageExtension.KEY_ENCIPHERMENT));
82
83
// camelCase match must be only 1st+CAPITALs
84
testCreateFail("ku=KeUs");
85
86
// camelCase match, must be only 1st + all CAPITALs
87
testCreate("ku=kCS", KeyUsageExtension.class,
88
x -> x.get(KeyUsageExtension.KEY_CERTSIGN));
89
90
// ... not all CAPITALs
91
testCreateFail("ku=kC");
92
93
// ... has lowercase letters
94
testCreateFail("ku=keCeSi");
95
96
// Ambiguous, keyAgreement or keyCertSign
97
testCreateFail("ku:c=ke");
98
99
// camelCase natch
100
testCreate("ku:c=dE", KeyUsageExtension.class,
101
x -> x.get(KeyUsageExtension.DATA_ENCIPHERMENT));
102
// prefix match
103
testCreate("ku:c=de", KeyUsageExtension.class,
104
x -> x.get(KeyUsageExtension.DECIPHER_ONLY));
105
106
// camelCase match
107
testCreate("ku:c=kA", KeyUsageExtension.class,
108
x -> x.get(KeyUsageExtension.KEY_AGREEMENT));
109
110
// camelCase match, fallback
111
testCreate("ku:c=ka", KeyUsageExtension.class,
112
x -> x.get(KeyUsageExtension.KEY_AGREEMENT));
113
114
// Testing oneOf() directly
115
testOneOf("a", -1, "b", "c"); // -1 means not found
116
testOneOf("a", -2, "ab", "ac"); // -2 means ambiguous
117
118
testOneOf("a", 0, "a", "ac"); //exact match
119
testOneOf("a", 0, "a", "b");
120
testOneOf("ac", 1, "a", "ac");
121
122
testOneOf("a", 0, "abc", "bcd");
123
testOneOf("ab", 0, "abc", "ABC");
124
testOneOf("ab", 0, "abc", "aBC");
125
testOneOf("ab", 0, "abc", "Abc");
126
testOneOf("AB", 1, "abc", "ABC");
127
testOneOf("aB", 0, "abcBcd", "abcDef");
128
testOneOf("ab", -2, "abcBcd", "abcDef");
129
testOneOf("aB", -2, "abcBcdEfg", "abcDef");
130
131
testOneOf("ab", 0, "abcDef", "axyBuv");
132
testOneOf("aB", 1, "abcDef", "axyBuv");
133
testOneOf("a", -2, "abcDef", "axyBuv");
134
135
testOneOf("aBC", -1, "a12BxyCuvDmn"); // 12 is not removed
136
testOneOf("a12BCD", 0, "a12BxyCuvDmn");
137
testOneOf("a12BC", -1, "a12BxyCuvDmn"); // must be full
138
139
// Fallback
140
testOneOf("bc", 0, "BasicConstraints");
141
testOneOf("BC", 0, "BasicConstraints");
142
testOneOf("BasicConstraints", 0, "BasicConstraints");
143
testOneOf("basicconstraints", 0, "BasicConstraints");
144
testOneOf("Basic", 0, "BasicConstraints");
145
testOneOf("basic", 0, "BasicConstraints");
146
147
testOneOf("BaCo", -1, "BasicConstraints");
148
}
149
150
// Expose some private methods
151
static void prepare() throws Exception {
152
createV3Extensions = Main.class.getDeclaredMethod(
153
"createV3Extensions",
154
CertificateExtensions.class,
155
CertificateExtensions.class,
156
List.class,
157
PublicKey.class,
158
KeyIdentifier.class);
159
createV3Extensions.setAccessible(true);
160
ctor = Main.class.getDeclaredConstructor();
161
ctor.setAccessible(true);
162
KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC");
163
pk = kpg.generateKeyPair().getPublic();
164
165
oneOf = Main.class.getDeclaredMethod(
166
"oneOf", String.class, String[].class);
167
oneOf.setAccessible(true);
168
}
169
170
/**
171
* Ensures the given type of extension is created with the option
172
*/
173
static <T extends Extension> void testCreate(String option, Class<T> clazz)
174
throws Exception {
175
testCreate(option, clazz, null);
176
}
177
178
/**
179
* Ensures an option is invalid and will be rejected
180
*/
181
static <T extends Extension> void testCreateFail(String option)
182
throws Exception {
183
testCreate(option, null, null);
184
}
185
186
/**
187
* Ensures the given type of extension is created and match the rule
188
* with the option.
189
*
190
* @param option the -ext option provided to keytool
191
* @param clazz the expected extension to create, null means none
192
* @param rule a predicate to check if the extension created is acceptable
193
* @param <T> the extected extension type
194
* @throws Exception if test result is unexpected
195
*/
196
static <T extends Extension> void testCreate(String option, Class<T> clazz,
197
PredicateWithException<T> rule) throws Exception {
198
try {
199
CertificateExtensions exts = (CertificateExtensions)
200
createV3Extensions.invoke(ctor.newInstance(),
201
null, null, List.of(option), pk, null);
202
203
// ATTENTION: the extensions created above might contain raw
204
// extensions (not of a subtype) and we need to store and reload
205
// it to resolve them to subtypes.
206
ByteArrayOutputStream bout = new ByteArrayOutputStream();
207
exts.encode(bout);
208
exts = new CertificateExtensions(new DerValue(bout.toByteArray()).data);
209
210
if (clazz == null) {
211
throw new Exception("Should fail");
212
} else {
213
for (Extension e : exts.getAllExtensions()) {
214
if (e.getClass() == clazz) {
215
if (rule == null || rule.test((T) e)) {
216
return;
217
}
218
}
219
}
220
throw new Exception("Unexpected result: " + exts);
221
}
222
} catch (InvocationTargetException e) {
223
if (clazz == null) {
224
return;
225
} else {
226
throw e;
227
}
228
}
229
}
230
231
@FunctionalInterface
232
interface PredicateWithException<T> {
233
boolean test(T t) throws Exception;
234
}
235
236
/**
237
* Ensures oneOf returns the expected result.
238
*
239
* @param s input
240
* @param expected expected value, -2 if ambiguous, -1 if no match
241
* @param items existing strings to match
242
* @throws Exception if test result is unexpected
243
*/
244
static void testOneOf(String s, int expected, String... items)
245
throws Exception {
246
try {
247
int res = (int)oneOf.invoke(null, s, items);
248
if (expected == -2) {
249
throw new Exception("Should fail");
250
} else {
251
if (expected != res) {
252
throw new Exception(
253
"Expected " + expected + ", actually " + res);
254
}
255
}
256
} catch (InvocationTargetException e) {
257
if (expected == -2) {
258
return;
259
} else {
260
throw e;
261
}
262
}
263
}
264
}
265
266