author | ascarpino |
Wed, 13 Apr 2016 10:20:40 -0700 | |
changeset 37348 | 9ccec3170d5e |
parent 33241 | 27eb2d6abda9 |
child 37782 | ad8fe7507ecc |
permissions | -rw-r--r-- |
2 | 1 |
/* |
37348
9ccec3170d5e
8152205: jdk.security.provider.preferred is ambiguously documented
ascarpino
parents:
33241
diff
changeset
|
2 |
* Copyright (c) 1997, 2016, 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 |
||
26 |
||
27 |
package java.security; |
|
28 |
||
29 |
import java.util.Enumeration; |
|
30 |
import java.util.WeakHashMap; |
|
11034
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
31 |
import java.util.concurrent.atomic.AtomicReference; |
5183
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
32 |
import sun.security.jca.GetInstance; |
2 | 33 |
import sun.security.util.Debug; |
34 |
import sun.security.util.SecurityConstants; |
|
35 |
||
36 |
||
37 |
/** |
|
38 |
* A Policy object is responsible for determining whether code executing |
|
39 |
* in the Java runtime environment has permission to perform a |
|
40 |
* security-sensitive operation. |
|
41 |
* |
|
42 |
* <p> There is only one Policy object installed in the runtime at any |
|
43 |
* given time. A Policy object can be installed by calling the |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
14775
diff
changeset
|
44 |
* {@code setPolicy} method. The installed Policy object can be |
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
14775
diff
changeset
|
45 |
* obtained by calling the {@code getPolicy} method. |
2 | 46 |
* |
47 |
* <p> If no Policy object has been installed in the runtime, a call to |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
14775
diff
changeset
|
48 |
* {@code getPolicy} installs an instance of the default Policy |
2 | 49 |
* implementation (a default subclass implementation of this abstract class). |
50 |
* The default Policy implementation can be changed by setting the value |
|
14775
2ed01c760aea
8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents:
11034
diff
changeset
|
51 |
* of the {@code policy.provider} security property to the fully qualified |
30444
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
52 |
* name of the desired Policy subclass implementation. The system class loader |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
53 |
* is used to load this class. |
2 | 54 |
* |
55 |
* <p> Application code can directly subclass Policy to provide a custom |
|
56 |
* implementation. In addition, an instance of a Policy object can be |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
14775
diff
changeset
|
57 |
* constructed by invoking one of the {@code getInstance} factory methods |
2 | 58 |
* with a standard type. The default policy type is "JavaPolicy". |
59 |
* |
|
60 |
* <p> Once a Policy instance has been installed (either by default, or by |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
14775
diff
changeset
|
61 |
* calling {@code setPolicy}), the Java runtime invokes its |
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
14775
diff
changeset
|
62 |
* {@code implies} method when it needs to |
2 | 63 |
* determine whether executing code (encapsulated in a ProtectionDomain) |
64 |
* can perform SecurityManager-protected operations. How a Policy object |
|
65 |
* retrieves its policy data is up to the Policy implementation itself. |
|
66 |
* The policy data may be stored, for example, in a flat ASCII file, |
|
67 |
* in a serialized binary file of the Policy class, or in a database. |
|
68 |
* |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
14775
diff
changeset
|
69 |
* <p> The {@code refresh} method causes the policy object to |
2 | 70 |
* refresh/reload its data. This operation is implementation-dependent. |
71 |
* For example, if the policy object stores its data in configuration files, |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
14775
diff
changeset
|
72 |
* calling {@code refresh} will cause it to re-read the configuration |
2 | 73 |
* policy files. If a refresh operation is not supported, this method does |
74 |
* nothing. Note that refreshed policy may not have an effect on classes |
|
75 |
* in a particular ProtectionDomain. This is dependent on the Policy |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
14775
diff
changeset
|
76 |
* provider's implementation of the {@code implies} |
2 | 77 |
* method and its PermissionCollection caching strategy. |
78 |
* |
|
79 |
* @author Roland Schemers |
|
80 |
* @author Gary Ellison |
|
81 |
* @see java.security.Provider |
|
82 |
* @see java.security.ProtectionDomain |
|
83 |
* @see java.security.Permission |
|
14775
2ed01c760aea
8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents:
11034
diff
changeset
|
84 |
* @see java.security.Security security properties |
2 | 85 |
*/ |
86 |
||
87 |
public abstract class Policy { |
|
88 |
||
89 |
/** |
|
90 |
* A read-only empty PermissionCollection instance. |
|
91 |
* @since 1.6 |
|
92 |
*/ |
|
93 |
public static final PermissionCollection UNSUPPORTED_EMPTY_COLLECTION = |
|
94 |
new UnsupportedEmptyCollection(); |
|
95 |
||
11034
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
96 |
// Information about the system-wide policy. |
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
97 |
private static class PolicyInfo { |
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
98 |
// the system-wide policy |
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
99 |
final Policy policy; |
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
100 |
// a flag indicating if the system-wide policy has been initialized |
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
101 |
final boolean initialized; |
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
102 |
|
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
103 |
PolicyInfo(Policy policy, boolean initialized) { |
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
104 |
this.policy = policy; |
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
105 |
this.initialized = initialized; |
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
106 |
} |
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
107 |
} |
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
108 |
|
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
109 |
// PolicyInfo is stored in an AtomicReference |
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
110 |
private static AtomicReference<PolicyInfo> policy = |
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
111 |
new AtomicReference<>(new PolicyInfo(null, false)); |
2 | 112 |
|
113 |
private static final Debug debug = Debug.getInstance("policy"); |
|
114 |
||
30444
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
115 |
// Default policy provider |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
116 |
private static final String DEFAULT_POLICY = |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
117 |
"sun.security.provider.PolicyFile"; |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
118 |
|
5183
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
119 |
// Cache mapping ProtectionDomain.Key to PermissionCollection |
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
120 |
private WeakHashMap<ProtectionDomain.Key, PermissionCollection> pdMapping; |
2 | 121 |
|
11034
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
122 |
/** package private for AccessControlContext and ProtectionDomain */ |
2 | 123 |
static boolean isSet() |
124 |
{ |
|
11034
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
125 |
PolicyInfo pi = policy.get(); |
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
126 |
return pi.policy != null && pi.initialized == true; |
2 | 127 |
} |
128 |
||
129 |
private static void checkPermission(String type) { |
|
130 |
SecurityManager sm = System.getSecurityManager(); |
|
131 |
if (sm != null) { |
|
132 |
sm.checkPermission(new SecurityPermission("createPolicy." + type)); |
|
133 |
} |
|
134 |
} |
|
135 |
||
136 |
/** |
|
137 |
* Returns the installed Policy object. This value should not be cached, |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
14775
diff
changeset
|
138 |
* as it may be changed by a call to {@code setPolicy}. |
2 | 139 |
* This method first calls |
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
14775
diff
changeset
|
140 |
* {@code SecurityManager.checkPermission} with a |
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
14775
diff
changeset
|
141 |
* {@code SecurityPermission("getPolicy")} permission |
8152
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
142 |
* to ensure it's ok to get the Policy object. |
2 | 143 |
* |
144 |
* @return the installed Policy. |
|
145 |
* |
|
146 |
* @throws SecurityException |
|
147 |
* if a security manager exists and its |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
14775
diff
changeset
|
148 |
* {@code checkPermission} method doesn't allow |
2 | 149 |
* getting the Policy object. |
150 |
* |
|
151 |
* @see SecurityManager#checkPermission(Permission) |
|
152 |
* @see #setPolicy(java.security.Policy) |
|
153 |
*/ |
|
154 |
public static Policy getPolicy() |
|
155 |
{ |
|
156 |
SecurityManager sm = System.getSecurityManager(); |
|
157 |
if (sm != null) |
|
158 |
sm.checkPermission(SecurityConstants.GET_POLICY_PERMISSION); |
|
159 |
return getPolicyNoCheck(); |
|
160 |
} |
|
161 |
||
162 |
/** |
|
163 |
* Returns the installed Policy object, skipping the security check. |
|
11034
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
164 |
* Used by ProtectionDomain and getPolicy. |
2 | 165 |
* |
166 |
* @return the installed Policy. |
|
167 |
*/ |
|
11034
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
168 |
static Policy getPolicyNoCheck() |
2 | 169 |
{ |
11034
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
170 |
PolicyInfo pi = policy.get(); |
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
171 |
// Use double-check idiom to avoid locking if system-wide policy is |
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
172 |
// already initialized |
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
173 |
if (pi.initialized == false || pi.policy == null) { |
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
174 |
synchronized (Policy.class) { |
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
175 |
PolicyInfo pinfo = policy.get(); |
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
176 |
if (pinfo.policy == null) { |
30444
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
177 |
return loadPolicyProvider(); |
2 | 178 |
} |
11034
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
179 |
return pinfo.policy; |
2 | 180 |
} |
181 |
} |
|
11034
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
182 |
return pi.policy; |
2 | 183 |
} |
184 |
||
185 |
/** |
|
30444
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
186 |
* Loads and instantiates a Policy implementation specified by the |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
187 |
* policy.provider security property. Note that this method should only |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
188 |
* be called by getPolicyNoCheck and from within a synchronized block with |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
189 |
* an intrinsic lock on the Policy.class. |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
190 |
*/ |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
191 |
private static Policy loadPolicyProvider() { |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
192 |
String policyProvider = |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
193 |
AccessController.doPrivileged(new PrivilegedAction<>() { |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
194 |
@Override |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
195 |
public String run() { |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
196 |
return Security.getProperty("policy.provider"); |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
197 |
} |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
198 |
}); |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
199 |
|
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
200 |
/* |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
201 |
* If policy.provider is not set or is set to the default provider, |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
202 |
* simply instantiate it and return. |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
203 |
*/ |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
204 |
if (policyProvider == null || policyProvider.isEmpty() || |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
205 |
policyProvider.equals(DEFAULT_POLICY)) |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
206 |
{ |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
207 |
Policy polFile = new sun.security.provider.PolicyFile(); |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
208 |
policy.set(new PolicyInfo(polFile, true)); |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
209 |
return polFile; |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
210 |
} |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
211 |
|
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
212 |
/* |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
213 |
* Locate, load, and instantiate the policy.provider impl using |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
214 |
* the system class loader. While doing so, install the bootstrap |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
215 |
* provider to avoid potential recursion. |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
216 |
*/ |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
217 |
Policy polFile = new sun.security.provider.PolicyFile(); |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
218 |
policy.set(new PolicyInfo(polFile, false)); |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
219 |
|
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
220 |
Policy pol = AccessController.doPrivileged(new PrivilegedAction<>() { |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
221 |
@Override |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
222 |
public Policy run() { |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
223 |
try { |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
224 |
ClassLoader scl = ClassLoader.getSystemClassLoader(); |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
225 |
Class<?> c = Class.forName(policyProvider, true, scl); |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
226 |
return (Policy)c.newInstance(); |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
227 |
} catch (Exception e) { |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
228 |
if (debug != null) { |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
229 |
debug.println("policy provider " + policyProvider + |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
230 |
" not available"); |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
231 |
e.printStackTrace(); |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
232 |
} |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
233 |
return null; |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
234 |
} |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
235 |
} |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
236 |
}); |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
237 |
|
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
238 |
if (pol == null) { |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
239 |
// Fallback and use the system default implementation |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
240 |
if (debug != null) { |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
241 |
debug.println("using " + DEFAULT_POLICY); |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
242 |
} |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
243 |
pol = polFile; |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
244 |
} |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
245 |
policy.set(new PolicyInfo(pol, true)); |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
246 |
return pol; |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
247 |
} |
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
248 |
|
054604aed79c
8075706: Policy implementation does not allow policy.provider to be on the class path
mullan
parents:
30033
diff
changeset
|
249 |
/** |
2 | 250 |
* Sets the system-wide Policy object. This method first calls |
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
14775
diff
changeset
|
251 |
* {@code SecurityManager.checkPermission} with a |
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
14775
diff
changeset
|
252 |
* {@code SecurityPermission("setPolicy")} |
2 | 253 |
* permission to ensure it's ok to set the Policy. |
254 |
* |
|
255 |
* @param p the new system Policy object. |
|
256 |
* |
|
257 |
* @throws SecurityException |
|
258 |
* if a security manager exists and its |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
14775
diff
changeset
|
259 |
* {@code checkPermission} method doesn't allow |
2 | 260 |
* setting the Policy. |
261 |
* |
|
262 |
* @see SecurityManager#checkPermission(Permission) |
|
263 |
* @see #getPolicy() |
|
264 |
* |
|
265 |
*/ |
|
266 |
public static void setPolicy(Policy p) |
|
267 |
{ |
|
268 |
SecurityManager sm = System.getSecurityManager(); |
|
269 |
if (sm != null) sm.checkPermission( |
|
270 |
new SecurityPermission("setPolicy")); |
|
271 |
if (p != null) { |
|
272 |
initPolicy(p); |
|
273 |
} |
|
274 |
synchronized (Policy.class) { |
|
11034
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
275 |
policy.set(new PolicyInfo(p, p != null)); |
2 | 276 |
} |
277 |
} |
|
278 |
||
279 |
/** |
|
280 |
* Initialize superclass state such that a legacy provider can |
|
281 |
* handle queries for itself. |
|
282 |
* |
|
283 |
* @since 1.4 |
|
284 |
*/ |
|
285 |
private static void initPolicy (final Policy p) { |
|
286 |
/* |
|
287 |
* A policy provider not on the bootclasspath could trigger |
|
288 |
* security checks fulfilling a call to either Policy.implies |
|
289 |
* or Policy.getPermissions. If this does occur the provider |
|
290 |
* must be able to answer for it's own ProtectionDomain |
|
291 |
* without triggering additional security checks, otherwise |
|
292 |
* the policy implementation will end up in an infinite |
|
293 |
* recursion. |
|
294 |
* |
|
295 |
* To mitigate this, the provider can collect it's own |
|
296 |
* ProtectionDomain and associate a PermissionCollection while |
|
297 |
* it is being installed. The currently installed policy |
|
298 |
* provider (if there is one) will handle calls to |
|
299 |
* Policy.implies or Policy.getPermissions during this |
|
300 |
* process. |
|
301 |
* |
|
302 |
* This Policy superclass caches away the ProtectionDomain and |
|
303 |
* statically binds permissions so that legacy Policy |
|
304 |
* implementations will continue to function. |
|
305 |
*/ |
|
306 |
||
307 |
ProtectionDomain policyDomain = |
|
30033
b9c86c17164a
8078468: Update security libraries to use diamond with anonymous classes
darcy
parents:
25859
diff
changeset
|
308 |
AccessController.doPrivileged(new PrivilegedAction<>() { |
2 | 309 |
public ProtectionDomain run() { |
310 |
return p.getClass().getProtectionDomain(); |
|
311 |
} |
|
312 |
}); |
|
313 |
||
314 |
/* |
|
315 |
* Collect the permissions granted to this protection domain |
|
316 |
* so that the provider can be security checked while processing |
|
317 |
* calls to Policy.implies or Policy.getPermissions. |
|
318 |
*/ |
|
319 |
PermissionCollection policyPerms = null; |
|
320 |
synchronized (p) { |
|
321 |
if (p.pdMapping == null) { |
|
11034
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
322 |
p.pdMapping = new WeakHashMap<>(); |
2 | 323 |
} |
324 |
} |
|
325 |
||
326 |
if (policyDomain.getCodeSource() != null) { |
|
11034
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
327 |
Policy pol = policy.get().policy; |
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
328 |
if (pol != null) { |
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
329 |
policyPerms = pol.getPermissions(policyDomain); |
2 | 330 |
} |
331 |
||
332 |
if (policyPerms == null) { // assume it has all |
|
333 |
policyPerms = new Permissions(); |
|
334 |
policyPerms.add(SecurityConstants.ALL_PERMISSION); |
|
335 |
} |
|
336 |
||
337 |
synchronized (p.pdMapping) { |
|
338 |
// cache of pd to permissions |
|
5183
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
339 |
p.pdMapping.put(policyDomain.key, policyPerms); |
2 | 340 |
} |
341 |
} |
|
342 |
return; |
|
343 |
} |
|
344 |
||
345 |
||
346 |
/** |
|
347 |
* Returns a Policy object of the specified type. |
|
348 |
* |
|
349 |
* <p> This method traverses the list of registered security providers, |
|
350 |
* starting with the most preferred Provider. |
|
351 |
* A new Policy object encapsulating the |
|
352 |
* PolicySpi implementation from the first |
|
353 |
* Provider that supports the specified type is returned. |
|
354 |
* |
|
355 |
* <p> Note that the list of registered providers may be retrieved via |
|
356 |
* the {@link Security#getProviders() Security.getProviders()} method. |
|
357 |
* |
|
33241
27eb2d6abda9
8133151: Preferred provider configuration for JCE
ascarpino
parents:
30444
diff
changeset
|
358 |
* @implNote |
27eb2d6abda9
8133151: Preferred provider configuration for JCE
ascarpino
parents:
30444
diff
changeset
|
359 |
* The JDK Reference Implementation additionally uses the |
37348
9ccec3170d5e
8152205: jdk.security.provider.preferred is ambiguously documented
ascarpino
parents:
33241
diff
changeset
|
360 |
* {@code jdk.security.provider.preferred} |
9ccec3170d5e
8152205: jdk.security.provider.preferred is ambiguously documented
ascarpino
parents:
33241
diff
changeset
|
361 |
* {@link Security#getProperty(String) Security} property to determine |
33241
27eb2d6abda9
8133151: Preferred provider configuration for JCE
ascarpino
parents:
30444
diff
changeset
|
362 |
* the preferred provider order for the specified algorithm. This |
27eb2d6abda9
8133151: Preferred provider configuration for JCE
ascarpino
parents:
30444
diff
changeset
|
363 |
* may be different than the order of providers returned by |
27eb2d6abda9
8133151: Preferred provider configuration for JCE
ascarpino
parents:
30444
diff
changeset
|
364 |
* {@link Security#getProviders() Security.getProviders()}. |
27eb2d6abda9
8133151: Preferred provider configuration for JCE
ascarpino
parents:
30444
diff
changeset
|
365 |
* |
8152
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
366 |
* @param type the specified Policy type. See the Policy section in the |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
367 |
* <a href= |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
368 |
* "{@docRoot}/../technotes/guides/security/StandardNames.html#Policy"> |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
369 |
* Java Cryptography Architecture Standard Algorithm Name Documentation</a> |
2 | 370 |
* for a list of standard Policy types. |
371 |
* |
|
372 |
* @param params parameters for the Policy, which may be null. |
|
373 |
* |
|
374 |
* @return the new Policy object. |
|
375 |
* |
|
376 |
* @exception SecurityException if the caller does not have permission |
|
377 |
* to get a Policy instance for the specified type. |
|
378 |
* |
|
379 |
* @exception NullPointerException if the specified type is null. |
|
380 |
* |
|
381 |
* @exception IllegalArgumentException if the specified parameters |
|
382 |
* are not understood by the PolicySpi implementation |
|
383 |
* from the selected Provider. |
|
384 |
* |
|
385 |
* @exception NoSuchAlgorithmException if no Provider supports a PolicySpi |
|
386 |
* implementation for the specified type. |
|
387 |
* |
|
388 |
* @see Provider |
|
389 |
* @since 1.6 |
|
390 |
*/ |
|
391 |
public static Policy getInstance(String type, Policy.Parameters params) |
|
392 |
throws NoSuchAlgorithmException { |
|
393 |
||
394 |
checkPermission(type); |
|
395 |
try { |
|
396 |
GetInstance.Instance instance = GetInstance.getInstance("Policy", |
|
397 |
PolicySpi.class, |
|
398 |
type, |
|
399 |
params); |
|
400 |
return new PolicyDelegate((PolicySpi)instance.impl, |
|
401 |
instance.provider, |
|
402 |
type, |
|
403 |
params); |
|
404 |
} catch (NoSuchAlgorithmException nsae) { |
|
405 |
return handleException(nsae); |
|
406 |
} |
|
407 |
} |
|
408 |
||
409 |
/** |
|
410 |
* Returns a Policy object of the specified type. |
|
411 |
* |
|
412 |
* <p> A new Policy object encapsulating the |
|
413 |
* PolicySpi implementation from the specified provider |
|
414 |
* is returned. The specified provider must be registered |
|
415 |
* in the provider list. |
|
416 |
* |
|
417 |
* <p> Note that the list of registered providers may be retrieved via |
|
418 |
* the {@link Security#getProviders() Security.getProviders()} method. |
|
419 |
* |
|
8152
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
420 |
* @param type the specified Policy type. See the Policy section in the |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
421 |
* <a href= |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
422 |
* "{@docRoot}/../technotes/guides/security/StandardNames.html#Policy"> |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
423 |
* Java Cryptography Architecture Standard Algorithm Name Documentation</a> |
2 | 424 |
* for a list of standard Policy types. |
425 |
* |
|
426 |
* @param params parameters for the Policy, which may be null. |
|
427 |
* |
|
428 |
* @param provider the provider. |
|
429 |
* |
|
430 |
* @return the new Policy object. |
|
431 |
* |
|
432 |
* @exception SecurityException if the caller does not have permission |
|
433 |
* to get a Policy instance for the specified type. |
|
434 |
* |
|
435 |
* @exception NullPointerException if the specified type is null. |
|
436 |
* |
|
437 |
* @exception IllegalArgumentException if the specified provider |
|
438 |
* is null or empty, |
|
439 |
* or if the specified parameters are not understood by |
|
440 |
* the PolicySpi implementation from the specified provider. |
|
441 |
* |
|
442 |
* @exception NoSuchProviderException if the specified provider is not |
|
443 |
* registered in the security provider list. |
|
444 |
* |
|
445 |
* @exception NoSuchAlgorithmException if the specified provider does not |
|
446 |
* support a PolicySpi implementation for the specified type. |
|
447 |
* |
|
448 |
* @see Provider |
|
449 |
* @since 1.6 |
|
450 |
*/ |
|
451 |
public static Policy getInstance(String type, |
|
452 |
Policy.Parameters params, |
|
453 |
String provider) |
|
454 |
throws NoSuchProviderException, NoSuchAlgorithmException { |
|
455 |
||
456 |
if (provider == null || provider.length() == 0) { |
|
457 |
throw new IllegalArgumentException("missing provider"); |
|
458 |
} |
|
459 |
||
460 |
checkPermission(type); |
|
461 |
try { |
|
462 |
GetInstance.Instance instance = GetInstance.getInstance("Policy", |
|
463 |
PolicySpi.class, |
|
464 |
type, |
|
465 |
params, |
|
466 |
provider); |
|
467 |
return new PolicyDelegate((PolicySpi)instance.impl, |
|
468 |
instance.provider, |
|
469 |
type, |
|
470 |
params); |
|
471 |
} catch (NoSuchAlgorithmException nsae) { |
|
11034
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
472 |
return handleException(nsae); |
2 | 473 |
} |
474 |
} |
|
475 |
||
476 |
/** |
|
477 |
* Returns a Policy object of the specified type. |
|
478 |
* |
|
479 |
* <p> A new Policy object encapsulating the |
|
480 |
* PolicySpi implementation from the specified Provider |
|
481 |
* object is returned. Note that the specified Provider object |
|
482 |
* does not have to be registered in the provider list. |
|
483 |
* |
|
8152
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
484 |
* @param type the specified Policy type. See the Policy section in the |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
485 |
* <a href= |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
486 |
* "{@docRoot}/../technotes/guides/security/StandardNames.html#Policy"> |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
487 |
* Java Cryptography Architecture Standard Algorithm Name Documentation</a> |
2 | 488 |
* for a list of standard Policy types. |
489 |
* |
|
490 |
* @param params parameters for the Policy, which may be null. |
|
491 |
* |
|
492 |
* @param provider the Provider. |
|
493 |
* |
|
494 |
* @return the new Policy object. |
|
495 |
* |
|
496 |
* @exception SecurityException if the caller does not have permission |
|
497 |
* to get a Policy instance for the specified type. |
|
498 |
* |
|
499 |
* @exception NullPointerException if the specified type is null. |
|
500 |
* |
|
501 |
* @exception IllegalArgumentException if the specified Provider is null, |
|
502 |
* or if the specified parameters are not understood by |
|
503 |
* the PolicySpi implementation from the specified Provider. |
|
504 |
* |
|
505 |
* @exception NoSuchAlgorithmException if the specified Provider does not |
|
506 |
* support a PolicySpi implementation for the specified type. |
|
507 |
* |
|
508 |
* @see Provider |
|
509 |
* @since 1.6 |
|
510 |
*/ |
|
511 |
public static Policy getInstance(String type, |
|
512 |
Policy.Parameters params, |
|
513 |
Provider provider) |
|
514 |
throws NoSuchAlgorithmException { |
|
515 |
||
516 |
if (provider == null) { |
|
517 |
throw new IllegalArgumentException("missing provider"); |
|
518 |
} |
|
519 |
||
520 |
checkPermission(type); |
|
521 |
try { |
|
522 |
GetInstance.Instance instance = GetInstance.getInstance("Policy", |
|
523 |
PolicySpi.class, |
|
524 |
type, |
|
525 |
params, |
|
526 |
provider); |
|
527 |
return new PolicyDelegate((PolicySpi)instance.impl, |
|
528 |
instance.provider, |
|
529 |
type, |
|
530 |
params); |
|
531 |
} catch (NoSuchAlgorithmException nsae) { |
|
11034
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
532 |
return handleException(nsae); |
2 | 533 |
} |
534 |
} |
|
535 |
||
536 |
private static Policy handleException(NoSuchAlgorithmException nsae) |
|
537 |
throws NoSuchAlgorithmException { |
|
538 |
Throwable cause = nsae.getCause(); |
|
539 |
if (cause instanceof IllegalArgumentException) { |
|
540 |
throw (IllegalArgumentException)cause; |
|
541 |
} |
|
542 |
throw nsae; |
|
543 |
} |
|
544 |
||
545 |
/** |
|
546 |
* Return the Provider of this Policy. |
|
547 |
* |
|
548 |
* <p> This Policy instance will only have a Provider if it |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
14775
diff
changeset
|
549 |
* was obtained via a call to {@code Policy.getInstance}. |
2 | 550 |
* Otherwise this method returns null. |
551 |
* |
|
552 |
* @return the Provider of this Policy, or null. |
|
553 |
* |
|
554 |
* @since 1.6 |
|
555 |
*/ |
|
556 |
public Provider getProvider() { |
|
557 |
return null; |
|
558 |
} |
|
559 |
||
560 |
/** |
|
561 |
* Return the type of this Policy. |
|
562 |
* |
|
563 |
* <p> This Policy instance will only have a type if it |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
14775
diff
changeset
|
564 |
* was obtained via a call to {@code Policy.getInstance}. |
2 | 565 |
* Otherwise this method returns null. |
566 |
* |
|
567 |
* @return the type of this Policy, or null. |
|
568 |
* |
|
569 |
* @since 1.6 |
|
570 |
*/ |
|
571 |
public String getType() { |
|
572 |
return null; |
|
573 |
} |
|
574 |
||
575 |
/** |
|
576 |
* Return Policy parameters. |
|
577 |
* |
|
578 |
* <p> This Policy instance will only have parameters if it |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
14775
diff
changeset
|
579 |
* was obtained via a call to {@code Policy.getInstance}. |
2 | 580 |
* Otherwise this method returns null. |
581 |
* |
|
582 |
* @return Policy parameters, or null. |
|
583 |
* |
|
584 |
* @since 1.6 |
|
585 |
*/ |
|
586 |
public Policy.Parameters getParameters() { |
|
587 |
return null; |
|
588 |
} |
|
589 |
||
590 |
/** |
|
591 |
* Return a PermissionCollection object containing the set of |
|
592 |
* permissions granted to the specified CodeSource. |
|
593 |
* |
|
594 |
* <p> Applications are discouraged from calling this method |
|
595 |
* since this operation may not be supported by all policy implementations. |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
14775
diff
changeset
|
596 |
* Applications should solely rely on the {@code implies} method |
2 | 597 |
* to perform policy checks. If an application absolutely must call |
598 |
* a getPermissions method, it should call |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
14775
diff
changeset
|
599 |
* {@code getPermissions(ProtectionDomain)}. |
2 | 600 |
* |
601 |
* <p> The default implementation of this method returns |
|
602 |
* Policy.UNSUPPORTED_EMPTY_COLLECTION. This method can be |
|
603 |
* overridden if the policy implementation can return a set of |
|
604 |
* permissions granted to a CodeSource. |
|
605 |
* |
|
606 |
* @param codesource the CodeSource to which the returned |
|
607 |
* PermissionCollection has been granted. |
|
608 |
* |
|
609 |
* @return a set of permissions granted to the specified CodeSource. |
|
610 |
* If this operation is supported, the returned |
|
611 |
* set of permissions must be a new mutable instance |
|
612 |
* and it must support heterogeneous Permission types. |
|
613 |
* If this operation is not supported, |
|
614 |
* Policy.UNSUPPORTED_EMPTY_COLLECTION is returned. |
|
615 |
*/ |
|
616 |
public PermissionCollection getPermissions(CodeSource codesource) { |
|
617 |
return Policy.UNSUPPORTED_EMPTY_COLLECTION; |
|
618 |
} |
|
619 |
||
620 |
/** |
|
621 |
* Return a PermissionCollection object containing the set of |
|
622 |
* permissions granted to the specified ProtectionDomain. |
|
623 |
* |
|
624 |
* <p> Applications are discouraged from calling this method |
|
625 |
* since this operation may not be supported by all policy implementations. |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
14775
diff
changeset
|
626 |
* Applications should rely on the {@code implies} method |
2 | 627 |
* to perform policy checks. |
628 |
* |
|
629 |
* <p> The default implementation of this method first retrieves |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
14775
diff
changeset
|
630 |
* the permissions returned via {@code getPermissions(CodeSource)} |
2 | 631 |
* (the CodeSource is taken from the specified ProtectionDomain), |
632 |
* as well as the permissions located inside the specified ProtectionDomain. |
|
633 |
* All of these permissions are then combined and returned in a new |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
14775
diff
changeset
|
634 |
* PermissionCollection object. If {@code getPermissions(CodeSource)} |
2 | 635 |
* returns Policy.UNSUPPORTED_EMPTY_COLLECTION, then this method |
636 |
* returns the permissions contained inside the specified ProtectionDomain |
|
637 |
* in a new PermissionCollection object. |
|
638 |
* |
|
639 |
* <p> This method can be overridden if the policy implementation |
|
640 |
* supports returning a set of permissions granted to a ProtectionDomain. |
|
641 |
* |
|
642 |
* @param domain the ProtectionDomain to which the returned |
|
643 |
* PermissionCollection has been granted. |
|
644 |
* |
|
645 |
* @return a set of permissions granted to the specified ProtectionDomain. |
|
646 |
* If this operation is supported, the returned |
|
647 |
* set of permissions must be a new mutable instance |
|
648 |
* and it must support heterogeneous Permission types. |
|
649 |
* If this operation is not supported, |
|
650 |
* Policy.UNSUPPORTED_EMPTY_COLLECTION is returned. |
|
651 |
* |
|
652 |
* @since 1.4 |
|
653 |
*/ |
|
654 |
public PermissionCollection getPermissions(ProtectionDomain domain) { |
|
655 |
PermissionCollection pc = null; |
|
656 |
||
657 |
if (domain == null) |
|
658 |
return new Permissions(); |
|
659 |
||
660 |
if (pdMapping == null) { |
|
661 |
initPolicy(this); |
|
662 |
} |
|
663 |
||
664 |
synchronized (pdMapping) { |
|
5183
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
665 |
pc = pdMapping.get(domain.key); |
2 | 666 |
} |
667 |
||
668 |
if (pc != null) { |
|
669 |
Permissions perms = new Permissions(); |
|
670 |
synchronized (pc) { |
|
671 |
for (Enumeration<Permission> e = pc.elements() ; e.hasMoreElements() ;) { |
|
672 |
perms.add(e.nextElement()); |
|
673 |
} |
|
674 |
} |
|
675 |
return perms; |
|
676 |
} |
|
677 |
||
678 |
pc = getPermissions(domain.getCodeSource()); |
|
679 |
if (pc == null || pc == UNSUPPORTED_EMPTY_COLLECTION) { |
|
680 |
pc = new Permissions(); |
|
681 |
} |
|
682 |
||
683 |
addStaticPerms(pc, domain.getPermissions()); |
|
684 |
return pc; |
|
685 |
} |
|
686 |
||
687 |
/** |
|
688 |
* add static permissions to provided permission collection |
|
689 |
*/ |
|
690 |
private void addStaticPerms(PermissionCollection perms, |
|
691 |
PermissionCollection statics) { |
|
692 |
if (statics != null) { |
|
693 |
synchronized (statics) { |
|
694 |
Enumeration<Permission> e = statics.elements(); |
|
695 |
while (e.hasMoreElements()) { |
|
696 |
perms.add(e.nextElement()); |
|
697 |
} |
|
698 |
} |
|
699 |
} |
|
700 |
} |
|
701 |
||
702 |
/** |
|
703 |
* Evaluates the global policy for the permissions granted to |
|
704 |
* the ProtectionDomain and tests whether the permission is |
|
705 |
* granted. |
|
706 |
* |
|
707 |
* @param domain the ProtectionDomain to test |
|
708 |
* @param permission the Permission object to be tested for implication. |
|
709 |
* |
|
710 |
* @return true if "permission" is a proper subset of a permission |
|
711 |
* granted to this ProtectionDomain. |
|
712 |
* |
|
713 |
* @see java.security.ProtectionDomain |
|
714 |
* @since 1.4 |
|
715 |
*/ |
|
716 |
public boolean implies(ProtectionDomain domain, Permission permission) { |
|
717 |
PermissionCollection pc; |
|
718 |
||
719 |
if (pdMapping == null) { |
|
720 |
initPolicy(this); |
|
721 |
} |
|
722 |
||
723 |
synchronized (pdMapping) { |
|
5183
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
724 |
pc = pdMapping.get(domain.key); |
2 | 725 |
} |
726 |
||
727 |
if (pc != null) { |
|
728 |
return pc.implies(permission); |
|
729 |
} |
|
730 |
||
731 |
pc = getPermissions(domain); |
|
732 |
if (pc == null) { |
|
733 |
return false; |
|
734 |
} |
|
735 |
||
736 |
synchronized (pdMapping) { |
|
737 |
// cache it |
|
5183
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
738 |
pdMapping.put(domain.key, pc); |
2 | 739 |
} |
740 |
||
741 |
return pc.implies(permission); |
|
742 |
} |
|
743 |
||
744 |
/** |
|
745 |
* Refreshes/reloads the policy configuration. The behavior of this method |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
14775
diff
changeset
|
746 |
* depends on the implementation. For example, calling {@code refresh} |
2 | 747 |
* on a file-based policy will cause the file to be re-read. |
748 |
* |
|
749 |
* <p> The default implementation of this method does nothing. |
|
750 |
* This method should be overridden if a refresh operation is supported |
|
751 |
* by the policy implementation. |
|
752 |
*/ |
|
753 |
public void refresh() { } |
|
754 |
||
755 |
/** |
|
756 |
* This subclass is returned by the getInstance calls. All Policy calls |
|
757 |
* are delegated to the underlying PolicySpi. |
|
758 |
*/ |
|
759 |
private static class PolicyDelegate extends Policy { |
|
760 |
||
761 |
private PolicySpi spi; |
|
762 |
private Provider p; |
|
763 |
private String type; |
|
764 |
private Policy.Parameters params; |
|
765 |
||
766 |
private PolicyDelegate(PolicySpi spi, Provider p, |
|
767 |
String type, Policy.Parameters params) { |
|
768 |
this.spi = spi; |
|
769 |
this.p = p; |
|
770 |
this.type = type; |
|
771 |
this.params = params; |
|
772 |
} |
|
773 |
||
5183
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
774 |
@Override public String getType() { return type; } |
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
775 |
|
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
776 |
@Override public Policy.Parameters getParameters() { return params; } |
2 | 777 |
|
5183
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
778 |
@Override public Provider getProvider() { return p; } |
2 | 779 |
|
5183
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
780 |
@Override |
2 | 781 |
public PermissionCollection getPermissions(CodeSource codesource) { |
782 |
return spi.engineGetPermissions(codesource); |
|
783 |
} |
|
5183
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
784 |
@Override |
2 | 785 |
public PermissionCollection getPermissions(ProtectionDomain domain) { |
786 |
return spi.engineGetPermissions(domain); |
|
787 |
} |
|
5183
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
788 |
@Override |
2 | 789 |
public boolean implies(ProtectionDomain domain, Permission perm) { |
790 |
return spi.engineImplies(domain, perm); |
|
791 |
} |
|
5183
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
792 |
@Override |
2 | 793 |
public void refresh() { |
794 |
spi.engineRefresh(); |
|
795 |
} |
|
796 |
} |
|
797 |
||
798 |
/** |
|
799 |
* This represents a marker interface for Policy parameters. |
|
800 |
* |
|
801 |
* @since 1.6 |
|
802 |
*/ |
|
803 |
public static interface Parameters { } |
|
804 |
||
805 |
/** |
|
806 |
* This class represents a read-only empty PermissionCollection object that |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
14775
diff
changeset
|
807 |
* is returned from the {@code getPermissions(CodeSource)} and |
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
14775
diff
changeset
|
808 |
* {@code getPermissions(ProtectionDomain)} |
2 | 809 |
* methods in the Policy class when those operations are not |
810 |
* supported by the Policy implementation. |
|
811 |
*/ |
|
812 |
private static class UnsupportedEmptyCollection |
|
813 |
extends PermissionCollection { |
|
814 |
||
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
815 |
private static final long serialVersionUID = -8492269157353014774L; |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
816 |
|
2 | 817 |
private Permissions perms; |
818 |
||
819 |
/** |
|
820 |
* Create a read-only empty PermissionCollection object. |
|
821 |
*/ |
|
822 |
public UnsupportedEmptyCollection() { |
|
823 |
this.perms = new Permissions(); |
|
824 |
perms.setReadOnly(); |
|
825 |
} |
|
826 |
||
827 |
/** |
|
828 |
* Adds a permission object to the current collection of permission |
|
829 |
* objects. |
|
830 |
* |
|
831 |
* @param permission the Permission object to add. |
|
832 |
* |
|
833 |
* @exception SecurityException - if this PermissionCollection object |
|
834 |
* has been marked readonly |
|
835 |
*/ |
|
5183
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
836 |
@Override public void add(Permission permission) { |
2 | 837 |
perms.add(permission); |
838 |
} |
|
839 |
||
840 |
/** |
|
841 |
* Checks to see if the specified permission is implied by the |
|
842 |
* collection of Permission objects held in this PermissionCollection. |
|
843 |
* |
|
844 |
* @param permission the Permission object to compare. |
|
845 |
* |
|
11034
6ec8b7a32dbc
7093090: Reduce synchronization in java.security.Policy.getPolicyNoCheck
mullan
parents:
10336
diff
changeset
|
846 |
* @return true if "permission" is implied by the permissions in |
2 | 847 |
* the collection, false if not. |
848 |
*/ |
|
5183
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
849 |
@Override public boolean implies(Permission permission) { |
2 | 850 |
return perms.implies(permission); |
851 |
} |
|
852 |
||
853 |
/** |
|
854 |
* Returns an enumeration of all the Permission objects in the |
|
855 |
* collection. |
|
856 |
* |
|
857 |
* @return an enumeration of all the Permissions. |
|
858 |
*/ |
|
5183
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
859 |
@Override public Enumeration<Permission> elements() { |
2 | 860 |
return perms.elements(); |
861 |
} |
|
862 |
} |
|
863 |
} |