Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/security/AlgorithmParameterGenerator.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.security.spec.AlgorithmParameterSpec;2829/**30* The {@code AlgorithmParameterGenerator} class is used to generate a31* set of32* parameters to be used with a certain algorithm. Parameter generators33* are constructed using the {@code getInstance} factory methods34* (static methods that return instances of a given class).35*36* <P>The object that will generate the parameters can be initialized37* in two different ways: in an algorithm-independent manner, or in an38* algorithm-specific manner:39*40* <ul>41* <li>The algorithm-independent approach uses the fact that all parameter42* generators share the concept of a "size" and a43* source of randomness. The measure of size is universally shared44* by all algorithm parameters, though it is interpreted differently45* for different algorithms. For example, in the case of parameters for46* the <i>DSA</i> algorithm, "size" corresponds to the size47* of the prime modulus (in bits).48* When using this approach, algorithm-specific parameter generation49* values - if any - default to some standard values, unless they can be50* derived from the specified size.51*52* <li>The other approach initializes a parameter generator object53* using algorithm-specific semantics, which are represented by a set of54* algorithm-specific parameter generation values. To generate55* Diffie-Hellman system parameters, for example, the parameter generation56* values usually57* consist of the size of the prime modulus and the size of the58* random exponent, both specified in number of bits.59* </ul>60*61* <P>In case the client does not explicitly initialize the62* AlgorithmParameterGenerator63* (via a call to an {@code init} method), each provider must supply (and64* document) a default initialization. For example, the Sun provider uses a65* default modulus prime size of 1024 bits for the generation of DSA66* parameters.67*68* <p> Every implementation of the Java platform is required to support the69* following standard {@code AlgorithmParameterGenerator} algorithms and70* keysizes in parentheses:71* <ul>72* <li>{@code DiffieHellman} (1024)</li>73* <li>{@code DSA} (1024)</li>74* </ul>75* These algorithms are described in the <a href=76* "{@docRoot}/../technotes/guides/security/StandardNames.html#AlgorithmParameterGenerator">77* AlgorithmParameterGenerator section</a> of the78* Java Cryptography Architecture Standard Algorithm Name Documentation.79* Consult the release documentation for your implementation to see if any80* other algorithms are supported.81*82* @author Jan Luehe83*84*85* @see AlgorithmParameters86* @see java.security.spec.AlgorithmParameterSpec87*88* @since 1.289*/9091public class AlgorithmParameterGenerator {9293// The provider94private Provider provider;9596// The provider implementation (delegate)97private AlgorithmParameterGeneratorSpi paramGenSpi;9899// The algorithm100private String algorithm;101102/**103* Creates an AlgorithmParameterGenerator object.104*105* @param paramGenSpi the delegate106* @param provider the provider107* @param algorithm the algorithm108*/109protected AlgorithmParameterGenerator110(AlgorithmParameterGeneratorSpi paramGenSpi, Provider provider,111String algorithm) {112this.paramGenSpi = paramGenSpi;113this.provider = provider;114this.algorithm = algorithm;115}116117/**118* Returns the standard name of the algorithm this parameter119* generator is associated with.120*121* @return the string name of the algorithm.122*/123public final String getAlgorithm() {124return this.algorithm;125}126127/**128* Returns an AlgorithmParameterGenerator object for generating129* a set of parameters to be used with the specified algorithm.130*131* <p> This method traverses the list of registered security Providers,132* starting with the most preferred Provider.133* A new AlgorithmParameterGenerator object encapsulating the134* AlgorithmParameterGeneratorSpi implementation from the first135* Provider that supports the specified algorithm is returned.136*137* <p> Note that the list of registered providers may be retrieved via138* the {@link Security#getProviders() Security.getProviders()} method.139*140* @param algorithm the name of the algorithm this141* parameter generator is associated with.142* See the AlgorithmParameterGenerator section in the <a href=143* "{@docRoot}/../technotes/guides/security/StandardNames.html#AlgorithmParameterGenerator">144* Java Cryptography Architecture Standard Algorithm Name Documentation</a>145* for information about standard algorithm names.146*147* @return the new AlgorithmParameterGenerator object.148*149* @exception NoSuchAlgorithmException if no Provider supports an150* AlgorithmParameterGeneratorSpi implementation for the151* specified algorithm.152*153* @see Provider154*/155public static AlgorithmParameterGenerator getInstance(String algorithm)156throws NoSuchAlgorithmException {157try {158Object[] objs = Security.getImpl(algorithm,159"AlgorithmParameterGenerator",160(String)null);161return new AlgorithmParameterGenerator162((AlgorithmParameterGeneratorSpi)objs[0],163(Provider)objs[1],164algorithm);165} catch(NoSuchProviderException e) {166throw new NoSuchAlgorithmException(algorithm + " not found");167}168}169170/**171* Returns an AlgorithmParameterGenerator object for generating172* a set of parameters to be used with the specified algorithm.173*174* <p> A new AlgorithmParameterGenerator object encapsulating the175* AlgorithmParameterGeneratorSpi implementation from the specified provider176* is returned. The specified provider must be registered177* in the security provider list.178*179* <p> Note that the list of registered providers may be retrieved via180* the {@link Security#getProviders() Security.getProviders()} method.181*182* @param algorithm the name of the algorithm this183* parameter generator is associated with.184* See the AlgorithmParameterGenerator section in the <a href=185* "{@docRoot}/../technotes/guides/security/StandardNames.html#AlgorithmParameterGenerator">186* Java Cryptography Architecture Standard Algorithm Name Documentation</a>187* for information about standard algorithm names.188*189* @param provider the string name of the Provider.190*191* @return the new AlgorithmParameterGenerator object.192*193* @exception NoSuchAlgorithmException if an AlgorithmParameterGeneratorSpi194* implementation for the specified algorithm is not195* available from the specified provider.196*197* @exception NoSuchProviderException if the specified provider is not198* registered in the security provider list.199*200* @exception IllegalArgumentException if the provider name is null201* or empty.202*203* @see Provider204*/205public static AlgorithmParameterGenerator getInstance(String algorithm,206String provider)207throws NoSuchAlgorithmException, NoSuchProviderException208{209if (provider == null || provider.length() == 0)210throw new IllegalArgumentException("missing provider");211Object[] objs = Security.getImpl(algorithm,212"AlgorithmParameterGenerator",213provider);214return new AlgorithmParameterGenerator215((AlgorithmParameterGeneratorSpi)objs[0], (Provider)objs[1],216algorithm);217}218219/**220* Returns an AlgorithmParameterGenerator object for generating221* a set of parameters to be used with the specified algorithm.222*223* <p> A new AlgorithmParameterGenerator object encapsulating the224* AlgorithmParameterGeneratorSpi implementation from the specified Provider225* object is returned. Note that the specified Provider object226* does not have to be registered in the provider list.227*228* @param algorithm the string name of the algorithm this229* parameter generator is associated with.230* See the AlgorithmParameterGenerator section in the <a href=231* "{@docRoot}/../technotes/guides/security/StandardNames.html#AlgorithmParameterGenerator">232* Java Cryptography Architecture Standard Algorithm Name Documentation</a>233* for information about standard algorithm names.234*235* @param provider the Provider object.236*237* @return the new AlgorithmParameterGenerator object.238*239* @exception NoSuchAlgorithmException if an AlgorithmParameterGeneratorSpi240* implementation for the specified algorithm is not available241* from the specified Provider object.242*243* @exception IllegalArgumentException if the specified provider is null.244*245* @see Provider246*247* @since 1.4248*/249public static AlgorithmParameterGenerator getInstance(String algorithm,250Provider provider)251throws NoSuchAlgorithmException252{253if (provider == null)254throw new IllegalArgumentException("missing provider");255Object[] objs = Security.getImpl(algorithm,256"AlgorithmParameterGenerator",257provider);258return new AlgorithmParameterGenerator259((AlgorithmParameterGeneratorSpi)objs[0], (Provider)objs[1],260algorithm);261}262263/**264* Returns the provider of this algorithm parameter generator object.265*266* @return the provider of this algorithm parameter generator object267*/268public final Provider getProvider() {269return this.provider;270}271272/**273* Initializes this parameter generator for a certain size.274* To create the parameters, the {@code SecureRandom}275* implementation of the highest-priority installed provider is used as276* the source of randomness.277* (If none of the installed providers supply an implementation of278* {@code SecureRandom}, a system-provided source of randomness is279* used.)280*281* @param size the size (number of bits).282*/283public final void init(int size) {284paramGenSpi.engineInit(size, new SecureRandom());285}286287/**288* Initializes this parameter generator for a certain size and source289* of randomness.290*291* @param size the size (number of bits).292* @param random the source of randomness.293*/294public final void init(int size, SecureRandom random) {295paramGenSpi.engineInit(size, random);296}297298/**299* Initializes this parameter generator with a set of algorithm-specific300* parameter generation values.301* To generate the parameters, the {@code SecureRandom}302* implementation of the highest-priority installed provider is used as303* the source of randomness.304* (If none of the installed providers supply an implementation of305* {@code SecureRandom}, a system-provided source of randomness is306* used.)307*308* @param genParamSpec the set of algorithm-specific parameter generation values.309*310* @exception InvalidAlgorithmParameterException if the given parameter311* generation values are inappropriate for this parameter generator.312*/313public final void init(AlgorithmParameterSpec genParamSpec)314throws InvalidAlgorithmParameterException {315paramGenSpi.engineInit(genParamSpec, new SecureRandom());316}317318/**319* Initializes this parameter generator with a set of algorithm-specific320* parameter generation values.321*322* @param genParamSpec the set of algorithm-specific parameter generation values.323* @param random the source of randomness.324*325* @exception InvalidAlgorithmParameterException if the given parameter326* generation values are inappropriate for this parameter generator.327*/328public final void init(AlgorithmParameterSpec genParamSpec,329SecureRandom random)330throws InvalidAlgorithmParameterException {331paramGenSpi.engineInit(genParamSpec, random);332}333334/**335* Generates the parameters.336*337* @return the new AlgorithmParameters object.338*/339public final AlgorithmParameters generateParameters() {340return paramGenSpi.engineGenerateParameters();341}342}343344345