⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 security.java

📁 gcc的组建
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* Security.java --- Java base security class implementation   Copyright (C) 1999, 2001, 2002, 2003, 2004, 2005  Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version. */package java.security;import gnu.classpath.SystemProperties;import gnu.classpath.Configuration;import java.io.IOException;import java.io.InputStream;import java.net.URL;import java.util.Collections;import java.util.Enumeration;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.LinkedHashSet;import java.util.Map;import java.util.Properties;import java.util.Set;import java.util.Vector;/** * This class centralizes all security properties and common security methods. * One of its primary uses is to manage providers. * * @author Mark Benvenuto (ivymccough@worldnet.att.net) */public final class Security{  private static final String ALG_ALIAS = "Alg.Alias.";  private static Vector providers = new Vector();  private static Properties secprops = new Properties();    static    {      String base = SystemProperties.getProperty("gnu.classpath.home.url");      String vendor = SystemProperties.getProperty("gnu.classpath.vm.shortname");      // Try VM specific security file      boolean loaded = loadProviders (base, vendor);          // Append classpath standard provider if possible      if (!loadProviders (base, "classpath")	  && !loaded	  && providers.size() == 0)	  {	      if (Configuration.DEBUG)		  {		      /* No providers found and both security files failed to		       * load properly. Give a warning in case of DEBUG is		       * enabled. Could be done with java.util.logging later.		       */		      System.err.println			  ("WARNING: could not properly read security provider files:");		      System.err.println			  ("         " + base + "/security/" + vendor			   + ".security");		      System.err.println			  ("         " + base + "/security/" + "classpath"			   + ".security");		      System.err.println			  ("         Falling back to standard GNU security provider");		  }	      providers.addElement (new gnu.java.security.provider.Gnu());	  }    }  // This class can't be instantiated.  private Security()  {  }  /**   * Tries to load the vender specific security providers from the given   * base URL. Returns true if the resource could be read and completely   * parsed successfully, false otherwise.   */  private static boolean loadProviders(String baseUrl, String vendor)  {    if (baseUrl == null || vendor == null)      return false;    boolean result = true;    String secfilestr = baseUrl + "/security/" + vendor + ".security";    try      {	InputStream fin = new URL(secfilestr).openStream();	secprops.load(fin);	int i = 1;	String name;	while ((name = secprops.getProperty("security.provider." + i)) != null)	  {	    Exception exception = null;	    try	      {            ClassLoader sys = ClassLoader.getSystemClassLoader();		providers.addElement(Class.forName(name, true, sys).newInstance());	      }	    catch (ClassNotFoundException x)	      {	        exception = x;	      }	    catch (InstantiationException x)	      {	        exception = x;	      }	    catch (IllegalAccessException x)	      {	        exception = x;	      }	    if (exception != null)	      {		System.err.println ("WARNING: Error loading security provider "				    + name + ": " + exception);		result = false;	      }	    i++;	  }      }    catch (IOException ignored)      {	result = false;      }    return result;  }  /**   * Gets a specified property for an algorithm. The algorithm name should be a   * standard name. See Appendix A in the Java Cryptography Architecture API   * Specification &amp; Reference 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 {@link 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 {@link AlgorithmParameters} and   * {@link KeyFactory} engine classes (introduced in the Java 2 platform)   * instead.   */  public static String getAlgorithmProperty(String algName, String propName)  {    if (algName == null || propName == null)      return null;    String property = String.valueOf(propName) + "." + String.valueOf(algName);    Provider p;    for (Iterator i = providers.iterator(); i.hasNext(); )      {        p = (Provider) i.next();        for (Iterator j = p.keySet().iterator(); j.hasNext(); )          {            String key = (String) j.next();            if (key.equalsIgnoreCase(property))              return p.getProperty(key);          }      }    return null;  }  /**   * <p>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, <code>1</code> is most preferred, followed by   * <code>2</code>, and so on.</p>   *   * <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 position, are shifted up one position (towards the   * end of the list of installed providers).</p>   *   * <p>A provider cannot be added if it is already installed.</p>   *   * <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.</p>   *   * @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   * <code>-1</code> if the provider was not added because it is already   * installed.   * @throws SecurityException if a security manager exists and its   * {@link SecurityManager#checkSecurityAccess(String)} method denies access   * to add a new provider.   * @see #getProvider(String)   * @see #removeProvider(String)   * @see SecurityPermission   */  public static int insertProviderAt(Provider provider, int position)  {    SecurityManager sm = System.getSecurityManager();    if (sm != null)      sm.checkSecurityAccess("insertProvider." + provider.getName());    position--;    int max = providers.size ();    for (int i = 0; i < max; i++)      {	if (((Provider) providers.elementAt(i)).getName().equals(provider.getName()))	  return -1;      }    if (position < 0)      position = 0;    if (position > max)      position = max;    providers.insertElementAt(provider, position);    return position + 1;  }  /**   * <p>Adds a provider to the next position available.</p>   *   * <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.</p>   *   * @param provider the provider to be added.   * @return the preference position in which the provider was added, or   * <code>-1</code> if the provider was not added because it is already   * installed.   * @throws SecurityException if a security manager exists and its   * {@link SecurityManager#checkSecurityAccess(String)} method denies access   * to add a new provider.   * @see #getProvider(String)   * @see #removeProvider(String)   * @see SecurityPermission   */  public static int addProvider(Provider provider)  {    return insertProviderAt (provider, providers.size () + 1);  }  /**   * <p>Removes the provider with the specified name.</p>   *   * <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>   *   * <p>This method returns silently if the provider is not installed.</p>   *   * <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.</p>   *   * @param name the name of the provider to remove.   * @throws SecurityException if a security manager exists and its   * {@link SecurityManager#checkSecurityAccess(String)} method denies access   * to remove the provider.   * @see #getProvider(String)   * @see #addProvider(Provider)   */  public static void removeProvider(String name)  {    SecurityManager sm = System.getSecurityManager();    if (sm != null)      sm.checkSecurityAccess("removeProvider." + name);    int max = providers.size ();    for (int i = 0; i < max; i++)      {	if (((Provider) providers.elementAt(i)).getName().equals(name))	  {	    providers.remove(i);	    break;	  }      }  }  /**   * 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 Provider[] getProviders()  {    Provider[] array = new Provider[providers.size ()];    providers.copyInto (array);    return array;  }  /**   * Returns the provider installed with the specified name, if any. Returns   * <code>null</code> 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(String)   * @see #addProvider(Provider)   */  public static Provider getProvider(String name)  {    Provider p;    int max = providers.size ();    for (int i = 0; i < max; i++)      {	p = (Provider) providers.elementAt(i);	if (p.getName().equals(name))	  return p;      }    return null;  }  /**   * <p>Gets a security property value.</p>   *

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -