Path: blob/master/src/java.base/share/classes/java/security/AlgorithmParameters.java
41152 views
/*1* Copyright (c) 1997, 2019, 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;30import java.util.Objects;3132/**33* This class is used as an opaque representation of cryptographic parameters.34*35* <p>An {@code AlgorithmParameters} object for managing the parameters36* for a particular algorithm can be obtained by37* calling one of the {@code getInstance} factory methods38* (static methods that return instances of a given class).39*40* <p>Once an {@code AlgorithmParameters} object is obtained, it must be41* initialized via a call to {@code init}, using an appropriate parameter42* specification or parameter encoding.43*44* <p>A transparent parameter specification is obtained from an45* {@code AlgorithmParameters} object via a call to46* {@code getParameterSpec}, and a byte encoding of the parameters is47* obtained via a call to {@code getEncoded}.48*49* <p> Every implementation of the Java platform is required to support the50* following standard {@code AlgorithmParameters} algorithms:51* <ul>52* <li>{@code AES}</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}/../specs/security/standard-names.html#algorithmparameters-algorithms">59* AlgorithmParameters section</a> of the60* Java Security Standard Algorithm Names Specification.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* @implNote129* The JDK Reference Implementation additionally uses the130* {@code jdk.security.provider.preferred}131* {@link Security#getProperty(String) Security} property to determine132* the preferred provider order for the specified algorithm. This133* may be different than the order of providers returned by134* {@link Security#getProviders() Security.getProviders()}.135*136* @param algorithm the name of the algorithm requested.137* See the AlgorithmParameters section in the <a href=138* "{@docRoot}/../specs/security/standard-names.html#algorithmparameters-algorithms">139* Java Security Standard Algorithm Names Specification</a>140* for information about standard algorithm names.141*142* @return the new parameter object143*144* @throws NoSuchAlgorithmException if no {@code Provider} supports an145* {@code AlgorithmParametersSpi} implementation for the146* specified algorithm147*148* @throws NullPointerException if {@code algorithm} is {@code null}149*150* @see Provider151*/152public static AlgorithmParameters getInstance(String algorithm)153throws NoSuchAlgorithmException {154Objects.requireNonNull(algorithm, "null algorithm name");155try {156Object[] objs = Security.getImpl(algorithm, "AlgorithmParameters",157(String)null);158return new AlgorithmParameters((AlgorithmParametersSpi)objs[0],159(Provider)objs[1],160algorithm);161} catch(NoSuchProviderException e) {162throw new NoSuchAlgorithmException(algorithm + " not found");163}164}165166/**167* Returns a parameter object for the specified algorithm.168*169* <p> A new AlgorithmParameters object encapsulating the170* AlgorithmParametersSpi implementation from the specified provider171* is returned. The specified provider must be registered172* in the security provider list.173*174* <p> Note that the list of registered providers may be retrieved via175* the {@link Security#getProviders() Security.getProviders()} method.176*177* <p>The returned parameter object must be initialized via a call to178* {@code init}, using an appropriate parameter specification or179* parameter encoding.180*181* @param algorithm the name of the algorithm requested.182* See the AlgorithmParameters section in the <a href=183* "{@docRoot}/../specs/security/standard-names.html#algorithmparameters-algorithms">184* Java Security Standard Algorithm Names Specification</a>185* for information about standard algorithm names.186*187* @param provider the name of the provider.188*189* @return the new parameter object190*191* @throws IllegalArgumentException if the provider name is {@code null}192* or empty193*194* @throws NoSuchAlgorithmException if an {@code AlgorithmParametersSpi}195* implementation for the specified algorithm is not196* available from the specified provider197*198* @throws NoSuchProviderException if the specified provider is not199* registered in the security provider list200*201* @throws NullPointerException if {@code algorithm} is {@code null}202*203* @see Provider204*/205public static AlgorithmParameters getInstance(String algorithm,206String provider)207throws NoSuchAlgorithmException, NoSuchProviderException208{209Objects.requireNonNull(algorithm, "null algorithm name");210if (provider == null || provider.isEmpty())211throw new IllegalArgumentException("missing provider");212Object[] objs = Security.getImpl(algorithm, "AlgorithmParameters",213provider);214return new AlgorithmParameters((AlgorithmParametersSpi)objs[0],215(Provider)objs[1],216algorithm);217}218219/**220* Returns a parameter object for the specified algorithm.221*222* <p> A new AlgorithmParameters object encapsulating the223* AlgorithmParametersSpi implementation from the specified Provider224* object is returned. Note that the specified Provider object225* does not have to be registered in the provider list.226*227* <p>The returned parameter object must be initialized via a call to228* {@code init}, using an appropriate parameter specification or229* parameter encoding.230*231* @param algorithm the name of the algorithm requested.232* See the AlgorithmParameters section in the <a href=233* "{@docRoot}/../specs/security/standard-names.html#algorithmparameters-algorithms">234* Java Security Standard Algorithm Names Specification</a>235* for information about standard algorithm names.236*237* @param provider the name of the provider.238*239* @return the new parameter object240*241* @throws IllegalArgumentException if the provider is {@code null}242*243* @throws NoSuchAlgorithmException if an244* {@code AlgorithmParameterGeneratorSpi}245* implementation for the specified algorithm is not available246* from the specified {@code Provider} object247*248* @throws NullPointerException if {@code algorithm} is {@code null}249*250* @see Provider251*252* @since 1.4253*/254public static AlgorithmParameters getInstance(String algorithm,255Provider provider)256throws NoSuchAlgorithmException257{258Objects.requireNonNull(algorithm, "null algorithm name");259if (provider == null)260throw new IllegalArgumentException("missing provider");261Object[] objs = Security.getImpl(algorithm, "AlgorithmParameters",262provider);263return new AlgorithmParameters((AlgorithmParametersSpi)objs[0],264(Provider)objs[1],265algorithm);266}267268/**269* Returns the provider of this parameter object.270*271* @return the provider of this parameter object272*/273public final Provider getProvider() {274return this.provider;275}276277/**278* Initializes this parameter object using the parameters279* specified in {@code paramSpec}.280*281* @param paramSpec the parameter specification.282*283* @throws InvalidParameterSpecException if the given parameter284* specification is inappropriate for the initialization of this parameter285* object, or if this parameter object has already been initialized.286*/287public final void init(AlgorithmParameterSpec paramSpec)288throws InvalidParameterSpecException289{290if (this.initialized)291throw new InvalidParameterSpecException("already initialized");292paramSpi.engineInit(paramSpec);293this.initialized = true;294}295296/**297* Imports the specified parameters and decodes them according to the298* primary decoding format for parameters. The primary decoding299* format for parameters is ASN.1, if an ASN.1 specification for this type300* of parameters exists.301*302* @param params the encoded parameters.303*304* @throws IOException on decoding errors, or if this parameter object305* has already been initialized.306*/307public final void init(byte[] params) throws IOException {308if (this.initialized)309throw new IOException("already initialized");310paramSpi.engineInit(params);311this.initialized = true;312}313314/**315* Imports the parameters from {@code params} and decodes them316* according to the specified decoding scheme.317* If {@code format} is null, the318* primary decoding format for parameters is used. The primary decoding319* format is ASN.1, if an ASN.1 specification for these parameters320* exists.321*322* @param params the encoded parameters.323*324* @param format the name of the decoding scheme.325*326* @throws IOException on decoding errors, or if this parameter object327* has already been initialized.328*/329public final void init(byte[] params, String format) throws IOException {330if (this.initialized)331throw new IOException("already initialized");332paramSpi.engineInit(params, format);333this.initialized = true;334}335336/**337* Returns a (transparent) specification of this parameter object.338* {@code paramSpec} identifies the specification class in which339* the parameters should be returned. It could, for example, be340* {@code DSAParameterSpec.class}, to indicate that the341* parameters should be returned in an instance of the342* {@code DSAParameterSpec} class.343*344* @param <T> the type of the parameter specification to be returrned345* @param paramSpec the specification class in which346* the parameters should be returned.347*348* @return the parameter specification.349*350* @throws InvalidParameterSpecException if the requested parameter351* specification is inappropriate for this parameter object, or if this352* parameter object has not been initialized.353*/354public final <T extends AlgorithmParameterSpec>355T getParameterSpec(Class<T> paramSpec)356throws InvalidParameterSpecException357{358if (this.initialized == false) {359throw new InvalidParameterSpecException("not initialized");360}361return paramSpi.engineGetParameterSpec(paramSpec);362}363364/**365* Returns the parameters in their primary encoding format.366* The primary encoding format for parameters is ASN.1, if an ASN.1367* specification for this type of parameters exists.368*369* @return the parameters encoded using their primary encoding format.370*371* @throws IOException on encoding errors, or if this parameter object372* has not been initialized.373*/374public final byte[] getEncoded() throws IOException375{376if (this.initialized == false) {377throw new IOException("not initialized");378}379return paramSpi.engineGetEncoded();380}381382/**383* Returns the parameters encoded in the specified scheme.384* If {@code format} is null, the385* primary encoding format for parameters is used. The primary encoding386* format is ASN.1, if an ASN.1 specification for these parameters387* exists.388*389* @param format the name of the encoding format.390*391* @return the parameters encoded using the specified encoding scheme.392*393* @throws IOException on encoding errors, or if this parameter object394* has not been initialized.395*/396public final byte[] getEncoded(String format) throws IOException397{398if (this.initialized == false) {399throw new IOException("not initialized");400}401return paramSpi.engineGetEncoded(format);402}403404/**405* Returns a formatted string describing the parameters.406*407* @return a formatted string describing the parameters, or null if this408* parameter object has not been initialized.409*/410public final String toString() {411if (this.initialized == false) {412return null;413}414return paramSpi.engineToString();415}416}417418419