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