src/java.base/share/classes/sun/security/ssl/ClientHello.java
author wetmore
Fri, 11 May 2018 15:53:12 -0700
branchJDK-8145252-TLS13-branch
changeset 56542 56aaa6cb3693
child 56608 34f33526b9a5
permissions -rw-r--r--
Initial TLSv1.3 Implementation
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.IOException;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    29
import java.nio.ByteBuffer;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    30
import java.security.SecureRandom;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    31
import java.security.cert.X509Certificate;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    32
import java.text.MessageFormat;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    33
import java.util.Arrays;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    34
import java.util.Collections;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    35
import java.util.LinkedList;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    36
import java.util.List;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    37
import java.util.Locale;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    38
import javax.net.ssl.SSLException;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    39
import javax.net.ssl.SSLHandshakeException;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    40
import javax.net.ssl.SSLPeerUnverifiedException;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    41
import javax.net.ssl.SSLProtocolException;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    42
import static sun.security.ssl.ClientAuthType.CLIENT_AUTH_REQUIRED;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    43
import sun.security.ssl.SSLHandshake.HandshakeMessage;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    44
import sun.security.ssl.SupportedVersionsExtension.CHSupportedVersionsSpec;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    45
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    46
/**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    47
 * Pack of the ClientHello handshake message.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    48
 */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    49
