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