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.KeyManagerFactory
28 */
29
30package com.sun.net.ssl;
31
32import java.security.*;
33
34/**
35 * This class acts as a factory for key managers based on a
36 * source of key material. Each key manager manages a specific
37 * type of key material for use by secure sockets. The key
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.KeyManagerFactory}.
42 */
43@Deprecated(since="1.4")
44public class KeyManagerFactory {
45    // The provider
46    private Provider provider;
47
48    // The provider implementation (delegate)
49    private KeyManagerFactorySpi factorySpi;
50
51    // The name of the key management algorithm.
52    private String algorithm;
53
54    /**
55     * <p>The default KeyManager can be changed by setting the value of the
56     * {@code sun.ssl.keymanager.type} security property to the desired name.
57     *
58     * @return the default type as specified by the
59     * {@code sun.ssl.keymanager.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.keymanager.type");
69            }
70        });
71        if (type == null) {
72            type = "SunX509";
73        }
74        return type;
75
76    }
77
78    /**
79     * Creates a KeyManagerFactory object.
80     *
81     * @param factorySpi the delegate
82     * @param provider the provider
83     * @param algorithm the algorithm
84     */
85    protected KeyManagerFactory(KeyManagerFactorySpi 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>KeyManagerFactory</code> object.
94     *
95     * <p>This is the same name that was specified in one of the
96     * <code>getInstance</code> calls that created this
97     * <code>KeyManagerFactory</code> object.
98     *
99     * @return the algorithm name of this <code>KeyManagerFactory</code> object.
100     */
101    public final String getAlgorithm() {
102        return this.algorithm;
103    }
104
105    /**
106     * Generates a <code>KeyManagerFactory</code> object that implements the
107     * specified key management algorithm.
108     * If the default provider package provides an implementation of the
109     * requested key management algorithm, an instance of
110     * <code>KeyManagerFactory</code> containing that implementation is
111     * returned.  If the algorithm is not available in the default provider
112     * package, other provider packages are searched.
113     *
114     * @param algorithm the standard name of the requested
115     * algorithm.
116     *
117     * @return the new <code>KeyManagerFactory</code> object
118     *
119     * @exception NoSuchAlgorithmException if the specified algorithm is not
120     * available in the default provider package or any of the other provider
121     * packages that were searched.
122     */
123    public static final KeyManagerFactory getInstance(String algorithm)
124        throws NoSuchAlgorithmException
125    {
126        try {
127            Object[] objs = SSLSecurity.getImpl(algorithm, "KeyManagerFactory",
128                                                (String) null);
129            return new KeyManagerFactory((KeyManagerFactorySpi)objs[0],
130                                    (Provider)objs[1],
131                                    algorithm);
132        } catch (NoSuchProviderException e) {
133            throw new NoSuchAlgorithmException(algorithm + " not found");
134        }
135    }
136
137    /**
138     * Generates a <code>KeyManagerFactory</code> object for the specified
139     * key management algorithm from the specified provider.
140     *
141     * @param algorithm the standard name of the requested
142     * algorithm.
143     * @param provider the name of the provider
144     *
145     * @return the new <code>KeyManagerFactory</code> object
146     *
147     * @exception NoSuchAlgorithmException if the specified algorithm is not
148     * available from the specified provider.
149     * @exception NoSuchProviderException if the specified provider has not
150     * been configured.
151     */
152    public static final KeyManagerFactory getInstance(String algorithm,
153                                                 String provider)
154        throws NoSuchAlgorithmException, NoSuchProviderException
155    {
156        if (provider == null || provider.length() == 0)
157            throw new IllegalArgumentException("missing provider");
158        Object[] objs = SSLSecurity.getImpl(algorithm, "KeyManagerFactory",
159                                            provider);
160        return new KeyManagerFactory((KeyManagerFactorySpi)objs[0],
161                                        (Provider)objs[1], algorithm);
162    }
163
164    /**
165     * Generates a <code>KeyManagerFactory</code> object for the specified
166     * key management algorithm from the specified provider.
167     *
168     * @param algorithm the standard name of the requested
169     * algorithm.
170     * @param provider an instance of the provider
171     *
172     * @return the new <code>KeyManagerFactory</code> object
173     *
174     * @exception NoSuchAlgorithmException if the specified algorithm is not
175     * available from the specified provider.
176     */
177    public static final KeyManagerFactory getInstance(String algorithm,
178                                                 Provider provider)
179        throws NoSuchAlgorithmException
180    {
181        if (provider == null)
182            throw new IllegalArgumentException("missing provider");
183        Object[] objs = SSLSecurity.getImpl(algorithm, "KeyManagerFactory",
184                                            provider);
185        return new KeyManagerFactory((KeyManagerFactorySpi)objs[0],
186                                        (Provider)objs[1], algorithm);
187    }
188
189    /**
190     * Returns the provider of this <code>KeyManagerFactory</code> object.
191     *
192     * @return the provider of this <code>KeyManagerFactory</code> object
193     */
194    public final Provider getProvider() {
195        return this.provider;
196    }
197
198
199    /**
200     * Initializes this factory with a source of key material. The
201     * provider may also include a provider-specific source
202     * of key material.
203     *
204     * @param ks the key store or null
205     * @param password the password for recovering keys
206     */
207    public void init(KeyStore ks, char[] password)
208        throws KeyStoreException, NoSuchAlgorithmException,
209            UnrecoverableKeyException {
210        factorySpi.engineInit(ks, password);
211    }
212
213    /**
214     * Returns one key manager for each type of key material.
215     * @return the key managers
216     */
217    public KeyManager[] getKeyManagers() {
218        return factorySpi.engineGetKeyManagers();
219    }
220}
221