final class ClientHello {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    50
    static final SSLProducer kickstartProducer =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    51
        new ClientHelloKickstartProducer();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    52
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    53
    static final SSLConsumer handshakeConsumer =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    54
        new ClientHelloConsumer();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    55
    static final HandshakeProducer handshakeProducer =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    56
        new ClientHelloProducer();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    57
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    58
    private static final HandshakeConsumer t12HandshakeConsumer =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    59
            new T12ClientHelloConsumer();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    60
    private static final HandshakeConsumer t13HandshakeConsumer =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    61
            new T13ClientHelloConsumer();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    62
    private static final HandshakeConsumer d12HandshakeConsumer =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    63
            new D12ClientHelloConsumer();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    64
    private static final HandshakeConsumer d13HandshakeConsumer =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    65
            new D13ClientHelloConsumer();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    66
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    67
    /**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    68
     * The ClientHello handshake message.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    69
     *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    70
     * See RFC 5264/4346/2246/6347 for the specifications.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    71
     */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    72
    static final class ClientHelloMessage extends HandshakeMessage {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    73
        private final boolean       isDTLS;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    74
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    75
        final int                   clientVersion;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    76
        final RandomCookie          clientRandom;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    77
        final SessionId             sessionId;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    78
        private byte[]              cookie;         // DTLS only
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    79
        final int[]                 cipherSuiteIds;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    80
        final List<CipherSuite>     cipherSuites;   // known cipher suites only
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    81
        final byte[]                compressionMethod;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    82
        final SSLExtensions         extensions;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    83
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    84
        private static final byte[]  NULL_COMPRESSION = new byte[] {0};
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    85
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    86
        ClientHelloMessage(HandshakeContext handshakeContext,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    87
                int clientVersion, SessionId sessionId,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    88
                List<CipherSuite> cipherSuites, SecureRandom generator) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    89
            super(handshakeContext);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    90
            this.isDTLS = handshakeContext.sslContext.isDTLS();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    91
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    92
            this.clientVersion = clientVersion;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    93
            this.clientRandom = new RandomCookie(generator);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    94
            this.sessionId = sessionId;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    95
            if (isDTLS) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    96
                this.cookie = new byte[0];
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    97
            } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    98
                this.cookie = null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    99
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   100
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   101
            this.cipherSuites = cipherSuites;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   102
            this.cipherSuiteIds = getCipherSuiteIds(cipherSuites);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   103
            this.extensions = new SSLExtensions(this);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   104
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   105
            // Don't support compression.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   106
            this.compressionMethod = NULL_COMPRESSION;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   107
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   108
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   109
        /* Read up to the binders in the PSK extension. After this method
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   110
         * returns, the ByteBuffer position will be at end of the message
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   111
         * fragment that should be hashed to produce the PSK binder values.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   112
         * The client of this method can use this position to determine the
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   113
         * message fragment and produce the binder values.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   114
         */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   115
        static void readPartial(TransportContext tc,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   116
                ByteBuffer m) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   117
            boolean isDTLS = tc.sslContext.isDTLS();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   118
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   119
            // version
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   120
            Record.getInt16(m);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   121
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   122
            new RandomCookie(m);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   123
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   124
            // session ID
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   125
            Record.getBytes8(m);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   126
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   127
            // DTLS cookie
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   128
            if (isDTLS) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   129
                Record.getBytes8(m);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   130
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   131
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   132
            // cipher suite IDs
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   133
            Record.getBytes16(m);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   134
            // compression method
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   135
            Record.getBytes8(m);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   136
            // read extensions, if present
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   137
            if (m.remaining() >= 2) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   138
                int remaining = Record.getInt16(m);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   139
                while (remaining > 0) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   140
                    int id = Record.getInt16(m);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   141
                    int extLen = Record.getInt16(m);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   142
                    remaining -= extLen + 4;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   143
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   144
                    if (id == SSLExtension.CH_PRE_SHARED_KEY.id) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   145
                        // ensure pre_shared_key is the last extension
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   146
                        if (remaining > 0) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   147
                            tc.fatal(Alert.ILLEGAL_PARAMETER,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   148
                            "pre_shared_key extension is not last");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   149
                        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   150
                        // read only up to the IDs
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   151
                        Record.getBytes16(m);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   152
                        return;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   153
                    } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   154
                        m.position(m.position() + extLen);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   155
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
            }   // Otherwise, ignore the remaining bytes.
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
        ClientHelloMessage(HandshakeContext handshakeContext, ByteBuffer m,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   162
                SSLExtension[] supportedExtensions) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   163
            super(handshakeContext);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   164
            this.isDTLS = handshakeContext.sslContext.isDTLS();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   165
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   166
            this.clientVersion = ((m.get() & 0xFF) << 8) | (m.get() & 0xFF);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   167
            this.clientRandom = new RandomCookie(m);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   168
            this.sessionId = new SessionId(Record.getBytes8(m));
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   169
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   170
                sessionId.checkLength(clientVersion);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   171
            } catch (SSLProtocolException ex) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   172
                handshakeContext.conContext.fatal(Alert.ILLEGAL_PARAMETER, ex);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   173
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   174
            if (isDTLS) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   175
                this.cookie = Record.getBytes8(m);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   176
            } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   177
                this.cookie = null;
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
            byte[] encodedIds = Record.getBytes16(m);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   181
            if (encodedIds.length == 0 || (encodedIds.length & 0x01) != 0) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   182
                handshakeContext.conContext.fatal(Alert.ILLEGAL_PARAMETER,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   183
                    "Invalid ClientHello message");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   184
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   185
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   186
            this.cipherSuiteIds = new int[encodedIds.length >> 1];
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   187
            for (int i = 0, j = 0; i < encodedIds.length; i++, j++) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   188
                cipherSuiteIds[j] =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   189
                    ((encodedIds[i++] & 0xFF) << 8) | (encodedIds[i] & 0xFF);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   190
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   191
            this.cipherSuites = getCipherSuites(cipherSuiteIds);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   192
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   193
            this.compressionMethod = Record.getBytes8(m);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   194
              // In TLS 1.3, use of certain extensions is mandatory.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   195
            if (m.hasRemaining()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   196
                this.extensions =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   197
                        new SSLExtensions(this, m, supportedExtensions);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   198
            } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   199
                this.extensions = new SSLExtensions(this);
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
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   203
        void setHelloCookie(byte[] cookie) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   204
            this.cookie = cookie;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   205
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   206
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   207
        // DTLS 1.0/1.2, for cookie generation.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   208
        byte[] getHelloCookieBytes() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   209
            HandshakeOutStream hos = new HandshakeOutStream(null);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   210
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   211
                // copied from send() method
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   212
                hos.putInt8((byte)((clientVersion >>> 8) & 0xFF));
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   213
                hos.putInt8((byte)(clientVersion & 0xFF));
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   214
                hos.write(clientRandom.randomBytes, 0, 32);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   215
                hos.putBytes8(sessionId.getId());
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   216
                // ignore cookie
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   217
                hos.putBytes16(getEncodedCipherSuites());
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   218
                hos.putBytes8(compressionMethod);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   219
                extensions.send(hos);       // In TLS 1.3, use of certain
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   220
                                            // extensions is mandatory.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   221
            } catch (IOException ioe) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   222
                // unlikely
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   223
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   224
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   225
            return hos.toByteArray();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   226
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   227
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   228
        // (D)TLS 1.3, for cookie generation.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   229
        byte[] getHeaderBytes() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   230
            HandshakeOutStream hos = new HandshakeOutStream(null);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   231
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   232
                // copied from send() method
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   233
                hos.putInt8((byte)((clientVersion >>> 8) & 0xFF));
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   234
                hos.putInt8((byte)(clientVersion & 0xFF));
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   235
                hos.write(clientRandom.randomBytes, 0, 32);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   236
                hos.putBytes8(sessionId.getId());
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   237
                hos.putBytes16(getEncodedCipherSuites());
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   238
                hos.putBytes8(compressionMethod);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   239
            } catch (IOException ioe) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   240
                // unlikely
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   241
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   242
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   243
            return hos.toByteArray();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   244
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   245
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   246
        private static int[] getCipherSuiteIds(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   247
                List<CipherSuite> cipherSuites) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   248
            if (cipherSuites != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   249
                int[] ids = new int[cipherSuites.size()];
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   250
                int i = 0;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   251
                for (CipherSuite cipherSuite : cipherSuites) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   252
                    ids[i++] = cipherSuite.id;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   253
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   254
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   255
                return ids;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   256
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   257
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   258
            return new int[0];
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   259
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   260
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   261
        private static List<CipherSuite> getCipherSuites(int[] ids) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   262
            List<CipherSuite> cipherSuites = new LinkedList<>();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   263
            for (int id : ids) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   264
                CipherSuite cipherSuite = CipherSuite.valueOf(id);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   265
                if (cipherSuite != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   266
                    cipherSuites.add(cipherSuite);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   267
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   268
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   269
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   270
            return Collections.unmodifiableList(cipherSuites);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   271
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   272
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   273
        private List<String> getCipherSuiteNames() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   274
            List<String> names = new LinkedList<>();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   275
            for (int id : cipherSuiteIds) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   276
                names.add(CipherSuite.nameOf(id) +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   277
                        "(" + Utilities.byte16HexString(id) + ")");            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   278
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   279
            return names;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   280
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   281
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   282
        private byte[] getEncodedCipherSuites() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   283
            byte[] encoded = new byte[cipherSuiteIds.length << 1];
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   284
            int i = 0;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   285
            for (int id : cipherSuiteIds) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   286
                encoded[i++] = (byte)(id >> 8);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   287
                encoded[i++] = (byte)id;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   288
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   289
            return encoded;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   290
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   291
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   292
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   293
        public SSLHandshake handshakeType() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   294
            return SSLHandshake.CLIENT_HELLO;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   295
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   296
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   297
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   298
        public int messageLength() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   299
            /*
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   300
             * Add fixed size parts of each field...
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   301
             * version + random + session + cipher + compress
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   302
             */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   303
            return (2 + 32 + 1 + 2 + 1
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   304
                + sessionId.length()        /* ... + variable parts */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   305
                + (isDTLS ? (1 + cookie.length) : 0)
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   306
                + (cipherSuiteIds.length * 2)
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   307
                + compressionMethod.length)
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   308
                + extensions.length();      // In TLS 1.3, use of certain
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   309
                                            // extensions is mandatory.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   310
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   311
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   312
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   313
        public void send(HandshakeOutStream hos) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   314
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   315
            sendCore(hos);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   316
            extensions.send(hos);       // In TLS 1.3, use of certain
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   317
                                        // extensions is mandatory.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   318
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   319
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   320
        void sendCore(HandshakeOutStream hos) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   321
            hos.putInt8((byte) (clientVersion >>> 8));
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   322
            hos.putInt8((byte) clientVersion);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   323
            hos.write(clientRandom.randomBytes, 0, 32);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   324
            hos.putBytes8(sessionId.getId());
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   325
            if (isDTLS) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   326
                hos.putBytes8(cookie);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   327
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   328
            hos.putBytes16(getEncodedCipherSuites());
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   329
            hos.putBytes8(compressionMethod);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   330
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   331
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   332
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   333
        public String toString() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   334
            if (isDTLS) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   335
                MessageFormat messageFormat = new MessageFormat(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   336
                    "\"ClientHello\": '{'\n" +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   337
                    "  \"client version\"      : \"{0}\",\n" +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   338
                    "  \"random\"              : \"{1}\",\n" +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   339
                    "  \"session id\"          : \"{2}\",\n" +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   340
                    "  \"cookie\"              : \"{3}\",\n" +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   341
                    "  \"cipher suites\"       : \"{4}\",\n" +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   342
                    "  \"compression methods\" : \"{5}\",\n" +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   343
                    "  \"extensions\"          : [\n" +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   344
                    "{6}\n" +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   345
                    "  ]\n" +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   346
                    "'}'",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   347
                    Locale.ENGLISH);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   348
                Object[] messageFields = {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   349
                    ProtocolVersion.nameOf(clientVersion),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   350
                    Utilities.toHexString(clientRandom.randomBytes),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   351
                    sessionId.toString(),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   352
                    Utilities.toHexString(cookie),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   353
                    getCipherSuiteNames().toString(),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   354
                    Utilities.toHexString(compressionMethod),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   355
                    Utilities.indent(Utilities.indent(extensions.toString()))
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   356
                };
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   357
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   358
                return messageFormat.format(messageFields);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   359
            } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   360
                MessageFormat messageFormat = new MessageFormat(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   361
                    "\"ClientHello\": '{'\n" +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   362
                    "  \"client version\"      : \"{0}\",\n" +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   363
                    "  \"random\"              : \"{1}\",\n" +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   364
                    "  \"session id\"          : \"{2}\",\n" +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   365
                    "  \"cipher suites\"       : \"{3}\",\n" +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   366
                    "  \"compression methods\" : \"{4}\",\n" +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   367
                    "  \"extensions\"          : [\n" +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   368
                    "{5}\n" +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   369
                    "  ]\n" +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   370
                    "'}'",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   371
                    Locale.ENGLISH);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   372
                Object[] messageFields = {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   373
                    ProtocolVersion.nameOf(clientVersion),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   374
                    Utilities.toHexString(clientRandom.randomBytes),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   375
                    sessionId.toString(),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   376
                    getCipherSuiteNames().toString(),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   377
                    Utilities.toHexString(compressionMethod),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   378
                    Utilities.indent(Utilities.indent(extensions.toString()))
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   379
                };
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   380
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   381
                return messageFormat.format(messageFields);
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
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   386
    /**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   387
     * The "ClientHello" handshake message kick start producer.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   388
     */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   389
    private static final
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   390
            class ClientHelloKickstartProducer implements SSLProducer {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   391
        // Prevent instantiation of this class.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   392
        private ClientHelloKickstartProducer() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   393
            // blank
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   394
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   395
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   396
        // Produce kickstart handshake message.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   397
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   398
        public byte[] produce(ConnectionContext context) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   399
            // The producing happens in client side only.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   400
            ClientHandshakeContext chc = (ClientHandshakeContext)context;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   401
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   402
            // clean up this producer
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   403
            chc.handshakeProducers.remove(SSLHandshake.CLIENT_HELLO.id);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   404
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   405
            // the max protocol version this client is supporting.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   406
            ProtocolVersion maxProtocolVersion = chc.maximumActiveProtocol;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   407
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   408
            // session ID of the ClientHello message
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   409
            SessionId sessionId = SSLSessionImpl.nullSession.getSessionId();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   410
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   411
            // a list of cipher suites sent by the client
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   412
            List<CipherSuite> cipherSuites = chc.activeCipherSuites;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   413
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   414
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   415
            // Try to resume an existing session.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   416
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   417
            SSLSessionContextImpl ssci = (SSLSessionContextImpl)
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   418
                    chc.sslContext.engineGetClientSessionContext();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   419
            SSLSessionImpl session = ssci.get(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   420
                    chc.conContext.transport.getPeerHost(),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   421
                    chc.conContext.transport.getPeerPort());
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   422
            if (session != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   423
                // If unsafe server certificate change is not allowed, reserve
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   424
                // current server certificates if the previous handshake is a
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   425
                // session-resumption abbreviated initial handshake.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   426
                if (!ClientHandshakeContext.allowUnsafeServerCertChange &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   427
                        session.isSessionResumption()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   428
                    try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   429
                        // If existing, peer certificate chain cannot be null.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   430
                        chc.reservedServerCerts =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   431
                            (X509Certificate[])session.getPeerCertificates();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   432
                    } catch (SSLPeerUnverifiedException puve) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   433
                        // Maybe not certificate-based, ignore the exception.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   434
                    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   435
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   436
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   437
                if (!session.isRejoinable()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   438
                    session = null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   439
                    if (SSLLogger.isOn &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   440
                            SSLLogger.isOn("ssl,handshake,verbose")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   441
                        SSLLogger.finest(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   442
                            "Can't resume, the sessoin is not rejoinable");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   443
                    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   444
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   445
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   446
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   447
            CipherSuite sessionSuite = null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   448
            if (session != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   449
                sessionSuite = session.getSuite();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   450
                if (!chc.isNegotiable(sessionSuite)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   451
                    session = null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   452
                    if (SSLLogger.isOn &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   453
                            SSLLogger.isOn("ssl,handshake,verbose")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   454
                        SSLLogger.finest(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   455
                            "Can't resume, unavailable sessoin cipher suite");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   456
                    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   457
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   458
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   459
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   460
            ProtocolVersion sessionVersion = null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   461
            if (session != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   462
                sessionVersion = session.getProtocolVersion();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   463
                if (!chc.isNegotiable(sessionVersion)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   464
                    session = null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   465
                    if (SSLLogger.isOn &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   466
                            SSLLogger.isOn("ssl,handshake,verbose")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   467
                        SSLLogger.finest(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   468
                            "Can't resume, unavailable protocol version");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   469
                    }
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
            if (session != null &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   474
                !sessionVersion.useTLS13PlusSpec() &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   475
                SSLConfiguration.useExtendedMasterSecret) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   476
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   477
                boolean isEmsAvailable = chc.sslConfig.isAvailable(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   478
                    SSLExtension.CH_EXTENDED_MASTER_SECRET, sessionVersion);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   479
                if (isEmsAvailable && !session.useExtendedMasterSecret &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   480
                        !SSLConfiguration.allowLegacyResumption) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   481
                    // perform full handshake instead
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   482
                    //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   483
                    // The client SHOULD NOT offer an abbreviated handshake
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   484
                    // to resume a session that does not use an extended
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   485
                    // master secret.  Instead, it SHOULD offer a full
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   486
                    // handshake.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   487
                     session = null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   488
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   489
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   490
                if ((session != null) &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   491
                        !ClientHandshakeContext.allowUnsafeServerCertChange) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   492
                    // It is fine to move on with abbreviate handshake if
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   493
                    // endpoint identification is enabled.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   494
                    String identityAlg = chc.sslConfig.identificationProtocol;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   495
                    if ((identityAlg == null || identityAlg.length() == 0)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   496
                        if (isEmsAvailable) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   497
                            if (!session.useExtendedMasterSecret) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   498
                                // perform full handshake instead
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   499
                                session = null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   500
                            }   // Otherwise, use extended master secret.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   501
                        } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   502
                            // The extended master secret extension does not
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   503
                            // apply to SSL 3.0.  Perform a full handshake
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   504
                            // instead.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   505
                            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   506
                            // Note that the useExtendedMasterSecret is
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   507
                            // extended to protect SSL 3.0 connections,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   508
                            // by discarding abbreviate handshake.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   509
                            session = null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   510
                        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   511
                    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   512
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   513
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   514
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   515
            if (session != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   516
                if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake,verbose")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   517
                    SSLLogger.finest("Try resuming session", session);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   518
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   519
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   520
                sessionId = session.getSessionId();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   521
                if (!maxProtocolVersion.equals(sessionVersion)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   522
                    maxProtocolVersion = sessionVersion;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   523
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   524
                    // Update protocol version number in underlying socket and
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   525
                    // handshake output stream, so that the output records
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   526
                    // (at the record layer) have the correct version
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   527
                    chc.setVersion(sessionVersion);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   528
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   529
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   530
                // If no new session is allowed, force use of the previous
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   531
                // session ciphersuite, and add the renegotiation SCSV if
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   532
                // necessary.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   533
                if (!chc.sslConfig.enableSessionCreation) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   534
                    if (!chc.conContext.isNegotiated &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   535
                        !sessionVersion.useTLS13PlusSpec() &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   536
                        cipherSuites.contains(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   537
                            CipherSuite.TLS_EMPTY_RENEGOTIATION_INFO_SCSV)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   538
                        cipherSuites = Arrays.asList(sessionSuite,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   539
                            CipherSuite.TLS_EMPTY_RENEGOTIATION_INFO_SCSV);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   540
                    } else {    // otherwise, use renegotiation_info extension
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   541
                        cipherSuites = Arrays.asList(sessionSuite);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   542
                    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   543
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   544
                    if (SSLLogger.isOn &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   545
                            SSLLogger.isOn("ssl,handshake,verbose")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   546
                        SSLLogger.finest(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   547
                            "No new session is allowed, so try to resume " +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   548
                            "the session cipher suite only", sessionSuite);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   549
                    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   550
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   551
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   552
                chc.isResumption = true;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   553
                chc.resumingSession = session;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   554
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   555
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   556
            if (session == null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   557
                if (!chc.sslConfig.enableSessionCreation) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   558
                    throw new SSLHandshakeException(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   559
                            "No new session is allowed and " +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   560
                            "no existing session can be resumed");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   561
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   562
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   563
                if (maxProtocolVersion.useTLS13PlusSpec() &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   564
                        SSLConfiguration.useCompatibilityMode) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   565
                    // In compatibility mode, the TLS 1.3 legacy_session_id
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   566
                    // field MUST be non-empty, so a client not offering a
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   567
                    // pre-TLS 1.3 session MUST generate a new 32-byte value.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   568
                    sessionId =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   569
                        new SessionId(true, chc.sslContext.getSecureRandom());
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   570
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   571
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   572
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   573
            ProtocolVersion minimumVersion = ProtocolVersion.NONE;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   574
            for (ProtocolVersion pv : chc.activeProtocols) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   575
                if (minimumVersion == ProtocolVersion.NONE ||
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   576
                        pv.compare(minimumVersion) < 0) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   577
                    minimumVersion = pv;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   578
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   579
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   580
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   581
            // exclude SCSV for secure renegotiation
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   582
            if (!minimumVersion.useTLS13PlusSpec()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   583
                if (chc.conContext.secureRenegotiation &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   584
                        cipherSuites.contains(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   585
                            CipherSuite.TLS_EMPTY_RENEGOTIATION_INFO_SCSV)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   586
                    // The cipherSuites may be unmodifiable
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   587
                    cipherSuites = new LinkedList<>(cipherSuites);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   588
                    cipherSuites.remove(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   589
                            CipherSuite.TLS_EMPTY_RENEGOTIATION_INFO_SCSV);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   590
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   591
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   592
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   593
            // make sure there is a negotiable cipher suite.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   594
            boolean negotiable = false;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   595
            for (CipherSuite suite : cipherSuites) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   596
                if (chc.isNegotiable(suite)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   597
                    negotiable = true;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   598
                    break;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   599
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   600
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   601
            if (!negotiable) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   602
                throw new SSLHandshakeException("No negotiable cipher suite");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   603
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   604
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   605
            // Create the handshake message.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   606
            ProtocolVersion clientHelloVersion = maxProtocolVersion;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   607
            if (clientHelloVersion.useTLS13PlusSpec()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   608
                // In (D)TLS 1.3, the client indicates its version preferences
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   609
                // in the "supported_versions" extension and the client_version
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   610
                // (legacy_version) field MUST be set to (D)TLS 1.2.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   611
                if (clientHelloVersion.isDTLS) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   612
                    clientHelloVersion = ProtocolVersion.DTLS12;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   613
                } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   614
                    clientHelloVersion = ProtocolVersion.TLS12;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   615
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   616
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   617
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   618
            ClientHelloMessage chm = new ClientHelloMessage(chc,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   619
                    clientHelloVersion.id, sessionId, cipherSuites,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   620
                    chc.sslContext.getSecureRandom());
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   621
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   622
            // cache the client random number for further using
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   623
            chc.clientHelloRandom = chm.clientRandom;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   624
            chc.clientHelloVersion = clientHelloVersion.id;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   625
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   626
            // Produce extensions for ClientHello handshake message.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   627
            SSLExtension[] extTypes = chc.sslConfig.getEnabledExtensions(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   628
                    SSLHandshake.CLIENT_HELLO, chc.activeProtocols);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   629
            chm.extensions.produce(chc, extTypes);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   630
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   631
            if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   632
                SSLLogger.fine("Produced ClientHello handshake message", chm);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   633
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   634
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   635
            // Output the handshake message.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   636
            chm.write(chc.handshakeOutput);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   637
            chc.handshakeOutput.flush();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   638
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   639
            // Reserve the initial ClientHello message for the follow on
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   640
            // cookie exchange if needed.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   641
            chc.initialClientHelloMsg = chm;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   642
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   643
            // What's the expected response?
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   644
            chc.handshakeConsumers.put(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   645
                    SSLHandshake.SERVER_HELLO.id, SSLHandshake.SERVER_HELLO);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   646
            if (chc.sslContext.isDTLS() &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   647
                    !minimumVersion.useTLS13PlusSpec()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   648
                chc.handshakeConsumers.put(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   649
                        SSLHandshake.HELLO_VERIFY_REQUEST.id,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   650
                        SSLHandshake.HELLO_VERIFY_REQUEST);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   651
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   652
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   653
            // The handshake message has been delivered.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   654
            return null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   655
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   656
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   657
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   658
    private static final
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   659
            class ClientHelloProducer implements HandshakeProducer {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   660
        // Prevent instantiation of this class.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   661
        private ClientHelloProducer() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   662
            // blank
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   663
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   664
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   665
        // Response to one of the following handshake message:
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   666
        //     HelloRequest                     (SSL 3.0/TLS 1.0/1.1/1.2)
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   667
        //     ServerHello(HelloRetryRequest)   (TLS 1.3)
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   668
        //     HelloVerifyRequest               (DTLS 1.0/1.2)
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   669
        //     KeyUpdate                        (TLS 1.3)
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   670
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   671
        public byte[] produce(ConnectionContext context,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   672
                HandshakeMessage message) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   673
            // The producing happens in client side only.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   674
            ClientHandshakeContext chc = (ClientHandshakeContext)context;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   675
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   676
            SSLHandshake ht = message.handshakeType();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   677
            if (ht == null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   678
                throw new UnsupportedOperationException("Not supported yet.");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   679
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   680
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   681
            switch (ht) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   682
                case HELLO_REQUEST:
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   683
                    // SSL 3.0/TLS 1.0/1.1/1.2
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   684
                    try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   685
                        chc.kickstart();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   686
                    } catch (IOException ioe) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   687
                        chc.conContext.fatal(Alert.HANDSHAKE_FAILURE, ioe);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   688
                    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   689
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   690
                    // The handshake message has been delivered.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   691
                    return null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   692
                case HELLO_VERIFY_REQUEST:
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   693
                    // DTLS 1.0/1.2
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   694
                    //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   695
                    // The HelloVerifyRequest consumer should have updated the
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   696
                    // ClientHello handshake message with cookie.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   697
                    if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   698
                        SSLLogger.fine(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   699
                            "Produced ClientHello(cookie) handshake message",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   700
                            chc.initialClientHelloMsg);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   701
                    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   702
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   703
                    // Output the handshake message.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   704
                    chc.initialClientHelloMsg.write(chc.handshakeOutput);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   705
                    chc.handshakeOutput.flush();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   706
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   707
                    // What's the expected response?
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   708
                    chc.handshakeConsumers.put(SSLHandshake.SERVER_HELLO.id,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   709
                            SSLHandshake.SERVER_HELLO);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   710
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   711
                    ProtocolVersion minimumVersion = ProtocolVersion.NONE;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   712
                    for (ProtocolVersion pv : chc.activeProtocols) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   713
                        if (minimumVersion == ProtocolVersion.NONE ||
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   714
                                pv.compare(minimumVersion) < 0) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   715
                            minimumVersion = pv;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   716
                        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   717
                    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   718
                    if (chc.sslContext.isDTLS() &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   719
                            !minimumVersion.useTLS13PlusSpec()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   720
                        chc.handshakeConsumers.put(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   721
                                SSLHandshake.HELLO_VERIFY_REQUEST.id,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   722
                                SSLHandshake.HELLO_VERIFY_REQUEST);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   723
                    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   724
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   725
                    // The handshake message has been delivered.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   726
                    return null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   727
                case KEY_UPDATE:
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   728
                    // TLS 1.3
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   729
                    throw new UnsupportedOperationException(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   730
                            "Not supported yet.");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   731
                case HELLO_RETRY_REQUEST:
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   732
                    // TLS 1.3
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   733
                    // The HelloRetryRequest consumer should have updated the
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   734
                    // ClientHello handshake message with cookie.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   735
                    if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   736
                        SSLLogger.fine(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   737
                            "Produced ClientHello(HRR) handshake message",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   738
                            chc.initialClientHelloMsg);
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
                    // Output the handshake message.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   742
                    chc.initialClientHelloMsg.write(chc.handshakeOutput);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   743
                    chc.handshakeOutput.flush();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   744
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   745
                    // What's the expected response?
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   746
                    chc.conContext.consumers.putIfAbsent(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   747
                            ContentType.CHANGE_CIPHER_SPEC.id,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   748
                            ChangeCipherSpec.t13Consumer);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   749
                    chc.handshakeConsumers.put(SSLHandshake.SERVER_HELLO.id,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   750
                            SSLHandshake.SERVER_HELLO);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   751
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   752
                    // The handshake message has been delivered.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   753
                    return null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   754
                default:
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   755
                    throw new UnsupportedOperationException(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   756
                            "Not supported yet.");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   757
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   758
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   759
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   760
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   761
    /**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   762
     * The "ClientHello" handshake message consumer.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   763
     */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   764
    private static final class ClientHelloConsumer implements SSLConsumer {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   765
        // Prevent instantiation of this class.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   766
        private ClientHelloConsumer() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   767
            // blank
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
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   771
        public void consume(ConnectionContext context,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   772
                ByteBuffer message) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   773
            // The consuming happens in server side only.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   774
            ServerHandshakeContext shc = (ServerHandshakeContext)context;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   775
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   776
            // clean up this consumer
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   777
            shc.handshakeConsumers.remove(SSLHandshake.CLIENT_HELLO.id);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   778
            if (!shc.handshakeConsumers.isEmpty()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   779
                shc.conContext.fatal(Alert.UNEXPECTED_MESSAGE,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   780
                        "No more handshake message allowed " +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   781
                        "in a ClientHello flight");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   782
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   783
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   784
            // Get enabled extension types in ClientHello handshake message.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   785
            SSLExtension[] enabledExtensions =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   786
                    shc.sslConfig.getEnabledExtensions(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   787
                            SSLHandshake.CLIENT_HELLO);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   788
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   789
            ClientHelloMessage chm =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   790
                    new ClientHelloMessage(shc, message, enabledExtensions);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   791
            if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   792
                SSLLogger.fine("Consuming ClientHello handshake message", chm);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   793
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   794
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   795
            shc.clientHelloVersion = chm.clientVersion;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   796
            onClientHello(shc, chm);
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
        private void onClientHello(ServerHandshakeContext context,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   800
                ClientHelloMessage clientHello) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   801
            // Negotiate protocol version.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   802
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   803
            // Check and lanuch SupportedVersions.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   804
            SSLExtension[] extTypes = new SSLExtension[] {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   805
                    SSLExtension.CH_SUPPORTED_VERSIONS
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   806
                };
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   807
            clientHello.extensions.consumeOnLoad(context, extTypes);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   808
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   809
            ProtocolVersion negotiatedProtocol;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   810
            CHSupportedVersionsSpec svs =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   811
                    (CHSupportedVersionsSpec)context.handshakeExtensions.get(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   812
                            SSLExtension.CH_SUPPORTED_VERSIONS);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   813
            if (svs != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   814
                negotiatedProtocol =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   815
                        negotiateProtocol(context, svs.requestedProtocols);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   816
            } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   817
                negotiatedProtocol =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   818
                        negotiateProtocol(context, clientHello.clientVersion);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   819
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   820
            context.negotiatedProtocol = negotiatedProtocol;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   821
            if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   822
                SSLLogger.fine(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   823
                    "Negotiated protocol version: " + negotiatedProtocol.name);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   824
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   825
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   826
            // Consume the handshake message for the specific protocol version.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   827
            if (negotiatedProtocol.isDTLS) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   828
                if (negotiatedProtocol.useTLS13PlusSpec()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   829
                    d13HandshakeConsumer.consume(context, clientHello);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   830
                } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   831
                    d12HandshakeConsumer.consume(context, clientHello);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   832
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   833
            } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   834
                if (negotiatedProtocol.useTLS13PlusSpec()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   835
                    t13HandshakeConsumer.consume(context, clientHello);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   836
                } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   837
                    t12HandshakeConsumer.consume(context, clientHello);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   838
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   839
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   840
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   841
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   842
        // Select a protocol version according to the
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   843
        // ClientHello.client_version.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   844
        private ProtocolVersion negotiateProtocol(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   845
                ServerHandshakeContext context,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   846
                int clientHelloVersion) throws SSLException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   847
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   848
            // Per TLS 1.3 specification, server MUST negotiate TLS 1.2 or prior
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   849
            // even if ClientHello.client_version is 0x0304 or later.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   850
            int chv = clientHelloVersion;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   851
            if (context.sslContext.isDTLS()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   852
                if (chv < ProtocolVersion.DTLS12.id) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   853
                    chv = ProtocolVersion.DTLS12.id;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   854
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   855
            } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   856
                if (chv > ProtocolVersion.TLS12.id) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   857
                    chv = ProtocolVersion.TLS12.id;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   858
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   859
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   860
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   861
            // Select a protocol version from the activated protocols.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   862
            ProtocolVersion pv = ProtocolVersion.selectedFrom(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   863
                    context.activeProtocols, chv);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   864
            if (pv == null || pv == ProtocolVersion.NONE ||
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   865
                    pv == ProtocolVersion.SSL20Hello) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   866
                context.conContext.fatal(Alert.PROTOCOL_VERSION,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   867
                    "Client requested protocol " +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   868
                    ProtocolVersion.nameOf(clientHelloVersion) +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   869
                    " is not enabled or supported in server context");
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
            return pv;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   873
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   874
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   875
        // Select a protocol version according to the
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   876
        // supported_versions extension.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   877
        private ProtocolVersion negotiateProtocol(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   878
                ServerHandshakeContext context,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   879
                int[] clientSupportedVersions) throws SSLException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   880
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   881
            // The client supported protocol versions are present in client
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   882
            // preference order.  This implementation chooses to use the server
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   883
            // preference of protocol versions instead.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   884
            for (ProtocolVersion spv : context.activeProtocols) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   885
                if (spv == ProtocolVersion.SSL20Hello) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   886
                    continue;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   887
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   888
                for (int cpv : clientSupportedVersions) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   889
                    if (cpv == ProtocolVersion.SSL20Hello.id) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   890
                        continue;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   891
                    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   892
                    if (spv.id == cpv) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   893
                        return spv;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   894
                    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   895
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   896
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   897
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   898
            // No protocol version can be negotiated.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   899
            context.conContext.fatal(Alert.PROTOCOL_VERSION,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   900
                "The client supported protocol versions " + Arrays.toString(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   901
                    ProtocolVersion.toStringArray(clientSupportedVersions)) +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   902
                " are not accepted by server preferences " +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   903
                context.activeProtocols);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   904
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   905
            return null;        // make the compiler happy
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   906
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   907
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   908
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   909
    /**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   910
     * The "ClientHello" handshake message consumer for TLS 1.2 and
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   911
     * prior SSL/TLS protocol versions.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   912
     */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   913
    private static final
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   914
            class T12ClientHelloConsumer implements HandshakeConsumer {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   915
        // Prevent instantiation of this class.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   916
        private T12ClientHelloConsumer() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   917
            // blank
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   918
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   919
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   920
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   921
        public void consume(ConnectionContext context,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   922
                HandshakeMessage message) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   923
            // The consuming happens in server side only.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   924
            ServerHandshakeContext shc = (ServerHandshakeContext)context;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   925
            ClientHelloMessage clientHello = (ClientHelloMessage)message;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   926
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   927
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   928
            // validate
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   929
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   930
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   931
            // Reject client initiated renegotiation?
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   932
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   933
            // If server side should reject client-initiated renegotiation,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   934
            // send an Alert.HANDSHAKE_FAILURE fatal alert, not a
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   935
            // no_renegotiation warning alert (no_renegotiation must be a
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   936
            // warning: RFC 2246).  no_renegotiation might seem more
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   937
            // natural at first, but warnings are not appropriate because
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   938
            // the sending party does not know how the receiving party
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   939
            // will behave.  This state must be treated as a fatal server
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   940
            // condition.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   941
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   942
            // This will not have any impact on server initiated renegotiation.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   943
            if (shc.conContext.isNegotiated) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   944
                if (!shc.conContext.secureRenegotiation &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   945
                        !HandshakeContext.allowUnsafeRenegotiation) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   946
                    shc.conContext.fatal(Alert.HANDSHAKE_FAILURE,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   947
                            "Unsafe renegotiation is not allowed");
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 (ServerHandshakeContext.rejectClientInitiatedRenego &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   951
                        !shc.kickstartMessageDelivered) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   952
                    shc.conContext.fatal(Alert.HANDSHAKE_FAILURE,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   953
                            "Client initiated renegotiation is not allowed");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   954
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   955
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   956
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   957
            // Is it an abbreviated handshake?
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   958
            if (clientHello.sessionId.length() != 0) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   959
                SSLSessionImpl previous = ((SSLSessionContextImpl)shc.sslContext
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   960
                            .engineGetServerSessionContext())
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   961
                            .get(clientHello.sessionId.getId());
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   962
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   963
                boolean resumingSession =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   964
                        (previous != null) && previous.isRejoinable();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   965
                if (!resumingSession) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   966
                    if (SSLLogger.isOn &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   967
                            SSLLogger.isOn("ssl,handshake,verbose")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   968
                        SSLLogger.finest(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   969
                                "Can't resume, " +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   970
                                "the existing sessoin is not rejoinable");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   971
                    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   972
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   973
                // Validate the negotiated protocol version.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   974
                if (resumingSession) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   975
                    ProtocolVersion sessionProtocol =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   976
                            previous.getProtocolVersion();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   977
                    if (sessionProtocol != shc.negotiatedProtocol) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   978
                        resumingSession = false;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   979
                        if (SSLLogger.isOn &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   980
                                SSLLogger.isOn("ssl,handshake,verbose")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   981
                            SSLLogger.finest(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   982
                                "Can't resume, not the same protocol version");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   983
                        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   984
                    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   985
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   986
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   987
                // Validate the required client authentication.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   988
                if (resumingSession &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   989
                    (shc.sslConfig.clientAuthType == CLIENT_AUTH_REQUIRED)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   990
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   991
                    try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   992
                        previous.getPeerPrincipal();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   993
                    } catch (SSLPeerUnverifiedException e) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   994
                        resumingSession = false;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   995
                        if (SSLLogger.isOn &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   996
                                SSLLogger.isOn("ssl,handshake,verbose")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   997
                            SSLLogger.finest(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   998
                                "Can't resume, " +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   999
                                "client authentication is required");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1000
                        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1001
                    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1002
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1003
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1004
                // Validate that the cached cipher suite.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1005
                if (resumingSession) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1006
                    CipherSuite suite = previous.getSuite();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1007
                    if ((!shc.isNegotiable(suite)) ||
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1008
                            (!clientHello.cipherSuites.contains(suite))) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1009
                        resumingSession = false;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1010
                        if (SSLLogger.isOn &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1011
                                SSLLogger.isOn("ssl,handshake,verbose")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1012
                            SSLLogger.finest(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1013
                                "Can't resume, " +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1014
                                "the session cipher suite is absent");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1015
                        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1016
                    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1017
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1018
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1019
                // So far so good.  Note that the handshake extensions may reset
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1020
                // the resuming options later.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1021
                shc.isResumption = resumingSession;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1022
                shc.resumingSession = resumingSession ? previous : null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1023
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1024
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1025
            // cache the client random number for further using
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1026
            shc.clientHelloRandom = clientHello.clientRandom;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1027
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1028
            // Check and launch ClientHello extensions.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1029
            SSLExtension[] extTypes = shc.sslConfig.getEnabledExtensions(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1030
                    SSLHandshake.CLIENT_HELLO);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1031
            clientHello.extensions.consumeOnLoad(shc, extTypes);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1032
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1033
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1034
            // update
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1035
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1036
            if (!shc.conContext.isNegotiated) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1037
                shc.conContext.protocolVersion = shc.negotiatedProtocol;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1038
                shc.conContext.outputRecord.setVersion(shc.negotiatedProtocol);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1039
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1040
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1041
            // update the responders
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1042
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1043
            // Only need to ServerHello, which may add more responders later.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1044
            // Note that ServerHello and HelloRetryRequest share the same
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1045
            // handshake type/id.  The ServerHello producer may be replaced
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1046
            // by HelloRetryRequest producer if needed.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1047
            shc.handshakeProducers.put(SSLHandshake.SERVER_HELLO.id,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1048
                    SSLHandshake.SERVER_HELLO);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1049
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1050
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1051
            // produce
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1052
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1053
            SSLHandshake[] probableHandshakeMessages = new SSLHandshake[] {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1054
                SSLHandshake.SERVER_HELLO,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1055
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1056
                // full handshake messages
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1057
                SSLHandshake.CERTIFICATE,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1058
                SSLHandshake.CERTIFICATE_STATUS,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1059
                SSLHandshake.SERVER_KEY_EXCHANGE,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1060
                SSLHandshake.CERTIFICATE_REQUEST,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1061
                SSLHandshake.SERVER_HELLO_DONE,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1062
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1063
                // abbreviated handshake messages
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1064
                SSLHandshake.FINISHED
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1065
            };
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1066
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1067
            for (SSLHandshake hs : probableHandshakeMessages) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1068
                HandshakeProducer handshakeProducer =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1069
                        shc.handshakeProducers.remove(hs.id);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1070
                if (handshakeProducer != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1071
                    handshakeProducer.produce(context, clientHello);
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
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1075
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1076
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1077
    /**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1078
     * The "ClientHello" handshake message consumer for TLS 1.3.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1079
     */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1080
    private static final
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1081
            class T13ClientHelloConsumer implements HandshakeConsumer {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1082
        // Prevent instantiation of this class.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1083
        private T13ClientHelloConsumer() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1084
            // blank
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1085
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1086
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1087
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1088
        public void consume(ConnectionContext context,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1089
                HandshakeMessage message) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1090
            // The consuming happens in server side only.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1091
            ServerHandshakeContext shc = (ServerHandshakeContext)context;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1092
            ClientHelloMessage clientHello = (ClientHelloMessage)message;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1093
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1094
            // The client may send a dummy change_cipher_spec record
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1095
            // immediately after the first ClientHello.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1096
            shc.conContext.consumers.putIfAbsent(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1097
                    ContentType.CHANGE_CIPHER_SPEC.id,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1098
                    ChangeCipherSpec.t13Consumer);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1099
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1100
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1101
            // validate
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1102
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1103
            // Check and launch ClientHello extensions.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1104
            SSLExtension[] extTypes = shc.sslConfig.getEnabledExtensions(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1105
                    SSLHandshake.CLIENT_HELLO);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1106
            clientHello.extensions.consumeOnLoad(shc, extTypes);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1107
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1108
            if (!shc.handshakeProducers.isEmpty()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1109
                // Should be HelloRetryRequest producer.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1110
                goHelloRetryRequest(shc, clientHello);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1111
            } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1112
                goServerHello(shc, clientHello);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1113
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1114
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1115
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1116
        private void goHelloRetryRequest(ServerHandshakeContext shc,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1117
                ClientHelloMessage clientHello) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1118
            HandshakeProducer handshakeProducer =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1119
                    shc.handshakeProducers.remove(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1120
                            SSLHandshake.HELLO_RETRY_REQUEST.id);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1121
            if (handshakeProducer != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1122
                    handshakeProducer.produce(shc, clientHello);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1123
            } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1124
                // unlikely
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1125
                shc.conContext.fatal(Alert.HANDSHAKE_FAILURE,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1126
                    "No HelloRetryRequest producer: " + shc.handshakeProducers);
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
            if (!shc.handshakeProducers.isEmpty()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1130
                // unlikely, but please double check.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1131
                shc.conContext.fatal(Alert.HANDSHAKE_FAILURE,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1132
                    "unknown handshake producers: " + shc.handshakeProducers);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1133
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1134
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1135
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1136
        private void goServerHello(ServerHandshakeContext shc,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1137
                ClientHelloMessage clientHello) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1138
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1139
            // validate
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1140
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1141
            shc.clientHelloRandom = clientHello.clientRandom;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1142
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1143
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1144
            // update
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1145
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1146
            if (!shc.conContext.isNegotiated) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1147
                shc.conContext.protocolVersion = shc.negotiatedProtocol;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1148
                shc.conContext.outputRecord.setVersion(shc.negotiatedProtocol);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1149
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1150
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1151
            // update the responders
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1152
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1153
            // Only ServerHello/HelloRetryRequest producer, which adds
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1154
            // more responders later.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1155
            shc.handshakeProducers.put(SSLHandshake.SERVER_HELLO.id,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1156
                SSLHandshake.SERVER_HELLO);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1157
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1158
            SSLHandshake[] probableHandshakeMessages = new SSLHandshake[] {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1159
                SSLHandshake.SERVER_HELLO,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1160
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1161
                // full handshake messages
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1162
                SSLHandshake.ENCRYPTED_EXTENSIONS,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1163
                SSLHandshake.CERTIFICATE_REQUEST,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1164
                SSLHandshake.CERTIFICATE,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1165
                SSLHandshake.CERTIFICATE_VERIFY,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1166
                SSLHandshake.FINISHED
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1167
            };
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1168
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1169
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1170
            // produce
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1171
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1172
            for (SSLHandshake hs : probableHandshakeMessages) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1173
                HandshakeProducer handshakeProducer =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1174
                        shc.handshakeProducers.remove(hs.id);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1175
                if (handshakeProducer != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1176
                    handshakeProducer.produce(shc, clientHello);
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
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1180
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1181
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1182
    /**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1183
     * The "ClientHello" handshake message consumer for DTLS 1.2 and
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1184
     * previous DTLS protocol versions.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1185
     */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1186
    private static final
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1187
            class D12ClientHelloConsumer implements HandshakeConsumer {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1188
        // Prevent instantiation of this class.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1189
        private D12ClientHelloConsumer() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1190
            // blank
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1191
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1192
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1193
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1194
        public void consume(ConnectionContext context,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1195
                HandshakeMessage message) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1196
            // The consuming happens in server side only.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1197
            ServerHandshakeContext shc = (ServerHandshakeContext)context;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1198
            ClientHelloMessage clientHello = (ClientHelloMessage)message;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1199
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1200
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1201
            // validate
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1202
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1203
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1204
            // Reject client initiated renegotiation?
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1205
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1206
            // If server side should reject client-initiated renegotiation,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1207
            // send an Alert.HANDSHAKE_FAILURE fatal alert, not a
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1208
            // no_renegotiation warning alert (no_renegotiation must be a
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1209
            // warning: RFC 2246).  no_renegotiation might seem more
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1210
            // natural at first, but warnings are not appropriate because
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1211
            // the sending party does not know how the receiving party
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1212
            // will behave.  This state must be treated as a fatal server
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1213
            // condition.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1214
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1215
            // This will not have any impact on server initiated renegotiation.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1216
            if (shc.conContext.isNegotiated) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1217
                if (!shc.conContext.secureRenegotiation &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1218
                        !HandshakeContext.allowUnsafeRenegotiation) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1219
                    shc.conContext.fatal(Alert.HANDSHAKE_FAILURE,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1220
                            "Unsafe renegotiation is not allowed");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1221
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1222
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1223
                if (ServerHandshakeContext.rejectClientInitiatedRenego &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1224
                        !shc.kickstartMessageDelivered) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1225
                    shc.conContext.fatal(Alert.HANDSHAKE_FAILURE,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1226
                            "Client initiated renegotiation is not allowed");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1227
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1228
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1229
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1230
            // Is it an abbreviated handshake?
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1231
            if (clientHello.sessionId.length() != 0) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1232
                SSLSessionImpl previous = ((SSLSessionContextImpl)shc.sslContext
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1233
                            .engineGetServerSessionContext())
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1234
                            .get(clientHello.sessionId.getId());
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1235
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1236
                boolean resumingSession =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1237
                        (previous != null) && previous.isRejoinable();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1238
                if (!resumingSession) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1239
                    if (SSLLogger.isOn &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1240
                            SSLLogger.isOn("ssl,handshake,verbose")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1241
                        SSLLogger.finest(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1242
                            "Can't resume, " +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1243
                            "the existing sessoin is not rejoinable");
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
                // Validate the negotiated protocol version.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1247
                if (resumingSession) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1248
                    ProtocolVersion sessionProtocol =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1249
                            previous.getProtocolVersion();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1250
                    if (sessionProtocol != shc.negotiatedProtocol) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1251
                        resumingSession = false;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1252
                        if (SSLLogger.isOn &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1253
                                SSLLogger.isOn("ssl,handshake,verbose")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1254
                            SSLLogger.finest(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1255
                                "Can't resume, not the same protocol version");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1256
                        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1257
                    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1258
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1259
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1260
                // Validate the required client authentication.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1261
                if (resumingSession &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1262
                    (shc.sslConfig.clientAuthType == CLIENT_AUTH_REQUIRED)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1263
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1264
                    try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1265
                        previous.getPeerPrincipal();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1266
                    } catch (SSLPeerUnverifiedException e) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1267
                        resumingSession = false;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1268
                        if (SSLLogger.isOn &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1269
                                SSLLogger.isOn("ssl,handshake,verbose")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1270
                            SSLLogger.finest(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1271
                                "Can't resume, " +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1272
                                "client authentication is required");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1273
                        }
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
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1277
                // Validate that the cached cipher suite.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1278
                if (resumingSession) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1279
                    CipherSuite suite = previous.getSuite();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1280
                    if ((!shc.isNegotiable(suite)) ||
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1281
                            (!clientHello.cipherSuites.contains(suite))) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1282
                        resumingSession = false;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1283
                        if (SSLLogger.isOn &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1284
                                SSLLogger.isOn("ssl,handshake,verbose")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1285
                            SSLLogger.finest(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1286
                                "Can't resume, " +
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1287
                                "the session cipher suite is absent");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1288
                        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1289
                    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1290
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1291
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1292
                // So far so good.  Note that the handshake extensions may reset
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1293
                // the resuming options later.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1294
                shc.isResumption = resumingSession;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1295
                shc.resumingSession = resumingSession ? previous : null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1296
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1297
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1298
            HelloCookieManager hcm =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1299
                shc.sslContext.getHelloCookieManager(ProtocolVersion.DTLS10);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1300
            if (!shc.isResumption &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1301
                !hcm.isCookieValid(shc, clientHello, clientHello.cookie)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1302
                //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1303
                // Perform cookie exchange for DTLS handshaking if no cookie
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1304
                // or the cookie is invalid in the ClientHello message.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1305
                //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1306
                // update the responders
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1307
                shc.handshakeProducers.put(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1308
                        SSLHandshake.HELLO_VERIFY_REQUEST.id,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1309
                        SSLHandshake.HELLO_VERIFY_REQUEST);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1310
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1311
                //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1312
                // produce response handshake message
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1313
                //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1314
                SSLHandshake.HELLO_VERIFY_REQUEST.produce(context, clientHello);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1315
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1316
                return;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1317
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1318
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1319
            // cache the client random number for further using
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1320
            shc.clientHelloRandom = clientHello.clientRandom;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1321
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1322
            // Check and launch ClientHello extensions.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1323
            SSLExtension[] extTypes = shc.sslConfig.getEnabledExtensions(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1324
                    SSLHandshake.CLIENT_HELLO);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1325
            clientHello.extensions.consumeOnLoad(shc, extTypes);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1326
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1327
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1328
            // update
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1329
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1330
            if (!shc.conContext.isNegotiated) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1331
                shc.conContext.protocolVersion = shc.negotiatedProtocol;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1332
                shc.conContext.outputRecord.setVersion(shc.negotiatedProtocol);
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
            // update the responders
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1336
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1337
            // Only need to ServerHello, which may add more responders later.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1338
            shc.handshakeProducers.put(SSLHandshake.SERVER_HELLO.id,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1339
                    SSLHandshake.SERVER_HELLO);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1340
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1341
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1342
            // produce
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1343
            //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1344
            SSLHandshake[] probableHandshakeMessages = new SSLHandshake[] {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1345
                SSLHandshake.SERVER_HELLO,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1346
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1347
                // full handshake messages
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1348
                SSLHandshake.CERTIFICATE,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1349
                SSLHandshake.CERTIFICATE_STATUS,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1350
                SSLHandshake.SERVER_KEY_EXCHANGE,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1351
                SSLHandshake.CERTIFICATE_REQUEST,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1352
                SSLHandshake.SERVER_HELLO_DONE,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1353
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1354
                // abbreviated handshake messages
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1355
                SSLHandshake.FINISHED
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1356
            };
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1357
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1358
            for (SSLHandshake hs : probableHandshakeMessages) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1359
                HandshakeProducer handshakeProducer =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1360
                        shc.handshakeProducers.remove(hs.id);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1361
                if (handshakeProducer != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1362
                    handshakeProducer.produce(context, clientHello);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1363
                }
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
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1368
    /**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1369
     * The "ClientHello" handshake message consumer for DTLS 1.3.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1370
     */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1371
    private static final
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1372
            class D13ClientHelloConsumer implements HandshakeConsumer {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1373
        // Prevent instantiation of this class.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1374
        private D13ClientHelloConsumer() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1375
            // blank
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1376
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1377
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1378
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1379
        public void consume(ConnectionContext context,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1380
                HandshakeMessage message) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1381
            throw new UnsupportedOperationException("Not supported yet.");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1382
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1383
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
  1384
}