Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/security/provider/SubjectCodeSource.java
41159 views
1
/*
2
* Copyright (c) 1999, 2021, 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package sun.security.provider;
27
28
import java.net.URL;
29
import java.util.*;
30
import java.security.CodeSource;
31
import java.security.Principal;
32
import java.security.cert.Certificate;
33
import java.lang.reflect.Constructor;
34
35
import javax.security.auth.Subject;
36
import sun.security.provider.PolicyParser.PrincipalEntry;
37
import sun.security.util.ResourcesMgr;
38
39
/**
40
* <p> This <code>SubjectCodeSource</code> class contains
41
* a <code>URL</code>, signer certificates, and either a <code>Subject</code>
42
* (that represents the <code>Subject</code> in the current
43
* <code>AccessControlContext</code>), or a linked list of Principals
44
* (that represent a "subject" in a <code>Policy</code>).
45
*
46
*/
47
class SubjectCodeSource extends CodeSource implements java.io.Serializable {
48
49
@java.io.Serial
50
private static final long serialVersionUID = 6039418085604715275L;
51
52
private Subject subject;
53
private LinkedList<PrincipalEntry> principals;
54
private static final Class<?>[] PARAMS = { String.class };
55
private static final sun.security.util.Debug debug =
56
sun.security.util.Debug.getInstance("auth", "\t[Auth Access]");
57
@SuppressWarnings("serial") // Not statically typed as Serializable
58
private ClassLoader sysClassLoader;
59
60
/**
61
* Creates a new <code>SubjectCodeSource</code>
62
* with the given <code>Subject</code>, principals, <code>URL</code>,
63
* and signers (Certificates). The <code>Subject</code>
64
* represents the <code>Subject</code> associated with the current
65
* <code>AccessControlContext</code>.
66
* The Principals are given as a <code>LinkedList</code>
67
* of <code>PolicyParser.PrincipalEntry</code> objects.
68
* Typically either a <code>Subject</code> will be provided,
69
* or a list of <code>principals</code> will be provided
70
* (not both).
71
*
72
* <p>
73
*
74
* @param subject the <code>Subject</code> associated with this
75
* <code>SubjectCodeSource</code> <p>
76
*
77
* @param url the <code>URL</code> associated with this
78
* <code>SubjectCodeSource</code> <p>
79
*
80
* @param certs the signers associated with this
81
* <code>SubjectCodeSource</code> <p>
82
*/
83
@SuppressWarnings("removal")
84
SubjectCodeSource(Subject subject,
85
LinkedList<PrincipalEntry> principals,
86
URL url, Certificate[] certs) {
87
88
super(url, certs);
89
this.subject = subject;
90
this.principals = (principals == null ?
91
new LinkedList<PrincipalEntry>() :
92
new LinkedList<PrincipalEntry>(principals));
93
sysClassLoader = java.security.AccessController.doPrivileged
94
(new java.security.PrivilegedAction<ClassLoader>() {
95
public ClassLoader run() {
96
return ClassLoader.getSystemClassLoader();
97
}
98
});
99
}
100
101
/**
102
* Get the Principals associated with this <code>SubjectCodeSource</code>.
103
* The Principals are retrieved as a <code>LinkedList</code>
104
* of <code>PolicyParser.PrincipalEntry</code> objects.
105
*
106
* <p>
107
*
108
* @return the Principals associated with this
109
* <code>SubjectCodeSource</code> as a <code>LinkedList</code>
110
* of <code>PolicyParser.PrincipalEntry</code> objects.
111
*/
112
LinkedList<PrincipalEntry> getPrincipals() {
113
return principals;
114
}
115
116
/**
117
* Get the <code>Subject</code> associated with this
118
* <code>SubjectCodeSource</code>. The <code>Subject</code>
119
* represents the <code>Subject</code> associated with the
120
* current <code>AccessControlContext</code>.
121
*
122
* <p>
123
*
124
* @return the <code>Subject</code> associated with this
125
* <code>SubjectCodeSource</code>.
126
*/
127
Subject getSubject() {
128
return subject;
129
}
130
131
/**
132
* Returns true if this <code>SubjectCodeSource</code> object "implies"
133
* the specified <code>CodeSource</code>.
134
* More specifically, this method makes the following checks.
135
* If any fail, it returns false. If they all succeed, it returns true.
136
*
137
* <p>
138
* <ol>
139
* <li> The provided codesource must not be <code>null</code>.
140
* <li> codesource must be an instance of <code>SubjectCodeSource</code>.
141
* <li> super.implies(codesource) must return true.
142
* <li> for each principal in this codesource's principal list:
143
* <ol>
144
* <li> if the principal is an instanceof
145
* <code>Principal</code>, then the principal must
146
* imply the provided codesource's <code>Subject</code>.
147
* <li> if the principal is not an instanceof
148
* <code>Principal</code>, then the provided
149
* codesource's <code>Subject</code> must have an
150
* associated <code>Principal</code>, <i>P</i>, where
151
* P.getClass().getName equals principal.principalClass,
152
* and P.getName() equals principal.principalName.
153
* </ol>
154
* </ol>
155
*
156
* <p>
157
*
158
* @param codesource the <code>CodeSource</code> to compare against.
159
*
160
* @return true if this <code>SubjectCodeSource</code> implies
161
* the specified <code>CodeSource</code>.
162
*/
163
public boolean implies(CodeSource codesource) {
164
165
LinkedList<PrincipalEntry> subjectList = null;
166
167
if (codesource == null ||
168
!(codesource instanceof SubjectCodeSource) ||
169
!(super.implies(codesource))) {
170
171
if (debug != null)
172
debug.println("\tSubjectCodeSource.implies: FAILURE 1");
173
return false;
174
}
175
176
SubjectCodeSource that = (SubjectCodeSource)codesource;
177
178
// if the principal list in the policy "implies"
179
// the Subject associated with the current AccessControlContext,
180
// then return true
181
182
if (this.principals == null) {
183
if (debug != null)
184
debug.println("\tSubjectCodeSource.implies: PASS 1");
185
return true;
186
}
187
188
if (that.getSubject() == null ||
189
that.getSubject().getPrincipals().size() == 0) {
190
if (debug != null)
191
debug.println("\tSubjectCodeSource.implies: FAILURE 2");
192
return false;
193
}
194
195
ListIterator<PrincipalEntry> li = this.principals.listIterator(0);
196
while (li.hasNext()) {
197
PrincipalEntry pppe = li.next();
198
try {
199
200
// use new Principal.implies method
201
202
Class<?> pClass = Class.forName(pppe.principalClass,
203
true, sysClassLoader);
204
if (!Principal.class.isAssignableFrom(pClass)) {
205
// not the right subtype
206
throw new ClassCastException(pppe.principalClass +
207
" is not a Principal");
208
}
209
Constructor<?> c = pClass.getConstructor(PARAMS);
210
Principal p = (Principal)c.newInstance(new Object[] {
211
pppe.principalName });
212
213
if (!p.implies(that.getSubject())) {
214
if (debug != null)
215
debug.println("\tSubjectCodeSource.implies: FAILURE 3");
216
return false;
217
} else {
218
if (debug != null)
219
debug.println("\tSubjectCodeSource.implies: PASS 2");
220
return true;
221
}
222
} catch (Exception e) {
223
224
// simply compare Principals
225
226
if (subjectList == null) {
227
228
if (that.getSubject() == null) {
229
if (debug != null)
230
debug.println("\tSubjectCodeSource.implies: " +
231
"FAILURE 4");
232
return false;
233
}
234
Iterator<Principal> i =
235
that.getSubject().getPrincipals().iterator();
236
237
subjectList = new LinkedList<PrincipalEntry>();
238
while (i.hasNext()) {
239
Principal p = i.next();
240
PrincipalEntry spppe = new PrincipalEntry
241
(p.getClass().getName(), p.getName());
242
subjectList.add(spppe);
243
}
244
}
245
246
if (!subjectListImpliesPrincipalEntry(subjectList, pppe)) {
247
if (debug != null)
248
debug.println("\tSubjectCodeSource.implies: FAILURE 5");
249
return false;
250
}
251
}
252
}
253
254
if (debug != null)
255
debug.println("\tSubjectCodeSource.implies: PASS 3");
256
return true;
257
}
258
259
/**
260
* This method returns, true, if the provided <i>subjectList</i>
261
* "contains" the <code>Principal</code> specified
262
* in the provided <i>pppe</i> argument.
263
*
264
* Note that the provided <i>pppe</i> argument may have
265
* wildcards (*) for the <code>Principal</code> class and name,
266
* which need to be considered.
267
*
268
* <p>
269
*
270
* @param subjectList a list of PolicyParser.PrincipalEntry objects
271
* that correspond to all the Principals in the Subject currently
272
* on this thread's AccessControlContext. <p>
273
*
274
* @param pppe the Principals specified in a grant entry.
275
*
276
* @return true if the provided <i>subjectList</i> "contains"
277
* the <code>Principal</code> specified in the provided
278
* <i>pppe</i> argument.
279
*/
280
private boolean subjectListImpliesPrincipalEntry(
281
LinkedList<PrincipalEntry> subjectList, PrincipalEntry pppe) {
282
283
ListIterator<PrincipalEntry> li = subjectList.listIterator(0);
284
while (li.hasNext()) {
285
PrincipalEntry listPppe = li.next();
286
287
if (pppe.getPrincipalClass().equals
288
(PrincipalEntry.WILDCARD_CLASS) ||
289
pppe.getPrincipalClass().equals(listPppe.getPrincipalClass()))
290
{
291
if (pppe.getPrincipalName().equals
292
(PrincipalEntry.WILDCARD_NAME) ||
293
pppe.getPrincipalName().equals(listPppe.getPrincipalName()))
294
return true;
295
}
296
}
297
return false;
298
}
299
300
/**
301
* Tests for equality between the specified object and this
302
* object. Two <code>SubjectCodeSource</code> objects are considered equal
303
* if their locations are of identical value, if the two sets of
304
* Certificates are of identical values, and if the
305
* Subjects are equal, and if the PolicyParser.PrincipalEntry values
306
* are of identical values. It is not required that
307
* the Certificates or PolicyParser.PrincipalEntry values
308
* be in the same order.
309
*
310
* <p>
311
*
312
* @param obj the object to test for equality with this object.
313
*
314
* @return true if the objects are considered equal, false otherwise.
315
*/
316
public boolean equals(Object obj) {
317
318
if (obj == this)
319
return true;
320
321
if (super.equals(obj) == false)
322
return false;
323
324
if (!(obj instanceof SubjectCodeSource))
325
return false;
326
327
SubjectCodeSource that = (SubjectCodeSource)obj;
328
329
// the principal lists must match
330
try {
331
if (this.getSubject() != that.getSubject())
332
return false;
333
} catch (SecurityException se) {
334
return false;
335
}
336
337
if ((this.principals == null && that.principals != null) ||
338
(this.principals != null && that.principals == null))
339
return false;
340
341
if (this.principals != null && that.principals != null) {
342
if (!this.principals.containsAll(that.principals) ||
343
!that.principals.containsAll(this.principals))
344
345
return false;
346
}
347
348
return true;
349
}
350
351
/**
352
* Return a hashcode for this <code>SubjectCodeSource</code>.
353
*
354
* <p>
355
*
356
* @return a hashcode for this <code>SubjectCodeSource</code>.
357
*/
358
public int hashCode() {
359
return super.hashCode();
360
}
361
362
/**
363
* Return a String representation of this <code>SubjectCodeSource</code>.
364
*
365
* <p>
366
*
367
* @return a String representation of this <code>SubjectCodeSource</code>.
368
*/
369
@SuppressWarnings("removal")
370
public String toString() {
371
String returnMe = super.toString();
372
if (getSubject() != null) {
373
if (debug != null) {
374
final Subject finalSubject = getSubject();
375
returnMe = returnMe + "\n" +
376
java.security.AccessController.doPrivileged
377
(new java.security.PrivilegedAction<String>() {
378
public String run() {
379
return finalSubject.toString();
380
}
381
});
382
} else {
383
returnMe = returnMe + "\n" + getSubject().toString();
384
}
385
}
386
if (principals != null) {
387
ListIterator<PrincipalEntry> li = principals.listIterator();
388
while (li.hasNext()) {
389
PrincipalEntry pppe = li.next();
390
returnMe = returnMe + ResourcesMgr.getAuthResourceString("NEWLINE") +
391
pppe.getPrincipalClass() + " " +
392
pppe.getPrincipalName();
393
}
394
}
395
return returnMe;
396
}
397
}
398
399