src/java.base/share/classes/sun/security/ssl/KeyManagerFactoryImpl.java
author wetmore
Fri, 11 May 2018 15:53:12 -0700
branchJDK-8145252-TLS13-branch
changeset 56542 56aaa6cb3693
parent 47216 71c04702a3d5
child 56646 e57205a6e4ee
permissions -rw-r--r--
Initial TLSv1.3 Implementation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
     2
 * Copyright (c) 1999, 2018, 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 sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.KeyStore.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
abstract class KeyManagerFactoryImpl extends KeyManagerFactorySpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    X509ExtendedKeyManager keyManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    boolean isInitialized;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    KeyManagerFactoryImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * Returns one key manager for each type of key material.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
    48
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    protected KeyManager[] engineGetKeyManagers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        if (!isInitialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            throw new IllegalStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                        "KeyManagerFactoryImpl is not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        return new KeyManager[] { keyManager };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // Factory for the SunX509 keymanager
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public static final class SunX509 extends KeyManagerFactoryImpl {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
    60
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        protected void engineInit(KeyStore ks, char[] password) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                KeyStoreException, NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                UnrecoverableKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            if ((ks != null) && SunJSSE.isFIPS()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                if (ks.getProvider() != SunJSSE.cryptoProvider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                    throw new KeyStoreException("FIPS mode: KeyStore must be "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                        + "from provider " + SunJSSE.cryptoProvider.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            keyManager = new SunX509KeyManagerImpl(ks, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            isInitialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
    74
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        protected void engineInit(ManagerFactoryParameters spec) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            throw new InvalidAlgorithmParameterException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                "SunX509KeyManager does not use ManagerFactoryParameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    // Factory for the X509 keymanager
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public static final class X509 extends KeyManagerFactoryImpl {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
    86
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        protected void engineInit(KeyStore ks, char[] password) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                KeyStoreException, NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                UnrecoverableKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            if (ks == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                keyManager = new X509KeyManagerImpl(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                        Collections.<Builder>emptyList());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            } else {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    94
                if (SunJSSE.isFIPS() &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    95
                        (ks.getProvider() != SunJSSE.cryptoProvider)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    96
                    throw new KeyStoreException(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    97
                        "FIPS mode: KeyStore must be " +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    98
                        "from provider " + SunJSSE.cryptoProvider.getName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    Builder builder = Builder.newInstance(ks,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                        new PasswordProtection(password));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    keyManager = new X509KeyManagerImpl(builder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    throw new KeyStoreException("initialization failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            isInitialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
   111
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        protected void engineInit(ManagerFactoryParameters params) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            if (params instanceof KeyStoreBuilderParameters == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                throw new InvalidAlgorithmParameterException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                "Parameters must be instance of KeyStoreBuilderParameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            if (SunJSSE.isFIPS()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                // XXX should be fixed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    ("FIPS mode: KeyStoreBuilderParameters not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            List<Builder> builders =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                ((KeyStoreBuilderParameters)params).getParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            keyManager = new X509KeyManagerImpl(builders);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            isInitialized = true;
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
}