src/java.base/share/classes/javax/crypto/SecretKeyFactory.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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This class represents a factory for secret keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <P> Key factories are used to convert <I>keys</I> (opaque
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 25859
diff changeset
    41
 * cryptographic keys of type {@code Key}) into <I>key specifications</I>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * (transparent representations of the underlying key material), and vice
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * versa.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Secret key factories operate only on secret (symmetric) keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <P> Key factories are bi-directional, i.e., they allow to build an opaque
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * key object from a given key specification (key material), or to retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * the underlying key material of a key object in a suitable format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <P> Application developers should refer to their provider's documentation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * to find out which key specifications are supported by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * {@link #generateSecret(java.security.spec.KeySpec) generateSecret} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * {@link #getKeySpec(javax.crypto.SecretKey, java.lang.Class) getKeySpec}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * methods.
59059
27a266d5fb13 8214483: Remove algorithms that use MD5 or DES from security requirements
mullan
parents: 47216
diff changeset
    55
 * For example, the DESede (Triple DES) secret-key factory supplied by the
27a266d5fb13 8214483: Remove algorithms that use MD5 or DES from security requirements
mullan
parents: 47216
diff changeset
    56
 * "SunJCE" provider supports {@code DESedeKeySpec} as a transparent
27a266d5fb13 8214483: Remove algorithms that use MD5 or DES from security requirements
mullan
parents: 47216
diff changeset
    57
 * representation of Triple DES keys.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    59
 * <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: 25859
