src/java.base/share/classes/sun/security/ssl/SunX509KeyManagerImpl.java
author wetmore
Fri, 11 May 2018 15:53:12 -0700
branchJDK-8145252-TLS13-branch
changeset 56542 56aaa6cb3693
parent 47216 71c04702a3d5
child 54443 dfba4e321ab3
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: 715
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: 715
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: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
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
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    28
import java.net.Socket;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    29
import java.security.Key;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    30
import java.security.KeyStore;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    31
import java.security.KeyStoreException;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    32
import java.security.NoSuchAlgorithmException;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    33
import java.security.Principal;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    34
import java.security.PrivateKey;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    35
import java.security.UnrecoverableKeyException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.cert.Certificate;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    37
import java.security.cert.X509Certificate;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    38
import java.util.ArrayList;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    39
import java.util.Collections;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    40
import java.util.Enumeration;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    41
import java.util.HashMap;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    42
import java.util.HashSet;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    43
import java.util.List;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    44
import java.util.Locale;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    45
import java.util.Map;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    46
import java.util.Set;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    47
import javax.net.ssl.SSLEngine;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    48
import javax.net.ssl.X509ExtendedKeyManager;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.security.auth.x500.X500Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
/**
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 14664
diff changeset
    53
 * An implementation of X509KeyManager backed by a KeyStore.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * The backing KeyStore is inspected when this object is constructed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * All key entries containing a PrivateKey and a non-empty chain of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * X509Certificate are then copied into an internal store. This means
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * that subsequent modifications of the KeyStore have no effect on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * X509KeyManagerImpl object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * Note that this class assumes that all keys are protected by the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * The JSSE handshake code currently calls into this class via
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * chooseClientAlias() and chooseServerAlias() to find the certificates to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * use. As implemented here, both always return the first alias returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * getClientAliases() and getServerAliases(). In turn, these methods are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * implemented by calling getAliases(), which performs the actual lookup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * Note that this class currently implements no checking of the local
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * certificates. In particular, it is *not* guaranteed that:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *  . the certificates are within their validity period and not revoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *  . the signatures verify
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *  . they form a PKIX compliant chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *  . the certificate extensions allow the certificate to be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *    the desired purpose.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * Chains that fail any of these criteria will probably be rejected by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * the remote peer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
final class SunX509KeyManagerImpl extends X509ExtendedKeyManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private static final String[] STRING0 = new String[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * The credentials from the KeyStore as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Map: String(alias) -> X509Credentials(credentials)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private Map<String,X509Credentials> credentialsMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Cached server aliases for the case issuers == null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * (in the current JSSE implementation, issuers are always null for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * server certs). See chooseServerAlias() for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * Map: String(keyType) -> String[](alias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
10334
26b816b3f236 7063647: To use synchronized map in key manager
xuelei
parents: 9035
diff changeset
    99
    private final Map<String,String[]> serverAliasCache;
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
     * Basic container for credentials implemented as an inner class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private static class X509Credentials {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        PrivateKey privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        X509Certificate[] certificates;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        private Set<X500Principal> issuerX500Principals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        X509Credentials(PrivateKey privateKey, X509Certificate[] certificates) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            // assert privateKey and certificates != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            this.privateKey = privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            this.certificates = certificates;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        synchronized Set<X500Principal> getIssuerX500Principals() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            // lazy initialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            if (issuerX500Principals == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                issuerX500Principals = new HashSet<X500Principal>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                for (int i = 0; i < certificates.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    issuerX500Principals.add(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                                certificates[i].getIssuerX500Principal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            return issuerX500Principals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
10334
26b816b3f236 7063647: To use synchronized map in key manager
xuelei
parents: 9035
diff changeset
   128
    SunX509KeyManagerImpl(KeyStore ks, char[] password)
26b816b3f236 7063647: To use synchronized map in key manager
xuelei
parents: 9035
diff changeset
   129
            throws KeyStoreException,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            NoSuchAlgorithmException, UnrecoverableKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        credentialsMap = new HashMap<String,X509Credentials>();
10334
26b816b3f236 7063647: To use synchronized map in key manager
xuelei
parents: 9035
diff changeset
   133
        serverAliasCache = Collections.synchronizedMap(
26b816b3f236 7063647: To use synchronized map in key manager
xuelei
parents: 9035
diff changeset
   134
                            new HashMap<String,String[]>());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (ks == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        for (Enumeration<String> aliases = ks.aliases();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                                        aliases.hasMoreElements(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            String alias = aliases.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            if (!ks.isKeyEntry(alias)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            Key key = ks.getKey(alias, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            if (key instanceof PrivateKey == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            Certificate[] certs = ks.getCertificateChain(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            if ((certs == null) || (certs.length == 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    !(certs[0] instanceof X509Certificate)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            if (!(certs instanceof X509Certificate[])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                Certificate[] tmp = new X509Certificate[certs.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                System.arraycopy(certs, 0, tmp, 0, certs.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                certs = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            X509Credentials cred = new X509Credentials((PrivateKey)key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                (X509Certificate[])certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            credentialsMap.put(alias, cred);
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   163
            if (SSLLogger.isOn && SSLLogger.isOn("keymanager")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   164
                SSLLogger.fine("found key for : " + alias, (Object[])certs);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Returns the certificate chain associated with the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @return the certificate chain (ordered with the user's certificate first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * and the root certificate authority last)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10334
diff changeset
   175
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public X509Certificate[] getCertificateChain(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        X509Credentials cred = credentialsMap.get(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (cred == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        } else {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   184
            return cred.certificates.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Returns the key associated with the given alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10334
diff changeset
   191
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public PrivateKey getPrivateKey(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        X509Credentials cred = credentialsMap.get(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (cred == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            return cred.privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
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
     * Choose an alias to authenticate the client side of a secure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * socket given the public key type and the list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * certificate issuer authorities recognized by the peer (if any).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10334
diff changeset
   209
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public String chooseClientAlias(String[] keyTypes, Principal[] issuers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            Socket socket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
         * We currently don't do anything with socket, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
         * someday we might.  It might be a useful hint for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
         * selecting one of the aliases we get back from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
         * getClientAliases().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        if (keyTypes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        for (int i = 0; i < keyTypes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            String[] aliases = getClientAliases(keyTypes[i], issuers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            if ((aliases != null) && (aliases.length > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                return aliases[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * Choose an alias to authenticate the client side of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * <code>SSLEngine</code> connection given the public key type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * and the list of certificate issuer authorities recognized by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * the peer (if any).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10334
diff changeset
   240
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public String chooseEngineClientAlias(String[] keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            Principal[] issuers, SSLEngine engine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
         * If we ever start using socket as a selection criteria,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
         * we'll need to adjust this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        return chooseClientAlias(keyType, issuers, null);
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
     * Choose an alias to authenticate the server side of a secure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * socket given the public key type and the list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * certificate issuer authorities recognized by the peer (if any).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10334
diff changeset
   255
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    public String chooseServerAlias(String keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            Principal[] issuers, Socket socket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
         * We currently don't do anything with socket, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
         * someday we might.  It might be a useful hint for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
         * selecting one of the aliases we get back from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
         * getServerAliases().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        if (keyType == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        String[] aliases;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        if (issuers == null || issuers.length == 0) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   271
            aliases = serverAliasCache.get(keyType);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            if (aliases == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                aliases = getServerAliases(keyType, issuers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                // Cache the result (positive and negative lookups)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                if (aliases == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    aliases = STRING0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                serverAliasCache.put(keyType, aliases);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            aliases = getServerAliases(keyType, issuers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        if ((aliases != null) && (aliases.length > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            return aliases[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * Choose an alias to authenticate the server side of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * <code>SSLEngine</code> connection given the public key type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * and the list of certificate issuer authorities recognized by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * the peer (if any).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10334
diff changeset
   297
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    public String chooseEngineServerAlias(String keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            Principal[] issuers, SSLEngine engine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
         * If we ever start using socket as a selection criteria,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
         * we'll need to adjust this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        return chooseServerAlias(keyType, issuers, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * Get the matching aliases for authenticating the client side of a secure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * socket given the public key type and the list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * certificate issuer authorities recognized by the peer (if any).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10334
diff changeset
   312
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    public String[] getClientAliases(String keyType, Principal[] issuers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        return getAliases(keyType, issuers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * Get the matching aliases for authenticating the server side of a secure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * socket given the public key type and the list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * certificate issuer authorities recognized by the peer (if any).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10334
diff changeset
   322
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    public String[] getServerAliases(String keyType, Principal[] issuers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        return getAliases(keyType, issuers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * Get the matching aliases for authenticating the either side of a secure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * socket given the public key type and the list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * certificate issuer authorities recognized by the peer (if any).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * Issuers comes to us in the form of X500Principal[].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    private String[] getAliases(String keyType, Principal[] issuers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        if (keyType == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        if (issuers == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            issuers = new X500Principal[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        if (issuers instanceof X500Principal[] == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            // normally, this will never happen but try to recover if it does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            issuers = convertPrincipals(issuers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        String sigType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        if (keyType.contains("_")) {
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 23010
diff changeset
   347
            int k = keyType.indexOf('_');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            sigType = keyType.substring(k + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            keyType = keyType.substring(0, k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            sigType = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        X500Principal[] x500Issuers = (X500Principal[])issuers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        // the algorithm below does not produce duplicates, so avoid Set
7990
57019dc81b66 7012003: diamond conversion for ssl
smarks
parents: 5506
diff changeset
   356
        List<String> aliases = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        for (Map.Entry<String,X509Credentials> entry :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                                                credentialsMap.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            String alias = entry.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            X509Credentials credentials = entry.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            X509Certificate[] certs = credentials.certificates;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            if (!keyType.equals(certs[0].getPublicKey().getAlgorithm())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            if (sigType != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                if (certs.length > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                    // if possible, check the public key in the issuer cert
10334
26b816b3f236 7063647: To use synchronized map in key manager
xuelei
parents: 9035
diff changeset
   371
                    if (!sigType.equals(
26b816b3f236 7063647: To use synchronized map in key manager
xuelei
parents: 9035
diff changeset
   372
                            certs[1].getPublicKey().getAlgorithm())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                    // Check the signature algorithm of the certificate itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    // Look for the "withRSA" in "SHA1withRSA", etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    String sigAlgName =
10334
26b816b3f236 7063647: To use synchronized map in key manager
xuelei
parents: 9035
diff changeset
   379
                        certs[0].getSigAlgName().toUpperCase(Locale.ENGLISH);
26b816b3f236 7063647: To use synchronized map in key manager
xuelei
parents: 9035
diff changeset
   380
                    String pattern = "WITH" +
26b816b3f236 7063647: To use synchronized map in key manager
xuelei
parents: 9035
diff changeset
   381
                        sigType.toUpperCase(Locale.ENGLISH);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                    if (sigAlgName.contains(pattern) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            if (issuers.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                // no issuer specified, match all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                aliases.add(alias);
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   391
                if (SSLLogger.isOn && SSLLogger.isOn("keymanager")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   392
                    SSLLogger.fine("matching alias: " + alias);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                Set<X500Principal> certIssuers =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                                        credentials.getIssuerX500Principals();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                for (int i = 0; i < x500Issuers.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                    if (certIssuers.contains(issuers[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                        aliases.add(alias);
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   400
                        if (SSLLogger.isOn && SSLLogger.isOn("keymanager")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   401
                            SSLLogger.fine("matching alias: " + alias);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   409
        String[] aliasStrings = aliases.toArray(STRING0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        return ((aliasStrings.length == 0) ? null : aliasStrings);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * Convert an array of Principals to an array of X500Principals, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * possible. Principals that cannot be converted are ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    private static X500Principal[] convertPrincipals(Principal[] principals) {
7990
57019dc81b66 7012003: diamond conversion for ssl
smarks
parents: 5506
diff changeset
   418
        List<X500Principal> list = new ArrayList<>(principals.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        for (int i = 0; i < principals.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            Principal p = principals[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            if (p instanceof X500Principal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                list.add((X500Principal)p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                    list.add(new X500Principal(p.getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                    // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        return list.toArray(new X500Principal[list.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
}