Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/net/ssl/KeyManagerFactory.java
38918 views
/*1* Copyright (c) 1999, 2012, 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.*;2930import sun.security.jca.GetInstance;3132/**33* This class acts as a factory for key managers based on a34* source of key material. Each key manager manages a specific35* type of key material for use by secure sockets. The key36* material is based on a KeyStore and/or provider specific sources.37*38* @since 1.439* @see KeyManager40*/41public class KeyManagerFactory {42// The provider43private Provider provider;4445// The provider implementation (delegate)46private KeyManagerFactorySpi factorySpi;4748// The name of the key management algorithm.49private String algorithm;5051/**52* Obtains the default KeyManagerFactory algorithm name.53*54* <p>The default algorithm can be changed at runtime by setting55* the value of the {@code ssl.KeyManagerFactory.algorithm}56* security property to the desired algorithm name.57*58* @see java.security.Security security properties59* @return the default algorithm name as specified by the60* {@code ssl.KeyManagerFactory.algorithm} security property, or an61* implementation-specific default if no such property exists.62*/63public final static String getDefaultAlgorithm() {64String type;65type = AccessController.doPrivileged(new PrivilegedAction<String>() {66@Override67public String run() {68return Security.getProperty(69"ssl.KeyManagerFactory.algorithm");70}71});72if (type == null) {73type = "SunX509";74}75return type;76}7778/**79* Creates a KeyManagerFactory object.80*81* @param factorySpi the delegate82* @param provider the provider83* @param algorithm the algorithm84*/85protected KeyManagerFactory(KeyManagerFactorySpi factorySpi,86Provider provider, String algorithm) {87this.factorySpi = factorySpi;88this.provider = provider;89this.algorithm = algorithm;90}9192/**93* Returns the algorithm name of this <code>KeyManagerFactory</code> object.94*95* <p>This is the same name that was specified in one of the96* <code>getInstance</code> calls that created this97* <code>KeyManagerFactory</code> object.98*99* @return the algorithm name of this <code>KeyManagerFactory</code> object.100*/101public final String getAlgorithm() {102return this.algorithm;103}104105/**106* Returns a <code>KeyManagerFactory</code> object that acts as a107* factory for key managers.108*109* <p> This method traverses the list of registered security Providers,110* starting with the most preferred Provider.111* A new KeyManagerFactory object encapsulating the112* KeyManagerFactorySpi implementation from the first113* Provider that supports the specified algorithm is returned.114*115* <p> Note that the list of registered providers may be retrieved via116* the {@link Security#getProviders() Security.getProviders()} method.117*118* @param algorithm the standard name of the requested algorithm.119* See the <a href=120* "{@docRoot}/../technotes/guides/security/jsse/JSSERefGuide.html">121* Java Secure Socket Extension Reference Guide </a>122* for information about standard algorithm names.123*124* @return the new <code>KeyManagerFactory</code> object.125*126* @exception NoSuchAlgorithmException if no Provider supports a127* KeyManagerFactorySpi implementation for the128* specified algorithm.129* @exception NullPointerException if <code>algorithm</code> is null.130*131* @see java.security.Provider132*/133public static final KeyManagerFactory getInstance(String algorithm)134throws NoSuchAlgorithmException {135GetInstance.Instance instance = GetInstance.getInstance136("KeyManagerFactory", KeyManagerFactorySpi.class,137algorithm);138return new KeyManagerFactory((KeyManagerFactorySpi)instance.impl,139instance.provider, algorithm);140}141142/**143* Returns a <code>KeyManagerFactory</code> object that acts as a144* factory for key managers.145*146* <p> A new KeyManagerFactory object encapsulating the147* KeyManagerFactorySpi implementation from the specified provider148* is returned. The specified provider must be registered149* in the security provider list.150*151* <p> Note that the list of registered providers may be retrieved via152* the {@link Security#getProviders() Security.getProviders()} method.153154* @param algorithm the standard name of the requested algorithm.155* See the <a href=156* "{@docRoot}/../technotes/guides/security/jsse/JSSERefGuide.html">157* Java Secure Socket Extension Reference Guide </a>158* for information about standard algorithm names.159*160* @param provider the name of the provider.161*162* @return the new <code>KeyManagerFactory</code> object.163*164* @throws NoSuchAlgorithmException if a KeyManagerFactorySpi165* implementation for the specified algorithm is not166* available from the specified provider.167*168* @throws NoSuchProviderException if the specified provider is not169* registered in the security provider list.170*171* @throws IllegalArgumentException if the provider name is null or empty.172* @throws NullPointerException if <code>algorithm</code> is null.173*174* @see java.security.Provider175*/176public static final KeyManagerFactory getInstance(String algorithm,177String provider) throws NoSuchAlgorithmException,178NoSuchProviderException {179GetInstance.Instance instance = GetInstance.getInstance180("KeyManagerFactory", KeyManagerFactorySpi.class,181algorithm, provider);182return new KeyManagerFactory((KeyManagerFactorySpi)instance.impl,183instance.provider, algorithm);184}185186/**187* Returns a <code>KeyManagerFactory</code> object that acts as a188* factory for key managers.189*190* <p> A new KeyManagerFactory object encapsulating the191* KeyManagerFactorySpi implementation from the specified Provider192* object is returned. Note that the specified Provider object193* does not have to be registered in the provider list.194*195* @param algorithm the standard name of the requested algorithm.196* See the <a href=197* "{@docRoot}/../technotes/guides/security/jsse/JSSERefGuide.html">198* Java Secure Socket Extension Reference Guide </a>199* for information about standard algorithm names.200*201* @param provider an instance of the provider.202*203* @return the new <code>KeyManagerFactory</code> object.204*205* @throws NoSuchAlgorithmException if a KeyManagerFactorySpi206* implementation for the specified algorithm is not available207* from the specified Provider object.208*209* @throws IllegalArgumentException if provider is null.210* @throws NullPointerException if <code>algorithm</code> is null.211*212* @see java.security.Provider213*/214public static final KeyManagerFactory getInstance(String algorithm,215Provider provider) throws NoSuchAlgorithmException {216GetInstance.Instance instance = GetInstance.getInstance217("KeyManagerFactory", KeyManagerFactorySpi.class,218algorithm, provider);219return new KeyManagerFactory((KeyManagerFactorySpi)instance.impl,220instance.provider, algorithm);221}222223/**224* Returns the provider of this <code>KeyManagerFactory</code> object.225*226* @return the provider of this <code>KeyManagerFactory</code> object227*/228public final Provider getProvider() {229return this.provider;230}231232233/**234* Initializes this factory with a source of key material.235* <P>236* The provider typically uses a KeyStore for obtaining237* key material for use during secure socket negotiations.238* The KeyStore is generally password-protected.239* <P>240* For more flexible initialization, please see241* {@link #init(ManagerFactoryParameters)}.242* <P>243*244* @param ks the key store or null245* @param password the password for recovering keys in the KeyStore246* @throws KeyStoreException if this operation fails247* @throws NoSuchAlgorithmException if the specified algorithm is not248* available from the specified provider.249* @throws UnrecoverableKeyException if the key cannot be recovered250* (e.g. the given password is wrong).251*/252public final void init(KeyStore ks, char[] password) throws253KeyStoreException, NoSuchAlgorithmException,254UnrecoverableKeyException {255factorySpi.engineInit(ks, password);256}257258259/**260* Initializes this factory with a source of provider-specific261* key material.262* <P>263* In some cases, initialization parameters other than a keystore264* and password may be needed by a provider. Users of that265* particular provider are expected to pass an implementation of266* the appropriate <CODE>ManagerFactoryParameters</CODE> as267* defined by the provider. The provider can then call the268* specified methods in the <CODE>ManagerFactoryParameters</CODE>269* implementation to obtain the needed information.270*271* @param spec an implementation of a provider-specific parameter272* specification273* @throws InvalidAlgorithmParameterException if an error is encountered274*/275public final void init(ManagerFactoryParameters spec) throws276InvalidAlgorithmParameterException {277factorySpi.engineInit(spec);278}279280281/**282* Returns one key manager for each type of key material.283*284* @return the key managers285* @throws IllegalStateException if the KeyManagerFactory is not initialized286*/287public final KeyManager[] getKeyManagers() {288return factorySpi.engineGetKeyManagers();289}290}291292293