Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/provider/PolicyParser/TokenStore.java
41152 views
1
/*
2
* Copyright (c) 2003, 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 sun.security.provider.*;
34
35
public class TokenStore {
36
37
private static final String POLICY_NO_STORE =
38
"grant { permission java.security.AllPermission; };";
39
40
private static final String POLICY_URL =
41
"keystore \"file:${test.src}${/}TokenStore.keystore\";" +
42
"grant signedby \"POLICY_URL\" {" +
43
" permission java.security.AllPermission;" +
44
"};" ;
45
46
private static final String POLICY_URL_T =
47
"keystore \"file:${test.src}${/}TokenStore.keystore\", \"JKS\";"+
48
"grant signedby \"POLICY_URL_T\" {" +
49
" permission java.security.AllPermission;" +
50
"};" ;
51
52
private static final String POLICY_URL_T_P =
53
"keystore \"file:${test.src}${/}TokenStore.keystore\"," +
54
" \"JKS\", \"SUN\";" +
55
"grant signedby \"POLICY_URL_T_P\" {" +
56
" permission java.security.AllPermission;" +
57
"};" ;
58
59
private static final String POLICY_URL_PWD =
60
"keystore \"file:${test.src}${/}TokenStore.keystore\";" +
61
"keystorePasswordURL \"file:${test.src}${/}TokenStore.pwd\";" +
62
"grant signedby \"POLICY_URL\" {" +
63
" permission java.security.AllPermission;" +
64
"};" ;
65
66
private static final String POLICY_URL_T_P_PWD =
67
"keystore \"file:${test.src}${/}TokenStore.keystore\"," +
68
" \"JKS\", \"SUN\";" +
69
"keystorePasswordURL \"file:${test.src}${/}TokenStore.pwd\";" +
70
"grant signedby \"POLICY_URL_T_P\" {" +
71
" permission java.security.AllPermission;" +
72
"};" ;
73
74
private static final String POLICY_PASS_NO_STORE =
75
"keystorePasswordURL \"file:${test.src}${/}TokenStore.pwd\";" +
76
"grant signedby \"POLICY_URL_T_P\" {" +
77
" permission java.security.AllPermission;" +
78
"};" ;
79
80
public static void main(String[] args) throws Exception {
81
82
// test no key store in policy
83
84
PolicyParser p = new PolicyParser();
85
p.read(new StringReader(POLICY_NO_STORE));
86
doNoStore(p);
87
StringWriter sw = new StringWriter();
88
p.write(sw);
89
PolicyParser newP = new PolicyParser();
90
newP.read(new StringReader(sw.toString()));
91
doNoStore(p);
92
93
// test policy keystore + URL
94
95
p = new PolicyParser();
96
p.read(new StringReader(POLICY_URL));
97
doURL(p, true);
98
sw = new StringWriter();
99
p.write(sw);
100
newP = new PolicyParser();
101
newP.read(new StringReader(sw.toString()));
102
doURL(p, true);
103
104
// test policy keystore + URL + type
105
106
p = new PolicyParser();
107
p.read(new StringReader(POLICY_URL_T));
108
doURL_T(p, true);
109
sw = new StringWriter();
110
p.write(sw);
111
newP = new PolicyParser();
112
newP.read(new StringReader(sw.toString()));
113
doURL_T(p, true);
114
115
// test policy keystore + URL + type + provider
116
117
p = new PolicyParser();
118
p.read(new StringReader(POLICY_URL_T_P));
119
doURL_T_P(p, true);
120
sw = new StringWriter();
121
p.write(sw);
122
newP = new PolicyParser();
123
newP.read(new StringReader(sw.toString()));
124
doURL_T_P(p, true);
125
126
// test policy keystore + URL + password
127
128
p = new PolicyParser();
129
p.read(new StringReader(POLICY_URL_PWD));
130
doURL(p, false);
131
doPwd(p);
132
sw = new StringWriter();
133
p.write(sw);
134
newP = new PolicyParser();
135
newP.read(new StringReader(sw.toString()));
136
doURL(p, false);
137
doPwd(p);
138
139
// test policy keystore + URL + type + provider + password
140
141
p = new PolicyParser();
142
p.read(new StringReader(POLICY_URL_T_P_PWD));
143
doURL_T_P(p, false);
144
doPwd(p);
145
sw = new StringWriter();
146
p.write(sw);
147
newP = new PolicyParser();
148
newP.read(new StringReader(sw.toString()));
149
doURL_T_P(p, false);
150
doPwd(p);
151
152
// test policy password with no keystore
153
p = new PolicyParser();
154
try {
155
p.read(new StringReader(POLICY_PASS_NO_STORE));
156
throw new SecurityException("expected parsing exception");
157
} catch (PolicyParser.ParsingException pe) {
158
// good
159
}
160
161
}
162
163
private static void checkPerm(PolicyParser p) throws Exception {
164
Enumeration e = p.grantElements();
165
boolean foundOne = false;
166
while (e.hasMoreElements()) {
167
PolicyParser.GrantEntry ge = (PolicyParser.GrantEntry)
168
e.nextElement();
169
if (ge.permissionEntries == null) {
170
throw new SecurityException("expected non-null perms");
171
} else {
172
foundOne = true;
173
}
174
}
175
if (!foundOne) {
176
throw new SecurityException("expected non-null grant entries");
177
}
178
}
179
180
private static void doNoStore(PolicyParser p) throws Exception {
181
if (p.getKeyStoreUrl() != null ||
182
p.getKeyStoreType() != null ||
183
p.getKeyStoreProvider() != null ||
184
p.getStorePassURL() != null) {
185
throw new SecurityException("expected null keystore");
186
}
187
checkPerm(p);
188
}
189
190
private static void doURL(PolicyParser p, boolean checkPwd)
191
throws Exception {
192
if (p.getKeyStoreUrl() == null ||
193
!(p.getKeyStoreUrl().endsWith("TokenStore.keystore")) ||
194
p.getKeyStoreType() != null ||
195
p.getKeyStoreProvider() != null) {
196
throw new SecurityException("invalid keystore values");
197
}
198
if (checkPwd) {
199
if (p.getStorePassURL() != null) {
200
throw new SecurityException("invalid keystore values");
201
}
202
}
203
checkPerm(p);
204
}
205
206
private static void doURL_T(PolicyParser p, boolean checkPwd)
207
throws Exception {
208
if (p.getKeyStoreUrl() == null ||
209
!(p.getKeyStoreUrl().endsWith("TokenStore.keystore")) ||
210
p.getKeyStoreType() == null ||
211
!(p.getKeyStoreType().equals("JKS")) ||
212
p.getKeyStoreProvider() != null) {
213
throw new SecurityException("invalid keystore values");
214
}
215
if (checkPwd) {
216
if (p.getStorePassURL() != null) {
217
throw new SecurityException("invalid keystore values");
218
}
219
}
220
checkPerm(p);
221
}
222
223
private static void doURL_T_P(PolicyParser p, boolean checkPwd)
224
throws Exception {
225
if (p.getKeyStoreUrl() == null ||
226
!(p.getKeyStoreUrl().endsWith("TokenStore.keystore")) ||
227
p.getKeyStoreType() == null ||
228
!(p.getKeyStoreType().equals("JKS")) ||
229
p.getKeyStoreProvider() == null ||
230
!(p.getKeyStoreProvider().equals("SUN"))) {
231
throw new SecurityException("invalid keystore values");
232
}
233
if (checkPwd) {
234
if (p.getStorePassURL() != null) {
235
throw new SecurityException("invalid keystore values");
236
}
237
}
238
checkPerm(p);
239
}
240
241
private static void doPwd(PolicyParser p) throws Exception {
242
if (p.getStorePassURL() == null ||
243
!(p.getStorePassURL().endsWith("TokenStore.pwd"))) {
244
throw new SecurityException("invalid password values");
245
}
246
}
247
}
248
249