Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/security/AlgorithmParameters.java
38829 views
/*1* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.security;2627import java.io.*;28import java.security.spec.AlgorithmParameterSpec;29import java.security.spec.InvalidParameterSpecException;3031/**32* This class is used as an opaque representation of cryptographic parameters.33*34* <p>An {@code AlgorithmParameters} object for managing the parameters35* for a particular algorithm can be obtained by36* calling one of the {@code getInstance} factory methods37* (static methods that return instances of a given class).38*39* <p>Once an {@code AlgorithmParameters} object is obtained, it must be40* initialized via a call to {@code init}, using an appropriate parameter41* specification or parameter encoding.42*43* <p>A transparent parameter specification is obtained from an44* {@code AlgorithmParameters} object via a call to45* {@code getParameterSpec}, and a byte encoding of the parameters is46* obtained via a call to {@code getEncoded}.47*48* <p> Every implementation of the Java platform is required to support the49* following standard {@code AlgorithmParameters} algorithms:50* <ul>51* <li>{@code AES}</li>52* <li>{@code DES}</li>53* <li>{@code DESede}</li>54* <li>{@code DiffieHellman}</li>55* <li>{@code DSA}</li>56* </ul>57* These algorithms are described in the <a href=58* "{@docRoot}/../technotes/guides/security/StandardNames.html#AlgorithmParameters">59* AlgorithmParameters section</a> of the60* Java Cryptography Architecture Standard Algorithm Name Documentation.61* Consult the release documentation for your implementation to see if any62* other algorithms are supported.63*64* @author Jan Luehe65*66*67* @see java.security.spec.AlgorithmParameterSpec68* @see java.security.spec.DSAParameterSpec69* @see KeyPairGenerator70*71* @since 1.272*/7374public class AlgorithmParameters {7576// The provider77private Provider provider;7879// The provider implementation (delegate)80private AlgorithmParametersSpi paramSpi;8182// The algorithm83private String algorithm;8485// Has this object been initialized?86private boolean initialized = false;8788/**89* Creates an AlgorithmParameters object.90*91* @param paramSpi the delegate92* @param provider the provider93* @param algorithm the algorithm94*/95protected AlgorithmParameters(AlgorithmParametersSpi paramSpi,96Provider provider, String algorithm)97{98this.paramSpi = paramSpi;99this.provider = provider;100this.algorithm = algorithm;101}102103/**104* Returns the name of the algorithm associated with this parameter object.105*106* @return the algorithm name.107*/108public final String getAlgorithm() {109return this.algorithm;110}111112/**113* Returns a parameter object for the specified algorithm.114*115* <p> This method traverses the list of registered security Providers,116* starting with the most preferred Provider.117* A new AlgorithmParameters object encapsulating the118* AlgorithmParametersSpi implementation from the first119* Provider that supports the specified algorithm is returned.120*121* <p> Note that the list of registered providers may be retrieved via122* the {@link Security#getProviders() Security.getProviders()} method.123*124* <p> The returned parameter object must be initialized via a call to125* {@code init}, using an appropriate parameter specification or126* parameter encoding.127*128* @param algorithm the name of the algorithm requested.129* See the AlgorithmParameters section in the <a href=130* "{@docRoot}/../technotes/guides/security/StandardNames.html#AlgorithmParameters">131* Java Cryptography Architecture Standard Algorithm Name Documentation</a>132* for information about standard algorithm names.133*134* @return the new parameter object.135*136* @exception NoSuchAlgorithmException if no Provider supports an137* AlgorithmParametersSpi implementation for the138* specified algorithm.139*140* @see Provider141*/142public static AlgorithmParameters getInstance(String algorithm)143throws NoSuchAlgorithmException {144try {145Object[] objs = Security.getImpl(algorithm, "AlgorithmParameters",146(String)null);147return new AlgorithmParameters((AlgorithmParametersSpi)objs[0],148(Provider)objs[1],149algorithm);150} catch(NoSuchProviderException e) {151throw new NoSuchAlgorithmException(algorithm + " not found");152}153}154155/**156* Returns a parameter object for the specified algorithm.157*158* <p> A new AlgorithmParameters object encapsulating the159* AlgorithmParametersSpi implementation from the specified provider160* is returned. The specified provider must be registered161* in the security provider list.162*163* <p> Note that the list of registered providers may be retrieved via164* the {@link Security#getProviders() Security.getProviders()} method.165*166* <p>The returned parameter object must be initialized via a call to167* {@code init}, using an appropriate parameter specification or168* parameter encoding.169*170* @param algorithm the name of the algorithm requested.171* See the AlgorithmParameters section in the <a href=172* "{@docRoot}/../technotes/guides/security/StandardNames.html#AlgorithmParameters">173* Java Cryptography Architecture Standard Algorithm Name Documentation</a>174* for information about standard algorithm names.175*176* @param provider the name of the provider.177*178* @return the new parameter object.179*180* @exception NoSuchAlgorithmException if an AlgorithmParametersSpi181* implementation for the specified algorithm is not182* available from the specified provider.183*184* @exception NoSuchProviderException if the specified provider is not185* registered in the security provider list.186*187* @exception IllegalArgumentException if the provider name is null188* or empty.189*190* @see Provider191*/192public static AlgorithmParameters getInstance(String algorithm,193String provider)194throws NoSuchAlgorithmException, NoSuchProviderException195{196if (provider == null || provider.length() == 0)197throw new IllegalArgumentException("missing provider");198Object[] objs = Security.getImpl(algorithm, "AlgorithmParameters",199provider);200return new AlgorithmParameters((AlgorithmParametersSpi)objs[0],201(Provider)objs[1],202algorithm);203}204205/**206* Returns a parameter object for the specified algorithm.207*208* <p> A new AlgorithmParameters object encapsulating the209* AlgorithmParametersSpi implementation from the specified Provider210* object is returned. Note that the specified Provider object211* does not have to be registered in the provider list.212*213* <p>The returned parameter object must be initialized via a call to214* {@code init}, using an appropriate parameter specification or215* parameter encoding.216*217* @param algorithm the name of the algorithm requested.218* See the AlgorithmParameters section in the <a href=219* "{@docRoot}/../technotes/guides/security/StandardNames.html#AlgorithmParameters">220* Java Cryptography Architecture Standard Algorithm Name Documentation</a>221* for information about standard algorithm names.222*223* @param provider the name of the provider.224*225* @return the new parameter object.226*227* @exception NoSuchAlgorithmException if an AlgorithmParameterGeneratorSpi228* implementation for the specified algorithm is not available229* from the specified Provider object.230*231* @exception IllegalArgumentException if the provider is null.232*233* @see Provider234*235* @since 1.4236*/237public static AlgorithmParameters getInstance(String algorithm,238Provider provider)239throws NoSuchAlgorithmException240{241if (provider == null)242throw new IllegalArgumentException("missing provider");243Object[] objs = Security.getImpl(algorithm, "AlgorithmParameters",244provider);245return new AlgorithmParameters((AlgorithmParametersSpi)objs[0],246(Provider)objs[1],247algorithm);248}249250/**251* Returns the provider of this parameter object.252*253* @return the provider of this parameter object254*/255public final Provider getProvider() {256return this.provider;257}258259/**260* Initializes this parameter object using the parameters261* specified in {@code paramSpec}.262*263* @param paramSpec the parameter specification.264*265* @exception InvalidParameterSpecException if the given parameter266* specification is inappropriate for the initialization of this parameter267* object, or if this parameter object has already been initialized.268*/269public final void init(AlgorithmParameterSpec paramSpec)270throws InvalidParameterSpecException271{272if (this.initialized)273throw new InvalidParameterSpecException("already initialized");274paramSpi.engineInit(paramSpec);275this.initialized = true;276}277278/**279* Imports the specified parameters and decodes them according to the280* primary decoding format for parameters. The primary decoding281* format for parameters is ASN.1, if an ASN.1 specification for this type282* of parameters exists.283*284* @param params the encoded parameters.285*286* @exception IOException on decoding errors, or if this parameter object287* has already been initialized.288*/289public final void init(byte[] params) throws IOException {290if (this.initialized)291throw new IOException("already initialized");292paramSpi.engineInit(params);293this.initialized = true;294}295296/**297* Imports the parameters from {@code params} and decodes them298* according to the specified decoding scheme.299* If {@code format} is null, the300* primary decoding format for parameters is used. The primary decoding301* format is ASN.1, if an ASN.1 specification for these parameters302* exists.303*304* @param params the encoded parameters.305*306* @param format the name of the decoding scheme.307*308* @exception IOException on decoding errors, or if this parameter object309* has already been initialized.310*/311public final void init(byte[] params, String format) throws IOException {312if (this.initialized)313throw new IOException("already initialized");314paramSpi.engineInit(params, format);315this.initialized = true;316}317318/**319* Returns a (transparent) specification of this parameter object.320* {@code paramSpec} identifies the specification class in which321* the parameters should be returned. It could, for example, be322* {@code DSAParameterSpec.class}, to indicate that the323* parameters should be returned in an instance of the324* {@code DSAParameterSpec} class.325*326* @param <T> the type of the parameter specification to be returrned327* @param paramSpec the specification class in which328* the parameters should be returned.329*330* @return the parameter specification.331*332* @exception InvalidParameterSpecException if the requested parameter333* specification is inappropriate for this parameter object, or if this334* parameter object has not been initialized.335*/336public final <T extends AlgorithmParameterSpec>337T getParameterSpec(Class<T> paramSpec)338throws InvalidParameterSpecException339{340if (this.initialized == false) {341throw new InvalidParameterSpecException("not initialized");342}343return paramSpi.engineGetParameterSpec(paramSpec);344}345346/**347* Returns the parameters in their primary encoding format.348* The primary encoding format for parameters is ASN.1, if an ASN.1349* specification for this type of parameters exists.350*351* @return the parameters encoded using their primary encoding format.352*353* @exception IOException on encoding errors, or if this parameter object354* has not been initialized.355*/356public final byte[] getEncoded() throws IOException357{358if (this.initialized == false) {359throw new IOException("not initialized");360}361return paramSpi.engineGetEncoded();362}363364/**365* Returns the parameters encoded in the specified scheme.366* If {@code format} is null, the367* primary encoding format for parameters is used. The primary encoding368* format is ASN.1, if an ASN.1 specification for these parameters369* exists.370*371* @param format the name of the encoding format.372*373* @return the parameters encoded using the specified encoding scheme.374*375* @exception IOException on encoding errors, or if this parameter object376* has not been initialized.377*/378public final byte[] getEncoded(String format) throws IOException379{380if (this.initialized == false) {381throw new IOException("not initialized");382}383return paramSpi.engineGetEncoded(format);384}385386/**387* Returns a formatted string describing the parameters.388*389* @return a formatted string describing the parameters, or null if this390* parameter object has not been initialized.391*/392public final String toString() {393if (this.initialized == false) {394return null;395}396return paramSpi.engineToString();397}398}399400401