Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / java / security / KeyPairGenerator.java
blob38bd007fa6a86c4826599190872956cf1c664fca
1 /* KeyPairGenerator.java --- Key Pair Generator Class
2 Copyright (C) 1999, 2002, 2003, 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 java.security;
41 import gnu.java.security.Engine;
43 import java.security.spec.AlgorithmParameterSpec;
45 /**
46 * <p>The <code>KeyPairGenerator</code> class is used to generate pairs of
47 * public and private keys. Key pair generators are constructed using the
48 * <code>getInstance()</code> factory methods (static methods that return
49 * instances of a given class).</p>
51 * <p>A Key pair generator for a particular algorithm creates a public/private
52 * key pair that can be used with this algorithm. It also associates
53 * algorithm-specific parameters with each of the generated keys.</p>
55 * <p>There are two ways to generate a key pair: in an algorithm-independent
56 * manner, and in an algorithm-specific manner. The only difference between the
57 * two is the initialization of the object:</p>
59 * <ul>
60 * <li><b>Algorithm-Independent Initialization</b><br/>
61 * All key pair generators share the concepts of a <i>keysize</i> and a
62 * <i>source of randomness</i>. The <i>keysize</i> is interpreted differently
63 * for different algorithms (e.g., in the case of the <i>DSA</i> algorithm,
64 * the <i>keysize</i> corresponds to the length of the modulus). There is an
65 * <code>initialize()</code> method in this <code>KeyPairGenerator</code>
66 * class that takes these two universally shared types of arguments. There
67 * is also one that takes just a <i>keysize</i> argument, and uses the
68 * {@link SecureRandom} implementation of the highest-priority installed
69 * provider as the <i>source of randomness</i>. (If none of the installed
70 * providers supply an implementation of {@link SecureRandom}, a
71 * system-provided source of randomness is used.)
73 * <p>Since no other parameters are specified when you call the above
74 * algorithm-independent initialize methods, it is up to the provider what
75 * to do about the algorithm-specific parameters (if any) to be associated
76 * with each of the keys.</p>
78 * <p>If the algorithm is the <i>DSA</i> algorithm, and the <i>keysize</i>
79 * (modulus size) is <code>512</code>, <code>768</code>, or <code>1024</code>,
80 * then the <b>GNU</b> provider uses a set of precomputed values for the
81 * <code>p</code>, <code>q</code>, and <code>g</code> parameters. If the
82 * <i>modulus size</i> is not one of the above values, the <b>GNU</b>
83 * provider creates a new set of parameters. Other providers might have
84 * precomputed parameter sets for more than just the three modulus sizes
85 * mentioned above. Still others might not have a list of precomputed
86 * parameters at all and instead always create new parameter sets.</p></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 e)
179 // Ignored.
183 throw new NoSuchAlgorithmException(algorithm);
187 * Generates a <code>KeyPairGenerator</code> object implementing the
188 * specified algorithm, as supplied from the specified provider, if
189 * such an algorithm is available from the provider.
191 * @param algorithm the standard string name of the algorithm. See
192 * Appendix A in the Java Cryptography Architecture API Specification
193 * &amp; Reference for information about standard algorithm names.
194 * @param provider the string name of the provider.
195 * @return the new <code>KeyPairGenerator</code> object.
196 * @throws NoSuchAlgorithmException if the algorithm is not available
197 * from the provider.
198 * @throws NoSuchProviderException if the provider is not available in the
199 * environment.
200 * @throws IllegalArgumentException if the provider name is <code>null</code>
201 * or empty.
202 * @see Provider
204 public static KeyPairGenerator getInstance(String algorithm, String provider)
205 throws NoSuchAlgorithmException, NoSuchProviderException
207 Provider p = Security.getProvider(provider);
208 if (p == null)
209 throw new NoSuchProviderException(provider);
211 return getInstance(algorithm, p);
215 * Generates a <code>KeyPairGenerator</code> object implementing the specified
216 * algorithm, as supplied from the specified provider, if such an algorithm is
217 * available from the provider. Note: the provider doesn't have to be
218 * registered.
220 * @param algorithm the standard string name of the algorithm. See Appendix A
221 * in the Java Cryptography Architecture API Specification &amp; Reference for
222 * information about standard algorithm names.
223 * @param provider the provider.
224 * @return the new <code>KeyPairGenerator</code> object.
225 * @throws NoSuchAlgorithmException if the <code>algorithm</code> is not
226 * available from the <code>provider</code>.
227 * @throws IllegalArgumentException if the <code>provider</code> is
228 * <code>null</code>.
229 * @since 1.4
230 * @see Provider
232 public static KeyPairGenerator getInstance(String algorithm,
233 Provider provider)
234 throws NoSuchAlgorithmException
236 if (provider == null)
237 throw new IllegalArgumentException("Illegal provider");
239 Object o = null;
242 o = Engine.getInstance(KEY_PAIR_GENERATOR, algorithm, provider);
244 catch (java.lang.reflect.InvocationTargetException ite)
246 throw new NoSuchAlgorithmException(algorithm);
249 KeyPairGenerator result = null;
250 if (o instanceof KeyPairGeneratorSpi)
252 result = new DummyKeyPairGenerator((KeyPairGeneratorSpi) o, algorithm);
254 else if (o instanceof KeyPairGenerator)
256 result = (KeyPairGenerator) o;
257 result.algorithm = algorithm;
259 result.provider = provider;
260 return result;
264 * Returns the provider of this key pair generator object.
266 * @return the provider of this key pair generator object.
268 public final Provider getProvider()
270 return provider;
274 * Initializes the key pair generator for a certain keysize using a default
275 * parameter set and the {@link SecureRandom} implementation of the
276 * highest-priority installed provider as the source of randomness. (If none
277 * of the installed providers supply an implementation of {@link SecureRandom},
278 * a system-provided source of randomness is used.)
280 * @param keysize the keysize. This is an algorithm-specific metric, such as
281 * modulus length, specified in number of bits.
282 * @throws InvalidParameterException if the keysize is not supported by this
283 * <code>KeyPairGenerator</code> object.
285 public void initialize(int keysize)
287 initialize(keysize, new SecureRandom());
291 * Initializes the key pair generator for a certain keysize with the given
292 * source of randomness (and a default parameter set).
294 * @param keysize the keysize. This is an algorithm-specific metric, such as
295 * modulus length, specified in number of bits.
296 * @param random the source of randomness.
297 * @throws InvalidParameterException if the <code>keysize</code> is not
298 * supported by this <code>KeyPairGenerator</code> object.
299 * @since 1.2
301 public void initialize(int keysize, SecureRandom random)
303 initialize(keysize, random);
307 * <p>Initializes the key pair generator using the specified parameter set and
308 * the {@link SecureRandom} implementation of the highest-priority installed
309 * provider as the source of randomness. (If none of the installed providers
310 * supply an implementation of {@link SecureRandom}, a system-provided source
311 * of randomness is used.)</p>
313 * <p>This concrete method has been added to this previously-defined abstract
314 * class. This method calls the
315 * {@link KeyPairGeneratorSpi#initialize(AlgorithmParameterSpec, SecureRandom)}
316 * initialize method, passing it <code>params</code> and a source of
317 * randomness (obtained from the highest-priority installed provider or
318 * system-provided if none of the installed providers supply one). That
319 * initialize method always throws an {@link UnsupportedOperationException}
320 * if it is not overridden by the provider.</p>
322 * @param params the parameter set used to generate the keys.
323 * @throws InvalidAlgorithmParameterException if the given parameters are
324 * inappropriate for this key pair generator.
325 * @since 1.2
327 public void initialize(AlgorithmParameterSpec params)
328 throws InvalidAlgorithmParameterException
330 initialize(params, new SecureRandom());
334 * <p>Initializes the key pair generator with the given parameter set and
335 * source of randomness.</p>
337 * <p>This concrete method has been added to this previously-defined abstract
338 * class. This method calls the
339 * {@link KeyPairGeneratorSpi#initialize(AlgorithmParameterSpec, SecureRandom)}
340 * initialize method, passing it <code>params</code> and <code>random</code>.
341 * That initialize method always throws an {@link UnsupportedOperationException}
342 * if it is not overridden by the provider.</p>
344 * @param params the parameter set used to generate the keys.
345 * @param random the source of randomness.
346 * @throws InvalidAlgorithmParameterException if the given parameters are
347 * inappropriate for this key pair generator.
348 * @since 1.2
350 public void initialize(AlgorithmParameterSpec params, SecureRandom random)
351 throws InvalidAlgorithmParameterException
353 super.initialize(params, random);
357 * <p>Generates a key pair.</p>
359 * <p>If this <code>KeyPairGenerator</code> has not been initialized
360 * explicitly, provider-specific defaults will be used for the size and other
361 * (algorithm-specific) values of the generated keys.</p>
363 * <p>This will generate a new key pair every time it is called.</p>
365 * <p>This method is functionally equivalent to {@link #generateKeyPair()}.</p>
367 * @return the generated key pair.
368 * @since 1.2
370 public final KeyPair genKeyPair()
374 return getInstance("DSA", "GNU").generateKeyPair();
376 catch (Exception e)
378 System.err.println("genKeyPair failed: " + e);
379 e.printStackTrace();
380 return null;
385 * <p>Generates a key pair.</p>
387 * <p>If this <code>KeyPairGenerator</code> has not been initialized
388 * explicitly, provider-specific defaults will be used for the size and other
389 * (algorithm-specific) values of the generated keys.</p>
391 * <p>This will generate a new key pair every time it is called.</p>
393 * <p>This method is functionally equivalent to {@link #genKeyPair()}.</p>
395 * @return the generated key pair.
397 public KeyPair generateKeyPair()
399 return genKeyPair();