1/*
2 * Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26/*
27 * NOTE:  this file was copied from javax.net.ssl.TrustManagerFactory
28 */
29
30package com.sun.net.ssl;
31
32import java.security.*;
33
34/**
35 * This class acts as a factory for trust managers based on a
36 * source of trust material. Each trust manager manages a specific
37 * type of trust material for use by secure sockets. The trust
38 * material is based on a KeyStore and/or provider specific sources.
39 *
40 * @deprecated As of JDK 1.4, this implementation-specific class was
41 *      replaced by {@link javax.net.ssl.TrustManagerFactory}.
42 */
43@Deprecated(since="1.4")
44public class TrustManagerFactory {
45    // The provider
46    private Provider provider;
47
48    // The provider implementation (delegate)
49    private TrustManagerFactorySpi factorySpi;
50
51    // The name of the trust management algorithm.
52    private String algorithm;
53
54    /**
55     * <p>The default TrustManager can be changed by setting the value of the
56     * {@code sun.ssl.trustmanager.type} security property to the desired name.
57     *
58     * @return the default type as specified by the
59     * {@code sun.ssl.trustmanager.type} security property, or an
60     * implementation-specific default if no such property exists.
61     *
62     * @see java.security.Security security properties
63     */
64    public static final String getDefaultAlgorithm() {
65        String type;
66        type = AccessController.doPrivileged(new PrivilegedAction<>() {
67            public String run() {
68                return Security.getProperty("sun.ssl.trustmanager.type");
69            }
70        });
71        if (type == null) {
72            type = "SunX509";
73        }
74        return type;
75
76    }
77
78    /**
79     * Creates a TrustManagerFactory object.
80     *
81     * @param factorySpi the delegate
82     * @param provider the provider
83     * @param algorithm the algorithm
84     */
85    protected TrustManagerFactory(TrustManagerFactorySpi factorySpi,
86            Provider provider, String algorithm) {
87        this.factorySpi = factorySpi;
88        this.provider = provider;
89        this.algorithm = algorithm;
90    }
91
92    /**
93     * Returns the algorithm name of this <code>TrustManagerFactory</code>
94     * object.
95     *
96     * <p>This is the same name that was specified in one of the
97     * <code>getInstance</code> calls that created this
98     * <code>TrustManagerFactory</code> object.
99     *
100     * @return the algorithm name of this <code>TrustManagerFactory</code>
101     * object.
102     */
103    public final String getAlgorithm() {
104        return this.algorithm;
105    }
106
107    /**
108     * Generates a <code>TrustManagerFactory</code> object that implements the
109     * specified trust management algorithm.
110     * If the default provider package provides an implementation of the
111     * requested trust management algorithm, an instance of
112     * <code>TrustManagerFactory</code> containing that implementation is
113     * returned.  If the algorithm is not available in the default provider
114     * package, other provider packages are searched.
115     *
116     * @param algorithm the standard name of the requested trust management
117     * algorithm.
118     *
119     * @return the new <code>TrustManagerFactory</code> object
120     *
121     * @exception NoSuchAlgorithmException if the specified algorithm is not
122     * available in the default provider package or any of the other provider
123     * packages that were searched.
124     */
125    public static final TrustManagerFactory getInstance(String algorithm)
126        throws NoSuchAlgorithmException
127    {
128        try {
129            Object[] objs = SSLSecurity.getImpl(algorithm,
130                "TrustManagerFactory", (String) null);
131            return new TrustManagerFactory((TrustManagerFactorySpi)objs[0],
132                                    (Provider)objs[1],
133                                    algorithm);
134        } catch (NoSuchProviderException e) {
135            throw new NoSuchAlgorithmException(algorithm + " not found");
136        }
137    }
138
139    /**
140     * Generates a <code>TrustManagerFactory</code> object for the specified
141     * trust management algorithm from the specified provider.
142     *
143     * @param algorithm the standard name of the requested trust management
144     * algorithm.
145     * @param provider the name of the provider
146     *
147     * @return the new <code>TrustManagerFactory</code> object
148     *
149     * @exception NoSuchAlgorithmException if the specified algorithm is not
150     * available from the specified provider.
151     * @exception NoSuchProviderException if the specified provider has not
152     * been configured.
153     */
154    public static final TrustManagerFactory getInstance(String algorithm,
155                                                 String provider)
156        throws NoSuchAlgorithmException, NoSuchProviderException
157    {
158        if (provider == null || provider.length() == 0)
159            throw new IllegalArgumentException("missing provider");
160        Object[] objs = SSLSecurity.getImpl(algorithm, "TrustManagerFactory",
161                                            provider);
162        return new TrustManagerFactory((TrustManagerFactorySpi)objs[0],
163            (Provider)objs[1], algorithm);
164    }
165
166    /**
167     * Generates a <code>TrustManagerFactory</code> object for the specified
168     * trust management algorithm from the specified provider.
169     *
170     * @param algorithm the standard name of the requested trust management
171     * algorithm.
172     * @param provider an instance of the provider
173     *
174     * @return the new <code>TrustManagerFactory</code> object
175     *
176     * @exception NoSuchAlgorithmException if the specified algorithm is not
177     * available from the specified provider.
178     */
179    public static final TrustManagerFactory getInstance(String algorithm,
180                                                 Provider provider)
181        throws NoSuchAlgorithmException
182    {
183        if (provider == null)
184            throw new IllegalArgumentException("missing provider");
185        Object[] objs = SSLSecurity.getImpl(algorithm, "TrustManagerFactory",
186                                            provider);
187        return new TrustManagerFactory((TrustManagerFactorySpi)objs[0],
188            (Provider)objs[1], algorithm);
189    }
190
191    /**
192     * Returns the provider of this <code>TrustManagerFactory</code> object.
193     *
194     * @return the provider of this <code>TrustManagerFactory</code> object
195     */
196    public final Provider getProvider() {
197        return this.provider;
198    }
199
200
201    /**
202     * Initializes this factory with a source of certificate
203     * authorities and related trust material. The
204     * provider may also include a provider-specific source
205     * of key material.
206     *
207     * @param ks the key store or null
208     */
209    public void init(KeyStore ks) throws KeyStoreException {
210        factorySpi.engineInit(ks);
211    }
212
213    /**
214     * Returns one trust manager for each type of trust material.
215     * @return the trust managers
216     */
217    public TrustManager[] getTrustManagers() {
218        return factorySpi.engineGetTrustManagers();
219    }
220}
221