author | chegar |
Sun, 17 Aug 2014 15:54:13 +0100 | |
changeset 25859 | 3317bb8137f4 |
parent 21334 | jdk/src/share/classes/java/security/KeyPairGenerator.java@c60dfce46a77 |
child 26736 | 5a93000b26cd |
permissions | -rw-r--r-- |
2 | 1 |
/* |
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9035
diff
changeset
|
2 |
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.security; |
|
27 |
||
28 |
import java.util.*; |
|
29 |
||
30 |
import java.security.spec.AlgorithmParameterSpec; |
|
31 |
||
32 |
import java.security.Provider.Service; |
|
33 |
||
34 |
import sun.security.jca.*; |
|
35 |
import sun.security.jca.GetInstance.Instance; |
|
36 |
||
37 |
/** |
|
38 |
* The KeyPairGenerator class is used to generate pairs of |
|
39 |
* public and private keys. Key pair generators are constructed using the |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9035
diff
changeset
|
40 |
* {@code getInstance} factory methods (static methods that |
2 | 41 |
* return instances of a given class). |
42 |
* |
|
43 |
* <p>A Key pair generator for a particular algorithm creates a public/private |
|
44 |
* key pair that can be used with this algorithm. It also associates |
|
45 |
* algorithm-specific parameters with each of the generated keys. |
|
46 |
* |
|
47 |
* <p>There are two ways to generate a key pair: in an algorithm-independent |
|
48 |
* manner, and in an algorithm-specific manner. |
|
49 |
* The only difference between the two is the initialization of the object: |
|
50 |
* |
|
51 |
* <ul> |
|
52 |
* <li><b>Algorithm-Independent Initialization</b> |
|
53 |
* <p>All key pair generators share the concepts of a keysize and a |
|
54 |
* source of randomness. The keysize is interpreted differently for different |
|
55 |
* algorithms (e.g., in the case of the <i>DSA</i> algorithm, the keysize |
|
56 |
* corresponds to the length of the modulus). |
|
57 |
* There is an |
|
58 |
* {@link #initialize(int, java.security.SecureRandom) initialize} |
|
59 |
* method in this KeyPairGenerator class that takes these two universally |
|
60 |
* shared types of arguments. There is also one that takes just a |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9035
diff
changeset
|
61 |
* {@code keysize} argument, and uses the {@code SecureRandom} |
2 | 62 |
* implementation of the highest-priority installed provider as the source |
63 |
* of randomness. (If none of the installed providers supply an implementation |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9035
diff
changeset
|
64 |
* of {@code SecureRandom}, a system-provided source of randomness is |
2 | 65 |
* used.) |
66 |
* |
|
67 |
* <p>Since no other parameters are specified when you call the above |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9035
diff
changeset
|
68 |
* algorithm-independent {@code initialize} methods, it is up to the |
2 | 69 |
* provider what to do about the algorithm-specific parameters (if any) to be |
70 |
* associated with each of the keys. |
|
71 |
* |
|
72 |
* <p>If the algorithm is the <i>DSA</i> algorithm, and the keysize (modulus |
|
73 |
* size) is 512, 768, or 1024, then the <i>Sun</i> provider uses a set of |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9035
diff
changeset
|
74 |
* precomputed values for the {@code p}, {@code q}, and |
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9035
diff
changeset
|
75 |
* {@code g} parameters. If the modulus size is not one of the above |
2 | 76 |
* values, the <i>Sun</i> provider creates a new set of parameters. Other |
77 |
* providers might have precomputed parameter sets for more than just the |
|
78 |
* three modulus sizes mentioned above. Still others might not have a list of |
|
79 |
* precomputed parameters at all and instead always create new parameter sets. |
|
80 |
* |
|
81 |
* <li><b>Algorithm-Specific Initialization</b> |
|
82 |
* <p>For situations where a set of algorithm-specific parameters already |
|
83 |
* exists (e.g., so-called <i>community parameters</i> in DSA), there are two |
|
84 |
* {@link #initialize(java.security.spec.AlgorithmParameterSpec) |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9035
diff
changeset
|
85 |
* initialize} methods that have an {@code AlgorithmParameterSpec} |
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9035
diff
changeset
|
86 |
* argument. One also has a {@code SecureRandom} argument, while the |
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9035
diff
changeset
|
87 |
* the other uses the {@code SecureRandom} |
2 | 88 |
* implementation of the highest-priority installed provider as the source |
89 |
* of randomness. (If none of the installed providers supply an implementation |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9035
diff
changeset
|
90 |
* of {@code SecureRandom}, a system-provided source of randomness is |
2 | 91 |
* used.) |
92 |
* </ul> |
|
93 |
* |
|
94 |
* <p>In case the client does not explicitly initialize the KeyPairGenerator |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9035
diff
changeset
|
95 |
* (via a call to an {@code initialize} method), each provider must |
2 | 96 |
* supply (and document) a default initialization. |
97 |
* For example, the <i>Sun</i> provider uses a default modulus size (keysize) |
|
98 |
* of 1024 bits. |
|
99 |
* |
|
100 |
* <p>Note that this class is abstract and extends from |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9035
diff
changeset
|
101 |
* {@code KeyPairGeneratorSpi} for historical reasons. |
2 | 102 |
* Application developers should only take notice of the methods defined in |
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9035
diff
changeset
|
103 |
* this {@code KeyPairGenerator} class; all the methods in |
2 | 104 |
* the superclass are intended for cryptographic service providers who wish to |
105 |
* supply their own implementations of key pair generators. |
|
106 |
* |
|
8152
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
107 |
* <p> Every implementation of the Java platform is required to support the |
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9035
diff
changeset
|
108 |
* following standard {@code KeyPairGenerator} algorithms and keysizes in |
8152
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
109 |
* parentheses: |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
110 |
* <ul> |
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9035
diff
changeset
|
111 |
* <li>{@code DiffieHellman} (1024)</li> |
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9035
diff
changeset
|
112 |
* <li>{@code DSA} (1024)</li> |
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9035
diff
changeset
|
113 |
* <li>{@code RSA} (1024, 2048)</li> |
8152
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
114 |
* </ul> |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
115 |
* These algorithms are described in the <a href= |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
116 |
* "{@docRoot}/../technotes/guides/security/StandardNames.html#KeyPairGenerator"> |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
117 |
* KeyPairGenerator section</a> of the |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
118 |
* Java Cryptography Architecture Standard Algorithm Name Documentation. |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
119 |
* Consult the release documentation for your implementation to see if any |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
120 |
* other algorithms are supported. |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
121 |
* |
2 | 122 |
* @author Benjamin Renaud |
123 |
* |
|
124 |
* @see java.security.spec.AlgorithmParameterSpec |
|
125 |
*/ |
|
126 |
||
127 |
public abstract class KeyPairGenerator extends KeyPairGeneratorSpi { |
|
128 |
||
129 |
private final String algorithm; |
|
130 |
||
131 |
// The provider |
|
132 |
Provider provider; |
|
133 |
||
134 |
/** |
|
135 |
* Creates a KeyPairGenerator object for the specified algorithm. |
|
136 |
* |
|
137 |
* @param algorithm the standard string name of the algorithm. |
|
8152
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
138 |
* See the KeyPairGenerator section in the <a href= |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
139 |
* "{@docRoot}/../technotes/guides/security/StandardNames.html#KeyPairGenerator"> |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
140 |
* Java Cryptography Architecture Standard Algorithm Name Documentation</a> |
2 | 141 |
* for information about standard algorithm names. |
142 |
*/ |
|
143 |
protected KeyPairGenerator(String algorithm) { |
|
144 |
this.algorithm = algorithm; |
|
145 |
} |
|
146 |
||
147 |
/** |
|
148 |
* Returns the standard name of the algorithm for this key pair generator. |
|
8152
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
149 |
* See the KeyPairGenerator section in the <a href= |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
150 |
* "{@docRoot}/../technotes/guides/security/StandardNames.html#KeyPairGenerator"> |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
151 |
* Java Cryptography Architecture Standard Algorithm Name Documentation</a> |
2 | 152 |
* for information about standard algorithm names. |
153 |
* |
|
154 |
* @return the standard string name of the algorithm. |
|
155 |
*/ |
|
156 |
public String getAlgorithm() { |
|
157 |
return this.algorithm; |
|
158 |
} |
|
159 |
||
160 |
private static KeyPairGenerator getInstance(Instance instance, |
|
161 |
String algorithm) { |
|
162 |
KeyPairGenerator kpg; |
|
163 |
if (instance.impl instanceof KeyPairGenerator) { |
|
164 |
kpg = (KeyPairGenerator)instance.impl; |
|
165 |
} else { |
|
166 |
KeyPairGeneratorSpi spi = (KeyPairGeneratorSpi)instance.impl; |
|
167 |
kpg = new Delegate(spi, algorithm); |
|
168 |
} |
|
169 |
kpg.provider = instance.provider; |
|
170 |
return kpg; |
|
171 |
} |
|
172 |
||
173 |
/** |
|
174 |
* Returns a KeyPairGenerator object that generates public/private |
|
175 |
* key pairs for the specified algorithm. |
|
176 |
* |
|
177 |
* <p> This method traverses the list of registered security Providers, |
|
178 |
* starting with the most preferred Provider. |
|
179 |
* A new KeyPairGenerator object encapsulating the |
|
180 |
* KeyPairGeneratorSpi implementation from the first |
|
181 |
* Provider that supports the specified algorithm is returned. |
|
182 |
* |
|
183 |
* <p> Note that the list of registered providers may be retrieved via |
|
184 |
* the {@link Security#getProviders() Security.getProviders()} method. |
|
185 |
* |
|
186 |
* @param algorithm the standard string name of the algorithm. |
|
8152
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
187 |
* See the KeyPairGenerator section in the <a href= |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
188 |
* "{@docRoot}/../technotes/guides/security/StandardNames.html#KeyPairGenerator"> |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
189 |
* Java Cryptography Architecture Standard Algorithm Name Documentation</a> |
2 | 190 |
* for information about standard algorithm names. |
191 |
* |
|
192 |
* @return the new KeyPairGenerator object. |
|
193 |
* |
|
194 |
* @exception NoSuchAlgorithmException if no Provider supports a |
|
195 |
* KeyPairGeneratorSpi implementation for the |
|
196 |
* specified algorithm. |
|
197 |
* |
|
198 |
* @see Provider |
|
199 |
*/ |
|
200 |
public static KeyPairGenerator getInstance(String algorithm) |
|
201 |
throws NoSuchAlgorithmException { |
|
202 |
List<Service> list = |
|
203 |
GetInstance.getServices("KeyPairGenerator", algorithm); |
|
204 |
Iterator<Service> t = list.iterator(); |
|
205 |
if (t.hasNext() == false) { |
|
206 |
throw new NoSuchAlgorithmException |
|
207 |
(algorithm + " KeyPairGenerator not available"); |
|
208 |
} |
|
209 |
// find a working Spi or KeyPairGenerator subclass |
|
210 |
NoSuchAlgorithmException failure = null; |
|
211 |
do { |
|
212 |
Service s = t.next(); |
|
213 |
try { |
|
214 |
Instance instance = |
|
215 |
GetInstance.getInstance(s, KeyPairGeneratorSpi.class); |
|
216 |
if (instance.impl instanceof KeyPairGenerator) { |
|
217 |
return getInstance(instance, algorithm); |
|
218 |
} else { |
|
219 |
return new Delegate(instance, t, algorithm); |
|
220 |
} |
|
221 |
} catch (NoSuchAlgorithmException e) { |
|
222 |
if (failure == null) { |
|
223 |
failure = e; |
|
224 |
} |
|
225 |
} |
|
226 |
} while (t.hasNext()); |
|
227 |
throw failure; |
|
228 |
} |
|
229 |
||
230 |
/** |
|
231 |
* Returns a KeyPairGenerator object that generates public/private |
|
232 |
* key pairs for the specified algorithm. |
|
233 |
* |
|
234 |
* <p> A new KeyPairGenerator object encapsulating the |
|
235 |
* KeyPairGeneratorSpi implementation from the specified provider |
|
236 |
* is returned. The specified provider must be registered |
|
237 |
* in the security provider list. |
|
238 |
* |
|
239 |
* <p> Note that the list of registered providers may be retrieved via |
|
240 |
* the {@link Security#getProviders() Security.getProviders()} method. |
|
241 |
* |
|
242 |
* @param algorithm the standard string name of the algorithm. |
|
8152
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
243 |
* See the KeyPairGenerator section in the <a href= |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
244 |
* "{@docRoot}/../technotes/guides/security/StandardNames.html#KeyPairGenerator"> |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
245 |
* Java Cryptography Architecture Standard Algorithm Name Documentation</a> |
2 | 246 |
* for information about standard algorithm names. |
247 |
* |
|
248 |
* @param provider the string name of the provider. |
|
249 |
* |
|
250 |
* @return the new KeyPairGenerator object. |
|
251 |
* |
|
252 |
* @exception NoSuchAlgorithmException if a KeyPairGeneratorSpi |
|
253 |
* implementation for the specified algorithm is not |
|
254 |
* available from the specified provider. |
|
255 |
* |
|
256 |
* @exception NoSuchProviderException if the specified provider is not |
|
257 |
* registered in the security provider list. |
|
258 |
* |
|
259 |
* @exception IllegalArgumentException if the provider name is null |
|
260 |
* or empty. |
|
261 |
* |
|
262 |
* @see Provider |
|
263 |
*/ |
|
264 |
public static KeyPairGenerator getInstance(String algorithm, |
|
265 |
String provider) |
|
266 |
throws NoSuchAlgorithmException, NoSuchProviderException { |
|
267 |
Instance instance = GetInstance.getInstance("KeyPairGenerator", |
|
268 |
KeyPairGeneratorSpi.class, algorithm, provider); |
|
269 |
return getInstance(instance, algorithm); |
|
270 |
} |
|
271 |
||
272 |
/** |
|
273 |
* Returns a KeyPairGenerator object that generates public/private |
|
274 |
* key pairs for the specified algorithm. |
|
275 |
* |
|
276 |
* <p> A new KeyPairGenerator object encapsulating the |
|
277 |
* KeyPairGeneratorSpi implementation from the specified Provider |
|
278 |
* object is returned. Note that the specified Provider object |
|
279 |
* does not have to be registered in the provider list. |
|
280 |
* |
|
281 |
* @param algorithm the standard string name of the algorithm. |
|
8152
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
282 |
* See the KeyPairGenerator section in the <a href= |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
283 |
* "{@docRoot}/../technotes/guides/security/StandardNames.html#KeyPairGenerator"> |
94e5966bdf22
5001004: Required Security Algorithms need to be defined
mullan
parents:
5506
diff
changeset
|
284 |
* Java Cryptography Architecture Standard Algorithm Name Documentation</a> |
2 | 285 |
* for information about standard algorithm names. |
286 |
* |
|
287 |
* @param provider the provider. |
|
288 |
* |
|
289 |
* @return the new KeyPairGenerator object. |
|
290 |
* |
|
291 |
* @exception NoSuchAlgorithmException if a KeyPairGeneratorSpi |
|
292 |
* implementation for the specified algorithm is not available |
|
293 |
* from the specified Provider object. |
|
294 |
* |
|
295 |
* @exception IllegalArgumentException if the specified provider is null. |
|
296 |
* |
|
297 |
* @see Provider |
|
298 |
* |
|
299 |
* @since 1.4 |
|
300 |
*/ |
|
301 |
public static KeyPairGenerator getInstance(String algorithm, |
|
302 |
Provider provider) throws NoSuchAlgorithmException { |
|
303 |
Instance instance = GetInstance.getInstance("KeyPairGenerator", |
|
304 |
KeyPairGeneratorSpi.class, algorithm, provider); |
|
305 |
return getInstance(instance, algorithm); |
|
306 |
} |
|
307 |
||
308 |
/** |
|
309 |
* Returns the provider of this key pair generator object. |
|
310 |
* |
|
311 |
* @return the provider of this key pair generator object |
|
312 |
*/ |
|
313 |
public final Provider getProvider() { |
|
314 |
disableFailover(); |
|
315 |
return this.provider; |
|
316 |
} |
|
317 |
||
318 |
void disableFailover() { |
|
319 |
// empty, overridden in Delegate |
|
320 |
} |
|
321 |
||
322 |
/** |
|
323 |
* Initializes the key pair generator for a certain keysize using |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9035
diff
changeset
|
324 |
* a default parameter set and the {@code SecureRandom} |
2 | 325 |
* implementation of the highest-priority installed provider as the source |
326 |
* of randomness. |
|
327 |
* (If none of the installed providers supply an implementation of |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9035
diff
changeset
|
328 |
* {@code SecureRandom}, a system-provided source of randomness is |
2 | 329 |
* used.) |
330 |
* |
|
331 |
* @param keysize the keysize. This is an |
|
332 |
* algorithm-specific metric, such as modulus length, specified in |
|
333 |
* number of bits. |
|
334 |
* |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9035
diff
changeset
|
335 |
* @exception InvalidParameterException if the {@code keysize} is not |
2 | 336 |
* supported by this KeyPairGenerator object. |
337 |
*/ |
|
338 |
public void initialize(int keysize) { |
|
339 |
initialize(keysize, JCAUtil.getSecureRandom()); |
|
340 |
} |
|
341 |
||
342 |
/** |
|
343 |
* Initializes the key pair generator for a certain keysize with |
|
344 |
* the given source of randomness (and a default parameter set). |
|
345 |
* |
|
346 |
* @param keysize the keysize. This is an |
|
347 |
* algorithm-specific metric, such as modulus length, specified in |
|
348 |
* number of bits. |
|
349 |
* @param random the source of randomness. |
|
350 |
* |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9035
diff
changeset
|
351 |
* @exception InvalidParameterException if the {@code keysize} is not |
2 | 352 |
* supported by this KeyPairGenerator object. |
353 |
* |
|
354 |
* @since 1.2 |
|
355 |
*/ |
|
356 |
public void initialize(int keysize, SecureRandom random) { |
|
357 |
// This does nothing, because either |
|
358 |
// 1. the implementation object returned by getInstance() is an |
|
359 |
// instance of KeyPairGenerator which has its own |
|
360 |
// initialize(keysize, random) method, so the application would |
|
361 |
// be calling that method directly, or |
|
362 |
// 2. the implementation returned by getInstance() is an instance |
|
363 |
// of Delegate, in which case initialize(keysize, random) is |
|
364 |
// overridden to call the corresponding SPI method. |
|
365 |
// (This is a special case, because the API and SPI method have the |
|
366 |
// same name.) |
|
367 |
} |
|
368 |
||
369 |
/** |
|
370 |
* Initializes the key pair generator using the specified parameter |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9035
diff
changeset
|
371 |
* set and the {@code SecureRandom} |
2 | 372 |
* implementation of the highest-priority installed provider as the source |
373 |
* of randomness. |
|
374 |
* (If none of the installed providers supply an implementation of |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9035
diff
changeset
|
375 |
* {@code SecureRandom}, a system-provided source of randomness is |
2 | 376 |
* used.). |
377 |
* |
|
378 |
* <p>This concrete method has been added to this previously-defined |
|
379 |
* abstract class. |
|
380 |
* This method calls the KeyPairGeneratorSpi |
|
381 |
* {@link KeyPairGeneratorSpi#initialize( |
|
382 |
* java.security.spec.AlgorithmParameterSpec, |
|
383 |
* java.security.SecureRandom) initialize} method, |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9035
diff
changeset
|
384 |
* passing it {@code params} and a source of randomness (obtained |
2 | 385 |
* from the highest-priority installed provider or system-provided if none |
386 |
* of the installed providers supply one). |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9035
diff
changeset
|
387 |
* That {@code initialize} method always throws an |
2 | 388 |
* UnsupportedOperationException if it is not overridden by the provider. |
389 |
* |
|
390 |
* @param params the parameter set used to generate the keys. |
|
391 |
* |
|
392 |
* @exception InvalidAlgorithmParameterException if the given parameters |
|
393 |
* are inappropriate for this key pair generator. |
|
394 |
* |
|
395 |
* @since 1.2 |
|
396 |
*/ |
|
397 |
public void initialize(AlgorithmParameterSpec params) |
|
398 |
throws InvalidAlgorithmParameterException { |
|
399 |
initialize(params, JCAUtil.getSecureRandom()); |
|
400 |
} |
|
401 |
||
402 |
/** |
|
403 |
* Initializes the key pair generator with the given parameter |
|
404 |
* set and source of randomness. |
|
405 |
* |
|
406 |
* <p>This concrete method has been added to this previously-defined |
|
407 |
* abstract class. |
|
408 |
* This method calls the KeyPairGeneratorSpi {@link |
|
409 |
* KeyPairGeneratorSpi#initialize( |
|
410 |
* java.security.spec.AlgorithmParameterSpec, |
|
411 |
* java.security.SecureRandom) initialize} method, |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9035
diff
changeset
|
412 |
* passing it {@code params} and {@code random}. |
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
9035
diff
changeset
|
413 |
* That {@code initialize} |
2 | 414 |
* method always throws an |
415 |
* UnsupportedOperationException if it is not overridden by the provider. |
|
416 |
* |
|
417 |
* @param params the parameter set used to generate the keys. |
|
418 |
* @param random the source of randomness. |
|
419 |
* |
|
420 |
* @exception InvalidAlgorithmParameterException if the given parameters |
|
421 |
* are inappropriate for this key pair generator. |
|
422 |
* |
|
423 |
* @since 1.2 |
|
424 |
*/ |
|
425 |
public void initialize(AlgorithmParameterSpec params, |
|
426 |
SecureRandom random) |
|
427 |
throws InvalidAlgorithmParameterException |
|
428 |
{ |
|
429 |
// This does nothing, because either |
|
430 |
// 1. the implementation object returned by getInstance() is an |
|
431 |
// instance of KeyPairGenerator which has its own |
|
432 |
// initialize(params, random) method, so the application would |
|
433 |
// be calling that method directly, or |
|
434 |
// 2. the implementation returned by getInstance() is an instance |
|
435 |
// of Delegate, in which case initialize(params, random) is |
|
436 |
// overridden to call the corresponding SPI method. |
|
437 |
// (This is a special case, because the API and SPI method have the |
|
438 |
// same name.) |
|
439 |
} |
|
440 |
||
441 |
/** |
|
442 |
* Generates a key pair. |
|
443 |
* |
|
444 |
* <p>If this KeyPairGenerator has not been initialized explicitly, |
|
445 |
* provider-specific defaults will be used for the size and other |
|
446 |
* (algorithm-specific) values of the generated keys. |
|
447 |
* |
|
448 |
* <p>This will generate a new key pair every time it is called. |
|
449 |
* |
|
450 |
* <p>This method is functionally equivalent to |
|
451 |
* {@link #generateKeyPair() generateKeyPair}. |
|
452 |
* |
|
453 |
* @return the generated key pair |
|
454 |
* |
|
455 |
* @since 1.2 |
|
456 |
*/ |
|
457 |
public final KeyPair genKeyPair() { |
|
458 |
return generateKeyPair(); |
|
459 |
} |
|
460 |
||
461 |
/** |
|
462 |
* Generates a key pair. |
|
463 |
* |
|
464 |
* <p>If this KeyPairGenerator has not been initialized explicitly, |
|
465 |
* provider-specific defaults will be used for the size and other |
|
466 |
* (algorithm-specific) values of the generated keys. |
|
467 |
* |
|
468 |
* <p>This will generate a new key pair every time it is called. |
|
469 |
* |
|
470 |
* <p>This method is functionally equivalent to |
|
471 |
* {@link #genKeyPair() genKeyPair}. |
|
472 |
* |
|
473 |
* @return the generated key pair |
|
474 |
*/ |
|
475 |
public KeyPair generateKeyPair() { |
|
476 |
// This does nothing (except returning null), because either: |
|
477 |
// |
|
478 |
// 1. the implementation object returned by getInstance() is an |
|
479 |
// instance of KeyPairGenerator which has its own implementation |
|
480 |
// of generateKeyPair (overriding this one), so the application |
|
481 |
// would be calling that method directly, or |
|
482 |
// |
|
483 |
// 2. the implementation returned by getInstance() is an instance |
|
484 |
// of Delegate, in which case generateKeyPair is |
|
485 |
// overridden to invoke the corresponding SPI method. |
|
486 |
// |
|
487 |
// (This is a special case, because in JDK 1.1.x the generateKeyPair |
|
488 |
// method was used both as an API and a SPI method.) |
|
489 |
return null; |
|
490 |
} |
|
491 |
||
492 |
||
493 |
/* |
|
494 |
* The following class allows providers to extend from KeyPairGeneratorSpi |
|
495 |
* rather than from KeyPairGenerator. It represents a KeyPairGenerator |
|
496 |
* with an encapsulated, provider-supplied SPI object (of type |
|
497 |
* KeyPairGeneratorSpi). |
|
498 |
* If the provider implementation is an instance of KeyPairGeneratorSpi, |
|
499 |
* the getInstance() methods above return an instance of this class, with |
|
500 |
* the SPI object encapsulated. |
|
501 |
* |
|
502 |
* Note: All SPI methods from the original KeyPairGenerator class have been |
|
503 |
* moved up the hierarchy into a new class (KeyPairGeneratorSpi), which has |
|
504 |
* been interposed in the hierarchy between the API (KeyPairGenerator) |
|
505 |
* and its original parent (Object). |
|
506 |
*/ |
|
507 |
||
508 |
// |
|
509 |
// error failover notes: |
|
510 |
// |
|
511 |
// . we failover if the implementation throws an error during init |
|
512 |
// by retrying the init on other providers |
|
513 |
// |
|
514 |
// . we also failover if the init succeeded but the subsequent call |
|
515 |
// to generateKeyPair() fails. In order for this to work, we need |
|
516 |
// to remember the parameters to the last successful call to init |
|
517 |
// and initialize() the next spi using them. |
|
518 |
// |
|
519 |
// . although not specified, KeyPairGenerators could be thread safe, |
|
520 |
// so we make sure we do not interfere with that |
|
521 |
// |
|
522 |
// . failover is not available, if: |
|
523 |
// . getInstance(algorithm, provider) was used |
|
524 |
// . a provider extends KeyPairGenerator rather than |
|
525 |
// KeyPairGeneratorSpi (JDK 1.1 style) |
|
526 |
// . once getProvider() is called |
|
527 |
// |
|
528 |
||
529 |
private static final class Delegate extends KeyPairGenerator { |
|
530 |
||
531 |
// The provider implementation (delegate) |
|
532 |
private volatile KeyPairGeneratorSpi spi; |
|
533 |
||
534 |
private final Object lock = new Object(); |
|
535 |
||
536 |
private Iterator<Service> serviceIterator; |
|
537 |
||
538 |
private final static int I_NONE = 1; |
|
539 |
private final static int I_SIZE = 2; |
|
540 |
private final static int I_PARAMS = 3; |
|
541 |
||
542 |
private int initType; |
|
543 |
private int initKeySize; |
|
544 |
private AlgorithmParameterSpec initParams; |
|
545 |
private SecureRandom initRandom; |
|
546 |
||
547 |
// constructor |
|
548 |
Delegate(KeyPairGeneratorSpi spi, String algorithm) { |
|
549 |
super(algorithm); |
|
550 |
this.spi = spi; |
|
551 |
} |
|
552 |
||
553 |
Delegate(Instance instance, Iterator<Service> serviceIterator, |
|
554 |
String algorithm) { |
|
555 |
super(algorithm); |
|
556 |
spi = (KeyPairGeneratorSpi)instance.impl; |
|
557 |
provider = instance.provider; |
|
558 |
this.serviceIterator = serviceIterator; |
|
559 |
initType = I_NONE; |
|
560 |
} |
|
561 |
||
562 |
/** |
|
563 |
* Update the active spi of this class and return the next |
|
564 |
* implementation for failover. If no more implemenations are |
|
565 |
* available, this method returns null. However, the active spi of |
|
566 |
* this class is never set to null. |
|
567 |
*/ |
|
568 |
private KeyPairGeneratorSpi nextSpi(KeyPairGeneratorSpi oldSpi, |
|
569 |
boolean reinit) { |
|
570 |
synchronized (lock) { |
|
571 |
// somebody else did a failover concurrently |
|
572 |
// try that spi now |
|
573 |
if ((oldSpi != null) && (oldSpi != spi)) { |
|
574 |
return spi; |
|
575 |
} |
|
576 |
if (serviceIterator == null) { |
|
577 |
return null; |
|
578 |
} |
|
579 |
while (serviceIterator.hasNext()) { |
|
580 |
Service s = serviceIterator.next(); |
|
581 |
try { |
|
582 |
Object inst = s.newInstance(null); |
|
583 |
// ignore non-spis |
|
584 |
if (inst instanceof KeyPairGeneratorSpi == false) { |
|
585 |
continue; |
|
586 |
} |
|
587 |
if (inst instanceof KeyPairGenerator) { |
|
588 |
continue; |
|
589 |
} |
|
590 |
KeyPairGeneratorSpi spi = (KeyPairGeneratorSpi)inst; |
|
591 |
if (reinit) { |
|
592 |
if (initType == I_SIZE) { |
|
593 |
spi.initialize(initKeySize, initRandom); |
|
594 |
} else if (initType == I_PARAMS) { |
|
595 |
spi.initialize(initParams, initRandom); |
|
596 |
} else if (initType != I_NONE) { |
|
597 |
throw new AssertionError |
|
598 |
("KeyPairGenerator initType: " + initType); |
|
599 |
} |
|
600 |
} |
|
601 |
provider = s.getProvider(); |
|
602 |
this.spi = spi; |
|
603 |
return spi; |
|
604 |
} catch (Exception e) { |
|
605 |
// ignore |
|
606 |
} |
|
607 |
} |
|
608 |
disableFailover(); |
|
609 |
return null; |
|
610 |
} |
|
611 |
} |
|
612 |
||
613 |
void disableFailover() { |
|
614 |
serviceIterator = null; |
|
615 |
initType = 0; |
|
616 |
initParams = null; |
|
617 |
initRandom = null; |
|
618 |
} |
|
619 |
||
620 |
// engine method |
|
621 |
public void initialize(int keysize, SecureRandom random) { |
|
622 |
if (serviceIterator == null) { |
|
623 |
spi.initialize(keysize, random); |
|
624 |
return; |
|
625 |
} |
|
626 |
RuntimeException failure = null; |
|
627 |
KeyPairGeneratorSpi mySpi = spi; |
|
628 |
do { |
|
629 |
try { |
|
630 |
mySpi.initialize(keysize, random); |
|
631 |
initType = I_SIZE; |
|
632 |
initKeySize = keysize; |
|
633 |
initParams = null; |
|
634 |
initRandom = random; |
|
635 |
return; |
|
636 |
} catch (RuntimeException e) { |
|
637 |
if (failure == null) { |
|
638 |
failure = e; |
|
639 |
} |
|
640 |
mySpi = nextSpi(mySpi, false); |
|
641 |
} |
|
642 |
} while (mySpi != null); |
|
643 |
throw failure; |
|
644 |
} |
|
645 |
||
646 |
// engine method |
|
647 |
public void initialize(AlgorithmParameterSpec params, |
|
648 |
SecureRandom random) throws InvalidAlgorithmParameterException { |
|
649 |
if (serviceIterator == null) { |
|
650 |
spi.initialize(params, random); |
|
651 |
return; |
|
652 |
} |
|
653 |
Exception failure = null; |
|
654 |
KeyPairGeneratorSpi mySpi = spi; |
|
655 |
do { |
|
656 |
try { |
|
657 |
mySpi.initialize(params, random); |
|
658 |
initType = I_PARAMS; |
|
659 |
initKeySize = 0; |
|
660 |
initParams = params; |
|
661 |
initRandom = random; |
|
662 |
return; |
|
663 |
} catch (Exception e) { |
|
664 |
if (failure == null) { |
|
665 |
failure = e; |
|
666 |
} |
|
667 |
mySpi = nextSpi(mySpi, false); |
|
668 |
} |
|
669 |
} while (mySpi != null); |
|
670 |
if (failure instanceof RuntimeException) { |
|
671 |
throw (RuntimeException)failure; |
|
672 |
} |
|
673 |
// must be an InvalidAlgorithmParameterException |
|
674 |
throw (InvalidAlgorithmParameterException)failure; |
|
675 |
} |
|
676 |
||
677 |
// engine method |
|
678 |
public KeyPair generateKeyPair() { |
|
679 |
if (serviceIterator == null) { |
|
680 |
return spi.generateKeyPair(); |
|
681 |
} |
|
682 |
RuntimeException failure = null; |
|
683 |
KeyPairGeneratorSpi mySpi = spi; |
|
684 |
do { |
|
685 |
try { |
|
686 |
return mySpi.generateKeyPair(); |
|
687 |
} catch (RuntimeException e) { |
|
688 |
if (failure == null) { |
|
689 |
failure = e; |
|
690 |
} |
|
691 |
mySpi = nextSpi(mySpi, true); |
|
692 |
} |
|
693 |
} while (mySpi != null); |
|
694 |
throw failure; |
|
695 |
} |
|
696 |
} |
|
697 |
||
698 |
} |