Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/security/Policy/SignedJar/SignedJarTest.java
41154 views
1
/*
2
* Copyright (c) 2015, 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
import java.io.File;
25
import java.nio.file.Files;
26
import java.nio.file.Paths;
27
import java.security.AccessControlException;
28
import java.security.AccessController;
29
import java.security.Permission;
30
import java.security.PrivilegedAction;
31
import jdk.test.lib.process.ProcessTools;
32
33
/**
34
* @test
35
* @bug 8048360 8242565
36
* @summary test policy entry with signedBy alias
37
* @library /test/lib
38
* @run main/othervm SignedJarTest
39
*/
40
public class SignedJarTest {
41
42
private static final String FS = File.separator;
43
private static final String JAVA_HOME = System.getProperty("test.jdk");
44
private static final String TESTCLASSES = System.getProperty("test.classes", "");
45
private static final String TESTSRC = System.getProperty("test.src", "");
46
private static final String KEYTOOL = JAVA_HOME + FS + "bin" + FS + "keytool";
47
private static final String JAR = JAVA_HOME + FS + "bin" + FS + "jar";
48
private static final String JARSIGNER = JAVA_HOME + FS + "bin" + FS + "jarsigner";
49
private static final String PASSWORD = "password";
50
private static final String PWDFILE = "keypass";
51
private static final String POLICY1 = "SignedJarTest_1.policy";
52
private static final String POLICY2 = "SignedJarTest_2.policy";
53
private static final String KEYSTORE1 = "both.jks";
54
private static final String KEYSTORE2 = "first.jks";
55
private static final String SECPROPS = TESTSRC + FS + "java.security";
56
57
public static void main(String args[]) throws Throwable {
58
//copy PrivilegeTest.class, policy files and keystore password file into current direcotry
59
Files.copy(Paths.get(TESTCLASSES, "PrivilegeTest.class"), Paths.get("PrivilegeTest.class"));
60
Files.copy(Paths.get(TESTSRC, POLICY1), Paths.get(POLICY1));
61
Files.copy(Paths.get(TESTSRC, POLICY2), Paths.get(POLICY2));
62
Files.copy(Paths.get(TESTSRC, PWDFILE), Paths.get(PWDFILE));
63
64
//create Jar file
65
ProcessTools.executeCommand(JAR, "-cvf", "test.jar", "PrivilegeTest.class");
66
67
//Creating first key , keystore both.jks
68
ProcessTools.executeCommand(KEYTOOL,
69
"-genkey",
70
"-keyalg", "DSA",
71
"-alias", "first",
72
"-keystore", KEYSTORE1,
73
"-keypass", PASSWORD,
74
"-dname", "cn=First",
75
"-storepass", PASSWORD
76
).shouldHaveExitValue(0);
77
78
//Creating Second key, keystore both.jks
79
ProcessTools.executeCommand(KEYTOOL,
80
"-genkey",
81
"-keyalg", "DSA",
82
// "-storetype","JKS",
83
"-alias", "second",
84
"-keystore", KEYSTORE1,
85
"-keypass", PASSWORD,
86
"-dname", "cn=Second",
87
"-storepass", PASSWORD
88
).shouldHaveExitValue(0);
89
90
//copy both.jks to first.jks, remove second Keypair from first.jks
91
Files.copy(Paths.get(KEYSTORE1), Paths.get(KEYSTORE2));
92
ProcessTools.executeCommand(KEYTOOL,
93
"-delete",
94
"-keystore", KEYSTORE2,
95
"-alias", "second",
96
"-storepass", PASSWORD
97
).shouldHaveExitValue(0);
98
99
//sign jar with first key, first.jar is only signed by first signer
100
ProcessTools.executeCommand(JARSIGNER,
101
"-keystore", KEYSTORE1,
102
"-storepass", PASSWORD,
103
"-keypass", PASSWORD,
104
"-signedjar", "first.jar", "test.jar",
105
"first").shouldHaveExitValue(0);
106
107
//sign jar with second key, both.jar is signed by first and second signer
108
ProcessTools.executeCommand(JARSIGNER,
109
"-keystore", KEYSTORE1,
110
"-storepass", PASSWORD,
111
"-keypass", PASSWORD,
112
"-signedjar", "both.jar", "first.jar",
113
"second").shouldHaveExitValue(0);
114
115
//test case 1
116
//setIO permission granted to code that was signed by first signer
117
//setFactory permission granted to code that was signed by second signer
118
//Keystore that contains both first and second keypairs
119
//code was singed by first signer
120
//Expect AccessControlException for setFactory permission
121
System.out.println("Test Case 1");
122
//copy policy file into current directory
123
String[] cmd = constructCMD("first.jar", POLICY1, "false", "true");
124
ProcessTools.executeTestJvm(cmd).shouldHaveExitValue(0);
125
126
//test case 2, test with both.jar
127
//setIO permission granted to code that was signed by first signer
128
//setFactory permission granted to code that was signed by second signer
129
//Keystore that contains both first and second keypairs
130
//code was singed by first signer and second signer
131
//Expect no AccessControlException
132
System.out.println("Test Case 2");
133
cmd = constructCMD("both.jar", POLICY1, "false", "false");
134
ProcessTools.executeTestJvm(cmd).shouldHaveExitValue(0);
135
136
//test case 3
137
//setIO permission granted to code that was signed by first signer
138
//setFactory permission granted to code that was signed by second signer
139
//Keystore that contains only first keypairs
140
//code was singed by first signer and second signer
141
//Expect AccessControlException for setFactory permission
142
System.out.println("Test Case 3");
143
cmd = constructCMD("both.jar", POLICY2, "false", "true");
144
ProcessTools.executeTestJvm(cmd).shouldHaveExitValue(0);
145
146
}
147
148
private static String[] constructCMD(String classpath, String policy, String arg1, String arg2) {
149
String[] cmd = {
150
"-classpath", classpath,
151
"-Djava.security.manager",
152
"-Djava.security.policy=" + policy,
153
"-Djava.security.properties=" + SECPROPS,
154
"PrivilegeTest",
155
arg1, arg2};
156
return cmd;
157
}
158
}
159
160
class PrivilegeTest {
161
162
private static final Permission PERM1 = new RuntimePermission("setIO");
163
private static final Permission PERM2 = new RuntimePermission("setFactory");
164
165
public static void main(String args[]) {
166
boolean expectException1 = Boolean.parseBoolean(args[0]);
167
boolean expectException2 = Boolean.parseBoolean(args[1]);
168
test(PERM1, expectException1);
169
test(PERM2, expectException2);
170
}
171
172
public static void test(Permission perm, boolean expectException) {
173
boolean getException = (Boolean) AccessController.doPrivileged((PrivilegedAction) () -> {
174
try {
175
AccessController.checkPermission(perm);
176
return (Boolean) false;
177
} catch (AccessControlException ex) {
178
return (Boolean) true;
179
}
180
});
181
182
if (expectException ^ getException) {
183
String message = "Check Permission :" + perm + "\n ExpectException = "
184
+ expectException + "\n getException = " + getException;
185
throw new RuntimeException(message);
186
}
187
188
}
189
190
}
191
192