jdk/src/share/classes/java/security/KeyFactory.java
author never
Mon, 12 Jul 2010 22:27:18 -0700
changeset 5926 a36f90d986b6
parent 5506 202f599c92aa
child 8152 94e5966bdf22
permissions -rw-r--r--
6968385: malformed xml in sweeper logging Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1997, 2006, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * cryptographic keys of type <code>Key</code>) into <I>key specifications</I>
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <code>DSAPublicKeySpec</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <code>X509EncodedKeySpec</code>. A key factory can be used to translate
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * @see Key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * @see PublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * @see PrivateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * @see java.security.spec.KeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * @see java.security.spec.DSAPublicKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * @see java.security.spec.X509EncodedKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
public class KeyFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private static final Debug debug =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                        Debug.getInstance("jca", "KeyFactory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    // The algorithm associated with this key factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private final String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    // The provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private Provider provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    // The provider implementation (delegate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private volatile KeyFactorySpi spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    // lock for mutex during provider selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private final Object lock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    // remaining services to try in provider selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    // null once provider is selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private Iterator<Service> serviceIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Creates a KeyFactory object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @param keyFacSpi the delegate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @param provider the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @param algorithm the name of the algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * to associate with this <tt>KeyFactory</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    protected KeyFactory(KeyFactorySpi keyFacSpi, Provider provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                         String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        this.spi = keyFacSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        this.provider = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private KeyFactory(String algorithm) throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        List<Service> list = GetInstance.getServices("KeyFactory", algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        serviceIterator = list.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        // fetch and instantiate initial spi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (nextSpi(null) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                (algorithm + " KeyFactory not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Returns a KeyFactory object that converts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * public/private keys of the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * <p> This method traverses the list of registered security Providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * starting with the most preferred Provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * A new KeyFactory object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * KeyFactorySpi implementation from the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Provider that supports the specified algorithm is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @param algorithm the name of the requested key algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * See Appendix A in the <a href=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * "../../../technotes/guides/security/crypto/CryptoSpec.html#AppA">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Java Cryptography Architecture API Specification &amp; Reference </a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @return the new KeyFactory object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @exception NoSuchAlgorithmException if no Provider supports a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *          KeyFactorySpi implementation for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *          specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public static KeyFactory getInstance(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return new KeyFactory(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Returns a KeyFactory object that converts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * public/private keys of the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * <p> A new KeyFactory object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * KeyFactorySpi implementation from the specified provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * is returned.  The specified provider must be registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @param algorithm the name of the requested key algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * See Appendix A in the <a href=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * "../../../technotes/guides/security/crypto/CryptoSpec.html#AppA">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * Java Cryptography Architecture API Specification &amp; Reference </a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @param provider the name of the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @return the new KeyFactory object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @exception NoSuchAlgorithmException if a KeyFactorySpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *          implementation for the specified algorithm is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *          available from the specified provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @exception NoSuchProviderException if the specified provider is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *          registered in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @exception IllegalArgumentException if the provider name is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *          or empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public static KeyFactory getInstance(String algorithm, String provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            throws NoSuchAlgorithmException, NoSuchProviderException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        Instance instance = GetInstance.getInstance("KeyFactory",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            KeyFactorySpi.class, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        return new KeyFactory((KeyFactorySpi)instance.impl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            instance.provider, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * Returns a KeyFactory object that converts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * public/private keys of the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * <p> A new KeyFactory object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * KeyFactorySpi implementation from the specified Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * object is returned.  Note that the specified Provider object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * does not have to be registered in the provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @param algorithm the name of the requested key algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * See Appendix A in the <a href=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * "../../../technotes/guides/security/crypto/CryptoSpec.html#AppA">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Java Cryptography Architecture API Specification &amp; Reference </a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @param provider the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @return the new KeyFactory object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @exception NoSuchAlgorithmException if a KeyFactorySpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *          implementation for the specified algorithm is not available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *          from the specified Provider object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @exception IllegalArgumentException if the specified provider is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    public static KeyFactory getInstance(String algorithm, Provider provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        Instance instance = GetInstance.getInstance("KeyFactory",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            KeyFactorySpi.class, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        return new KeyFactory((KeyFactorySpi)instance.impl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            instance.provider, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * Returns the provider of this key factory object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @return the provider of this key factory object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    public final Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            // disable further failover after this call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            serviceIterator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            return provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Gets the name of the algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * associated with this <tt>KeyFactory</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @return the name of the algorithm associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * <tt>KeyFactory</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public final String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        return this.algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * Update the active KeyFactorySpi of this class and return the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * implementation for failover. If no more implemenations are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * available, this method returns null. However, the active spi of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * this class is never set to null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    private KeyFactorySpi nextSpi(KeyFactorySpi oldSpi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            // somebody else did a failover concurrently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            // try that spi now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            if ((oldSpi != null) && (oldSpi != spi)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                return spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            while (serviceIterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                Service s = serviceIterator.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                    Object obj = s.newInstance(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    if (obj instanceof KeyFactorySpi == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    KeyFactorySpi spi = (KeyFactorySpi)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    provider = s.getProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    this.spi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    return spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            serviceIterator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * Generates a public key object from the provided key specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * (key material).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @param keySpec the specification (key material) of the public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @return the public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * @exception InvalidKeySpecException if the given key specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * is inappropriate for this key factory to produce a public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    public final PublicKey generatePublic(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            return spi.engineGeneratePublic(keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        Exception failure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        KeyFactorySpi mySpi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                return mySpi.engineGeneratePublic(keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                if (failure == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                mySpi = nextSpi(mySpi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        } while (mySpi != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if (failure instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            throw (RuntimeException)failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        if (failure instanceof InvalidKeySpecException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            throw (InvalidKeySpecException)failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                ("Could not generate public key", failure);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * Generates a private key object from the provided key specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * (key material).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @param keySpec the specification (key material) of the private key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @return the private key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @exception InvalidKeySpecException if the given key specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * is inappropriate for this key factory to produce a private key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    public final PrivateKey generatePrivate(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            return spi.engineGeneratePrivate(keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        Exception failure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        KeyFactorySpi mySpi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                return mySpi.engineGeneratePrivate(keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                if (failure == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                mySpi = nextSpi(mySpi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        } while (mySpi != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        if (failure instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            throw (RuntimeException)failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        if (failure instanceof InvalidKeySpecException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            throw (InvalidKeySpecException)failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                ("Could not generate private key", failure);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * Returns a specification (key material) of the given key object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * <code>keySpec</code> identifies the specification class in which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * the key material should be returned. It could, for example, be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * <code>DSAPublicKeySpec.class</code>, to indicate that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * key material should be returned in an instance of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * <code>DSAPublicKeySpec</code> class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @param key the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * @param keySpec the specification class in which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * the key material should be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * @return the underlying key specification (key material) in an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * of the requested specification class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @exception InvalidKeySpecException if the requested key specification is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * inappropriate for the given key, or the given key cannot be processed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * (e.g., the given key has an unrecognized algorithm or format).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    public final <T extends KeySpec> T getKeySpec(Key key, Class<T> keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            return spi.engineGetKeySpec(key, keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        Exception failure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        KeyFactorySpi mySpi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                return mySpi.engineGetKeySpec(key, keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                if (failure == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                    failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                mySpi = nextSpi(mySpi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        } while (mySpi != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        if (failure instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            throw (RuntimeException)failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        if (failure instanceof InvalidKeySpecException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            throw (InvalidKeySpecException)failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                ("Could not get key spec", failure);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * Translates a key object, whose provider may be unknown or potentially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * untrusted, into a corresponding key object of this key factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @param key the key whose provider is unknown or untrusted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * @return the translated key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * @exception InvalidKeyException if the given key cannot be processed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * by this key factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    public final Key translateKey(Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            return spi.engineTranslateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        Exception failure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        KeyFactorySpi mySpi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                return mySpi.engineTranslateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                if (failure == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                    failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                mySpi = nextSpi(mySpi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        } while (mySpi != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        if (failure instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            throw (RuntimeException)failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        if (failure instanceof InvalidKeyException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            throw (InvalidKeyException)failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                ("Could not translate key", failure);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
}