src/java.base/share/classes/java/security/KeyPairGenerator.java
author jboes
Fri, 20 Sep 2019 11:07:52 +0100
changeset 58242 94bb65cb37d3
parent 47478 438e0c9f2f17
permissions -rw-r--r--
8230648: Replace @exception tag with @throws in java.base Summary: Minor coding style update of javadoc tag in any file in java.base Reviewed-by: prappo, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47478
diff changeset
     2
 * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.spec.AlgorithmParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.Provider.Service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.jca.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.jca.GetInstance.Instance;
26736
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
    36
import sun.security.util.Debug;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * The KeyPairGenerator class is used to generate pairs of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * 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
    41
 * {@code getInstance} factory methods (static methods that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * return instances of a given class).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p>A Key pair generator for a particular algorithm creates a public/private
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * key pair that can be used with this algorithm. It also associates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * algorithm-specific parameters with each of the generated keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p>There are two ways to generate a key pair: in an algorithm-independent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * manner, and in an algorithm-specific manner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * The only difference between the two is the initialization of the object:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <li><b>Algorithm-Independent Initialization</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <p>All key pair generators share the concepts of a keysize and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * source of randomness. The keysize is interpreted differently for different
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * algorithms (e.g., in the case of the <i>DSA</i> algorithm, the keysize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * corresponds to the length of the modulus).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * There is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * {@link #initialize(int, java.security.SecureRandom) initialize}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * method in this KeyPairGenerator class that takes these two universally
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * 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
    62
 * {@code keysize} argument, and uses the {@code SecureRandom}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * implementation of the highest-priority installed provider as the source
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * 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
    65
 * of {@code SecureRandom}, a system-provided source of randomness is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * used.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <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
    69
 * algorithm-independent {@code initialize} methods, it is up to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * provider what to do about the algorithm-specific parameters (if any) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * associated with each of the keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <p>If the algorithm is the <i>DSA</i> algorithm, and the keysize (modulus
36249
8b92e4bedbd4 8138653: Default key sizes for the AlgorithmParameterGenerator and KeyPairGenerator implementations should be upgraded
mullan
parents: 33241
diff changeset
    74
 * size) is 512, 768, 1024, or 2048, 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
    75
 * 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
    76
 * {@code g} parameters. If the modulus size is not one of the above
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * values, the <i>Sun</i> provider creates a new set of parameters. Other
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * providers might have precomputed parameter sets for more than just the
40789
43b42538af90 8165413: Typos in javadoc: extra period, wrong number, misspelled word
igerasim
parents: 37348
diff changeset
    79
 * modulus sizes mentioned above. Still others might not have a list of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * precomputed parameters at all and instead always create new parameter sets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <li><b>Algorithm-Specific Initialization</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * <p>For situations where a set of algorithm-specific parameters already
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * exists (e.g., so-called <i>community parameters</i> in DSA), there are two
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * {@link #initialize(java.security.spec.AlgorithmParameterSpec)
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
    86
 * initialize} methods that have an {@code AlgorithmParameterSpec}
47478
438e0c9f2f17 8190382: fix small typographic errors in comments
smarks
parents: 47216
diff changeset
    87
 * argument. One also has a {@code SecureRandom} argument, while
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
    88
 * the other uses the {@code SecureRandom}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * implementation of the highest-priority installed provider as the source
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * 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
    91
 * of {@code SecureRandom}, a system-provided source of randomness is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * used.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * <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
    96
 * (via a call to an {@code initialize} method), each provider must
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * supply (and document) a default initialization.
45435
7a91c865edd4 8180635: (doc) Clarify the compatibility and interoperability issue when using provider default values
valeriep
parents: 45434
diff changeset
    98
 * See the Keysize Restriction sections of the
45665
6f21cd7ec80e 8178114: Fix guide links in security APIs
wetmore
parents: 45435
diff changeset
    99
 * {@extLink security_guide_jdk_providers JDK Providers}
45435
7a91c865edd4 8180635: (doc) Clarify the compatibility and interoperability issue when using provider default values
valeriep
parents: 45434
diff changeset
   100
 * document for information on the KeyPairGenerator defaults used by
7a91c865edd4 8180635: (doc) Clarify the compatibility and interoperability issue when using provider default values
valeriep
parents: 45434
diff changeset
   101
 * JDK providers.
7a91c865edd4 8180635: (doc) Clarify the compatibility and interoperability issue when using provider default values
valeriep
parents: 45434
diff changeset
   102
 * However, note that defaults may vary across different providers.
7a91c865edd4 8180635: (doc) Clarify the compatibility and interoperability issue when using provider default values
valeriep
parents: 45434
diff changeset
   103
 * Additionally, the default value for a provider may change in a future
7a91c865edd4 8180635: (doc) Clarify the compatibility and interoperability issue when using provider default values
valeriep
parents: 45434
diff changeset
   104
 * version. Therefore, it is recommended to explicitly initialize the
7a91c865edd4 8180635: (doc) Clarify the compatibility and interoperability issue when using provider default values
valeriep
parents: 45434
diff changeset
   105
 * KeyPairGenerator instead of relying on provider-specific defaults.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * <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
   108
 * {@code KeyPairGeneratorSpi} for historical reasons.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * 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
   110
 * this {@code KeyPairGenerator} class; all the methods in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * the superclass are intended for cryptographic service providers who wish to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * supply their own implementations of key pair generators.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   114
 * <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
   115
 * following standard {@code KeyPairGenerator} algorithms and keysizes in
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   116
 * parentheses:
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   117
 * <ul>
32647
53b8fd1f3840 8015388: Required algorithms for JDK 9
mullan
parents: 26736
diff changeset
   118
 * <li>{@code DiffieHellman} (1024, 2048, 4096)</li>
53b8fd1f3840 8015388: Required algorithms for JDK 9
mullan
parents: 26736
diff changeset
   119
 * <li>{@code DSA} (1024, 2048)</li>
53b8fd1f3840 8015388: Required algorithms for JDK 9
mullan
parents: 26736
diff changeset
   120
 * <li>{@code RSA} (1024, 2048, 4096)</li>
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   121
 * </ul>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   122
 * These algorithms are described in the <a href=
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 41826
diff changeset
   123
 * "{@docRoot}/../specs/security/standard-names.html#keypairgenerator-algorithms">
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   124
 * KeyPairGenerator section</a> of the
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 41826
diff changeset
   125
 * Java Security Standard Algorithm Names Specification.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   126
 * 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
   127
 * other algorithms are supported.
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   128
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * @author Benjamin Renaud
45434
4582657c7260 8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents: 45118
diff changeset
   130
 * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * @see java.security.spec.AlgorithmParameterSpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
public abstract class KeyPairGenerator extends KeyPairGeneratorSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
26736
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   137
    private static final Debug pdebug =
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   138
                        Debug.getInstance("provider", "Provider");
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   139
    private static final boolean skipDebug =
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   140
        Debug.isOn("engine=") && !Debug.isOn("keypairgenerator");
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   141
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    private final String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    // The provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    Provider provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Creates a KeyPairGenerator object for the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param algorithm the standard string name of the algorithm.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   151
     * See the KeyPairGenerator section in the <a href=
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 41826
diff changeset
   152
     * "{@docRoot}/../specs/security/standard-names.html#keypairgenerator-algorithms">
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 41826
diff changeset
   153
     * Java Security Standard Algorithm Names Specification</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    protected KeyPairGenerator(String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * 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
   162
     * See the KeyPairGenerator section in the <a href=
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 41826
diff changeset
   163
     * "{@docRoot}/../specs/security/standard-names.html#keypairgenerator-algorithms">
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 41826
diff changeset
   164
     * Java Security Standard Algorithm Names Specification</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @return the standard string name of the algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return this.algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    private static KeyPairGenerator getInstance(Instance instance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        KeyPairGenerator kpg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if (instance.impl instanceof KeyPairGenerator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            kpg = (KeyPairGenerator)instance.impl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            KeyPairGeneratorSpi spi = (KeyPairGeneratorSpi)instance.impl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            kpg = new Delegate(spi, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        kpg.provider = instance.provider;
26736
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   183
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   184
        if (!skipDebug && pdebug != null) {
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   185
            pdebug.println("KeyPairGenerator." + algorithm +
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   186
                " algorithm from: " + kpg.provider.getName());
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   187
        }
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   188
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        return kpg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Returns a KeyPairGenerator object that generates public/private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * key pairs for the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * <p> This method traverses the list of registered security Providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * starting with the most preferred Provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * A new KeyPairGenerator object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * KeyPairGeneratorSpi implementation from the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Provider that supports the specified algorithm is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   205
     * @implNote
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   206
     * The JDK Reference Implementation additionally uses the
37348
9ccec3170d5e 8152205: jdk.security.provider.preferred is ambiguously documented
ascarpino
parents: 36249
diff changeset
   207
     * {@code jdk.security.provider.preferred}
9ccec3170d5e 8152205: jdk.security.provider.preferred is ambiguously documented
ascarpino
parents: 36249
diff changeset
   208
     * {@link Security#getProperty(String) Security} property to determine
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   209
     * the preferred provider order for the specified algorithm. This
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   210
     * may be different than the order of providers returned by
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   211
     * {@link Security#getProviders() Security.getProviders()}.
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   212
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @param algorithm the standard string name of the algorithm.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   214
     * See the KeyPairGenerator section in the <a href=
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 41826
diff changeset
   215
     * "{@docRoot}/../specs/security/standard-names.html#keypairgenerator-algorithms">
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 41826
diff changeset
   216
     * Java Security Standard Algorithm Names Specification</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 40789
diff changeset
   219
     * @return the new {@code KeyPairGenerator} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 40789
diff changeset
   221
     * @throws NoSuchAlgorithmException if no {@code Provider} supports a
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 40789
diff changeset
   222
     *         {@code KeyPairGeneratorSpi} implementation for the
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 40789
diff changeset
   223
     *         specified algorithm
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 40789
diff changeset
   224
     *
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 40789
diff changeset
   225
     * @throws NullPointerException if {@code algorithm} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    public static KeyPairGenerator getInstance(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            throws NoSuchAlgorithmException {
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 40789
diff changeset
   231
        Objects.requireNonNull(algorithm, "null algorithm name");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        List<Service> list =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                GetInstance.getServices("KeyPairGenerator", algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        Iterator<Service> t = list.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if (t.hasNext() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                (algorithm + " KeyPairGenerator not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        // find a working Spi or KeyPairGenerator subclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        NoSuchAlgorithmException failure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            Service s = t.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                Instance instance =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    GetInstance.getInstance(s, KeyPairGeneratorSpi.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                if (instance.impl instanceof KeyPairGenerator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    return getInstance(instance, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    return new Delegate(instance, t, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                if (failure == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        } while (t.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        throw failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * Returns a KeyPairGenerator object that generates public/private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * key pairs for the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * <p> A new KeyPairGenerator object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * KeyPairGeneratorSpi implementation from the specified provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * is returned.  The specified provider must be registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @param algorithm the standard string name of the algorithm.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   273
     * See the KeyPairGenerator section in the <a href=
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 41826
diff changeset
   274
     * "{@docRoot}/../specs/security/standard-names.html#keypairgenerator-algorithms">
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 41826
diff changeset
   275
     * Java Security Standard Algorithm Names Specification</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @param provider the string name of the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 40789
diff changeset
   280
     * @return the new {@code KeyPairGenerator} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 40789
diff changeset
   282
     * @throws IllegalArgumentException if the provider name is {@code null}
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 40789
diff changeset
   283
     *         or empty
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 40789
diff changeset
   285
     * @throws NoSuchAlgorithmException if a {@code KeyPairGeneratorSpi}
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 40789
diff changeset
   286
     *         implementation for the specified algorithm is not
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 40789
diff changeset
   287
     *         available from the specified provider
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 40789
diff changeset
   289
     * @throws NoSuchProviderException if the specified provider is not
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 40789
diff changeset
   290
     *         registered in the security provider list
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 40789
diff changeset
   291
     *
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 40789
diff changeset
   292
     * @throws NullPointerException if {@code algorithm} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    public static KeyPairGenerator getInstance(String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            String provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            throws NoSuchAlgorithmException, NoSuchProviderException {
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 40789
diff changeset
   299
        Objects.requireNonNull(algorithm, "null algorithm name");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        Instance instance = GetInstance.getInstance("KeyPairGenerator",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                KeyPairGeneratorSpi.class, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        return getInstance(instance, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * Returns a KeyPairGenerator object that generates public/private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * key pairs for the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * <p> A new KeyPairGenerator object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * KeyPairGeneratorSpi implementation from the specified Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * object is returned.  Note that the specified Provider object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * does not have to be registered in the provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @param algorithm the standard string name of the algorithm.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   315
     * See the KeyPairGenerator section in the <a href=
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 41826
diff changeset
   316
     * "{@docRoot}/../specs/security/standard-names.html#keypairgenerator-algorithms">
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 41826
diff changeset
   317
     * Java Security Standard Algorithm Names Specification</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @param provider the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 40789
diff changeset
   322
     * @return the new {@code KeyPairGenerator} object
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 40789
diff changeset
   323
     *
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 40789
diff changeset
   324
     * @throws IllegalArgumentException if the specified provider is
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 40789
diff changeset
   325
     *         {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 40789
diff changeset
   327
     * @throws NoSuchAlgorithmException if a {@code KeyPairGeneratorSpi}
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 40789
diff changeset
   328
     *         implementation for the specified algorithm is not available
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 40789
diff changeset
   329
     *         from the specified {@code Provider} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 40789
diff changeset
   331
     * @throws NullPointerException if {@code algorithm} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    public static KeyPairGenerator getInstance(String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            Provider provider) throws NoSuchAlgorithmException {
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 40789
diff changeset
   339
        Objects.requireNonNull(algorithm, "null algorithm name");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        Instance instance = GetInstance.getInstance("KeyPairGenerator",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                KeyPairGeneratorSpi.class, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        return getInstance(instance, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * Returns the provider of this key pair generator object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @return the provider of this key pair generator object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    public final Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        disableFailover();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        return this.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    void disableFailover() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        // empty, overridden in Delegate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * 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
   361
     * a default parameter set and the {@code SecureRandom}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * implementation of the highest-priority installed provider as the source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * (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
   365
     * {@code SecureRandom}, a system-provided source of randomness is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * used.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @param keysize the keysize. This is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * algorithm-specific metric, such as modulus length, specified in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * number of bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47478
diff changeset
   372
     * @throws    InvalidParameterException if the {@code keysize} is not
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * supported by this KeyPairGenerator object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    public void initialize(int keysize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        initialize(keysize, JCAUtil.getSecureRandom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * Initializes the key pair generator for a certain keysize with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * the given source of randomness (and a default parameter set).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @param keysize the keysize. This is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * algorithm-specific metric, such as modulus length, specified in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * number of bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * @param random the source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47478
diff changeset
   388
     * @throws    InvalidParameterException if the {@code keysize} is not
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * supported by this KeyPairGenerator object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    public void initialize(int keysize, SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        // This does nothing, because either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        // 1. the implementation object returned by getInstance() is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        //    instance of KeyPairGenerator which has its own
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        //    initialize(keysize, random) method, so the application would
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        //    be calling that method directly, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        // 2. the implementation returned by getInstance() is an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        //    of Delegate, in which case initialize(keysize, random) is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        //    overridden to call the corresponding SPI method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        // (This is a special case, because the API and SPI method have the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        // same name.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * 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
   408
     * set and the {@code SecureRandom}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * implementation of the highest-priority installed provider as the source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * (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
   412
     * {@code SecureRandom}, a system-provided source of randomness is
40789
43b42538af90 8165413: Typos in javadoc: extra period, wrong number, misspelled word
igerasim
parents: 37348
diff changeset
   413
     * used.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * <p>This concrete method has been added to this previously-defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * abstract class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * This method calls the KeyPairGeneratorSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * {@link KeyPairGeneratorSpi#initialize(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * java.security.spec.AlgorithmParameterSpec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * java.security.SecureRandom) initialize} method,
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
   421
     * passing it {@code params} and a source of randomness (obtained
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * from the highest-priority installed provider or system-provided if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * of the installed providers supply one).
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
   424
     * That {@code initialize} method always throws an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * UnsupportedOperationException if it is not overridden by the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * @param params the parameter set used to generate the keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47478
diff changeset
   429
     * @throws    InvalidAlgorithmParameterException if the given parameters
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * are inappropriate for this key pair generator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    public void initialize(AlgorithmParameterSpec params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            throws InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        initialize(params, JCAUtil.getSecureRandom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * Initializes the key pair generator with the given parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * set and source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * <p>This concrete method has been added to this previously-defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * abstract class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * This method calls the KeyPairGeneratorSpi {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * KeyPairGeneratorSpi#initialize(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * java.security.spec.AlgorithmParameterSpec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * java.security.SecureRandom) initialize} method,
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
   449
     * passing it {@code params} and {@code random}.
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
   450
     * That {@code initialize}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * method always throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * UnsupportedOperationException if it is not overridden by the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * @param params the parameter set used to generate the keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * @param random the source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47478
diff changeset
   457
     * @throws    InvalidAlgorithmParameterException if the given parameters
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * are inappropriate for this key pair generator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    public void initialize(AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                           SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        throws InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        // This does nothing, because either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        // 1. the implementation object returned by getInstance() is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        //    instance of KeyPairGenerator which has its own
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        //    initialize(params, random) method, so the application would
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        //    be calling that method directly, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        // 2. the implementation returned by getInstance() is an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        //    of Delegate, in which case initialize(params, random) is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        //    overridden to call the corresponding SPI method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        // (This is a special case, because the API and SPI method have the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        // same name.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * Generates a key pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * <p>If this KeyPairGenerator has not been initialized explicitly,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * provider-specific defaults will be used for the size and other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * (algorithm-specific) values of the generated keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * <p>This will generate a new key pair every time it is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * <p>This method is functionally equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * {@link #generateKeyPair() generateKeyPair}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * @return the generated key pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    public final KeyPair genKeyPair() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        return generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * Generates a key pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * <p>If this KeyPairGenerator has not been initialized explicitly,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * provider-specific defaults will be used for the size and other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * (algorithm-specific) values of the generated keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * <p>This will generate a new key pair every time it is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * <p>This method is functionally equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * {@link #genKeyPair() genKeyPair}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * @return the generated key pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    public KeyPair generateKeyPair() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        // This does nothing (except returning null), because either:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        // 1. the implementation object returned by getInstance() is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        //    instance of KeyPairGenerator which has its own implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        //    of generateKeyPair (overriding this one), so the application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        //    would be calling that method directly, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        // 2. the implementation returned by getInstance() is an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        //    of Delegate, in which case generateKeyPair is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        //    overridden to invoke the corresponding SPI method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        // (This is a special case, because in JDK 1.1.x the generateKeyPair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        // method was used both as an API and a SPI method.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * The following class allows providers to extend from KeyPairGeneratorSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * rather than from KeyPairGenerator. It represents a KeyPairGenerator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * with an encapsulated, provider-supplied SPI object (of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * KeyPairGeneratorSpi).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * If the provider implementation is an instance of KeyPairGeneratorSpi,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * the getInstance() methods above return an instance of this class, with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * the SPI object encapsulated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * Note: All SPI methods from the original KeyPairGenerator class have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * moved up the hierarchy into a new class (KeyPairGeneratorSpi), which has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * been interposed in the hierarchy between the API (KeyPairGenerator)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * and its original parent (Object).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    // error failover notes:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    //  . we failover if the implementation throws an error during init
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    //    by retrying the init on other providers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    //  . we also failover if the init succeeded but the subsequent call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    //    to generateKeyPair() fails. In order for this to work, we need
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    //    to remember the parameters to the last successful call to init
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    //    and initialize() the next spi using them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    //  . although not specified, KeyPairGenerators could be thread safe,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    //    so we make sure we do not interfere with that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    //  . failover is not available, if:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    //    . getInstance(algorithm, provider) was used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    //    . a provider extends KeyPairGenerator rather than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    //      KeyPairGeneratorSpi (JDK 1.1 style)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    //    . once getProvider() is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    private static final class Delegate extends KeyPairGenerator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        // The provider implementation (delegate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        private volatile KeyPairGeneratorSpi spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        private final Object lock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        private Iterator<Service> serviceIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32647
diff changeset
   575
        private static final int I_NONE   = 1;
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32647
diff changeset
   576
        private static final int I_SIZE   = 2;
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32647
diff changeset
   577
        private static final int I_PARAMS = 3;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        private int initType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        private int initKeySize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        private AlgorithmParameterSpec initParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        private SecureRandom initRandom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        // constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        Delegate(KeyPairGeneratorSpi spi, String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            super(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            this.spi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        Delegate(Instance instance, Iterator<Service> serviceIterator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            super(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            spi = (KeyPairGeneratorSpi)instance.impl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            provider = instance.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            this.serviceIterator = serviceIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            initType = I_NONE;
26736
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   597
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   598
            if (!skipDebug && pdebug != null) {
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   599
                pdebug.println("KeyPairGenerator." + algorithm +
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   600
                    " algorithm from: " + provider.getName());
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   601
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
         * Update the active spi of this class and return the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
         * implementation for failover. If no more implemenations are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
         * available, this method returns null. However, the active spi of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
         * this class is never set to null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        private KeyPairGeneratorSpi nextSpi(KeyPairGeneratorSpi oldSpi,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                boolean reinit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                // somebody else did a failover concurrently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                // try that spi now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                if ((oldSpi != null) && (oldSpi != spi)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                    return spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                while (serviceIterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                    Service s = serviceIterator.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                        Object inst = s.newInstance(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                        // ignore non-spis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                        if (inst instanceof KeyPairGeneratorSpi == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                        if (inst instanceof KeyPairGenerator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                        KeyPairGeneratorSpi spi = (KeyPairGeneratorSpi)inst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                        if (reinit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                            if (initType == I_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                                spi.initialize(initKeySize, initRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                            } else if (initType == I_PARAMS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                                spi.initialize(initParams, initRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                            } else if (initType != I_NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                                throw new AssertionError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                                    ("KeyPairGenerator initType: " + initType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                        provider = s.getProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                        this.spi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                        return spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                        // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                disableFailover();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        void disableFailover() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            serviceIterator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            initType = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            initParams = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            initRandom = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        // engine method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        public void initialize(int keysize, SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                spi.initialize(keysize, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            RuntimeException failure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            KeyPairGeneratorSpi mySpi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                    mySpi.initialize(keysize, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                    initType = I_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                    initKeySize = keysize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                    initParams = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                    initRandom = random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                    if (failure == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                        failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                    mySpi = nextSpi(mySpi, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            } while (mySpi != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            throw failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        // engine method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        public void initialize(AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                SecureRandom random) throws InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                spi.initialize(params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            Exception failure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            KeyPairGeneratorSpi mySpi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                    mySpi.initialize(params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                    initType = I_PARAMS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                    initKeySize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                    initParams = params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                    initRandom = random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                    if (failure == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                        failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                    mySpi = nextSpi(mySpi, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            } while (mySpi != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            if (failure instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                throw (RuntimeException)failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            // must be an InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            throw (InvalidAlgorithmParameterException)failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        // engine method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        public KeyPair generateKeyPair() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                return spi.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            RuntimeException failure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            KeyPairGeneratorSpi mySpi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                    return mySpi.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                    if (failure == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                        failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                    mySpi = nextSpi(mySpi, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            } while (mySpi != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            throw failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
}