📄 security.java
字号:
reloadProviders(); Provider[] result = new Provider[providers.size()]; providers.copyInto(result); return result; } /** * Returns the provider installed with the specified name, if * any. Returns null if no provider with the specified name is * installed. * * @param name the name of the provider to get. * * @return the provider of the specified name. * * @see #removeProvider * @see #addProvider */ public static synchronized Provider getProvider(String name) { reloadProviders(); Enumeration enum_ = providers.elements(); while (enum_.hasMoreElements()) { Provider prov = (Provider)enum_.nextElement(); if (prov.getName().equals(name)) { return prov; } } return null; } /** * Returns an array containing all installed providers that satisfy the * specified selection criterion, or null if no such providers have been * installed. The returned providers are ordered * according to their <a href= * "#insertProviderAt(java.security.Provider, int)">preference order</a>. * * <p> A cryptographic service is always associated with a particular * algorithm or type. For example, a digital signature service is * always associated with a particular algorithm (e.g., DSA), * and a CertificateFactory service is always associated with * a particular certificate type (e.g., X.509). * NOTE: <B>java.security.cert.CertificateFactory</B> is found in J2ME CDC * profiles such as J2ME Foundation Profile. * * <p>The selection criterion must be specified in one of the following two formats: * <ul> * <li> <i><crypto_service>.<algorithm_or_type></i> <p> The * cryptographic service name must not contain any dots. * <p> A * provider satisfies the specified selection criterion iff the provider implements the * specified algorithm or type for the specified cryptographic service. * <p> For example, "CertificateFactory.X.509" * would be satisfied by any provider that supplied * a CertificateFactory implementation for X.509 certificates. * NOTE: <B>java.security.cert.CertificateFactory</B> is found in J2ME CDC * profiles such as J2ME Foundation Profile. * <li> <i><crypto_service>.<algorithm_or_type> <attribute_name>:< attribute_value></i> * <p> The cryptographic service name must not contain any dots. There * must be one or more space charaters between the the <i><algorithm_or_type></i> * and the <i><attribute_name></i>. * <p> A provider satisfies this selection criterion iff the * provider implements the specified algorithm or type for the specified * cryptographic service and its implementation meets the * constraint expressed by the specified attribute name/value pair. * <p> For example, "Signature.SHA1withDSA KeySize:1024" would be * satisfied by any provider that implemented * the SHA1withDSA signature algorithm with a keysize of 1024 (or larger). * NOTE: <B>java.security.Signature</B> is found in J2ME CDC profiles such as * J2ME Foundation Profile. * * </ul> * * <p> See Appendix A in the <a href= * "../../../guide/security/CryptoSpec.html#AppA"> * Java Cryptogaphy Architecture API Specification & Reference </a> * for information about standard cryptographic service names, standard * algorithm names and standard attribute names. * * @param filter the criterion for selecting * providers. The filter is case-insensitive. * * @return all the installed providers that satisfy the selection * criterion, or null if no such providers have been installed. * * @throws InvalidParameterException * if the filter is not in the required format * * @see #getProviders(java.util.Map) */ public static Provider[] getProviders(String filter) { String key = null; String value = null; int index = filter.indexOf(':'); if (index == -1) { key = new String(filter); value = ""; } else { key = filter.substring(0, index); value = filter.substring(index + 1); } Hashtable hashtableFilter = new Hashtable(1); hashtableFilter.put(key, value); return (getProviders(hashtableFilter)); } /** * Returns an array containing all installed providers that satisfy the specified * selection criteria, or null if no such providers have been installed. * The returned providers are ordered * according to their <a href= * "#insertProviderAt(java.security.Provider, int)">preference order</a>. * * <p>The selection criteria are represented by a map. * Each map entry represents a selection criterion. * A provider is selected iff it satisfies all selection * criteria. The key for any entry in such a map must be in one of the * following two formats: * <ul> * <li> <i><crypto_service>.<algorithm_or_type></i> * <p> The cryptographic service name must not contain any dots. * <p> The value associated with the key must be an empty string. * <p> A provider * satisfies this selection criterion iff the provider implements the * specified algorithm or type for the specified cryptographic service. * <li> <i><crypto_service>.<algorithm_or_type> <attribute_name></i> * <p> The cryptographic service name must not contain any dots. There * must be one or more space charaters between the <i><algorithm_or_type></i> * and the <i><attribute_name></i>. * <p> The value associated with the key must be a non-empty string. * A provider satisfies this selection criterion iff the * provider implements the specified algorithm or type for the specified * cryptographic service and its implementation meets the * constraint expressed by the specified attribute name/value pair. * </ul> * * <p> See Appendix A in the <a href= * "../../../guide/security/CryptoSpec.html#AppA"> * Java Cryptogaphy Architecture API Specification & Reference </a> * for information about standard cryptographic service names, standard * algorithm names and standard attribute names. * * @param filter the criteria for selecting * providers. The filter is case-insensitive. * * @return all the installed providers that satisfy the selection * criteria, or null if no such providers have been installed. * * @throws InvalidParameterException * if the filter is not in the required format * * @see #getProviders(java.lang.String) */ public static Provider[] getProviders(Map filter) { // Get all installed providers first. // Then only return those providers who satisfy the selection criteria. Provider[] allProviders = Security.getProviders(); Set keySet = filter.keySet(); LinkedHashSet candidates = new LinkedHashSet(5); // Returns all installed providers // if the selection criteria is null. if ((keySet == null) || (allProviders == null)) { return allProviders; } boolean firstSearch = true; // For each selection criterion, remove providers // which don't satisfy the criterion from the candidate set. for (Iterator ite = keySet.iterator(); ite.hasNext(); ) { String key = (String)ite.next(); String value = (String)filter.get(key); LinkedHashSet newCandidates = getAllQualifyingCandidates(key, value, allProviders); if (firstSearch) { candidates = newCandidates; firstSearch = false; } if ((newCandidates != null) && !newCandidates.isEmpty()) { // For each provider in the candidates set, if it // isn't in the newCandidate set, we should remove // it from the candidate set. for (Iterator cansIte = candidates.iterator(); cansIte.hasNext(); ) { Provider prov = (Provider)cansIte.next(); if (!newCandidates.contains(prov)) { cansIte.remove(); } } } else { candidates = null; break; } } if ((candidates == null) || (candidates.isEmpty())) return null; Object[] candidatesArray = candidates.toArray(); Provider[] result = new Provider[candidatesArray.length]; for (int i = 0; i < result.length; i++) { result[i] = (Provider)candidatesArray[i]; } return result; } private static boolean checkSuperclass(Class subclass, Class superclass) { while(!subclass.equals(superclass)) { subclass = subclass.getSuperclass(); if (subclass == null) { return false; } } return true; } /* * Returns an array of objects: the first object in the array is * an instance of an implementation of the requested algorithm * and type, and the second object in the array identifies the provider * of that implementation. * The <code>provider</code> argument can be null, in which case all * configured providers will be searched in order of preference. */ static Object[] getImpl(String algorithm, String type, String provider) throws NoSuchAlgorithmException, NoSuchProviderException { ProviderProperty pp = getEngineClassName(algorithm, provider, type); return doGetImpl(algorithm, type, pp); } static Object[] getImpl(String algorithm, String type, String provider, Object params) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException { ProviderProperty pp = getEngineClassName(algorithm, provider, type); return doGetImpl(algorithm, type, pp, params); } /* * Returns an array of objects: the first object in the array is * an instance of an implementation of the requested algorithm * and type, and the second object in the array identifies the provider * of that implementation. * The <code>provider</code> argument cannot be null. */ static Object[] getImpl(String algorithm, String type, Provider provider) throws NoSuchAlgorithmException { ProviderProperty pp = getEngineClassName(algorithm, provider, type); return doGetImpl(algorithm, type, pp); } static Object[] getImpl(String algorithm, String type, Provider provider, Object params) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException { ProviderProperty pp = getEngineClassName(algorithm, provider, type); return doGetImpl(algorithm, type, pp, params); } private static Object[] doGetImpl(String algorithm, String type, ProviderProperty pp) throws NoSuchAlgorithmException { try { return doGetImpl(algorithm, type, pp, null); } catch (InvalidAlgorithmParameterException e) { // should not occur throw new NoSuchAlgorithmException(e.getMessage()); } } private static Object[] doGetImpl(String algorithm, String type, ProviderProperty pp, Object params) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException { String className = pp.className; String providerName = pp.provider.getName(); try { // java.security.<type>.Spi is a system class, therefore // Class.forName() always works Class typeClass; if (type.equals("CertificateFactory") || type.equals("CertPathBuilder") || type.equals("CertPathValidator") || type.equals("CertStore")) { typeClass = Class.forName("java.security.cert." + type + "Spi"); } else { typeClass = Class.forName("java.security." + type + "Spi"); } // Load the implementation class using the same class loader that // was used to load the associated provider. // In order to get the class loader of a class, the caller's class // loader must be the same as or an ancestor of the class loader // being returned. // Since java.security.Security is a system class, it can get the // class loader of any class (the system class loader is an // ancestor of all class loaders). ClassLoader cl = pp.provider.getClass().getClassLoader(); Class implClass; if (cl != null) { implClass = cl.loadClass(className); } else { implClass = Class.forName(className); } if (checkSuperclass(implClass, typeClass)) { Object obj; if (type.equals("CertStore")) { Constructor cons = implClass.getConstructor(new Class[] { Class.forName ("java.security.cert.CertStoreParameters") }); obj = cons.newInstance(new Object[] {params}); } else obj = implClass.newInstance(); return new Object[] { obj, pp.provider }; } else { throw new NoSuchAlgorithmException("class configured for " + type + ": " + className + " not a " + type); } } catch (ClassNotFoundException e) { throw new NoSuchAlgorithmException("class configured for " + type + "(provider: " + providerName + ")" + "cannot be found.\n" + e.getMessage()); } catch (InstantiationException e) { throw (NoSuchAlgorithmException) new NoSuchAlgorithmException("class " + className + " configured for " + type + "(provider: " + providerName + ") cannot be " + "instantiated.\n").initCause(e); } catch (IllegalAccessException e) { throw new NoSuchAlgorithmException("class " + className + " configured for " + type + "(provider: " + providerName + ") cannot be accessed.\n" + e.getMessage()); } catch (SecurityException e) { throw new NoSuchAlgorithmException("class " + className + " configured for " + type + "(provider: " + providerName + ") cannot be accessed.\n" + e.getMessage()); } catch (NoSuchMethodException e) { throw new NoSuchAlgorithmException("constructor for " + "class " + className + " configured for " + type + "(provider: " + providerName + ") cannot be instantiated.\n" + e.getMessage()); } catch (InvocationTargetException e) { Throwable t = e.getCause(); if (t != null && t instanceof InvalidAlgorithmParameterException) throw (InvalidAlgorithmParameterException) t; else throw new InvalidAlgorithmParameterException("constructor " + "for class " + className + " configured for " + type + "(provider: " + providerName + ") cannot be instantiated.\n" + e.getMessage()); } } /** * Gets a security property value. * * <p>First, if there is a security manager, its * <code>checkPermission</code> method is called with a * <code>java.security.SecurityPermission("getProperty."+key)</code> * permission to see if it's ok to retrieve the specified * security property value.. * * @param key the key of the property being retrieved. * * @return the value of the security property corresponding to key. * * @throws SecurityException * if a security manager exists and its <code>{@link * java.lang.SecurityManager#checkPermission}</code> method * denies * access to retrieve the specified security property value * * @see #setProperty * @see java.security.SecurityPermission */ public static String getProperty(String key) { SecurityManager sm = System.getSecurityManager(); if (sm != null) { sm.checkPermission(new SecurityPermission("getProperty."+ key)); } String name = props.getProperty(key); if (name != null) name = name.trim(); // could be a class name with trailing ws return name; } /** * Sets a security property value.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -