jdk/src/share/classes/javax/crypto/SecretKeyFactory.java
author jjg
Mon, 15 Aug 2011 11:48:20 -0700
changeset 10336 0bb1999251f8
parent 8575 fda3a3c1320f
permissions -rw-r--r--
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror Reviewed-by: xuelei, mullan Contributed-by: alexandre.boulgakov@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
8575
fda3a3c1320f 7022467: SecretKeyFactory doesn't support algorithm "AES" on Windows and Linux
mullan
parents: 8152
diff changeset
     2
 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.crypto;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.Provider.Service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.jca.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.jca.GetInstance.Instance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This class represents a factory for secret keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <P> Key factories are used to convert <I>keys</I> (opaque
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * cryptographic keys of type <code>Key</code>) into <I>key specifications</I>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * (transparent representations of the underlying key material), and vice
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * versa.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Secret key factories operate only on secret (symmetric) keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <P> Key factories are bi-directional, i.e., they allow to build an opaque
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * key object from a given key specification (key material), or to retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * the underlying key material of a key object in a suitable format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <P> Application developers should refer to their provider's documentation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * to find out which key specifications are supported by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * {@link #generateSecret(java.security.spec.KeySpec) generateSecret} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * {@link #getKeySpec(javax.crypto.SecretKey, java.lang.Class) getKeySpec}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * For example, the DES secret-key factory supplied by the "SunJCE" provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * supports <code>DESKeySpec</code> as a transparent representation of DES
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * keys, and that provider's secret-key factory for Triple DES keys supports
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <code>DESedeKeySpec</code> as a transparent representation of Triple DES
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    61
 * <p> Every implementation of the Java platform is required to support the
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    62
 * following standard <code>SecretKeyFactory</code> algorithms:
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    63
 * <ul>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    64
 * <li><tt>DES</tt></li>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    65
 * <li><tt>DESede</tt></li>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    66
 * </ul>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    67
 * These algorithms are described in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    68
 * "{@docRoot}/../technotes/guides/security/StandardNames.html#SecretKeyFactory">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    69
 * SecretKeyFactory section</a> of the
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    70
 * Java Cryptography Architecture Standard Algorithm Name Documentation.
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    71
 * 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
    72
 * other algorithms are supported.
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    73
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * @see SecretKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * @see javax.crypto.spec.DESKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * @see javax.crypto.spec.DESedeKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * @see javax.crypto.spec.PBEKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * @since 1.4
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 SecretKeyFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    // The provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private Provider provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    // The algorithm associated with this 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 implementation (delegate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private volatile SecretKeyFactorySpi spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    // lock for mutex during provider selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private final Object lock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    // remaining services to try in provider selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    // null once provider is selected
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8575
diff changeset
    99
    private Iterator<Service> serviceIterator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Creates a SecretKeyFactory object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @param keyFacSpi the delegate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @param provider the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @param algorithm the secret-key algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    protected SecretKeyFactory(SecretKeyFactorySpi keyFacSpi,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                               Provider provider, String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        this.spi = keyFacSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        this.provider = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private SecretKeyFactory(String algorithm) throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        this.algorithm = algorithm;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8575
diff changeset
   117
        List<Service> list =
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8575
diff changeset
   118
                GetInstance.getServices("SecretKeyFactory", algorithm);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        serviceIterator = list.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        // fetch and instantiate initial spi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (nextSpi(null) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                (algorithm + " SecretKeyFactory not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Returns a <code>SecretKeyFactory</code> object that converts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * secret keys of the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * <p> This method traverses the list of registered security Providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * starting with the most preferred Provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * A new SecretKeyFactory object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * SecretKeyFactorySpi implementation from the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Provider that supports the specified algorithm is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @param algorithm the standard name of the requested secret-key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * algorithm.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   142
     * See the SecretKeyFactory section in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   143
     * "{@docRoot}/../technotes/guides/security/StandardNames.html#SecretKeyFactory">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   144
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @return the new <code>SecretKeyFactory</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @exception NullPointerException if the specified algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *          is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @exception NoSuchAlgorithmException if no Provider supports a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *          SecretKeyFactorySpi implementation for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *          specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public static final SecretKeyFactory getInstance(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return new SecretKeyFactory(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Returns a <code>SecretKeyFactory</code> object that converts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * secret keys of the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * <p> A new SecretKeyFactory object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * SecretKeyFactorySpi implementation from the specified provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * is returned.  The specified provider must be registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @param algorithm the standard name of the requested secret-key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * algorithm.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   177
     * See the SecretKeyFactory section in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   178
     * "{@docRoot}/../technotes/guides/security/StandardNames.html#SecretKeyFactory">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   179
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @param provider the name of the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @return the new <code>SecretKeyFactory</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @exception NoSuchAlgorithmException if a SecretKeyFactorySpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *          implementation for the specified algorithm is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *          available from the specified provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @exception NullPointerException if the specified algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *          is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @throws NoSuchProviderException if the specified provider is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *          registered in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @exception IllegalArgumentException if the <code>provider</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *          is null or empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    public static final SecretKeyFactory getInstance(String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            String provider) throws NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            NoSuchProviderException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        Instance instance = JceSecurity.getInstance("SecretKeyFactory",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                SecretKeyFactorySpi.class, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return new SecretKeyFactory((SecretKeyFactorySpi)instance.impl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                instance.provider, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Returns a <code>SecretKeyFactory</code> object that converts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * secret keys of the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * <p> A new SecretKeyFactory object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * SecretKeyFactorySpi implementation from the specified Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * object is returned.  Note that the specified Provider object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * does not have to be registered in the provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @param algorithm the standard name of the requested secret-key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * algorithm.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   221
     * See the SecretKeyFactory section in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   222
     * "{@docRoot}/../technotes/guides/security/StandardNames.html#SecretKeyFactory">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   223
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @param provider the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @return the new <code>SecretKeyFactory</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @exception NullPointerException if the specified algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @exception NoSuchAlgorithmException if a SecretKeyFactorySpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *          implementation for the specified algorithm is not available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *          from the specified Provider object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @exception IllegalArgumentException if the <code>provider</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *          is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    public static final SecretKeyFactory getInstance(String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            Provider provider) throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        Instance instance = JceSecurity.getInstance("SecretKeyFactory",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                SecretKeyFactorySpi.class, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        return new SecretKeyFactory((SecretKeyFactorySpi)instance.impl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                instance.provider, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Returns the provider of this <code>SecretKeyFactory</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @return the provider of this <code>SecretKeyFactory</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    public final Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            // disable further failover after this call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            serviceIterator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            return provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * Returns the algorithm name of this <code>SecretKeyFactory</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * <p>This is the same name that was specified in one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * <code>getInstance</code> calls that created this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * <code>SecretKeyFactory</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @return the algorithm name of this <code>SecretKeyFactory</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    public final String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        return this.algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * Update the active spi of this class and return the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * implementation for failover. If no more implemenations are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * available, this method returns null. However, the active spi of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * this class is never set to null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    private SecretKeyFactorySpi nextSpi(SecretKeyFactorySpi oldSpi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            // somebody else did a failover concurrently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            // try that spi now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            if ((oldSpi != null) && (oldSpi != spi)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                return spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            while (serviceIterator.hasNext()) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8575
diff changeset
   294
                Service s = serviceIterator.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                if (JceSecurity.canUseProvider(s.getProvider()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    Object obj = s.newInstance(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    if (obj instanceof SecretKeyFactorySpi == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    SecretKeyFactorySpi spi = (SecretKeyFactorySpi)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    provider = s.getProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    this.spi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    return spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            serviceIterator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * Generates a <code>SecretKey</code> object from the provided key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * specification (key material).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @param keySpec the specification (key material) of the secret key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @return the secret key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @exception InvalidKeySpecException if the given key specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * is inappropriate for this secret-key factory to produce a secret key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    public final SecretKey generateSecret(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            return spi.engineGenerateSecret(keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        Exception failure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        SecretKeyFactorySpi mySpi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                return mySpi.engineGenerateSecret(keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                if (failure == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                mySpi = nextSpi(mySpi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        } while (mySpi != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        if (failure instanceof InvalidKeySpecException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            throw (InvalidKeySpecException)failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                ("Could not generate secret key", failure);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * Returns a specification (key material) of the given key object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * in the requested format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @param key the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @param keySpec the requested format in which the key material shall be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @return the underlying key specification (key material) in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * requested format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * @exception InvalidKeySpecException if the requested key specification is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * inappropriate for the given key (e.g., the algorithms associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * <code>key</code> and <code>keySpec</code> do not match, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * <code>key</code> references a key on a cryptographic hardware device
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * whereas <code>keySpec</code> is the specification of a software-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * key), or the given key cannot be dealt with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * (e.g., the given key has an algorithm or format not supported by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * secret-key factory).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8575
diff changeset
   371
    public final KeySpec getKeySpec(SecretKey key, Class<?> keySpec)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            return spi.engineGetKeySpec(key, keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        Exception failure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        SecretKeyFactorySpi mySpi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                return mySpi.engineGetKeySpec(key, keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                if (failure == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                    failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                mySpi = nextSpi(mySpi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        } while (mySpi != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        if (failure instanceof InvalidKeySpecException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            throw (InvalidKeySpecException)failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                ("Could not get key spec", failure);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * Translates a key object, whose provider may be unknown or potentially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * untrusted, into a corresponding key object of this secret-key factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * @param key the key whose provider is unknown or untrusted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * @return the translated key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @exception InvalidKeyException if the given key cannot be processed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * by this secret-key factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    public final SecretKey translateKey(SecretKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        if (serviceIterator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            return spi.engineTranslateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        Exception failure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        SecretKeyFactorySpi mySpi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                return mySpi.engineTranslateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                if (failure == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                mySpi = nextSpi(mySpi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        } while (mySpi != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        if (failure instanceof InvalidKeyException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            throw (InvalidKeyException)failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                ("Could not translate key", failure);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
}