1 /* KeyPairGenerator.java --- Key Pair Generator Class
2 Copyright (C) 1999, 2002, 2003, 2004, 2005 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)
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., 51 Franklin Street, Fifth Floor, Boston, MA
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
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.lang.reflect.InvocationTargetException;
44 import java.security.spec.AlgorithmParameterSpec;
47 * <code>KeyPairGenerator</code> is a class used to generate key-pairs for a
50 * <p>The <code>KeyPairGenerator</code> is created with the
51 * <code>getInstance()</code> Factory methods. It is used to generate a pair of
52 * public and private keys for a specific algorithm and associate this key-pair
53 * with the algorithm parameters it was initialized with.</p>
56 * @see AlgorithmParameterSpec
57 * @author Mark Benvenuto
58 * @author Casey Marshall
60 public abstract class KeyPairGenerator extends KeyPairGeneratorSpi
62 /** The service name for key pair generators. */
63 private static final String KEY_PAIR_GENERATOR = "KeyPairGenerator";
66 private String algorithm;
69 * Constructs a new instance of <code>KeyPairGenerator</code>.
72 * the algorithm to use.
74 protected KeyPairGenerator(String algorithm)
76 this.algorithm = algorithm;
81 * Returns the name of the algorithm used.
83 * @return the name of the algorithm used.
85 public String getAlgorithm()
91 * Returns a new instance of <code>KeyPairGenerator</code> which generates
92 * key-pairs for the specified algorithm.
94 * @param algorithm the name of the algorithm to use.
95 * @return a new instance repesenting the desired algorithm.
96 * @throws NoSuchAlgorithmException if the algorithm is not implemented by any
98 * @throws IllegalArgumentException if <code>algorithm</code> is
99 * <code>null</code> or is an empty string.
101 public static KeyPairGenerator getInstance(String algorithm)
102 throws NoSuchAlgorithmException
104 Provider[] p = Security.getProviders();
105 NoSuchAlgorithmException lastException = null;
106 for (int i = 0; i < p.length; i++)
109 return getInstance(algorithm, p[i]);
111 catch (NoSuchAlgorithmException x)
115 if (lastException != null)
117 throw new NoSuchAlgorithmException(algorithm);
121 * Returns a new instance of <code>KeyPairGenerator</code> which generates
122 * key-pairs for the specified algorithm from a named provider.
124 * @param algorithm the name of the algorithm to use.
125 * @param provider the name of a {@link Provider} to use.
126 * @return a new instance repesenting the desired algorithm.
127 * @throws NoSuchAlgorithmException if the algorithm is not implemented by the
129 * @throws NoSuchProviderException if the named provider was not found.
130 * @throws IllegalArgumentException if either <code>algorithm</code> or
131 * <code>provider</code> is <code>null</code> or empty.
133 public static KeyPairGenerator getInstance(String algorithm, String provider)
134 throws NoSuchAlgorithmException, NoSuchProviderException
136 if (provider == null)
137 throw new IllegalArgumentException("provider MUST NOT be null");
138 provider = provider.trim();
139 if (provider.length() == 0)
140 throw new IllegalArgumentException("provider MUST NOT be empty");
141 Provider p = Security.getProvider(provider);
143 throw new NoSuchProviderException(provider);
144 return getInstance(algorithm, p);
148 * Returns a new instance of <code>KeyPairGenerator</code> which generates
149 * key-pairs for the specified algorithm from a designated {@link Provider}.
152 * the name of the algorithm to use.
154 * the {@link Provider} to use.
155 * @return a new insatnce repesenting the desired algorithm.
156 * @throws NoSuchAlgorithmException
157 * if the algorithm is not implemented by the {@link Provider}.
158 * @throws IllegalArgumentException if either <code>algorithm</code> or
159 * <code>provider</code> is <code>null</code>, or if
160 * <code>algorithm</code> is an empty string.
164 public static KeyPairGenerator getInstance(String algorithm,
166 throws NoSuchAlgorithmException
168 StringBuilder sb = new StringBuilder("KeyPairGenerator for algorithm [")
169 .append(algorithm).append("] from provider[")
170 .append(provider).append("] ");
174 o = Engine.getInstance(KEY_PAIR_GENERATOR, algorithm, provider);
176 catch (InvocationTargetException x)
178 Throwable cause = x.getCause();
179 if (cause instanceof NoSuchAlgorithmException)
180 throw (NoSuchAlgorithmException) cause;
183 sb.append("could not be created");
184 NoSuchAlgorithmException y = new NoSuchAlgorithmException(sb.toString());
188 KeyPairGenerator result;
189 if (o instanceof KeyPairGenerator)
191 result = (KeyPairGenerator) o;
192 result.algorithm = algorithm;
194 else if (o instanceof KeyPairGeneratorSpi)
195 result = new DummyKeyPairGenerator((KeyPairGeneratorSpi) o, algorithm);
198 sb.append("is of an unexpected Type: ").append(o.getClass().getName());
199 throw new NoSuchAlgorithmException(sb.toString());
201 result.provider = provider;
206 * Returns the {@link Provider} of this instance.
208 * @return the {@link Provider} of this instance.
210 public final Provider getProvider()
216 * Initializes this instance for the specified key size. Since no source of
217 * randomness is specified, a default one will be used.
220 * the size of keys to use.
222 public void initialize(int keysize)
224 initialize(keysize, new SecureRandom());
228 * Initializes this instance for the specified key size and
229 * {@link SecureRandom}.
232 * the size of keys to use.
234 * the {@link SecureRandom} to use.
237 public void initialize(int keysize, SecureRandom random)
242 * Initializes this instance with the specified
243 * {@link AlgorithmParameterSpec}. Since no source of randomness is specified,
244 * a default one will be used.
247 * the {@link AlgorithmParameterSpec} to use.
248 * @throws InvalidAlgorithmParameterException
249 * if the designated specifications are invalid.
252 public void initialize(AlgorithmParameterSpec params)
253 throws InvalidAlgorithmParameterException
255 initialize(params, new SecureRandom());
259 * Initializes this instance with the specified {@link AlgorithmParameterSpec}
260 * and {@link SecureRandom}.
263 * the {@link AlgorithmParameterSpec} to use.
265 * the {@link SecureRandom} to use.
266 * @throws InvalidAlgorithmParameterException
267 * if the designated specifications are invalid.
270 public void initialize(AlgorithmParameterSpec params, SecureRandom random)
271 throws InvalidAlgorithmParameterException
273 super.initialize(params, random);
277 * Generates a new "DSA" {@link KeyPair} from the "GNU" security provider.
279 * <p>This method generates a unique key-pair each time it is called.</p>
281 * @return a new unique {@link KeyPair}.
282 * @see #generateKeyPair()
285 public final KeyPair genKeyPair()
289 return getInstance("DSA", "GNU").generateKeyPair();
293 System.err.println("genKeyPair failed: " + e);
300 * Generates a new "DSA" {@link KeyPair} from the "GNU" security provider.
302 * <p>This method generates a unique key pair each time it is called.</p>
304 * @return a new unique {@link KeyPair}.
307 public KeyPair generateKeyPair()