Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/tools/keytool/GenKeyPairSigner.java
41152 views
1
/*
2
* Copyright (c) 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 8260693
27
* @summary Test for keytool -genkeypair with -signer and -signerkeypass options
28
* @library /test/lib
29
* @modules java.base/sun.security.util
30
*/
31
32
import jdk.test.lib.SecurityTools;
33
import jdk.test.lib.process.OutputAnalyzer;
34
35
import java.io.*;
36
import java.security.cert.Certificate;
37
import java.security.cert.X509Certificate;
38
import java.security.KeyStore;
39
import java.security.PublicKey;
40
import java.util.Arrays;
41
import sun.security.util.DerValue;
42
import sun.security.util.KeyUtil;
43
import sun.security.util.KnownOIDs;
44
import static sun.security.util.KnownOIDs.*;
45
46
public class GenKeyPairSigner {
47
48
static OutputAnalyzer kt(String cmd, String ks) throws Exception {
49
return SecurityTools.keytool("-storepass changeit " + cmd +
50
" -keystore " + ks);
51
}
52
53
static OutputAnalyzer ktjks(String cmd, String ks, String kpass) throws Exception {
54
return SecurityTools.keytool("-storepass changeit " + cmd +
55
" -keystore " + ks + " -storetype jks" + " -keypass " +
56
kpass);
57
}
58
59
public static void main(String[] args) throws Exception {
60
testSignerPKCS12();
61
testSignerJKS();
62
testSignerOpt();
63
}
64
65
static void testSignerPKCS12() throws Exception {
66
KeyStore kstore;
67
X509Certificate cert;
68
String sigName, pKeyAlg;
69
PublicKey pKey;
70
int keyLen;
71
72
/*
73
* The signer alias is stored in the PKCS12 keystore
74
*/
75
System.out.println("Testing the signer alias that is stored in the PKCS12 keystore");
76
System.out.println("Generating a root cert with SubjectKeyIdentifier extension");
77
SecurityTools.keytool("-keystore ks -storepass changeit " +
78
"-genkeypair -keyalg EdDSA -alias ca -dname CN=CA -ext bc:c " +
79
"-ext 2.5.29.14=04:14:00:01:02:03:04:05:06:07:08:09:10:11:12:13:14:15:16:17:18:19")
80
.shouldContain("Generating 255 bit Ed25519 key pair and self-signed certificate (Ed25519) with a validity of 90 days")
81
.shouldContain("for: CN=CA")
82
.shouldHaveExitValue(0);
83
84
System.out.println("Generating an XDH cert with -signer option");
85
SecurityTools.keytool("-keystore ks -storepass changeit " +
86
"-genkeypair -keyalg XDH -alias e1 -dname CN=E1 -signer ca")
87
.shouldContain("Generating 255 bit XDH key pair and a certificate (Ed25519) issued by <ca> with a validity of 90 days")
88
.shouldContain("for: CN=E1")
89
.shouldHaveExitValue(0);
90
91
// examine the resulting cert
92
kstore = KeyStore.getInstance(new File("ks"), "changeit".toCharArray());
93
cert = (X509Certificate)kstore.getCertificate("e1");
94
95
Certificate[] certChain = kstore.getCertificateChain("e1");
96
if (certChain.length != 2) {
97
throw new Exception("Generated cert chain is in error");
98
}
99
100
sigName = cert.getSigAlgName();
101
if (sigName != "Ed25519") {
102
throw new Exception("Signature algorithm name is in error");
103
}
104
105
pKey = cert.getPublicKey();
106
keyLen = KeyUtil.getKeySize(pKey);
107
if (keyLen != 255) {
108
throw new Exception("Key size is in error");
109
}
110
111
pKeyAlg = pKey.getAlgorithm();
112
if (pKeyAlg != "XDH") {
113
throw new Exception("Subject Public Key Algorithm is in error");
114
}
115
116
SecurityTools.keytool("-keystore ks -storepass changeit " +
117
"-list -v")
118
.shouldContain("Alias name: e1")
119
.shouldContain("Certificate chain length: 2")
120
.shouldContain("Signature algorithm name: Ed25519")
121
.shouldContain("Subject Public Key Algorithm: 255-bit XDH key")
122
.shouldHaveExitValue(0);
123
124
// check to make sure that cert's AKID is created from the SKID of the signing cert
125
byte[] expectedId = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
126
0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19};
127
128
byte[] authorityKeyIdExt = cert.getExtensionValue(
129
KnownOIDs.AuthorityKeyID.value());
130
131
byte[] authorityKeyId = null;
132
if (authorityKeyIdExt == null) {
133
throw new Exception("Failed to get AKID extension from the cert");
134
} else {
135
try {
136
authorityKeyId = new DerValue(authorityKeyIdExt).getOctetString();
137
} catch (IOException e) {
138
throw new Exception("Failed to get AKID encoded OctetString in the cert");
139
}
140
}
141
142
authorityKeyId = Arrays.copyOfRange(authorityKeyId, 4, authorityKeyId.length);
143
if (!Arrays.equals(authorityKeyId, expectedId)) {
144
throw new Exception("Failed due to AKID mismatch");
145
}
146
147
kt("-genkeypair -keyalg RSA -alias ca2 -dname CN=CA2 -ext bc:c ",
148
"ks");
149
150
System.out.println("Generating an X448 cert with -signer option");
151
SecurityTools.keytool("-keystore ks -storepass changeit " +
152
"-genkeypair -keyalg X448 -alias e2 -dname CN=E2 -sigalg SHA384withRSA -signer ca2")
153
.shouldContain("Generating 448 bit XDH key pair and a certificate (SHA384withRSA) issued by <ca2> with a validity of 90 days")
154
.shouldContain("for: CN=E2")
155
.shouldHaveExitValue(0);
156
157
// examine the resulting cert
158
kstore = KeyStore.getInstance(new File("ks"), "changeit".toCharArray());
159
cert = (X509Certificate)kstore.getCertificate("e2");
160
sigName = cert.getSigAlgName();
161
if (sigName != "SHA384withRSA") {
162
throw new Exception("Signature algorithm name is in error");
163
}
164
165
pKey = cert.getPublicKey();
166
keyLen = KeyUtil.getKeySize(pKey);
167
if (keyLen != 448) {
168
throw new Exception("Key size is in error");
169
}
170
171
pKeyAlg = pKey.getAlgorithm();
172
if (pKeyAlg != "XDH") {
173
throw new Exception("Subject Public Key Algorithm is in error");
174
}
175
176
SecurityTools.keytool("-keystore ks -storepass changeit " +
177
"-list -v")
178
.shouldContain("Alias name: e2")
179
.shouldContain("Signature algorithm name: SHA384withRSA")
180
.shouldContain("Subject Public Key Algorithm: 448-bit XDH key")
181
.shouldHaveExitValue(0);
182
183
kt("-genkeypair -keyalg DSA -alias ca3 -dname CN=CA3 -ext bc:c ",
184
"ks");
185
186
System.out.println("Generating a DH cert with -signer option");
187
SecurityTools.keytool("-keystore ks -storepass changeit " +
188
"-genkeypair -keyalg DH -alias e3 -dname CN=E3 -signer ca3")
189
.shouldContain("Generating 2,048 bit DH key pair and a certificate (SHA256withDSA) issued by <ca3> with a validity of 90 days")
190
.shouldContain("for: CN=E3")
191
.shouldHaveExitValue(0);
192
193
// examine the resulting cert
194
kstore = KeyStore.getInstance(new File("ks"), "changeit".toCharArray());
195
cert = (X509Certificate)kstore.getCertificate("e3");
196
sigName = cert.getSigAlgName();
197
if (sigName != "SHA256withDSA") {
198
throw new Exception("Signature algorithm name is in error");
199
}
200
201
pKey = cert.getPublicKey();
202
keyLen = KeyUtil.getKeySize(pKey);
203
if (keyLen != 2048) {
204
throw new Exception("Key size is in error");
205
}
206
207
pKeyAlg = pKey.getAlgorithm();
208
if (pKeyAlg != "DH") {
209
throw new Exception("Subject Public Key Algorithm is in error");
210
}
211
212
SecurityTools.keytool("-keystore ks -storepass changeit " +
213
"-list -v")
214
.shouldContain("Alias name: e3")
215
.shouldContain("Signature algorithm name: SHA256withRSA")
216
.shouldContain("Subject Public Key Algorithm: 2048-bit DH key")
217
.shouldHaveExitValue(0);
218
}
219
220
static void testSignerJKS() throws Exception {
221
KeyStore kstore;
222
X509Certificate cert;
223
String sigName, pKeyAlg;
224
PublicKey pKey;
225
int keyLen;
226
227
/*
228
* The signer alias is stored in the JKS keystore
229
* Using JKS keystore here is to test the scenario when the private key
230
* of the signer entry is protected by a password different from the
231
* store password, and -signerkeypass option needs to be specified
232
* along with -signer option.
233
*/
234
System.out.println("Testing the signer alias that is stored in the JKS keystore");
235
ktjks("-genkeypair -keyalg RSA -keysize 1024 -alias ca -dname CN=CA -ext bc:c",
236
"ksjks", "cakeypass");
237
238
System.out.println("Generating an DSA cert with -signer and -signerkeypass options");
239
SecurityTools.keytool("-keystore ksjks -storepass changeit -storetype jks " +
240
"-genkeypair -keyalg DSA -keysize 1024 -alias ca1 -dname CN=CA1 " +
241
"-keypass ca1keypass -signer ca -signerkeypass cakeypass")
242
.shouldContain("Generating 1,024 bit DSA key pair and a certificate (SHA256withRSA) issued by <ca> with a validity of 90 days")
243
.shouldContain("for: CN=CA1")
244
.shouldContain("The generated certificate #1 of 2 uses a 1024-bit DSA key which is considered a security risk")
245
.shouldContain("The generated certificate #2 of 2 uses a 1024-bit RSA key which is considered a security risk")
246
.shouldHaveExitValue(0);
247
248
System.out.println("Generating an XDH cert with -signer and -signerkeypass options");
249
SecurityTools.keytool("-keystore ksjks -storepass changeit -storetype jks " +
250
"-genkeypair -keyalg XDH -alias e1 -dname CN=E1 " +
251
"-keypass e1keypass -signer ca1 -signerkeypass ca1keypass")
252
.shouldContain("Generating 255 bit XDH key pair and a certificate (SHA256withDSA) issued by <ca1> with a validity of 90 days")
253
.shouldContain("for: CN=E1")
254
.shouldContain("The generated certificate #2 of 3 uses a 1024-bit DSA key which is considered a security risk")
255
.shouldContain("The generated certificate #3 of 3 uses a 1024-bit RSA key which is considered a security risk")
256
.shouldHaveExitValue(0);
257
258
// examine the resulting cert
259
kstore = KeyStore.getInstance(new File("ksjks"), "changeit".toCharArray());
260
cert = (X509Certificate)kstore.getCertificate("e1");
261
262
Certificate[] certChain = kstore.getCertificateChain("e1");
263
if (certChain.length != 3) {
264
throw new Exception("Generated cert chain is in error");
265
}
266
267
sigName = cert.getSigAlgName();
268
if (sigName != "SHA256withDSA") {
269
throw new Exception("Signature algorithm name is in error");
270
}
271
272
pKey = cert.getPublicKey();
273
keyLen = KeyUtil.getKeySize(pKey);
274
if (keyLen != 255) {
275
throw new Exception("Key size is in error");
276
}
277
278
pKeyAlg = pKey.getAlgorithm();
279
if (pKeyAlg != "XDH") {
280
throw new Exception("Subject Public Key Algorithm is in error");
281
}
282
283
SecurityTools.keytool("-keystore ksjks -storepass changeit " +
284
"-list -v")
285
.shouldContain("Alias name: e1")
286
.shouldContain("Certificate chain length: 3")
287
.shouldContain("Signature algorithm name: SHA256withDSA")
288
.shouldContain("Subject Public Key Algorithm: 255-bit XDH key")
289
.shouldHaveExitValue(0);
290
}
291
292
static void testSignerOpt() throws Exception {
293
294
SecurityTools.keytool("-keystore ks -storepass changeit " +
295
"-genkeypair -keyalg X25519 -alias e4 -dname CN=E4")
296
.shouldContain("Cannot derive signature algorithm from XDH")
297
.shouldHaveExitValue(1);
298
299
SecurityTools.keytool("-keystore ks -storepass changeit " +
300
"-genkeypair -keyalg X448 -alias e4 -dname CN=E4 -signer noca")
301
.shouldContain("Alias <noca> does not exist")
302
.shouldHaveExitValue(1);
303
304
SecurityTools.keytool("-genkeypair --help")
305
.shouldContain("-signer <alias> signer alias")
306
.shouldContain("-signerkeypass <arg> signer key password")
307
.shouldHaveExitValue(0);
308
}
309
}
310
311