📄 security.java
字号:
* provider) mapping the key, if any. * The order in which the providers are looked up is the * provider-preference order, as specificed in the security * properties file. */ private static ProviderProperty getProviderProperty(String key) { ProviderProperty entry = (ProviderProperty)providerPropertiesCache.get(key); if (entry != null) { return entry; } for (int i = 0; i < providers.size(); i++) { String matchKey = null; Provider prov = (Provider)providers.elementAt(i); String prop = prov.getProperty(key); if (prop == null) { // Is there a match if we do a case-insensitive property name // comparison? Let's try ... for (Enumeration enum_ = prov.keys(); enum_.hasMoreElements() && prop==null; ) { matchKey = (String)enum_.nextElement(); if (key.equalsIgnoreCase(matchKey)) { prop = prov.getProperty(matchKey); break; } } } if (prop != null) { ProviderProperty newEntry = new ProviderProperty(); newEntry.className = prop; newEntry.provider = prov; providerPropertiesCache.put(key, newEntry); if (matchKey != null) { // Store the property value in the cache under the exact // property name, as specified by the provider providerPropertiesCache.put(matchKey, newEntry); } return newEntry; } } return entry; } /** * Returns the property (if any) mapping the key for the given provider. */ private static String getProviderProperty(String key, Provider provider) { String prop = provider.getProperty(key); if (prop == null) { // Is there a match if we do a case-insensitive property name // comparison? Let's try ... for (Enumeration enum_ = provider.keys(); enum_.hasMoreElements() && prop==null; ) { String matchKey = (String)enum_.nextElement(); if (key.equalsIgnoreCase(matchKey)) { prop = provider.getProperty(matchKey); break; } } } return prop; } /** * We always map names to standard names */ private static String getStandardName(String alias, String engineType, Provider prov) { return getProviderProperty("Alg.Alias." + engineType + "." + alias, prov); } /** * Gets a specified property for an algorithm. The algorithm name * should be a standard name. See Appendix A in the <a href= * "../../../guide/security/CryptoSpec.html#AppA"> * Java Cryptography Architecture API Specification & Reference </a> * for information about standard algorithm names. * One possible use is by specialized algorithm parsers, which may map * classes to algorithms which they understand (much like Key parsers * do). * * param algName the algorithm name. * * param propName the name of the property to get. * * return the value of the specified property. * * deprecated This method used to return the value of a proprietary * property in the master file of the "SUN" Cryptographic Service * Provider in order to determine how to parse algorithm-specific * parameters. Use the new provider-based and algorithm-independent * <code>AlgorithmParameters</code> and <code>KeyFactory</code> engine * classes (introduced in the Java 2 platform) instead. * public static String getAlgorithmProperty(String algName, String propName) { reloadProviders(); ProviderProperty entry = getProviderProperty("Alg." + propName + "." + algName); if (entry != null) { return entry.className; } else { return null; } } */ /* * Lookup the algorithm in our list of providers. Process * each provider in priority order one at a time looking for * either the direct engine property or a matching alias. */ private static ProviderProperty getEngineClassName(String algName, String engineType) throws NoSuchAlgorithmException { ProviderProperty pp; String key = engineType; if (algName != null) key += "." + algName; pp = (ProviderProperty)engineCache.get(key); if (pp != null) return pp; synchronized (Security.class) { sun.misc.Launcher l = sun.misc.Launcher.getLauncher(); /* * In case some providers have been loaded out of the * priority order when the launcher l is null, we should * clear the vector "providers" and reset the indexStaticProviders * to zero when the launcher l isn't null. * * We should only do the above if the "reloadProviders" is true * which means that the method reloadProviders() hasn't * load all statically registered providers yet. * Once the reloadProviders() method has loaded all statically * registered providers, we shouldn't clear the vector * "providers" in this getEngineClassName() method. */ if ((reloadProviders == true) && (l != null) && (resetProviderIndex == true)) { resetProviderIndex = false; indexStaticProviders = 0; providers.removeAllElements(); providerPropertiesCache.clear(); engineCache.clear(); searchResultsCache.clear(); providerLoads.clear(); } // We should call loadOneMoreProvider() if no provider // has been loaded yet. Otherwise, we may not be able to // get in the following "for" loop. if (providers.size() == 0) { loadOneMoreProvider(); } for (int i = 0; i < providers.size(); i++) { Provider prov = (Provider)providers.elementAt(i); try { pp = getEngineClassName(algName, prov, engineType); } catch (NoSuchAlgorithmException e) { if (i == providers.size() - 1) { // The requested algorithm may be available in // a registered provider which hasn't been loaded // yet. Let's try to load one more registered // provider. The method loadOneMoreProvider() // won't do anything if we have tried to load all // registered providers. loadOneMoreProvider(); } continue; } /* Cache it */ engineCache.put(key, pp); return pp; } } throw new NoSuchAlgorithmException(algName.toUpperCase() + " " + engineType + " not available"); } private static ProviderProperty getEngineClassName(String algName, String provider, String engineType) throws NoSuchAlgorithmException, NoSuchProviderException { if (provider == null) { return getEngineClassName(algName, engineType); } // check if the provider is installed Provider prov = getProvider(provider); if (prov == null) { throw new NoSuchProviderException("no such provider: " + provider); } return getEngineClassName(algName, prov, engineType); } /** * The parameter provider cannot be null. */ private static ProviderProperty getEngineClassName(String algName, Provider provider, String engineType) throws NoSuchAlgorithmException { String key; if (engineType.equalsIgnoreCase("SecureRandom") && algName == null) key = engineType; else key = engineType + "." + algName; String className = getProviderProperty(key, provider); if (className == null) { if (engineType.equalsIgnoreCase("SecureRandom") && algName == null) throw new NoSuchAlgorithmException ("SecureRandom not available for provider " + provider.getName()); else { // try algName as alias name String stdName = getStandardName(algName, engineType, provider); if (stdName != null) key = engineType + "." + stdName; if ((stdName == null) || (className = getProviderProperty(key, provider)) == null) throw new NoSuchAlgorithmException("no such algorithm: " + algName + " for provider " + provider.getName()); } } ProviderProperty entry = new ProviderProperty(); entry.className = className; entry.provider = provider; return entry; } /** * Adds a new provider, at a specified position. The position is * the preference order in which providers are searched for * requested algorithms. Note that it is not guaranteed that this * preference will be respected. The position is 1-based, that is, * 1 is most preferred, followed by 2, and so on. * * <p>If the given provider is installed at the requested position, * the provider that used to be at that position, and all providers * with a position greater than <code>position</code>, are shifted up * one position (towards the end of the list of installed providers). * * <p>A provider cannot be added if it is already installed. * * <p>First, if there is a security manager, its * <code>checkSecurityAccess</code> * method is called with the string * <code>"insertProvider."+provider.getName()</code> * to see if it's ok to add a new provider. * If the default implementation of <code>checkSecurityAccess</code> * is used (i.e., that method is not overriden), then this will result in * a call to the security manager's <code>checkPermission</code> method * with a * <code>SecurityPermission("insertProvider."+provider.getName())</code> * permission. * * @param provider the provider to be added. * * @param position the preference position that the caller would * like for this provider. * * @return the actual preference position in which the provider was * added, or -1 if the provider was not added because it is * already installed. * * @throws SecurityException * if a security manager exists and its <code>{@link * java.lang.SecurityManager#checkSecurityAccess}</code> method * denies access to add a new provider * * @see #getProvider * @see #removeProvider * @see java.security.SecurityPermission */ public static synchronized int insertProviderAt(Provider provider, int position) { reloadProviders(); check("insertProvider."+provider.getName()); /* First check if the provider is already installed */ Provider already = getProvider(provider.getName()); if (already != null) { return -1; } int size = providers.size(); if (position > size || position <= 0) { position = size+1; } providers.insertElementAt(provider, position-1); // empty provider-property cache providerPropertiesCache.clear(); engineCache.clear(); searchResultsCache.clear(); return position; } /** * Adds a provider to the next position available. * * <p>First, if there is a security manager, its * <code>checkSecurityAccess</code> * method is called with the string * <code>"insertProvider."+provider.getName()</code> * to see if it's ok to add a new provider. * If the default implementation of <code>checkSecurityAccess</code> * is used (i.e., that method is not overriden), then this will result in * a call to the security manager's <code>checkPermission</code> method * with a * <code>SecurityPermission("insertProvider."+provider.getName())</code> * permission. * * @param provider the provider to be added. * * @return the preference position in which the provider was * added, or -1 if the provider was not added because it is * already installed. * * @throws SecurityException * if a security manager exists and its <code>{@link * java.lang.SecurityManager#checkSecurityAccess}</code> method * denies access to add a new provider * * @see #getProvider * @see #removeProvider * @see java.security.SecurityPermission */ public static int addProvider(Provider provider) { /* * We can't assign a position here because the statically * registered providers may not have been installed yet. * insertProviderAt() will fix that value after it has * loaded the static providers. */ return insertProviderAt(provider, 0); } /** * Removes the provider with the specified name. * * <p>When the specified provider is removed, all providers located * at a position greater than where the specified provider was are shifted * down one position (towards the head of the list of installed * providers). * * <p>This method returns silently if the provider is not installed. * * <p>First, if there is a security manager, its * <code>checkSecurityAccess</code> * method is called with the string <code>"removeProvider."+name</code> * to see if it's ok to remove the provider. * If the default implementation of <code>checkSecurityAccess</code> * is used (i.e., that method is not overriden), then this will result in * a call to the security manager's <code>checkPermission</code> method * with a <code>SecurityPermission("removeProvider."+name)</code> * permission. * * @param name the name of the provider to remove. * * @throws SecurityException * if a security manager exists and its <code>{@link * java.lang.SecurityManager#checkSecurityAccess}</code> method * denies * access to remove the provider * * @see #getProvider * @see #addProvider */ public static synchronized void removeProvider(String name) { reloadProviders(); check("removeProvider."+name); Provider provider = getProvider(name); if (provider != null) { for (Iterator i=providers.iterator(); i.hasNext(); ) if (i.next()==provider) i.remove(); // empty provider-property cache providerPropertiesCache.clear(); engineCache.clear(); searchResultsCache.clear(); } } /** * Returns an array containing all the installed providers. The order of * the providers in the array is their preference order. * * @return an array of all the installed providers. */ public static synchronized Provider[] getProviders() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -