src/java.base/share/classes/sun/security/ssl/X509KeyManagerImpl.java
author xuelei
Mon, 25 Jun 2018 13:41:39 -0700
changeset 50768 68fa3d4026ea
parent 47216 71c04702a3d5
child 57950 4612a3cfb927
permissions -rw-r--r--
8196584: TLS 1.3 Implementation Reviewed-by: ascarpino, coffeys, dfuchs, jjiang, jnimeh, mullan, rhalade, ssahoo, valeriep, weijun, wetmore, xuelei Contributed-by: Adam Petcher <adam.petcher@oracle.com>, Amanda Jiang <amanda.jiang@oracle.com>, Anthony Scarpino <anthony.scarpino@oracle.com>, Bradford Wetmore <bradford.wetmore@oracle.com>, Jamil Nimeh <jamil.j.nimeh@oracle.com>, John Jiang <sha.jiang@oracle.com>, Rajan Halade <rajan.halade@oracle.com>, Sibabrata Sahoo <sibabrata.sahoo@oracle.com>, Valerie Peng <valerie.peng@oracle.com>, Weijun Wang <weijun.wang@oracle.com>, Xuelei Fan <xuelei.fan@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
     2
 * Copyright (c) 2004, 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.lang.ref.*;
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    29
import java.net.Socket;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    30
import java.security.AlgorithmConstraints;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    31
import java.security.KeyStore;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    32
import java.security.KeyStore.Builder;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    33
import java.security.KeyStore.Entry;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    34
import java.security.KeyStore.PrivateKeyEntry;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    35
import java.security.Principal;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    36
import java.security.PrivateKey;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    37
import java.security.cert.CertPathValidatorException;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    38
import java.security.cert.Certificate;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    39
import java.security.cert.CertificateException;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    40
import java.security.cert.X509Certificate;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.concurrent.atomic.AtomicLong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.net.ssl.*;
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
    44
import sun.security.provider.certpath.AlgorithmChecker;
43701
fe8c324ba97c 8160655: Fix denyAfter and usage types for security properties
ascarpino
parents: 43220
diff changeset
    45
import sun.security.validator.Validator;
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
    46
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * The new X509 key manager implementation. The main differences to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * old SunX509 key manager are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *  . it is based around the KeyStore.Builder API. This allows it to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *    other forms of KeyStore protection or password input (e.g. a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *    CallbackHandler) or to have keys within one KeyStore protected by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *    different keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *  . it can use multiple KeyStores at the same time.
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 14664
diff changeset
    55
 *  . it is explicitly designed to accommodate KeyStores that change over
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *    the lifetime of the process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *  . it makes an effort to choose the key that matches best, i.e. one that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *    is not expired and has the appropriate certificate extensions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * Note that this code is not explicitly performance optimzied yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
final class X509KeyManagerImpl extends X509ExtendedKeyManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        implements X509KeyManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // for unit testing only, set via privileged reflection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private static Date verificationDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // list of the builders
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private final List<Builder> builders;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // counter to generate unique ids for the aliases
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private final AtomicLong uidCounter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // cached entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private final Map<String,Reference<PrivateKeyEntry>> entryCacheMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    X509KeyManagerImpl(Builder builder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        this(Collections.singletonList(builder));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    X509KeyManagerImpl(List<Builder> builders) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        this.builders = builders;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        uidCounter = new AtomicLong();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        entryCacheMap = Collections.synchronizedMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                        (new SizedMap<String,Reference<PrivateKeyEntry>>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    // LinkedHashMap with a max size of 10
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    // see LinkedHashMap JavaDocs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private static class SizedMap<K,V> extends LinkedHashMap<K,V> {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
    93
        private static final long serialVersionUID = -8211222668790986062L;
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
    94
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        @Override protected boolean removeEldestEntry(Map.Entry<K,V> eldest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            return size() > 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    // public methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   104
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public X509Certificate[] getCertificateChain(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        PrivateKeyEntry entry = getEntry(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        return entry == null ? null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                (X509Certificate[])entry.getCertificateChain();
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: 14194
diff changeset
   111
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public PrivateKey getPrivateKey(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        PrivateKeyEntry entry = getEntry(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        return entry == null ? null : entry.getPrivateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   117
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public String chooseClientAlias(String[] keyTypes, Principal[] issuers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            Socket socket) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   120
        return chooseAlias(getKeyTypes(keyTypes), issuers, CheckType.CLIENT,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   121
                        getAlgorithmConstraints(socket));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   124
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public String chooseEngineClientAlias(String[] keyTypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            Principal[] issuers, SSLEngine engine) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   127
        return chooseAlias(getKeyTypes(keyTypes), issuers, CheckType.CLIENT,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   128
                        getAlgorithmConstraints(engine));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   131
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public String chooseServerAlias(String keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            Principal[] issuers, Socket socket) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   134
        return chooseAlias(getKeyTypes(keyType), issuers, CheckType.SERVER,
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   135
            getAlgorithmConstraints(socket),
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   136
            X509TrustManagerImpl.getRequestedServerNames(socket),
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   137
            "HTTPS");    // The SNI HostName is a fully qualified domain name.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   138
                         // The certificate selection scheme for SNI HostName
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   139
                         // is similar to HTTPS endpoint identification scheme
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   140
                         // implemented in this provider.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   141
                         //
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   142
                         // Using HTTPS endpoint identification scheme to guide
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   143
                         // the selection of an appropriate authentication
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   144
                         // certificate according to requested SNI extension.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   145
                         //
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   146
                         // It is not a really HTTPS endpoint identification.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   149
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public String chooseEngineServerAlias(String keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            Principal[] issuers, SSLEngine engine) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   152
        return chooseAlias(getKeyTypes(keyType), issuers, CheckType.SERVER,
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   153
            getAlgorithmConstraints(engine),
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   154
            X509TrustManagerImpl.getRequestedServerNames(engine),
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   155
            "HTTPS");    // The SNI HostName is a fully qualified domain name.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   156
                         // The certificate selection scheme for SNI HostName
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   157
                         // is similar to HTTPS endpoint identification scheme
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   158
                         // implemented in this provider.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   159
                         //
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   160
                         // Using HTTPS endpoint identification scheme to guide
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   161
                         // the selection of an appropriate authentication
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   162
                         // certificate according to requested SNI extension.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   163
                         //
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   164
                         // It is not a really HTTPS endpoint identification.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   167
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public String[] getClientAliases(String keyType, Principal[] issuers) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   169
        return getAliases(keyType, issuers, CheckType.CLIENT, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   172
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public String[] getServerAliases(String keyType, Principal[] issuers) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   174
        return getAliases(keyType, issuers, CheckType.SERVER, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    // implementation private methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   181
    // Gets algorithm constraints of the socket.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   182
    private AlgorithmConstraints getAlgorithmConstraints(Socket socket) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   183
        if (socket != null && socket.isConnected() &&
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   184
                                        socket instanceof SSLSocket) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   185
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   186
            SSLSocket sslSocket = (SSLSocket)socket;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   187
            SSLSession session = sslSocket.getHandshakeSession();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   188
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   189
            if (session != null) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   190
                if (ProtocolVersion.useTLS12PlusSpec(session.getProtocol())) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   191
                    String[] peerSupportedSignAlgs = null;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   192
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   193
                    if (session instanceof ExtendedSSLSession) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   194
                        ExtendedSSLSession extSession =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   195
                            (ExtendedSSLSession)session;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   196
                        peerSupportedSignAlgs =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   197
                            extSession.getPeerSupportedSignatureAlgorithms();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   198
                    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   199
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   200
                    return new SSLAlgorithmConstraints(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   201
                        sslSocket, peerSupportedSignAlgs, true);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   202
                }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   203
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   204
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   205
            return new SSLAlgorithmConstraints(sslSocket, true);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   206
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   207
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   208
        return new SSLAlgorithmConstraints((SSLSocket)null, true);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   209
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   210
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   211
    // Gets algorithm constraints of the engine.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   212
    private AlgorithmConstraints getAlgorithmConstraints(SSLEngine engine) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   213
        if (engine != null) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   214
            SSLSession session = engine.getHandshakeSession();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   215
            if (session != null) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   216
                if (ProtocolVersion.useTLS12PlusSpec(session.getProtocol())) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   217
                    String[] peerSupportedSignAlgs = null;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   218
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   219
                    if (session instanceof ExtendedSSLSession) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   220
                        ExtendedSSLSession extSession =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   221
                            (ExtendedSSLSession)session;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   222
                        peerSupportedSignAlgs =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   223
                            extSession.getPeerSupportedSignatureAlgorithms();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   224
                    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   225
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   226
                    return new SSLAlgorithmConstraints(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   227
                        engine, peerSupportedSignAlgs, true);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   228
                }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   229
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   230
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   231
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   232
        return new SSLAlgorithmConstraints(engine, true);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   233
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   234
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    // we construct the alias we return to JSSE as seen in the code below
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    // a unique id is included to allow us to reliably cache entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    // between the calls to getCertificateChain() and getPrivateKey()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    // even if tokens are inserted or removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    private String makeAlias(EntryStatus entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        return uidCounter.incrementAndGet() + "." + entry.builderIndex + "."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                + entry.alias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    private PrivateKeyEntry getEntry(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        // if the alias is null, return immediately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            return 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
        // try to get the entry from cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        Reference<PrivateKeyEntry> ref = entryCacheMap.get(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        PrivateKeyEntry entry = (ref != null) ? ref.get() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (entry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            return entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        // parse the alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        int firstDot = alias.indexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        int secondDot = alias.indexOf('.', firstDot + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        if ((firstDot == -1) || (secondDot == firstDot)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            // invalid alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            int builderIndex = Integer.parseInt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                                (alias.substring(firstDot + 1, secondDot));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            String keyStoreAlias = alias.substring(secondDot + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            Builder builder = builders.get(builderIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            KeyStore ks = builder.getKeyStore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            Entry newEntry = ks.getEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    (keyStoreAlias, builder.getProtectionParameter(alias));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            if (newEntry instanceof PrivateKeyEntry == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                // unexpected type of entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            entry = (PrivateKeyEntry)newEntry;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   277
            entryCacheMap.put(alias, new SoftReference<PrivateKeyEntry>(entry));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            return entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    // Class to help verify that the public key algorithm (and optionally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    // the signature algorithm) of a certificate matches what we need.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    private static class KeyType {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        final String keyAlgorithm;
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   290
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   291
        // In TLS 1.2, the signature algorithm  has been obsoleted by the
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   292
        // supported_signature_algorithms, and the certificate type no longer
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   293
        // restricts the algorithm used to sign the certificate.
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   294
        //
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   295
        // However, because we don't support certificate type checking other
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   296
        // than rsa_sign, dss_sign and ecdsa_sign, we don't have to check the
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   297
        // protocol version here.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        final String sigKeyAlgorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        KeyType(String algorithm) {
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 23010
diff changeset
   301
            int k = algorithm.indexOf('_');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            if (k == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                keyAlgorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                sigKeyAlgorithm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                keyAlgorithm = algorithm.substring(0, k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                sigKeyAlgorithm = algorithm.substring(k + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        boolean matches(Certificate[] chain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            if (!chain[0].getPublicKey().getAlgorithm().equals(keyAlgorithm)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            if (sigKeyAlgorithm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            if (chain.length > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                // if possible, check the public key in the issuer cert
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   320
                return sigKeyAlgorithm.equals(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   321
                        chain[1].getPublicKey().getAlgorithm());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                // Check the signature algorithm of the certificate itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                // Look for the "withRSA" in "SHA1withRSA", etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                X509Certificate issuer = (X509Certificate)chain[0];
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   326
                String sigAlgName =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   327
                        issuer.getSigAlgName().toUpperCase(Locale.ENGLISH);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   328
                String pattern =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   329
                        "WITH" + sigKeyAlgorithm.toUpperCase(Locale.ENGLISH);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                return sigAlgName.contains(pattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    private static List<KeyType> getKeyTypes(String ... keyTypes) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   336
        if ((keyTypes == null) ||
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   337
                (keyTypes.length == 0) || (keyTypes[0] == null)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
7990
57019dc81b66 7012003: diamond conversion for ssl
smarks
parents: 7043
diff changeset
   340
        List<KeyType> list = new ArrayList<>(keyTypes.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        for (String keyType : keyTypes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            list.add(new KeyType(keyType));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * Return the best alias that fits the given parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * The algorithm we use is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *   . scan through all the aliases in all builders in order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *   . as soon as we find a perfect match, return
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   352
     *     (i.e. a match with a cert that has appropriate key usage,
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   353
     *      qualified endpoint identity, and is not expired).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *   . if we do not find a perfect match, keep looping and remember
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *     the imperfect matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *   . at the end, sort the imperfect matches. we prefer expired certs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *     with appropriate key usage to certs with the wrong key usage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *     return the first one of them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     */
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   360
    private String chooseAlias(List<KeyType> keyTypeList, Principal[] issuers,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   361
            CheckType checkType, AlgorithmConstraints constraints) {
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   362
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   363
        return chooseAlias(keyTypeList, issuers,
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   364
                                    checkType, constraints, null, null);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   365
    }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   366
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   367
    private String chooseAlias(List<KeyType> keyTypeList, Principal[] issuers,
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   368
            CheckType checkType, AlgorithmConstraints constraints,
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   369
            List<SNIServerName> requestedServerNames, String idAlgorithm) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   370
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   371
        if (keyTypeList == null || keyTypeList.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        Set<Principal> issuerSet = getIssuerSet(issuers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        List<EntryStatus> allResults = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        for (int i = 0, n = builders.size(); i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            try {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   379
                List<EntryStatus> results = getAliases(i, keyTypeList,
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   380
                            issuerSet, false, checkType, constraints,
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   381
                            requestedServerNames, idAlgorithm);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                if (results != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                    // the results will either be a single perfect match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    // or 1 or more imperfect matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                    // if it's a perfect match, return immediately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    EntryStatus status = results.get(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                    if (status.checkResult == CheckResult.OK) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   388
                        if (SSLLogger.isOn && SSLLogger.isOn("keymanager")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   389
                            SSLLogger.fine("KeyMgr: choosing key: " + status);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                        return makeAlias(status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                    if (allResults == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                        allResults = new ArrayList<EntryStatus>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    allResults.addAll(results);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        if (allResults == null) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   403
            if (SSLLogger.isOn && SSLLogger.isOn("keymanager")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   404
                SSLLogger.fine("KeyMgr: no matching key found");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        Collections.sort(allResults);
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   409
        if (SSLLogger.isOn && SSLLogger.isOn("keymanager")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   410
            SSLLogger.fine(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   411
                    "KeyMgr: no good matching key found, "
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   412
                    + "returning best match out of", allResults);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        return makeAlias(allResults.get(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * Return all aliases that (approximately) fit the parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * These are perfect matches plus imperfect matches (expired certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * and certificates with the wrong extensions).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * The perfect matches will be first in the array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    public String[] getAliases(String keyType, Principal[] issuers,
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   424
            CheckType checkType, AlgorithmConstraints constraints) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        if (keyType == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        Set<Principal> issuerSet = getIssuerSet(issuers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        List<KeyType> keyTypeList = getKeyTypes(keyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        List<EntryStatus> allResults = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        for (int i = 0, n = builders.size(); i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            try {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   434
                List<EntryStatus> results = getAliases(i, keyTypeList,
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   435
                                    issuerSet, true, checkType, constraints,
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   436
                                    null, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                if (results != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                    if (allResults == null) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   439
                        allResults = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                    allResults.addAll(results);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        }
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   447
        if (allResults == null || allResults.isEmpty()) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   448
            if (SSLLogger.isOn && SSLLogger.isOn("keymanager")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   449
                SSLLogger.fine("KeyMgr: no matching alias found");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        Collections.sort(allResults);
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   454
        if (SSLLogger.isOn && SSLLogger.isOn("keymanager")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   455
            SSLLogger.fine("KeyMgr: getting aliases", allResults);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        return toAliases(allResults);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    // turn candidate entries into unique aliases we can return to JSSE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    private String[] toAliases(List<EntryStatus> results) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        String[] s = new String[results.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        for (EntryStatus result : results) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            s[i++] = makeAlias(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    // make a Set out of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    private Set<Principal> getIssuerSet(Principal[] issuers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        if ((issuers != null) && (issuers.length != 0)) {
7990
57019dc81b66 7012003: diamond conversion for ssl
smarks
parents: 7043
diff changeset
   473
            return new HashSet<>(Arrays.asList(issuers));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    // a candidate match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    // identifies the entry by builder and alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    // and includes the result of the certificate check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    private static class EntryStatus implements Comparable<EntryStatus> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        final int builderIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        final int keyIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        final String alias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        final CheckResult checkResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        EntryStatus(int builderIndex, int keyIndex, String alias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                Certificate[] chain, CheckResult checkResult) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            this.builderIndex = builderIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            this.keyIndex = keyIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            this.alias = alias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            this.checkResult = checkResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   497
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        public int compareTo(EntryStatus other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            int result = this.checkResult.compareTo(other.checkResult);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            return (result == 0) ? (this.keyIndex - other.keyIndex) : result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   503
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            String s = alias + " (verified: " + checkResult + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            if (builderIndex == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                return "Builder #" + builderIndex + ", alias: " + s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    // enum for the type of certificate check we want to perform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    // (client or server)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    // also includes the check code itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    private static enum CheckType {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        // enum constant for "no check" (currently not used)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        NONE(Collections.<String>emptySet()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        // enum constant for "tls client" check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        // valid EKU for TLS client: any, tls_client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        CLIENT(new HashSet<String>(Arrays.asList(new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            "2.5.29.37.0", "1.3.6.1.5.5.7.3.2" }))),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        // enum constant for "tls server" check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        // valid EKU for TLS server: any, tls_server, ns_sgc, ms_sgc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        SERVER(new HashSet<String>(Arrays.asList(new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            "2.5.29.37.0", "1.3.6.1.5.5.7.3.1", "2.16.840.1.113730.4.1",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            "1.3.6.1.4.1.311.10.3.3" })));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        // set of valid EKU values for this type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        final Set<String> validEku;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        CheckType(Set<String> validEku) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            this.validEku = validEku;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        private static boolean getBit(boolean[] keyUsage, int bit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            return (bit < keyUsage.length) && keyUsage[bit];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   544
        // Check if this certificate is appropriate for this type of use
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   545
        // first check extensions, if they match, check expiration.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   546
        //
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   547
        // Note: we may want to move this code into the sun.security.validator
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        // package
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   549
        CheckResult check(X509Certificate cert, Date date,
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   550
                List<SNIServerName> serverNames, String idAlgorithm) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   551
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            if (this == NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                return CheckResult.OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            // check extensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                // check extended key usage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                List<String> certEku = cert.getExtendedKeyUsage();
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   560
                if ((certEku != null) &&
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   561
                        Collections.disjoint(validEku, certEku)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                    // if extension present and it does not contain any of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                    // the valid EKU OIDs, return extension_mismatch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                    return CheckResult.EXTENSION_MISMATCH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                // check key usage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                boolean[] ku = cert.getKeyUsage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                if (ku != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                    String algorithm = cert.getPublicKey().getAlgorithm();
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   571
                    boolean supportsDigitalSignature = getBit(ku, 0);
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   572
                    switch (algorithm) {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   573
                        case "RSA":
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   574
                            // require either signature bit
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   575
                            // or if server also allow key encipherment bit
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   576
                            if (!supportsDigitalSignature) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   577
                                if (this == CLIENT || getBit(ku, 2) == false) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   578
                                    return CheckResult.EXTENSION_MISMATCH;
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   579
                                }
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   580
                            }
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   581
                            break;
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   582
                        case "RSASSA-PSS":
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   583
                            if (!supportsDigitalSignature && (this == SERVER)) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   584
                                return CheckResult.EXTENSION_MISMATCH;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   585
                            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   586
                            break;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   587
                        case "DSA":
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   588
                            // require signature bit
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   589
                            if (!supportsDigitalSignature) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                                return CheckResult.EXTENSION_MISMATCH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                            }
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   592
                            break;
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   593
                        case "DH":
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   594
                            // require keyagreement bit
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   595
                            if (getBit(ku, 4) == false) {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   596
                                return CheckResult.EXTENSION_MISMATCH;
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   597
                            }
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   598
                            break;
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   599
                        case "EC":
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   600
                            // require signature bit
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   601
                            if (!supportsDigitalSignature) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   602
                                return CheckResult.EXTENSION_MISMATCH;
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   603
                            }
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   604
                            // For servers, also require key agreement.
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   605
                            // This is not totally accurate as the keyAgreement
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   606
                            // bit is only necessary for static ECDH key
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   607
                            // exchange and not ephemeral ECDH. We leave it in
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   608
                            // for now until there are signs that this check
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   609
                            // causes problems for real world EC certificates.
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   610
                            if ((this == SERVER) && (getBit(ku, 4) == false)) {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   611
                                return CheckResult.EXTENSION_MISMATCH;
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   612
                            }
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   613
                            break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            } catch (CertificateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                // extensions unparseable, return failure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                return CheckResult.EXTENSION_MISMATCH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                cert.checkValidity(date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            } catch (CertificateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                return CheckResult.EXPIRED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            }
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   626
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   627
            if (serverNames != null && !serverNames.isEmpty()) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   628
                for (SNIServerName serverName : serverNames) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   629
                    if (serverName.getType() ==
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   630
                                StandardConstants.SNI_HOST_NAME) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   631
                        if (!(serverName instanceof SNIHostName)) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   632
                            try {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   633
                                serverName =
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   634
                                    new SNIHostName(serverName.getEncoded());
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   635
                            } catch (IllegalArgumentException iae) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   636
                                // unlikely to happen, just in case ...
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   637
                                if (SSLLogger.isOn &&
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   638
                                        SSLLogger.isOn("keymanager")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   639
                                    SSLLogger.fine(
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   640
                                       "Illegal server name: " + serverName);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   641
                                }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   642
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   643
                                return CheckResult.INSENSITIVE;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   644
                            }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   645
                        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   646
                        String hostname =
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   647
                                ((SNIHostName)serverName).getAsciiName();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   648
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   649
                        try {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   650
                            X509TrustManagerImpl.checkIdentity(hostname,
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   651
                                                        cert, idAlgorithm);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   652
                        } catch (CertificateException e) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   653
                            if (SSLLogger.isOn &&
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   654
                                    SSLLogger.isOn("keymanager")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   655
                                SSLLogger.fine(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   656
                                    "Certificate identity does not match " +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   657
                                    "Server Name Inidication (SNI): " +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   658
                                    hostname);
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   659
                            }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   660
                            return CheckResult.INSENSITIVE;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   661
                        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   662
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   663
                        break;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   664
                    }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   665
                }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   666
            }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   667
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   668
            return CheckResult.OK;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        }
43701
fe8c324ba97c 8160655: Fix denyAfter and usage types for security properties
ascarpino
parents: 43220
diff changeset
   670
fe8c324ba97c 8160655: Fix denyAfter and usage types for security properties
ascarpino
parents: 43220
diff changeset
   671
        public String getValidator() {
fe8c324ba97c 8160655: Fix denyAfter and usage types for security properties
ascarpino
parents: 43220
diff changeset
   672
            if (this == CLIENT) {
fe8c324ba97c 8160655: Fix denyAfter and usage types for security properties
ascarpino
parents: 43220
diff changeset
   673
                return Validator.VAR_TLS_CLIENT;
fe8c324ba97c 8160655: Fix denyAfter and usage types for security properties
ascarpino
parents: 43220
diff changeset
   674
            } else if (this == SERVER) {
fe8c324ba97c 8160655: Fix denyAfter and usage types for security properties
ascarpino
parents: 43220
diff changeset
   675
                return Validator.VAR_TLS_SERVER;
fe8c324ba97c 8160655: Fix denyAfter and usage types for security properties
ascarpino
parents: 43220
diff changeset
   676
            }
fe8c324ba97c 8160655: Fix denyAfter and usage types for security properties
ascarpino
parents: 43220
diff changeset
   677
            return Validator.VAR_GENERIC;
fe8c324ba97c 8160655: Fix denyAfter and usage types for security properties
ascarpino
parents: 43220
diff changeset
   678
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    // enum for the result of the extension check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    // NOTE: the order of the constants is important as they are used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    // for sorting, i.e. OK is best, followed by EXPIRED and EXTENSION_MISMATCH
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    private static enum CheckResult {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        OK,                     // ok or not checked
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   686
        INSENSITIVE,            // server name indication insensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        EXPIRED,                // extensions valid but cert expired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        EXTENSION_MISMATCH,     // extensions invalid (expiration not checked)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * Return a List of all candidate matches in the specified builder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * that fit the parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * We exclude entries in the KeyStore if they are not:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     *  . private key entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     *  . the certificates are not X509 certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     *  . the algorithm of the key in the EE cert doesn't match one of keyTypes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     *  . none of the certs is issued by a Principal in issuerSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * Using those entries would not be possible or they would almost
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * certainly be rejected by the peer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * In addition to those checks, we also check the extensions in the EE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * cert and its expiration. Even if there is a mismatch, we include
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * such certificates because they technically work and might be accepted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * by the peer. This leads to more graceful failure and better error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * messages if the cert expires from one day to the next.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * The return values are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     *   . null, if there are no matching entries at all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     *   . if 'findAll' is 'false' and there is a perfect match, a List
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     *     with a single element (early return)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     *   . if 'findAll' is 'false' and there is NO perfect match, a List
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     *     with all the imperfect matches (expired, wrong extensions)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     *   . if 'findAll' is 'true', a List with all perfect and imperfect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     *     matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    private List<EntryStatus> getAliases(int builderIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            List<KeyType> keyTypes, Set<Principal> issuerSet,
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   719
            boolean findAll, CheckType checkType,
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   720
            AlgorithmConstraints constraints,
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   721
            List<SNIServerName> requestedServerNames,
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   722
            String idAlgorithm) throws Exception {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   723
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        Builder builder = builders.get(builderIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        KeyStore ks = builder.getKeyStore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        List<EntryStatus> results = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        Date date = verificationDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        boolean preferred = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        for (Enumeration<String> e = ks.aliases(); e.hasMoreElements(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            String alias = e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            // check if it is a key entry (private key or secret key)
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   732
            if (!ks.isKeyEntry(alias)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            Certificate[] chain = ks.getCertificateChain(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            if ((chain == null) || (chain.length == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                // must be secret key entry, ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            }
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   741
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   742
            boolean incompatible = false;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   743
            for (Certificate cert : chain) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   744
                if (cert instanceof X509Certificate == false) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   745
                    // not an X509Certificate, ignore this alias
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   746
                    incompatible = true;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   747
                    break;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   748
                }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   749
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   750
            if (incompatible) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   751
                continue;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   752
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   753
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            // check keytype
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            int keyIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            int j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            for (KeyType keyType : keyTypes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                if (keyType.matches(chain)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                    keyIndex = j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                j++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            if (keyIndex == -1) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   765
                if (SSLLogger.isOn && SSLLogger.isOn("keymanager")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   766
                    SSLLogger.fine("Ignore alias " + alias
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                                + ": key algorithm does not match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            // check issuers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            if (issuerSet != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                boolean found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                for (Certificate cert : chain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                    X509Certificate xcert = (X509Certificate)cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                    if (issuerSet.contains(xcert.getIssuerX500Principal())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                        found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                if (found == false) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   782
                    if (SSLLogger.isOn && SSLLogger.isOn("keymanager")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   783
                        SSLLogger.fine(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   784
                                "Ignore alias " + alias
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   785
                                + ": issuers do not match");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            }
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   790
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   791
            // check the algorithm constraints
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   792
            if (constraints != null &&
43701
fe8c324ba97c 8160655: Fix denyAfter and usage types for security properties
ascarpino
parents: 43220
diff changeset
   793
                    !conformsToAlgorithmConstraints(constraints, chain,
fe8c324ba97c 8160655: Fix denyAfter and usage types for security properties
ascarpino
parents: 43220
diff changeset
   794
                            checkType.getValidator())) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   795
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   796
                if (SSLLogger.isOn && SSLLogger.isOn("keymanager")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   797
                    SSLLogger.fine("Ignore alias " + alias +
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   798
                            ": certificate list does not conform to " +
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   799
                            "algorithm constraints");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   800
                }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   801
                continue;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   802
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   803
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            if (date == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                date = new Date();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            CheckResult checkResult =
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   808
                    checkType.check((X509Certificate)chain[0], date,
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 10336
diff changeset
   809
                                    requestedServerNames, idAlgorithm);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            EntryStatus status =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                    new EntryStatus(builderIndex, keyIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                                        alias, chain, checkResult);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            if (!preferred && checkResult == CheckResult.OK && keyIndex == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                preferred = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            if (preferred && (findAll == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                // if we have a good match and do not need all matches,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                // return immediately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                return Collections.singletonList(status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                if (results == null) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   822
                    results = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                results.add(status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        return results;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   830
    private static boolean conformsToAlgorithmConstraints(
43701
fe8c324ba97c 8160655: Fix denyAfter and usage types for security properties
ascarpino
parents: 43220
diff changeset
   831
            AlgorithmConstraints constraints, Certificate[] chain,
fe8c324ba97c 8160655: Fix denyAfter and usage types for security properties
ascarpino
parents: 43220
diff changeset
   832
            String variant) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   833
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   834
        AlgorithmChecker checker =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   835
                new AlgorithmChecker(constraints, null, variant);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   836
        try {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   837
            checker.init(false);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   838
        } catch (CertPathValidatorException cpve) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   839
            // unlikely to happen
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   840
            if (SSLLogger.isOn && SSLLogger.isOn("keymanager")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   841
                SSLLogger.fine(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   842
                    "Cannot initialize algorithm constraints checker", cpve);
43220
937cb78b2016 8173066: More verbose debug output for selection of X509 certs
xuelei
parents: 32649
diff changeset
   843
            }
937cb78b2016 8173066: More verbose debug output for selection of X509 certs
xuelei
parents: 32649
diff changeset
   844
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   845
            return false;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   846
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   847
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   848
        // It is a forward checker, so we need to check from trust to target.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   849
        for (int i = chain.length - 1; i >= 0; i--) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   850
            Certificate cert = chain[i];
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   851
            try {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   852
                // We don't care about the unresolved critical extensions.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   853
                checker.check(cert, Collections.<String>emptySet());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   854
            } catch (CertPathValidatorException cpve) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   855
                if (SSLLogger.isOn && SSLLogger.isOn("keymanager")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   856
                    SSLLogger.fine("Certificate does not conform to " +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   857
                            "algorithm constraints", cert, cpve);
43220
937cb78b2016 8173066: More verbose debug output for selection of X509 certs
xuelei
parents: 32649
diff changeset
   858
                }
937cb78b2016 8173066: More verbose debug output for selection of X509 certs
xuelei
parents: 32649
diff changeset
   859
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   860
                return false;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   861
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   862
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   863
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   864
        return true;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   865
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
}