jdk/src/java.base/share/classes/java/security/KeyFactory.java
author coleenp
Wed, 30 Aug 2017 19:18:22 -0400
changeset 47098 e704f55561c3
parent 45118 e4258d800b54
permissions -rw-r--r--
8164207: Checking missing load-acquire in relation to _pd_set in dictionary.cpp Summary: Use load_acquire for accessing DictionaryEntry::_pd_set since it's accessed outside the SystemDictionary_lock Reviewed-by: zgu, twisti, dholmes, adinn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 41826
diff changeset
     2
 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.Provider.Service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.spec.KeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.spec.InvalidKeySpecException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.util.Debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.jca.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.jca.GetInstance.Instance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Key factories are used to convert <I>keys</I> (opaque
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
    40
 * cryptographic keys of type {@code Key}) into <I>key specifications</I>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * (transparent representations of the underlying key material), and vice
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * versa.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <P> Key factories are bi-directional. That is, they allow you to build an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * opaque key object from a given key specification (key material), or to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * retrieve the underlying key material of a key object in a suitable format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <P> Multiple compatible key specifications may exist for the same key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * For example, a DSA public key may be specified using
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
    50
 * {@code DSAPublicKeySpec} or
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
    51
 * {@code X509EncodedKeySpec}. A key factory can be used to translate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * between compatible key specifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <P> The following is an example of how to use a key factory in order to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * instantiate a DSA public key from its encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Assume Alice has received a digital signature from Bob.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * Bob also sent her his public key (in encoded format) to verify
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * his signature. Alice then performs the following actions:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * X509EncodedKeySpec bobPubKeySpec = new X509EncodedKeySpec(bobEncodedPubKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * KeyFactory keyFactory = KeyFactory.getInstance("DSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * PublicKey bobPubKey = keyFactory.generatePublic(bobPubKeySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * Signature sig = Signature.getInstance("DSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * sig.initVerify(bobPubKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * sig.update(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * sig.verify(signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    70
 * <p> Every implementation of the Java platform is required to support the
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
    71
 * following standard {@code KeyFactory} algorithms:
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    72
 * <ul>
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
    73
 * <li>{@code DiffieHellman}</li>
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
    74
 * <li>{@code DSA}</li>
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
    75
 * <li>{@code RSA}</li>
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    76
 * </ul>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    77
 * These algorithms are described in the <a href=
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 41826
diff changeset
    78
 * "{@docRoot}/../specs/security/standard-names.html#keyfactory-algorithms">
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    79
 * KeyFactory section</a> of the
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 41826
diff changeset
    80
 * Java Security Standard Algorithm Names Specification.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    81
 * 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
    82
 * other algorithms are supported.
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    83
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * @see Key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * @see PublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * @see PrivateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * @see java.security.spec.KeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * @see java.security.spec.DSAPublicKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * @see java.security.spec.X509EncodedKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
public class KeyFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private static final Debug debug =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                        Debug.getInstance("jca", "KeyFactory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    // The algorithm associated with this key factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private final String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    // The provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private Provider provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    // The provider implementation (delegate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private volatile KeyFactorySpi spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    // lock for mutex during provider selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private final Object lock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    // remaining services to try in provider selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    // null once provider is selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private Iterator<Service> serviceIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Creates a KeyFactory object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @param keyFacSpi the delegate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param provider the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @param algorithm the name of the algorithm
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
   123
     * to associate with this {@code KeyFactory}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    protected KeyFactory(KeyFactorySpi keyFacSpi, Provider provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                         String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        this.spi = keyFacSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        this.provider = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private KeyFactory(String algorithm) throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        List<Service> list = GetInstance.getServices("KeyFactory", algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        serviceIterator = list.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        // fetch and instantiate initial spi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (nextSpi(null) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                (algorithm + " KeyFactory not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Returns a KeyFactory object that converts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * public/private keys of the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * <p> This method traverses the list of registered security Providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * starting with the most preferred Provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * A new KeyFactory object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * KeyFactorySpi implementation from the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * Provider that supports the specified algorithm is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 25859
diff changeset
   156
     * @implNote
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 25859
diff changeset
   157
     * The JDK Reference Implementation additionally uses the
37348
9ccec3170d5e 8152205: jdk.security.provider.preferred is ambiguously documented
ascarpino
parents: 33241
diff changeset
   158
     * {@code jdk.security.provider.preferred}
9ccec3170d5e 8152205: jdk.security.provider.preferred is ambiguously documented
ascarpino
parents: 33241
diff changeset
   159
     * {@link Security#getProperty(String) Security} property to determine
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 25859
diff changeset
   160
     * the preferred provider order for the specified algorithm. This
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 25859
diff changeset
   161
     * may be different than the order of providers returned by
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 25859
diff changeset
   162
     * {@link Security#getProviders() Security.getProviders()}.
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 25859
diff changeset
   163
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @param algorithm the name of the requested key algorithm.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   165
     * See the KeyFactory section in the <a href=
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 41826
diff changeset
   166
     * "{@docRoot}/../specs/security/standard-names.html#keyfactory-algorithms">
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 41826
diff changeset
   167
     * Java Security Standard Algorithm Names Specification</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   170
     * @return the new {@code KeyFactory} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   172
     * @throws NoSuchAlgorithmException if no {@code Provider} supports a
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   173
     *         {@code KeyFactorySpi} implementation for the
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   174
     *         specified algorithm
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   175
     *
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   176
     * @throws NullPointerException if {@code algorithm} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public static KeyFactory getInstance(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            throws NoSuchAlgorithmException {
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   182
        Objects.requireNonNull(algorithm, "null algorithm name");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        return new KeyFactory(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Returns a KeyFactory object that converts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * public/private keys of the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * <p> A new KeyFactory object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * KeyFactorySpi implementation from the specified provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * is returned.  The specified provider must be registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @param algorithm the name of the requested key algorithm.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   199
     * See the KeyFactory section in the <a href=
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 41826
diff changeset
   200
     * "{@docRoot}/../specs/security/standard-names.html#keyfactory-algorithms">
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 41826
diff changeset
   201
     * Java Security Standard Algorithm Names Specification</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @param provider the name of the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   206
     * @return the new {@code KeyFactory} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   208
     * @throws IllegalArgumentException if the provider name is {@code null}
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   209
     *         or empty
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   211
     * @throws NoSuchAlgorithmException if a {@code KeyFactorySpi}
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   212
     *         implementation for the specified algorithm is not
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   213
     *         available from the specified provider
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   215
     * @throws NoSuchProviderException if the specified provider is not
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   216
     *         registered in the security provider list
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   217
     *
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   218
     * @throws NullPointerException if {@code algorithm} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public static KeyFactory getInstance(String algorithm, String provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            throws NoSuchAlgorithmException, NoSuchProviderException {
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   224
        Objects.requireNonNull(algorithm, "null algorithm name");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        Instance instance = GetInstance.getInstance("KeyFactory",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            KeyFactorySpi.class, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        return new KeyFactory((KeyFactorySpi)instance.impl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            instance.provider, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * Returns a KeyFactory object that converts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * public/private keys of the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * <p> A new KeyFactory object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * KeyFactorySpi implementation from the specified Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * object is returned.  Note that the specified Provider object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * does not have to be registered in the provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @param algorithm the name of the requested key algorithm.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   241
     * See the KeyFactory section in the <a href=
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 41826
diff changeset
   242
     * "{@docRoot}/../specs/security/standard-names.html#keyfactory-algorithms">
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 41826
diff changeset
   243
     * Java Security Standard Algorithm Names Specification</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @param provider the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   248
     * @return the new {@code KeyFactory} object
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   249
     *
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   250
     * @throws IllegalArgumentException if the specified provider is
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   251
     *         {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   253
     * @throws NoSuchAlgorithmException if a {@code KeyFactorySpi}
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   254
     *         implementation for the specified algorithm is not available
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   255
     *         from the specified {@code Provider} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   257
     * @throws NullPointerException if {@code algorithm} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    public static KeyFactory getInstance(String algorithm, Provider provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            throws NoSuchAlgorithmException {
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   265
        Objects.requireNonNull(algorithm, "null algorithm name");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        Instance instance = GetInstance.getInstance("KeyFactory",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            KeyFactorySpi.class, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        return new KeyFactory((KeyFactorySpi)instance.impl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            instance.provider, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * Returns the provider of this key factory object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @return the provider of this key factory object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public final Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            // disable further failover after this call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            serviceIterator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            return provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * Gets the name of the algorithm
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
   287
     * associated with this {@code KeyFactory}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @return the name of the algorithm associated with this
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
   290
     * {@code KeyFactory}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    public final String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        return this.algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * Update the active KeyFactorySpi of this class and return the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * implementation for failover. If no more implemenations are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * available, this method returns null. However, the active spi of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * this class is never set to null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    private KeyFactorySpi nextSpi(KeyFactorySpi oldSpi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            // somebody else did a failover concurrently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            // try that spi now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            if ((oldSpi != null) && (oldSpi != spi)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                return spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            while (serviceIterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                Service s = serviceIterator.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    Object obj = s.newInstance(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    if (obj instanceof KeyFactorySpi == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                    KeyFactorySpi spi = (KeyFactorySpi)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                    provider = s.getProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    this.spi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    return spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            serviceIterator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * Generates a public key object from the provided key specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * (key material).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @param keySpec the specification (key material) of the public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @return the public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * @exception InvalidKeySpecException if the given key specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * is inappropriate for this key factory to produce a public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    public final PublicKey generatePublic(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            return spi.engineGeneratePublic(keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        Exception failure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        KeyFactorySpi mySpi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                return mySpi.engineGeneratePublic(keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                if (failure == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                mySpi = nextSpi(mySpi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        } while (mySpi != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        if (failure instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            throw (RuntimeException)failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        if (failure instanceof InvalidKeySpecException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            throw (InvalidKeySpecException)failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                ("Could not generate public key", failure);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * Generates a private key object from the provided key specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * (key material).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @param keySpec the specification (key material) of the private key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @return the private key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * @exception InvalidKeySpecException if the given key specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * is inappropriate for this key factory to produce a private key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    public final PrivateKey generatePrivate(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            return spi.engineGeneratePrivate(keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        Exception failure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        KeyFactorySpi mySpi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                return mySpi.engineGeneratePrivate(keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                if (failure == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                    failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                mySpi = nextSpi(mySpi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        } while (mySpi != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        if (failure instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            throw (RuntimeException)failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        if (failure instanceof InvalidKeySpecException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            throw (InvalidKeySpecException)failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                ("Could not generate private key", failure);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * Returns a specification (key material) of the given key object.
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
   410
     * {@code keySpec} identifies the specification class in which
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * the key material should be returned. It could, for example, be
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
   412
     * {@code DSAPublicKeySpec.class}, to indicate that the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * key material should be returned in an instance of the
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
   414
     * {@code DSAPublicKeySpec} class.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *
19828
b4f91bc595fe 8024432: Fix doclint issues in java.security
juh
parents: 18579
diff changeset
   416
     * @param <T> the type of the key specification to be returned
b4f91bc595fe 8024432: Fix doclint issues in java.security
juh
parents: 18579
diff changeset
   417
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * @param key the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @param keySpec the specification class in which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * the key material should be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * @return the underlying key specification (key material) in an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * of the requested specification class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @exception InvalidKeySpecException if the requested key specification is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * inappropriate for the given key, or the given key cannot be processed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * (e.g., the given key has an unrecognized algorithm or format).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    public final <T extends KeySpec> T getKeySpec(Key key, Class<T> keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            return spi.engineGetKeySpec(key, keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        Exception failure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        KeyFactorySpi mySpi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                return mySpi.engineGetKeySpec(key, keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                if (failure == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                mySpi = nextSpi(mySpi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        } while (mySpi != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        if (failure instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            throw (RuntimeException)failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        if (failure instanceof InvalidKeySpecException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            throw (InvalidKeySpecException)failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                ("Could not get key spec", failure);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * Translates a key object, whose provider may be unknown or potentially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * untrusted, into a corresponding key object of this key factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * @param key the key whose provider is unknown or untrusted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * @return the translated key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * @exception InvalidKeyException if the given key cannot be processed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * by this key factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    public final Key translateKey(Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            return spi.engineTranslateKey(key);
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
        KeyFactorySpi 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
                return mySpi.engineTranslateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                if (failure == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                    failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                mySpi = nextSpi(mySpi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        } while (mySpi != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        if (failure instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            throw (RuntimeException)failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        if (failure instanceof InvalidKeyException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            throw (InvalidKeyException)failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                ("Could not translate key", failure);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
}