Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/tools/keytool/fakecacerts/TrustedCert.java
41154 views
1
/*
2
* Copyright (c) 2020, 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 8244148
27
* @summary Test keytool -printcert with -keystore and -trustcacerts options
28
* @library /test/lib
29
* @library /test/jdk/sun/security/util/module_patch
30
* @build java.base/sun.security.util.FilePaths
31
* @modules java.base/sun.security.util
32
* java.base/jdk.internal.misc
33
* @run main TrustedCert
34
*/
35
36
import jdk.test.lib.SecurityTools;
37
import jdk.test.lib.process.OutputAnalyzer;
38
import jdk.test.lib.security.KeyStoreUtils;
39
40
import java.io.ByteArrayOutputStream;
41
import java.io.File;
42
import java.io.IOException;
43
import java.nio.file.Files;
44
import java.nio.file.Paths;
45
46
public class TrustedCert {
47
48
// The --patch-module must be explicitly specified on the keytool
49
// command line because it's in a separate process
50
private static final String PATCH_OPTION;
51
52
static {
53
String tmp = "";
54
for (String a : jdk.internal.misc.VM.getRuntimeArguments()) {
55
if (a.startsWith("--patch-module")) {
56
tmp = "-J" + a + " ";
57
break;
58
}
59
}
60
PATCH_OPTION = tmp;
61
}
62
63
static OutputAnalyzer kt(String cmd, String ks) throws Exception {
64
return SecurityTools.keytool(cmd + " -keystore " + ks
65
+ " -storepass changeit")
66
.shouldHaveExitValue(0);
67
}
68
69
static OutputAnalyzer kt1(String cmd, String ks) throws Exception {
70
return SecurityTools.keytool(cmd + " -keystore " + ks
71
+ " -storepass changeit")
72
.shouldNotHaveExitValue(0);
73
}
74
75
static OutputAnalyzer patchcmd(String cmd, String options, String ks,
76
boolean nonzero) throws Exception {
77
if (nonzero) {
78
return kt1(PATCH_OPTION + " -" + cmd + " " + options, ks);
79
} else {
80
return kt(PATCH_OPTION + " -" + cmd + " " + options, ks);
81
}
82
}
83
84
static void rm(String s) throws IOException {
85
System.out.println("---------------------------------------------");
86
System.out.println("$ rm " + s);
87
Files.deleteIfExists(Paths.get(s));
88
}
89
90
private static void cat(String dest, String... src) throws IOException {
91
System.out.println("---------------------------------------------");
92
System.out.printf("$ cat ");
93
94
ByteArrayOutputStream bout = new ByteArrayOutputStream();
95
for (String s : src) {
96
System.out.printf(s + " ");
97
bout.write(Files.readAllBytes(Paths.get(s)));
98
}
99
Files.write(Paths.get(dest), bout.toByteArray());
100
System.out.println("> " + dest);
101
}
102
103
public static void main(String[] args) throws Exception {
104
105
// Test -printcert with root CA in local keystore
106
kt("-genkeypair -keyalg rsa -keysize 1024 -sigalg SHA1withRSA " +
107
"-dname CN=ROOT -ext bc:c", "root.jks");
108
kt("-genkeypair -keyalg RSA -dname CN=CA -ext bc:c", "ca.jks");
109
kt("-genkeypair -keyalg RSA -dname CN=SERVER", "server.jks");
110
111
kt("-exportcert -rfc -file root.pem", "root.jks");
112
kt("-importcert -alias root -file root.pem -noprompt", "ca.jks");
113
kt("-importcert -alias root -file root.pem -noprompt", "server.jks");
114
115
kt("-certreq -file ca.req", "ca.jks");
116
kt("-gencert -ext BC=0 -rfc -infile ca.req -outfile ca.pem", "root.jks");
117
kt("-importcert -file ca.pem", "ca.jks");
118
119
kt("-certreq -file server.req", "server.jks");
120
kt("-gencert -ext ku:c=dig,keyEncipherment -rfc -infile server.req " +
121
"-outfile server.pem", "ca.jks");
122
kt("-importcert -file server.pem", "server.jks");
123
124
cat("fullchain.pem", "server.pem", "root.pem");
125
kt("-printcert -file fullchain.pem ", "server.jks")
126
.shouldNotMatch("SHA1withRSA signature algorithm.*security risk")
127
.shouldMatch("1024-bit RSA key.*security risk");
128
129
rm("ca.jks");
130
rm("server.jks");
131
rm("mycacerts");
132
133
// Test -printcert with root CA in cacerts keystore
134
kt("-genkeypair -keyalg RSA -dname CN=CA -ext bc:c", "ca.jks");
135
kt("-genkeypair -keyalg RSA -dname CN=SERVER", "server.jks");
136
137
// import root CA to mycacerts keystore
138
KeyStoreUtils.createCacerts("mycacerts", "root.pem");
139
140
kt("-certreq -file ca.req", "ca.jks");
141
kt("-gencert -ext BC=0 -rfc -infile ca.req -outfile ca.pem", "root.jks");
142
143
patchcmd("importcert", "-file ca.pem", "ca.jks", true);
144
patchcmd("importcert", "-file ca.pem -trustcacerts", "ca.jks", false);
145
146
kt("-certreq -file server.req", "server.jks");
147
kt("-gencert -ext ku:c=dig,keyEncipherment -rfc -infile server.req " +
148
"-outfile server.pem", "ca.jks");
149
kt("-importcert -file server.pem -noprompt", "server.jks");
150
151
cat("fullchain.pem", "server.pem", "root.pem");
152
153
patchcmd("-printcert", "-file fullchain.pem -trustcacerts", "server.jks", false)
154
.shouldNotMatch("SHA1withRSA signature algorithm.*security risk")
155
.shouldMatch("1024-bit RSA key.*security risk");
156
157
patchcmd("-printcert", "-file fullchain.pem", "server.jks", false)
158
.shouldMatch("SHA1withRSA signature algorithm.*security risk")
159
.shouldMatch("1024-bit RSA key.*security risk");
160
}
161
}
162
163