Path: blob/master/src/java.base/share/classes/java/security/AlgorithmParameterGenerator.java
41152 views
/*1* Copyright (c) 1997, 2021, 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;28import java.util.Objects;29import sun.security.jca.JCAUtil;3031/**32* The {@code AlgorithmParameterGenerator} class is used to generate a33* set of34* parameters to be used with a certain algorithm. Parameter generators35* are constructed using the {@code getInstance} factory methods36* (static methods that return instances of a given class).37*38* <P>The object that will generate the parameters can be initialized39* in two different ways: in an algorithm-independent manner, or in an40* algorithm-specific manner:41*42* <ul>43* <li>The algorithm-independent approach uses the fact that all parameter44* generators share the concept of a "size" and a45* source of randomness. The measure of size is universally shared46* by all algorithm parameters, though it is interpreted differently47* for different algorithms. For example, in the case of parameters for48* the <i>DSA</i> algorithm, "size" corresponds to the size49* of the prime modulus (in bits).50* When using this approach, algorithm-specific parameter generation51* values - if any - default to some standard values, unless they can be52* derived from the specified size.53*54* <li>The other approach initializes a parameter generator object55* using algorithm-specific semantics, which are represented by a set of56* algorithm-specific parameter generation values. To generate57* Diffie-Hellman system parameters, for example, the parameter generation58* values usually59* consist of the size of the prime modulus and the size of the60* random exponent, both specified in number of bits.61* </ul>62*63* <P>In case the client does not explicitly initialize the64* AlgorithmParameterGenerator (via a call to an {@code init} method),65* each provider must supply (and document) a default initialization.66* See the Keysize Restriction sections of the67* {@extLink security_guide_jdk_providers JDK Providers}68* document for information on the AlgorithmParameterGenerator defaults69* used by JDK providers.70* However, note that defaults may vary across different providers.71* Additionally, the default value for a provider may change in a future72* version. Therefore, it is recommended to explicitly initialize the73* AlgorithmParameterGenerator instead of relying on provider-specific defaults.74*75* <p> Every implementation of the Java platform is required to support the76* following standard {@code AlgorithmParameterGenerator} algorithms and77* keysizes in parentheses:78* <ul>79* <li>{@code DiffieHellman} (1024, 2048)</li>80* <li>{@code DSA} (1024, 2048)</li>81* </ul>82* These algorithms are described in the <a href=83* "{@docRoot}/../specs/security/standard-names.html#algorithmparametergenerator-algorithms">84* AlgorithmParameterGenerator section</a> of the85* Java Security Standard Algorithm Names Specification.86* Consult the release documentation for your implementation to see if any87* other algorithms are supported.88*89* @author Jan Luehe90*91*92* @see AlgorithmParameters93* @see java.security.spec.AlgorithmParameterSpec94*95* @since 1.296*/9798public class AlgorithmParameterGenerator {99100// The provider101private Provider provider;102103// The provider implementation (delegate)104private AlgorithmParameterGeneratorSpi paramGenSpi;105106// The algorithm107private String algorithm;108109/**110* Creates an AlgorithmParameterGenerator object.111*112* @param paramGenSpi the delegate113* @param provider the provider114* @param algorithm the algorithm115*/116protected AlgorithmParameterGenerator117(AlgorithmParameterGeneratorSpi paramGenSpi, Provider provider,118String algorithm) {119this.paramGenSpi = paramGenSpi;120this.provider = provider;121this.algorithm = algorithm;122}123124/**125* Returns the standard name of the algorithm this parameter126* generator is associated with.127*128* @return the string name of the algorithm.129*/130public final String getAlgorithm() {131return this.algorithm;132}133134/**135* Returns an AlgorithmParameterGenerator object for generating136* a set of parameters to be used with the specified algorithm.137*138* <p> This method traverses the list of registered security Providers,139* starting with the most preferred Provider.140* A new AlgorithmParameterGenerator object encapsulating the141* AlgorithmParameterGeneratorSpi implementation from the first142* Provider that supports the specified algorithm is returned.143*144* <p> Note that the list of registered providers may be retrieved via145* the {@link Security#getProviders() Security.getProviders()} method.146*147* @implNote148* The JDK Reference Implementation additionally uses the149* {@code jdk.security.provider.preferred}150* {@link Security#getProperty(String) Security} property to determine151* the preferred provider order for the specified algorithm. This152* may be different than the order of providers returned by153* {@link Security#getProviders() Security.getProviders()}.154*155* @param algorithm the name of the algorithm this156* parameter generator is associated with.157* See the AlgorithmParameterGenerator section in the <a href=158* "{@docRoot}/../specs/security/standard-names.html#algorithmparametergenerator-algorithms">159* Java Security Standard Algorithm Names Specification</a>160* for information about standard algorithm names.161*162* @return the new {@code AlgorithmParameterGenerator} object163*164* @throws NoSuchAlgorithmException if no {@code Provider} supports an165* {@code AlgorithmParameterGeneratorSpi} implementation for the166* specified algorithm167*168* @throws NullPointerException if {@code algorithm} is {@code null}169*170* @see Provider171*/172public static AlgorithmParameterGenerator getInstance(String algorithm)173throws NoSuchAlgorithmException {174Objects.requireNonNull(algorithm, "null algorithm name");175try {176Object[] objs = Security.getImpl(algorithm,177"AlgorithmParameterGenerator",178(String)null);179return new AlgorithmParameterGenerator180((AlgorithmParameterGeneratorSpi)objs[0],181(Provider)objs[1],182algorithm);183} catch(NoSuchProviderException e) {184throw new NoSuchAlgorithmException(algorithm + " not found");185}186}187188/**189* Returns an AlgorithmParameterGenerator object for generating190* a set of parameters to be used with the specified algorithm.191*192* <p> A new AlgorithmParameterGenerator object encapsulating the193* AlgorithmParameterGeneratorSpi implementation from the specified provider194* is returned. The specified provider must be registered195* in the security provider list.196*197* <p> Note that the list of registered providers may be retrieved via198* the {@link Security#getProviders() Security.getProviders()} method.199*200* @param algorithm the name of the algorithm this201* parameter generator is associated with.202* See the AlgorithmParameterGenerator section in the <a href=203* "{@docRoot}/../specs/security/standard-names.html#algorithmparametergenerator-algorithms">204* Java Security Standard Algorithm Names Specification</a>205* for information about standard algorithm names.206*207* @param provider the string name of the Provider.208*209* @return the new {@code AlgorithmParameterGenerator} object210*211* @throws IllegalArgumentException if the provider name is {@code null}212* or empty213*214* @throws NoSuchAlgorithmException if an215* {@code AlgorithmParameterGeneratorSpi}216* implementation for the specified algorithm is not217* available from the specified provider218*219* @throws NoSuchProviderException if the specified provider is not220* registered in the security provider list221*222* @throws NullPointerException if {@code algorithm} is {@code null}223*224* @see Provider225*/226public static AlgorithmParameterGenerator getInstance(String algorithm,227String provider)228throws NoSuchAlgorithmException, NoSuchProviderException229{230Objects.requireNonNull(algorithm, "null algorithm name");231if (provider == null || provider.isEmpty())232throw new IllegalArgumentException("missing provider");233Object[] objs = Security.getImpl(algorithm,234"AlgorithmParameterGenerator",235provider);236return new AlgorithmParameterGenerator237((AlgorithmParameterGeneratorSpi)objs[0], (Provider)objs[1],238algorithm);239}240241/**242* Returns an AlgorithmParameterGenerator object for generating243* a set of parameters to be used with the specified algorithm.244*245* <p> A new AlgorithmParameterGenerator object encapsulating the246* AlgorithmParameterGeneratorSpi implementation from the specified Provider247* object is returned. Note that the specified Provider object248* does not have to be registered in the provider list.249*250* @param algorithm the string name of the algorithm this251* parameter generator is associated with.252* See the AlgorithmParameterGenerator section in the <a href=253* "{@docRoot}/../specs/security/standard-names.html#algorithmparametergenerator-algorithms">254* Java Security Standard Algorithm Names Specification</a>255* for information about standard algorithm names.256*257* @param provider the {@code Provider} object.258*259* @return the new {@code AlgorithmParameterGenerator} object260*261* @throws IllegalArgumentException if the specified provider is262* {@code null}263*264* @throws NoSuchAlgorithmException if an265* {@code AlgorithmParameterGeneratorSpi}266* implementation for the specified algorithm is not available267* from the specified {@code Provider} object268*269* @throws NullPointerException if {@code algorithm} is {@code null}270*271* @see Provider272*273* @since 1.4274*/275public static AlgorithmParameterGenerator getInstance(String algorithm,276Provider provider)277throws NoSuchAlgorithmException278{279Objects.requireNonNull(algorithm, "null algorithm name");280if (provider == null)281throw new IllegalArgumentException("missing provider");282Object[] objs = Security.getImpl(algorithm,283"AlgorithmParameterGenerator",284provider);285return new AlgorithmParameterGenerator286((AlgorithmParameterGeneratorSpi)objs[0], (Provider)objs[1],287algorithm);288}289290/**291* Returns the provider of this algorithm parameter generator object.292*293* @return the provider of this algorithm parameter generator object294*/295public final Provider getProvider() {296return this.provider;297}298299/**300* Initializes this parameter generator for a certain size.301* To create 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 size the size (number of bits).309*/310public final void init(int size) {311paramGenSpi.engineInit(size, JCAUtil.getDefSecureRandom());312}313314/**315* Initializes this parameter generator for a certain size and source316* of randomness.317*318* @param size the size (number of bits).319* @param random the source of randomness.320*/321public final void init(int size, SecureRandom random) {322paramGenSpi.engineInit(size, random);323}324325/**326* Initializes this parameter generator with a set of algorithm-specific327* parameter generation values.328* To generate the parameters, the {@code SecureRandom}329* implementation of the highest-priority installed provider is used as330* the source of randomness.331* (If none of the installed providers supply an implementation of332* {@code SecureRandom}, a system-provided source of randomness is333* used.)334*335* @param genParamSpec the set of algorithm-specific parameter generation values.336*337* @throws InvalidAlgorithmParameterException if the given parameter338* generation values are inappropriate for this parameter generator.339*/340public final void init(AlgorithmParameterSpec genParamSpec)341throws InvalidAlgorithmParameterException {342paramGenSpi.engineInit(genParamSpec, JCAUtil.getDefSecureRandom());343}344345/**346* Initializes this parameter generator with a set of algorithm-specific347* parameter generation values.348*349* @param genParamSpec the set of algorithm-specific parameter generation values.350* @param random the source of randomness.351*352* @throws InvalidAlgorithmParameterException if the given parameter353* generation values are inappropriate for this parameter generator.354*/355public final void init(AlgorithmParameterSpec genParamSpec,356SecureRandom random)357throws InvalidAlgorithmParameterException {358paramGenSpi.engineInit(genParamSpec, random);359}360361/**362* Generates the parameters.363*364* @return the new AlgorithmParameters object.365*/366public final AlgorithmParameters generateParameters() {367return paramGenSpi.engineGenerateParameters();368}369}370371372