Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/tools/keytool/CheckCertAKID.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 8257497
27
* @summary Check if issuer's SKID is used to establish the AKID for the subject cert
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.KeyStore;
37
import java.security.cert.X509Certificate;
38
import java.util.Arrays;
39
import sun.security.util.DerValue;
40
import sun.security.util.KnownOIDs;
41
import static sun.security.util.KnownOIDs.*;
42
43
public class CheckCertAKID {
44
45
static OutputAnalyzer kt(String cmd, String ks) throws Exception {
46
return SecurityTools.keytool("-storepass changeit " + cmd +
47
" -keystore " + ks);
48
}
49
50
public static void main(String[] args) throws Exception {
51
52
System.out.println("Generating a root cert with SubjectKeyIdentifier extension");
53
kt("-genkeypair -keyalg rsa -alias ca -dname CN=CA -ext bc:c " +
54
"-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",
55
"ks");
56
57
kt("-exportcert -alias ca -rfc -file root.cert", "ks");
58
59
SecurityTools.keytool("-keystore ks -storepass changeit " +
60
"-printcert -file root.cert")
61
.shouldNotContain("AuthorityKeyIdentifier")
62
.shouldContain("SubjectKeyIdentifier")
63
.shouldContain("0000: 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15")
64
.shouldContain("0010: 16 17 18 19")
65
.shouldHaveExitValue(0);
66
67
System.out.println("Generating an end entity cert using issuer CA's SKID as its AKID");
68
kt("-genkeypair -keyalg rsa -alias e1 -dname CN=E1", "ks");
69
kt("-certreq -alias e1 -file tmp.req", "ks");
70
kt("-gencert -alias ca -ext san=dns:e1 -infile tmp.req -outfile tmp.cert ",
71
"ks");
72
kt("-importcert -alias e1 -file tmp.cert", "ks");
73
74
byte[] expectedId = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
75
0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19};
76
77
KeyStore kstore = KeyStore.getInstance(new File("ks"),
78
"changeit".toCharArray());
79
X509Certificate cert = (X509Certificate)kstore.getCertificate("e1");
80
byte[] authorityKeyIdExt = cert.getExtensionValue(
81
KnownOIDs.AuthorityKeyID.value());
82
83
byte[] authorityKeyId = null;
84
if (authorityKeyIdExt == null) {
85
System.out.println("Failed to get AKID extension from the cert");
86
System.exit(1);
87
} else {
88
try {
89
authorityKeyId = new DerValue(authorityKeyIdExt).getOctetString();
90
} catch (IOException e) {
91
System.out.println("Failed to get AKID encoded OctetString in the cert");
92
System.exit(1);
93
}
94
}
95
96
authorityKeyId = Arrays.copyOfRange(authorityKeyId, 4, authorityKeyId.length);
97
if (!Arrays.equals(authorityKeyId, expectedId)) {
98
System.out.println("Failed due to AKID mismatch");
99
System.exit(1);
100
}
101
}
102
}
103
104