jdk/src/share/classes/sun/security/ssl/X509KeyManagerImpl.java
author xuelei
Fri, 08 Apr 2011 02:00:09 -0700
changeset 9246 c459f79af46b
parent 7990 57019dc81b66
child 9035 1255eb81cc2f
permissions -rw-r--r--
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled Summary: Reorg the SSLContext implementation Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
     2
 * Copyright (c) 2004, 2010, 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.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *  . it is explicitly designed to accomodate KeyStores that change over
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private final static boolean useDebug =
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> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        @Override protected boolean removeEldestEntry(Map.Entry<K,V> eldest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            return size() > 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    // public methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public X509Certificate[] getCertificateChain(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        PrivateKeyEntry entry = getEntry(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        return entry == null ? null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                (X509Certificate[])entry.getCertificateChain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public PrivateKey getPrivateKey(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        PrivateKeyEntry entry = getEntry(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        return entry == null ? null : entry.getPrivateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public String chooseClientAlias(String[] keyTypes, Principal[] issuers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            Socket socket) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   116
        return chooseAlias(getKeyTypes(keyTypes), issuers, CheckType.CLIENT,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   117
                        getAlgorithmConstraints(socket));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public String chooseEngineClientAlias(String[] keyTypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            Principal[] issuers, SSLEngine engine) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   122
        return chooseAlias(getKeyTypes(keyTypes), issuers, CheckType.CLIENT,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   123
                        getAlgorithmConstraints(engine));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public String chooseServerAlias(String keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            Principal[] issuers, Socket socket) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   128
        return chooseAlias(getKeyTypes(keyType), issuers, CheckType.SERVER,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   129
                        getAlgorithmConstraints(socket));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public String chooseEngineServerAlias(String keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            Principal[] issuers, SSLEngine engine) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   134
        return chooseAlias(getKeyTypes(keyType), issuers, CheckType.SERVER,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   135
                        getAlgorithmConstraints(engine));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public String[] getClientAliases(String keyType, Principal[] issuers) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   139
        return getAliases(keyType, issuers, CheckType.CLIENT, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public String[] getServerAliases(String keyType, Principal[] issuers) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   143
        return getAliases(keyType, issuers, CheckType.SERVER, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    // implementation private methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   150
    // Gets algorithm constraints of the socket.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   151
    private AlgorithmConstraints getAlgorithmConstraints(Socket socket) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   152
        if (socket != null && socket.isConnected() &&
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   153
                                        socket instanceof SSLSocket) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   154
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   155
            SSLSocket sslSocket = (SSLSocket)socket;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   156
            SSLSession session = sslSocket.getHandshakeSession();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   157
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   158
            if (session != null) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   159
                ProtocolVersion protocolVersion =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   160
                    ProtocolVersion.valueOf(session.getProtocol());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   161
                if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   162
                    String[] peerSupportedSignAlgs = null;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   163
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   164
                    if (session instanceof ExtendedSSLSession) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   165
                        ExtendedSSLSession extSession =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   166
                            (ExtendedSSLSession)session;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   167
                        peerSupportedSignAlgs =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   168
                            extSession.getPeerSupportedSignatureAlgorithms();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   169
                    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   170
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   171
                    return new SSLAlgorithmConstraints(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   172
                        sslSocket, peerSupportedSignAlgs, true);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   173
                }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   174
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   175
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   176
            return new SSLAlgorithmConstraints(sslSocket, true);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   177
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   178
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   179
        return new SSLAlgorithmConstraints((SSLSocket)null, true);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   180
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   181
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   182
    // Gets algorithm constraints of the engine.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   183
    private AlgorithmConstraints getAlgorithmConstraints(SSLEngine engine) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   184
        if (engine != null) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   185
            SSLSession session = engine.getHandshakeSession();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   186
            if (session != null) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   187
                ProtocolVersion protocolVersion =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   188
                    ProtocolVersion.valueOf(session.getProtocol());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   189
                if (protocolVersion.v >= ProtocolVersion.TLS12.v) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   190
                    String[] peerSupportedSignAlgs = null;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   191
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   192
                    if (session instanceof ExtendedSSLSession) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   193
                        ExtendedSSLSession extSession =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   194
                            (ExtendedSSLSession)session;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   195
                        peerSupportedSignAlgs =
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   196
                            extSession.getPeerSupportedSignatureAlgorithms();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   197
                    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   198
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   199
                    return new SSLAlgorithmConstraints(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   200
                        engine, peerSupportedSignAlgs, true);
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
        }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   204
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   205
        return new SSLAlgorithmConstraints(engine, true);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   206
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   207
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    // we construct the alias we return to JSSE as seen in the code below
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    // a unique id is included to allow us to reliably cache entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    // between the calls to getCertificateChain() and getPrivateKey()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    // even if tokens are inserted or removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    private String makeAlias(EntryStatus entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        return uidCounter.incrementAndGet() + "." + entry.builderIndex + "."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                + entry.alias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    private PrivateKeyEntry getEntry(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        // if the alias is null, return immediately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        // try to get the entry from cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        Reference<PrivateKeyEntry> ref = entryCacheMap.get(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        PrivateKeyEntry entry = (ref != null) ? ref.get() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        if (entry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            return entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        // parse the alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        int firstDot = alias.indexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        int secondDot = alias.indexOf('.', firstDot + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if ((firstDot == -1) || (secondDot == firstDot)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            // invalid alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            int builderIndex = Integer.parseInt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                                (alias.substring(firstDot + 1, secondDot));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            String keyStoreAlias = alias.substring(secondDot + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            Builder builder = builders.get(builderIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            KeyStore ks = builder.getKeyStore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            Entry newEntry = ks.getEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    (keyStoreAlias, builder.getProtectionParameter(alias));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            if (newEntry instanceof PrivateKeyEntry == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                // unexpected type of entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            entry = (PrivateKeyEntry)newEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            entryCacheMap.put(alias, new SoftReference(entry));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            return entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    // Class to help verify that the public key algorithm (and optionally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    // the signature algorithm) of a certificate matches what we need.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    private static class KeyType {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        final String keyAlgorithm;
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   263
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   264
        // 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
   265
        // supported_signature_algorithms, and the certificate type no longer
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   266
        // restricts the algorithm used to sign the certificate.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   267
        // However, because we don't support certificate type checking other
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   268
        // 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
   269
        // protocol version here.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        final String sigKeyAlgorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        KeyType(String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            int k = algorithm.indexOf("_");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            if (k == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                keyAlgorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                sigKeyAlgorithm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                keyAlgorithm = algorithm.substring(0, k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                sigKeyAlgorithm = algorithm.substring(k + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        boolean matches(Certificate[] chain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            if (!chain[0].getPublicKey().getAlgorithm().equals(keyAlgorithm)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            if (sigKeyAlgorithm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            if (chain.length > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                // if possible, check the public key in the issuer cert
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   292
                return sigKeyAlgorithm.equals(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   293
                        chain[1].getPublicKey().getAlgorithm());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                // Check the signature algorithm of the certificate itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                // Look for the "withRSA" in "SHA1withRSA", etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                X509Certificate issuer = (X509Certificate)chain[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                String sigAlgName = issuer.getSigAlgName().toUpperCase(ENGLISH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                String pattern = "WITH" + sigKeyAlgorithm.toUpperCase(ENGLISH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                return sigAlgName.contains(pattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    private static List<KeyType> getKeyTypes(String ... keyTypes) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   306
        if ((keyTypes == null) ||
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   307
                (keyTypes.length == 0) || (keyTypes[0] == null)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
7990
57019dc81b66 7012003: diamond conversion for ssl
smarks
parents: 7043
diff changeset
   310
        List<KeyType> list = new ArrayList<>(keyTypes.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        for (String keyType : keyTypes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            list.add(new KeyType(keyType));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * Return the best alias that fits the given parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * The algorithm we use is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *   . scan through all the aliases in all builders in order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *   . as soon as we find a perfect match, return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *     (i.e. a match with a cert that has appropriate key usage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *      and is not expired).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *   . if we do not find a perfect match, keep looping and remember
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *     the imperfect matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *   . at the end, sort the imperfect matches. we prefer expired certs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     *     with appropriate key usage to certs with the wrong key usage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *     return the first one of them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     */
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   330
    private String chooseAlias(List<KeyType> keyTypeList, Principal[] issuers,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   331
            CheckType checkType, AlgorithmConstraints constraints) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        if (keyTypeList == null || keyTypeList.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        Set<Principal> issuerSet = getIssuerSet(issuers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        List<EntryStatus> allResults = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        for (int i = 0, n = builders.size(); i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            try {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   340
                List<EntryStatus> results = getAliases(i, keyTypeList,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   341
                                    issuerSet, false, checkType, constraints);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                if (results != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    // the results will either be a single perfect match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    // or 1 or more imperfect matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    // if it's a perfect match, return immediately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    EntryStatus status = results.get(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    if (status.checkResult == CheckResult.OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                        if (useDebug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                            debug.println("KeyMgr: choosing key: " + status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                        return makeAlias(status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    if (allResults == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                        allResults = new ArrayList<EntryStatus>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    allResults.addAll(results);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        if (allResults == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            if (useDebug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                debug.println("KeyMgr: no matching key found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        Collections.sort(allResults);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        if (useDebug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            debug.println("KeyMgr: no good matching key found, "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                        + "returning best match out of:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            debug.println(allResults.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        return makeAlias(allResults.get(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * Return all aliases that (approximately) fit the parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * These are perfect matches plus imperfect matches (expired certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * and certificates with the wrong extensions).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * The perfect matches will be first in the array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    public String[] getAliases(String keyType, Principal[] issuers,
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   384
            CheckType checkType, AlgorithmConstraints constraints) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        if (keyType == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        Set<Principal> issuerSet = getIssuerSet(issuers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        List<KeyType> keyTypeList = getKeyTypes(keyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        List<EntryStatus> allResults = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        for (int i = 0, n = builders.size(); i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            try {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   394
                List<EntryStatus> results = getAliases(i, keyTypeList,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   395
                                    issuerSet, true, checkType, constraints);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                if (results != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                    if (allResults == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                        allResults = new ArrayList<EntryStatus>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                    allResults.addAll(results);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        if (allResults == null || allResults.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            if (useDebug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                debug.println("KeyMgr: no matching alias found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        Collections.sort(allResults);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        if (useDebug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            debug.println("KeyMgr: getting aliases: " + allResults);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        return toAliases(allResults);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    // turn candidate entries into unique aliases we can return to JSSE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    private String[] toAliases(List<EntryStatus> results) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        String[] s = new String[results.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        for (EntryStatus result : results) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            s[i++] = makeAlias(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    // make a Set out of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    private Set<Principal> getIssuerSet(Principal[] issuers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        if ((issuers != null) && (issuers.length != 0)) {
7990
57019dc81b66 7012003: diamond conversion for ssl
smarks
parents: 7043
diff changeset
   432
            return new HashSet<>(Arrays.asList(issuers));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    // a candidate match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    // identifies the entry by builder and alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    // and includes the result of the certificate check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    private static class EntryStatus implements Comparable<EntryStatus> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        final int builderIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        final int keyIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        final String alias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        final CheckResult checkResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        EntryStatus(int builderIndex, int keyIndex, String alias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                Certificate[] chain, CheckResult checkResult) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            this.builderIndex = builderIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            this.keyIndex = keyIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            this.alias = alias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            this.checkResult = checkResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        public int compareTo(EntryStatus other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            int result = this.checkResult.compareTo(other.checkResult);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            return (result == 0) ? (this.keyIndex - other.keyIndex) : result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            String s = alias + " (verified: " + checkResult + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            if (builderIndex == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                return "Builder #" + builderIndex + ", alias: " + s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    // enum for the type of certificate check we want to perform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    // (client or server)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    // also includes the check code itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    private static enum CheckType {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        // enum constant for "no check" (currently not used)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        NONE(Collections.<String>emptySet()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        // enum constant for "tls client" check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        // valid EKU for TLS client: any, tls_client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        CLIENT(new HashSet<String>(Arrays.asList(new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            "2.5.29.37.0", "1.3.6.1.5.5.7.3.2" }))),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        // enum constant for "tls server" check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        // valid EKU for TLS server: any, tls_server, ns_sgc, ms_sgc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        SERVER(new HashSet<String>(Arrays.asList(new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            "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
   488
            "1.3.6.1.4.1.311.10.3.3" })));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        // set of valid EKU values for this type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        final Set<String> validEku;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        CheckType(Set<String> validEku) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            this.validEku = validEku;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        private static boolean getBit(boolean[] keyUsage, int bit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            return (bit < keyUsage.length) && keyUsage[bit];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        // check if this certificate is appropriate for this type of use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        // first check extensions, if they match, check expiration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        // note: we may want to move this code into the sun.security.validator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        // package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        CheckResult check(X509Certificate cert, Date date) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            if (this == NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                return CheckResult.OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            // check extensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                // check extended key usage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                List<String> certEku = cert.getExtendedKeyUsage();
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   514
                if ((certEku != null) &&
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   515
                        Collections.disjoint(validEku, certEku)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                    // if extension present and it does not contain any of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                    // the valid EKU OIDs, return extension_mismatch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                    return CheckResult.EXTENSION_MISMATCH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                // check key usage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                boolean[] ku = cert.getKeyUsage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                if (ku != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                    String algorithm = cert.getPublicKey().getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                    boolean kuSignature = getBit(ku, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                    if (algorithm.equals("RSA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                        // require either signature bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                        // or if server also allow key encipherment bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                        if (kuSignature == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                            if ((this == CLIENT) || (getBit(ku, 2) == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                                return CheckResult.EXTENSION_MISMATCH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                    } else if (algorithm.equals("DSA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                        // require signature bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                        if (kuSignature == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                            return CheckResult.EXTENSION_MISMATCH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                    } else if (algorithm.equals("DH")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                        // require keyagreement bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                        if (getBit(ku, 4) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                            return CheckResult.EXTENSION_MISMATCH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                    } else if (algorithm.equals("EC")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                        // require signature bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                        if (kuSignature == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                            return CheckResult.EXTENSION_MISMATCH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                        // For servers, also require key agreement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                        // This is not totally accurate as the keyAgreement bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                        // is only necessary for static ECDH key exchange and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                        // not ephemeral ECDH. We leave it in for now until
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                        // there are signs that this check causes problems
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                        // for real world EC certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                        if ((this == SERVER) && (getBit(ku, 4) == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                            return CheckResult.EXTENSION_MISMATCH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            } catch (CertificateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                // extensions unparseable, return failure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                return CheckResult.EXTENSION_MISMATCH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                cert.checkValidity(date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                return CheckResult.OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            } catch (CertificateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                return CheckResult.EXPIRED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    // enum for the result of the extension check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    // NOTE: the order of the constants is important as they are used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    // for sorting, i.e. OK is best, followed by EXPIRED and EXTENSION_MISMATCH
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    private static enum CheckResult {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        OK,                     // ok or not checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        EXPIRED,                // extensions valid but cert expired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        EXTENSION_MISMATCH,     // extensions invalid (expiration not checked)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * Return a List of all candidate matches in the specified builder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * that fit the parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * We exclude entries in the KeyStore if they are not:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     *  . private key entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     *  . the certificates are not X509 certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     *  . the algorithm of the key in the EE cert doesn't match one of keyTypes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     *  . none of the certs is issued by a Principal in issuerSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * Using those entries would not be possible or they would almost
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * certainly be rejected by the peer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * In addition to those checks, we also check the extensions in the EE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * cert and its expiration. Even if there is a mismatch, we include
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * such certificates because they technically work and might be accepted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * by the peer. This leads to more graceful failure and better error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * messages if the cert expires from one day to the next.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * The return values are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     *   . null, if there are no matching entries at all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     *   . if 'findAll' is 'false' and there is a perfect match, a List
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     *     with a single element (early return)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     *   . if 'findAll' is 'false' and there is NO perfect match, a List
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     *     with all the imperfect matches (expired, wrong extensions)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     *   . if 'findAll' is 'true', a List with all perfect and imperfect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     *     matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    private List<EntryStatus> getAliases(int builderIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            List<KeyType> keyTypes, Set<Principal> issuerSet,
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   611
            boolean findAll, CheckType checkType,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   612
            AlgorithmConstraints constraints) throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        Builder builder = builders.get(builderIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        KeyStore ks = builder.getKeyStore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        List<EntryStatus> results = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        Date date = verificationDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        boolean preferred = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        for (Enumeration<String> e = ks.aliases(); e.hasMoreElements(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            String alias = e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            // check if it is a key entry (private key or secret key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            if (ks.isKeyEntry(alias) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            Certificate[] chain = ks.getCertificateChain(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            if ((chain == null) || (chain.length == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                // must be secret key entry, ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            }
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   630
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   631
            boolean incompatible = false;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   632
            for (Certificate cert : chain) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   633
                if (cert instanceof X509Certificate == false) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   634
                    // not an X509Certificate, ignore this alias
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   635
                    incompatible = true;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   636
                    break;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   637
                }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   638
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   639
            if (incompatible) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   640
                continue;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   641
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   642
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            // check keytype
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            int keyIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            int j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            for (KeyType keyType : keyTypes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                if (keyType.matches(chain)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                    keyIndex = j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                j++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            if (keyIndex == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                if (useDebug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                    debug.println("Ignoring alias " + alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                                + ": key algorithm does not match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            // check issuers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            if (issuerSet != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                boolean found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                for (Certificate cert : chain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                    X509Certificate xcert = (X509Certificate)cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                    if (issuerSet.contains(xcert.getIssuerX500Principal())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                        found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                if (found == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                    if (useDebug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                        debug.println("Ignoring alias " + alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                                    + ": issuers do not match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            }
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   678
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   679
            // check the algorithm constraints
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   680
            if (constraints != null &&
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   681
                    !conformsToAlgorithmConstraints(constraints, chain)) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   682
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   683
                if (useDebug) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   684
                    debug.println("Ignoring alias " + alias +
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   685
                            ": certificate list does not conform to " +
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   686
                            "algorithm constraints");
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   687
                }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   688
                continue;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   689
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   690
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            if (date == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                date = new Date();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            CheckResult checkResult =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                    checkType.check((X509Certificate)chain[0], date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            EntryStatus status =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                    new EntryStatus(builderIndex, keyIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                                        alias, chain, checkResult);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            if (!preferred && checkResult == CheckResult.OK && keyIndex == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                preferred = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            if (preferred && (findAll == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                // if we have a good match and do not need all matches,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                // return immediately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                return Collections.singletonList(status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                if (results == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                    results = new ArrayList<EntryStatus>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                results.add(status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        return results;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   716
    private static boolean conformsToAlgorithmConstraints(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   717
            AlgorithmConstraints constraints, Certificate[] chain) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   718
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   719
        AlgorithmChecker checker = new AlgorithmChecker(constraints);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   720
        try {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   721
            checker.init(false);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   722
        } catch (CertPathValidatorException cpve) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   723
            // unlikely to happen
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   724
            return false;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   725
        }
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
        // 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
   728
        for (int i = chain.length - 1; i >= 0; i--) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   729
            Certificate cert = chain[i];
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   730
            try {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   731
                // We don't care about the unresolved critical extensions.
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   732
                checker.check(cert, Collections.<String>emptySet());
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   733
            } catch (CertPathValidatorException cpve) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   734
                return false;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   735
            }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   736
        }
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
        return true;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   739
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   740
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
}