J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2000-2006 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | /* |
| 27 | * NOTE: this file was copied from javax.net.ssl.KeyManagerFactory |
| 28 | */ |
| 29 | |
| 30 | package com.sun.net.ssl; |
| 31 | |
| 32 | import 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 |
| 44 | public 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 | * "sun.ssl.keymanager.type" security property (in the Java security |
| 57 | * properties file) to the desired name. |
| 58 | * |
| 59 | * @return the default type as specified in the |
| 60 | * Java security properties file, or an implementation-specific default |
| 61 | * if no such property exists. |
| 62 | */ |
| 63 | public final static String getDefaultAlgorithm() { |
| 64 | String type; |
| 65 | type = AccessController.doPrivileged(new PrivilegedAction<String>() { |
| 66 | public String run() { |
| 67 | return Security.getProperty("sun.ssl.keymanager.type"); |
| 68 | } |
| 69 | }); |
| 70 | if (type == null) { |
| 71 | type = "SunX509"; |
| 72 | } |
| 73 | return type; |
| 74 | |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Creates a KeyManagerFactory object. |
| 79 | * |
| 80 | * @param factorySpi the delegate |
| 81 | * @param provider the provider |
| 82 | * @param algorithm the algorithm |
| 83 | */ |
| 84 | protected KeyManagerFactory(KeyManagerFactorySpi factorySpi, |
| 85 | Provider provider, String algorithm) { |
| 86 | this.factorySpi = factorySpi; |
| 87 | this.provider = provider; |
| 88 | this.algorithm = algorithm; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Returns the algorithm name of this <code>KeyManagerFactory</code> object. |
| 93 | * |
| 94 | * <p>This is the same name that was specified in one of the |
| 95 | * <code>getInstance</code> calls that created this |
| 96 | * <code>KeyManagerFactory</code> object. |
| 97 | * |
| 98 | * @return the algorithm name of this <code>KeyManagerFactory</code> object. |
| 99 | */ |
| 100 | public final String getAlgorithm() { |
| 101 | return this.algorithm; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Generates a <code>KeyManagerFactory</code> object that implements the |
| 106 | * specified key management algorithm. |
| 107 | * If the default provider package provides an implementation of the |
| 108 | * requested key management algorithm, an instance of |
| 109 | * <code>KeyManagerFactory</code> containing that implementation is |
| 110 | * returned. If the algorithm is not available in the default provider |
| 111 | * package, other provider packages are searched. |
| 112 | * |
| 113 | * @param algorithm the standard name of the requested |
| 114 | * algorithm. |
| 115 | * |
| 116 | * @return the new <code>KeyManagerFactory</code> object |
| 117 | * |
| 118 | * @exception NoSuchAlgorithmException if the specified algorithm is not |
| 119 | * available in the default provider package or any of the other provider |
| 120 | * packages that were searched. |
| 121 | */ |
| 122 | public static final KeyManagerFactory getInstance(String algorithm) |
| 123 | throws NoSuchAlgorithmException |
| 124 | { |
| 125 | try { |
| 126 | Object[] objs = SSLSecurity.getImpl(algorithm, "KeyManagerFactory", |
| 127 | (String) null); |
| 128 | return new KeyManagerFactory((KeyManagerFactorySpi)objs[0], |
| 129 | (Provider)objs[1], |
| 130 | algorithm); |
| 131 | } catch (NoSuchProviderException e) { |
| 132 | throw new NoSuchAlgorithmException(algorithm + " not found"); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Generates a <code>KeyManagerFactory</code> object for the specified |
| 138 | * key management algorithm from the specified provider. |
| 139 | * |
| 140 | * @param algorithm the standard name of the requested |
| 141 | * algorithm. |
| 142 | * @param provider the name of the provider |
| 143 | * |
| 144 | * @return the new <code>KeyManagerFactory</code> object |
| 145 | * |
| 146 | * @exception NoSuchAlgorithmException if the specified algorithm is not |
| 147 | * available from the specified provider. |
| 148 | * @exception NoSuchProviderException if the specified provider has not |
| 149 | * been configured. |
| 150 | */ |
| 151 | public static final KeyManagerFactory getInstance(String algorithm, |
| 152 | String provider) |
| 153 | throws NoSuchAlgorithmException, NoSuchProviderException |
| 154 | { |
| 155 | if (provider == null || provider.length() == 0) |
| 156 | throw new IllegalArgumentException("missing provider"); |
| 157 | Object[] objs = SSLSecurity.getImpl(algorithm, "KeyManagerFactory", |
| 158 | provider); |
| 159 | return new KeyManagerFactory((KeyManagerFactorySpi)objs[0], |
| 160 | (Provider)objs[1], algorithm); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Generates a <code>KeyManagerFactory</code> object for the specified |
| 165 | * key management algorithm from the specified provider. |
| 166 | * |
| 167 | * @param algorithm the standard name of the requested |
| 168 | * algorithm. |
| 169 | * @param provider an instance of the provider |
| 170 | * |
| 171 | * @return the new <code>KeyManagerFactory</code> object |
| 172 | * |
| 173 | * @exception NoSuchAlgorithmException if the specified algorithm is not |
| 174 | * available from the specified provider. |
| 175 | */ |
| 176 | public static final KeyManagerFactory getInstance(String algorithm, |
| 177 | Provider provider) |
| 178 | throws NoSuchAlgorithmException |
| 179 | { |
| 180 | if (provider == null) |
| 181 | throw new IllegalArgumentException("missing provider"); |
| 182 | Object[] objs = SSLSecurity.getImpl(algorithm, "KeyManagerFactory", |
| 183 | provider); |
| 184 | return new KeyManagerFactory((KeyManagerFactorySpi)objs[0], |
| 185 | (Provider)objs[1], algorithm); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Returns the provider of this <code>KeyManagerFactory</code> object. |
| 190 | * |
| 191 | * @return the provider of this <code>KeyManagerFactory</code> object |
| 192 | */ |
| 193 | public final Provider getProvider() { |
| 194 | return this.provider; |
| 195 | } |
| 196 | |
| 197 | |
| 198 | /** |
| 199 | * Initializes this factory with a source of key material. The |
| 200 | * provider may also include a provider-specific source |
| 201 | * of key material. |
| 202 | * |
| 203 | * @param ks the key store or null |
| 204 | * @param password the password for recovering keys |
| 205 | */ |
| 206 | public void init(KeyStore ks, char[] password) |
| 207 | throws KeyStoreException, NoSuchAlgorithmException, |
| 208 | UnrecoverableKeyException { |
| 209 | factorySpi.engineInit(ks, password); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Returns one key manager for each type of key material. |
| 214 | * @return the key managers |
| 215 | */ |
| 216 | public KeyManager[] getKeyManagers() { |
| 217 | return factorySpi.engineGetKeyManagers(); |
| 218 | } |
| 219 | } |