0N/A/*
2362N/A * Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.net.ssl;
0N/A
0N/Aimport java.security.Security;
0N/Aimport java.security.*;
0N/A
0N/Aimport sun.security.jca.GetInstance;
0N/A
0N/A/**
0N/A * This class acts as a factory for key managers based on a
0N/A * source of key material. Each key manager manages a specific
0N/A * type of key material for use by secure sockets. The key
0N/A * material is based on a KeyStore and/or provider specific sources.
0N/A *
0N/A * @since 1.4
0N/A * @see KeyManager
0N/A */
0N/Apublic class KeyManagerFactory {
0N/A // The provider
0N/A private Provider provider;
0N/A
0N/A // The provider implementation (delegate)
0N/A private KeyManagerFactorySpi factorySpi;
0N/A
0N/A // The name of the key management algorithm.
0N/A private String algorithm;
0N/A
0N/A /**
0N/A * Obtains the default KeyManagerFactory algorithm name.
0N/A *
0N/A * <p>The default algorithm can be changed at runtime by setting
0N/A * the value of the "ssl.KeyManagerFactory.algorithm" security
0N/A * property (set in the Java security properties file or by calling
0N/A * {@link java.security.Security#setProperty(java.lang.String,
0N/A * java.lang.String)})
0N/A * to the desired algorithm name.
0N/A *
0N/A * @see java.security.Security#setProperty(java.lang.String,
0N/A * java.lang.String)
0N/A * @return the default algorithm name as specified in the
0N/A * Java security properties, or an implementation-specific
0N/A * default if no such property exists.
0N/A */
0N/A public final static String getDefaultAlgorithm() {
0N/A String type;
0N/A type = AccessController.doPrivileged(new PrivilegedAction<String>() {
0N/A public String run() {
0N/A return Security.getProperty(
0N/A "ssl.KeyManagerFactory.algorithm");
0N/A }
0N/A });
0N/A if (type == null) {
0N/A type = "SunX509";
0N/A }
0N/A return type;
0N/A }
0N/A
0N/A /**
0N/A * Creates a KeyManagerFactory object.
0N/A *
0N/A * @param factorySpi the delegate
0N/A * @param provider the provider
0N/A * @param algorithm the algorithm
0N/A */
0N/A protected KeyManagerFactory(KeyManagerFactorySpi factorySpi,
0N/A Provider provider, String algorithm) {
0N/A this.factorySpi = factorySpi;
0N/A this.provider = provider;
0N/A this.algorithm = algorithm;
0N/A }
0N/A
0N/A /**
0N/A * Returns the algorithm name of this <code>KeyManagerFactory</code> object.
0N/A *
0N/A * <p>This is the same name that was specified in one of the
0N/A * <code>getInstance</code> calls that created this
0N/A * <code>KeyManagerFactory</code> object.
0N/A *
0N/A * @return the algorithm name of this <code>KeyManagerFactory</code> object.
0N/A */
0N/A public final String getAlgorithm() {
0N/A return this.algorithm;
0N/A }
0N/A
0N/A /**
0N/A * Returns a <code>KeyManagerFactory</code> object that acts as a
0N/A * factory for key managers.
0N/A *
0N/A * <p> This method traverses the list of registered security Providers,
0N/A * starting with the most preferred Provider.
0N/A * A new KeyManagerFactory object encapsulating the
0N/A * KeyManagerFactorySpi implementation from the first
0N/A * Provider that supports the specified algorithm is returned.
0N/A *
0N/A * <p> Note that the list of registered providers may be retrieved via
0N/A * the {@link Security#getProviders() Security.getProviders()} method.
0N/A *
0N/A * @param algorithm the standard name of the requested algorithm.
0N/A * See the <a href=
0N/A * "{@docRoot}/../technotes/guides/security/jsse/JSSERefGuide.html">
0N/A * Java Secure Socket Extension Reference Guide </a>
0N/A * for information about standard algorithm names.
0N/A *
0N/A * @return the new <code>KeyManagerFactory</code> object.
0N/A *
0N/A * @exception NoSuchAlgorithmException if no Provider supports a
0N/A * KeyManagerFactorySpi implementation for the
0N/A * specified algorithm.
0N/A * @exception NullPointerException if <code>algorithm</code> is null.
0N/A *
0N/A * @see java.security.Provider
0N/A */
0N/A public static final KeyManagerFactory getInstance(String algorithm)
0N/A throws NoSuchAlgorithmException {
0N/A GetInstance.Instance instance = GetInstance.getInstance
0N/A ("KeyManagerFactory", KeyManagerFactorySpi.class,
0N/A algorithm);
0N/A return new KeyManagerFactory((KeyManagerFactorySpi)instance.impl,
0N/A instance.provider, algorithm);
0N/A }
0N/A
0N/A /**
0N/A * Returns a <code>KeyManagerFactory</code> object that acts as a
0N/A * factory for key managers.
0N/A *
0N/A * <p> A new KeyManagerFactory object encapsulating the
0N/A * KeyManagerFactorySpi implementation from the specified provider
0N/A * is returned. The specified provider must be registered
0N/A * in the security provider list.
0N/A *
0N/A * <p> Note that the list of registered providers may be retrieved via
0N/A * the {@link Security#getProviders() Security.getProviders()} method.
0N/A
0N/A * @param algorithm the standard name of the requested algorithm.
0N/A * See the <a href=
0N/A * "{@docRoot}/../technotes/guides/security/jsse/JSSERefGuide.html">
0N/A * Java Secure Socket Extension Reference Guide </a>
0N/A * for information about standard algorithm names.
0N/A *
0N/A * @param provider the name of the provider.
0N/A *
0N/A * @return the new <code>KeyManagerFactory</code> object.
0N/A *
0N/A * @throws NoSuchAlgorithmException if a KeyManagerFactorySpi
0N/A * implementation for the specified algorithm is not
0N/A * available from the specified provider.
0N/A *
0N/A * @throws NoSuchProviderException if the specified provider is not
0N/A * registered in the security provider list.
0N/A *
0N/A * @throws IllegalArgumentException if the provider name is null or empty.
0N/A * @throws NullPointerException if <code>algorithm</code> is null.
0N/A *
0N/A * @see java.security.Provider
0N/A */
0N/A public static final KeyManagerFactory getInstance(String algorithm,
0N/A String provider) throws NoSuchAlgorithmException,
0N/A NoSuchProviderException {
0N/A GetInstance.Instance instance = GetInstance.getInstance
0N/A ("KeyManagerFactory", KeyManagerFactorySpi.class,
0N/A algorithm, provider);
0N/A return new KeyManagerFactory((KeyManagerFactorySpi)instance.impl,
0N/A instance.provider, algorithm);
0N/A }
0N/A
0N/A /**
0N/A * Returns a <code>KeyManagerFactory</code> object that acts as a
0N/A * factory for key managers.
0N/A *
0N/A * <p> A new KeyManagerFactory object encapsulating the
0N/A * KeyManagerFactorySpi implementation from the specified Provider
0N/A * object is returned. Note that the specified Provider object
0N/A * does not have to be registered in the provider list.
0N/A *
0N/A * @param algorithm the standard name of the requested algorithm.
0N/A * See the <a href=
0N/A * "{@docRoot}/../technotes/guides/security/jsse/JSSERefGuide.html">
0N/A * Java Secure Socket Extension Reference Guide </a>
0N/A * for information about standard algorithm names.
0N/A *
0N/A * @param provider an instance of the provider.
0N/A *
0N/A * @return the new <code>KeyManagerFactory</code> object.
0N/A *
0N/A * @throws NoSuchAlgorithmException if a KeyManagerFactorySpi
0N/A * implementation for the specified algorithm is not available
0N/A * from the specified Provider object.
0N/A *
0N/A * @throws IllegalArgumentException if provider is null.
0N/A * @throws NullPointerException if <code>algorithm</code> is null.
0N/A *
0N/A * @see java.security.Provider
0N/A */
0N/A public static final KeyManagerFactory getInstance(String algorithm,
0N/A Provider provider) throws NoSuchAlgorithmException {
0N/A GetInstance.Instance instance = GetInstance.getInstance
0N/A ("KeyManagerFactory", KeyManagerFactorySpi.class,
0N/A algorithm, provider);
0N/A return new KeyManagerFactory((KeyManagerFactorySpi)instance.impl,
0N/A instance.provider, algorithm);
0N/A }
0N/A
0N/A /**
0N/A * Returns the provider of this <code>KeyManagerFactory</code> object.
0N/A *
0N/A * @return the provider of this <code>KeyManagerFactory</code> object
0N/A */
0N/A public final Provider getProvider() {
0N/A return this.provider;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Initializes this factory with a source of key material.
0N/A * <P>
0N/A * The provider typically uses a KeyStore for obtaining
0N/A * key material for use during secure socket negotiations.
0N/A * The KeyStore is generally password-protected.
0N/A * <P>
0N/A * For more flexible initialization, please see
0N/A * {@link #init(ManagerFactoryParameters)}.
0N/A * <P>
0N/A *
0N/A * @param ks the key store or null
0N/A * @param password the password for recovering keys in the KeyStore
0N/A * @throws KeyStoreException if this operation fails
0N/A * @throws NoSuchAlgorithmException if the specified algorithm is not
0N/A * available from the specified provider.
0N/A * @throws UnrecoverableKeyException if the key cannot be recovered
0N/A * (e.g. the given password is wrong).
0N/A */
0N/A public final void init(KeyStore ks, char[] password) throws
0N/A KeyStoreException, NoSuchAlgorithmException,
0N/A UnrecoverableKeyException {
0N/A factorySpi.engineInit(ks, password);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Initializes this factory with a source of provider-specific
0N/A * key material.
0N/A * <P>
0N/A * In some cases, initialization parameters other than a keystore
0N/A * and password may be needed by a provider. Users of that
0N/A * particular provider are expected to pass an implementation of
0N/A * the appropriate <CODE>ManagerFactoryParameters</CODE> as
0N/A * defined by the provider. The provider can then call the
0N/A * specified methods in the <CODE>ManagerFactoryParameters</CODE>
0N/A * implementation to obtain the needed information.
0N/A *
0N/A * @param spec an implementation of a provider-specific parameter
0N/A * specification
0N/A * @throws InvalidAlgorithmParameterException if an error is encountered
0N/A */
0N/A public final void init(ManagerFactoryParameters spec) throws
0N/A InvalidAlgorithmParameterException {
0N/A factorySpi.engineInit(spec);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns one key manager for each type of key material.
0N/A *
0N/A * @return the key managers
0N/A * @throws IllegalStateException if the KeyManagerFactory is not initialized
0N/A */
0N/A public final KeyManager[] getKeyManagers() {
0N/A return factorySpi.engineGetKeyManagers();
0N/A }
0N/A}