2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / security / KeyPairGenerator.java
blobb9b07852aa426bb454a152b79a457a36f5f4bb29
1 /* KeyPairGenerator.java --- Key Pair Generator Class
2 Copyright (C) 1999, 2002, 2003 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. */
38 package java.security;
40 import java.security.spec.AlgorithmParameterSpec;
42 import gnu.java.security.Engine;
44 /**
45 * <p>The <code>KeyPairGenerator</code> class is used to generate pairs of
46 * public and private keys. Key pair generators are constructed using the
47 * <code>getInstance()</code> factory methods (static methods that return
48 * instances of a given class).</p>
50 * <p>A Key pair generator for a particular algorithm creates a public/private
51 * key pair that can be used with this algorithm. It also associates
52 * algorithm-specific parameters with each of the generated keys.</p>
54 * <p>There are two ways to generate a key pair: in an algorithm-independent
55 * manner, and in an algorithm-specific manner. The only difference between the
56 * two is the initialization of the object:</p>
58 * <ul>
59 * <li><b>Algorithm-Independent Initialization</b><br/>
60 * All key pair generators share the concepts of a <i>keysize</i> and a
61 * <i>source of randomness</i>. The <i>keysize</i> is interpreted differently
62 * for different algorithms (e.g., in the case of the <i>DSA</i> algorithm,
63 * the <i>keysize</i> corresponds to the length of the modulus). There is an
64 * <code>initialize()</code> method in this <code>KeyPairGenerator</code>
65 * class that takes these two universally shared types of arguments. There
66 * is also one that takes just a <i>keysize</i> argument, and uses the
67 * {@link SecureRandom} implementation of the highest-priority installed
68 * provider as the <i>source of randomness</i>. (If none of the installed
69 * providers supply an implementation of {@link SecureRandom}, a
70 * system-provided source of randomness is used.)<br/><br/>
72 * Since no other parameters are specified when you call the above
73 * algorithm-independent initialize methods, it is up to the provider what
74 * to do about the algorithm-specific parameters (if any) to be associated
75 * with each of the keys.<br/><br/>
77 * If the algorithm is the <i>DSA</i> algorithm, and the <i>keysize</i>
78 * (modulus size) is <code>512</code>, <code>768</code>, or <code>1024</code>,
79 * then the <b>GNU</b> provider uses a set of precomputed values for the
80 * <code>p</code>, <code>q</code>, and <code>g</code> parameters. If the
81 * <i>modulus size</i> is not one of the above values, the <b>GNU</b>
82 * provider creates a new set of parameters. Other providers might have
83 * precomputed parameter sets for more than just the three modulus sizes
84 * mentioned above. Still others might not have a list of precomputed
85 * parameters at all and instead always create new parameter sets.<br/></li>
87 * <li><b>Algorithm-Specific Initialization</b><br/>
88 * For situations where a set of algorithm-specific parameters already
89 * exists (e.g., so-called <i>community parameters</i> in <i>DSA</i>), there
90 * are two initialize methods that have an {@link AlgorithmParameterSpec}
91 * argument. One also has a {@link SecureRandom} argument, while the the
92 * other uses the {@link SecureRandom} implementation of the highest-priority
93 * installed provider as the source of randomness. (If none of the installed
94 * providers supply an implementation of {@link SecureRandom}, a
95 * system-provided source of randomness is used.)</li>
96 * </ul>
98 * <p>In case the client does not explicitly initialize the
99 * <code>KeyPairGenerator</code> (via a call to an initialize method), each
100 * provider must supply (and document) a default initialization. For example,
101 * the <b>GNU</b> provider uses a default modulus size (keysize) of
102 * <code>1024</code> bits.</p>
104 * <p>Note that this class is abstract and extends from {@link
105 * KeyPairGeneratorSpi} for historical reasons. Application developers should
106 * only take notice of the methods defined in this <code>KeyPairGenerator</code>
107 * class; all the methods in the superclass are intended for cryptographic
108 * service providers who wish to supply their own implementations of key pair
109 * generators.</p>
111 * @see Signature
112 * @see KeyPair
113 * @see AlgorithmParameterSpec
114 * @author Mark Benvenuto
115 * @author Casey Marshall
117 public abstract class KeyPairGenerator extends KeyPairGeneratorSpi
119 /** The service name for key pair generators. */
120 private static final String KEY_PAIR_GENERATOR = "KeyPairGenerator";
122 Provider provider;
123 private String algorithm;
126 * Creates a <code>KeyPairGenerator</code> object for the specified
127 * algorithm.
129 * @param algorithm the standard string name of the algorithm.
130 * See Appendix A in the Java Cryptography Architecture API
131 * Specification &amp; Reference for information about standard
132 * algorithm names.
134 protected KeyPairGenerator(String algorithm)
136 this.algorithm = algorithm;
137 this.provider = null;
141 * Returns the standard name of the algorithm for this key pair generator.
142 * See Appendix A in the Java Cryptography Architecture API Specification
143 * &amp; Reference for information about standard algorithm names.
145 * @return the standard string name of the algorithm.
147 public String getAlgorithm()
149 return algorithm;
153 * Generates a <code>KeyPairGenerator</code> object that implements the
154 * specified digest algorithm. If the default provider package provides an
155 * implementation of the requested digest algorithm, an instance of
156 * <code>KeyPairGenerator</code> containing that implementation is returned.
157 * If the algorithm is not available in the default package, other packages
158 * are searched.
160 * @param algorithm the standard string name of the algorithm. See Appendix A
161 * in the Java Cryptography Architecture API Specification &amp; Reference for
162 * information about standard algorithm names.
163 * @return the new <code>KeyPairGenerator</code> object.
164 * @throws NoSuchAlgorithmException if the algorithm is not available in the
165 * environment.
167 public static KeyPairGenerator getInstance(String algorithm)
168 throws NoSuchAlgorithmException
170 Provider[] p = Security.getProviders();
171 for (int i = 0; i < p.length; i++)
175 return getInstance(algorithm, p[i]);
177 catch (NoSuchAlgorithmException ignored) {}
180 throw new NoSuchAlgorithmException(algorithm);
184 * Generates a <code>KeyPairGenerator</code> object implementing the
185 * specified algorithm, as supplied from the specified provider, if
186 * such an algorithm is available from the provider.
188 * @param algorithm the standard string name of the algorithm. See
189 * Appendix A in the Java Cryptography Architecture API Specification
190 * &amp; Reference for information about standard algorithm names.
191 * @param provider the string name of the provider.
192 * @return the new <code>KeyPairGenerator</code> object.
193 * @throws NoSuchAlgorithmException if the algorithm is not available
194 * from the provider.
195 * @throws NoSuchProviderException if the provider is not available in the
196 * environment.
197 * @throws IllegalArgumentException if the provider name is <code>null</code>
198 * or empty.
199 * @see Provider
201 public static KeyPairGenerator getInstance(String algorithm, String provider)
202 throws NoSuchAlgorithmException, NoSuchProviderException
204 Provider p = Security.getProvider(provider);
205 if (p == null)
206 throw new NoSuchProviderException(provider);
208 return getInstance(algorithm, p);
212 * Generates a <code>KeyPairGenerator</code> object implementing the specified
213 * algorithm, as supplied from the specified provider, if such an algorithm is
214 * available from the provider. Note: the provider doesn't have to be
215 * registered.
217 * @param algorithm the standard string name of the algorithm. See Appendix A
218 * in the Java Cryptography Architecture API Specification &amp; Reference for
219 * information about standard algorithm names.
220 * @param provider the provider.
221 * @return the new <code>KeyPairGenerator</code> object.
222 * @throws NoSuchAlgorithmException if the <code>algorithm</code> is not
223 * available from the <code>provider</code>.
224 * @throws IllegalArgumentException if the <code>provider</code> is
225 * <code>null</code>.
226 * @since 1.4
227 * @see Provider
229 public static KeyPairGenerator getInstance(String algorithm,
230 Provider provider)
231 throws NoSuchAlgorithmException
233 if (provider == null)
234 throw new IllegalArgumentException("Illegal provider");
236 Object o = null;
239 o = Engine.getInstance(KEY_PAIR_GENERATOR, algorithm, provider);
241 catch (java.lang.reflect.InvocationTargetException ite)
243 throw new NoSuchAlgorithmException(algorithm);
246 KeyPairGenerator result = null;
247 if (o instanceof KeyPairGeneratorSpi)
249 result = new DummyKeyPairGenerator((KeyPairGeneratorSpi) o, algorithm);
251 else if (o instanceof KeyPairGenerator)
253 result = (KeyPairGenerator) o;
254 result.algorithm = algorithm;
256 result.provider = provider;
257 return result;
261 * Returns the provider of this key pair generator object.
263 * @return the provider of this key pair generator object.
265 public final Provider getProvider()
267 return provider;
271 * Initializes the key pair generator for a certain keysize using a default
272 * parameter set and the {@link SecureRandom} implementation of the
273 * highest-priority installed provider as the source of randomness. (If none
274 * of the installed providers supply an implementation of {@link SecureRandom},
275 * a system-provided source of randomness is used.)
277 * @param keysize the keysize. This is an algorithm-specific metric, such as
278 * modulus length, specified in number of bits.
279 * @throws InvalidParameterException if the keysize is not supported by this
280 * <code>KeyPairGenerator</code> object.
282 public void initialize(int keysize)
284 initialize(keysize, new SecureRandom());
288 * Initializes the key pair generator for a certain keysize with the given
289 * source of randomness (and a default parameter set).
291 * @param keysize the keysize. This is an algorithm-specific metric, such as
292 * modulus length, specified in number of bits.
293 * @param random the source of randomness.
294 * @throws InvalidParameterException if the <code>keysize</code> is not
295 * supported by this <code>KeyPairGenerator</code> object.
296 * @since 1.2
298 public void initialize(int keysize, SecureRandom random)
300 initialize(keysize, random);
304 * <p>Initializes the key pair generator using the specified parameter set and
305 * the {@link SecureRandom} implementation of the highest-priority installed
306 * provider as the source of randomness. (If none of the installed providers
307 * supply an implementation of {@link SecureRandom}, a system-provided source
308 * of randomness is used.)</p>
310 * <p>This concrete method has been added to this previously-defined abstract
311 * class. This method calls the
312 * {@link KeyPairGeneratorSpi#initialize(AlgorithmParameterSpec, SecureRandom)}
313 * initialize method, passing it <code>params</code> and a source of
314 * randomness (obtained from the highest-priority installed provider or
315 * system-provided if none of the installed providers supply one). That
316 * initialize method always throws an {@link UnsupportedOperationException}
317 * if it is not overridden by the provider.</p>
319 * @param params the parameter set used to generate the keys.
320 * @throws InvalidAlgorithmParameterException if the given parameters are
321 * inappropriate for this key pair generator.
322 * @since 1.2
324 public void initialize(AlgorithmParameterSpec params)
325 throws InvalidAlgorithmParameterException
327 initialize(params, new SecureRandom());
331 * <p>Initializes the key pair generator with the given parameter set and
332 * source of randomness.</p>
334 * <p>This concrete method has been added to this previously-defined abstract
335 * class. This method calls the
336 * {@link KeyPairGeneratorSpi#initialize(AlgorithmParameterSpec, SecureRandom)}
337 * initialize method, passing it <code>params</code> and <code>random</code>.
338 * That initialize method always throws an {@link UnsupportedOperationException}
339 * if it is not overridden by the provider.</p>
341 * @param params the parameter set used to generate the keys.
342 * @param random the source of randomness.
343 * @throws InvalidAlgorithmParameterException if the given parameters are
344 * inappropriate for this key pair generator.
345 * @since 1.2
347 public void initialize(AlgorithmParameterSpec params, SecureRandom random)
348 throws InvalidAlgorithmParameterException
350 super.initialize(params, random);
354 * <p>Generates a key pair.</p>
356 * <p>If this <code>KeyPairGenerator</code> has not been initialized
357 * explicitly, provider-specific defaults will be used for the size and other
358 * (algorithm-specific) values of the generated keys.</p>
360 * <p>This will generate a new key pair every time it is called.</p>
362 * <p>This method is functionally equivalent to {@link #generateKeyPair()}.</p>
364 * @return the generated key pair.
365 * @since 1.2
367 public final KeyPair genKeyPair()
371 return getInstance("DSA", "GNU").generateKeyPair();
373 catch (Exception e)
375 System.err.println("genKeyPair failed: " + e);
376 e.printStackTrace();
377 return null;
382 * <p>Generates a key pair.</p>
384 * <p>If this <code>KeyPairGenerator</code> has not been initialized
385 * explicitly, provider-specific defaults will be used for the size and other
386 * (algorithm-specific) values of the generated keys.</p>
388 * <p>This will generate a new key pair every time it is called.</p>
390 * <p>This method is functionally equivalent to {@link #genKeyPair()}.</p>
392 * @return the generated key pair.
394 public KeyPair generateKeyPair()
396 return genKeyPair();