Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / javax / net / ssl / KeyManagerFactory.java
blob938dbcec9b687e745a00993465424a6d5f7ab720
1 /* KeyManagerFactory.java -- factory for key managers.
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.net.ssl;
41 import gnu.java.security.Engine;
43 import java.lang.reflect.InvocationTargetException;
44 import java.security.AccessController;
45 import java.security.InvalidAlgorithmParameterException;
46 import java.security.KeyStore;
47 import java.security.KeyStoreException;
48 import java.security.NoSuchAlgorithmException;
49 import java.security.NoSuchProviderException;
50 import java.security.PrivilegedAction;
51 import java.security.Provider;
52 import java.security.Security;
53 import java.security.UnrecoverableKeyException;
55 /**
56 * A class that creates key manager implementations based on a
57 * requested algorithm.
59 * @author Casey Marshall (rsdio@metastatic.org)
61 public class KeyManagerFactory
64 // Constants and fields.
65 // ------------------------------------------------------------------
67 /** The service name for key manager factories. */
68 private static final String KEY_MANAGER_FACTORY = "KeyManagerFactory";
70 /** The system default trust manager algorithm. */
71 private static final String DEFAULT_ALGORITHM = "JessieX509";
73 /** The underlying engine. */
74 private final KeyManagerFactorySpi kmfSpi;
76 /** The provider of this implementation. */
77 private final Provider provider;
79 /** The name of this algorithm. */
80 private final String algorithm;
82 // Constructor.
83 // ------------------------------------------------------------------
85 /**
86 * Create a new key manager factory.
88 * @param kmfSpi The underlying engine.
89 * @param provider The engine's provider.
90 * @param algorithm The name of this algorithm.
92 protected KeyManagerFactory(KeyManagerFactorySpi kmfSpi,
93 Provider provider, String algorithm)
95 this.kmfSpi = kmfSpi;
96 this.provider = provider;
97 this.algorithm = algorithm;
100 // Class methods.
101 // ------------------------------------------------------------------
104 * Get the default algorithm name. This value may be specified at
105 * run-time via the security property
106 * "ssl.KeyManagerFactory.algorithm". If this property is
107 * not specified, this method returns "JessieX509".
109 * @return The default key manager factory algorithm's name.
111 public static final String getDefaultAlgorithm()
113 String alg = null;
116 alg = (String) AccessController.doPrivileged(
117 new PrivilegedAction()
119 public Object run()
121 return Security.getProperty("ssl.KeyManagerFactory.algorithm");
126 catch (SecurityException se)
129 if (alg == null)
130 alg = DEFAULT_ALGORITHM;
131 return alg;
135 * Get an instance of the named key manager factory, from the first
136 * provider that implements it.
138 * @param algorithm The type of key manager factory to get.
139 * @return An appropriate implementation of that algoritm.
140 * @throws NoSuchAlgorithmException If no provider implements the
141 * requested algorithm.
143 public static final KeyManagerFactory getInstance(String algorithm)
144 throws NoSuchAlgorithmException
146 Provider[] provs = Security.getProviders();
147 for (int i = 0; i < provs.length; i++)
151 return getInstance(algorithm, provs[i]);
153 catch (NoSuchAlgorithmException ignore)
157 throw new NoSuchAlgorithmException(algorithm);
161 * Get an instance of the named key manager factory, from the named
162 * provider.
164 * @param algorithm The type of key manager factory to get.
165 * @param provider The name of the provider to get the
166 * implementation from.
167 * @return An appropriate implementation of that algorithm.
168 * @throws NoSuchAlgorithmException If the provider does not
169 * implement the requested algorithm.
170 * @throws NoSuchProviderException If the named provider does not
171 * exist.
173 public static final KeyManagerFactory getInstance(String algorithm, String provider)
174 throws NoSuchAlgorithmException, NoSuchProviderException
176 if (provider == null)
177 throw new IllegalArgumentException("provider is null");
178 Provider p = Security.getProvider(provider);
179 if (p == null)
180 throw new NoSuchProviderException(provider);
181 return getInstance(algorithm, p);
185 * Get an instance of the named key manager factory, from the given
186 * provider.
188 * @param algorithm The type of key manager factory to get.
189 * @param provider The provider to get the implementation from.
190 * @return An appropriate implementation of that algorithm.
191 * @throws NoSuchAlgorithmException If the provider does not
192 * implement the requested algorithm.
193 * @throws IllegalArgumentException If <i>provider</i> is null.
195 public static final KeyManagerFactory getInstance(String algorithm, Provider provider)
196 throws NoSuchAlgorithmException
198 if (provider == null)
199 throw new IllegalArgumentException("provider is null");
202 return new KeyManagerFactory((KeyManagerFactorySpi)
203 Engine.getInstance(KEY_MANAGER_FACTORY, algorithm, provider),
204 provider, algorithm);
206 catch (InvocationTargetException ite)
208 throw new NoSuchAlgorithmException(algorithm);
210 catch (ClassCastException cce)
212 throw new NoSuchAlgorithmException(algorithm);
216 // Instance methods.
217 // -------------------------------------------------------------------
220 * Returns the name of this key manager factory algorithm.
222 * @return The name of this key manager factory algorithm.
224 public final String getAlgorithm()
226 return algorithm;
230 * Get an array of key managers appropriate for this algorithm, with
231 * the most preferred manager first.
233 * @return The array of key managers.
235 public final KeyManager[] getKeyManagers()
237 return kmfSpi.engineGetKeyManagers();
241 * Returns the provider of this implementation.
243 * @return The provider of this implementation.
245 public final Provider getProvider()
247 return provider;
251 * Initialize this instance with an implementation-dependent
252 * parameter object.
254 * @param params The parameters to initialize with.
255 * @throws InvalidAlgorithmParameterException If the specified
256 * parameters are inappropriate.
258 public final void init(ManagerFactoryParameters params)
259 throws InvalidAlgorithmParameterException
261 kmfSpi.engineInit(params);
265 * Initialize this instance with a key store and a password for
266 * private key entries.
268 * @param store The key store to read.
269 * @param passwd The password protecting private keys in the store.
270 * @throws KeyStoreException If an error occurs reading the keys.
271 * @throws NoSuchAlgorithmException If an algorithm (such as a
272 * certificate algorithm) is not available.
273 * @throws UnrecoverableKeyException If the password is incorrect.
275 public final void init(KeyStore store, char[] passwd)
276 throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException
278 kmfSpi.engineInit(store, passwd);