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