Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/tools/jarsigner/CheckSignerCertChain.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 8259401 8266225
27
* @summary Check certificates in signer's cert chain to see if warning emitted
28
* @library /test/lib
29
*/
30
31
import jdk.test.lib.SecurityTools;
32
import jdk.test.lib.process.OutputAnalyzer;
33
import jdk.test.lib.util.JarUtils;
34
35
import java.nio.file.Files;
36
import java.nio.file.Path;
37
import java.nio.file.Paths;
38
39
public class CheckSignerCertChain {
40
41
private static final String JAVA_SECURITY_FILE = "java.security";
42
43
static OutputAnalyzer kt(String cmd, String ks) throws Exception {
44
return SecurityTools.keytool("-storepass changeit " + cmd +
45
" -keystore " + ks);
46
}
47
48
static void gencert(String owner, String cmd) throws Exception {
49
kt("-certreq -alias " + owner + " -file tmp.req", "ks");
50
kt("-gencert -infile tmp.req -outfile tmp.cert " + cmd, "ks");
51
kt("-importcert -alias " + owner + " -file tmp.cert", "ks");
52
}
53
54
public static void main(String[] args) throws Exception {
55
56
// root certificate using SHA1withRSA and 1024-bit key
57
System.out.println("Generating a root cert using SHA1withRSA and 1024-bit key");
58
kt("-genkeypair -keyalg rsa -alias ca -dname CN=CA -ext bc:c " +
59
"-keysize 1024 -sigalg SHA1withRSA", "ks");
60
kt("-genkeypair -keyalg rsa -alias ca1 -dname CN=CA1", "ks");
61
kt("-genkeypair -keyalg rsa -alias e1 -dname CN=E1", "ks");
62
63
// intermediate certificate using SHA1withRSA and 2048-bit key
64
System.out.println("Generating an intermediate cert using SHA1withRSA and 2048-bit key");
65
gencert("ca1", "-alias ca -ext san=dns:ca1 -ext bc:c " +
66
"-sigalg SHA1withRSA ");
67
68
// end entity certificate using SHA256withRSA and 2048-bit key
69
System.out.println("Generating an end entity cert using SHA256withRSA and 2048-bit key");
70
gencert("e1", "-alias ca1 -ext san=dns:e1 ");
71
72
JarUtils.createJarFile(Path.of("a.jar"), Path.of("."), Path.of("ks"));
73
74
SecurityTools.jarsigner("-keystore ks -storepass changeit " +
75
"-signedjar signeda.jar " +
76
"-sigalg SHA256withRSA " +
77
"-verbose" +
78
" a.jar e1")
79
.shouldContain("Signature algorithm: SHA1withRSA (weak), 2048-bit key")
80
// For trusted cert, warning should be generated for its weak 1024-bit
81
// key, but not for its SHA1withRSA algorithm.
82
.shouldContain("Signature algorithm: SHA1withRSA, 1024-bit key (weak)")
83
.shouldHaveExitValue(0);
84
85
kt("-exportcert -alias ca -rfc -file cacert", "ks");
86
kt("-importcert -noprompt -file cacert", "caks");
87
88
SecurityTools.jarsigner("-verify -certs signeda.jar " +
89
"-keystore caks -storepass changeit -verbose -debug")
90
.shouldContain("Signature algorithm: SHA1withRSA (weak), 2048-bit key")
91
// For trusted cert, warning should be generated for its weak 1024-bit
92
// key, but not for its SHA1withRSA algorithm.
93
.shouldContain("Signature algorithm: SHA1withRSA, 1024-bit key (weak)")
94
.shouldHaveExitValue(0);
95
96
/*
97
* Generate a non-self-signed certificate using MD5withRSA as its signature
98
* algorithm to sign a JAR file.
99
*/
100
kt("-genkeypair -keyalg rsa -alias cacert -dname CN=CACERT -ext bc:c ", "ks");
101
kt("-genkeypair -keyalg rsa -alias ee -dname CN=EE -ext bc:c ", "ks");
102
gencert("ee", "-alias cacert -ext san=dns:ee -sigalg MD5withRSA");
103
104
Files.writeString(Files.createFile(Paths.get(JAVA_SECURITY_FILE)),
105
"jdk.certpath.disabledAlgorithms=\n" +
106
"jdk.jar.disabledAlgorithms=MD5\n");
107
108
SecurityTools.jarsigner("-keystore ks -storepass changeit " +
109
"-signedjar signeda.jar " +
110
"-verbose " +
111
"-J-Djava.security.properties=" +
112
JAVA_SECURITY_FILE +
113
" a.jar ee")
114
.shouldNotContain("Signature algorithm: MD5withRSA (disabled), 2048-bit key")
115
.shouldContain("Signature algorithm: SHA256withRSA, 2048-bit key")
116
.shouldNotContain("Invalid certificate chain: Algorithm constraints check failed on signature algorithm: MD5withRSA")
117
.shouldHaveExitValue(0);
118
119
Files.deleteIfExists(Paths.get(JAVA_SECURITY_FILE));
120
Files.writeString(Files.createFile(Paths.get(JAVA_SECURITY_FILE)),
121
"jdk.certpath.disabledAlgorithms=MD5\n" +
122
"jdk.jar.disabledAlgorithms=\n");
123
124
SecurityTools.jarsigner("-keystore ks -storepass changeit " +
125
"-signedjar signeda.jar " +
126
"-verbose " +
127
"-J-Djava.security.properties=" +
128
JAVA_SECURITY_FILE +
129
" a.jar ee")
130
.shouldContain("Signature algorithm: MD5withRSA (disabled), 2048-bit key")
131
.shouldContain("Signature algorithm: SHA256withRSA, 2048-bit key")
132
.shouldContain("Invalid certificate chain: Algorithm constraints check failed on signature algorithm: MD5withRSA")
133
.shouldHaveExitValue(0);
134
135
kt("-exportcert -alias cacert -rfc -file cacert", "ks");
136
kt("-importcert -noprompt -file cacert", "caks1");
137
138
SecurityTools.jarsigner("-verify -certs signeda.jar " +
139
"-keystore caks1 -storepass changeit -verbose -debug")
140
.shouldContain("Signature algorithm: MD5withRSA (disabled), 2048-bit key")
141
.shouldContain("Signature algorithm: SHA256withRSA, 2048-bit key")
142
.shouldContain("Invalid certificate chain: Algorithm constraints check failed on signature algorithm: MD5withRSA")
143
.shouldHaveExitValue(0);
144
}
145
}
146
147