Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/provider/PolicyFile/TokenStore.java
41153 views
1
/*
2
* Copyright (c) 2003, 2017, 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 4919147
27
* @summary Support for token-based KeyStores
28
* @modules java.base/sun.security.provider
29
*/
30
31
import java.io.*;
32
import java.util.*;
33
import java.net.*;
34
import java.security.AllPermission;
35
import java.security.CodeSource;
36
import java.security.ProtectionDomain;
37
import java.security.Permission;
38
import java.security.KeyStore;
39
import java.security.cert.*;
40
import sun.security.provider.*;
41
42
public class TokenStore {
43
44
private static String DIR =
45
System.getProperty("test.classes", ".") + File.separatorChar;
46
private static final char[] storePassword = new char[]
47
{ 'T', 'o', 'k', 'e', 'n', 'S', 't', 'o', 'r', 'e' };
48
49
50
// policy files that will get written
51
private static String NO_STORE_FILE = DIR + "TokenStore.NoStore";
52
private static String URL_FILE = DIR + "TokenStore.Url";
53
private static String URL_T_FILE = DIR + "TokenStore.UrlT";
54
private static String URL_T_P_FILE = DIR + "TokenStore.UrlTP";
55
private static String URL_PWD_FILE = DIR + "TokenStore.UrlPwd";
56
private static String URL_T_P_PWD_FILE = DIR + "TokenStore.UrlTPPwd";
57
private static String BADPASS_FILE = DIR + "TokenStore.BadPass";
58
59
private static String RELPASS_FILE =
60
System.getProperty("test.src", ".") + File.separatorChar +
61
"TokenStore.RelPassPolicy";
62
63
// protection domains
64
private static ProtectionDomain NO_STORE_DOMAIN;
65
private static ProtectionDomain URL_DOMAIN;
66
private static ProtectionDomain URL_T_DOMAIN;
67
private static ProtectionDomain URL_T_P_DOMAIN;
68
69
// policy contents written to files
70
private static final String POLICY_NO_STORE =
71
"grant { permission java.security.AllPermission; };";
72
73
private static final String POLICY_URL =
74
"keystore \"file:${test.src}${/}TokenStore.keystore\";" +
75
"grant signedby \"POLICY_URL\" {" +
76
" permission java.security.AllPermission;" +
77
"};" ;
78
79
private static final String POLICY_URL_T =
80
"keystore \"file:${test.src}${/}TokenStore.keystore\", \"JKS\";"+
81
"grant signedby \"POLICY_URL_T\" {" +
82
" permission java.security.AllPermission;" +
83
"};" ;
84
85
private static final String POLICY_URL_T_P =
86
"keystore \"file:${test.src}${/}TokenStore.keystore\"," +
87
" \"JKS\", \"SUN\";" +
88
"grant signedby \"POLICY_URL_T_P\" {" +
89
" permission java.security.AllPermission;" +
90
"};" ;
91
92
private static final String POLICY_URL_PWD =
93
"keystore \"file:${test.src}${/}TokenStore.keystore\";" +
94
"keystorePasswordURL \"file:${test.src}${/}TokenStore.pwd\";" +
95
"grant signedby \"POLICY_URL\" {" +
96
" permission java.security.AllPermission;" +
97
"};" ;
98
99
private static final String POLICY_URL_T_P_PWD =
100
"keystore \"file:${test.src}${/}TokenStore.keystore\"," +
101
" \"JKS\", \"SUN\";" +
102
"keystorePasswordURL \"file:${test.src}${/}TokenStore.pwd\";" +
103
"grant signedby \"POLICY_URL_T_P\" {" +
104
" permission java.security.AllPermission;" +
105
"};" ;
106
107
private static final String POLICY_BADPASS =
108
"keystore \"file:${test.src}${/}TokenStore.keystore\"," +
109
" \"JKS\", \"SUN\";" +
110
"keystorePasswordURL \"file:${test.src}${/}TokenStore.java\";" +
111
"grant signedby \"POLICY_URL_T_P\" {" +
112
" permission java.security.AllPermission;" +
113
"};" ;
114
115
private static void init() throws Exception {
116
117
// first write policy files
118
119
PolicyParser pp = new PolicyParser();
120
pp.read(new StringReader(POLICY_NO_STORE));
121
pp.write(new FileWriter(NO_STORE_FILE, false));
122
123
pp = new PolicyParser();
124
pp.read(new StringReader(POLICY_URL));
125
pp.write(new FileWriter(URL_FILE, false));
126
127
pp = new PolicyParser();
128
pp.read(new StringReader(POLICY_URL_T));
129
pp.write(new FileWriter(URL_T_FILE, false));
130
131
pp = new PolicyParser();
132
pp.read(new StringReader(POLICY_URL_T_P));
133
pp.write(new FileWriter(URL_T_P_FILE, false));
134
135
pp = new PolicyParser();
136
pp.read(new StringReader(POLICY_URL_PWD));
137
pp.write(new FileWriter(URL_PWD_FILE, false));
138
139
pp = new PolicyParser();
140
pp.read(new StringReader(POLICY_URL_T_P_PWD));
141
pp.write(new FileWriter(URL_T_P_PWD_FILE, false));
142
143
pp = new PolicyParser();
144
pp.read(new StringReader(POLICY_BADPASS));
145
pp.write(new FileWriter(BADPASS_FILE, false));
146
147
// next load keystore data to build PD's
148
149
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
150
ks.load(new FileInputStream
151
(System.getProperty("test.src", ".") +
152
File.separatorChar +
153
"TokenStore.keystore"),
154
storePassword);
155
156
NO_STORE_DOMAIN = new ProtectionDomain
157
(new CodeSource(new URL("file:/foo"),
158
(java.security.cert.Certificate[]) null),
159
null, // perms
160
null, // class loader
161
null); // principals
162
163
Certificate[] chain = (Certificate[])
164
ks.getCertificateChain("POLICY_URL");
165
URL_DOMAIN = new ProtectionDomain
166
(new CodeSource(new URL("file:/foo"), chain),
167
null, // perms
168
null, // class loader
169
null); // principals
170
171
chain = (Certificate[])
172
ks.getCertificateChain("POLICY_URL_T");
173
URL_T_DOMAIN = new ProtectionDomain
174
(new CodeSource(new URL("file:/foo"), chain),
175
null, // perms
176
null, // class loader
177
null); // principals
178
179
chain = (Certificate[])
180
ks.getCertificateChain("POLICY_URL_T_P");
181
URL_T_P_DOMAIN = new ProtectionDomain
182
(new CodeSource(new URL("file:/foo"), chain),
183
null, // perms
184
null, // class loader
185
null); // principals
186
}
187
188
public static void main(String[] args) throws Exception {
189
190
init();
191
192
// test no key store in policy
193
194
System.setProperty("java.security.policy", "=" + NO_STORE_FILE);
195
PolicyFile p = new PolicyFile();
196
checkPerm(p, NO_STORE_DOMAIN);
197
198
// test policy keystore + URL
199
200
System.setProperty("java.security.policy", "=" + URL_FILE);
201
p = new PolicyFile();
202
checkPerm(p, URL_DOMAIN);
203
204
// test policy keystore + URL + type
205
206
System.setProperty("java.security.policy", "=" + URL_T_FILE);
207
p = new PolicyFile();
208
checkPerm(p, URL_T_DOMAIN);
209
210
// test policy keystore + URL + type + provider
211
212
System.setProperty("java.security.policy", "=" + URL_T_P_FILE);
213
p = new PolicyFile();
214
checkPerm(p, URL_T_P_DOMAIN);
215
216
// test policy keystore + URL + password
217
218
System.setProperty("java.security.policy", "=" + URL_FILE);
219
p = new PolicyFile();
220
checkPerm(p, URL_DOMAIN);
221
222
// test policy keystore + URL + type + provider + password
223
224
System.setProperty("java.security.policy", "=" + URL_T_P_FILE);
225
p = new PolicyFile();
226
checkPerm(p, URL_T_P_DOMAIN);
227
228
// test policy keystore + URL + type + provider + BAD password
229
230
System.setProperty("java.security.policy", "=" + BADPASS_FILE);
231
p = new PolicyFile();
232
try {
233
checkPerm(p, URL_T_P_DOMAIN);
234
throw new RuntimeException("expected SecurityException");
235
} catch (SecurityException se) {
236
// good
237
//se.printStackTrace();
238
}
239
240
// test policy keystore + URL + type + provider + RELATIVE password
241
242
System.setProperty("java.security.policy", "=" + RELPASS_FILE);
243
p = new PolicyFile();
244
checkPerm(p, URL_T_P_DOMAIN);
245
}
246
247
private static void checkPerm(PolicyFile p, ProtectionDomain pd)
248
throws Exception {
249
boolean foundIt = false;
250
Enumeration perms = p.getPermissions(pd).elements();
251
while (perms.hasMoreElements()) {
252
Permission perm = (Permission)perms.nextElement();
253
if (perm instanceof AllPermission) {
254
foundIt = true;
255
break;
256
}
257
}
258
if (!foundIt) {
259
throw new SecurityException("expected AllPermission");
260
}
261
}
262
}
263
264