author | wetmore |
Mon, 05 Dec 2016 17:04:02 -0800 | |
changeset 42365 | 5e640c2994d6 |
parent 40565 | jdk/src/java.base/share/classes/javax/crypto/JceSecurity.java@3ac0ba151e70 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
36119 | 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 |
||
42365
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
26 |
/* |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
27 |
* README README README README README README README README README |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
28 |
* |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
29 |
* This file is the template for generating the JceSecurity.java source |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
30 |
* file. |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
31 |
* |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
32 |
* In the current jdk builds, this file is first preprocessed to replace |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
33 |
* @@JCE_DEFAULT_POLICY@ [sic] with "limited" or "unlimited" which is |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
34 |
* determined by the $(UNLIMTED_CRYPTO) make variable. This variable is |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
35 |
* set by top-level configure script, using either |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
36 |
* --disable-unlimited-crypto or --enable-unlimited-crypto [default]. |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
37 |
* |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
38 |
* Since this file is a generated source, incremental changes to |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
39 |
* this file require regenerating the source. Compilation options: |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
40 |
* |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
41 |
* (fewer dependencies/"faster" ones first) |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
42 |
* |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
43 |
* 1. make JDK_FILTER=javax/crypto java.base-gensrc-only java.base-java-only |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
44 |
* 2. make java.base-gensrc-only java.base-java-only |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
45 |
* 3. make java.base-gensrc-only java.base-only |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
46 |
* 4. make java.base-only |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
47 |
* 5. make |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
48 |
*/ |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
49 |
|
2 | 50 |
package javax.crypto; |
51 |
||
52 |
import java.util.*; |
|
53 |
import java.io.*; |
|
54 |
import java.net.URL; |
|
40565
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
55 |
import java.nio.file.*; |
2 | 56 |
import java.security.*; |
57 |
||
58 |
import java.security.Provider.Service; |
|
59 |
||
60 |
import sun.security.jca.*; |
|
61 |
import sun.security.jca.GetInstance.Instance; |
|
42365
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
62 |
import sun.security.util.Debug; |
2 | 63 |
|
64 |
/** |
|
65 |
* This class instantiates implementations of JCE engine classes from |
|
66 |
* providers registered with the java.security.Security object. |
|
67 |
* |
|
68 |
* @author Jan Luehe |
|
69 |
* @author Sharon Liu |
|
70 |
* @since 1.4 |
|
71 |
*/ |
|
72 |
||
73 |
final class JceSecurity { |
|
42365
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
74 |
|
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
75 |
|
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
76 |
private static final Debug debug = Debug.getInstance("jca"); |
2 | 77 |
|
78 |
static final SecureRandom RANDOM = new SecureRandom(); |
|
79 |
||
80 |
// The defaultPolicy and exemptPolicy will be set up |
|
81 |
// in the static initializer. |
|
82 |
private static CryptoPermissions defaultPolicy = null; |
|
83 |
private static CryptoPermissions exemptPolicy = null; |
|
84 |
||
85 |
// Map<Provider,?> of the providers we already have verified |
|
86 |
// value == PROVIDER_VERIFIED is successfully verified |
|
87 |
// value is failure cause Exception in error case |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
30033
diff
changeset
|
88 |
private static final Map<Provider, Object> verificationResults = |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
89 |
new IdentityHashMap<>(); |
2 | 90 |
|
91 |
// Map<Provider,?> of the providers currently being verified |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
30033
diff
changeset
|
92 |
private static final Map<Provider, Object> verifyingProviders = |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
93 |
new IdentityHashMap<>(); |
2 | 94 |
|
36119 | 95 |
private static final boolean isRestricted; |
2 | 96 |
|
97 |
/* |
|
98 |
* Don't let anyone instantiate this. |
|
99 |
*/ |
|
100 |
private JceSecurity() { |
|
101 |
} |
|
102 |
||
103 |
static { |
|
104 |
try { |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
105 |
AccessController.doPrivileged( |
30033
b9c86c17164a
8078468: Update security libraries to use diamond with anonymous classes
darcy
parents:
26861
diff
changeset
|
106 |
new PrivilegedExceptionAction<> () { |
26861
47dde7f5cf36
8058845: Update JCE environment for build improvements
wetmore
parents:
25859
diff
changeset
|
107 |
@Override |
47dde7f5cf36
8058845: Update JCE environment for build improvements
wetmore
parents:
25859
diff
changeset
|
108 |
public Void run() throws Exception { |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
109 |
setupJurisdictionPolicies(); |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
110 |
return null; |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
111 |
} |
26861
47dde7f5cf36
8058845: Update JCE environment for build improvements
wetmore
parents:
25859
diff
changeset
|
112 |
} |
47dde7f5cf36
8058845: Update JCE environment for build improvements
wetmore
parents:
25859
diff
changeset
|
113 |
); |
2 | 114 |
|
115 |
isRestricted = defaultPolicy.implies( |
|
116 |
CryptoAllPermission.INSTANCE) ? false : true; |
|
117 |
} catch (Exception e) { |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
118 |
throw new SecurityException( |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
119 |
"Can not initialize cryptographic mechanism", e); |
2 | 120 |
} |
121 |
} |
|
122 |
||
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
123 |
static Instance getInstance(String type, Class<?> clazz, String algorithm, |
2 | 124 |
String provider) throws NoSuchAlgorithmException, |
125 |
NoSuchProviderException { |
|
126 |
Service s = GetInstance.getService(type, algorithm, provider); |
|
127 |
Exception ve = getVerificationResult(s.getProvider()); |
|
128 |
if (ve != null) { |
|
129 |
String msg = "JCE cannot authenticate the provider " + provider; |
|
130 |
throw (NoSuchProviderException) |
|
131 |
new NoSuchProviderException(msg).initCause(ve); |
|
132 |
} |
|
133 |
return GetInstance.getInstance(s, clazz); |
|
134 |
} |
|
135 |
||
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
136 |
static Instance getInstance(String type, Class<?> clazz, String algorithm, |
2 | 137 |
Provider provider) throws NoSuchAlgorithmException { |
138 |
Service s = GetInstance.getService(type, algorithm, provider); |
|
139 |
Exception ve = JceSecurity.getVerificationResult(provider); |
|
140 |
if (ve != null) { |
|
141 |
String msg = "JCE cannot authenticate the provider " |
|
142 |
+ provider.getName(); |
|
143 |
throw new SecurityException(msg, ve); |
|
144 |
} |
|
145 |
return GetInstance.getInstance(s, clazz); |
|
146 |
} |
|
147 |
||
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
148 |
static Instance getInstance(String type, Class<?> clazz, String algorithm) |
2 | 149 |
throws NoSuchAlgorithmException { |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
150 |
List<Service> services = GetInstance.getServices(type, algorithm); |
2 | 151 |
NoSuchAlgorithmException failure = null; |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
152 |
for (Service s : services) { |
2 | 153 |
if (canUseProvider(s.getProvider()) == false) { |
154 |
// allow only signed providers |
|
155 |
continue; |
|
156 |
} |
|
157 |
try { |
|
158 |
Instance instance = GetInstance.getInstance(s, clazz); |
|
159 |
return instance; |
|
160 |
} catch (NoSuchAlgorithmException e) { |
|
161 |
failure = e; |
|
162 |
} |
|
163 |
} |
|
164 |
throw new NoSuchAlgorithmException("Algorithm " + algorithm |
|
165 |
+ " not available", failure); |
|
166 |
} |
|
167 |
||
168 |
/** |
|
169 |
* Verify if the JAR at URL codeBase is a signed exempt application |
|
170 |
* JAR file and returns the permissions bundled with the JAR. |
|
171 |
* |
|
172 |
* @throws Exception on error |
|
173 |
*/ |
|
174 |
static CryptoPermissions verifyExemptJar(URL codeBase) throws Exception { |
|
26861
47dde7f5cf36
8058845: Update JCE environment for build improvements
wetmore
parents:
25859
diff
changeset
|
175 |
ProviderVerifier pv = new ProviderVerifier(codeBase, true); |
47dde7f5cf36
8058845: Update JCE environment for build improvements
wetmore
parents:
25859
diff
changeset
|
176 |
pv.verify(); |
47dde7f5cf36
8058845: Update JCE environment for build improvements
wetmore
parents:
25859
diff
changeset
|
177 |
return pv.getPermissions(); |
2 | 178 |
} |
179 |
||
180 |
/** |
|
181 |
* Verify if the JAR at URL codeBase is a signed provider JAR file. |
|
182 |
* |
|
183 |
* @throws Exception on error |
|
184 |
*/ |
|
26861
47dde7f5cf36
8058845: Update JCE environment for build improvements
wetmore
parents:
25859
diff
changeset
|
185 |
static void verifyProvider(URL codeBase, Provider p) throws Exception { |
2 | 186 |
// Verify the provider JAR file and all |
187 |
// supporting JAR files if there are any. |
|
26861
47dde7f5cf36
8058845: Update JCE environment for build improvements
wetmore
parents:
25859
diff
changeset
|
188 |
ProviderVerifier pv = new ProviderVerifier(codeBase, p, false); |
47dde7f5cf36
8058845: Update JCE environment for build improvements
wetmore
parents:
25859
diff
changeset
|
189 |
pv.verify(); |
2 | 190 |
} |
191 |
||
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
30033
diff
changeset
|
192 |
private static final Object PROVIDER_VERIFIED = Boolean.TRUE; |
2 | 193 |
|
194 |
/* |
|
195 |
* Verify that the provider JAR files are signed properly, which |
|
196 |
* means the signer's certificate can be traced back to a |
|
197 |
* JCE trusted CA. |
|
198 |
* Return null if ok, failure Exception if verification failed. |
|
199 |
*/ |
|
200 |
static synchronized Exception getVerificationResult(Provider p) { |
|
201 |
Object o = verificationResults.get(p); |
|
202 |
if (o == PROVIDER_VERIFIED) { |
|
203 |
return null; |
|
204 |
} else if (o != null) { |
|
205 |
return (Exception)o; |
|
206 |
} |
|
207 |
if (verifyingProviders.get(p) != null) { |
|
208 |
// this method is static synchronized, must be recursion |
|
209 |
// return failure now but do not save the result |
|
210 |
return new NoSuchProviderException("Recursion during verification"); |
|
211 |
} |
|
212 |
try { |
|
213 |
verifyingProviders.put(p, Boolean.FALSE); |
|
214 |
URL providerURL = getCodeBase(p.getClass()); |
|
26861
47dde7f5cf36
8058845: Update JCE environment for build improvements
wetmore
parents:
25859
diff
changeset
|
215 |
verifyProvider(providerURL, p); |
2 | 216 |
// Verified ok, cache result |
217 |
verificationResults.put(p, PROVIDER_VERIFIED); |
|
218 |
return null; |
|
219 |
} catch (Exception e) { |
|
220 |
verificationResults.put(p, e); |
|
221 |
return e; |
|
222 |
} finally { |
|
223 |
verifyingProviders.remove(p); |
|
224 |
} |
|
225 |
} |
|
226 |
||
227 |
// return whether this provider is properly signed and can be used by JCE |
|
228 |
static boolean canUseProvider(Provider p) { |
|
229 |
return getVerificationResult(p) == null; |
|
230 |
} |
|
231 |
||
232 |
// dummy object to represent null |
|
233 |
private static final URL NULL_URL; |
|
234 |
||
235 |
static { |
|
236 |
try { |
|
40565
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
237 |
NULL_URL = new URL("http://null.oracle.com/"); |
2 | 238 |
} catch (Exception e) { |
239 |
throw new RuntimeException(e); |
|
240 |
} |
|
241 |
} |
|
242 |
||
243 |
// reference to a Map we use as a cache for codebases |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
244 |
private static final Map<Class<?>, URL> codeBaseCacheRef = |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
245 |
new WeakHashMap<>(); |
2 | 246 |
|
247 |
/* |
|
24501
767c30e88a61
8028627: Unsynchronized code path from javax.crypto.Cipher to the WeakHashMap used by JceSecurity to store codebase mappings
robm
parents:
10336
diff
changeset
|
248 |
* Returns the CodeBase for the given class. |
2 | 249 |
*/ |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
250 |
static URL getCodeBase(final Class<?> clazz) { |
24501
767c30e88a61
8028627: Unsynchronized code path from javax.crypto.Cipher to the WeakHashMap used by JceSecurity to store codebase mappings
robm
parents:
10336
diff
changeset
|
251 |
synchronized (codeBaseCacheRef) { |
767c30e88a61
8028627: Unsynchronized code path from javax.crypto.Cipher to the WeakHashMap used by JceSecurity to store codebase mappings
robm
parents:
10336
diff
changeset
|
252 |
URL url = codeBaseCacheRef.get(clazz); |
767c30e88a61
8028627: Unsynchronized code path from javax.crypto.Cipher to the WeakHashMap used by JceSecurity to store codebase mappings
robm
parents:
10336
diff
changeset
|
253 |
if (url == null) { |
26861
47dde7f5cf36
8058845: Update JCE environment for build improvements
wetmore
parents:
25859
diff
changeset
|
254 |
url = AccessController.doPrivileged( |
30033
b9c86c17164a
8078468: Update security libraries to use diamond with anonymous classes
darcy
parents:
26861
diff
changeset
|
255 |
new PrivilegedAction<>() { |
26861
47dde7f5cf36
8058845: Update JCE environment for build improvements
wetmore
parents:
25859
diff
changeset
|
256 |
@Override |
47dde7f5cf36
8058845: Update JCE environment for build improvements
wetmore
parents:
25859
diff
changeset
|
257 |
public URL run() { |
47dde7f5cf36
8058845: Update JCE environment for build improvements
wetmore
parents:
25859
diff
changeset
|
258 |
ProtectionDomain pd = clazz.getProtectionDomain(); |
47dde7f5cf36
8058845: Update JCE environment for build improvements
wetmore
parents:
25859
diff
changeset
|
259 |
if (pd != null) { |
47dde7f5cf36
8058845: Update JCE environment for build improvements
wetmore
parents:
25859
diff
changeset
|
260 |
CodeSource cs = pd.getCodeSource(); |
47dde7f5cf36
8058845: Update JCE environment for build improvements
wetmore
parents:
25859
diff
changeset
|
261 |
if (cs != null) { |
47dde7f5cf36
8058845: Update JCE environment for build improvements
wetmore
parents:
25859
diff
changeset
|
262 |
return cs.getLocation(); |
47dde7f5cf36
8058845: Update JCE environment for build improvements
wetmore
parents:
25859
diff
changeset
|
263 |
} |
24501
767c30e88a61
8028627: Unsynchronized code path from javax.crypto.Cipher to the WeakHashMap used by JceSecurity to store codebase mappings
robm
parents:
10336
diff
changeset
|
264 |
} |
26861
47dde7f5cf36
8058845: Update JCE environment for build improvements
wetmore
parents:
25859
diff
changeset
|
265 |
return NULL_URL; |
2 | 266 |
} |
26861
47dde7f5cf36
8058845: Update JCE environment for build improvements
wetmore
parents:
25859
diff
changeset
|
267 |
}); |
24501
767c30e88a61
8028627: Unsynchronized code path from javax.crypto.Cipher to the WeakHashMap used by JceSecurity to store codebase mappings
robm
parents:
10336
diff
changeset
|
268 |
codeBaseCacheRef.put(clazz, url); |
767c30e88a61
8028627: Unsynchronized code path from javax.crypto.Cipher to the WeakHashMap used by JceSecurity to store codebase mappings
robm
parents:
10336
diff
changeset
|
269 |
} |
767c30e88a61
8028627: Unsynchronized code path from javax.crypto.Cipher to the WeakHashMap used by JceSecurity to store codebase mappings
robm
parents:
10336
diff
changeset
|
270 |
return (url == NULL_URL) ? null : url; |
2 | 271 |
} |
272 |
} |
|
273 |
||
40565
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
274 |
// This is called from within an doPrivileged block. |
2 | 275 |
private static void setupJurisdictionPolicies() throws Exception { |
40565
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
276 |
|
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
277 |
// Sanity check the crypto.policy Security property. Single |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
278 |
// directory entry, no pseudo-directories (".", "..", leading/trailing |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
279 |
// path separators). normalize()/getParent() will help later. |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
280 |
String cryptoPolicyProperty = Security.getProperty("crypto.policy"); |
42365
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
281 |
|
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
282 |
/* |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
283 |
* In case no property is present, rather than fail catastrophically, |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
284 |
* we at least try for a "sane" value, which is what we were |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
285 |
* built with. We first preprocess this file to plug in that |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
286 |
* value, then compile the result gensrc. |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
287 |
* |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
288 |
* Log the warning first. |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
289 |
*/ |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
290 |
if (cryptoPolicyProperty == null) { |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
291 |
cryptoPolicyProperty = "@@JCE_DEFAULT_POLICY@@"; |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
292 |
if (debug != null) { |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
293 |
debug.println( |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
294 |
"Security Property 'crypto.policy' not found: " |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
295 |
+ "using '" + cryptoPolicyProperty + "' as fallback"); |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
296 |
} |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
297 |
} |
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
298 |
|
40565
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
299 |
Path cpPath = Paths.get(cryptoPolicyProperty); |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
300 |
|
42365
5e640c2994d6
8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents:
40565
diff
changeset
|
301 |
if ((cpPath.getNameCount() != 1) || |
40565
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
302 |
(cpPath.compareTo(cpPath.getFileName()) != 0)) { |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
303 |
throw new SecurityException( |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
304 |
"Invalid policy directory name format: " + |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
305 |
cryptoPolicyProperty); |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
306 |
} |
2 | 307 |
|
40565
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
308 |
// Prepend java.home to get the full path. normalize() in |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
309 |
// case an extra "." or ".." snuck in somehow. |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
310 |
String javaHomeProperty = System.getProperty("java.home"); |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
311 |
Path javaHomePolicyPath = Paths.get(javaHomeProperty, "conf", |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
312 |
"security", "policy").normalize(); |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
313 |
Path cryptoPolicyPath = Paths.get(javaHomeProperty, "conf", "security", |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
314 |
"policy", cryptoPolicyProperty).normalize(); |
2 | 315 |
|
40565
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
316 |
if (cryptoPolicyPath.getParent().compareTo(javaHomePolicyPath) != 0) { |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
317 |
throw new SecurityException( |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
318 |
"Invalid cryptographic jurisdiction policy directory path: " + |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
319 |
cryptoPolicyProperty); |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
320 |
} |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
321 |
|
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
322 |
if (!Files.isDirectory(cryptoPolicyPath) |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
323 |
|| !Files.isReadable(cryptoPolicyPath)) { |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
324 |
throw new SecurityException( |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
325 |
"Can't read cryptographic policy directory: " + |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
326 |
cryptoPolicyProperty); |
2 | 327 |
} |
328 |
||
40565
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
329 |
try (DirectoryStream<Path> stream = Files.newDirectoryStream( |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
330 |
cryptoPolicyPath, "{default,exempt}_*.policy")) { |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
331 |
for (Path entry : stream) { |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
332 |
try (InputStream is = new BufferedInputStream( |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
333 |
Files.newInputStream(entry))) { |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
334 |
String filename = entry.getFileName().toString(); |
2 | 335 |
|
40565
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
336 |
CryptoPermissions tmpPerms = new CryptoPermissions(); |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
337 |
tmpPerms.load(is); |
2 | 338 |
|
40565
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
339 |
if (filename.startsWith("default_")) { |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
340 |
// Did we find a default perms? |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
341 |
defaultPolicy = ((defaultPolicy == null) ? tmpPerms : |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
342 |
defaultPolicy.getMinimum(tmpPerms)); |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
343 |
} else if (filename.startsWith("exempt_")) { |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
344 |
// Did we find a exempt perms? |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
345 |
exemptPolicy = ((exemptPolicy == null) ? tmpPerms : |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
346 |
exemptPolicy.getMinimum(tmpPerms)); |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
347 |
} else { |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
348 |
// This should never happen. newDirectoryStream |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
349 |
// should only throw return "{default,exempt}_*.policy" |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
350 |
throw new SecurityException( |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
351 |
"Unexpected jurisdiction policy files in : " + |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
352 |
cryptoPolicyProperty); |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
353 |
} |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
354 |
} catch (Exception e) { |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
355 |
throw new SecurityException( |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
356 |
"Couldn't parse jurisdiction policy files in: " + |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
357 |
cryptoPolicyProperty); |
2 | 358 |
} |
359 |
} |
|
40565
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
360 |
} catch (DirectoryIteratorException ex) { |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
361 |
// I/O error encountered during the iteration, |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
362 |
// the cause is an IOException |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
363 |
throw new SecurityException( |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
364 |
"Couldn't iterate through the jurisdiction policy files: " + |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
365 |
cryptoPolicyProperty); |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
366 |
} |
2 | 367 |
|
40565
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
368 |
// Must have a default policy |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
369 |
if ((defaultPolicy == null) || defaultPolicy.isEmpty()) { |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
370 |
throw new SecurityException( |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
371 |
"Missing mandatory jurisdiction policy files: " + |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
372 |
cryptoPolicyProperty); |
2 | 373 |
} |
40565
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
374 |
|
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
375 |
// If there was an empty exempt policy file, ignore it. |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
376 |
if ((exemptPolicy != null) && exemptPolicy.isEmpty()) { |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
377 |
exemptPolicy = null; |
3ac0ba151e70
8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
36511
diff
changeset
|
378 |
} |
2 | 379 |
} |
380 |
||
381 |
static CryptoPermissions getDefaultPolicy() { |
|
382 |
return defaultPolicy; |
|
383 |
} |
|
384 |
||
385 |
static CryptoPermissions getExemptPolicy() { |
|
386 |
return exemptPolicy; |
|
387 |
} |
|
388 |
||
389 |
static boolean isRestricted() { |
|
390 |
return isRestricted; |
|
391 |
} |
|
392 |
} |