jdk/src/java.base/share/classes/com/sun/net/ssl/KeyManagerFactory.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 14775 jdk/src/share/classes/com/sun/net/ssl/KeyManagerFactory.java@2ed01c760aea
child 30033 b9c86c17164a
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14775
2ed01c760aea 8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents: 5506
diff changeset
     2
 * Copyright (c) 2000, 2012, 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
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * NOTE:  this file was copied from javax.net.ssl.KeyManagerFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
package com.sun.net.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * This class acts as a factory for key managers based on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * source of key material. Each key manager manages a specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * type of key material for use by secure sockets. The key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * material is based on a KeyStore and/or provider specific sources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @deprecated As of JDK 1.4, this implementation-specific class was
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *      replaced by {@link javax.net.ssl.KeyManagerFactory}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
@Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class KeyManagerFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    // The provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private Provider provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // The provider implementation (delegate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private KeyManagerFactorySpi factorySpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // The name of the key management algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * <p>The default KeyManager can be changed by setting the value of the
14775
2ed01c760aea 8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents: 5506
diff changeset
    56
     * {@code sun.ssl.keymanager.type} security property to the desired name.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     *
14775
2ed01c760aea 8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents: 5506
diff changeset
    58
     * @return the default type as specified by the
2ed01c760aea 8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents: 5506
diff changeset
    59
     * {@code sun.ssl.keymanager.type} security property, or an
2ed01c760aea 8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents: 5506
diff changeset
    60
     * implementation-specific default if no such property exists.
2ed01c760aea 8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents: 5506
diff changeset
    61
     *
2ed01c760aea 8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents: 5506
diff changeset
    62
     * @see java.security.Security security properties
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public final static String getDefaultAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        String type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        type = AccessController.doPrivileged(new PrivilegedAction<String>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            public String run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                return Security.getProperty("sun.ssl.keymanager.type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        if (type == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            type = "SunX509";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        return type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Creates a KeyManagerFactory object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @param factorySpi the delegate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @param provider the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @param algorithm the algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    protected KeyManagerFactory(KeyManagerFactorySpi factorySpi,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                                Provider provider, String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        this.factorySpi = factorySpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        this.provider = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Returns the algorithm name of this <code>KeyManagerFactory</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * <p>This is the same name that was specified in one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * <code>getInstance</code> calls that created this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * <code>KeyManagerFactory</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @return the algorithm name of this <code>KeyManagerFactory</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public final String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        return this.algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Generates a <code>KeyManagerFactory</code> object that implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * specified key management algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * If the default provider package provides an implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * requested key management algorithm, an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * <code>KeyManagerFactory</code> containing that implementation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * returned.  If the algorithm is not available in the default provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * package, other provider packages are searched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param algorithm the standard name of the requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @return the new <code>KeyManagerFactory</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @exception NoSuchAlgorithmException if the specified algorithm is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * available in the default provider package or any of the other provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * packages that were searched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public static final KeyManagerFactory getInstance(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        throws NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            Object[] objs = SSLSecurity.getImpl(algorithm, "KeyManagerFactory",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                                                (String) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            return new KeyManagerFactory((KeyManagerFactorySpi)objs[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                    (Provider)objs[1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                                    algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        } catch (NoSuchProviderException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            throw new NoSuchAlgorithmException(algorithm + " not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Generates a <code>KeyManagerFactory</code> object for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * key management algorithm from the specified provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @param algorithm the standard name of the requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @param provider the name of the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @return the new <code>KeyManagerFactory</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @exception NoSuchAlgorithmException if the specified algorithm is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * available from the specified provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @exception NoSuchProviderException if the specified provider has not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * been configured.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public static final KeyManagerFactory getInstance(String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                                                 String provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        throws NoSuchAlgorithmException, NoSuchProviderException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (provider == null || provider.length() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            throw new IllegalArgumentException("missing provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        Object[] objs = SSLSecurity.getImpl(algorithm, "KeyManagerFactory",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                                            provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return new KeyManagerFactory((KeyManagerFactorySpi)objs[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                                        (Provider)objs[1], algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Generates a <code>KeyManagerFactory</code> object for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * key management algorithm from the specified provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @param algorithm the standard name of the requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @param provider an instance of the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @return the new <code>KeyManagerFactory</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @exception NoSuchAlgorithmException if the specified algorithm is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * available from the specified provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public static final KeyManagerFactory getInstance(String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                                                 Provider provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        throws NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (provider == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            throw new IllegalArgumentException("missing provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        Object[] objs = SSLSecurity.getImpl(algorithm, "KeyManagerFactory",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                                            provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        return new KeyManagerFactory((KeyManagerFactorySpi)objs[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                                        (Provider)objs[1], algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * Returns the provider of this <code>KeyManagerFactory</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @return the provider of this <code>KeyManagerFactory</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public final Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        return this.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Initializes this factory with a source of key material. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * provider may also include a provider-specific source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * of key material.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @param ks the key store or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @param password the password for recovering keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    public void init(KeyStore ks, char[] password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        throws KeyStoreException, NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            UnrecoverableKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        factorySpi.engineInit(ks, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * Returns one key manager for each type of key material.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @return the key managers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    public KeyManager[] getKeyManagers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return factorySpi.engineGetKeyManagers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
}