Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/tools/jarsigner/CheckUsage.java
41152 views
1
/*
2
* Copyright (c) 2010, 2019, 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 7004168
27
* @summary jarsigner -verify checks for KeyUsage codesigning ext on all certs
28
* instead of just signing cert
29
* @library /test/lib
30
*/
31
32
import jdk.test.lib.SecurityTools;
33
import jdk.test.lib.process.OutputAnalyzer;
34
import jdk.test.lib.util.JarUtils;
35
36
import java.nio.file.Files;
37
import java.nio.file.Path;
38
import java.util.List;
39
40
public class CheckUsage {
41
42
static OutputAnalyzer keytool(String cmd) throws Exception {
43
return SecurityTools.keytool("-keypass changeit -storepass changeit "
44
+ "-keyalg rsa " + cmd);
45
}
46
47
public static void main(String[] args) throws Exception {
48
Files.write(Path.of("x"), List.of("x"));
49
JarUtils.createJarFile(Path.of("a.jar"), Path.of("."), Path.of("x"));
50
51
// ################### 3 Keystores #######################
52
53
// Keystore js.jks: including CA and Publisher
54
// CA contains a non-empty KeyUsage
55
keytool("-keystore js.jks -genkeypair -alias ca -dname CN=CA "
56
+ "-ext KU=kCS -ext bc -validity 365");
57
keytool("-keystore js.jks -genkeypair -alias pub -dname CN=Publisher");
58
59
// Publisher contains the correct KeyUsage
60
keytool("-keystore js.jks -certreq -alias pub -file pub.req");
61
keytool("-keystore js.jks -gencert -alias ca -ext KU=dig -validity 365 "
62
+ "-infile pub.req -outfile pub.cert");
63
keytool("-keystore js.jks -importcert -alias pub -file pub.cert");
64
65
// Keystore trust.jks: including CA only
66
keytool("-keystore js.jks -exportcert -alias ca -file ca.cert");
67
keytool("-keystore trust.jks -importcert -alias ca -noprompt -file ca.cert");
68
69
// Keystore unrelated.jks: unrelated
70
keytool("-keystore unrelated.jks -genkeypair -alias nothing "
71
+ "-dname CN=Nothing -validity 365");
72
73
// ################### 4 Tests #######################
74
75
// Test 1: Sign should be OK
76
77
SecurityTools.jarsigner("-keystore js.jks -storepass changeit a.jar pub")
78
.shouldHaveExitValue(0);
79
80
// Test 2: Verify should be OK
81
82
SecurityTools.jarsigner("-keystore trust.jks -storepass changeit "
83
+ "-strict -verify a.jar")
84
.shouldHaveExitValue(0);
85
86
// Test 3: When no keystore is specified, the error is only
87
// "chain invalid"
88
89
SecurityTools.jarsigner("-strict -verify a.jar")
90
.shouldHaveExitValue(4);
91
92
// Test 4: When unrelated keystore is specified, the error is
93
// "chain invalid" and "not alias in keystore"
94
95
SecurityTools.jarsigner("-keystore unrelated.jks -storepass changeit "
96
+ "-strict -verify a.jar")
97
.shouldHaveExitValue(36);
98
}
99
}
100
101