diff options
author | Tom Tromey <tromey@redhat.com> | 2003-04-19 20:54:55 +0000 |
---|---|---|
committer | Tom Tromey <tromey@gcc.gnu.org> | 2003-04-19 20:54:55 +0000 |
commit | 7451c1559ef877317965306543fd792944044b2c (patch) | |
tree | 847219286ef699ba044b108442bad4ce9ee618f5 /libjava/java/security/KeyFactory.java | |
parent | 9e9e204234b883d783914aa909e96c57d45ac081 (diff) | |
download | gcc-7451c1559ef877317965306543fd792944044b2c.zip gcc-7451c1559ef877317965306543fd792944044b2c.tar.gz gcc-7451c1559ef877317965306543fd792944044b2c.tar.bz2 |
Makefile.in: Rebuilt.
* Makefile.in: Rebuilt.
* Makefile.am (ordinary_java_source_files): Added new files.
* java/security/AlgorithmParameterGenerator.java,
java/security/AlgorithmParameters.java, java/security/Engine.java,
java/security/Identity.java, java/security/IdentityScope.java,
java/security/KeyFactory.java,
java/security/KeyPairGenerator.java, java/security/KeyStore.java,
java/security/MessageDigest.java, java/security/Policy.java,
java/security/ProtectionDomain.java,
java/security/SecureRandom.java, java/security/Security.java,
java/security/Signature.java, java/security/SignatureSpi.java,
java/security/SignedObject.java, java/security/Signer.java,
java/security/interfaces/RSAMultiPrimePrivateCrtKey.java,
java/security/spec/PSSParameterSpec.java,
java/security/spec/RSAMultiPrimePrivateCrtKeySpec.java,
java/security/spec/RSAOtherPrimeInfo.java: New versions from
Classpath.
From-SVN: r65829
Diffstat (limited to 'libjava/java/security/KeyFactory.java')
-rw-r--r-- | libjava/java/security/KeyFactory.java | 273 |
1 files changed, 159 insertions, 114 deletions
diff --git a/libjava/java/security/KeyFactory.java b/libjava/java/security/KeyFactory.java index 667c2fb..918bf3d 100644 --- a/libjava/java/security/KeyFactory.java +++ b/libjava/java/security/KeyFactory.java @@ -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,37 +36,67 @@ 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.KeySpec; import java.security.spec.InvalidKeySpecException; +import java.security.NoSuchAlgorithmException; /** - 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 + * <p>Key factories are used to convert keys (opaque cryptographic keys of type + * {@link Key}) into key specifications (transparent representations of the + * underlying key material), and vice versa.</p> + * + * <p>Key factories are bi-directional. That is, they allow you to build an + * opaque key object from a given key specification (key material), or to + * retrieve the underlying key material of a key object in a suitable format.</p> + * + * <p>Multiple compatible key specifications may exist for the same key. For + * example, a <i>DSA</i> public key may be specified using {@link + * java.security.spec.DSAPublicKeySpec} or {@link + * java.security.spec.X509EncodedKeySpec}. A key factory can be used to + * translate between compatible key specifications.</p> + * + * <p>The following is an example of how to use a key factory in order to + * instantiate a <i>DSA</i> public key from its encoding. Assume Alice has + * received a digital signature from Bob. Bob also sent her his public key (in + * encoded format) to verify his signature. Alice then performs the following + * actions: + * + * <pre> + * X509EncodedKeySpec bobPubKeySpec = new X509EncodedKeySpec(bobEncodedPubKey); + * KeyFactory keyFactory = KeyFactory.getInstance("DSA"); + * PublicKey bobPubKey = keyFactory.generatePublic(bobPubKeySpec); + * Signature sig = Signature.getInstance("DSA"); + * sig.initVerify(bobPubKey); + * sig.update(data); + * sig.verify(signature); + * </pre> + * + * @since 1.2 + * @see Key + * @see PublicKey + * @see PrivateKey + * @see KeySpec + * @see java.security.spec.DSAPublicKeySpec + * @see java.security.spec.X509EncodedKeySpec @author Mark Benvenuto */ public class KeyFactory { + /** The service name for key factories. */ + private static final String KEY_FACTORY = "KeyFactory"; + private KeyFactorySpi keyFacSpi; private Provider provider; private String algorithm; /** - Constructs a new keyFactory with the specified parameters. - - @param keyFacSpi Key Factory SPI to use - @param provider the provider of the Key Factory SPI - @param algorithm the name of the key algorithm for this key factory + * Creates a <code>KeyFactory</code> object. + * + * @param keyFacSpi the delegate. + * @param provider the provider. + * @param algorithm the name of the algorithm to associate with this + * <code>KeyFactory</code>. */ protected KeyFactory(KeyFactorySpi keyFacSpi, Provider provider, String algorithm) @@ -76,85 +106,102 @@ public class KeyFactory 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. - - @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 + /** + * Generates a <code>KeyFactory</code> object that implements the specified + * algorithm. If the default provider package provides an implementation of + * the requested algorithm, an instance of <code>KeyFactory</code> containing + * that implementation is returned. If the algorithm is not available in the + * default package, other packages are searched. + * + * @param algorithm the name of the requested key algorithm. See Appendix A + * in the Java Cryptography Architecture API Specification & Reference + * for information about standard algorithm names. + * @return a <code>KeyFactory</code> object for the specified algorithm. + * @throws NoSuchAlgorithmException if the requested algorithm is not + * available in the default provider package or any of the other provider + * packages that were searched. */ public static KeyFactory getInstance(String algorithm) throws NoSuchAlgorithmException { Provider[] p = Security.getProviders(); - 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 ignored) {} 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 + /** + * Generates a <code>KeyFactory</code> object for the specified algorithm + * from the specified provider. + * + * @param algorithm the name of the requested key algorithm. See Appendix A + * in the Java Cryptography Architecture API Specification & Reference + * for information about standard algorithm names. + * @param provider the name of the provider. + * @return a <code>KeyFactory</code> object for the specified algorithm. + * @throws NoSuchAlgorithmException if the algorithm is not available from + * the specified provider. + * @throws NoSuchProviderException if the provider has not been configured. + * @throws IllegalArgumentException if the provider name is null or empty. + * @see Provider */ 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) + /** + * Generates a <code>KeyFactory</code> object for the specified algorithm from + * the specified provider. Note: the <code>provider</code> doesn't have to be + * registered. + * + * @param algorithm the name of the requested key algorithm. See Appendix A + * in the Java Cryptography Architecture API Specification & Reference for + * information about standard algorithm names. + * @param provider the provider. + * @return a <code>KeyFactory</code> object for the specified algorithm. + * @throws NoSuchAlgorithmException if the algorithm is not available from + * the specified provider. + * @throws IllegalArgumentException if the <code>provider</code> is + * <code>null</code>. + * @since 1.4 + * @see Provider + */ + public static KeyFactory getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException { + if (provider == null) + throw new IllegalArgumentException("Illegal provider"); try { - return new KeyFactory((KeyFactorySpi) Class.forName(classname). - newInstance(), provider, algorithm); - } - catch (ClassNotFoundException cnfe) - { - throw new NoSuchAlgorithmException("Class not found"); + return new KeyFactory((KeyFactorySpi) + Engine.getInstance(KEY_FACTORY, algorithm, provider), + provider, algorithm); } - catch (InstantiationException ie) + catch (ClassCastException cce) { - throw new NoSuchAlgorithmException("Class instantiation failed"); - } - catch (IllegalAccessException iae) - { - throw new NoSuchAlgorithmException("Illegal Access"); - } + throw new NoSuchAlgorithmException(algorithm); + } } /** - Gets the provider that the class is from. - - @return the provider of this class + * Returns the provider of this key factory object. + * + * @return the provider of this key factory object. */ public final Provider getProvider() { @@ -162,9 +209,10 @@ public class KeyFactory } /** - Returns the name of the algorithm used - - @return A string with the name of the algorithm + * Gets the name of the algorithm associated with this <code>KeyFactory</code>. + * + * @return the name of the algorithm associated with this + * <code>KeyFactory</code>. */ public final String getAlgorithm() { @@ -172,52 +220,51 @@ public class KeyFactory } /** - Generates a public key from the provided key specification. - - @param keySpec key specification - - @return the public key - - @throws InvalidKeySpecException invalid key specification for - this key factory to produce a public key + * Generates a public key object from the provided key specification (key + * material). + * + * @param keySpec the specification (key material) of the public key. + * @return the public key. + * @throws InvalidKeySpecException if the given key specification is + * inappropriate for this key factory to produce a public key. */ - public final PublicKey generatePublic(KeySpec keySpec) throws - InvalidKeySpecException + public final PublicKey generatePublic(KeySpec keySpec) + throws InvalidKeySpecException { return keyFacSpi.engineGeneratePublic(keySpec); } /** - Generates a private key from the provided key specification. - - @param keySpec key specification - - @return the private key - - @throws InvalidKeySpecException invalid key specification for - this key factory to produce a private key + * Generates a private key object from the provided key specification (key + * material). + * + * @param keySpec the specification (key material) of the private key. + * @return the private key. + * @throws InvalidKeySpecException if the given key specification is + * inappropriate for this key factory to produce a private key. */ - public final PrivateKey generatePrivate(KeySpec keySpec) throws - InvalidKeySpecException + public final PrivateKey generatePrivate(KeySpec keySpec) + throws InvalidKeySpecException { return keyFacSpi.engineGeneratePrivate(keySpec); } /** - Returns a key specification for the given key. keySpec - identifies the specification class to return the key - material in. - - @param key the key - @param keySpec the specification class to return the - key material in. - - @return the key specification in an instance of the requested - specification class - - @throws InvalidKeySpecException the requested key specification - is inappropriate for this key or the key is - unrecognized. + * Returns a specification (key material) of the given key object. + * <code>keySpec</code> identifies the specification class in which the key + * material should be returned. It could, for example, be + * <code>DSAPublicKeySpec.class</code>, to indicate that the key material + * should be returned in an instance of the {@link + * java.security.spec.DSAPublicKeySpec} class. + * + * @param key the key. + * @param keySpec the specification class in which the key material should be + * returned. + * @return the underlying key specification (key material) in an instance of + * the requested specification class. + * @throws InvalidKeySpecException if the requested key specification is + * inappropriate for the given key, or the given key cannot be processed + * (e.g., the given key has an unrecognized algorithm or format). */ public final KeySpec getKeySpec(Key key, Class keySpec) throws InvalidKeySpecException @@ -226,15 +273,13 @@ public class KeyFactory } /** - Translates the key from an unknown or untrusted provider - into a key for this key factory. - - @param the key from an unknown or untrusted provider - - @return the translated key - - @throws InvalidKeySpecException if the key cannot be - processed by this key factory + * Translates a key object, whose provider may be unknown or potentially + * untrusted, into a corresponding key object of this key factory. + * + * @param key the key whose provider is unknown or untrusted. + * @return the translated key. + * @throws InvalidKeyException if the given key cannot be processed by this + * key factory. */ public final Key translateKey(Key key) throws InvalidKeyException { |