0N/A/*
3909N/A * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.security;
0N/A
0N/Aimport java.io.*;
0N/Aimport java.security.spec.AlgorithmParameterSpec;
0N/Aimport java.security.spec.InvalidParameterSpecException;
0N/A
0N/A/**
0N/A * This class is used as an opaque representation of cryptographic parameters.
0N/A *
0N/A * <p>An <code>AlgorithmParameters</code> object for managing the parameters
0N/A * for a particular algorithm can be obtained by
0N/A * calling one of the <code>getInstance</code> factory methods
0N/A * (static methods that return instances of a given class).
0N/A *
0N/A * <p>Once an <code>AlgorithmParameters</code> object is obtained, it must be
0N/A * initialized via a call to <code>init</code>, using an appropriate parameter
0N/A * specification or parameter encoding.
0N/A *
0N/A * <p>A transparent parameter specification is obtained from an
0N/A * <code>AlgorithmParameters</code> object via a call to
0N/A * <code>getParameterSpec</code>, and a byte encoding of the parameters is
0N/A * obtained via a call to <code>getEncoded</code>.
0N/A *
3465N/A * <p> Every implementation of the Java platform is required to support the
3465N/A * following standard <code>AlgorithmParameters</code> algorithms:
3465N/A * <ul>
3465N/A * <li><tt>AES</tt></li>
3465N/A * <li><tt>DES</tt></li>
3465N/A * <li><tt>DESede</tt></li>
3465N/A * <li><tt>DiffieHellman</tt></li>
3465N/A * <li><tt>DSA</tt></li>
3465N/A * </ul>
3465N/A * These algorithms are described in the <a href=
3465N/A * "{@docRoot}/../technotes/guides/security/StandardNames.html#AlgorithmParameters">
3465N/A * AlgorithmParameters section</a> of the
3465N/A * Java Cryptography Architecture Standard Algorithm Name Documentation.
3465N/A * Consult the release documentation for your implementation to see if any
3465N/A * other algorithms are supported.
3465N/A *
0N/A * @author Jan Luehe
0N/A *
0N/A *
0N/A * @see java.security.spec.AlgorithmParameterSpec
0N/A * @see java.security.spec.DSAParameterSpec
0N/A * @see KeyPairGenerator
0N/A *
0N/A * @since 1.2
0N/A */
0N/A
0N/Apublic class AlgorithmParameters {
0N/A
0N/A // The provider
0N/A private Provider provider;
0N/A
0N/A // The provider implementation (delegate)
0N/A private AlgorithmParametersSpi paramSpi;
0N/A
0N/A // The algorithm
0N/A private String algorithm;
0N/A
0N/A // Has this object been initialized?
0N/A private boolean initialized = false;
0N/A
0N/A /**
0N/A * Creates an AlgorithmParameters object.
0N/A *
0N/A * @param paramSpi the delegate
0N/A * @param provider the provider
0N/A * @param algorithm the algorithm
0N/A */
0N/A protected AlgorithmParameters(AlgorithmParametersSpi paramSpi,
0N/A Provider provider, String algorithm)
0N/A {
0N/A this.paramSpi = paramSpi;
0N/A this.provider = provider;
0N/A this.algorithm = algorithm;
0N/A }
0N/A
0N/A /**
0N/A * Returns the name of the algorithm associated with this parameter object.
0N/A *
0N/A * @return the algorithm name.
0N/A */
0N/A public final String getAlgorithm() {
0N/A return this.algorithm;
0N/A }
0N/A
0N/A /**
0N/A * Returns a parameter object for the specified algorithm.
0N/A *
0N/A * <p> This method traverses the list of registered security Providers,
0N/A * starting with the most preferred Provider.
0N/A * A new AlgorithmParameters object encapsulating the
0N/A * AlgorithmParametersSpi implementation from the first
0N/A * Provider that supports the specified algorithm is returned.
0N/A *
0N/A * <p> Note that the list of registered providers may be retrieved via
0N/A * the {@link Security#getProviders() Security.getProviders()} method.
0N/A *
0N/A * <p> The returned parameter object must be initialized via a call to
0N/A * <code>init</code>, using an appropriate parameter specification or
0N/A * parameter encoding.
0N/A *
0N/A * @param algorithm the name of the algorithm requested.
3465N/A * See the AlgorithmParameters section in the <a href=
3465N/A * "{@docRoot}/../technotes/guides/security/StandardNames.html#AlgorithmParameters">
3465N/A * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
0N/A * for information about standard algorithm names.
0N/A *
0N/A * @return the new parameter object.
0N/A *
0N/A * @exception NoSuchAlgorithmException if no Provider supports an
0N/A * AlgorithmParametersSpi implementation for the
0N/A * specified algorithm.
0N/A *
0N/A * @see Provider
0N/A */
0N/A public static AlgorithmParameters getInstance(String algorithm)
0N/A throws NoSuchAlgorithmException {
0N/A try {
0N/A Object[] objs = Security.getImpl(algorithm, "AlgorithmParameters",
0N/A (String)null);
0N/A return new AlgorithmParameters((AlgorithmParametersSpi)objs[0],
0N/A (Provider)objs[1],
0N/A algorithm);
0N/A } catch(NoSuchProviderException e) {
0N/A throw new NoSuchAlgorithmException(algorithm + " not found");
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns a parameter object for the specified algorithm.
0N/A *
0N/A * <p> A new AlgorithmParameters object encapsulating the
0N/A * AlgorithmParametersSpi implementation from the specified provider
0N/A * is returned. The specified provider must be registered
0N/A * in the security provider list.
0N/A *
0N/A * <p> Note that the list of registered providers may be retrieved via
0N/A * the {@link Security#getProviders() Security.getProviders()} method.
0N/A *
0N/A * <p>The returned parameter object must be initialized via a call to
0N/A * <code>init</code>, using an appropriate parameter specification or
0N/A * parameter encoding.
0N/A *
0N/A * @param algorithm the name of the algorithm requested.
3465N/A * See the AlgorithmParameters section in the <a href=
3465N/A * "{@docRoot}/../technotes/guides/security/StandardNames.html#AlgorithmParameters">
3465N/A * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
0N/A * for information about standard algorithm names.
0N/A *
0N/A * @param provider the name of the provider.
0N/A *
0N/A * @return the new parameter object.
0N/A *
0N/A * @exception NoSuchAlgorithmException if an AlgorithmParametersSpi
0N/A * implementation for the specified algorithm is not
0N/A * available from the specified provider.
0N/A *
0N/A * @exception NoSuchProviderException if the specified provider is not
0N/A * registered in the security provider list.
0N/A *
0N/A * @exception IllegalArgumentException if the provider name is null
0N/A * or empty.
0N/A *
0N/A * @see Provider
0N/A */
0N/A public static AlgorithmParameters getInstance(String algorithm,
0N/A String provider)
0N/A throws NoSuchAlgorithmException, NoSuchProviderException
0N/A {
0N/A if (provider == null || provider.length() == 0)
0N/A throw new IllegalArgumentException("missing provider");
0N/A Object[] objs = Security.getImpl(algorithm, "AlgorithmParameters",
0N/A provider);
0N/A return new AlgorithmParameters((AlgorithmParametersSpi)objs[0],
0N/A (Provider)objs[1],
0N/A algorithm);
0N/A }
0N/A
0N/A /**
0N/A * Returns a parameter object for the specified algorithm.
0N/A *
0N/A * <p> A new AlgorithmParameters object encapsulating the
0N/A * AlgorithmParametersSpi implementation from the specified Provider
0N/A * object is returned. Note that the specified Provider object
0N/A * does not have to be registered in the provider list.
0N/A *
0N/A * <p>The returned parameter object must be initialized via a call to
0N/A * <code>init</code>, using an appropriate parameter specification or
0N/A * parameter encoding.
0N/A *
0N/A * @param algorithm the name of the algorithm requested.
3465N/A * See the AlgorithmParameters section in the <a href=
3465N/A * "{@docRoot}/../technotes/guides/security/StandardNames.html#AlgorithmParameters">
3465N/A * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
0N/A * for information about standard algorithm names.
0N/A *
0N/A * @param provider the name of the provider.
0N/A *
0N/A * @return the new parameter object.
0N/A *
0N/A * @exception NoSuchAlgorithmException if an AlgorithmParameterGeneratorSpi
0N/A * implementation for the specified algorithm is not available
0N/A * from the specified Provider object.
0N/A *
0N/A * @exception IllegalArgumentException if the provider is null.
0N/A *
0N/A * @see Provider
0N/A *
0N/A * @since 1.4
0N/A */
0N/A public static AlgorithmParameters getInstance(String algorithm,
0N/A Provider provider)
0N/A throws NoSuchAlgorithmException
0N/A {
0N/A if (provider == null)
0N/A throw new IllegalArgumentException("missing provider");
0N/A Object[] objs = Security.getImpl(algorithm, "AlgorithmParameters",
0N/A provider);
0N/A return new AlgorithmParameters((AlgorithmParametersSpi)objs[0],
0N/A (Provider)objs[1],
0N/A algorithm);
0N/A }
0N/A
0N/A /**
0N/A * Returns the provider of this parameter object.
0N/A *
0N/A * @return the provider of this parameter object
0N/A */
0N/A public final Provider getProvider() {
0N/A return this.provider;
0N/A }
0N/A
0N/A /**
0N/A * Initializes this parameter object using the parameters
0N/A * specified in <code>paramSpec</code>.
0N/A *
0N/A * @param paramSpec the parameter specification.
0N/A *
0N/A * @exception InvalidParameterSpecException if the given parameter
0N/A * specification is inappropriate for the initialization of this parameter
0N/A * object, or if this parameter object has already been initialized.
0N/A */
0N/A public final void init(AlgorithmParameterSpec paramSpec)
0N/A throws InvalidParameterSpecException
0N/A {
0N/A if (this.initialized)
0N/A throw new InvalidParameterSpecException("already initialized");
0N/A paramSpi.engineInit(paramSpec);
0N/A this.initialized = true;
0N/A }
0N/A
0N/A /**
0N/A * Imports the specified parameters and decodes them according to the
0N/A * primary decoding format for parameters. The primary decoding
0N/A * format for parameters is ASN.1, if an ASN.1 specification for this type
0N/A * of parameters exists.
0N/A *
0N/A * @param params the encoded parameters.
0N/A *
0N/A * @exception IOException on decoding errors, or if this parameter object
0N/A * has already been initialized.
0N/A */
0N/A public final void init(byte[] params) throws IOException {
0N/A if (this.initialized)
0N/A throw new IOException("already initialized");
0N/A paramSpi.engineInit(params);
0N/A this.initialized = true;
0N/A }
0N/A
0N/A /**
0N/A * Imports the parameters from <code>params</code> and decodes them
0N/A * according to the specified decoding scheme.
0N/A * If <code>format</code> is null, the
0N/A * primary decoding format for parameters is used. The primary decoding
0N/A * format is ASN.1, if an ASN.1 specification for these parameters
0N/A * exists.
0N/A *
0N/A * @param params the encoded parameters.
0N/A *
0N/A * @param format the name of the decoding scheme.
0N/A *
0N/A * @exception IOException on decoding errors, or if this parameter object
0N/A * has already been initialized.
0N/A */
0N/A public final void init(byte[] params, String format) throws IOException {
0N/A if (this.initialized)
0N/A throw new IOException("already initialized");
0N/A paramSpi.engineInit(params, format);
0N/A this.initialized = true;
0N/A }
0N/A
0N/A /**
0N/A * Returns a (transparent) specification of this parameter object.
0N/A * <code>paramSpec</code> identifies the specification class in which
0N/A * the parameters should be returned. It could, for example, be
0N/A * <code>DSAParameterSpec.class</code>, to indicate that the
0N/A * parameters should be returned in an instance of the
0N/A * <code>DSAParameterSpec</code> class.
0N/A *
0N/A * @param paramSpec the specification class in which
0N/A * the parameters should be returned.
0N/A *
0N/A * @return the parameter specification.
0N/A *
0N/A * @exception InvalidParameterSpecException if the requested parameter
0N/A * specification is inappropriate for this parameter object, or if this
0N/A * parameter object has not been initialized.
0N/A */
0N/A public final <T extends AlgorithmParameterSpec>
0N/A T getParameterSpec(Class<T> paramSpec)
0N/A throws InvalidParameterSpecException
0N/A {
0N/A if (this.initialized == false) {
0N/A throw new InvalidParameterSpecException("not initialized");
0N/A }
0N/A return paramSpi.engineGetParameterSpec(paramSpec);
0N/A }
0N/A
0N/A /**
0N/A * Returns the parameters in their primary encoding format.
0N/A * The primary encoding format for parameters is ASN.1, if an ASN.1
0N/A * specification for this type of parameters exists.
0N/A *
0N/A * @return the parameters encoded using their primary encoding format.
0N/A *
0N/A * @exception IOException on encoding errors, or if this parameter object
0N/A * has not been initialized.
0N/A */
0N/A public final byte[] getEncoded() throws IOException
0N/A {
0N/A if (this.initialized == false) {
0N/A throw new IOException("not initialized");
0N/A }
0N/A return paramSpi.engineGetEncoded();
0N/A }
0N/A
0N/A /**
0N/A * Returns the parameters encoded in the specified scheme.
0N/A * If <code>format</code> is null, the
0N/A * primary encoding format for parameters is used. The primary encoding
0N/A * format is ASN.1, if an ASN.1 specification for these parameters
0N/A * exists.
0N/A *
0N/A * @param format the name of the encoding format.
0N/A *
0N/A * @return the parameters encoded using the specified encoding scheme.
0N/A *
0N/A * @exception IOException on encoding errors, or if this parameter object
0N/A * has not been initialized.
0N/A */
0N/A public final byte[] getEncoded(String format) throws IOException
0N/A {
0N/A if (this.initialized == false) {
0N/A throw new IOException("not initialized");
0N/A }
0N/A return paramSpi.engineGetEncoded(format);
0N/A }
0N/A
0N/A /**
0N/A * Returns a formatted string describing the parameters.
0N/A *
0N/A * @return a formatted string describing the parameters, or null if this
0N/A * parameter object has not been initialized.
0N/A */
0N/A public final String toString() {
0N/A if (this.initialized == false) {
0N/A return null;
0N/A }
0N/A return paramSpi.engineToString();
0N/A }
0N/A}