src/java.base/share/classes/javax/crypto/KeyGenerator.java
author mullan
Wed, 13 Nov 2019 13:43:06 -0500
changeset 59059 27a266d5fb13
parent 47216 71c04702a3d5
permissions -rw-r--r--
8214483: Remove algorithms that use MD5 or DES from security requirements Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
59059
27a266d5fb13 8214483: Remove algorithms that use MD5 or DES from security requirements
mullan
parents: 47216
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 javax.crypto;
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.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.Provider.Service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.spec.*;
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
 * This class provides the functionality of a secret (symmetric) key generator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26861
diff changeset
    41
 * <p>Key generators are constructed using one of the {@code getInstance}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * class methods of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p>KeyGenerator objects are reusable, i.e., after a key has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * generated, the same KeyGenerator object can be re-used to generate further
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * 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: 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 generators share the concepts of a <i>keysize</i> and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <i>source of randomness</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * There is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * {@link #init(int, java.security.SecureRandom) init}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * method in this KeyGenerator class that takes these two universally
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * shared types of arguments. There is also one that takes just a
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26861
diff changeset
    60
 * {@code keysize} argument, and uses the SecureRandom implementation
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * of the highest-priority installed provider as the source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * (or a system-provided source of randomness if none of the installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * providers supply a SecureRandom implementation), and one that takes just a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p>Since no other parameters are specified when you call the above
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26861
diff changeset
    67
 * algorithm-independent {@code init} methods, it is up to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * provider what to do about the algorithm-specific parameters (if any) to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * associated with each of the keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <li><b>Algorithm-Specific Initialization</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <p>For situations where a set of algorithm-specific parameters already
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * exists, there are two
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * {@link #init(java.security.spec.AlgorithmParameterSpec) init}
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26861
diff changeset
    75
 * methods that have an {@code AlgorithmParameterSpec}
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26861
diff changeset
    76
 * argument. One also has a {@code SecureRandom} argument, while the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * other uses the SecureRandom implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * of the highest-priority installed provider as the source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * (or a system-provided source of randomness if none of the installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * providers supply a SecureRandom implementation).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * <p>In case the client does not explicitly initialize the KeyGenerator
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26861
diff changeset
    84
 * (via a call to an {@code init} method), each provider must
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * supply (and document) a default initialization.
45435
7a91c865edd4 8180635: (doc) Clarify the compatibility and interoperability issue when using provider default values
valeriep
parents: 45118
diff changeset
    86
 * See the Keysize Restriction sections of the
45665
6f21cd7ec80e 8178114: Fix guide links in security APIs
wetmore
parents: 45435
diff changeset
    87
 * {@extLink security_guide_jdk_providers JDK Providers}
45435
7a91c865edd4 8180635: (doc) Clarify the compatibility and interoperability issue when using provider default values
valeriep
parents: 45118
diff changeset
    88
 * document for information on the KeyGenerator defaults used by
7a91c865edd4 8180635: (doc) Clarify the compatibility and interoperability issue when using provider default values
valeriep
parents: 45118
diff changeset
    89
 * JDK providers.
7a91c865edd4 8180635: (doc) Clarify the compatibility and interoperability issue when using provider default values
valeriep
parents: 45118
diff changeset
    90
 * 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: 45118
diff changeset
    91
 * 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: 45118
diff changeset
    92
 * 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: 45118
diff changeset
    93
 * KeyGenerator instead of relying on provider-specific defaults.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    95
 * <p> Every implementation of the Java platform is required to support the
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26861
diff changeset
    96
 * following standard {@code KeyGenerator} algorithms with the keysizes in
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    97
 * parentheses:
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    98
 * <ul>
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26861
diff changeset
    99
 * <li>{@code AES} (128)</li>
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26861
diff changeset
   100
 * <li>{@code DESede} (168)</li>
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26861
diff changeset
   101
 * <li>{@code HmacSHA1}</li>
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26861
diff changeset
   102
 * <li>{@code HmacSHA256}</li>
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   103
 * </ul>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   104
 * These algorithms are described in the <a href=
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 42780
diff changeset
   105
 * "{@docRoot}/../specs/security/standard-names.html#keygenerator-algorithms">
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   106
 * KeyGenerator section</a> of the
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 42780
diff changeset
   107
 * Java Security Standard Algorithm Names Specification.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   108
 * 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
   109
 * other algorithms are supported.
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   110
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * @see SecretKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
public class KeyGenerator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
26736
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   119
    private static final Debug pdebug =
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   120
                        Debug.getInstance("provider", "Provider");
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   121
    private static final boolean skipDebug =
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   122
        Debug.isOn("engine=") && !Debug.isOn("keygenerator");
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   123
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    // see java.security.KeyPairGenerator for failover notes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32275
diff changeset
   126
    private static final int I_NONE   = 1;
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32275
diff changeset
   127
    private static final int I_RANDOM = 2;
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32275
diff changeset
   128
    private static final int I_PARAMS = 3;
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32275
diff changeset
   129
    private static final int I_SIZE   = 4;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    // The provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private Provider provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    // The provider implementation (delegate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private volatile KeyGeneratorSpi spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    // The algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private final String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private final Object lock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   142
    private Iterator<Service> serviceIterator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    private int initType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private int initKeySize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    private AlgorithmParameterSpec initParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    private SecureRandom initRandom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Creates a KeyGenerator object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @param keyGenSpi the delegate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param provider the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param algorithm the algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    protected KeyGenerator(KeyGeneratorSpi keyGenSpi, Provider provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                           String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        this.spi = keyGenSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        this.provider = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        this.algorithm = algorithm;
26736
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   161
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   162
        if (!skipDebug && pdebug != null) {
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   163
            pdebug.println("KeyGenerator." + algorithm + " algorithm from: " +
42780
7781326fff20 8170876: NPE in JCE engine classes with java.security.debug=provider
mullan
parents: 41826
diff changeset
   164
                getProviderName());
26736
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   165
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    private KeyGenerator(String algorithm) throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        this.algorithm = algorithm;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   170
        List<Service> list =
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   171
                GetInstance.getServices("KeyGenerator", algorithm);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        serviceIterator = list.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        initType = I_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        // fetch and instantiate initial spi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (nextSpi(null, false) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                (algorithm + " KeyGenerator not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
26736
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   179
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   180
        if (!skipDebug && pdebug != null) {
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   181
            pdebug.println("KeyGenerator." + algorithm + " algorithm from: " +
42780
7781326fff20 8170876: NPE in JCE engine classes with java.security.debug=provider
mullan
parents: 41826
diff changeset
   182
                getProviderName());
26736
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   183
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
42780
7781326fff20 8170876: NPE in JCE engine classes with java.security.debug=provider
mullan
parents: 41826
diff changeset
   186
    private String getProviderName() {
7781326fff20 8170876: NPE in JCE engine classes with java.security.debug=provider
mullan
parents: 41826
diff changeset
   187
        return (provider == null) ? "(no provider)" : provider.getName();
7781326fff20 8170876: NPE in JCE engine classes with java.security.debug=provider
mullan
parents: 41826
diff changeset
   188
    }
7781326fff20 8170876: NPE in JCE engine classes with java.security.debug=provider
mullan
parents: 41826
diff changeset
   189
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    /**
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26861
diff changeset
   191
     * Returns the algorithm name of this {@code KeyGenerator} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * <p>This is the same name that was specified in one of the
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26861
diff changeset
   194
     * {@code getInstance} calls that created this
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26861
diff changeset
   195
     * {@code KeyGenerator} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26861
diff changeset
   197
     * @return the algorithm name of this {@code KeyGenerator} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public final String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        return this.algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    /**
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26861
diff changeset
   204
     * Returns a {@code KeyGenerator} object that generates secret keys
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * for the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * <p> This method traverses the list of registered security Providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * starting with the most preferred Provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * A new KeyGenerator object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * KeyGeneratorSpi implementation from the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Provider that supports the specified algorithm is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   216
     * @implNote
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   217
     * The JDK Reference Implementation additionally uses the
37348
9ccec3170d5e 8152205: jdk.security.provider.preferred is ambiguously documented
ascarpino
parents: 33241
diff changeset
   218
     * {@code jdk.security.provider.preferred}
9ccec3170d5e 8152205: jdk.security.provider.preferred is ambiguously documented
ascarpino
parents: 33241
diff changeset
   219
     * {@link Security#getProperty(String) Security} property to determine
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   220
     * the preferred provider order for the specified algorithm. This
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   221
     * may be different than the order of providers returned by
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   222
     * {@link Security#getProviders() Security.getProviders()}.
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32649
diff changeset
   223
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @param algorithm the standard name of the requested key algorithm.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   225
     * See the KeyGenerator section in the <a href=
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 42780
diff changeset
   226
     * "{@docRoot}/../specs/security/standard-names.html#keygenerator-algorithms">
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 42780
diff changeset
   227
     * Java Security Standard Algorithm Names Specification</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   230
     * @return the new {@code KeyGenerator} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   232
     * @throws NoSuchAlgorithmException if no {@code Provider} supports a
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   233
     *         {@code KeyGeneratorSpi} implementation for the
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   234
     *         specified algorithm
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   235
     *
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   236
     * @throws NullPointerException if {@code algorithm} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    public static final KeyGenerator getInstance(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            throws NoSuchAlgorithmException {
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   242
        Objects.requireNonNull(algorithm, "null algorithm name");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        return new KeyGenerator(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26861
diff changeset
   247
     * Returns a {@code KeyGenerator} object that generates secret keys
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * for the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * <p> A new KeyGenerator object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * KeyGeneratorSpi implementation from the specified provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * is returned.  The specified provider must be registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @param algorithm the standard name of the requested key algorithm.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   259
     * See the KeyGenerator section in the <a href=
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 42780
diff changeset
   260
     * "{@docRoot}/../specs/security/standard-names.html#keygenerator-algorithms">
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 42780
diff changeset
   261
     * Java Security Standard Algorithm Names Specification</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @param provider the name of the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   266
     * @return the new {@code KeyGenerator} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   268
     * @throws IllegalArgumentException if the {@code provider}
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   269
     *         is {@code null} or empty
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   271
     * @throws NoSuchAlgorithmException if a {@code KeyGeneratorSpi}
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   272
     *         implementation for the specified algorithm is not
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   273
     *         available from the specified provider
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   275
     * @throws NoSuchProviderException if the specified provider is not
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   276
     *         registered in the security provider list
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   278
     * @throws NullPointerException if {@code algorithm} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    public static final KeyGenerator getInstance(String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            String provider) throws NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            NoSuchProviderException {
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   285
        Objects.requireNonNull(algorithm, "null algorithm name");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        Instance instance = JceSecurity.getInstance("KeyGenerator",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                KeyGeneratorSpi.class, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        return new KeyGenerator((KeyGeneratorSpi)instance.impl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                instance.provider, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    /**
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26861
diff changeset
   293
     * Returns a {@code KeyGenerator} object that generates secret keys
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * for the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * <p> A new KeyGenerator object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * KeyGeneratorSpi implementation from the specified Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * object is returned.  Note that the specified Provider object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * does not have to be registered in the provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @param algorithm the standard name of the requested key algorithm.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   302
     * See the KeyGenerator section in the <a href=
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 42780
diff changeset
   303
     * "{@docRoot}/../specs/security/standard-names.html#keygenerator-algorithms">
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 42780
diff changeset
   304
     * Java Security Standard Algorithm Names Specification</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @param provider the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   309
     * @return the new {@code KeyGenerator} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   311
     * @throws IllegalArgumentException if the {@code provider}
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   312
     *         is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   314
     * @throws NoSuchAlgorithmException if a {@code KeyGeneratorSpi}
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   315
     *         implementation for the specified algorithm is not available
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   316
     *         from the specified {@code Provider} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   318
     * @throws NullPointerException if {@code algorithm} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    public static final KeyGenerator getInstance(String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            Provider provider) throws NoSuchAlgorithmException {
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   324
        Objects.requireNonNull(algorithm, "null algorithm name");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        Instance instance = JceSecurity.getInstance("KeyGenerator",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                KeyGeneratorSpi.class, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        return new KeyGenerator((KeyGeneratorSpi)instance.impl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                instance.provider, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    /**
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26861
diff changeset
   332
     * Returns the provider of this {@code KeyGenerator} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26861
diff changeset
   334
     * @return the provider of this {@code KeyGenerator} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    public final Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            disableFailover();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            return provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * Update the active spi of this class and return the next
26861
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 26736
diff changeset
   345
     * implementation for failover. If no more implementations are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * available, this method returns null. However, the active spi of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * this class is never set to null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    private KeyGeneratorSpi nextSpi(KeyGeneratorSpi oldSpi,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            boolean reinit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            // somebody else did a failover concurrently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            // try that spi now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            if ((oldSpi != null) && (oldSpi != spi)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                return spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            while (serviceIterator.hasNext()) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   361
                Service s = serviceIterator.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                if (JceSecurity.canUseProvider(s.getProvider()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    Object inst = s.newInstance(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    // ignore non-spis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    if (inst instanceof KeyGeneratorSpi == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    KeyGeneratorSpi spi = (KeyGeneratorSpi)inst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    if (reinit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                        if (initType == I_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                            spi.engineInit(initKeySize, initRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                        } else if (initType == I_PARAMS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                            spi.engineInit(initParams, initRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                        } else if (initType == I_RANDOM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                            spi.engineInit(initRandom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                        } else if (initType != I_NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                            throw new AssertionError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                                ("KeyGenerator initType: " + initType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    provider = s.getProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                    this.spi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    return spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                    // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            disableFailover();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    void disableFailover() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        serviceIterator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        initType = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        initParams = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        initRandom = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * Initializes this key generator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * @param random the source of randomness for this generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    public final void init(SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            spi.engineInit(random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        RuntimeException failure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        KeyGeneratorSpi mySpi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                mySpi.engineInit(random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                initType = I_RANDOM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                initKeySize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                initParams = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                initRandom = random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                if (failure == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                    failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                mySpi = nextSpi(mySpi, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        } while (mySpi != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        throw failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * Initializes this key generator with the specified parameter set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * <p> If this key generator requires any random bytes, it will get them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * using the
18771
9dadb0719cea 8019772: Fix doclint issues in javax.crypto and javax.security subpackages
juh
parents: 10336
diff changeset
   438
     * {@link java.security.SecureRandom}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * implementation of the highest-priority installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * provider as the source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * (If none of the installed providers supply an implementation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * SecureRandom, a system-provided source of randomness will be used.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * @param params the key generation parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * @exception InvalidAlgorithmParameterException if the given parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * are inappropriate for this key generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    public final void init(AlgorithmParameterSpec params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        throws InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        init(params, JceSecurity.RANDOM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * Initializes this key generator with the specified parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * set and a user-provided source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * @param params the key generation parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * @param random the source of randomness for this key generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 26861
diff changeset
   462
     * @exception InvalidAlgorithmParameterException if {@code params} is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * inappropriate for this key generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    public final void init(AlgorithmParameterSpec params, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        throws InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            spi.engineInit(params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        Exception failure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        KeyGeneratorSpi mySpi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                mySpi.engineInit(params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                initType = I_PARAMS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                initKeySize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                initParams = params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                initRandom = random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                if (failure == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                    failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                mySpi = nextSpi(mySpi, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        } while (mySpi != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        if (failure instanceof InvalidAlgorithmParameterException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            throw (InvalidAlgorithmParameterException)failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        if (failure instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            throw (RuntimeException)failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        throw new InvalidAlgorithmParameterException("init() failed", failure);
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
     * Initializes this key generator for a certain keysize.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * <p> If this key generator requires any random bytes, it will get them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * using the
18771
9dadb0719cea 8019772: Fix doclint issues in javax.crypto and javax.security subpackages
juh
parents: 10336
diff changeset
   503
     * {@link java.security.SecureRandom}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * implementation of the highest-priority installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * provider as the source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * (If none of the installed providers supply an implementation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * SecureRandom, a system-provided source of randomness will be used.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * @param keysize the keysize. This is an algorithm-specific metric,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * specified in number of bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * @exception InvalidParameterException if the keysize is wrong or not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    public final void init(int keysize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        init(keysize, JceSecurity.RANDOM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * Initializes this key generator for a certain keysize, using a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * user-provided source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * @param keysize the keysize. This is an algorithm-specific metric,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * specified in number of bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * @param random the source of randomness for this key generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * @exception InvalidParameterException if the keysize is wrong or not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    public final void init(int keysize, SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            spi.engineInit(keysize, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        RuntimeException failure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        KeyGeneratorSpi mySpi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                mySpi.engineInit(keysize, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                initType = I_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                initKeySize = keysize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                initParams = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                initRandom = random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                if (failure == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                    failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                mySpi = nextSpi(mySpi, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        } while (mySpi != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        throw failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * Generates a secret key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * @return the new key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    public final SecretKey generateKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            return spi.engineGenerateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        RuntimeException failure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        KeyGeneratorSpi mySpi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                return mySpi.engineGenerateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                if (failure == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                    failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                mySpi = nextSpi(mySpi, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        } while (mySpi != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        throw failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
}