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