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