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