This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.
| Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
|---|---|---|
| Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |
| Other format: | [Raw text] | |
Hello,
Attached is a patch for a number of classes in the java.security package
with attempts to fix and/or improve upon the `getInstance' factory
methods of MessageDigest, KeyFactory, etc. The basic idea was to replace
the bulk of each `getInstance' with a call to a new package-private
class method (and class) Engine.getInstance, which should both make
these factory methods more robust without repeating a lot of code in
each class. There is also a fix for a bias in the `next(int numBits)'
method of SecureRandom (filed as PR 9271).
One new package-private class -- java.security.Engine -- is added.
Summary of changes follows:
* AlgorithmParameterGenerator: modify getInstance methods to use
the Engine class; adds JDK1.4 getInstance method; getInstance
throws an exception if provider argument is null; minor
comment improvements or elaborations; copyright updated to
include 2003.
* AlgorithmParameters: likewise.
* KeyFactory: likewise.
* KeyPairGenerator: likewise.
* MessageDigest: likewise.
* Signature: likewise.
* SecureRandom: likewise; fix for a bias in the `next(int
numBits)' method.
* KeyStore: modify getInstance methods to use the Engine class.
* Engine: new package-private class with a single package-
private class method that implements a general getInstance.
* libjava/Makefile.am: adds java/security/Engine.java.
Cheers,
--
Casey Marshall || rsdio@metastatic.org
Index: libjava/Makefile.am
===================================================================
RCS file: /cvsroot/gcc/gcc/libjava/Makefile.am,v
retrieving revision 1.267
diff -u -r1.267 Makefile.am
--- libjava/Makefile.am 2 Jan 2003 00:14:21 -0000 1.267
+++ libjava/Makefile.am 17 Jan 2003 15:56:23 -0000
@@ -2106,6 +2106,7 @@
java/security/DigestInputStream.java \
java/security/DomainCombiner.java \
java/security/DummyMessageDigest.java \
+java/security/Engine.java \
java/security/GeneralSecurityException.java \
java/security/Guard.java \
java/security/GuardedObject.java \
Index: libjava/java/security/AlgorithmParameterGenerator.java
===================================================================
RCS file: /cvsroot/gcc/gcc/libjava/java/security/AlgorithmParameterGenerator.java,v
retrieving revision 1.2
diff -u -r1.2 AlgorithmParameterGenerator.java
--- libjava/java/security/AlgorithmParameterGenerator.java 22 Jan 2002 22:40:30 -0000 1.2
+++ libjava/java/security/AlgorithmParameterGenerator.java 17 Jan 2003 15:56:24 -0000
@@ -1,5 +1,5 @@
/* AlgorithmParameterGenerator.java --- Algorithm Parameter Generator
- Copyright (C) 1999 Free Software Foundation, Inc.
+ Copyright (C) 1999, 2003 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -39,20 +39,43 @@
import java.security.spec.AlgorithmParameterSpec;
/**
- AlgorithmParameterGenerator is used to generate
- algorithm parameters for specified algorithms.
- This class is used to generate the algorithm parameters
- for a specific algorithm.
-
- @since JDK 1.2
- @author Mark Benvenuto
+ * Algorithm parameter generators produce {@link AlgorithmParameters}
+ * for various cryptographic algorithms.
+ *
+ * <p>Parameter generation my be done in an either algorithm-independent
+ * or -dependent fashion. The algorithm-independent way takes only a
+ * single integer argument (or a default) that is typically interpreted
+ * as the "size" -- for example in the case of the <acronym
+ * name="Digital Signature Algorithm">DSA</acronym> signature
+ * algorithm this is the size of the prime modulus, in bits.
+ *
+ * <p>The algorithm-dependent approach takes a parameter specification
+ * as its argument (an implementation of {@link AlgorithmParameterSpec}),
+ * and from that produces the appropriate algorithm parameters.
+ *
+ * @see AlgorithmParameters
+ * @see AlgorithmParameterSpec
+ * @since JDK 1.2
+ * @author Mark Benvenuto
+ * @author Casey Marshall
*/
public class AlgorithmParameterGenerator
{
+
+ // Constants and fields.
+ // ------------------------------------------------------------------------
+
+ /** Service name for algorithm parameter generators. */
+ private static final String ALGORITHM_PARAMETER_GENERATOR =
+ "AlgorithmParameterGenerator";
+
private AlgorithmParameterGeneratorSpi paramGenSpi;
private Provider provider;
private String algorithm;
+ // Constructor.
+ // ------------------------------------------------------------------------
+
/**
Creates an instance of AlgorithmParameters
@@ -69,92 +92,102 @@
this.algorithm = algorithm;
}
- /**
- Returns the name of the algorithm used
-
- @return A string with the name of the algorithm
- */
- public final String getAlgorithm()
- {
- return algorithm;
- }
+ // Class methods.
+ // ------------------------------------------------------------------------
/**
- Gets an instance of the AlgorithmParameterGenerator class
- which generates algorithm parameters for the specified algorithm.
- If the algorithm is not found then, it throws NoSuchAlgorithmException.
-
- @param algorithm the name of algorithm to choose
- @return a AlgorithmParameterGenerator repesenting the desired algorithm
-
- @throws NoSuchAlgorithmException if the algorithm is not implemented by providers
+ * Gets an instance of the AlgorithmParameterGenerator class
+ * which generates algorithm parameters for the specified algorithm.
+ * If the algorithm is not found then, it throws NoSuchAlgorithmException.
+ *
+ * @param algorithm the name of algorithm to choose
+ * @return a AlgorithmParameterGenerator repesenting the desired algorithm
+ *
+ * @throws NoSuchAlgorithmException if the algorithm is not implemented by
+ * providers, or if the implmementation cannot be instantiated.
*/
public static AlgorithmParameterGenerator getInstance(String algorithm)
throws NoSuchAlgorithmException
{
Provider[] p = Security.getProviders();
- for (int i = 0; i < p.length; i++)
- {
- String classname =
- p[i].getProperty("AlgorithmParameterGenerator." + algorithm);
- if (classname != null)
- return getInstance(classname, algorithm, p[i]);
- }
+ for (int i = 0; i < p.length; i++) {
+ try {
+ return getInstance(algorithm, p[i]);
+ } catch (NoSuchAlgorithmException ignore) { }
+ }
throw new NoSuchAlgorithmException(algorithm);
}
/**
- Gets an instance of the AlgorithmParameterGenerator class
- which generates algorithm parameters for the specified algorithm.
- If the algorithm is not found then, it throws NoSuchAlgorithmException.
-
- @param algorithm the name of algorithm to choose
- @param provider the name of the provider to find the algorithm in
- @return a AlgorithmParameterGenerator repesenting the desired algorithm
-
- @throws NoSuchAlgorithmException if the algorithm is not implemented by the provider
- @throws NoSuchProviderException if the provider is not found
+ * Get an algorithm parameter generator for the given algorithm from
+ * the named provider.
+ *
+ * @param algorithm the name of algorithm to choose
+ * @param provider the name of the provider to find the algorithm in
+ * @return an AlgorithmParameterGenerator for the desired algorithm
+ *
+ * @throws NoSuchAlgorithmException if the algorithm is not implemented
+ * by the provider, or if the implementation cannot be
+ * instantiated.
+ * @throws NoSuchProviderException If there is no provider named
+ * <code>provider</code> currently installed.
+ * @throws IllegalArgumentException If <code>provider</code> is null
+ * or empty.
*/
- public static AlgorithmParameterGenerator getInstance(String algorithm,
- String provider)
- throws NoSuchAlgorithmException, NoSuchProviderException
+ public static AlgorithmParameterGenerator
+ getInstance(String algorithm, String provider)
+ throws NoSuchAlgorithmException, NoSuchProviderException
{
+ if (provider == null || provider.length() == 0)
+ throw new IllegalArgumentException("Illegal provider");
Provider p = Security.getProvider(provider);
if (p == null)
throw new NoSuchProviderException();
- return getInstance(p.
- getProperty("AlgorithmParameterGenerator." +
- algorithm), algorithm, p);
+ return getInstance(algorithm, p);
}
- private static AlgorithmParameterGenerator getInstance(String classname,
- String algorithm,
- Provider provider)
- throws NoSuchAlgorithmException
+ /**
+ * Get an algorithm parameter generator for the given algorithm from
+ * the specified provider.
+ *
+ * @param algorithm The algorithm to get the parameter generator for.
+ * @param provider The provider to get the implementation from.
+ * @throws NoSuchAlgorithmException If the provider does not implement
+ * a parameter generator for the given algorithm, or if the
+ * implementation cannot be instantiated.
+ * @throws IllegalArgumentExcpetion If <code>provider</code> is null.
+ * @since 1.4
+ */
+ public static AlgorithmParameterGenerator
+ getInstance(String algorithm, Provider provider)
+ throws NoSuchAlgorithmException
{
+ if (provider == null)
+ throw new IllegalArgumentException("Illegal provider");
+ try {
+ return new AlgorithmParameterGenerator(
+ (AlgorithmParameterGeneratorSpi) Engine.getInstance(
+ ALGORITHM_PARAMETER_GENERATOR, algorithm, provider),
+ provider, algorithm);
+ } catch (ClassCastException cce) {
+ throw new NoSuchAlgorithmException(algorithm);
+ }
+ }
- try
- {
- return new
- AlgorithmParameterGenerator((AlgorithmParameterGeneratorSpi) Class.
- forName(classname).newInstance(),
- provider, algorithm);
- }
- catch (ClassNotFoundException cnfe)
- {
- throw new NoSuchAlgorithmException("Class not found");
- }
- catch (InstantiationException ie)
- {
- throw new NoSuchAlgorithmException("Class instantiation failed");
- }
- catch (IllegalAccessException iae)
- {
- throw new NoSuchAlgorithmException("Illegal Access");
- }
+ // Instance methods.
+ // ------------------------------------------------------------------------
+
+ /**
+ Returns the name of the algorithm used
+
+ @return A string with the name of the algorithm
+ */
+ public final String getAlgorithm()
+ {
+ return algorithm;
}
/**
Index: libjava/java/security/AlgorithmParameters.java
===================================================================
RCS file: /cvsroot/gcc/gcc/libjava/java/security/AlgorithmParameters.java,v
retrieving revision 1.2
diff -u -r1.2 AlgorithmParameters.java
--- libjava/java/security/AlgorithmParameters.java 22 Jan 2002 22:40:30 -0000 1.2
+++ libjava/java/security/AlgorithmParameters.java 17 Jan 2003 15:56:24 -0000
@@ -1,5 +1,5 @@
/* AlgorithmParameters.java --- Algorithm Parameters Implementation Class
- Copyright (C) 1999 Free Software Foundation, Inc.
+ Copyright (C) 1999, 2003 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -35,32 +35,49 @@
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
-
package java.security;
+
import java.security.spec.InvalidParameterSpecException;
import java.security.spec.AlgorithmParameterSpec;
import java.io.IOException;
/**
- AlgorithmParameters is the Algorithm Parameters class which
- provides an interface through which to modify parameters for
- classes. This class is used to manage the algorithm parameters.
-
- @since JDK 1.2
- @author Mark Benvenuto
+ * An opaque representation of a cryptographic algorithm's parameters.
+ *
+ * <p>This class provides methods for encoding and decoding algorithm
+ * parameters to and from standard formats (<acronym
+ * name="Abstract Syntax Notation One">ASN.1</acronym> for example), and
+ * for translating parameters to and from transparent representations
+ * (subclasses of {@link AlgorithmParameterSpec}).
+ *
+ * @see AlgorithmParameterGenerator
+ * @see AlgorithmParameterSpec
+ * @since JDK 1.2
+ * @author Mark Benvenuto
+ * @author Casey Marshall
*/
public class AlgorithmParameters
{
+
+ // Constants and fields.
+ // ------------------------------------------------------------------------
+
+ /** Service name for algorithm parameters. */
+ private static final String ALGORITHM_PARAMETERS = "AlgorithmParameters";
+
private AlgorithmParametersSpi paramSpi;
private Provider provider;
private String algorithm;
- /**
- Creates an instance of AlgorithmParameters
+ // Constructor.
+ // ------------------------------------------------------------------------
- @param paramSpi A parameters engine to use
- @param provider A provider to use
- @param algorithm The algorithm
+ /**
+ * Creates an instance of AlgorithmParameters
+ *
+ * @param paramSpi A parameters engine to use
+ * @param provider A provider to use
+ * @param algorithm The algorithm
*/
protected AlgorithmParameters(AlgorithmParametersSpi paramSpi,
Provider provider, String algorithm)
@@ -70,98 +87,96 @@
this.algorithm = algorithm;
}
- /**
- Returns the name of the algorithm used
-
- @return A string with the name of the algorithm
- */
- public final String getAlgorithm()
- {
- return algorithm;
- }
-
- /**
- Gets an instance of the AlgorithmParameters class representing
- the specified algorithm parameters. If the algorithm is not
- found then, it throws NoSuchAlgorithmException.
+ // Class methods.
+ // ------------------------------------------------------------------------
- The returned AlgorithmParameters must still be intialized with
- init().
-
- @param algorithm the name of algorithm to choose
- @return a AlgorithmParameters repesenting the desired algorithm
-
- @throws NoSuchAlgorithmException if the algorithm is not implemented by providers
+ /**
+ * Returns an instance of AlgorithmParameters for the given algorithm.
+ *
+ * @param algorithm the name of algorithm to choose
+ * @return An AlgorithmParameters for the given algorithm
+ * @throws NoSuchAlgorithmException If no provider implements algorithm
+ * parameters for the given algorithm or if no implementation
+ * can be instantiated.
*/
public static AlgorithmParameters getInstance(String algorithm) throws
NoSuchAlgorithmException
{
Provider[] p = Security.getProviders();
- for (int i = 0; i < p.length; i++)
- {
- String classname =
- p[i].getProperty("AlgorithmParameters." + algorithm);
- if (classname != null)
- return getInstance(classname, algorithm, p[i]);
- }
+ for (int i = 0; i < p.length; i++) {
+ try {
+ return getInstance(algorithm, p[i]);
+ } catch (NoSuchAlgorithmException ignored) { }
+ }
throw new NoSuchAlgorithmException(algorithm);
}
- /**
- Gets an instance of the AlgorithmParameters class representing
- the specified algorithm parameters from the specified provider.
- If the algorithm is not found then, it throws
- NoSuchAlgorithmException. If the provider is not found, then
- it throws NoSuchProviderException.
-
- The returned AlgorithmParameters must still be intialized with
- init().
-
- @param algorithm the name of algorithm to choose
- @param provider the name of the provider to find the algorithm in
- @return a AlgorithmParameters repesenting the desired algorithm
-
- @throws NoSuchAlgorithmException if the algorithm is not implemented by the provider
- @throws NoSuchProviderException if the provider is not found
+ /**
+ * Returns an instance of AlgorithmParameters for the given algorithm
+ * from the named provider.
+ *
+ * @param algorithm the name of algorithm to choose
+ * @param provider the name of the provider to find the algorithm in
+ * @return a AlgorithmParameters repesenting the desired algorithm
+ * @throws NoSuchAlgorithmException if the algorithm is not implemented by
+ * the provider, or if the implementation cannot be instantiated.
+ * @throws NoSuchProviderException If no provider named <code>provider</code>
+ * is installed.
+ * @throws IllegalArgumentException If <code>provider</code> is null
+ * or is empty.
*/
- public static AlgorithmParameters getInstance(String algorithm,
- String provider) throws
- NoSuchAlgorithmException, NoSuchProviderException
+ public static AlgorithmParameters
+ getInstance(String algorithm, String provider)
+ throws NoSuchAlgorithmException, NoSuchProviderException
{
+ if (provider == null || provider.length() == 0)
+ throw new IllegalArgumentException("Illegal provider");
Provider p = Security.getProvider(provider);
if (p == null)
throw new NoSuchProviderException();
- return getInstance(p.getProperty("AlgorithmParameters." + algorithm),
- algorithm, p);
+ return getInstance(algorithm, p);
}
- private static AlgorithmParameters getInstance(String classname,
- String algorithm,
- Provider provider)
- throws NoSuchAlgorithmException
+ /**
+ * Returns an instance of AlgorithmParameters for the given algorithm
+ * from the specified provider.
+ *
+ * @param algorithm the name of algorithm to choose
+ * @param provider the name of the provider to find the algorithm in
+ * @return a AlgorithmParameters repesenting the desired algorithm
+ * @throws NoSuchAlgorithmException if the algorithm is not implemented by
+ * the provider, or if the implementation cannot be instantiated.
+ * @throws IllegalArgumentException If <code>provider</code> is null.
+ */
+ public static AlgorithmParameters
+ getInstance(String algorithm, Provider provider)
+ throws NoSuchAlgorithmException
{
+ if (provider == null)
+ throw new IllegalArgumentException("Illegal provider");
+ try {
+ return new AlgorithmParameters((AlgorithmParametersSpi)
+ Engine.getInstance(ALGORITHM_PARAMETERS, algorithm, provider),
+ provider, algorithm);
+ } catch (ClassCastException cce) {
+ throw new NoSuchAlgorithmException(algorithm);
+ }
+ }
+
+ // Instnace methods.
+ // ------------------------------------------------------------------------
- try
- {
- return new AlgorithmParameters((AlgorithmParametersSpi) Class.
- forName(classname).newInstance(),
- provider, algorithm);
- }
- catch (ClassNotFoundException cnfe)
- {
- throw new NoSuchAlgorithmException("Class not found");
- }
- catch (InstantiationException ie)
- {
- throw new NoSuchAlgorithmException("Class instantiation failed");
- }
- catch (IllegalAccessException iae)
- {
- throw new NoSuchAlgorithmException("Illegal Access");
- }
+ /**
+ Returns the name of the algorithm used
+
+ @return A string with the name of the algorithm
+ */
+ public final String getAlgorithm()
+ {
+ return algorithm;
}
/**
Index: libjava/java/security/Engine.java
===================================================================
RCS file: libjava/java/security/Engine.java
diff -N libjava/java/security/Engine.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ libjava/java/security/Engine.java 17 Jan 2003 15:56:25 -0000
@@ -0,0 +1,140 @@
+/* Engine -- generic getInstance method.
+ Copyright (C) 2003 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the 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, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+02111-1307 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+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;
+
+/**
+ * Generic implementation of the getInstance methods in the various
+ * engine classes in java.security.
+ * <p>
+ * These classes ({@link java.security.Signature} for example) can be
+ * thought of as the "chrome, upholstery, and steering wheel", and the SPI
+ * (service provider interface, e.g. {@link java.security.SignatureSpi})
+ * classes can be thought of as the "engine" -- providing the actual
+ * functionality of whatever cryptographic algorithm the instance
+ * represents.
+ *
+ * @see Provider
+ * @author Casey Marshall
+ */
+final class Engine {
+
+ // Constants.
+ // ------------------------------------------------------------------------
+
+ /** Prefix for aliases. */
+ private static final String ALG_ALIAS = "Alg.Alias.";
+
+ /** Maximum number of aliases to try. */
+ private static final int MAX_ALIASES = 5;
+
+ // Constructors.
+ // ------------------------------------------------------------------------
+
+ /** This class cannot be instantiated. */
+ private Engine() { }
+
+ // Class methods.
+ // ------------------------------------------------------------------------
+
+ /**
+ * Get the implementation for <i>algorithm</i> for service
+ * <i>service</i> from <i>provider</i>. The service is e.g.
+ * "Signature", and the algorithm "DSA".
+ *
+ * @param service The service name.
+ * @param algorithm The name of the algorithm to get.
+ * @param provider The provider to get the implementation from.
+ * @return The engine class for the specified algorithm; the object
+ * returned is typically a subclass of the SPI class for that
+ * service, but callers should check that this is so.
+ * @throws NoSuchAlgorithmException If the implementation cannot be
+ * found or cannot be instantiated.
+ * @throws IllegalArgumentException If any of the three arguments are null.
+ */
+ static Object
+ getInstance(String service, String algorithm, Provider provider)
+ throws NoSuchAlgorithmException
+ {
+ if (service == null || algorithm == null || provider == null)
+ throw new IllegalArgumentException();
+
+ // If there is no property "service.algorithm"
+ if (provider.getProperty(service + "." + algorithm) == null) {
+ // Iterate through aliases, until we find the class name or resolve
+ // too many aliases.
+ String alias = null;
+ int count = 0;
+ while ((alias = provider.getProperty(
+ ALG_ALIAS + service + "." + algorithm)) != null)
+ {
+ if (algorithm.equals(alias)) // Refers to itself!
+ break;
+ algorithm = alias;
+ if (count++ > MAX_ALIASES)
+ throw new NoSuchAlgorithmException("too many aliases");
+ }
+ if (provider.getProperty(service + "." + algorithm) == null)
+ throw new NoSuchAlgorithmException(algorithm);
+ }
+
+ // Find and instantiate the implementation.
+ Class clazz = null;
+ ClassLoader loader = provider.getClass().getClassLoader();
+ String error = algorithm;
+ try {
+ if (loader != null) {
+ clazz = loader.loadClass(provider.getProperty(service + "." + algorithm));
+ } else {
+ clazz = Class.forName(provider.getProperty(service + "." + algorithm));
+ }
+ return clazz.newInstance();
+ } catch (ClassNotFoundException cnfe) {
+ error = "class not found: " + algorithm;
+ } catch (IllegalAccessException iae) {
+ error = "illegal access: " + iae.getMessage();
+ } catch (InstantiationException ie) {
+ error = "instantiation exception: " + ie.getMessage();
+ } catch (ExceptionInInitializerError eiie) {
+ error = "exception in initializer: " + eiie.getMessage();
+ } catch (SecurityException se) {
+ error = "security exception: " + se.getMessage();
+ }
+
+ throw new NoSuchAlgorithmException(error);
+ }
+}
Index: libjava/java/security/KeyFactory.java
===================================================================
RCS file: /cvsroot/gcc/gcc/libjava/java/security/KeyFactory.java,v
retrieving revision 1.2
diff -u -r1.2 KeyFactory.java
--- libjava/java/security/KeyFactory.java 22 Jan 2002 22:40:30 -0000 1.2
+++ libjava/java/security/KeyFactory.java 17 Jan 2003 15:56:25 -0000
@@ -1,5 +1,5 @@
/* KeyFactory.java --- Key Factory Class
- Copyright (C) 1999 Free Software Foundation, Inc.
+ Copyright (C) 1999, 2003 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -36,31 +36,42 @@
exception statement from your version. */
package java.security;
+
import java.security.spec.KeySpec;
import java.security.spec.InvalidKeySpecException;
/**
- Key factories are used to convert keys (opaque cryptographic
- keys of type Key) into key specifications (transparent
- representations of the underlying key material).
-
- Key factories are bi-directional. They allow a key class
- to be converted into a key specification (key material) and
- back again.
-
- For example DSA public keys can be specified as
- DSAPublicKeySpec or X509EncodedKeySpec. The key factory
- translate these key specifications.
-
- @since JDK 1.2
- @author Mark Benvenuto
+ * Key factories are used to convert keys (opaque cryptographic keys of
+ * type {@link Key}) to and from key specifications (transparent
+ * representations of the underlying key material), and to translate
+ * keys into vendor-specific types.
+ *
+ * <p>For example, DSA public keys can be specified as {@link
+ * java.security.spec.DSAPublicKeySpec} or {@link
+ * java.security.spec.X509EncodedKeySpec}, and DSA key factories can
+ * translate these key specifications into instances of {@link
+ * java.security.interfaces.DSAKey}.
+ *
+ * @since JDK 1.2
+ * @author Mark Benvenuto
+ * @author Casey Marshall
*/
public class KeyFactory
{
+
+ // Constants and fields.
+ // ------------------------------------------------------------------------
+
+ /** The service name for key factories. */
+ private static final String KEY_FACTORY = "KeyFactory";
+
private KeyFactorySpi keyFacSpi;
private Provider provider;
private String algorithm;
+ // Constructors.
+ // ------------------------------------------------------------------------
+
/**
Constructs a new keyFactory with the specified parameters.
@@ -76,15 +87,16 @@
this.algorithm = algorithm;
}
- /**
- Gets an instance of the KeyFactory class representing
- the specified key factory. If the algorithm is not
- found then, it throws NoSuchAlgorithmException.
+ // Class methods.
+ // ------------------------------------------------------------------------
- @param algorithm the name of algorithm to choose
- @return a KeyFactory repesenting the desired algorithm
-
- @throws NoSuchAlgorithmException if the algorithm is not implemented by providers
+ /**
+ * Get a key factory from the first provider that implements it.
+ *
+ * @param algorithm the name of algorithm to choose
+ * @return a KeyFactory repesenting the desired algorithm
+ * @throws NoSuchAlgorithmException If the algorithm is not implemented
+ * by providers or if the implementation cannot be instantiated.
*/
public static KeyFactory getInstance(String algorithm)
throws NoSuchAlgorithmException
@@ -93,63 +105,65 @@
for (int i = 0; i < p.length; i++)
{
- String classname = p[i].getProperty("KeyFactory." + algorithm);
- if (classname != null)
- return getInstance(classname, algorithm, p[i]);
+ try {
+ return getInstance(algorithm, p[i]);
+ } catch (NoSuchAlgorithmException ignore) { }
}
throw new NoSuchAlgorithmException(algorithm);
}
- /**
- Gets an instance of the KeyFactory class representing
- the specified key factory from the specified provider.
- If the algorithm is not found then, it throws
- NoSuchAlgorithmException. If the provider is not found, then
- it throws NoSuchProviderException.
-
- @param algorithm the name of algorithm to choose
- @param provider the name of the provider to find the algorithm in
- @return a KeyFactory repesenting the desired algorithm
-
- @throws NoSuchAlgorithmException if the algorithm is not implemented by the provider
- @throws NoSuchProviderException if the provider is not found
+ /**
+ * Get a key factory from a named provider.
+ *
+ * @param algorithm The name of the algorithm.
+ * @param provider The name of the provider.
+ * @return a KeyFactory for the desired algorithm
+ * @throws NoSuchAlgorithmException If the algorithm is not implemented
+ * by the provider or if the implementation cannot be instantiated.
+ * @throws NoSuchProviderException If the provider is not found.
+ * @throws IllegalArgumentException If <code>provider</code> is null or
+ * is empty.
*/
public static KeyFactory getInstance(String algorithm, String provider)
throws NoSuchAlgorithmException, NoSuchProviderException
{
+ if (provider == null || provider.length() == 0)
+ throw new IllegalArgumentException("Illegal provider");
Provider p = Security.getProvider(provider);
if (p == null)
throw new NoSuchProviderException();
- return getInstance(p.getProperty("KeyFactory." + algorithm),
- algorithm, p);
+ return getInstance(algorithm, p);
}
- private static KeyFactory getInstance(String classname,
- String algorithm,
- Provider provider)
- throws NoSuchAlgorithmException
+ /**
+ * Get a key factory from the specified provider.
+ *
+ * @param algorithm The name of the algorithm.
+ * @param provider The provider to get the implementation from.
+ * @return A key factory for the given algorithm.
+ * @throws NoSuchAlgorithmException If the provider does not implement
+ * the given algorithm, or if the implementation cannot be
+ * instantiated.
+ * @throws IllegalArgumentException If <code>provider</code> is null.
+ */
+ public static KeyFactory getInstance(String algorithm, Provider provider)
+ throws NoSuchAlgorithmException
{
-
- try
- {
- return new KeyFactory((KeyFactorySpi) Class.forName(classname).
- newInstance(), provider, algorithm);
- }
- catch (ClassNotFoundException cnfe)
- {
- throw new NoSuchAlgorithmException("Class not found");
- }
- catch (InstantiationException ie)
- {
- throw new NoSuchAlgorithmException("Class instantiation failed");
- }
- catch (IllegalAccessException iae)
- {
- throw new NoSuchAlgorithmException("Illegal Access");
- }
+ if (provider == null)
+ throw new IllegalArgumentException("Illegal provider");
+ try {
+ return new KeyFactory((KeyFactorySpi)
+ Engine.getInstance(KEY_FACTORY, algorithm, provider),
+ provider, algorithm);
+ } catch (ClassCastException cce) {
+ throw new NoSuchAlgorithmException(algorithm);
+ }
}
+
+ // Instance methods.
+ // ------------------------------------------------------------------------
/**
Gets the provider that the class is from.
Index: libjava/java/security/KeyPairGenerator.java
===================================================================
RCS file: /cvsroot/gcc/gcc/libjava/java/security/KeyPairGenerator.java,v
retrieving revision 1.4
diff -u -r1.4 KeyPairGenerator.java
--- libjava/java/security/KeyPairGenerator.java 17 Nov 2002 00:10:24 -0000 1.4
+++ libjava/java/security/KeyPairGenerator.java 17 Jan 2003 15:56:29 -0000
@@ -1,5 +1,5 @@
/* KeyPairGenerator.java --- Key Pair Generator Class
- Copyright (C) 1999, 2002 Free Software Foundation, Inc.
+ Copyright (C) 1999, 2002, 2003 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -40,25 +40,34 @@
import java.security.spec.AlgorithmParameterSpec;
/**
- KeyPairGenerator is the class used to generate key pairs
- for a security algorithm.
-
- The KeyPairGenerator is created with the getInstance()
- methods. The class is used to generate public and private
- keys for an algorithm and associate it with
- algorithm parameters.
-
- @author Mark Benvenuto
+ * An interface to a key pair generator. Key pairs are used in various
+ * <i>public-key</i> cryptographic algorithms, such as digital signing,
+ * key agreement, and asymmetric cryptography.
+ *
+ * @see Signature
+ * @see KeyPair
+ * @author Mark Benvenuto
+ * @author Casey Marshall
*/
public abstract class KeyPairGenerator extends KeyPairGeneratorSpi
{
+
+ // Constants and fields.
+ // ------------------------------------------------------------------------
+
+ /** The service name for key pair generators. */
+ private static final String KEY_PAIR_GENERATOR = "KeyPairGenerator";
+
Provider provider;
private String algorithm;
- /**
- Constructs a new KeyPairGenerator
+ // Constructors.
+ // ------------------------------------------------------------------------
- @param algorithm the algorithm to use
+ /**
+ * Constructs a new KeyPairGenerator
+ *
+ * @param algorithm the algorithm to use
*/
protected KeyPairGenerator(String algorithm)
{
@@ -66,60 +75,52 @@
this.provider = null;
}
- /**
- Returns the name of the algorithm used
-
- @return A string with the name of the algorithm
- */
- public String getAlgorithm()
- {
- return algorithm;
- }
-
- /**
- Gets an instance of the KeyPairGenerator class
- which generates key pairs for the specified algorithm.
- If the algorithm is not found then, it throws NoSuchAlgorithmException.
-
- @param algorithm the name of algorithm to choose
- @return a AlgorithmParameterGenerator repesenting the desired algorithm
+ // Class methods.
+ // ------------------------------------------------------------------------
- @throws NoSuchAlgorithmException if the algorithm is not implemented by
- providers
+ /**
+ * Get a key pair generator for the specified algorithm from the first
+ * provider that implements it.
+ *
+ * @param algorithm the name of algorithm to choose
+ * @return a AlgorithmParameterGenerator repesenting the desired algorithm
+ * @throws NoSuchAlgorithmException if the algorithm is not implemented by
+ * any installed provider or if no implementation can be
+ * instantiated.
*/
- public static KeyPairGenerator getInstance(String algorithm) throws
- NoSuchAlgorithmException
+ public static KeyPairGenerator getInstance(String algorithm)
+ throws NoSuchAlgorithmException
{
Provider[] p = Security.getProviders();
- for (int i = 0; i < p.length; i++)
- {
- try
- {
- return getInstance(algorithm, p[i]);
- }
- catch (NoSuchAlgorithmException ignored) {}
- }
+ for (int i = 0; i < p.length; i++) {
+ try {
+ return getInstance(algorithm, p[i]);
+ } catch (NoSuchAlgorithmException ignored) { }
+ }
throw new NoSuchAlgorithmException(algorithm);
}
- /**
- Gets an instance of the KeyPairGenerator class
- which generates key pairs for the specified algorithm.
- If the algorithm is not found then, it throws NoSuchAlgorithmException.
-
- @param algorithm the name of algorithm to choose
- @param provider the name of the provider to find the algorithm in
- @return a AlgorithmParameterGenerator repesenting the desired algorithm
-
- @throws NoSuchAlgorithmException if the algorithm is not implemented by
- the provider
- @throws NoSuchProviderException if the provider is not found
+ /**
+ * Get a key pair generator for the specified algorithm from the named
+ * provider.
+ *
+ * @param algorithm The name of the algorithm.
+ * @param provider The name of the provider.
+ * @return A KeyPairGenerator for the desired algorithm.
+ * @throws NoSuchAlgorithmException If the algorithm is not implemented by
+ * the provider or if the implementation cannot be instantiated.
+ * @throws NoSuchProviderException If no provider named
+ * <code>provider</code> is installed.
+ * @throws IllegalArgumentException If <code>provider</code> is null
+ * or is empty.
*/
public static KeyPairGenerator getInstance(String algorithm, String provider)
- throws NoSuchAlgorithmException, NoSuchProviderException
+ throws NoSuchAlgorithmException, NoSuchProviderException
{
+ if (provider == null || provider.length() == 0)
+ throw new IllegalArgumentException("Illegal provider");
Provider p = Security.getProvider(provider);
if (p == null)
throw new NoSuchProviderException(provider);
@@ -127,63 +128,48 @@
return getInstance(algorithm, p);
}
- private static KeyPairGenerator getInstance(String algorithm, Provider p)
- throws NoSuchAlgorithmException
- {
- // try the name as is
- String className = p.getProperty("KeyPairGenerator." + algorithm);
- if (className == null) { // try all uppercase
- String upper = algorithm.toUpperCase();
- className = p.getProperty("KeyPairGenerator." + upper);
- if (className == null) { // try if it's an alias
- String alias = p.getProperty("Alg.Alias.KeyPairGenerator." + algorithm);
- if (alias == null) { // try all-uppercase alias name
- alias = p.getProperty("Alg.Alias.KeyPairGenerator." + upper);
- if (alias == null) { // spit the dummy
- throw new NoSuchAlgorithmException(algorithm);
- }
- }
- className = p.getProperty("KeyPairGenerator." + alias);
- if (className == null) {
- throw new NoSuchAlgorithmException(algorithm);
- }
- }
+ /**
+ * Get a key pair generator for the specified algorithm from the given
+ * provider.
+ *
+ * @param algorithm The name of the algorithm.
+ * @param provider The provider.
+ * @return A KeyPairGenerator for the desired algorithm.
+ * @throws NoSuchAlgorithmException If the algorithm is not implemented by
+ * the provider or if the implementation cannot be instantiated.
+ * @throws IllegalArgumentException If <code>provider</code> is null.
+ * @since 1.4
+ */
+ public static KeyPairGenerator
+ getInstance(String algorithm, Provider provider)
+ throws NoSuchAlgorithmException
+ {
+ if (provider == null)
+ throw new IllegalArgumentException("Illegal provider");
+
+ Object o = Engine.getInstance(KEY_PAIR_GENERATOR, algorithm, provider);
+ KeyPairGenerator result = null;
+ if (o instanceof KeyPairGeneratorSpi) {
+ result = new DummyKeyPairGenerator((KeyPairGeneratorSpi) o, algorithm);
+ } else if (o instanceof KeyPairGenerator) {
+ result = (KeyPairGenerator) o;
+ result.algorithm = algorithm;
}
- return getInstance(className, algorithm, p);
+ result.provider = provider;
+ return result;
}
- private static KeyPairGenerator getInstance(String classname,
- String algorithm,
- Provider provider)
- throws NoSuchAlgorithmException
+ // Instance methods.
+ // ------------------------------------------------------------------------
+
+ /**
+ Returns the name of the algorithm used
+
+ @return A string with the name of the algorithm
+ */
+ public String getAlgorithm()
{
- try
- {
- Object o = Class.forName(classname).newInstance();
- KeyPairGenerator kpg;
- if (o instanceof KeyPairGeneratorSpi)
- kpg = new DummyKeyPairGenerator((KeyPairGeneratorSpi) o, algorithm);
- else
- {
- kpg = (KeyPairGenerator) o;
- kpg.algorithm = algorithm;
- }
-
- kpg.provider = provider;
- return kpg;
- }
- catch (ClassNotFoundException cnfe)
- {
- throw new NoSuchAlgorithmException("Class not found");
- }
- catch (InstantiationException ie)
- {
- throw new NoSuchAlgorithmException("Class instantiation failed");
- }
- catch (IllegalAccessException iae)
- {
- throw new NoSuchAlgorithmException("Illegal Access");
- }
+ return algorithm;
}
/**
Index: libjava/java/security/KeyStore.java
===================================================================
RCS file: /cvsroot/gcc/gcc/libjava/java/security/KeyStore.java,v
retrieving revision 1.3
diff -u -r1.3 KeyStore.java
--- libjava/java/security/KeyStore.java 18 Nov 2002 18:09:35 -0000 1.3
+++ libjava/java/security/KeyStore.java 17 Jan 2003 15:56:29 -0000
@@ -1,5 +1,5 @@
/* KeyStore.java --- Key Store Class
- Copyright (C) 1999, 2002 Free Software Foundation, Inc.
+ Copyright (C) 1999, 2002, 2003 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -44,37 +44,52 @@
import java.util.Enumeration;
/**
- Keystore represents an in-memory collection of keys and
- certificates. There are two types of entries:
-
- * Key Entry
-
- This type of keystore entry store sensitive crytographic key
- information in a protected format.Typically this is a secret
- key or a private key with a certificate chain.
-
-
- * Trusted Ceritificate Entry
-
- This type of keystore entry contains a single public key
- certificate belonging to annother entity. It is called trusted
- because the keystore owner trusts that the certificates
- belongs to the subject (owner) of the certificate.
-
- The keystore contains an "alias" string for each entry.
-
- The structure and persistentence of the key store is not
- specified. Any method could be used to protect sensitive
- (private or secret) keys. Smart cards or integrated
- cryptographic engines could be used or the keystore could
- be simply stored in a file.
+ * Keystore represents an in-memory collection of keys and
+ * certificates. There are two types of entries:
+ *
+ * <dl>
+ * <dt>Key Entry</dt>
+ *
+ * <dd><p>This type of keystore entry store sensitive crytographic key
+ * information in a protected format.Typically this is a secret
+ * key or a private key with a certificate chain.</p></dd>
+ *
+ * <dt>Trusted Ceritificate Entry</dt>
+ *
+ * <dd><p>This type of keystore entry contains a single public key
+ * certificate belonging to annother entity. It is called trusted
+ * because the keystore owner trusts that the certificates
+ * belongs to the subject (owner) of the certificate.</p></dd>
+ * </dl>
+ *
+ * <p>Entries in a key store are referred to by their "alias": a simple
+ * unique string.
+ *
+ * <p>The structure and persistentence of the key store is not
+ * specified. Any method could be used to protect sensitive
+ * (private or secret) keys. Smart cards or integrated
+ * cryptographic engines could be used or the keystore could
+ * be simply stored in a file.</p>
+ *
+ * @see java.security.cert.Certificate
+ * @see Key
*/
public class KeyStore
{
+
+ // Constants and fields.
+ // ------------------------------------------------------------------------
+
+ /** Service name for key stores. */
+ private static final String KEY_STORE = "KeyStore";
+
private KeyStoreSpi keyStoreSpi;
private Provider provider;
private String type;
+ // Constructors.
+ // ------------------------------------------------------------------------
+
/**
Creates an instance of KeyStore
@@ -89,48 +104,47 @@
this.type = type;
}
- /**
- Gets an instance of the KeyStore class representing
- the specified keystore. If the type is not
- found then, it throws KeyStoreException.
-
- @param type the type of keystore to choose
-
- @return a KeyStore repesenting the desired type
+ // Class methods.
+ // ------------------------------------------------------------------------
- @throws KeyStoreException if the type of keystore is not implemented by providers
+ /**
+ * Gets an instance of the KeyStore class representing
+ * the specified keystore. If the type is not
+ * found then, it throws KeyStoreException.
+ *
+ * @param type the type of keystore to choose
+ * @return a KeyStore repesenting the desired type
+ * @throws KeyStoreException if the type of keystore is not implemented
+ * by providers or the implementation cannot be instantiated.
*/
public static KeyStore getInstance(String type) throws KeyStoreException
{
Provider[] p = Security.getProviders();
- for (int i = 0; i < p.length; i++)
- {
- String classname = p[i].getProperty("KeyStore." + type);
- if (classname != null)
- return getInstance(classname, type, p[i]);
- }
+ for (int i = 0; i < p.length; i++) {
+ try {
+ return getInstance(type, p[i]);
+ } catch (KeyStoreException ignore) { }
+ }
throw new KeyStoreException(type);
}
/**
- Gets an instance of the KeyStore class representing
- the specified key store from the specified provider.
- If the type is not found then, it throws KeyStoreException.
- If the provider is not found, then it throws
- NoSuchProviderException.
-
- @param type the type of keystore to choose
- @param provider the provider name
-
- @return a KeyStore repesenting the desired type
-
- @throws KeyStoreException if the type of keystore is not
- implemented by the given provider
- @throws NoSuchProviderException if the provider is not found
- @throws IllegalArgumentException if the provider string is
- null or empty
+ * Gets an instance of the KeyStore class representing
+ * the specified key store from the specified provider.
+ * If the type is not found then, it throws KeyStoreException.
+ * If the provider is not found, then it throws
+ * NoSuchProviderException.
+ *
+ * @param type the type of keystore to choose
+ * @param provider the provider name
+ * @return a KeyStore repesenting the desired type
+ * @throws KeyStoreException if the type of keystore is not
+ * implemented by the given provider
+ * @throws NoSuchProviderException if the provider is not found
+ * @throws IllegalArgumentException if the provider string is
+ * null or empty
*/
public static KeyStore getInstance(String type, String provider)
throws KeyStoreException, NoSuchProviderException
@@ -141,60 +155,59 @@
if (p == null)
throw new NoSuchProviderException();
- return getInstance(p.getProperty("KeyStore." + type), type, p);
+ return getInstance(type, p);
}
/**
- Gets an instance of the KeyStore class representing
- the specified key store from the specified provider.
- If the type is not found then, it throws KeyStoreException.
- If the provider is not found, then it throws
- NoSuchProviderException.
-
- @param type the type of keystore to choose
- @param provider the keystore provider
-
- @return a KeyStore repesenting the desired type
-
- @throws KeyStoreException if the type of keystore is not
- implemented by the given provider
- @throws IllegalArgumentException if the provider object is null
- @since 1.4
+ * Gets an instance of the KeyStore class representing
+ * the specified key store from the specified provider.
+ * If the type is not found then, it throws KeyStoreException.
+ * If the provider is not found, then it throws
+ * NoSuchProviderException.
+ *
+ * @param type the type of keystore to choose
+ * @param provider the keystore provider
+ * @return a KeyStore repesenting the desired type
+ * @throws KeyStoreException if the type of keystore is not
+ * implemented by the given provider
+ * @throws IllegalArgumentException if the provider object is null
+ * @since 1.4
*/
public static KeyStore getInstance(String type, Provider provider)
throws KeyStoreException
{
if (provider == null)
throw new IllegalArgumentException("Illegal provider");
-
- return getInstance(provider.getProperty("KeyStore." + type),
- type, provider);
+ try {
+ return new KeyStore(
+ (KeyStoreSpi) Engine.getInstance(KEY_STORE, type, provider),
+ provider, type);
+ } catch (NoSuchAlgorithmException nsae) {
+ throw new KeyStoreException(type);
+ } catch (ClassCastException cce) {
+ throw new KeyStoreException(type);
+ }
}
- private static KeyStore getInstance(String classname,
- String type,
- Provider provider)
- throws KeyStoreException
+ /**
+ * Returns the default KeyStore type. This method looks up the
+ * type in <JAVA_HOME>/lib/security/java.security with the
+ * property "keystore.type" or if that fails then "jks" .
+ */
+ public static final String getDefaultType()
{
- try
- {
- return new KeyStore((KeyStoreSpi) Class.forName(classname).
- newInstance(), provider, type);
- }
- catch (ClassNotFoundException cnfe)
- {
- throw new KeyStoreException("Class not found");
- }
- catch (InstantiationException ie)
- {
- throw new KeyStoreException("Class instantiation failed");
- }
- catch (IllegalAccessException iae)
- {
- throw new KeyStoreException("Illegal Access");
- }
+ // Security reads every property in java.security so it
+ // will return this property if it exists.
+ String tmp = Security.getProperty("keystore.type");
+
+ if (tmp == null)
+ tmp = "jks";
+
+ return tmp;
}
+ // Instance methods.
+ // ------------------------------------------------------------------------
/**
Gets the provider that the class is from.
@@ -471,21 +484,4 @@
keyStoreSpi.engineLoad(stream, password);
}
- /**
- Returns the default KeyStore type. This method looks up the
- type in <JAVA_HOME>/lib/security/java.security with the
- property "keystore.type" or if that fails then "jks" .
- */
- public static final String getDefaultType()
- {
- String tmp;
- //Security reads every property in java.security so it
- //will return this property if it exists.
- tmp = Security.getProperty("keystore.type");
-
- if (tmp == null)
- tmp = "jks";
-
- return tmp;
- }
}
Index: libjava/java/security/MessageDigest.java
===================================================================
RCS file: /cvsroot/gcc/gcc/libjava/java/security/MessageDigest.java,v
retrieving revision 1.8
diff -u -r1.8 MessageDigest.java
--- libjava/java/security/MessageDigest.java 17 Nov 2002 00:10:24 -0000 1.8
+++ libjava/java/security/MessageDigest.java 17 Jan 2003 15:56:29 -0000
@@ -1,6 +1,5 @@
-
/* MessageDigest.java --- The message digest interface.
- Copyright (C) 1999, 2002 Free Software Foundation, Inc.
+ Copyright (C) 1999, 2002, 2003 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -38,17 +37,53 @@
package java.security;
+/**
+ * An interface to message digest algorithms. A message digest
+ * algorithm takes as input a variable number of bytes and produces a
+ * fixed-length "fingerprint" of this data, with the property that it is
+ * computationally infeasible to (a) determine the source data from a
+ * given message digest, or (b) find two input texts that produce the same
+ * digest. Message digests are commonly used for ensuring the integrity
+ * or authenticity of data, and are used at the core of many signature
+ * and message authentication algorithms.
+ * <p>
+ * To get an implementation of a message digest, query the {@link
+ * #getInstance(java.lang.String)} method with the name of the
+ * algorithm, e.g.:
+ *
+ * <blockquote>
+ * <code>MessageDigest md = MessageDigest.getInstance("SHA1");</code>
+ * </blockquote>
+ *
+ * <p>
+ * What algorithms are available depend upon what providers are
+ * installed and what message digests they implement.
+ *
+ * @see MessageDigestSpi
+ * @see Provider
+ * @since JDK 1.1
+ */
public abstract class MessageDigest extends MessageDigestSpi
{
+
+ // Constants and fields.
+ // ------------------------------------------------------------------------
+
+ /** The service name for message digests. */
+ private static final String MESSAGE_DIGEST = "MessageDigest";
+
private String algorithm;
Provider provider;
private byte[] lastDigest;
- /**
- Creates a MessageDigest representing the specified
- algorithm.
+ // Constructors.
+ // ------------------------------------------------------------------------
- @param algorithm the name of digest algorithm to choose
+ /**
+ * Creates a MessageDigest representing the specified
+ * algorithm.
+ *
+ * @param algorithm the name of digest algorithm to choose
*/
protected MessageDigest(String algorithm)
{
@@ -56,130 +91,98 @@
provider = null;
}
- /**
- Gets an instance of the MessageDigest class representing
- the specified digest. If the algorithm is not found then,
- it throws NoSuchAlgorithmException.
+ // Class methods.
+ // ------------------------------------------------------------------------
- @param algorithm the name of digest algorithm to choose
- @return a MessageDigest representing the desired algorithm
-
- @exception NoSuchAlgorithmException if the algorithm is not implemented by
- providers
+ /**
+ * Get a message digest from the first provider that implements it.
+ *
+ * @param algorithm The name of digest algorithm.
+ * @return An instance of the desired message digest.
+ * @throws NoSuchAlgorithmException If the algorithm is not implemented
+ * by any installed provider.
+ * @see #getInstance(java.lang.String,java.lang.String)
+ * @see #getInstance(java.lang.String,java.security.Provider)
*/
public static MessageDigest getInstance(String algorithm)
- throws NoSuchAlgorithmException
+ throws NoSuchAlgorithmException
{
Provider[] p = Security.getProviders();
- for (int i = 0; i < p.length; i++)
- {
- try
- {
- return getInstance(algorithm, p[i]);
- }
- catch (NoSuchAlgorithmException ignored) {}
- }
+ for (int i = 0; i < p.length; i++) {
+ try {
+ return getInstance(algorithm, p[i]);
+ } catch (NoSuchAlgorithmException ignored) { }
+ }
throw new NoSuchAlgorithmException(algorithm);
}
- /**
- Gets an instance of the MessageDigest class representing
- the specified digest from the specified provider. If the
- algorithm is not found then, it throws NoSuchAlgorithmException.
- If the provider is not found, then it throws
- NoSuchProviderException.
-
- @param algorithm the name of digest algorithm to choose
- @param provider the name of the provider to find the algorithm in
- @return a MessageDigest representing the desired algorithm
-
- @exception NoSuchAlgorithmException if the algorithm is not implemented by
- the provider
- @exception NoSuchProviderException if the provider is not found
+ /**
+ * Get a message digest from a named provider.
+ *
+ * @param algorithm The name of digest algorithm.
+ * @param provider The name of the provider.
+ * @return An instance of the desired message digest.
+ *
+ * @throws NoSuchAlgorithmException if the algorithm is not implemented by
+ * the provider, or if the implementation cannot be instantiated.
+ * @throws NoSuchProviderException If no provider named
+ * <code>provider</code> is currently installed.
+ * @throws IllegalArgumentException If <code>provider</code> is null
+ * or is empty.
+ * @see #getInstance(java.lang.String,java.security.Provider)
*/
-
public static MessageDigest getInstance(String algorithm, String provider)
- throws NoSuchAlgorithmException, NoSuchProviderException
+ throws NoSuchAlgorithmException, NoSuchProviderException
{
- Provider p = Security.getProvider(provider);
+ if (provider == null || provider.length() == 0)
+ throw new IllegalArgumentException("Illegal provider");
+ Provider p = Security.getProvider(provider);
if (p == null)
throw new NoSuchProviderException(provider);
return getInstance(algorithm, p);
}
- private static MessageDigest getInstance(String algorithm, Provider p)
- throws NoSuchAlgorithmException
- {
- // try the name as is
- String className = p.getProperty("MessageDigest." + algorithm);
- if (className == null) { // try all uppercase
- String upper = algorithm.toUpperCase();
- className = p.getProperty("MessageDigest." + upper);
- if (className == null) { // try if it's an alias
- String alias = p.getProperty("Alg.Alias.MessageDigest." +algorithm);
- if (alias == null) { // try all-uppercase alias name
- alias = p.getProperty("Alg.Alias.MessageDigest." +upper);
- if (alias == null) { // spit the dummy
- throw new NoSuchAlgorithmException(algorithm);
- }
- }
- className = p.getProperty("MessageDigest." + alias);
- if (className == null) {
- throw new NoSuchAlgorithmException(algorithm);
- }
- }
- }
- return getInstance(className, algorithm, p);
- }
-
- private static MessageDigest getInstance(String classname,
- String algorithm,
- Provider provider)
+ /**
+ * Gets an implementation of the specified message digest algorithm
+ * from the specified provider.
+ *
+ * @param algorithm The message digest algorithm's name.
+ * @param provider The provider from which to get the implementation.
+ * @throws NoSuchAlgorithmException If the provider does not implement
+ * the algorithm, or if the implementation cannot be
+ * instantiated.
+ * @throws IllegalArgumentException If <code>provider</code> is null.
+ * @since 1.4
+ */
+ public static MessageDigest getInstance(String algorithm, Provider provider)
throws NoSuchAlgorithmException
{
- if (classname == null)
- throw new NoSuchAlgorithmException(algorithm);
-
+ if (provider == null)
+ throw new IllegalArgumentException("Illegal provider");
MessageDigest result = null;
- try
- {
- Object obj = Class.forName(classname).newInstance();
- if (obj instanceof MessageDigest) {
- result = (MessageDigest) obj;
- result.algorithm = algorithm;
- } else if (obj instanceof MessageDigestSpi) {
- result = new DummyMessageDigest((MessageDigestSpi) obj, algorithm);
- } else {
- throw new ClassCastException("Class "+classname+" from Provider "
- +provider.getName()
- +" does not extend java.security.MessageDigestSpi");
- }
- result.provider = provider;
- return result;
- }
- catch (ClassNotFoundException cnfe)
- {
- throw new NoSuchAlgorithmException(algorithm + ": Class not found.");
- }
- catch (InstantiationException ie)
- {
- throw new NoSuchAlgorithmException(algorithm
- + ": Class instantiation failed.");
- }
- catch (IllegalAccessException iae)
- {
- throw new NoSuchAlgorithmException(algorithm + ": Illegal Access");
- }
+ Object o = Engine.getInstance(MESSAGE_DIGEST, algorithm, provider);
+ if (o instanceof MessageDigestSpi) {
+ result = new DummyMessageDigest((MessageDigestSpi) o, algorithm);
+ } else if (o instanceof MessageDigest) {
+ result = (MessageDigest) o;
+ result.algorithm = algorithm;
+ } else {
+ throw new NoSuchAlgorithmException(algorithm);
+ }
+ result.provider = provider;
+ return result;
}
+ // Instance methods.
+ // ------------------------------------------------------------------------
/**
- Gets the provider that the MessageDigest is from.
-
- @return the provider the this MessageDigest
+ * Gets the provider that the MessageDigest is from.
+ *
+ * @return The provider of this message digest implementation.
*/
public final Provider getProvider()
{
@@ -187,9 +190,9 @@
}
/**
- Updates the digest with the byte.
-
- @param input byte to update the digest with
+ * Updates the digest with a single byte.
+ *
+ * @param input The byte to update the digest with.
*/
public void update(byte input)
{
@@ -197,32 +200,32 @@
}
/**
- Updates the digest with the bytes from the array from the
- specified offset to the specified length.
-
- @param input bytes to update the digest with
- @param offset the offset to start at
- @param len length of the data to update with
+ * Updates the digest with a portion of a byte array.
+ *
+ * @param input The bytes to update the digest with.
+ * @param offset The offset to start at in the array.
+ * @param len The number of bytes to update.
*/
- public void update(byte[]input, int offset, int len)
+ public void update(byte[] input, int offset, int len)
{
engineUpdate(input, offset, len);
}
/**
- Updates the digest with the bytes from the array.
-
- @param input bytes to update the digest with
+ * Updates the digest with an entire byte array.
+ *
+ * @param input The bytes to update the digest with.
+ * @see #update(byte[],int,int)
*/
- public void update(byte[]input)
+ public void update(byte[] input)
{
engineUpdate(input, 0, input.length);
}
/**
- Computes the digest of the stored data.
-
- @return a byte array representing the message digest
+ * Computes the final digest of the stored data, and resets this instance.
+ *
+ * @return a byte array representing the message digest
*/
public byte[] digest()
{
@@ -230,13 +233,13 @@
}
/**
- Computes the final digest of the stored bytes and returns
- them.
-
- @param buf An array of bytes to store the digest
- @param offset An offset to start storing the digest at
- @param len The length of the buffer
- @return Returns the length of the buffer
+ * Computes the final digest of the stored bytes and stores it into
+ * the given byte array, returning the number of bytes stored.
+ *
+ * @param buf An array of bytes to store the digest.
+ * @param offset The offset to start storing the digest at.
+ * @param len The maximum number of bytes to store in the array.
+ * @return The number of bytes stored into <code>buf</code>
*/
public int digest(byte[]buf, int offset, int len) throws DigestException
{
Index: libjava/java/security/SecureRandom.java
===================================================================
RCS file: /cvsroot/gcc/gcc/libjava/java/security/SecureRandom.java,v
retrieving revision 1.7
diff -u -r1.7 SecureRandom.java
--- libjava/java/security/SecureRandom.java 13 Dec 2002 14:21:07 -0000 1.7
+++ libjava/java/security/SecureRandom.java 17 Jan 2003 15:56:29 -0000
@@ -1,5 +1,5 @@
/* SecureRandom.java --- Secure Random class implmentation
- Copyright (C) 1999, 2001, 2002 Free Software Foundation, Inc.
+ Copyright (C) 1999, 2001, 2002, 2003 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -36,19 +36,30 @@
exception statement from your version. */
package java.security;
+
import java.io.Serializable;
import java.util.Random;
import java.util.Enumeration;
/**
- SecureRandom is the class interface for using SecureRandom
- providers. It provides an interface to the SecureRandomSpi
- engine so that programmers can generate pseudo-random numbers.
-
- @author Mark Benvenuto <ivymccough@worldnet.att.net>
+ * An interface to a cryptographically secure pseudo-random number
+ * generator (PRNG). Random (or at least unguessable) numbers are used
+ * in all areas of security and cryptography, from the generation of
+ * keys and initialization vectors to the generation of random padding
+ * bytes.
+ *
+ * @author Mark Benvenuto <ivymccough@worldnet.att.net>
+ * @author Casey Marshall
*/
public class SecureRandom extends Random
{
+
+ // Constants and fields.
+ // ------------------------------------------------------------------------
+
+ /** Service name for PRNGs. */
+ private static final String SECURE_RANDOM = "SecureRandom";
+
static final long serialVersionUID = 4940670005562187L;
//Serialized Field
@@ -59,6 +70,9 @@
SecureRandomSpi secureRandomSpi = null;
byte[] state = null;
+ // Constructors.
+ // ------------------------------------------------------------------------
+
/**
Default constructor for SecureRandom. It constructs a
new SecureRandom by instantating the first SecureRandom
@@ -68,7 +82,7 @@
on the first call to getnextBytes it will force a seed.
It is maintained for backwards compatibility and programs
- should use getInstance.
+ should use {@link #getInstance(java.lang.String)}.
*/
public SecureRandom()
{
@@ -86,21 +100,17 @@
while (e.hasMoreElements())
{
key = (String) e.nextElement();
- if (key.startsWith("SECURERANDOM."))
- {
- if ((classname = p[i].getProperty(key)) != null)
- {
- try
- {
- secureRandomSpi = (SecureRandomSpi) Class.
- forName(classname).newInstance();
- provider = p[i];
- return;
- }
- catch (Throwable ignore) { }
- }
- }
- }
+ if (key.startsWith("SECURERANDOM.")) {
+ if ((classname = p[i].getProperty(key)) != null) {
+ try {
+ secureRandomSpi = (SecureRandomSpi) Class.
+ forName(classname).newInstance();
+ provider = p[i];
+ return;
+ } catch (Throwable ignore) { }
+ }
+ }
+ }
}
// Nothing found. Fall back to SHA1PRNG
@@ -140,15 +150,17 @@
this.provider = provider;
}
- /**
- Returns an instance of a SecureRandom. It creates the class
- for the specified algorithm if it exists from a provider.
-
- @param algorithm A SecureRandom algorithm to use
+ // Class methods.
+ // ------------------------------------------------------------------------
- @return Returns a new SecureRandom implmenting the chosen algorithm
-
- @throws NoSuchAlgorithmException if the algorithm cannot be found
+ /**
+ * Returns an instance of a SecureRandom. It creates the class from
+ * the first provider that implements it.
+ *
+ * @param algorithm The algorithm name.
+ * @return A new SecureRandom implmenting the given algorithm.
+ * @throws NoSuchAlgorithmException If no installed provider implements
+ * the given algorithm.
*/
public static SecureRandom getInstance(String algorithm) throws
NoSuchAlgorithmException
@@ -156,11 +168,9 @@
Provider p[] = Security.getProviders();
for (int i = 0; i < p.length; i++)
{
- try
- {
- return getInstance(algorithm, p[i]);
- }
- catch (NoSuchAlgorithmException ignored) { }
+ try {
+ return getInstance(algorithm, p[i]);
+ } catch (NoSuchAlgorithmException ignored) { }
}
// None found.
@@ -168,21 +178,26 @@
}
/**
- Returns an instance of a SecureRandom. It creates the class
- for the specified algorithm from the specified provider.
-
- @param algorithm A SecureRandom algorithm to use
- @param provider A security provider to use
-
- @return Returns a new SecureRandom implmenting the chosen algorithm
-
- @throws NoSuchAlgorithmException if the algorithm cannot be found
- @throws NoSuchProviderException if the provider cannot be found
+ * Returns an instance of a SecureRandom. It creates the class
+ * for the specified algorithm from the named provider.
+ *
+ * @param algorithm The algorithm name.
+ * @param provider The provider name.
+ * @return A new SecureRandom implmenting the chosen algorithm.
+ * @throws NoSuchAlgorithmException If the named provider does not implement
+ * the algorithm, or if the implementation cannot be
+ * instantiated.
+ * @throws NoSuchProviderException If no provider named
+ * <code>provider</code> is currently installed.
+ * @throws IllegalArgumentException If <code>provider</code> is null
+ * or is empty.
*/
- public static SecureRandom getInstance(String algorithm,
- String provider) throws
- NoSuchAlgorithmException, NoSuchProviderException
+ public static SecureRandom getInstance(String algorithm, String provider)
+ throws NoSuchAlgorithmException, NoSuchProviderException
{
+ if (provider == null || provider.length() == 0)
+ throw new IllegalArgumentException("Illegal provider");
+
Provider p = Security.getProvider(provider);
if (p == null)
throw new NoSuchProviderException();
@@ -191,87 +206,31 @@
}
/**
- Returns an instance of a SecureRandom. It creates the class for
- the specified algorithm from the given provider.
-
- @param algorithm The SecureRandom algorithm to create.
- @param provider The provider to get the instance from.
-
- @throws NoSuchAlgorithmException If the algorithm cannot be found, or
- if the class cannot be instantiated.
- */
- public static SecureRandom getInstance(String algorithm,
- Provider provider) throws
- NoSuchAlgorithmException
- {
- return getInstance(algorithm, provider, true);
+ * Returns an instance of a SecureRandom. It creates the class for
+ * the specified algorithm from the given provider.
+ *
+ * @param algorithm The SecureRandom algorithm to create.
+ * @param provider The provider to get the instance from.
+ * @throws NoSuchAlgorithmException If the algorithm cannot be found, or
+ * if the class cannot be instantiated.
+ * @throws IllegalArgumentException If <code>provider</code> is null.
+ */
+ public static SecureRandom getInstance(String algorithm, Provider provider)
+ throws NoSuchAlgorithmException
+ {
+ if (provider == null)
+ throw new IllegalArgumentException("Illegal provider");
+ try {
+ return new SecureRandom((SecureRandomSpi)
+ Engine.getInstance(SECURE_RANDOM, algorithm, provider),
+ provider);
+ } catch (ClassCastException cce) {
+ throw new NoSuchAlgorithmException(algorithm);
+ }
}
- /**
- Creates the instance of SecureRandom, recursing to resolve aliases.
-
- @param algorithm The SecureRandom algorithm to create.
- @param provider The provider to get the implementation from.
- @param recurse Whether or not to recurse to resolve aliases.
-
- @throws NoSuchAlgorithmException If the algorithm cannot be found,
- if there are too many aliases, or if the class cannot be
- instantiated.
- */
- private static SecureRandom getInstance(String algorithm,
- Provider provider,
- boolean recurse)
- throws NoSuchAlgorithmException
- {
- String msg = algorithm;
- for (Enumeration e = provider.propertyNames(); e.hasMoreElements(); )
- {
- // We could replace the boolean with an integer, incrementing it
- // every
- String key = (String) e.nextElement();
- if (key.startsWith("SECURERANDOM.")
- && key.substring(13).equalsIgnoreCase(algorithm))
- {
- try
- {
- Class c = Class.forName(provider.getProperty(key));
- return new SecureRandom((SecureRandomSpi) c.newInstance(),
- provider);
- }
- catch (Throwable ignored) { }
- }
- else if (key.startsWith("ALG.ALIAS.SECURERANDOM.")
- && key.substring(23).equalsIgnoreCase(algorithm) && recurse)
- {
- try
- {
- // First see if this alias refers to a class in this
- // provider.
- return getInstance(provider.getProperty(key), provider, false);
- }
- catch (NoSuchAlgorithmException nsae)
- {
- Provider[] provs = Security.getProviders();
- for (int i = 0; i < provs.length; i++)
- {
- if (provs[i] == provider)
- continue;
- // Now try other providers for the implementation
- try
- {
- return getInstance(provider.getProperty(key),
- provs[i], false);
- }
- catch (NoSuchAlgorithmException nsae2)
- {
- msg = nsae2.getMessage();
- }
- }
- }
- }
- }
- throw new NoSuchAlgorithmException(algorithm);
- }
+ // Instance methods.
+ // ------------------------------------------------------------------------
/**
Returns the provider being used by the current SecureRandom class.
@@ -317,8 +276,8 @@
(byte) (0xff & (seed >> 16)),
(byte) (0xff & (seed >> 8)),
(byte) (0xff & seed)
- };
- secureRandomSpi.engineSetSeed(tmp);
+ };
+ secureRandomSpi.engineSetSeed(tmp);
}
}
@@ -358,9 +317,13 @@
int ret = 0;
for (int i = 0; i < tmp.length; i++)
- ret |= tmp[i] << (8 * i);
+ ret |= tmp[i] & 0xFF << (8 * i);
+
+ int mask = 0;
+ for (int i = 0; i < numBits; i++)
+ mask |= 1 << i;
- return ret;
+ return ret & mask;
}
/**
Index: libjava/java/security/Signature.java
===================================================================
RCS file: /cvsroot/gcc/gcc/libjava/java/security/Signature.java,v
retrieving revision 1.4
diff -u -r1.4 Signature.java
--- libjava/java/security/Signature.java 17 Nov 2002 00:10:24 -0000 1.4
+++ libjava/java/security/Signature.java 17 Jan 2003 15:56:29 -0000
@@ -1,5 +1,5 @@
/* Signature.java --- Signature Class
- Copyright (C) 1999, 2002 Free Software Foundation, Inc.
+ Copyright (C) 1999, 2002, 2003 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -36,43 +36,61 @@
exception statement from your version. */
package java.security;
+
import java.security.spec.AlgorithmParameterSpec;
/**
- Signature is used to provide an interface to digital signature
- algorithms. Digital signatures provide authentication and data
- integrity of digital data.
-
- The GNU provider provides the NIST standard DSA which uses DSA
+ <p>Signature is an interface to digital signature algorithms. Digital
+ signatures provide authentication and data integrity of digital data.
+
+ <p>The GNU provider provides the NIST standard DSA which uses DSA
and SHA-1. It can be specified by SHA/DSA, SHA-1/DSA or its
OID. If the RSA signature algorithm is provided then
it could be MD2/RSA. MD5/RSA, or SHA-1/RSA. The algorithm must
- be specified because there is no default.
+ be specified because there is no default.</p>
- Signature provides implementation-independent algorithms which
+ <p>Signature provides implementation-independent algorithms which
are requested by the user through getInstance. It can be
requested by specifying just the algorithm name or by
- specifying both the algorithm name and provider name.
-
- The three phases of using Signature are:
-
- 1. Initialing
-
- * It must be initialized with a private key for signing.
- * It must be initialized with a public key for verifying.
-
- 2. Updating
-
- Update the bytes for signing or verifying with calls to update.
-
- 3. Signing or Verify the signature on the currently stored
- bytes by calling sign or verify.
+ specifying both the algorithm name and provider name.
+
+ <p>The three phases of using Signature are:</p>
+
+ <ol>
+ <li><b>Initialising.</b> The instance is initialized with either one of:
+
+ <ul>
+ <li>{@link #initSign(java.security.PrivateKey,java.security.SecureRandom)}
+ with a private key for signing.</li>
+ <li>{@link #initVerify(java.security.PublicKey)} with a public key or a
+ certificate for verifying.</li>
+ </ul></li>
+
+ <li><b>Updating.</b>
+
+ <p>Zero or more bytes may be added to the data being signed or verified,
+ depending on how this instance was initialized.</p></li>
+
+ <li><b>Signing or Verifying.</b>
+
+ <p>The signature is then either produced over the data input with any of
+ the {@link #sign()} methods, or a signatre is tested for validity with
+ {@link #verify(byte[])}.</p></li>
+ </ol>
@author Mark Benvenuto <ivymccough@worldnet.att.net>
+ @author Casey Marshall
@since JDK 1.1
*/
public abstract class Signature extends SignatureSpi
{
+
+ // Constants and fields.
+ // ------------------------------------------------------------------------
+
+ /** Service name for signatures. */
+ private static final String SIGNATURE = "Signature";
+
/**
Possible state variable which signifies if it has not been
initialized.
@@ -99,6 +117,9 @@
private String algorithm;
Provider provider;
+ // Constructor.
+ // ------------------------------------------------------------------------
+
/**
Creates a new signature for this algorithm.
@@ -110,52 +131,56 @@
state = UNINITIALIZED;
}
- /**
- Gets an instance of the Signature class representing
- the specified signature. If the algorithm is not found then,
- it throws NoSuchAlgorithmException.
-
- @param algorithm the name of signature algorithm to choose
- @return a Signature repesenting the desired algorithm
+ // Class methods.
+ // ------------------------------------------------------------------------
- @throws NoSuchAlgorithmException if the algorithm is not implemented by
- providers
+ /**
+ * Gets an instance of the Signature class representing
+ * the specified signature. If the algorithm is not found then,
+ * it throws NoSuchAlgorithmException.
+ *
+ * @param algorithm the name of signature algorithm to choose
+ * @return a Signature repesenting the desired algorithm
+ * @throws NoSuchAlgorithmException if the algorithm is not implemented by
+ * any currently installed provider.
*/
public static Signature getInstance(String algorithm)
throws NoSuchAlgorithmException
{
Provider[] p = Security.getProviders();
- for (int i = 0; i < p.length; i++)
- {
- try
- {
- return getInstance(algorithm, p[i]);
- }
- catch (NoSuchAlgorithmException ignored) {}
- }
+ for (int i = 0; i < p.length; i++) {
+ try {
+ return getInstance(algorithm, p[i]);
+ } catch (NoSuchAlgorithmException ignored) { }
+ }
throw new NoSuchAlgorithmException(algorithm);
}
/**
- Gets an instance of the Signature class representing
- the specified signature from the specified provider. If the
- algorithm is not found then, it throws NoSuchAlgorithmException.
- If the provider is not found, then it throws
- NoSuchProviderException.
-
- @param algorithm the name of signature algorithm to choose
- @param provider the name of the provider to find the algorithm in
- @return a Signature repesenting the desired algorithm
-
- @throws NoSuchAlgorithmException if the algorithm is not implemented by
- the provider
- @throws NoSuchProviderException if the provider is not found
+ * Gets an instance of the Signature class representing
+ * the specified signature from the specified provider. If the
+ * algorithm is not found then, it throws NoSuchAlgorithmException.
+ * If the provider is not found, then it throws
+ * NoSuchProviderException.
+ *
+ * @param algorithm the name of signature algorithm to choose
+ * @param provider the name of the provider to find the algorithm in
+ * @return a Signature repesenting the desired algorithm
+ *
+ * @throws NoSuchAlgorithmException If the algorithm is not implemented by
+ * the provider or if the implementation cannot be instatiated.
+ * @throws NoSuchProviderException If there is no provider named
+ * <code>provider</code> currently installed.
+ * @throws IllegalArgumentException If <code>provider</code> is null or is
+ * empty.
*/
public static Signature getInstance(String algorithm, String provider)
throws NoSuchAlgorithmException, NoSuchProviderException
{
+ if (provider == null || provider.length() == 0)
+ throw new IllegalArgumentException("Illegal provider");
Provider p = Security.getProvider(provider);
if (p == null)
throw new NoSuchProviderException(provider);
@@ -163,64 +188,40 @@
return getInstance(algorithm, p);
}
- private static Signature getInstance(String algorithm, Provider p)
+ /**
+ * Gets an instance of the given signature algorithm from the
+ * specified provider.
+ *
+ * @param algorithm The name of the signature algorithm to create.
+ * @param provider The provider to get the implementation from.
+ * @return An appropriate instance of Signature.
+ * @throws NoSuchAlgorithmException If the algorithm is not
+ * implemented by the provider, or if the implementation
+ * cannot be instantiated.
+ * @throws IllegalArgumentException If <code>provider</code> is null.
+ * @since 1.4
+ */
+ public static Signature getInstance(String algorithm, Provider provider)
throws NoSuchAlgorithmException
{
- // try the name as is
- String className = p.getProperty("Signature." + algorithm);
- if (className == null) { // try all uppercase
- String upper = algorithm.toUpperCase();
- className = p.getProperty("Signature." + upper);
- if (className == null) { // try if it's an alias
- String alias = p.getProperty("Alg.Alias.Signature." + algorithm);
- if (alias == null) {
- alias = p.getProperty("Alg.Alias.Signature." + upper);
- if (alias == null) { // spit the dummy
- throw new NoSuchAlgorithmException(algorithm);
- }
- }
- className = p.getProperty("Signature." + alias);
- if (className == null) {
- throw new NoSuchAlgorithmException(algorithm);
- }
- }
+ if (provider == null)
+ throw new IllegalArgumentException("Illegal provider");
+ Signature result = null;
+ Object o = Engine.getInstance(SIGNATURE, algorithm, provider);
+ if (o instanceof SignatureSpi) {
+ result = new DummySignature((SignatureSpi) o, algorithm);
+ } else if (o instanceof Signature) {
+ result = (Signature) o;
+ result.algorithm = algorithm;
+ } else {
+ throw new NoSuchAlgorithmException(algorithm);
}
- return getInstance(className, algorithm, p);
+ result.provider = provider;
+ return result;
}
- private static Signature getInstance(String classname,
- String algorithm,
- Provider provider)
- throws NoSuchAlgorithmException
- {
- try
- {
- Object o = Class.forName(classname).newInstance();
- Signature sig;
- if (o instanceof SignatureSpi)
- sig = new DummySignature((SignatureSpi) o, algorithm);
- else
- {
- sig = (Signature) o;
- sig.algorithm = algorithm;
- }
-
- sig.provider = provider;
- return sig;
- }
- catch (ClassNotFoundException cnfe)
- {
- throw new NoSuchAlgorithmException("Class not found");
- }
- catch (InstantiationException ie)
- {
- throw new NoSuchAlgorithmException("Class instantiation failed");
- }
- catch (IllegalAccessException iae)
- {
- throw new NoSuchAlgorithmException("Illegal Access");
- }
- }
+ // Instance methods.
+ // ------------------------------------------------------------------------
/**
Gets the provider that the Signature is from.
@@ -262,13 +263,13 @@
state = VERIFY;
if (certificate.getType().equals("X509"))
{
- java.security.cert.X509Certificate cert =
- (java.security.cert.X509Certificate) certificate;
+ java.security.cert.X509Certificate cert =
+ (java.security.cert.X509Certificate) certificate;
- boolean[]array = cert.getKeyUsage();
- if (array != null && array[0] == false)
- throw new InvalidKeyException
- ("KeyUsage of this Certificate indicates it cannot be used for digital signing");
+ boolean[] array = cert.getKeyUsage();
+ if (array != null && array[0] == false)
+ throw new InvalidKeyException
+ ("KeyUsage of this Certificate indicates it cannot be used for digital signing");
}
this.initVerify(certificate.getPublicKey());
}
@@ -319,8 +320,8 @@
{
if (state == SIGN)
{
- state = UNINITIALIZED;
- return engineSign();
+ state = UNINITIALIZED;
+ return engineSign();
}
else
throw new SignatureException();
@@ -355,8 +356,8 @@
{
if (state == SIGN)
{
- state = UNINITIALIZED;
- return engineSign(outbuf, offset, len);
+ state = UNINITIALIZED;
+ return engineSign(outbuf, offset, len);
}
else
throw new SignatureException();
@@ -376,8 +377,8 @@
{
if (state == VERIFY)
{
- state = UNINITIALIZED;
- return engineVerify(signature);
+ state = UNINITIALIZED;
+ return engineVerify(signature);
}
else
throw new SignatureException();
Attachment:
msg00116/pgp00000.pgp
Description: PGP signature
| Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
|---|---|---|
| Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |