0N/A/*
2362N/A * Copyright (c) 1999, 2008, 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 trust managers based on a
0N/A * source of trust material. Each trust manager manages a specific
0N/A * type of trust material for use by secure sockets. The trust
0N/A * material is based on a KeyStore and/or provider specific sources.
0N/A *
0N/A * @since 1.4
0N/A * @see TrustManager
0N/A */
0N/Apublic class TrustManagerFactory {
0N/A // The provider
0N/A private Provider provider;
0N/A
0N/A // The provider implementation (delegate)
0N/A private TrustManagerFactorySpi factorySpi;
0N/A
0N/A // The name of the trust management algorithm.
0N/A private String algorithm;
0N/A
0N/A /**
0N/A * Obtains the default TrustManagerFactory algorithm name.
0N/A *
0N/A * <p>The default TrustManager can be changed at runtime by setting
0N/A * the value of the "ssl.TrustManagerFactory.algorithm" security
0N/A * property (set in the Java security properties file or by calling
0N/A * {@link java.security.Security#setProperty(String, String) })
0N/A * to the desired algorithm name.
0N/A *
0N/A * @return the default algorithm name as specified in the
0N/A * Java security properties, or an implementation-specific default
0N/A * 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.TrustManagerFactory.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 TrustManagerFactory 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 TrustManagerFactory(TrustManagerFactorySpi 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>TrustManagerFactory</code>
0N/A * 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>TrustManagerFactory</code> object.
0N/A *
0N/A * @return the algorithm name of this <code>TrustManagerFactory</code>
0N/A * 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>TrustManagerFactory</code> object that acts as a
0N/A * factory for trust 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 TrustManagerFactory object encapsulating the
0N/A * TrustManagerFactorySpi 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 trust management
0N/A * algorithm. 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>TrustManagerFactory</code> object.
0N/A *
0N/A * @exception NoSuchAlgorithmException if no Provider supports a
0N/A * TrustManagerFactorySpi implementation for the
0N/A * specified algorithm.
251N/A * @exception NullPointerException if algorithm is null.
0N/A *
0N/A * @see java.security.Provider
0N/A */
0N/A public static final TrustManagerFactory getInstance(String algorithm)
0N/A throws NoSuchAlgorithmException {
0N/A GetInstance.Instance instance = GetInstance.getInstance
0N/A ("TrustManagerFactory", TrustManagerFactorySpi.class,
0N/A algorithm);
0N/A return new TrustManagerFactory((TrustManagerFactorySpi)instance.impl,
0N/A instance.provider, algorithm);
0N/A }
0N/A
0N/A /**
0N/A * Returns a <code>TrustManagerFactory</code> object that acts as a
0N/A * factory for trust 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 trust management
0N/A * algorithm. 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>TrustManagerFactory</code> object
0N/A *
0N/A * @throws NoSuchAlgorithmException if a TrustManagerFactorySpi
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.
251N/A * @throws NullPointerException if algorithm is null.
0N/A *
0N/A * @see java.security.Provider
0N/A */
0N/A public static final TrustManagerFactory getInstance(String algorithm,
0N/A String provider) throws NoSuchAlgorithmException,
0N/A NoSuchProviderException {
0N/A GetInstance.Instance instance = GetInstance.getInstance
0N/A ("TrustManagerFactory", TrustManagerFactorySpi.class,
0N/A algorithm, provider);
0N/A return new TrustManagerFactory((TrustManagerFactorySpi)instance.impl,
0N/A instance.provider, algorithm);
0N/A }
0N/A
0N/A /**
0N/A * Returns a <code>TrustManagerFactory</code> object that acts as a
0N/A * factory for trust managers.
0N/A *
0N/A * <p> A new TrustManagerFactory object encapsulating the
0N/A * TrustManagerFactorySpi 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 trust management
0N/A * algorithm. 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>TrustManagerFactory</code> object.
0N/A *
0N/A * @throws NoSuchAlgorithmException if a TrustManagerFactorySpi
0N/A * implementation for the specified algorithm is not available
0N/A * from the specified Provider object.
0N/A *
0N/A * @throws IllegalArgumentException if the provider is null.
251N/A * @throws NullPointerException if algorithm is null.
0N/A *
0N/A * @see java.security.Provider
0N/A */
0N/A public static final TrustManagerFactory getInstance(String algorithm,
0N/A Provider provider) throws NoSuchAlgorithmException {
0N/A GetInstance.Instance instance = GetInstance.getInstance
0N/A ("TrustManagerFactory", TrustManagerFactorySpi.class,
0N/A algorithm, provider);
0N/A return new TrustManagerFactory((TrustManagerFactorySpi)instance.impl,
0N/A instance.provider, algorithm);
0N/A }
0N/A
0N/A /**
0N/A * Returns the provider of this <code>TrustManagerFactory</code> object.
0N/A *
0N/A * @return the provider of this <code>TrustManagerFactory</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 certificate
0N/A * authorities and related trust material.
0N/A * <P>
0N/A * The provider typically uses a KeyStore as a basis for making
0N/A * trust decisions.
0N/A * <P>
0N/A * For more flexible initialization, please see
0N/A * {@link #init(ManagerFactoryParameters)}.
0N/A *
0N/A * @param ks the key store, or null
0N/A * @throws KeyStoreException if this operation fails
0N/A */
0N/A public final void init(KeyStore ks) throws KeyStoreException {
0N/A factorySpi.engineInit(ks);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Initializes this factory with a source of provider-specific
0N/A * trust material.
0N/A * <P>
0N/A * In some cases, initialization parameters other than a keystore
0N/A * may be needed by a provider. Users of that particular provider
0N/A * are expected to pass an implementation of the appropriate
0N/A * <CODE>ManagerFactoryParameters</CODE> as defined by the
0N/A * provider. The provider can then call the specified methods in
0N/A * the <CODE>ManagerFactoryParameters</CODE> implementation to obtain the
0N/A * 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
0N/A * 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 trust manager for each type of trust material.
0N/A *
251N/A * @throws IllegalStateException if the factory is not initialized.
251N/A *
0N/A * @return the trust managers
0N/A */
0N/A public final TrustManager[] getTrustManagers() {
0N/A return factorySpi.engineGetTrustManagers();
0N/A }
0N/A}