src/java.base/share/classes/sun/security/ssl/CertificateMessage.java
author xuelei
Mon, 25 Jun 2018 08:14:11 -0700
branchJDK-8145252-TLS13-branch
changeset 56806 32a737f51e37
parent 56782 b472b5917a1b
permissions -rw-r--r--
Support RSASSS-PSS for TLS 1.2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     1
/*
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     2
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     4
 *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    10
 *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    15
 * accompanied this code).
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    16
 *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    20
 *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    23
 * questions.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    24
 */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    25
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    26
package sun.security.ssl;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    27
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    28
import java.io.ByteArrayInputStream;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    29
import java.io.IOException;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    30
import java.nio.ByteBuffer;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    31
import java.security.PublicKey;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    32
import java.security.cert.CertPathValidatorException;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    33
import java.security.cert.CertPathValidatorException.BasicReason;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    34
import java.security.cert.CertPathValidatorException.Reason;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    35
import java.security.cert.CertificateEncodingException;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    36
import java.security.cert.CertificateException;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    37
import java.security.cert.CertificateFactory;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    38
import java.security.cert.CertificateParsingException;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    39
import java.security.cert.X509Certificate;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    40
import java.text.MessageFormat;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    41
import java.util.ArrayList;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    42
import java.util.Arrays;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    43
import java.util.Collection;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    44
import java.util.Collections;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    45
import java.util.HashSet;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    46
import java.util.LinkedList;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    47
import java.util.List;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    48
import java.util.Locale;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    49
import javax.net.ssl.SSLEngine;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    50
import javax.net.ssl.SSLException;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    51
import javax.net.ssl.SSLProtocolException;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    52
import javax.net.ssl.SSLSocket;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    53
import javax.net.ssl.X509ExtendedTrustManager;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    54
import javax.net.ssl.X509TrustManager;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    55
import javax.security.auth.x500.X500Principal;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    56
import static sun.security.ssl.ClientAuthType.CLIENT_AUTH_REQUIRED;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    57
import sun.security.ssl.ClientHello.ClientHelloMessage;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    58
import sun.security.ssl.SSLHandshake.HandshakeMessage;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    59
import sun.security.ssl.X509Authentication.X509Credentials;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    60
import sun.security.ssl.X509Authentication.X509Possession;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    61
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    62
/**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    63
 * Pack of the CertificateMessage handshake message.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    64
 */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    65
