-
Notifications
You must be signed in to change notification settings - Fork 70
/
KeyToolTest.java
376 lines (295 loc) · 12.3 KB
/
KeyToolTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
* Copyright (C) 2023, THL A29 Limited, a Tencent company. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package com.tencent.kona.pkix.tool;
import com.tencent.kona.pkix.PKIXUtils;
import com.tencent.kona.pkix.TestUtils;
import com.tencent.kona.sun.security.ec.ECOperator;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.tencent.kona.sun.security.tools.keytool.Main;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.PublicKey;
import java.security.cert.X509Certificate;
import java.security.interfaces.ECPublicKey;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* The tests for KeyTool.
*/
public class KeyToolTest {
private static final Path BASE_DIR = Paths.get("build", "KeyToolTest");
private static final Path KEYSTORE = path("keystore.ks");
private static final Path ROOT_KEYSTORE = path("root.ks");
private static final Path CA_KEYSTORE = path("ca.ks");
private static final Path EE_KEYSTORE = path("ee.ks");
private static final String PASSWORD = "testpassword";
@BeforeAll
public static void setup() {
TestUtils.addProviders();
}
@BeforeEach
public void prepare() throws IOException {
TestUtils.deleteDirIfExists(BASE_DIR);
Files.createDirectory(BASE_DIR);
}
@AfterEach
public void clean() throws IOException {
TestUtils.deleteDirIfExists(BASE_DIR);
}
@Test
public void testGenSelfSignedCertOnPKCS12() throws Throwable {
testGenSelfSignedCertr("PKCS12");
}
@Test
public void testGenSelfSignedCertrOnJKS() throws Throwable {
testGenSelfSignedCertr("JKS");
}
private void testGenSelfSignedCertr(String storeType) throws Throwable {
genKeyPair(KEYSTORE, storeType, "rsa", "RSA", null, "SHA256withRSA");
genKeyPair(KEYSTORE, storeType, "ec-secp256r1", "EC", "SECP256R1", "SHA256withECDSA");
genKeyPair(KEYSTORE, storeType, "ec-sm2", "EC", "curveSM2", "SM3withSM2");
outputCert(KEYSTORE, storeType, "rsa", null);
outputCert(KEYSTORE, storeType, "ec-secp256r1", null);
outputCert(KEYSTORE, storeType, "ec-sm2", null);
}
@Test
public void testGenCSROnPKCS12() throws Throwable {
testGenCSR("PKCS12");
}
@Test
public void testGenCSROnJKS() throws Throwable {
testGenCSR("JKS");
}
private void testGenCSR(String storeType) throws Throwable {
genKeyPair(ROOT_KEYSTORE, storeType, "rsa-root", "RSA", null, "SHA256withRSA");
genKeyPair(ROOT_KEYSTORE, storeType, "ec-secp256r1-root", "EC", "SECP256R1", "SHA256withECDSA");
genKeyPair(ROOT_KEYSTORE, storeType, "ec-sm2-root", "EC", "curveSM2", "SM3withSM2");
genCSR(ROOT_KEYSTORE, storeType, "rsa-root", "SHA256withRSA", path("rsa-root.csr"));
genCSR(ROOT_KEYSTORE, storeType, "ec-secp256r1-root", "SHA256withECDSA", path("ec-secp256r1-root.csr"));
genCSR(ROOT_KEYSTORE, storeType, "ec-sm2-root", "SM3withSM2", path("ec-sm2-root.csr"));
}
@Test
public void testGenCertChainOnPKCS12() throws Throwable {
testGenCertChain("PKCS12");
}
@Test
public void testGenCertChainOnJKS() throws Throwable {
testGenCertChain("JKS");
}
@Test
public void testPBEAlgorithmOnPKCS12() throws Throwable {
genKeyPair(path("SM3AndSM4.ks"), "PKCS12", "SM3AndSM4",
"EC", "curveSM2", "SM3withSM2",
"PBEWithHmacSM3AndSM4", "PBEWithHmacSM3AndSM4", "HmacPBESM3");
genKeyPair(path("SM3AndSM4-SHA256AndAES.ks"), "PKCS12",
"SM3AndSM4-SHA256AndAES", "EC", "curveSM2", "SM3withSM2",
"PBEWithHmacSM3AndSM4", "PBEWithHmacSHA256AndAES_256", "HmacPBESHA256");
genKeyPair(path("SHA256AndAES.ks"), "PKCS12", "SHA256AndAES",
"EC", "curveSM2", "SM3withSM2",
"PBEWithHmacSHA256AndAES_256", "PBEWithHmacSHA256AndAES_256", "HmacPBESM3");
genKeyPair(path("SHA256AndAES-SM3AndSM4.ks"), "PKCS12",
"SHA256AndAES-SM3AndSM4", "EC", "curveSM2", "SM3withSM2",
"PBEWithHmacSHA256AndAES_256", "PBEWithHmacSM3AndSM4", "HmacPBESM3");
}
private void testGenCertChain(String storeType) throws Throwable {
genCertChain(storeType, "RSA", null, "SHA256withRSA");
genCertChain(storeType, "EC", "SECP256R1", "SHA256withECDSA");
genCertChain(storeType, "EC", "curveSM2", "SM3withSM2");
}
private void genCertChain(String storeType, String keyAlg, String group,
String sigAlg) throws Throwable {
String suffix = suffix(keyAlg, group, sigAlg);
String rootAlias = "root-" + suffix;
String caAlias = "ca-" + suffix;
String eeAlias = "ee-" + suffix;
Path caCSRPath = path(caAlias + ".csr");
Path eeCSRPath = path(eeAlias + ".csr");
genKeyPair(ROOT_KEYSTORE, storeType, rootAlias, keyAlg, group, sigAlg);
genKeyPair(CA_KEYSTORE, storeType, caAlias, keyAlg, group, sigAlg);
genKeyPair(EE_KEYSTORE, storeType, eeAlias, keyAlg, group, sigAlg);
outputCert(ROOT_KEYSTORE, storeType, rootAlias, path(rootAlias + ".crt"));
genCSR(CA_KEYSTORE, storeType, caAlias, sigAlg, caCSRPath);
Path caCertPath = path(caAlias + ".crt");
genCert(ROOT_KEYSTORE, storeType, rootAlias, sigAlg, caCSRPath, caCertPath);
checkCert(caCertPath, keyAlg, group, sigAlg);
genCSR(EE_KEYSTORE, storeType, eeAlias, sigAlg, eeCSRPath);
Path eeCertPath = path(eeAlias + ".crt");
genCert(CA_KEYSTORE, storeType, caAlias, sigAlg, eeCSRPath, eeCertPath);
checkCert(eeCertPath, keyAlg, group, sigAlg);
}
private static String suffix(String keyAlg, String group, String sigAlg) {
StringBuilder suffix = new StringBuilder();
suffix.append(keyAlg);
if (group != null) {
suffix.append("-").append(group);
}
suffix.append("-").append(sigAlg);
return suffix.toString();
}
private static void outputCert(Path keystore, String storeType,
String alias, Path certPath) throws Throwable {
List<String> args = new ArrayList<>();
args.add("-v");
args.add("-exportcert");
args.add("-rfc");
args.add("-keystore");
args.add(keystore.toString());
args.add("-storetype");
args.add(storeType);
args.add("-storepass");
args.add(PASSWORD);
args.add("-alias");
args.add(alias);
if (certPath != null) {
args.add("-file");
args.add(certPath.toString());
}
System.out.println("outputCert: " + String.join(" ", args));
Main.main(args.toArray(new String[0]));
}
private static void genKeyPair(Path keystorePath, String storeType,
String alias, String keyAlg, String group, String sigAlg)
throws Throwable {
genKeyPair(keystorePath, storeType, alias, keyAlg, group, sigAlg,
null, null, null);
}
private static void genKeyPair(Path keystorePath, String storeType,
String alias, String keyAlg, String group, String sigAlg,
String certPbeAlg, String keyPbeAlg, String macAlg)
throws Throwable {
List<String> args = new ArrayList<>();
args.add("-v");
args.add("-genkeypair");
args.add("-keystore");
args.add(keystorePath.toString());
args.add("-storetype");
args.add(storeType);
args.add("-storepass");
args.add(PASSWORD);
args.add("-alias");
args.add(alias);
args.add("-keyalg");
args.add(keyAlg);
args.add("-keypass");
args.add(PASSWORD);
if (group != null) {
args.add("-groupname");
args.add(group);
}
args.add("-sigalg");
args.add(sigAlg);
args.add("-dname");
args.add("CN=" + alias);
List<String> jvmOptions = new ArrayList<>();
if (certPbeAlg != null) {
jvmOptions.add(
"-Dcom.tencent.kona.keystore.pkcs12.certPbeAlgorithm=" + certPbeAlg);
}
if (keyPbeAlg != null) {
jvmOptions.add(
"-Dcom.tencent.kona.keystore.pkcs12.keyPbeAlgorithm=" + keyPbeAlg);
}
if (macAlg != null) {
jvmOptions.add(
"-Dcom.tencent.kona.keystore.pkcs12.macAlgorithm=" + macAlg);
}
System.out.println("genKeyPair: " + String.join(" ", args));
Main.main(args.toArray(new String[0]));
}
private static void genCSR(Path keystorePath, String storeType,
String alias, String sigAlg, Path csrPath) throws Throwable {
List<String> args = new ArrayList<>();
args.add("-v");
args.add("-certreq");
args.add("-keystore");
args.add(keystorePath.toString());
args.add("-storetype");
args.add(storeType);
args.add("-storepass");
args.add(PASSWORD);
args.add("-alias");
args.add(alias);
args.add("-keypass");
args.add(PASSWORD);
args.add("-sigalg");
args.add(sigAlg);
if (csrPath != null) {
args.add("-file");
args.add(csrPath.toString());
}
System.out.println("genCSR: " + String.join(" ", args));
Main.main(args.toArray(new String[0]));
}
private static void genCert(Path keystorePath, String storeType,
String issuerAlias, String sigAlg , Path csrPath, Path certPath)
throws Throwable {
List<String> args = new ArrayList<>();
args.add("-v");
args.add("-gencert");
args.add("-rfc");
args.add("-keystore");
args.add(keystorePath.toString());
args.add("-storetype");
args.add(storeType);
args.add("-storepass");
args.add(PASSWORD);
args.add("-alias");
args.add(issuerAlias);
args.add("-keypass");
args.add(PASSWORD);
args.add("-sigalg");
args.add(sigAlg);
args.add("-infile");
args.add(csrPath.toString());
if (certPath != null) {
args.add("-outfile");
args.add(certPath.toString());
}
System.out.println("genCert: " + String.join(" ", args));
Main.main(args.toArray(new String[0]));
}
private static void checkCert(Path certPath, String expectedKeyAlg,
String expectedGroup, String expectedSigAlg) throws Exception {
X509Certificate cert = PKIXUtils.getCertificate(String.join(
"\n", Files.readAllLines(certPath)));
PublicKey publicKey = cert.getPublicKey();
assertEquals(expectedKeyAlg, publicKey.getAlgorithm());
if (expectedGroup != null) {
ECPublicKey ecPublicKey = (ECPublicKey) publicKey;
BigInteger expectedOrder = null;
if ("CURVESM2".equalsIgnoreCase(expectedGroup)) {
expectedOrder = ECOperator.SM2.getOrder();
} else if ("SECP256R1".equalsIgnoreCase(expectedGroup)) {
expectedOrder = ECOperator.SECP256R1.getOrder();
}
assertEquals(expectedOrder, ecPublicKey.getParams().getOrder());
}
assertEquals(expectedSigAlg, cert.getSigAlgName());
}
private static Path path(String file) {
return BASE_DIR.resolve(Paths.get(file));
}
}