diff changeset
    60
 * following standard {@code SecretKeyFactory} algorithms:
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    61
 * <ul>
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 25859
diff changeset
    62
 * <li>{@code DESede}</li>
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    63
 * </ul>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    64
 * These algorithms are described in the <a href=
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 41826
diff changeset
    65
 * "{@docRoot}/../specs/security/standard-names.html#secretkeyfactory-algorithms">
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    66
 * SecretKeyFactory section</a> of the
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 41826
diff changeset
    67
 * Java Security Standard Algorithm Names Specification.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    68
 * 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
    69
 * other algorithms are supported.
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    70
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * @see SecretKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * @see javax.crypto.spec.DESedeKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * @see javax.crypto.spec.PBEKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
public class SecretKeyFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    // The provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private Provider provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // The algorithm associated with this factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private final String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    // The provider implementation (delegate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private volatile SecretKeyFactorySpi spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    // lock for mutex during provider selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private final Object lock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    // remaining services to try in provider selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    // null once provider is selected
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8575
diff changeset
    95
    private Iterator<Service> serviceIterator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Creates a SecretKeyFactory object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @param keyFacSpi the delegate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @param provider the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @param algorithm the secret-key algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    protected SecretKeyFactory(SecretKeyFactorySpi keyFacSpi,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                               Provider provider, String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        this.spi = keyFacSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        this.provider = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private SecretKeyFactory(String algorithm) throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        this.algorithm = algorithm;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8575
diff changeset
   113
        List<Service> list =
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8575
diff changeset
   114
                GetInstance.getServices("SecretKeyFactory", algorithm);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        serviceIterator = list.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        // fetch and instantiate initial spi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (nextSpi(null) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                (algorithm + " SecretKeyFactory not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 25859
diff changeset
   124
     * Returns a {@code SecretKeyFactory} object that converts
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * secret keys of the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * <p> This method traverses the list of registered security Providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * starting with the most preferred Provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * A new SecretKeyFactory object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * SecretKeyFactorySpi implementation from the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Provider that supports the specified algorithm is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32275
diff changeset
   136
     * @implNote
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32275
diff changeset
   137
     * The JDK Reference Implementation additionally uses the
37348
9ccec3170d5e 8152205: jdk.security.provider.preferred is ambiguously documented
ascarpino
parents: 33241
diff changeset
   138
     * {@code jdk.security.provider.preferred}
9ccec3170d5e 8152205: jdk.security.provider.preferred is ambiguously documented
ascarpino
parents: 33241
diff changeset
   139
     * {@link Security#getProperty(String) Security} property to determine
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32275
diff changeset
   140
     * the preferred provider order for the specified algorithm. This
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32275
diff changeset
   141
     * may be different than the order of providers returned by
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32275
diff changeset
   142
     * {@link Security#getProviders() Security.getProviders()}.
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 32275
diff changeset
   143
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @param algorithm the standard name of the requested secret-key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * algorithm.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   146
     * See the SecretKeyFactory section in the <a href=
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 41826
diff changeset
   147
     * "{@docRoot}/../specs/security/standard-names.html#secretkeyfactory-algorithms">
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 41826
diff changeset
   148
     * Java Security Standard Algorithm Names Specification</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   151
     * @return the new {@code SecretKeyFactory} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   153
     * @throws NoSuchAlgorithmException if no {@code Provider} supports a
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   154
     *         {@code SecretKeyFactorySpi} implementation for the
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   155
     *         specified algorithm
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   157
     * @throws NullPointerException if {@code algorithm} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public static final SecretKeyFactory getInstance(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            throws NoSuchAlgorithmException {
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   163
        Objects.requireNonNull(algorithm, "null algorithm name");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        return new SecretKeyFactory(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 25859
diff changeset
   168
     * Returns a {@code SecretKeyFactory} object that converts
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * secret keys of the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * <p> A new SecretKeyFactory object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * SecretKeyFactorySpi implementation from the specified provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * is returned.  The specified provider must be registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @param algorithm the standard name of the requested secret-key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * algorithm.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   181
     * See the SecretKeyFactory section in the <a href=
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 41826
diff changeset
   182
     * "{@docRoot}/../specs/security/standard-names.html#secretkeyfactory-algorithms">
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 41826
diff changeset
   183
     * Java Security Standard Algorithm Names Specification</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @param provider the name of the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   188
     * @return the new {@code SecretKeyFactory} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   190
     * @throws IllegalArgumentException if the {@code provider}
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   191
     *         is {@code null} or empty
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   193
     * @throws NoSuchAlgorithmException if a {@code SecretKeyFactorySpi}
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   194
     *         implementation for the specified algorithm is not
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   195
     *         available from the specified provider
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @throws NoSuchProviderException if the specified provider is not
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   198
     *         registered in the security provider list
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   200
     * @throws NullPointerException if {@code algorithm} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public static final SecretKeyFactory getInstance(String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            String provider) throws NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            NoSuchProviderException {
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   207
        Objects.requireNonNull(algorithm, "null algorithm name");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        Instance instance = JceSecurity.getInstance("SecretKeyFactory",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                SecretKeyFactorySpi.class, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        return new SecretKeyFactory((SecretKeyFactorySpi)instance.impl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                instance.provider, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /**
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 25859
diff changeset
   215
     * Returns a {@code SecretKeyFactory} object that converts
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * secret keys of the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * <p> A new SecretKeyFactory object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * SecretKeyFactorySpi implementation from the specified Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * object is returned.  Note that the specified Provider object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * does not have to be registered in the provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @param algorithm the standard name of the requested secret-key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * algorithm.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   225
     * See the SecretKeyFactory section in the <a href=
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 41826
diff changeset
   226
     * "{@docRoot}/../specs/security/standard-names.html#secretkeyfactory-algorithms">
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 41826
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
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @param provider the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   232
     * @return the new {@code SecretKeyFactory} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   234
     * @throws IllegalArgumentException if the {@code provider}
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   235
     *         is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   237
     * @throws NoSuchAlgorithmException if a {@code SecretKeyFactorySpi}
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   238
     *         implementation for the specified algorithm is not available
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   239
     *         from the specified {@code Provider} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   241
     * @throws NullPointerException if {@code algorithm} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public static final SecretKeyFactory getInstance(String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            Provider provider) throws NoSuchAlgorithmException {
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   247
        Objects.requireNonNull(algorithm, "null algorithm name");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        Instance instance = JceSecurity.getInstance("SecretKeyFactory",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                SecretKeyFactorySpi.class, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        return new SecretKeyFactory((SecretKeyFactorySpi)instance.impl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                instance.provider, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 25859
diff changeset
   255
     * Returns the provider of this {@code SecretKeyFactory} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 25859
diff changeset
   257
     * @return the provider of this {@code SecretKeyFactory} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    public final Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            // disable further failover after this call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            serviceIterator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            return provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /**
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 25859
diff changeset
   268
     * Returns the algorithm name of this {@code SecretKeyFactory} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * <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: 25859
diff changeset
   271
     * {@code getInstance} calls that created this
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 25859
diff changeset
   272
     * {@code SecretKeyFactory} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 25859
diff changeset
   274
     * @return the algorithm name of this {@code SecretKeyFactory}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public final String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        return this.algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Update the active spi of this class and return the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * implementation for failover. If no more implemenations are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * available, this method returns null. However, the active spi of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * this class is never set to null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    private SecretKeyFactorySpi nextSpi(SecretKeyFactorySpi oldSpi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            // somebody else did a failover concurrently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            // try that spi now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            if ((oldSpi != null) && (oldSpi != spi)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                return spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            while (serviceIterator.hasNext()) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8575
diff changeset
   298
                Service s = serviceIterator.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                if (JceSecurity.canUseProvider(s.getProvider()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    Object obj = s.newInstance(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    if (obj instanceof SecretKeyFactorySpi == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    SecretKeyFactorySpi spi = (SecretKeyFactorySpi)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    provider = s.getProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    this.spi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                    return spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                    // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            serviceIterator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    /**
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 25859
diff changeset
   321
     * Generates a {@code SecretKey} object from the provided key
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * specification (key material).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @param keySpec the specification (key material) of the secret key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @return the secret key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @exception InvalidKeySpecException if the given key specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * is inappropriate for this secret-key factory to produce a secret key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    public final SecretKey generateSecret(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            return spi.engineGenerateSecret(keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        Exception failure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        SecretKeyFactorySpi mySpi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                return mySpi.engineGenerateSecret(keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                if (failure == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                mySpi = nextSpi(mySpi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        } while (mySpi != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        if (failure instanceof InvalidKeySpecException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            throw (InvalidKeySpecException)failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                ("Could not generate secret key", failure);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * Returns a specification (key material) of the given key object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * in the requested format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @param key the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @param keySpec the requested format in which the key material shall be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @return the underlying key specification (key material) in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * requested format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @exception InvalidKeySpecException if the requested key specification is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * inappropriate for the given key (e.g., the algorithms associated with
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 25859
diff changeset
   368
     * {@code key} and {@code keySpec} do not match, or
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 25859
diff changeset
   369
     * {@code key} references a key on a cryptographic hardware device
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 25859
diff changeset
   370
     * whereas {@code keySpec} is the specification of a software-based
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * key), or the given key cannot be dealt with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * (e.g., the given key has an algorithm or format not supported by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * secret-key factory).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8575
diff changeset
   375
    public final KeySpec getKeySpec(SecretKey key, Class<?> keySpec)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            return spi.engineGetKeySpec(key, keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        Exception failure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        SecretKeyFactorySpi mySpi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                return mySpi.engineGetKeySpec(key, keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                if (failure == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                    failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                mySpi = nextSpi(mySpi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        } while (mySpi != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        if (failure instanceof InvalidKeySpecException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            throw (InvalidKeySpecException)failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                ("Could not get key spec", failure);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * Translates a key object, whose provider may be unknown or potentially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * untrusted, into a corresponding key object of this secret-key factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @param key the key whose provider is unknown or untrusted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @return the translated key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @exception InvalidKeyException if the given key cannot be processed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * by this secret-key factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    public final SecretKey translateKey(SecretKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            return spi.engineTranslateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        Exception failure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        SecretKeyFactorySpi mySpi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                return mySpi.engineTranslateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                if (failure == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                mySpi = nextSpi(mySpi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        } while (mySpi != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        if (failure instanceof InvalidKeyException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            throw (InvalidKeyException)failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                ("Could not translate key", failure);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
}