Path: blob/master/src/java.base/share/classes/javax/net/ssl/KeyManagerFactory.java
41159 views
/*1* Copyright (c) 1999, 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 javax.net.ssl;2627import java.security.Security;28import java.security.*;29import java.util.Objects;3031import sun.security.jca.GetInstance;3233/**34* This class acts as a factory for key managers based on a35* source of key material. Each key manager manages a specific36* type of key material for use by secure sockets. The key37* material is based on a KeyStore and/or provider specific sources.38*39* @since 1.440* @see KeyManager41*/42public class KeyManagerFactory {43// The provider44private Provider provider;4546// The provider implementation (delegate)47private KeyManagerFactorySpi factorySpi;4849// The name of the key management algorithm.50private String algorithm;5152/**53* Obtains the default KeyManagerFactory algorithm name.54*55* <p>The default algorithm can be changed at runtime by setting56* the value of the {@code ssl.KeyManagerFactory.algorithm}57* security property to the desired algorithm name.58*59* @see java.security.Security security properties60* @return the default algorithm name as specified by the61* {@code ssl.KeyManagerFactory.algorithm} security property, or an62* implementation-specific default if no such property exists.63*/64@SuppressWarnings("removal")65public static final String getDefaultAlgorithm() {66String type;67type = AccessController.doPrivileged(new PrivilegedAction<>() {68@Override69public String run() {70return Security.getProperty(71"ssl.KeyManagerFactory.algorithm");72}73});74if (type == null) {75type = "SunX509";76}77return type;78}7980/**81* Creates a KeyManagerFactory object.82*83* @param factorySpi the delegate84* @param provider the provider85* @param algorithm the algorithm86*/87protected KeyManagerFactory(KeyManagerFactorySpi factorySpi,88Provider provider, String algorithm) {89this.factorySpi = factorySpi;90this.provider = provider;91this.algorithm = algorithm;92}9394/**95* Returns the algorithm name of this <code>KeyManagerFactory</code> object.96*97* <p>This is the same name that was specified in one of the98* <code>getInstance</code> calls that created this99* <code>KeyManagerFactory</code> object.100*101* @return the algorithm name of this <code>KeyManagerFactory</code> object.102*/103public final String getAlgorithm() {104return this.algorithm;105}106107/**108* Returns a <code>KeyManagerFactory</code> object that acts as a109* factory for key managers.110*111* <p> This method traverses the list of registered security Providers,112* starting with the most preferred Provider.113* A new KeyManagerFactory object encapsulating the114* KeyManagerFactorySpi implementation from the first115* Provider that supports the specified algorithm is returned.116*117* <p> Note that the list of registered providers may be retrieved via118* the {@link Security#getProviders() Security.getProviders()} method.119*120* @implNote121* The JDK Reference Implementation additionally uses the122* {@code jdk.security.provider.preferred}123* {@link Security#getProperty(String) Security} property to determine124* the preferred provider order for the specified algorithm. This125* may be different than the order of providers returned by126* {@link Security#getProviders() Security.getProviders()}.127*128* @param algorithm the standard name of the requested algorithm.129* See the <a href=130* "{@docRoot}/../specs/security/standard-names.html#keymanagerfactory-algorithms">131* KeyManagerFactory section</a> in the Java Security Standard132* Algorithm Names Specification for information about standard133* algorithm names.134*135* @return the new {@code KeyManagerFactory} object136*137* @throws NoSuchAlgorithmException if no {@code Provider} supports a138* {@code KeyManagerFactorySpi} implementation for the139* specified algorithm140*141* @throws NullPointerException if {@code algorithm} is {@code null}142*143* @see java.security.Provider144*/145public static final KeyManagerFactory getInstance(String algorithm)146throws NoSuchAlgorithmException {147Objects.requireNonNull(algorithm, "null algorithm name");148GetInstance.Instance instance = GetInstance.getInstance149("KeyManagerFactory", KeyManagerFactorySpi.class,150algorithm);151return new KeyManagerFactory((KeyManagerFactorySpi)instance.impl,152instance.provider, algorithm);153}154155/**156* Returns a <code>KeyManagerFactory</code> object that acts as a157* factory for key managers.158*159* <p> A new KeyManagerFactory object encapsulating the160* KeyManagerFactorySpi implementation from the specified provider161* is returned. The specified provider must be registered162* in the security provider list.163*164* <p> Note that the list of registered providers may be retrieved via165* the {@link Security#getProviders() Security.getProviders()} method.166*167* @param algorithm the standard name of the requested algorithm.168* See the <a href=169* "{@docRoot}/../specs/security/standard-names.html#keymanagerfactory-algorithms">170* KeyManagerFactory section</a> in the Java Security Standard171* Algorithm Names Specification for information about standard172* algorithm names.173*174* @param provider the name of the provider.175*176* @return the new {@code KeyManagerFactory} object177*178* @throws IllegalArgumentException if the provider name is {@code null}179* or empty180*181* @throws NoSuchAlgorithmException if a {@code KeyManagerFactorySpi}182* implementation for the specified algorithm is not183* available from the specified provider184*185* @throws NoSuchProviderException if the specified provider is not186* registered in the security provider list187*188* @throws NullPointerException if {@code algorithm} is {@code null}189*190* @see java.security.Provider191*/192public static final KeyManagerFactory getInstance(String algorithm,193String provider) throws NoSuchAlgorithmException,194NoSuchProviderException {195Objects.requireNonNull(algorithm, "null algorithm name");196GetInstance.Instance instance = GetInstance.getInstance197("KeyManagerFactory", KeyManagerFactorySpi.class,198algorithm, provider);199return new KeyManagerFactory((KeyManagerFactorySpi)instance.impl,200instance.provider, algorithm);201}202203/**204* Returns a <code>KeyManagerFactory</code> object that acts as a205* factory for key managers.206*207* <p> A new KeyManagerFactory object encapsulating the208* KeyManagerFactorySpi implementation from the specified Provider209* object is returned. Note that the specified Provider object210* does not have to be registered in the provider list.211*212* @param algorithm the standard name of the requested algorithm.213* See the <a href=214* "{@docRoot}/../specs/security/standard-names.html#keymanagerfactory-algorithms">215* KeyManagerFactory section</a> in the Java Security Standard216* Algorithm Names Specification for information about standard217* algorithm names.218*219* @param provider an instance of the provider.220*221* @return the new {@code KeyManagerFactory} object222*223* @throws IllegalArgumentException if provider is {@code null}224*225* @throws NoSuchAlgorithmException if a {@code @KeyManagerFactorySpi}226* implementation for the specified algorithm is not available227* from the specified Provider object228*229* @throws NullPointerException if {@code algorithm} is {@code null}230*231* @see java.security.Provider232*/233public static final KeyManagerFactory getInstance(String algorithm,234Provider provider) throws NoSuchAlgorithmException {235Objects.requireNonNull(algorithm, "null algorithm name");236GetInstance.Instance instance = GetInstance.getInstance237("KeyManagerFactory", KeyManagerFactorySpi.class,238algorithm, provider);239return new KeyManagerFactory((KeyManagerFactorySpi)instance.impl,240instance.provider, algorithm);241}242243/**244* Returns the provider of this <code>KeyManagerFactory</code> object.245*246* @return the provider of this <code>KeyManagerFactory</code> object247*/248public final Provider getProvider() {249return this.provider;250}251252253/**254* Initializes this factory with a source of key material.255* <P>256* The provider typically uses a KeyStore for obtaining257* key material for use during secure socket negotiations.258* The KeyStore is generally password-protected.259* <P>260* For more flexible initialization, please see261* {@link #init(ManagerFactoryParameters)}.262*263* @param ks the key store or null264* @param password the password for recovering keys in the KeyStore265* @throws KeyStoreException if this operation fails266* @throws NoSuchAlgorithmException if the specified algorithm is not267* available from the specified provider.268* @throws UnrecoverableKeyException if the key cannot be recovered269* (e.g. the given password is wrong).270*/271public final void init(KeyStore ks, char[] password) throws272KeyStoreException, NoSuchAlgorithmException,273UnrecoverableKeyException {274factorySpi.engineInit(ks, password);275}276277278/**279* Initializes this factory with a source of provider-specific280* key material.281* <P>282* In some cases, initialization parameters other than a keystore283* and password may be needed by a provider. Users of that284* particular provider are expected to pass an implementation of285* the appropriate <CODE>ManagerFactoryParameters</CODE> as286* defined by the provider. The provider can then call the287* specified methods in the <CODE>ManagerFactoryParameters</CODE>288* implementation to obtain the needed information.289*290* @param spec an implementation of a provider-specific parameter291* specification292* @throws InvalidAlgorithmParameterException if an error is encountered293*/294public final void init(ManagerFactoryParameters spec) throws295InvalidAlgorithmParameterException {296factorySpi.engineInit(spec);297}298299300/**301* Returns one key manager for each type of key material.302*303* @return the key managers304* @throws IllegalStateException if the KeyManagerFactory is not initialized305*/306public final KeyManager[] getKeyManagers() {307return factorySpi.engineGetKeyManagers();308}309}310311312