author | jjg |
Mon, 15 Aug 2011 11:48:20 -0700 | |
changeset 10336 | 0bb1999251f8 |
parent 5506 | 202f599c92aa |
child 13558 | 3fb57310d83b |
permissions | -rw-r--r-- |
2 | 1 |
/* |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 1999, 2011, 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 |
package javax.crypto; |
|
27 |
||
28 |
import java.security.*; |
|
29 |
import java.net.*; |
|
30 |
import java.util.*; |
|
31 |
||
32 |
/** |
|
33 |
* The JCE security manager. |
|
34 |
* |
|
35 |
* <p>The JCE security manager is responsible for determining the maximum |
|
36 |
* allowable cryptographic strength for a given applet/application, for a given |
|
37 |
* algorithm, by consulting the configured jurisdiction policy files and |
|
38 |
* the cryptographic permissions bundled with the applet/application. |
|
39 |
* |
|
40 |
* <p>Note that this security manager is never installed, only instantiated. |
|
41 |
* |
|
42 |
* @author Jan Luehe |
|
43 |
* |
|
44 |
* @since 1.4 |
|
45 |
*/ |
|
46 |
||
47 |
final class JceSecurityManager extends SecurityManager { |
|
48 |
||
49 |
private static final CryptoPermissions defaultPolicy; |
|
50 |
private static final CryptoPermissions exemptPolicy; |
|
51 |
private static final CryptoAllPermission allPerm; |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
52 |
private static final Vector<Class<?>> TrustedCallersCache = |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
53 |
new Vector<>(2); |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
54 |
private static final Map<URL, CryptoPermissions> exemptCache = |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
55 |
new HashMap<>(); |
2 | 56 |
|
57 |
// singleton instance |
|
58 |
static final JceSecurityManager INSTANCE; |
|
59 |
||
60 |
static { |
|
61 |
defaultPolicy = JceSecurity.getDefaultPolicy(); |
|
62 |
exemptPolicy = JceSecurity.getExemptPolicy(); |
|
63 |
allPerm = CryptoAllPermission.INSTANCE; |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
64 |
INSTANCE = AccessController.doPrivileged( |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
65 |
new PrivilegedAction<JceSecurityManager>() { |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
66 |
public JceSecurityManager run() { |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
67 |
return new JceSecurityManager(); |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
68 |
} |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
69 |
}); |
2 | 70 |
} |
71 |
||
72 |
private JceSecurityManager() { |
|
73 |
// empty |
|
74 |
} |
|
75 |
||
76 |
/** |
|
77 |
* Returns the maximum allowable crypto strength for the given |
|
78 |
* applet/application, for the given algorithm. |
|
79 |
*/ |
|
80 |
CryptoPermission getCryptoPermission(String alg) { |
|
81 |
// Need to convert to uppercase since the crypto perm |
|
82 |
// lookup is case sensitive. |
|
83 |
alg = alg.toUpperCase(Locale.ENGLISH); |
|
84 |
||
85 |
// If CryptoAllPermission is granted by default, we return that. |
|
86 |
// Otherwise, this will be the permission we return if anything goes |
|
87 |
// wrong. |
|
88 |
CryptoPermission defaultPerm = getDefaultPermission(alg); |
|
89 |
if (defaultPerm == CryptoAllPermission.INSTANCE) { |
|
90 |
return defaultPerm; |
|
91 |
} |
|
92 |
||
93 |
// Determine the codebase of the caller of the JCE API. |
|
94 |
// This is the codebase of the first class which is not in |
|
95 |
// javax.crypto.* packages. |
|
96 |
// NOTE: javax.crypto.* package maybe subject to package |
|
97 |
// insertion, so need to check its classloader as well. |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
98 |
Class<?>[] context = getClassContext(); |
2 | 99 |
URL callerCodeBase = null; |
100 |
int i; |
|
101 |
for (i=0; i<context.length; i++) { |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
102 |
Class<?> cls = context[i]; |
2 | 103 |
callerCodeBase = JceSecurity.getCodeBase(cls); |
104 |
if (callerCodeBase != null) { |
|
105 |
break; |
|
106 |
} else { |
|
107 |
if (cls.getName().startsWith("javax.crypto.")) { |
|
108 |
// skip jce classes since they aren't the callers |
|
109 |
continue; |
|
110 |
} |
|
111 |
// use default permission when the caller is system classes |
|
112 |
return defaultPerm; |
|
113 |
} |
|
114 |
} |
|
115 |
||
116 |
if (i == context.length) { |
|
117 |
return defaultPerm; |
|
118 |
} |
|
119 |
||
120 |
CryptoPermissions appPerms; |
|
121 |
synchronized (this.getClass()) { |
|
122 |
if (exemptCache.containsKey(callerCodeBase)) { |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
123 |
appPerms = exemptCache.get(callerCodeBase); |
2 | 124 |
} else { |
125 |
appPerms = getAppPermissions(callerCodeBase); |
|
126 |
exemptCache.put(callerCodeBase, appPerms); |
|
127 |
} |
|
128 |
} |
|
129 |
||
130 |
if (appPerms == null) { |
|
131 |
return defaultPerm; |
|
132 |
} |
|
133 |
||
134 |
// If the app was granted the special CryptoAllPermission, return that. |
|
135 |
if (appPerms.implies(allPerm)) { |
|
136 |
return allPerm; |
|
137 |
} |
|
138 |
||
139 |
// Check if the crypto permissions granted to the app contain a |
|
140 |
// crypto permission for the requested algorithm that does not require |
|
141 |
// any exemption mechanism to be enforced. |
|
142 |
// Return that permission, if present. |
|
143 |
PermissionCollection appPc = appPerms.getPermissionCollection(alg); |
|
144 |
if (appPc == null) { |
|
145 |
return defaultPerm; |
|
146 |
} |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
147 |
Enumeration<Permission> enum_ = appPc.elements(); |
2 | 148 |
while (enum_.hasMoreElements()) { |
149 |
CryptoPermission cp = (CryptoPermission)enum_.nextElement(); |
|
150 |
if (cp.getExemptionMechanism() == null) { |
|
151 |
return cp; |
|
152 |
} |
|
153 |
} |
|
154 |
||
155 |
// Check if the jurisdiction file for exempt applications contains |
|
156 |
// any entries for the requested algorithm. |
|
157 |
// If not, return the default permission. |
|
158 |
PermissionCollection exemptPc = |
|
159 |
exemptPolicy.getPermissionCollection(alg); |
|
160 |
if (exemptPc == null) { |
|
161 |
return defaultPerm; |
|
162 |
} |
|
163 |
||
164 |
// In the jurisdiction file for exempt applications, go through the |
|
165 |
// list of CryptoPermission entries for the requested algorithm, and |
|
166 |
// stop at the first entry: |
|
167 |
// - that is implied by the collection of crypto permissions granted |
|
168 |
// to the app, and |
|
169 |
// - whose exemption mechanism is available from one of the |
|
170 |
// registered CSPs |
|
171 |
enum_ = exemptPc.elements(); |
|
172 |
while (enum_.hasMoreElements()) { |
|
173 |
CryptoPermission cp = (CryptoPermission)enum_.nextElement(); |
|
174 |
try { |
|
175 |
ExemptionMechanism.getInstance(cp.getExemptionMechanism()); |
|
176 |
if (cp.getAlgorithm().equals( |
|
177 |
CryptoPermission.ALG_NAME_WILDCARD)) { |
|
178 |
CryptoPermission newCp; |
|
179 |
if (cp.getCheckParam()) { |
|
180 |
newCp = new CryptoPermission( |
|
181 |
alg, cp.getMaxKeySize(), |
|
182 |
cp.getAlgorithmParameterSpec(), |
|
183 |
cp.getExemptionMechanism()); |
|
184 |
} else { |
|
185 |
newCp = new CryptoPermission( |
|
186 |
alg, cp.getMaxKeySize(), |
|
187 |
cp.getExemptionMechanism()); |
|
188 |
} |
|
189 |
if (appPerms.implies(newCp)) { |
|
190 |
return newCp; |
|
191 |
} |
|
192 |
} |
|
193 |
||
194 |
if (appPerms.implies(cp)) { |
|
195 |
return cp; |
|
196 |
} |
|
197 |
} catch (Exception e) { |
|
198 |
continue; |
|
199 |
} |
|
200 |
} |
|
201 |
return defaultPerm; |
|
202 |
} |
|
203 |
||
204 |
private static CryptoPermissions getAppPermissions(URL callerCodeBase) { |
|
205 |
// Check if app is exempt, and retrieve the permissions bundled with it |
|
206 |
try { |
|
207 |
return JceSecurity.verifyExemptJar(callerCodeBase); |
|
208 |
} catch (Exception e) { |
|
209 |
// Jar verification fails |
|
210 |
return null; |
|
211 |
} |
|
212 |
||
213 |
} |
|
214 |
||
215 |
/** |
|
216 |
* Returns the default permission for the given algorithm. |
|
217 |
*/ |
|
218 |
private CryptoPermission getDefaultPermission(String alg) { |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
219 |
Enumeration<Permission> enum_ = |
2 | 220 |
defaultPolicy.getPermissionCollection(alg).elements(); |
221 |
return (CryptoPermission)enum_.nextElement(); |
|
222 |
} |
|
223 |
||
224 |
// See bug 4341369 & 4334690 for more info. |
|
225 |
boolean isCallerTrusted() { |
|
226 |
// Get the caller and its codebase. |
|
227 |
Class[] context = getClassContext(); |
|
228 |
URL callerCodeBase = null; |
|
229 |
int i; |
|
230 |
for (i=0; i<context.length; i++) { |
|
231 |
callerCodeBase = JceSecurity.getCodeBase(context[i]); |
|
232 |
if (callerCodeBase != null) { |
|
233 |
break; |
|
234 |
} |
|
235 |
} |
|
236 |
// The caller is in the JCE framework. |
|
237 |
if (i == context.length) { |
|
238 |
return true; |
|
239 |
} |
|
240 |
//The caller has been verified. |
|
241 |
if (TrustedCallersCache.contains(context[i])) { |
|
242 |
return true; |
|
243 |
} |
|
244 |
// Check whether the caller is a trusted provider. |
|
245 |
try { |
|
246 |
JceSecurity.verifyProviderJar(callerCodeBase); |
|
247 |
} catch (Exception e2) { |
|
248 |
return false; |
|
249 |
} |
|
250 |
TrustedCallersCache.addElement(context[i]); |
|
251 |
return true; |
|
252 |
} |
|
253 |
} |