final class CertificateMessage {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    66
    static final SSLConsumer t12HandshakeConsumer =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    67
        new T12CertificateConsumer();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    68
    static final HandshakeProducer t12HandshakeProducer =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    69
        new T12CertificateProducer();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    70
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    71
    static final SSLConsumer t13HandshakeConsumer =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    72
        new T13CertificateConsumer();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    73
    static final HandshakeProducer t13HandshakeProducer =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    74
        new T13CertificateProducer();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    75
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    76
    /**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    77
     * The Certificate handshake message for TLS 1.2 and previous
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    78
     * SSL/TLS protocol versions.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    79
     *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    80
     * In server mode, the certificate handshake message is sent whenever the
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    81
     * agreed-upon key exchange method uses certificates for authentication.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    82
     * In client mode, this message is only sent if the server requests a
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    83
     * certificate for client authentication.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    84
     *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    85
     *       opaque ASN.1Cert<1..2^24-1>;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    86
     *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    87
     * SSL 3.0:
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    88
     *       struct {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    89
     *           ASN.1Cert certificate_list<1..2^24-1>;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    90
     *       } Certificate;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    91
     * Note: For SSL 3.0 client authentication, if no suitable certificate
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    92
     * is available, the client should send a no_certificate alert instead.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    93
     * This alert is only a warning; however, the server may respond with
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    94
     * a fatal handshake failure alert if client authentication is required.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    95
     *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    96
     * TLS 1.0/1.1/1.2:
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    97
     *       struct {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    98
     *           ASN.1Cert certificate_list<0..2^24-1>;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    99
     *       } Certificate;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   100
     */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   101
    static final class T12CertificateMessage extends HandshakeMessage {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   102
        final List<byte[]> encodedCertChain;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   103
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   104
        T12CertificateMessage(HandshakeContext handshakeContext,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   105
                X509Certificate[] certChain) throws SSLException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   106
            super(handshakeContext);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   107
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   108
            List<byte[]> encodedCerts = new ArrayList<>(certChain.length);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   109
            for (X509Certificate cert : certChain) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   110
                try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   111
                    encodedCerts.add(cert.getEncoded());
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   112
                } catch (CertificateEncodingException cee) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   113
                    // unlikely
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   114
                    handshakeContext.conContext.fatal(Alert.INTERNAL_ERROR,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   115
                            "Could not encode certificate (" +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   116
                            cert.getSubjectX500Principal() + ")", cee);
56761
001e4e7c0a01 Various nits and dead code removal
ascarpino
parents: 56603
diff changeset
   117
                    break;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   118
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   119
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   120
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   121
            this.encodedCertChain = encodedCerts;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   122
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   123
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   124
        T12CertificateMessage(HandshakeContext handshakeContext,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   125
                ByteBuffer m) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   126
            super(handshakeContext);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   127
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   128
            int listLen = Record.getInt24(m);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   129
            if (listLen > m.remaining()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   130
                handshakeContext.conContext.fatal(Alert.ILLEGAL_PARAMETER,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   131
                    "Error parsing certificate message:no sufficient data");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   132
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   133
            if (listLen > 0) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   134
                List<byte[]> encodedCerts = new LinkedList<>();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   135
                while (listLen > 0) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   136
                    byte[] encodedCert = Record.getBytes24(m);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   137
                    listLen -= (3 + encodedCert.length);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   138
                    encodedCerts.add(encodedCert);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   139
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   140
                this.encodedCertChain = encodedCerts;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   141
            } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   142
                this.encodedCertChain = Collections.emptyList();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   143
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   144
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   145
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   146
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   147
        public SSLHandshake handshakeType() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   148
            return SSLHandshake.CERTIFICATE;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   149
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   150
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   151
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   152
        public int messageLength() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   153
            int msgLen = 3;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   154
            for (byte[] encodedCert : encodedCertChain) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   155
                msgLen += (encodedCert.length + 3);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   156
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   157
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   158
            return msgLen;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   159
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   160
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   161
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   162
        public void send(HandshakeOutStream hos) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   163
            int listLen = 0;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   164
            for (byte[] encodedCert : encodedCertChain) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   165
                listLen += (encodedCert.length + 3);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   166
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   167
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   168
            hos.putInt24(listLen);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   169
            for (byte[] encodedCert : encodedCertChain) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   170
                hos.putBytes24(encodedCert);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   171
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   172
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   173
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   174
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   175
        public String toString() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   176
            if (encodedCertChain.isEmpty()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   177
                return "\"Certificates\": <empty list>";
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   178
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   179
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   180
            Object[] x509Certs = new Object[encodedCertChain.size()];
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   181
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   182
                CertificateFactory cf = CertificateFactory.getInstance("X.509");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   183
                int i = 0;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   184
                for (byte[] encodedCert : encodedCertChain) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   185
                    Object obj;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   186
                    try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   187
                        obj = (X509Certificate)cf.generateCertificate(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   188
                                    new ByteArrayInputStream(encodedCert));
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   189
                    } catch (CertificateException ce) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   190
                        obj = encodedCert;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   191
                    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   192
                    x509Certs[i++] = obj;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   193
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   194
            } catch (CertificateException ce) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   195
                // no X.509 certificate factory service
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   196
                int i = 0;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   197
                for (byte[] encodedCert : encodedCertChain) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   198
                    x509Certs[i++] = encodedCert;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   199
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   200
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   201
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   202
            MessageFormat messageFormat = new MessageFormat(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   203
                    "\"Certificates\": [\n" +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   204
                    "{0}\n" +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   205
                    "]",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   206
                    Locale.ENGLISH);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   207
            Object[] messageFields = {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   208
                SSLLogger.toString(x509Certs)
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   209
            };
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   210
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   211
            return messageFormat.format(messageFields);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   212
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   213
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   214
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   215
    /**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   216
     * The "Certificate" handshake message producer for TLS 1.2 and
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   217
     * previous SSL/TLS protocol versions.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   218
     */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   219
    private static final
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   220
            class T12CertificateProducer implements HandshakeProducer {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   221
        // Prevent instantiation of this class.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   222
        private T12CertificateProducer() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   223
            // blank
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   224
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   225
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   226
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   227
        public byte[] produce(ConnectionContext context,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   228
                HandshakeMessage message) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   229
            // The producing happens in handshake context only.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   230
            HandshakeContext hc = (HandshakeContext)context;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   231
            if (hc.sslConfig.isClientMode) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   232
                return onProduceCertificate(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   233
                        (ClientHandshakeContext)context, message);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   234
            } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   235
                return onProduceCertificate(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   236
                        (ServerHandshakeContext)context, message);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   237
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   238
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   239
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   240
        private byte[] onProduceCertificate(ServerHandshakeContext shc,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   241
                SSLHandshake.HandshakeMessage message) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   242
            X509Possession x509Possession = null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   243
            for (SSLPossession possession : shc.handshakePossessions) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   244
                if (possession instanceof X509Possession) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   245
                    x509Possession = (X509Possession)possession;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   246
                    break;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   247
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   248
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   249
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   250
            if (x509Possession == null) {       // unlikely
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   251
                shc.conContext.fatal(Alert.INTERNAL_ERROR,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   252
                    "No expected X.509 certificate for server authentication");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   253
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   254
                return null;        // make the compiler happy
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   255
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   256
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   257
            shc.handshakeSession.setLocalPrivateKey(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   258
                    x509Possession.popPrivateKey);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   259
            shc.handshakeSession.setLocalCertificates(x509Possession.popCerts);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   260
            T12CertificateMessage cm =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   261
                    new T12CertificateMessage(shc, x509Possession.popCerts);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   262
            if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   263
                SSLLogger.fine(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   264
                    "Produced server Certificate handshake message", cm);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   265
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   266
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   267
            // Output the handshake message.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   268
            cm.write(shc.handshakeOutput);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   269
            shc.handshakeOutput.flush();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   270
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   271
            // The handshake message has been delivered.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   272
            return null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   273
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   274
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   275
        private byte[] onProduceCertificate(ClientHandshakeContext chc,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   276
                SSLHandshake.HandshakeMessage message) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   277
            X509Possession x509Possession = null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   278
            for (SSLPossession possession : chc.handshakePossessions) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   279
                if (possession instanceof X509Possession) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   280
                    x509Possession = (X509Possession)possession;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   281
                    break;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   282
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   283
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   284
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   285
            // Report to the server if no appropriate cert was found.  For
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   286
            // SSL 3.0, send a no_certificate alert;  TLS 1.0/1.1/1.2 uses
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   287
            // an empty cert chain instead.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   288
            if (x509Possession == null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   289
                if (chc.negotiatedProtocol.useTLS10PlusSpec()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   290
                    if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   291
                        SSLLogger.fine(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   292
                            "No X.509 certificate for client authentication, " +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   293
                            "use empty Certificate message instead");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   294
                    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   295
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   296
                    x509Possession =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   297
                            new X509Possession(null, new X509Certificate[0]);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   298
                } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   299
                    if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   300
                        SSLLogger.fine(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   301
                            "No X.509 certificate for client authentication, " +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   302
                            "send a no_certificate alert");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   303
                    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   304
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   305
                    chc.conContext.warning(Alert.NO_CERTIFICATE);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   306
                    return null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   307
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   308
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   309
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   310
            chc.handshakeSession.setLocalPrivateKey(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   311
                    x509Possession.popPrivateKey);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   312
            if (x509Possession.popCerts != null &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   313
                    x509Possession.popCerts.length != 0) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   314
                chc.handshakeSession.setLocalCertificates(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   315
                        x509Possession.popCerts);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   316
            } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   317
                chc.handshakeSession.setLocalCertificates(null);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   318
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   319
            T12CertificateMessage cm =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   320
                    new T12CertificateMessage(chc, x509Possession.popCerts);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   321
            if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   322
                SSLLogger.fine(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   323
                    "Produced client Certificate handshake message", cm);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   324
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   325
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   326
            // Output the handshake message.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   327
            cm.write(chc.handshakeOutput);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   328
            chc.handshakeOutput.flush();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   329
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   330
            // The handshake message has been delivered.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   331
            return null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   332
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   333
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   334
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   335
    /**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   336
     * The "Certificate" handshake message consumer for TLS 1.2 and
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   337
     * previous SSL/TLS protocol versions.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   338
     */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   339
    static final
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   340
            class T12CertificateConsumer implements SSLConsumer {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   341
        // Prevent instantiation of this class.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   342
        private T12CertificateConsumer() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   343
            // blank
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   344
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   345
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   346
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   347
        public void consume(ConnectionContext context,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   348
                ByteBuffer message) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   349
            // The consuming happens in handshake context only.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   350
            HandshakeContext hc = (HandshakeContext)context;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   351
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   352
            // clean up this consumer
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   353
            hc.handshakeConsumers.remove(SSLHandshake.CERTIFICATE.id);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   354
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   355
            T12CertificateMessage cm = new T12CertificateMessage(hc, message);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   356
            if (hc.sslConfig.isClientMode) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   357
                if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   358
                    SSLLogger.fine(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   359
                        "Consuming server Certificate handshake message", cm);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   360
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   361
                onCertificate((ClientHandshakeContext)context, cm);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   362
            } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   363
                if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   364
                    SSLLogger.fine(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   365
                        "Consuming client Certificate handshake message", cm);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   366
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   367
                onCertificate((ServerHandshakeContext)context, cm);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   368
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   369
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   370
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   371
        private void onCertificate(ServerHandshakeContext shc,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   372
                T12CertificateMessage certificateMessage )throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   373
            List<byte[]> encodedCerts = certificateMessage.encodedCertChain;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   374
            if (encodedCerts == null || encodedCerts.isEmpty()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   375
                if (shc.sslConfig.clientAuthType !=
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   376
                        ClientAuthType.CLIENT_AUTH_REQUESTED) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   377
                    // unexpected or require client authentication
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   378
                    shc.conContext.fatal(Alert.BAD_CERTIFICATE,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   379
                        "Empty server certificate chain");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   380
                } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   381
                    return;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   382
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   383
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   384
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   385
            X509Certificate[] x509Certs =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   386
                    new X509Certificate[encodedCerts.size()];
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   387
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   388
                CertificateFactory cf = CertificateFactory.getInstance("X.509");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   389
                int i = 0;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   390
                for (byte[] encodedCert : encodedCerts) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   391
                    x509Certs[i++] = (X509Certificate)cf.generateCertificate(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   392
                                    new ByteArrayInputStream(encodedCert));
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   393
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   394
            } catch (CertificateException ce) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   395
                shc.conContext.fatal(Alert.BAD_CERTIFICATE,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   396
                    "Failed to parse server certificates", ce);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   397
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   398
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   399
            checkClientCerts(shc, x509Certs);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   400
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   401
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   402
            // update
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   403
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   404
            shc.handshakeCredentials.add(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   405
                new X509Credentials(x509Certs[0].getPublicKey(), x509Certs));
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   406
            shc.handshakeSession.setPeerCertificates(x509Certs);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   407
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   408
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   409
        private void onCertificate(ClientHandshakeContext chc,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   410
                T12CertificateMessage certificateMessage) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   411
            List<byte[]> encodedCerts = certificateMessage.encodedCertChain;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   412
            if (encodedCerts == null || encodedCerts.isEmpty()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   413
                chc.conContext.fatal(Alert.BAD_CERTIFICATE,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   414
                    "Empty server certificate chain");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   415
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   416
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   417
            X509Certificate[] x509Certs =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   418
                    new X509Certificate[encodedCerts.size()];
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   419
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   420
                CertificateFactory cf = CertificateFactory.getInstance("X.509");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   421
                int i = 0;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   422
                for (byte[] encodedCert : encodedCerts) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   423
                    x509Certs[i++] = (X509Certificate)cf.generateCertificate(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   424
                                    new ByteArrayInputStream(encodedCert));
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   425
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   426
            } catch (CertificateException ce) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   427
                chc.conContext.fatal(Alert.BAD_CERTIFICATE,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   428
                    "Failed to parse server certificates", ce);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   429
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   430
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   431
            // Allow server certificate change in client side during
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   432
            // renegotiation after a session-resumption abbreviated
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   433
            // initial handshake?
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   434
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   435
            // DO NOT need to check allowUnsafeServerCertChange here. We only
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   436
            // reserve server certificates when allowUnsafeServerCertChange is
