12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package javax.crypto;
27
28 import java.util.*;
29 import java.util.jar.*;
30 import java.io.*;
31 import java.net.URL;
32 import java.security.*;
33
34 import java.security.Provider.Service;
35
36 import sun.security.jca.*;
37 import sun.security.jca.GetInstance.Instance;
38
39 /**
40 * This class instantiates implementations of JCE engine classes from
41 * providers registered with the java.security.Security object.
42 *
43 * @author Jan Luehe
44 * @author Sharon Liu
45 * @since 1.4
46 */
47
48 final class JceSecurity {
49
50 static final SecureRandom RANDOM = new SecureRandom();
51
52 // The defaultPolicy and exemptPolicy will be set up
53 // in the static initializer.
54 private static CryptoPermissions defaultPolicy = null;
55 private static CryptoPermissions exemptPolicy = null;
56
57 // Map<Provider,?> of the providers we already have verified
58 // value == PROVIDER_VERIFIED is successfully verified
59 // value is failure cause Exception in error case
60 private final static Map<Provider, Object> verificationResults =
61 new IdentityHashMap<>();
62
63 // Map<Provider,?> of the providers currently being verified
64 private final static Map<Provider, Object> verifyingProviders =
65 new IdentityHashMap<>();
66
67 private static final boolean isRestricted;
68
69 /*
70 * Don't let anyone instantiate this.
71 */
72 private JceSecurity() {
73 }
74
75 static {
76 try {
77 AccessController.doPrivileged(
78 new PrivilegedExceptionAction<Object>() {
79 public Object run() throws Exception {
80 setupJurisdictionPolicies();
81 return null;
82 }
83 });
84
85 isRestricted = defaultPolicy.implies(
86 CryptoAllPermission.INSTANCE) ? false : true;
87 } catch (Exception e) {
88 throw new SecurityException(
187 verificationResults.put(p, PROVIDER_VERIFIED);
188 return null;
189 } catch (Exception e) {
190 verificationResults.put(p, e);
191 return e;
192 } finally {
193 verifyingProviders.remove(p);
194 }
195 }
196
197 // return whether this provider is properly signed and can be used by JCE
198 static boolean canUseProvider(Provider p) {
199 return getVerificationResult(p) == null;
200 }
201
202 // dummy object to represent null
203 private static final URL NULL_URL;
204
205 static {
206 try {
207 NULL_URL = new URL("http://null.sun.com/");
208 } catch (Exception e) {
209 throw new RuntimeException(e);
210 }
211 }
212
213 // reference to a Map we use as a cache for codebases
214 private static final Map<Class<?>, URL> codeBaseCacheRef =
215 new WeakHashMap<>();
216
217 /*
218 * Returns the CodeBase for the given class.
219 */
220 static URL getCodeBase(final Class<?> clazz) {
221 synchronized (codeBaseCacheRef) {
222 URL url = codeBaseCacheRef.get(clazz);
223 if (url == null) {
224 url = AccessController.doPrivileged(new PrivilegedAction<URL>() {
225 public URL run() {
226 ProtectionDomain pd = clazz.getProtectionDomain();
227 if (pd != null) {
228 CodeSource cs = pd.getCodeSource();
229 if (cs != null) {
230 return cs.getLocation();
231 }
232 }
233 return NULL_URL;
234 }
235 });
236 codeBaseCacheRef.put(clazz, url);
237 }
238 return (url == NULL_URL) ? null : url;
239 }
240 }
241
242 private static void setupJurisdictionPolicies() throws Exception {
243 String javaHomeDir = System.getProperty("java.home");
244 String sep = File.separator;
245 String pathToPolicyJar = javaHomeDir + sep + "lib" + sep +
246 "security" + sep;
247
248 File exportJar = new File(pathToPolicyJar, "US_export_policy.jar");
249 File importJar = new File(pathToPolicyJar, "local_policy.jar");
250 URL jceCipherURL = ClassLoader.getSystemResource
251 ("javax/crypto/Cipher.class");
252
253 if ((jceCipherURL == null) ||
254 !exportJar.exists() || !importJar.exists()) {
255 throw new SecurityException
256 ("Cannot locate policy or framework files!");
257 }
258
259 // Read jurisdiction policies.
260 CryptoPermissions defaultExport = new CryptoPermissions();
261 CryptoPermissions exemptExport = new CryptoPermissions();
262 loadPolicies(exportJar, defaultExport, exemptExport);
263
264 CryptoPermissions defaultImport = new CryptoPermissions();
265 CryptoPermissions exemptImport = new CryptoPermissions();
266 loadPolicies(importJar, defaultImport, exemptImport);
267
268 // Merge the export and import policies for default applications.
269 if (defaultExport.isEmpty() || defaultImport.isEmpty()) {
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package javax.crypto;
27
28 import java.util.*;
29 import java.util.jar.*;
30 import java.io.*;
31 import java.net.URL;
32 import java.nio.file.*;
33 import java.security.*;
34
35 import java.security.Provider.Service;
36
37 import sun.security.jca.*;
38 import sun.security.jca.GetInstance.Instance;
39 import sun.security.util.Debug;
40
41 /**
42 * This class instantiates implementations of JCE engine classes from
43 * providers registered with the java.security.Security object.
44 *
45 * @author Jan Luehe
46 * @author Sharon Liu
47 * @since 1.4
48 */
49
50 final class JceSecurity {
51
52 static final SecureRandom RANDOM = new SecureRandom();
53
54 // The defaultPolicy and exemptPolicy will be set up
55 // in the static initializer.
56 private static CryptoPermissions defaultPolicy = null;
57 private static CryptoPermissions exemptPolicy = null;
58
59 // Map<Provider,?> of the providers we already have verified
60 // value == PROVIDER_VERIFIED is successfully verified
61 // value is failure cause Exception in error case
62 private final static Map<Provider, Object> verificationResults =
63 new IdentityHashMap<>();
64
65 // Map<Provider,?> of the providers currently being verified
66 private final static Map<Provider, Object> verifyingProviders =
67 new IdentityHashMap<>();
68
69 private static final boolean isRestricted;
70
71 private static final Debug debug =
72 Debug.getInstance("jca", "Cipher");
73
74 /*
75 * Don't let anyone instantiate this.
76 */
77 private JceSecurity() {
78 }
79
80 static {
81 try {
82 AccessController.doPrivileged(
83 new PrivilegedExceptionAction<Object>() {
84 public Object run() throws Exception {
85 setupJurisdictionPolicies();
86 return null;
87 }
88 });
89
90 isRestricted = defaultPolicy.implies(
91 CryptoAllPermission.INSTANCE) ? false : true;
92 } catch (Exception e) {
93 throw new SecurityException(
192 verificationResults.put(p, PROVIDER_VERIFIED);
193 return null;
194 } catch (Exception e) {
195 verificationResults.put(p, e);
196 return e;
197 } finally {
198 verifyingProviders.remove(p);
199 }
200 }
201
202 // return whether this provider is properly signed and can be used by JCE
203 static boolean canUseProvider(Provider p) {
204 return getVerificationResult(p) == null;
205 }
206
207 // dummy object to represent null
208 private static final URL NULL_URL;
209
210 static {
211 try {
212 NULL_URL = new URL("http://null.oracle.com/");
213 } catch (Exception e) {
214 throw new RuntimeException(e);
215 }
216 }
217
218 // reference to a Map we use as a cache for codebases
219 private static final Map<Class<?>, URL> codeBaseCacheRef =
220 new WeakHashMap<>();
221
222 /*
223 * Returns the CodeBase for the given class.
224 */
225 static URL getCodeBase(final Class<?> clazz) {
226 synchronized (codeBaseCacheRef) {
227 URL url = codeBaseCacheRef.get(clazz);
228 if (url == null) {
229 url = AccessController.doPrivileged(new PrivilegedAction<URL>() {
230 public URL run() {
231 ProtectionDomain pd = clazz.getProtectionDomain();
232 if (pd != null) {
233 CodeSource cs = pd.getCodeSource();
234 if (cs != null) {
235 return cs.getLocation();
236 }
237 }
238 return NULL_URL;
239 }
240 });
241 codeBaseCacheRef.put(clazz, url);
242 }
243 return (url == NULL_URL) ? null : url;
244 }
245 }
246
247 /*
248 * This is called from within an doPrivileged block.
249 *
250 * Following logic is used to decide what policy files are selected.
251 *
252 * If the new Security property (crypto.policy) is set in the
253 * java.security file, or has been set dynamically using the
254 * Security.setProperty() call before the JCE framework has
255 * been initialized, that setting will be used.
256 * Remember - this property is not defined by default. A conscious
257 * user edit or an application call is required.
258 *
259 * Otherwise, if user has policy jar files installed in the legacy
260 * jre/lib/security/ directory, the JDK will honor whatever
261 * setting is set by those policy files. (legacy/current behavior)
262 *
263 * If none of the above 2 conditions are met, the JDK will default
264 * to using the limited crypto policy files found in the
265 * jre/lib/security/policy/limited/ directory
266 */
267 private static void setupJurisdictionPolicies() throws Exception {
268 // Sanity check the crypto.policy Security property. Single
269 // directory entry, no pseudo-directories (".", "..", leading/trailing
270 // path separators). normalize()/getParent() will help later.
271 String javaHomeProperty = System.getProperty("java.home");
272 String cryptoPolicyProperty = Security.getProperty("crypto.policy");
273 Path cpPath = (cryptoPolicyProperty == null) ? null :
274 Paths.get(cryptoPolicyProperty);
275
276 if ((cpPath != null) && ((cpPath.getNameCount() != 1) ||
277 (cpPath.compareTo(cpPath.getFileName())) != 0)) {
278 throw new SecurityException(
279 "Invalid policy directory name format: " +
280 cryptoPolicyProperty);
281 }
282
283 if (cpPath == null) {
284 // Security property is not set, use default path
285 cpPath = Paths.get(javaHomeProperty, "lib", "security");
286 } else {
287 // populate with java.home
288 cpPath = Paths.get(javaHomeProperty, "lib", "security",
289 "policy", cryptoPolicyProperty);
290 }
291
292 if (debug != null) {
293 debug.println("crypto policy directory: " + cpPath);
294 }
295
296 File exportJar = new File(cpPath.toFile(),"US_export_policy.jar");
297 File importJar = new File(cpPath.toFile(),"local_policy.jar");
298
299 if (cryptoPolicyProperty == null && (!exportJar.exists() ||
300 !importJar.exists())) {
301 // Compatibility set up. If crypto.policy is not defined.
302 // check to see if legacy jars exist in lib directory. If
303 // they don't exist, we default to limited policy mode.
304 cpPath = Paths.get(
305 javaHomeProperty, "lib", "security", "policy", "limited");
306 // point to the new jar files in limited directory
307 exportJar = new File(cpPath.toFile(),"US_export_policy.jar");
308 importJar = new File(cpPath.toFile(),"local_policy.jar");
309 }
310
311 URL jceCipherURL = ClassLoader.getSystemResource
312 ("javax/crypto/Cipher.class");
313
314 if ((jceCipherURL == null) ||
315 !exportJar.exists() || !importJar.exists()) {
316 throw new SecurityException
317 ("Cannot locate policy or framework files!");
318 }
319
320 // Read jurisdiction policies.
321 CryptoPermissions defaultExport = new CryptoPermissions();
322 CryptoPermissions exemptExport = new CryptoPermissions();
323 loadPolicies(exportJar, defaultExport, exemptExport);
324
325 CryptoPermissions defaultImport = new CryptoPermissions();
326 CryptoPermissions exemptImport = new CryptoPermissions();
327 loadPolicies(importJar, defaultImport, exemptImport);
328
329 // Merge the export and import policies for default applications.
330 if (defaultExport.isEmpty() || defaultImport.isEmpty()) {
|