OSDN Git Service

2004-11-16 Andreas Tobler <a.tobler@schweiz.ch>
[pf3gnuchains/gcc-fork.git] / libjava / java / security / Security.java
index 56335bf..01c0a05 100644 (file)
@@ -1,5 +1,5 @@
 /* Security.java --- Java base security class implementation
-   Copyright (C) 1999, 2001, 2002, 2003, Free Software Foundation, Inc.
+   Copyright (C) 1999, 2001, 2002, 2003, 2004  Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -35,13 +35,16 @@ this exception to your version of the library, but you are not
 obligated to do so.  If you do not wish to do so, delete this
 exception statement from your version. */
 
+
 package java.security;
 
-import java.io.FileNotFoundException;
-import java.io.InputStream;
+import gnu.java.security.action.GetPropertyAction;
+
+import gnu.classpath.Configuration;
+
 import java.io.IOException;
+import java.io.InputStream;
 import java.net.URL;
-import java.security.Provider;
 import java.util.Collections;
 import java.util.Enumeration;
 import java.util.HashMap;
@@ -57,40 +60,52 @@ 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>
+ * @author Mark Benvenuto (ivymccough@worldnet.att.net)
  */
-public final class Security extends Object
+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 = System.getProperty("gnu.classpath.home.url");
-    String vendor = System.getProperty("gnu.classpath.vm.shortname");
-    
-    // Try VM specific security file
-    boolean loaded = loadProviders(base, vendor);
+    {
+      GetPropertyAction getProp = new GetPropertyAction("gnu.classpath.home.url");
+      String base = (String) AccessController.doPrivileged(getProp);
+      getProp = new GetPropertyAction("gnu.classpath.vm.shortname");
+      String vendor = (String) AccessController.doPrivileged(getProp);
+
+      // Try VM specific security file
+      boolean loaded = loadProviders (base, vendor);
     
-    // Append classpath standard provider if possible
-    if (!loadProviders(base, "classpath") && !loaded && providers.size() == 0)
-      {
-       // No providers found and both security files failed to load properly.
-       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());
-      }
-  }
-
+      // 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 ()
+  private Security()
   {
   }
 
@@ -147,7 +162,7 @@ public final class Security extends Object
        result = false;
       }
 
-    return false;
+    return result;
   }
 
   /**
@@ -234,7 +249,7 @@ public final class Security extends Object
     int max = providers.size ();
     for (int i = 0; i < max; i++)
       {
-       if (((Provider) providers.elementAt(i)).getName() == provider.getName())
+       if (((Provider) providers.elementAt(i)).getName().equals(provider.getName()))
          return -1;
       }
 
@@ -309,7 +324,7 @@ public final class Security extends Object
     int max = providers.size ();
     for (int i = 0; i < max; i++)
       {
-       if (((Provider) providers.elementAt(i)).getName() == name)
+       if (((Provider) providers.elementAt(i)).getName().equals(name))
          {
            providers.remove(i);
            break;
@@ -325,7 +340,7 @@ public final class Security extends Object
    */
   public static Provider[] getProviders()
   {
-    Provider array[] = new Provider[providers.size ()];
+    Provider[] array = new Provider[providers.size ()];
     providers.copyInto (array);
     return array;
   }
@@ -346,7 +361,7 @@ public final class Security extends Object
     for (int i = 0; i < max; i++)
       {
        p = (Provider) providers.elementAt(i);
-       if (p.getName() == name)
+       if (p.getName().equals(name))
          return p;
       }
     return null;
@@ -408,7 +423,7 @@ public final class Security extends Object
    * MessageDigest, Cipher, Mac, KeyStore). Returns an empty Set if there is no
    * provider that supports the specified service. For a complete list of Java
    * cryptographic services, please see the Java Cryptography Architecture API
-   * Specification & Reference. Note: the returned set is immutable.
+   * Specification &amp; Reference. Note: the returned set is immutable.
    *
    * @param serviceName the name of the Java cryptographic service (e.g.,
    * Signature, MessageDigest, Cipher, Mac, KeyStore). Note: this parameter is
@@ -463,7 +478,7 @@ public final class Security extends Object
    * formats:</p>
    *
    * <ul>
-   *    <li><p>&lt;crypto_service>.&lt;algorithm_or_type></p>
+   *    <li><p>&lt;crypto_service&gt;.&lt;algorithm_or_type&gt;</p>
    *    <p>The cryptographic service name must not contain any dots.</p>
    *    <p>A provider satisfies the specified selection criterion iff the
    *    provider implements the specified algorithm or type for the specified
@@ -472,10 +487,10 @@ public final class Security extends Object
    *    provider that supplied a CertificateFactory implementation for X.509
    *    certificates.</p></li>
    *
-   *    <li><p>&lt;crypto_service>.&lt;algorithm_or_type>&nbsp;&lt;attribute_name>:&lt;attribute_value></p>
+   *    <li><p>&lt;crypto_service&gt;.&lt;algorithm_or_type&gt; &lt;attribute_name&gt;:&lt;attribute_value&gt;</p>
    *    <p>The cryptographic service name must not contain any dots. There must
-   *    be one or more space charaters between the the &lt;algorithm_or_type>
-   *    and the &lt;attribute_name>.</p>
+   *    be one or more space charaters between the the &lt;algorithm_or_type&gt;
+   *    and the &lt;attribute_name&gt;.</p>
    *    <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
@@ -527,17 +542,17 @@ public final class Security extends Object
   * of the following two formats:</p>
   *
   * <ul>
-  *    <li><p>&lt;crypto_service>.&lt;algorithm_or_type></p>
+  *    <li><p>&lt;crypto_service&gt;.&lt;algorithm_or_type&gt;</p>
   *    <p>The cryptographic service name must not contain any dots.</p>
   *    <p>The value associated with the key must be an empty string.</p>
   *    <p>A provider satisfies this selection criterion iff the provider
   *    implements the specified algorithm or type for the specified
   *    cryptographic service.</p></li>
   *
-  *    <li><p>&lt;crypto_service>.&lt;algorithm_or_type> &lt;attribute_name></p>
+  *    <li><p>&lt;crypto_service&gt;.&lt;algorithm_or_type&gt; &lt;attribute_name&gt;</p>
   *    <p>The cryptographic service name must not contain any dots. There must
-  *    be one or more space charaters between the &lt;algorithm_or_type> and
-  *    the &lt;attribute_name>.</p>
+  *    be one or more space charaters between the &lt;algorithm_or_type&gt; and
+  *    the &lt;attribute_name&gt;.</p>
   *    <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
@@ -667,7 +682,7 @@ public final class Security extends Object
     outer: for (int r = 0; r < 3; r++) // guard against circularity
       {
         serviceDotAlgorithm = (svc+"."+String.valueOf(algo)).trim();
-        inner: for (it = p.keySet().iterator(); it.hasNext(); )
+        for (it = p.keySet().iterator(); it.hasNext(); )
           {
             key = (String) it.next();
             if (key.equalsIgnoreCase(serviceDotAlgorithm)) // eureka