56761
001e4e7c0a01 Various nits and dead code removal
ascarpino
parents: 56603
diff changeset
   437
            // false.
56559
a423173d0578 rollbak behavior changes of extended secret extension
xuelei
parents: 56542
diff changeset
   438
            if (chc.reservedServerCerts != null &&
a423173d0578 rollbak behavior changes of extended secret extension
xuelei
parents: 56542
diff changeset
   439
                    !chc.handshakeSession.useExtendedMasterSecret) {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   440
                // It is not necessary to check the certificate update if
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   441
                // endpoint identification is enabled.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   442
                String identityAlg = chc.sslConfig.identificationProtocol;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   443
                if ((identityAlg == null || identityAlg.length() == 0) &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   444
                        !isIdentityEquivalent(x509Certs[0],
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   445
                                chc.reservedServerCerts[0])) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   446
                    chc.conContext.fatal(Alert.BAD_CERTIFICATE,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   447
                            "server certificate change is restricted " +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   448
                            "during renegotiation");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   449
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   450
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   451
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   452
            // ask the trust manager to verify the chain
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   453
            if (chc.staplingActive) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   454
                // Defer the certificate check until after we've received the
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   455
                // CertificateStatus message.  If that message doesn't come in
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   456
                // immediately following this message we will execute the
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   457
                // check from CertificateStatus' absent handler.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   458
                chc.deferredCerts = x509Certs;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   459
            } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   460
                // We're not doing stapling, so perform the check right now
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   461
                checkServerCerts(chc, x509Certs);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   462
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   463
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   464
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   465
            // update
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   466
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   467
            chc.handshakeCredentials.add(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   468
                new X509Credentials(x509Certs[0].getPublicKey(), x509Certs));
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   469
            chc.handshakeSession.setPeerCertificates(x509Certs);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   470
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   471
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   472
        /*
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   473
         * Whether the certificates can represent the same identity?
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   474
         *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   475
         * The certificates can be used to represent the same identity:
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   476
         *     1. If the subject alternative names of IP address are present
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   477
         *        in both certificates, they should be identical; otherwise,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   478
         *     2. if the subject alternative names of DNS name are present in
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   479
         *        both certificates, they should be identical; otherwise,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   480
         *     3. if the subject fields are present in both certificates, the
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   481
         *        certificate subjects and issuers should be identical.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   482
         */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   483
        private static boolean isIdentityEquivalent(X509Certificate thisCert,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   484
                X509Certificate prevCert) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   485
            if (thisCert.equals(prevCert)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   486
                return true;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   487
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   488
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   489
            // check subject alternative names
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   490
            Collection<List<?>> thisSubjectAltNames = null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   491
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   492
                thisSubjectAltNames = thisCert.getSubjectAlternativeNames();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   493
            } catch (CertificateParsingException cpe) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   494
                if (SSLLogger.isOn && SSLLogger.isOn("handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   495
                    SSLLogger.fine(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   496
                        "Attempt to obtain subjectAltNames extension failed!");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   497
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   498
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   499
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   500
            Collection<List<?>> prevSubjectAltNames = null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   501
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   502
                prevSubjectAltNames = prevCert.getSubjectAlternativeNames();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   503
            } catch (CertificateParsingException cpe) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   504
                if (SSLLogger.isOn && SSLLogger.isOn("handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   505
                    SSLLogger.fine(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   506
                        "Attempt to obtain subjectAltNames extension failed!");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   507
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   508
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   509
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   510
            if (thisSubjectAltNames != null && prevSubjectAltNames != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   511
                // check the iPAddress field in subjectAltName extension
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   512
                //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   513
                // 7: subject alternative name of type IP.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   514
                Collection<String> thisSubAltIPAddrs =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   515
                            getSubjectAltNames(thisSubjectAltNames, 7);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   516
                Collection<String> prevSubAltIPAddrs =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   517
                            getSubjectAltNames(prevSubjectAltNames, 7);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   518
                if (thisSubAltIPAddrs != null && prevSubAltIPAddrs != null &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   519
                    isEquivalent(thisSubAltIPAddrs, prevSubAltIPAddrs)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   520
                    return true;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   521
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   522
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   523
                // check the dNSName field in subjectAltName extension
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   524
                // 2: subject alternative name of type IP.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   525
                Collection<String> thisSubAltDnsNames =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   526
                            getSubjectAltNames(thisSubjectAltNames, 2);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   527
                Collection<String> prevSubAltDnsNames =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   528
                            getSubjectAltNames(prevSubjectAltNames, 2);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   529
                if (thisSubAltDnsNames != null && prevSubAltDnsNames != null &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   530
                    isEquivalent(thisSubAltDnsNames, prevSubAltDnsNames)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   531
                    return true;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   532
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   533
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   534
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   535
            // check the certificate subject and issuer
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   536
            X500Principal thisSubject = thisCert.getSubjectX500Principal();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   537
            X500Principal prevSubject = prevCert.getSubjectX500Principal();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   538
            X500Principal thisIssuer = thisCert.getIssuerX500Principal();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   539
            X500Principal prevIssuer = prevCert.getIssuerX500Principal();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   540
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   541
            return (!thisSubject.getName().isEmpty() &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   542
                    !prevSubject.getName().isEmpty() &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   543
                    thisSubject.equals(prevSubject) &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   544
                    thisIssuer.equals(prevIssuer));
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   545
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   546
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   547
        /*
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   548
         * Returns the subject alternative name of the specified type in the
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   549
         * subjectAltNames extension of a certificate.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   550
         *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   551
         * Note that only those subjectAltName types that use String data
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   552
         * should be passed into this function.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   553
         */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   554
        private static Collection<String> getSubjectAltNames(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   555
                Collection<List<?>> subjectAltNames, int type) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   556
            HashSet<String> subAltDnsNames = null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   557
            for (List<?> subjectAltName : subjectAltNames) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   558
                int subjectAltNameType = (Integer)subjectAltName.get(0);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   559
                if (subjectAltNameType == type) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   560
                    String subAltDnsName = (String)subjectAltName.get(1);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   561
                    if ((subAltDnsName != null) && !subAltDnsName.isEmpty()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   562
                        if (subAltDnsNames == null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   563
                            subAltDnsNames =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   564
                                    new HashSet<>(subjectAltNames.size());
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   565
                        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   566
                        subAltDnsNames.add(subAltDnsName);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   567
                    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   568
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   569
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   570
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   571
            return subAltDnsNames;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   572
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   573
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   574
        private static boolean isEquivalent(Collection<String> thisSubAltNames,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   575
                Collection<String> prevSubAltNames) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   576
            for (String thisSubAltName : thisSubAltNames) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   577
                for (String prevSubAltName : prevSubAltNames) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   578
                    // Only allow the exactly match.  No wildcard character
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   579
                    // checking.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   580
                    if (thisSubAltName.equalsIgnoreCase(prevSubAltName)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   581
                        return true;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   582
                    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   583
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   584
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   585
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   586
            return false;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   587
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   588
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   589
        /**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   590
         * Perform client-side checking of server certificates.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   591
         *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   592
         * @param certs an array of {@code X509Certificate} objects presented
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   593
         *      by the server in the ServerCertificate message.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   594
         *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   595
         * @throws IOException if a failure occurs during validation or
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   596
         *      the trust manager associated with the {@code SSLContext} is not
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   597
         *      an {@code X509ExtendedTrustManager}.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   598
         */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   599
        static void checkServerCerts(ClientHandshakeContext chc,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   600
                X509Certificate[] certs) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   601
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   602
            X509TrustManager tm = chc.sslContext.getX509TrustManager();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   603
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   604
            // find out the key exchange algorithm used
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   605
            // use "RSA" for non-ephemeral "RSA_EXPORT"
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   606
            String keyExchangeString;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   607
            if (chc.negotiatedCipherSuite.keyExchange ==
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   608
                    CipherSuite.KeyExchange.K_RSA_EXPORT ||
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   609
                    chc.negotiatedCipherSuite.keyExchange ==
56782
b472b5917a1b typo, format and copyrigth corrections
xuelei
parents: 56761
diff changeset
   610
                            CipherSuite.KeyExchange.K_DHE_RSA_EXPORT) {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   611
                keyExchangeString = CipherSuite.KeyExchange.K_RSA.name;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   612
            } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   613
                keyExchangeString = chc.negotiatedCipherSuite.keyExchange.name;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   614
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   615
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   616
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   617
                if (tm instanceof X509ExtendedTrustManager) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   618
                    if (chc.conContext.transport instanceof SSLEngine) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   619
                        SSLEngine engine = (SSLEngine)chc.conContext.transport;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   620
                        ((X509ExtendedTrustManager)tm).checkServerTrusted(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   621
                            certs.clone(),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   622
                            keyExchangeString,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   623
                            engine);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   624
                    } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   625
                        SSLSocket socket = (SSLSocket)chc.conContext.transport;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   626
                        ((X509ExtendedTrustManager)tm).checkServerTrusted(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   627
                            certs.clone(),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   628
                            keyExchangeString,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   629
                            socket);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   630
                    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   631
                } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   632
                    // Unlikely to happen, because we have wrapped the old
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   633
                    // X509TrustManager with the new X509ExtendedTrustManager.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   634
                    throw new CertificateException(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   635
                            "Improper X509TrustManager implementation");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   636
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   637
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   638
                // Once the server certificate chain has been validated, set
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   639
                // the certificate chain in the TLS session.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   640
                chc.handshakeSession.setPeerCertificates(certs);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   641
            } catch (CertificateException ce) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   642
                chc.conContext.fatal(getCertificateAlert(chc, ce), ce);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   643
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   644
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   645
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   646
        private static void checkClientCerts(ServerHandshakeContext shc,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   647
                X509Certificate[] certs) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   648
            X509TrustManager tm = shc.sslContext.getX509TrustManager();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   649
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   650
            // find out the types of client authentication used
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   651
            PublicKey key = certs[0].getPublicKey();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   652
            String keyAlgorithm = key.getAlgorithm();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   653
            String authType;
56806
32a737f51e37 Support RSASSS-PSS for TLS 1.2
xuelei
parents: 56782
diff changeset
   654
            switch (keyAlgorithm) {
32a737f51e37 Support RSASSS-PSS for TLS 1.2
xuelei
parents: 56782
diff changeset
   655
                case "RSA":
32a737f51e37 Support RSASSS-PSS for TLS 1.2
xuelei
parents: 56782
diff changeset
   656
                case "DSA":
32a737f51e37 Support RSASSS-PSS for TLS 1.2
xuelei
parents: 56782
diff changeset
   657
                case "EC":
32a737f51e37 Support RSASSS-PSS for TLS 1.2
xuelei
parents: 56782
diff changeset
   658
                case "RSASSA-PSS":
32a737f51e37 Support RSASSS-PSS for TLS 1.2
xuelei
parents: 56782
diff changeset
   659
                    authType = keyAlgorithm;
32a737f51e37 Support RSASSS-PSS for TLS 1.2
xuelei
parents: 56782
diff changeset
   660
                    break;
32a737f51e37 Support RSASSS-PSS for TLS 1.2
xuelei
parents: 56782
diff changeset
   661
                default:
32a737f51e37 Support RSASSS-PSS for TLS 1.2
xuelei
parents: 56782
diff changeset
   662
                    // unknown public key type
32a737f51e37 Support RSASSS-PSS for TLS 1.2
xuelei
parents: 56782
diff changeset
   663
                    authType = "UNKNOWN";
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   664
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   665
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   666
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   667
                if (tm instanceof X509ExtendedTrustManager) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   668
                    if (shc.conContext.transport instanceof SSLEngine) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   669
                        SSLEngine engine = (SSLEngine)shc.conContext.transport;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   670
                        ((X509ExtendedTrustManager)tm).checkClientTrusted(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   671
                            certs.clone(),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   672
                            authType,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   673
                            engine);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   674
                    } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   675
                        SSLSocket socket = (SSLSocket)shc.conContext.transport;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   676
                        ((X509ExtendedTrustManager)tm).checkClientTrusted(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   677
                            certs.clone(),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   678
                            authType,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   679
                            socket);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   680
                    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   681
                } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   682
                    // Unlikely to happen, because we have wrapped the old
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   683
                    // X509TrustManager with the new X509ExtendedTrustManager.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   684
                    throw new CertificateException(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   685
                            "Improper X509TrustManager implementation");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   686
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   687
            } catch (CertificateException ce) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   688
                shc.conContext.fatal(Alert.CERTIFICATE_UNKNOWN, ce);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   689
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   690
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   691
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   692
        /**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   693
         * When a failure happens during certificate checking from an
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   694
         * {@link X509TrustManager}, determine what TLS alert description
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   695
         * to use.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   696
         *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   697
         * @param cexc The exception thrown by the {@link X509TrustManager}
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   698
         *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   699
         * @return A byte value corresponding to a TLS alert description number.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   700
         */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   701
        private static Alert getCertificateAlert(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   702
                ClientHandshakeContext chc, CertificateException cexc) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   703
            // The specific reason for the failure will determine how to
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   704
            // set the alert description value
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   705
            Alert alert = Alert.CERTIFICATE_UNKNOWN;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   706
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   707
            Throwable baseCause = cexc.getCause();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   708
            if (baseCause instanceof CertPathValidatorException) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   709
                CertPathValidatorException cpve =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   710
                        (CertPathValidatorException)baseCause;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   711
                Reason reason = cpve.getReason();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   712
                if (reason == BasicReason.REVOKED) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   713
                    alert = chc.staplingActive ?
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   714
                            Alert.BAD_CERT_STATUS_RESPONSE :
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   715
                            Alert.CERTIFICATE_REVOKED;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   716
                } else if (
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   717
                        reason == BasicReason.UNDETERMINED_REVOCATION_STATUS) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   718
                    alert = chc.staplingActive ?
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   719
                            Alert.BAD_CERT_STATUS_RESPONSE :
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   720
                            Alert.CERTIFICATE_UNKNOWN;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   721
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   722
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   723
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   724
            return alert;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   725
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   726
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   727
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   728
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   729
    /**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   730
     * The certificate entry used in Certificate handshake message for TLS 1.3.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   731
     */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   732
    static final class CertificateEntry {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   733
        final byte[] encoded;       // encoded cert or public key
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   734
        private final SSLExtensions extensions;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   735
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   736
        CertificateEntry(byte[] encoded, SSLExtensions extensions) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   737
            this.encoded = encoded;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   738
            this.extensions = extensions;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   739
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   740
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   741
        private int getEncodedSize() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   742
            int extLen = extensions.length();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   743
            if (extLen == 0) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   744
                extLen = 2;     // empty extensions
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   745
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   746
            return 3 + encoded.length + extLen;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   747
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   748
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   749
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   750
        public String toString() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   751
            MessageFormat messageFormat = new MessageFormat(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   752
                "\n'{'\n" +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   753
                "{0}\n" +                       // X.509 certificate
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   754
                "  \"extensions\": '{'\n" +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   755
                "{1}\n" +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   756
                "  '}'\n" +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   757
                "'}',", Locale.ENGLISH);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   758
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   759
            Object x509Certs;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   760
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   761
                // Don't support certificate type extension (RawPublicKey) yet.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   762
                CertificateFactory cf = CertificateFactory.getInstance("X.509");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   763
                x509Certs =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   764
                    cf.generateCertificate(new ByteArrayInputStream(encoded));
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   765
            } catch (CertificateException ce) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   766
                // no X.509 certificate factory service
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   767
                x509Certs = encoded;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   768
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   769
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   770
            Object[] messageFields = {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   771
                SSLLogger.toString(x509Certs),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   772
                Utilities.indent(extensions.toString(), "    ")
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   773
            };
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   774
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   775
            return messageFormat.format(messageFields);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   776
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   777
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   778
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   779
    /**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   780
     * The Certificate handshake message for TLS 1.3.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   781
     */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   782
    static final class T13CertificateMessage extends HandshakeMessage {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   783
        private final byte[] requestContext;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   784
        private final List<CertificateEntry> certEntries;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   785
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   786
        T13CertificateMessage(HandshakeContext context,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   787
                byte[] requestContext, X509Certificate[] certificates)
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   788
                throws SSLException, CertificateException  {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   789
            super(context);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   790
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   791
            this.requestContext = requestContext.clone();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   792
            this.certEntries = new LinkedList<>();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   793
            for (X509Certificate cert : certificates) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   794
                byte[] encoded = cert.getEncoded();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   795
                SSLExtensions extensions = new SSLExtensions(this);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   796
                certEntries.add(new CertificateEntry(encoded, extensions));
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   797
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   798
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   799
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   800
        T13CertificateMessage(HandshakeContext handshakeContext,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   801
                byte[] requestContext, List<CertificateEntry> certificates) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   802
            super(handshakeContext);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   803
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   804
            this.requestContext = requestContext.clone();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   805
            this.certEntries = certificates;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   806
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   807
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   808
        T13CertificateMessage(HandshakeContext handshakeContext,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   809
                ByteBuffer m) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   810
            super(handshakeContext);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   811
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   812
            // struct {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   813
            //      opaque certificate_request_context<0..2^8-1>;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   814
            //      CertificateEntry certificate_list<0..2^24-1>;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   815
            //  } Certificate;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   816
            if (m.remaining() < 4) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   817
                throw new SSLProtocolException(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   818
                        "Invalid Certificate message: " +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   819
                        "insufficient data (length=" + m.remaining() + ")");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   820
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   821
            this.requestContext = Record.getBytes8(m);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   822
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   823
            if (m.remaining() < 3) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   824
                throw new SSLProtocolException(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   825
                        "Invalid Certificate message: " +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   826
                        "insufficient certificate entries data (length=" +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   827
                        m.remaining() + ")");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   828
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   829
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   830
            int listLen = Record.getInt24(m);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   831
            if (listLen != m.remaining()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   832
                throw new SSLProtocolException(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   833
                    "Invalid Certificate message: " +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   834
                    "incorrect list length (length=" + listLen + ")");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   835
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   836
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   837
            SSLExtension[] enabledExtensions =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   838
                handshakeContext.sslConfig.getEnabledExtensions(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   839
                        SSLHandshake.CERTIFICATE);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   840
            List<CertificateEntry> certList = new LinkedList<>();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   841
            while (m.hasRemaining()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   842
                // Note: support only X509 CertificateType right now.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   843
                byte[] encodedCert = Record.getBytes24(m);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   844
                if (encodedCert.length == 0) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   845
                    throw new SSLProtocolException(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   846
                        "Invalid Certificate message: empty cert_data");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   847
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   848
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   849
                SSLExtensions extensions =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   850
                        new SSLExtensions(this, m, enabledExtensions);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   851
                certList.add(new CertificateEntry(encodedCert, extensions));
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   852
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   853
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   854
            this.certEntries = Collections.unmodifiableList(certList);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   855
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   856
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   857
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   858
        public SSLHandshake handshakeType() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   859
            return SSLHandshake.CERTIFICATE;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   860
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   861
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   862
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   863
        public int messageLength() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   864
            int msgLen = 4 + requestContext.length;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   865
            for (CertificateEntry entry : certEntries) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   866
                msgLen += entry.getEncodedSize();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   867
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   868
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   869
            return msgLen;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   870
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   871
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   872
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   873
        public void send(HandshakeOutStream hos) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   874
            int entryListLen = 0;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   875
            for (CertificateEntry entry : certEntries) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   876
                entryListLen += entry.getEncodedSize();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   877
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   878
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   879
            hos.putBytes8(requestContext);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   880
            hos.putInt24(entryListLen);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   881
            for (CertificateEntry entry : certEntries) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   882
                hos.putBytes24(entry.encoded);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   883
                // Is it an empty extensions?
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   884
                if (entry.extensions.length() == 0) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   885
                    hos.putInt16(0);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   886
                } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   887
                    entry.extensions.send(hos);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   888
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   889
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   890
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   891
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   892
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   893
        public String toString() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   894
            MessageFormat messageFormat = new MessageFormat(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   895
                "\"Certificate\": '{'\n" +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   896
                "  \"certificate_request_context\": \"{0}\",\n" +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   897
                "  \"certificate_list\": [{1}\n]\n" +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   898
                "'}'",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   899
                Locale.ENGLISH);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   900
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   901
            StringBuilder builder = new StringBuilder(512);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   902
            for (CertificateEntry entry : certEntries) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   903
                builder.append(entry.toString());
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   904
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   905
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   906
            Object[] messageFields = {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   907
                Utilities.toHexString(requestContext),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   908
                Utilities.indent(builder.toString())
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   909
            };
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   910
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   911
            return messageFormat.format(messageFields);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   912
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   913
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   914
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   915
    /**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   916
     * The "Certificate" handshake message producer for TLS 1.3.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   917
     */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   918
    private static final
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   919
            class T13CertificateProducer implements HandshakeProducer {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   920
        // Prevent instantiation of this class.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   921
        private T13CertificateProducer() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   922
            // blank
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   923
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   924
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   925
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   926
        public byte[] produce(ConnectionContext context,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   927
                HandshakeMessage message) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   928
            // The producing happens in handshake context only.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   929
            HandshakeContext hc = (HandshakeContext)context;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   930
            if (hc.sslConfig.isClientMode) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   931
                return onProduceCertificate(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   932
                        (ClientHandshakeContext)context, message);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   933
            } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   934
                return onProduceCertificate(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   935
                        (ServerHandshakeContext)context, message);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   936
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   937
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   938
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   939
        private byte[] onProduceCertificate(ServerHandshakeContext shc,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   940
                HandshakeMessage message) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   941
            ClientHelloMessage clientHello = (ClientHelloMessage)message;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   942
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   943
            SSLPossession pos = choosePossession(shc, clientHello);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   944
            if (pos == null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   945
                shc.conContext.fatal(Alert.HANDSHAKE_FAILURE,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   946
                        "No available authentication scheme");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   947
                return null;    // make the complier happy
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   948
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   949
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   950
            if (!(pos instanceof X509Possession)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   951
                shc.conContext.fatal(Alert.HANDSHAKE_FAILURE,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   952
                        "No X.509 certificate for server authentication");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   953
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   954
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   955
            X509Possession x509Possession = (X509Possession)pos;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   956
            X509Certificate[] localCerts = x509Possession.popCerts;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   957
            if (localCerts == null || localCerts.length == 0) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   958
                shc.conContext.fatal(Alert.HANDSHAKE_FAILURE,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   959
                        "No X.509 certificate for server authentication");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   960
                return null;    // make the complier happy
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   961
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   962
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   963
            // update the context
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   964
            shc.handshakePossessions.add(x509Possession);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   965
            shc.handshakeSession.setLocalPrivateKey(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   966
                    x509Possession.popPrivateKey);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   967
            shc.handshakeSession.setLocalCertificates(localCerts);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   968
            T13CertificateMessage cm;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   969
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   970
                cm = new T13CertificateMessage(shc, (new byte[0]), localCerts);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   971
            } catch (SSLException | CertificateException ce) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   972
                shc.conContext.fatal(Alert.HANDSHAKE_FAILURE,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   973
                        "Failed to produce server Certificate message", ce);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   974
                return null;    // make the complier happy
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   975
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   976
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   977
            // Check the OCSP stapling extensions and attempt
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   978
            // to get responses.  If the resulting stapleParams is non
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   979
            // null, it implies that stapling is enabled on the server side.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   980
            shc.stapleParams = StatusResponseManager.processStapling(shc);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   981
            shc.staplingActive = (shc.stapleParams != null);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   982
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   983
            // Process extensions for each CertificateEntry.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   984
            // Since there can be multiple CertificateEntries within a
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   985
            // single CT message, we will pin a specific CertificateEntry
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   986
            // into the ServerHandshakeContext so individual extension
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   987
            // producers know which X509Certificate it is processing in
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   988
            // each call.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   989
            SSLExtension[] enabledCTExts = shc.sslConfig.getEnabledExtensions(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   990
                    SSLHandshake.CERTIFICATE,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   991
                    Arrays.asList(ProtocolVersion.PROTOCOLS_OF_13));
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   992
            for (CertificateEntry certEnt : cm.certEntries) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   993
                shc.currentCertEntry = certEnt;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   994
                certEnt.extensions.produce(shc, enabledCTExts);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   995
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   996
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   997
            if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   998
                SSLLogger.fine("Produced server Certificate message", cm);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   999
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1000
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1001
            // Output the handshake message.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1002
            cm.write(shc.handshakeOutput);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1003
            shc.handshakeOutput.flush();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1004
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1005
            // The handshake message has been delivered.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1006
            return null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1007
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1008
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1009
        private static SSLPossession choosePossession(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1010
                HandshakeContext hc,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1011
                ClientHelloMessage clientHello) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1012
            if (hc.peerRequestedCertSignSchemes == null ||
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1013
                    hc.peerRequestedCertSignSchemes.isEmpty()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1014
                if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1015
                    SSLLogger.warning(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1016
                            "No signature_algorithms(_cert) in ClientHello");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1017
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1018
                return null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1019
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1020
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1021
            Collection<String> checkedKeyTypes = new HashSet<>();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1022
            for (SignatureScheme ss : hc.peerRequestedCertSignSchemes) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1023
                if (checkedKeyTypes.contains(ss.keyAlgorithm)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1024
                    if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1025
                        SSLLogger.warning(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1026
                            "Unsupported authentication scheme: " + ss.name);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1027
                    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1028
                    continue;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1029
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1030
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1031
                // Don't select a signature scheme unless we will be able to
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1032
                // produce a CertificateVerify message later
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1033
                if (SignatureScheme.getPreferableAlgorithm(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1034
                    hc.peerRequestedSignatureSchemes,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1035
                    ss, hc.negotiatedProtocol) == null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1036
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1037
                    if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1038
                        SSLLogger.warning(
56806
32a737f51e37 Support RSASSS-PSS for TLS 1.2
xuelei
parents: 56782
diff changeset
  1039
                            "Unable to produce CertificateVerify for " +
32a737f51e37 Support RSASSS-PSS for TLS 1.2
xuelei
parents: 56782
diff changeset
  1040
                            "signature scheme: " + ss.name);
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1041
                    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1042
                    checkedKeyTypes.add(ss.keyAlgorithm);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1043
                    continue;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1044
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1045
56806
32a737f51e37 Support RSASSS-PSS for TLS 1.2
xuelei
parents: 56782
diff changeset
  1046
                SSLAuthentication ka = X509Authentication.valueOf(ss);
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1047
                if (ka == null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1048
                    if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1049
                        SSLLogger.warning(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1050
                            "Unsupported authentication scheme: " + ss.name);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1051
                    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1052
                    checkedKeyTypes.add(ss.keyAlgorithm);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1053
                    continue;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1054
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1055
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1056
                SSLPossession pos = ka.createPossession(hc);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1057
                if (pos == null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1058
                    if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1059
                        SSLLogger.warning(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1060
                            "Unavailable authentication scheme: " + ss.name);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1061
                    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1062
                    continue;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1063
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1064
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1065
                return pos;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1066
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1067
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1068
            if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1069
                SSLLogger.warning("No available authentication scheme");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1070
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1071
            return null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1072
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1073
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1074
        private byte[] onProduceCertificate(ClientHandshakeContext chc,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1075
                HandshakeMessage message) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1076
            ClientHelloMessage clientHello = (ClientHelloMessage)message;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1077
            SSLPossession pos = choosePossession(chc, clientHello);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1078
            X509Certificate[] localCerts;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1079
            if (pos == null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1080
                if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1081
                    SSLLogger.fine("No available client authentication scheme");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1082
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1083
                localCerts = new X509Certificate[0];
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1084
            } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1085
                chc.handshakePossessions.add(pos);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1086
                if (!(pos instanceof X509Possession)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1087
                    if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1088
                        SSLLogger.fine(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1089
                            "No X.509 certificate for client authentication");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1090
                    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1091
                    localCerts = new X509Certificate[0];
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1092
                } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1093
                    X509Possession x509Possession = (X509Possession)pos;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1094
                    localCerts = x509Possession.popCerts;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1095
                    chc.handshakeSession.setLocalPrivateKey(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1096
                            x509Possession.popPrivateKey);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1097
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1098
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1099
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1100
            if (localCerts != null && localCerts.length != 0) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1101
                chc.handshakeSession.setLocalCertificates(localCerts);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1102
            } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1103
                chc.handshakeSession.setLocalCertificates(null);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1104
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1105
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1106
            T13CertificateMessage cm;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1107
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1108
                cm = new T13CertificateMessage(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1109
                        chc, chc.certRequestContext, localCerts);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1110
            } catch (SSLException | CertificateException ce) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1111
                chc.conContext.fatal(Alert.HANDSHAKE_FAILURE,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1112
                        "Failed to produce client Certificate message", ce);
56761
001e4e7c0a01 Various nits and dead code removal
ascarpino
parents: 56603
diff changeset
  1113
                return null;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1114
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1115
            if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1116
                SSLLogger.fine("Produced client Certificate message", cm);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1117
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1118
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1119
            // Output the handshake message.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1120
            cm.write(chc.handshakeOutput);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1121
            chc.handshakeOutput.flush();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1122
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1123
            // The handshake message has been delivered.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1124
            return null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1125
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1126
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1127
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1128
    /**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1129
     * The "Certificate" handshake message consumer for TLS 1.3.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1130
     */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1131
    private static final class T13CertificateConsumer implements SSLConsumer {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1132
        // Prevent instantiation of this class.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1133
        private T13CertificateConsumer() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1134
            // blank
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1135
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1136
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1137
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1138
        public void consume(ConnectionContext context,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1139
                ByteBuffer message) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1140
            // The consuming happens in handshake context only.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1141
            HandshakeContext hc = (HandshakeContext)context;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1142
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1143
            // clean up this consumer
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1144
            hc.handshakeConsumers.remove(SSLHandshake.CERTIFICATE.id);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1145
            T13CertificateMessage cm = new T13CertificateMessage(hc, message);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1146
            if (hc.sslConfig.isClientMode) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1147
                if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1148
                    SSLLogger.fine(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1149
                        "Consuming server Certificate handshake message", cm);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1150
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1151
                onConsumeCertificate((ClientHandshakeContext)context, cm);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1152
            } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1153
                if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1154
                    SSLLogger.fine(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1155
                        "Consuming client Certificate handshake message", cm);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1156
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1157
                onConsumeCertificate((ServerHandshakeContext)context, cm);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1158
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1159
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1160
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1161
        private void onConsumeCertificate(ServerHandshakeContext shc,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1162
                T13CertificateMessage certificateMessage )throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1163
            if (certificateMessage.certEntries == null ||
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1164
                    certificateMessage.certEntries.isEmpty()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1165
                if (shc.sslConfig.clientAuthType == CLIENT_AUTH_REQUIRED) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1166
                    shc.conContext.fatal(Alert.BAD_CERTIFICATE,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1167
                        "Empty client certificate chain");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1168
                } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1169
                    // optional client authentication
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1170
                    return;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1171
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1172
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1173
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1174
            // check client certificate entries
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1175
            X509Certificate[] cliCerts =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1176
                    checkClientCerts(shc, certificateMessage.certEntries);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1177
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1178
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1179
            // update
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1180
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1181
            shc.handshakeCredentials.add(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1182
                new X509Credentials(cliCerts[0].getPublicKey(), cliCerts));
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1183
            shc.handshakeSession.setPeerCertificates(cliCerts);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1184
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1185
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1186
        private void onConsumeCertificate(ClientHandshakeContext chc,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1187
                T13CertificateMessage certificateMessage )throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1188
            if (certificateMessage.certEntries == null ||
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1189
                    certificateMessage.certEntries.isEmpty()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1190
                chc.conContext.fatal(Alert.BAD_CERTIFICATE,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1191
                    "Empty server certificate chain");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1192
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1193
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1194
            // Each CertificateEntry will have its own set of extensions
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1195
            // which must be consumed.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1196
            SSLExtension[] enabledExtensions =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1197
                chc.sslConfig.getEnabledExtensions(SSLHandshake.CERTIFICATE);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1198
            for (CertificateEntry certEnt : certificateMessage.certEntries) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1199
                certEnt.extensions.consumeOnLoad(chc, enabledExtensions);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1200
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1201
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1202
            // check server certificate entries
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1203
            X509Certificate[] srvCerts =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1204
                    checkServerCerts(chc, certificateMessage.certEntries);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1205
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1206
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1207
            // update
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1208
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1209
            chc.handshakeCredentials.add(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1210
                new X509Credentials(srvCerts[0].getPublicKey(), srvCerts));
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1211
            chc.handshakeSession.setPeerCertificates(srvCerts);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1212
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1213
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1214
        private static X509Certificate[] checkClientCerts(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1215
                ServerHandshakeContext shc,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1216
                List<CertificateEntry> certEntries) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1217
            X509Certificate[] certs =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1218
                    new X509Certificate[certEntries.size()];
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1219
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1220
                CertificateFactory cf = CertificateFactory.getInstance("X.509");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1221
                int i = 0;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1222
                for (CertificateEntry entry : certEntries) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1223
                    certs[i++] = (X509Certificate)cf.generateCertificate(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1224
                                    new ByteArrayInputStream(entry.encoded));
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1225
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1226
            } catch (CertificateException ce) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1227
                shc.conContext.fatal(Alert.BAD_CERTIFICATE,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1228
                    "Failed to parse server certificates", ce);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1229
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1230
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1231
            // find out the types of client authentication used
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1232
            String keyAlgorithm = certs[0].getPublicKey().getAlgorithm();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1233
            String authType;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1234
            switch (keyAlgorithm) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1235
                case "RSA":
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1236
                case "DSA":
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1237
                case "EC":
56806
32a737f51e37 Support RSASSS-PSS for TLS 1.2
xuelei
parents: 56782
diff changeset
  1238
                case "RSASSA-PSS":
32a737f51e37 Support RSASSS-PSS for TLS 1.2
xuelei
parents: 56782
diff changeset
  1239
                    authType = keyAlgorithm;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1240
                    break;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1241
                default:
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1242
                    // unknown public key type
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1243
                    authType = "UNKNOWN";
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1244
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1245
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1246
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1247
                X509TrustManager tm = shc.sslContext.getX509TrustManager();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1248
                if (tm instanceof X509ExtendedTrustManager) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1249
                    if (shc.conContext.transport instanceof SSLEngine) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1250
                        SSLEngine engine = (SSLEngine)shc.conContext.transport;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1251
                        ((X509ExtendedTrustManager)tm).checkClientTrusted(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1252
                            certs.clone(),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1253
                            authType,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1254
                            engine);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1255
                    } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1256
                        SSLSocket socket = (SSLSocket)shc.conContext.transport;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1257
                        ((X509ExtendedTrustManager)tm).checkClientTrusted(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1258
                            certs.clone(),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1259
                            authType,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1260
                            socket);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1261
                    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1262
                } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1263
                    // Unlikely to happen, because we have wrapped the old
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1264
                    // X509TrustManager with the new X509ExtendedTrustManager.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1265
                    throw new CertificateException(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1266
                            "Improper X509TrustManager implementation");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1267
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1268
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1269
                // Once the client certificate chain has been validated, set
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1270
                // the certificate chain in the TLS session.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1271
                shc.handshakeSession.setPeerCertificates(certs);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1272
            } catch (CertificateException ce) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1273
                shc.conContext.fatal(Alert.CERTIFICATE_UNKNOWN, ce);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1274
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1275
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1276
            return certs;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1277
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1278
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1279
        private static X509Certificate[] checkServerCerts(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1280
                ClientHandshakeContext chc,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1281
                List<CertificateEntry> certEntries) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1282
            X509Certificate[] certs =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1283
                    new X509Certificate[certEntries.size()];
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1284
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1285
                CertificateFactory cf = CertificateFactory.getInstance("X.509");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1286
                int i = 0;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1287
                for (CertificateEntry entry : certEntries) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1288
                    certs[i++] = (X509Certificate)cf.generateCertificate(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1289
                                    new ByteArrayInputStream(entry.encoded));
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1290
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1291
            } catch (CertificateException ce) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1292
                chc.conContext.fatal(Alert.BAD_CERTIFICATE,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1293
                    "Failed to parse server certificates", ce);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1294
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1295
56806
32a737f51e37 Support RSASSS-PSS for TLS 1.2
xuelei
parents: 56782
diff changeset
  1296
            // find out the types of server authentication used
32a737f51e37 Support RSASSS-PSS for TLS 1.2
xuelei
parents: 56782
diff changeset
  1297
            //
32a737f51e37 Support RSASSS-PSS for TLS 1.2
xuelei
parents: 56782
diff changeset
  1298
            // Note that the "UNKNOWN" authentication type is sufficient to
32a737f51e37 Support RSASSS-PSS for TLS 1.2
xuelei
parents: 56782
diff changeset
  1299
            // check the required digitalSignature KeyUsage for TLS 1.3.
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1300
            String authType = "UNKNOWN";
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1301
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1302
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1303
                X509TrustManager tm = chc.sslContext.getX509TrustManager();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1304
                if (tm instanceof X509ExtendedTrustManager) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1305
                    if (chc.conContext.transport instanceof SSLEngine) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1306
                        SSLEngine engine = (SSLEngine)chc.conContext.transport;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1307
                        ((X509ExtendedTrustManager)tm).checkServerTrusted(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1308
                            certs.clone(),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1309
                            authType,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1310
                            engine);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1311
                    } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1312
                        SSLSocket socket = (SSLSocket)chc.conContext.transport;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1313
                        ((X509ExtendedTrustManager)tm).checkServerTrusted(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1314
                            certs.clone(),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1315
                            authType,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1316
                            socket);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1317
                    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1318
                } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1319
                    // Unlikely to happen, because we have wrapped the old
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1320
                    // X509TrustManager with the new X509ExtendedTrustManager.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1321
                    throw new CertificateException(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1322
                            "Improper X509TrustManager implementation");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1323
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1324
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1325
                // Once the server certificate chain has been validated, set
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1326
                // the certificate chain in the TLS session.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1327
                chc.handshakeSession.setPeerCertificates(certs);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1328
            } catch (CertificateException ce) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1329
                chc.conContext.fatal(getCertificateAlert(chc, ce), ce);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1330
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1331
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1332
            return certs;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1333
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1334
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1335
        /**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1336
         * When a failure happens during certificate checking from an
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1337
         * {@link X509TrustManager}, determine what TLS alert description
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1338
         * to use.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1339
         *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1340
         * @param cexc The exception thrown by the {@link X509TrustManager}
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1341
         *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1342
         * @return A byte value corresponding to a TLS alert description number.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1343
         */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1344
        private static Alert getCertificateAlert(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1345
                ClientHandshakeContext chc, CertificateException cexc) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1346
            // The specific reason for the failure will determine how to
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1347
            // set the alert description value
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1348
            Alert alert = Alert.CERTIFICATE_UNKNOWN;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1349
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1350
            Throwable baseCause = cexc.getCause();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1351
            if (baseCause instanceof CertPathValidatorException) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1352
                CertPathValidatorException cpve =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1353
                        (CertPathValidatorException)baseCause;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1354
                Reason reason = cpve.getReason();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1355
                if (reason == BasicReason.REVOKED) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1356
                    alert = chc.staplingActive ?
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1357
                            Alert.BAD_CERT_STATUS_RESPONSE :
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1358
                            Alert.CERTIFICATE_REVOKED;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1359
                } else if (
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1360
                        reason == BasicReason.UNDETERMINED_REVOCATION_STATUS) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1361
                    alert = chc.staplingActive ?
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1362
                            Alert.BAD_CERT_STATUS_RESPONSE :
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1363
                            Alert.CERTIFICATE_UNKNOWN;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1364
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1365
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1366
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1367
            return alert;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1368
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1369
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1370
}