Path: blob/master/src/java.base/share/classes/javax/net/ssl/TrustManagerFactory.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 trust managers based on a35* source of trust material. Each trust manager manages a specific36* type of trust material for use by secure sockets. The trust37* material is based on a KeyStore and/or provider-specific sources.38*39* <p> Every implementation of the Java platform is required to support the40* following standard {@code TrustManagerFactory} algorithm:41* <ul>42* <li>{@code PKIX}</li>43* </ul>44* This algorithm is described in the <a href=45* "{@docRoot}/../specs/security/standard-names.html#trustmanagerfactory-algorithms">46* TrustManagerFactory section</a> of the47* Java Security Standard Algorithm Names Specification.48* Consult the release documentation for your implementation to see if any49* other algorithms are supported.50*51* @since 1.452* @see TrustManager53*/54public class TrustManagerFactory {55// The provider56private Provider provider;5758// The provider implementation (delegate)59private TrustManagerFactorySpi factorySpi;6061// The name of the trust management algorithm.62private String algorithm;6364/**65* Obtains the default TrustManagerFactory algorithm name.66*67* <p>The default TrustManager can be changed at runtime by setting68* the value of the {@code ssl.TrustManagerFactory.algorithm}69* security property to the desired algorithm name.70*71* @see java.security.Security security properties72* @return the default algorithm name as specified by the73* {@code ssl.TrustManagerFactory.algorithm} security property, or an74* implementation-specific default if no such property exists.75*/76@SuppressWarnings("removal")77public static final String getDefaultAlgorithm() {78String type;79type = AccessController.doPrivileged(new PrivilegedAction<>() {80@Override81public String run() {82return Security.getProperty(83"ssl.TrustManagerFactory.algorithm");84}85});86if (type == null) {87type = "SunX509";88}89return type;90}9192/**93* Creates a TrustManagerFactory object.94*95* @param factorySpi the delegate96* @param provider the provider97* @param algorithm the algorithm98*/99protected TrustManagerFactory(TrustManagerFactorySpi factorySpi,100Provider provider, String algorithm) {101this.factorySpi = factorySpi;102this.provider = provider;103this.algorithm = algorithm;104}105106/**107* Returns the algorithm name of this <code>TrustManagerFactory</code>108* object.109*110* <p>This is the same name that was specified in one of the111* <code>getInstance</code> calls that created this112* <code>TrustManagerFactory</code> object.113*114* @return the algorithm name of this <code>TrustManagerFactory</code>115* object116*/117public final String getAlgorithm() {118return this.algorithm;119}120121/**122* Returns a <code>TrustManagerFactory</code> object that acts as a123* factory for trust managers.124*125* <p> This method traverses the list of registered security Providers,126* starting with the most preferred Provider.127* A new TrustManagerFactory object encapsulating the128* TrustManagerFactorySpi implementation from the first129* Provider that supports the specified algorithm is returned.130*131* <p> Note that the list of registered providers may be retrieved via132* the {@link Security#getProviders() Security.getProviders()} method.133*134* @implNote135* The JDK Reference Implementation additionally uses the136* {@code jdk.security.provider.preferred}137* {@link Security#getProperty(String) Security} property to determine138* the preferred provider order for the specified algorithm. This139* may be different than the order of providers returned by140* {@link Security#getProviders() Security.getProviders()}.141*142* @param algorithm the standard name of the requested trust management143* algorithm. See the <a href=144* "{@docRoot}/../specs/security/standard-names.html#trustmanagerfactory-algorithms">145* TrustManagerFactory section</a> in the Java Security Standard146* Algorithm Names Specification for information about standard147* algorithm names.148*149* @return the new {@code TrustManagerFactory} object150*151* @throws NoSuchAlgorithmException if no {@code Provider} supports a152* {@code TrustManagerFactorySpi} implementation for the153* specified algorithm154*155* @throws NullPointerException if {@code algorithm} is {@code null}156*157* @see java.security.Provider158*/159public static final TrustManagerFactory getInstance(String algorithm)160throws NoSuchAlgorithmException {161Objects.requireNonNull(algorithm, "null algorithm name");162GetInstance.Instance instance = GetInstance.getInstance163("TrustManagerFactory", TrustManagerFactorySpi.class,164algorithm);165return new TrustManagerFactory((TrustManagerFactorySpi)instance.impl,166instance.provider, algorithm);167}168169/**170* Returns a <code>TrustManagerFactory</code> object that acts as a171* factory for trust managers.172*173* <p> A new KeyManagerFactory object encapsulating the174* KeyManagerFactorySpi implementation from the specified provider175* is returned. The specified provider must be registered176* in the security provider list.177*178* <p> Note that the list of registered providers may be retrieved via179* the {@link Security#getProviders() Security.getProviders()} method.180*181* @param algorithm the standard name of the requested trust management182* algorithm. See the <a href=183* "{@docRoot}/../specs/security/standard-names.html#trustmanagerfactory-algorithms">184* TrustManagerFactory section</a> in the Java Security Standard185* Algorithm Names Specification for information about standard186* algorithm names.187*188* @param provider the name of the provider.189*190* @return the new {@code TrustManagerFactory} object191*192* @throws IllegalArgumentException if the provider name is193* {@code null} or empty194*195* @throws NoSuchAlgorithmException if a {@code TrustManagerFactorySpi}196* implementation for the specified algorithm is not197* available from the specified provider198*199* @throws NoSuchProviderException if the specified provider is not200* registered in the security provider list201*202* @throws NullPointerException if {@code algorithm} is {@code null}203*204* @see java.security.Provider205*/206public static final TrustManagerFactory getInstance(String algorithm,207String provider) throws NoSuchAlgorithmException,208NoSuchProviderException {209Objects.requireNonNull(algorithm, "null algorithm name");210GetInstance.Instance instance = GetInstance.getInstance211("TrustManagerFactory", TrustManagerFactorySpi.class,212algorithm, provider);213return new TrustManagerFactory((TrustManagerFactorySpi)instance.impl,214instance.provider, algorithm);215}216217/**218* Returns a <code>TrustManagerFactory</code> object that acts as a219* factory for trust managers.220*221* <p> A new TrustManagerFactory object encapsulating the222* TrustManagerFactorySpi implementation from the specified Provider223* object is returned. Note that the specified Provider object224* does not have to be registered in the provider list.225*226* @param algorithm the standard name of the requested trust management227* algorithm. See the <a href=228* "{@docRoot}/../specs/security/standard-names.html#trustmanagerfactory-algorithms">229* TrustManagerFactory section</a> in the Java Security Standard230* Algorithm Names Specification for information about standard231* algorithm names.232*233* @param provider an instance of the provider.234*235* @return the new {@code TrustManagerFactory} object236*237* @throws IllegalArgumentException if the provider is {@code null}238*239* @throws NoSuchAlgorithmException if a {@code TrustManagerFactorySpi}240* implementation for the specified algorithm is not available241* from the specified {@code Provider} object242*243* @throws NullPointerException if {@code algorithm} is {@code null}244*245* @see java.security.Provider246*/247public static final TrustManagerFactory getInstance(String algorithm,248Provider provider) throws NoSuchAlgorithmException {249Objects.requireNonNull(algorithm, "null algorithm name");250GetInstance.Instance instance = GetInstance.getInstance251("TrustManagerFactory", TrustManagerFactorySpi.class,252algorithm, provider);253return new TrustManagerFactory((TrustManagerFactorySpi)instance.impl,254instance.provider, algorithm);255}256257/**258* Returns the provider of this <code>TrustManagerFactory</code> object.259*260* @return the provider of this <code>TrustManagerFactory</code> object261*/262public final Provider getProvider() {263return this.provider;264}265266267/**268* Initializes this factory with a source of certificate269* authorities and related trust material.270* <P>271* The provider typically uses a KeyStore as a basis for making272* trust decisions.273* <P>274* For more flexible initialization, please see275* {@link #init(ManagerFactoryParameters)}.276*277* @param ks the key store, or null278* @throws KeyStoreException if this operation fails279*/280public final void init(KeyStore ks) throws KeyStoreException {281factorySpi.engineInit(ks);282}283284285/**286* Initializes this factory with a source of provider-specific287* trust material.288* <P>289* In some cases, initialization parameters other than a keystore290* may be needed by a provider. Users of that particular provider291* are expected to pass an implementation of the appropriate292* <CODE>ManagerFactoryParameters</CODE> as defined by the293* provider. The provider can then call the specified methods in294* the <CODE>ManagerFactoryParameters</CODE> implementation to obtain the295* needed information.296*297* @param spec an implementation of a provider-specific parameter298* specification299* @throws InvalidAlgorithmParameterException if an error is300* encountered301*/302public final void init(ManagerFactoryParameters spec) throws303InvalidAlgorithmParameterException {304factorySpi.engineInit(spec);305}306307308/**309* Returns one trust manager for each type of trust material.310*311* @throws IllegalStateException if the factory is not initialized.312*313* @return the trust managers314*/315public final TrustManager[] getTrustManagers() {316return factorySpi.engineGetTrustManagers();317}318}319320321