src/java.base/share/classes/sun/security/ssl/ServerHello.java
author wetmore
Tue, 21 Aug 2018 11:30:48 -0700
changeset 51574 ed52ea83f830
parent 50768 68fa3d4026ea
child 53055 c36464ea1f04
permissions -rw-r--r--
8207317: SSLEngine negotiation fail exception behavior changed from fail-fast to fail-lazy Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
     1
/*
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
     2
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
     4
 *
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    10
 *
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    15
 * accompanied this code).
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    16
 *
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    20
 *
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    23
 * questions.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    24
 */
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    25
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    26
package sun.security.ssl;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    27
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    28
import java.io.IOException;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    29
import java.nio.ByteBuffer;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    30
import java.security.AlgorithmConstraints;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    31
import java.security.GeneralSecurityException;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    32
import java.text.MessageFormat;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    33
import java.util.Arrays;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    34
import java.util.LinkedList;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    35
import java.util.List;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    36
import java.util.Locale;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    37
import java.util.Map;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    38
import java.util.Optional;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    39
import javax.crypto.SecretKey;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    40
import javax.crypto.spec.IvParameterSpec;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    41
import javax.net.ssl.SSLException;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    42
import javax.net.ssl.SSLHandshakeException;
51574
ed52ea83f830 8207317: SSLEngine negotiation fail exception behavior changed from fail-fast to fail-lazy
wetmore
parents: 50768
diff changeset
    43
import javax.net.ssl.SSLProtocolException;
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    44
import sun.security.ssl.CipherSuite.KeyExchange;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    45
import sun.security.ssl.ClientHello.ClientHelloMessage;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    46
import sun.security.ssl.SSLCipher.SSLReadCipher;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    47
import sun.security.ssl.SSLCipher.SSLWriteCipher;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    48
import sun.security.ssl.SSLHandshake.HandshakeMessage;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    49
import sun.security.ssl.SupportedVersionsExtension.SHSupportedVersionsSpec;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    50
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    51
/**
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    52
 * Pack of the ServerHello/HelloRetryRequest handshake message.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    53
 */
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    54
final class ServerHello {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    55
    static final SSLConsumer handshakeConsumer =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    56
        new ServerHelloConsumer();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    57
    static final HandshakeProducer t12HandshakeProducer =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    58
        new T12ServerHelloProducer();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    59
    static final HandshakeProducer t13HandshakeProducer =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    60
        new T13ServerHelloProducer();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    61
    static final HandshakeProducer hrrHandshakeProducer =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    62
        new T13HelloRetryRequestProducer();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    63
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    64
    static final HandshakeProducer hrrReproducer =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    65
        new T13HelloRetryRequestReproducer();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    66
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    67
    private static final HandshakeConsumer t12HandshakeConsumer =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    68
        new T12ServerHelloConsumer();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    69
    private static final HandshakeConsumer t13HandshakeConsumer =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    70
        new T13ServerHelloConsumer();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    71
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    72
    private static final HandshakeConsumer d12HandshakeConsumer =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    73
        new T12ServerHelloConsumer();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    74
    private static final HandshakeConsumer d13HandshakeConsumer =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    75
        new T13ServerHelloConsumer();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    76
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    77
    private static final HandshakeConsumer t13HrrHandshakeConsumer =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    78
        new T13HelloRetryRequestConsumer();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    79
    private static final HandshakeConsumer d13HrrHandshakeConsumer =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    80
        new T13HelloRetryRequestConsumer();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    81
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    82
    /**
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    83
     * The ServerHello handshake message.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    84
     */
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    85
    static final class ServerHelloMessage extends HandshakeMessage {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    86
        final ProtocolVersion           serverVersion;      // TLS 1.3 legacy
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    87
        final RandomCookie              serverRandom;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    88
        final SessionId                 sessionId;          // TLS 1.3 legacy
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    89
        final CipherSuite               cipherSuite;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    90
        final byte                      compressionMethod;  // TLS 1.3 legacy
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    91
        final SSLExtensions             extensions;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    92
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    93
        // The HelloRetryRequest producer needs to use the ClientHello message
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    94
        // for cookie generation.  Please don't use this field for other
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    95
        // purpose unless it is really necessary.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    96
        final ClientHelloMessage        clientHello;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    97
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    98
        // Reserved for HelloRetryRequest consumer.  Please don't use this
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
    99
        // field for other purpose unless it is really necessary.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   100
        final ByteBuffer                handshakeRecord;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   101
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   102
        ServerHelloMessage(HandshakeContext context,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   103
                ProtocolVersion serverVersion, SessionId sessionId,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   104
                CipherSuite cipherSuite, RandomCookie serverRandom,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   105
                ClientHelloMessage clientHello) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   106
            super(context);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   107
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   108
            this.serverVersion = serverVersion;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   109
            this.serverRandom = serverRandom;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   110
            this.sessionId = sessionId;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   111
            this.cipherSuite = cipherSuite;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   112
            this.compressionMethod = 0x00;      // Don't support compression.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   113
            this.extensions = new SSLExtensions(this);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   114
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   115
            // Reserve the ClientHello message for cookie generation.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   116
            this.clientHello = clientHello;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   117
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   118
            // The handshakeRecord field is used for HelloRetryRequest consumer
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   119
            // only.  It's fine to set it to null for generating side of the
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   120
            // ServerHello/HelloRetryRequest message.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   121
            this.handshakeRecord = null;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   122
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   123
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   124
        ServerHelloMessage(HandshakeContext context,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   125
                ByteBuffer m) throws IOException {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   126
            super(context);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   127
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   128
            // Reserve for HelloRetryRequest consumer if needed.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   129
            this.handshakeRecord = m.duplicate();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   130
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   131
            byte major = m.get();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   132
            byte minor = m.get();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   133
            this.serverVersion = ProtocolVersion.valueOf(major, minor);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   134
            if (this.serverVersion == null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   135
                // The client should only request for known protocol versions.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   136
                context.conContext.fatal(Alert.PROTOCOL_VERSION,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   137
                    "Unsupported protocol version: " +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   138
                    ProtocolVersion.nameOf(major, minor));
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   139
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   140
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   141
            this.serverRandom = new RandomCookie(m);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   142
            this.sessionId = new SessionId(Record.getBytes8(m));
51574
ed52ea83f830 8207317: SSLEngine negotiation fail exception behavior changed from fail-fast to fail-lazy
wetmore
parents: 50768
diff changeset
   143
            try {
ed52ea83f830 8207317: SSLEngine negotiation fail exception behavior changed from fail-fast to fail-lazy
wetmore
parents: 50768
diff changeset
   144
                sessionId.checkLength(serverVersion.id);
ed52ea83f830 8207317: SSLEngine negotiation fail exception behavior changed from fail-fast to fail-lazy
wetmore
parents: 50768
diff changeset
   145
            } catch (SSLProtocolException ex) {
ed52ea83f830 8207317: SSLEngine negotiation fail exception behavior changed from fail-fast to fail-lazy
wetmore
parents: 50768
diff changeset
   146
                handshakeContext.conContext.fatal(Alert.ILLEGAL_PARAMETER, ex);
ed52ea83f830 8207317: SSLEngine negotiation fail exception behavior changed from fail-fast to fail-lazy
wetmore
parents: 50768
diff changeset
   147
            }
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   148
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   149
            int cipherSuiteId = Record.getInt16(m);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   150
            this.cipherSuite = CipherSuite.valueOf(cipherSuiteId);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   151
            if (cipherSuite == null || !context.isNegotiable(cipherSuite)) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   152
                context.conContext.fatal(Alert.ILLEGAL_PARAMETER,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   153
                    "Server selected improper ciphersuite " +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   154
                    CipherSuite.nameOf(cipherSuiteId));
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   155
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   156
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   157
            this.compressionMethod = m.get();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   158
            if (compressionMethod != 0) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   159
                context.conContext.fatal(Alert.ILLEGAL_PARAMETER,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   160
                    "compression type not supported, " + compressionMethod);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   161
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   162
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   163
            SSLExtension[] supportedExtensions;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   164
            if (serverRandom.isHelloRetryRequest()) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   165
                supportedExtensions = context.sslConfig.getEnabledExtensions(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   166
                            SSLHandshake.HELLO_RETRY_REQUEST);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   167
            } else {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   168
                supportedExtensions = context.sslConfig.getEnabledExtensions(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   169
                            SSLHandshake.SERVER_HELLO);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   170
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   171
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   172
            if (m.hasRemaining()) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   173
                this.extensions =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   174
                    new SSLExtensions(this, m, supportedExtensions);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   175
            } else {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   176
                this.extensions = new SSLExtensions(this);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   177
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   178
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   179
            // The clientHello field is used for HelloRetryRequest producer
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   180
            // only.  It's fine to set it to null for receiving side of
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   181
            // ServerHello/HelloRetryRequest message.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   182
            this.clientHello = null;        // not used, let it be null;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   183
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   184
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   185
        @Override
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   186
        public SSLHandshake handshakeType() {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   187
            return serverRandom.isHelloRetryRequest() ?
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   188
                SSLHandshake.HELLO_RETRY_REQUEST : SSLHandshake.SERVER_HELLO;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   189
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   190
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   191
        @Override
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   192
        public int messageLength() {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   193
            // almost fixed header size, except session ID and extensions:
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   194
            //      major + minor = 2
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   195
            //      random = 32
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   196
            //      session ID len field = 1
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   197
            //      cipher suite = 2
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   198
            //      compression = 1
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   199
            //      extensions: if present, 2 + length of extensions
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   200
            // In TLS 1.3, use of certain extensions is mandatory.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   201
            return 38 + sessionId.length() + extensions.length();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   202
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   203
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   204
        @Override
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   205
        public void send(HandshakeOutStream hos) throws IOException {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   206
            hos.putInt8(serverVersion.major);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   207
            hos.putInt8(serverVersion.minor);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   208
            hos.write(serverRandom.randomBytes);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   209
            hos.putBytes8(sessionId.getId());
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   210
            hos.putInt8((cipherSuite.id >> 8) & 0xFF);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   211
            hos.putInt8(cipherSuite.id & 0xff);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   212
            hos.putInt8(compressionMethod);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   213
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   214
            extensions.send(hos);           // In TLS 1.3, use of certain
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   215
                                            // extensions is mandatory.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   216
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   217
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   218
        @Override
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   219
        public String toString() {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   220
            MessageFormat messageFormat = new MessageFormat(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   221
                "\"{0}\": '{'\n" +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   222
                "  \"server version\"      : \"{1}\",\n" +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   223
                "  \"random\"              : \"{2}\",\n" +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   224
                "  \"session id\"          : \"{3}\",\n" +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   225
                "  \"cipher suite\"        : \"{4}\",\n" +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   226
                "  \"compression methods\" : \"{5}\",\n" +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   227
                "  \"extensions\"          : [\n" +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   228
                "{6}\n" +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   229
                "  ]\n" +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   230
                "'}'",
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   231
                Locale.ENGLISH);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   232
            Object[] messageFields = {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   233
                serverRandom.isHelloRetryRequest() ?
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   234
                    "HelloRetryRequest" : "ServerHello",
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   235
                serverVersion.name,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   236
                Utilities.toHexString(serverRandom.randomBytes),
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   237
                sessionId.toString(),
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   238
                cipherSuite.name + "(" +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   239
                        Utilities.byte16HexString(cipherSuite.id) + ")",
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   240
                Utilities.toHexString(compressionMethod),
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   241
                Utilities.indent(extensions.toString(), "    ")
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   242
            };
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   243
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   244
            return messageFormat.format(messageFields);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   245
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   246
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   247
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   248
    /**
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   249
     * The "ServerHello" handshake message producer.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   250
     */
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   251
    private static final class T12ServerHelloProducer
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   252
            implements HandshakeProducer {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   253
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   254
        // Prevent instantiation of this class.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   255
        private T12ServerHelloProducer() {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   256
            // blank
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   257
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   258
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   259
        @Override
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   260
        public byte[] produce(ConnectionContext context,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   261
                HandshakeMessage message) throws IOException {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   262
            // The producing happens in server side only.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   263
            ServerHandshakeContext shc = (ServerHandshakeContext)context;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   264
            ClientHelloMessage clientHello = (ClientHelloMessage)message;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   265
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   266
            // If client hasn't specified a session we can resume, start a
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   267
            // new one and choose its cipher suite and compression options,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   268
            // unless new session creation is disabled for this connection!
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   269
            if (!shc.isResumption || shc.resumingSession == null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   270
                if (!shc.sslConfig.enableSessionCreation) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   271
                    throw new SSLException(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   272
                        "Not resumption, and no new session is allowed");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   273
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   274
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   275
                if (shc.localSupportedSignAlgs == null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   276
                    shc.localSupportedSignAlgs =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   277
                        SignatureScheme.getSupportedAlgorithms(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   278
                                shc.algorithmConstraints, shc.activeProtocols);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   279
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   280
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   281
                SSLSessionImpl session =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   282
                        new SSLSessionImpl(shc, CipherSuite.C_NULL);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   283
                session.setMaximumPacketSize(shc.sslConfig.maximumPacketSize);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   284
                shc.handshakeSession = session;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   285
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   286
                // consider the handshake extension impact
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   287
                SSLExtension[] enabledExtensions =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   288
                        shc.sslConfig.getEnabledExtensions(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   289
                            SSLHandshake.CLIENT_HELLO, shc.negotiatedProtocol);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   290
                clientHello.extensions.consumeOnTrade(shc, enabledExtensions);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   291
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   292
                // negotiate the cipher suite.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   293
                KeyExchangeProperties credentials =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   294
                        chooseCipherSuite(shc, clientHello);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   295
                if (credentials == null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   296
                    shc.conContext.fatal(Alert.HANDSHAKE_FAILURE,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   297
                            "no cipher suites in common");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   298
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   299
                    return null;    // make the compiler happy
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   300
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   301
                shc.negotiatedCipherSuite = credentials.cipherSuite;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   302
                shc.handshakeKeyExchange = credentials.keyExchange;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   303
                shc.handshakeSession.setSuite(credentials.cipherSuite);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   304
                shc.handshakePossessions.addAll(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   305
                        Arrays.asList(credentials.possessions));
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   306
                shc.handshakeHash.determine(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   307
                        shc.negotiatedProtocol, shc.negotiatedCipherSuite);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   308
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   309
                // Check the incoming OCSP stapling extensions and attempt
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   310
                // to get responses.  If the resulting stapleParams is non
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   311
                // null, it implies that stapling is enabled on the server side.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   312
                shc.stapleParams = StatusResponseManager.processStapling(shc);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   313
                shc.staplingActive = (shc.stapleParams != null);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   314
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   315
                // update the responders
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   316
                SSLKeyExchange ke = credentials.keyExchange;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   317
                if (ke != null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   318
                    for (Map.Entry<Byte, HandshakeProducer> me :
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   319
                            ke.getHandshakeProducers(shc)) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   320
                        shc.handshakeProducers.put(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   321
                                me.getKey(), me.getValue());
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   322
                    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   323
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   324
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   325
                if ((ke != null) &&
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   326
                        (shc.sslConfig.clientAuthType !=
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   327
                                ClientAuthType.CLIENT_AUTH_NONE) &&
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   328
                        !shc.negotiatedCipherSuite.isAnonymous()) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   329
                    for (SSLHandshake hs :
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   330
                            ke.getRelatedHandshakers(shc)) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   331
                        if (hs == SSLHandshake.CERTIFICATE) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   332
                            shc.handshakeProducers.put(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   333
                                    SSLHandshake.CERTIFICATE_REQUEST.id,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   334
                                    SSLHandshake.CERTIFICATE_REQUEST);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   335
                            break;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   336
                        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   337
                    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   338
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   339
                shc.handshakeProducers.put(SSLHandshake.SERVER_HELLO_DONE.id,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   340
                        SSLHandshake.SERVER_HELLO_DONE);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   341
            } else {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   342
                shc.handshakeSession = shc.resumingSession;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   343
                shc.negotiatedProtocol =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   344
                        shc.resumingSession.getProtocolVersion();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   345
                shc.negotiatedCipherSuite = shc.resumingSession.getSuite();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   346
                shc.handshakeHash.determine(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   347
                        shc.negotiatedProtocol, shc.negotiatedCipherSuite);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   348
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   349
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   350
            // Generate the ServerHello handshake message.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   351
            ServerHelloMessage shm = new ServerHelloMessage(shc,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   352
                    shc.negotiatedProtocol,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   353
                    shc.handshakeSession.getSessionId(),
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   354
                    shc.negotiatedCipherSuite,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   355
                    new RandomCookie(shc),
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   356
                    clientHello);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   357
            shc.serverHelloRandom = shm.serverRandom;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   358
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   359
            // Produce extensions for ServerHello handshake message.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   360
            SSLExtension[] serverHelloExtensions =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   361
                shc.sslConfig.getEnabledExtensions(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   362
                        SSLHandshake.SERVER_HELLO, shc.negotiatedProtocol);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   363
            shm.extensions.produce(shc, serverHelloExtensions);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   364
            if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   365
                SSLLogger.fine("Produced ServerHello handshake message", shm);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   366
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   367
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   368
            // Output the handshake message.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   369
            shm.write(shc.handshakeOutput);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   370
            shc.handshakeOutput.flush();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   371
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   372
            if (shc.isResumption && shc.resumingSession != null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   373
                SSLTrafficKeyDerivation kdg =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   374
                    SSLTrafficKeyDerivation.valueOf(shc.negotiatedProtocol);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   375
                if (kdg == null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   376
                    // unlikely
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   377
                    shc.conContext.fatal(Alert.INTERNAL_ERROR,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   378
                            "Not supported key derivation: " +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   379
                            shc.negotiatedProtocol);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   380
                } else {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   381
                    shc.handshakeKeyDerivation = kdg.createKeyDerivation(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   382
                            shc, shc.resumingSession.getMasterSecret());
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   383
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   384
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   385
                // update the responders
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   386
                shc.handshakeProducers.put(SSLHandshake.FINISHED.id,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   387
                        SSLHandshake.FINISHED);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   388
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   389
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   390
            // The handshake message has been delivered.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   391
            return null;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   392
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   393
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   394
        private static KeyExchangeProperties chooseCipherSuite(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   395
                ServerHandshakeContext shc,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   396
                ClientHelloMessage clientHello) throws IOException {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   397
            List<CipherSuite> preferred;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   398
            List<CipherSuite> proposed;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   399
            if (shc.sslConfig.preferLocalCipherSuites) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   400
                preferred = shc.activeCipherSuites;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   401
                proposed = clientHello.cipherSuites;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   402
            } else {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   403
                preferred = clientHello.cipherSuites;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   404
                proposed = shc.activeCipherSuites;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   405
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   406
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   407
            List<CipherSuite> legacySuites = new LinkedList<>();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   408
            for (CipherSuite cs : preferred) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   409
                if (!HandshakeContext.isNegotiable(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   410
                        proposed, shc.negotiatedProtocol, cs)) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   411
                    continue;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   412
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   413
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   414
                if (shc.sslConfig.clientAuthType ==
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   415
                        ClientAuthType.CLIENT_AUTH_REQUIRED) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   416
                    if ((cs.keyExchange == KeyExchange.K_DH_ANON) ||
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   417
                        (cs.keyExchange == KeyExchange.K_ECDH_ANON)) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   418
                        continue;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   419
                    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   420
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   421
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   422
                SSLKeyExchange ke = SSLKeyExchange.valueOf(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   423
                        cs.keyExchange, shc.negotiatedProtocol);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   424
                if (ke == null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   425
                    continue;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   426
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   427
                if (!ServerHandshakeContext.legacyAlgorithmConstraints.permits(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   428
                        null, cs.name, null)) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   429
                    legacySuites.add(cs);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   430
                    continue;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   431
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   432
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   433
                SSLPossession[] hcds = ke.createPossessions(shc);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   434
                if ((hcds == null) || (hcds.length == 0)) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   435
                    continue;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   436
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   437
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   438
                // The cipher suite has been negotiated.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   439
                if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   440
                    SSLLogger.fine("use cipher suite " + cs.name);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   441
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   442
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   443
                return new KeyExchangeProperties(cs, ke, hcds);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   444
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   445
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   446
            for (CipherSuite cs : legacySuites) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   447
                SSLKeyExchange ke = SSLKeyExchange.valueOf(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   448
                        cs.keyExchange,  shc.negotiatedProtocol);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   449
                if (ke != null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   450
                    SSLPossession[] hcds = ke.createPossessions(shc);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   451
                    if ((hcds != null) && (hcds.length != 0)) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   452
                        if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   453
                            SSLLogger.warning(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   454
                                "use legacy cipher suite " + cs.name);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   455
                        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   456
                        return new KeyExchangeProperties(cs, ke, hcds);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   457
                    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   458
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   459
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   460
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   461
            shc.conContext.fatal(Alert.HANDSHAKE_FAILURE,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   462
                    "no cipher suites in common");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   463
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   464
            return null;    // make the compiler happy.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   465
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   466
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   467
        private static final class KeyExchangeProperties {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   468
            final CipherSuite cipherSuite;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   469
            final SSLKeyExchange keyExchange;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   470
            final SSLPossession[] possessions;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   471
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   472
            private KeyExchangeProperties(CipherSuite cipherSuite,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   473
                    SSLKeyExchange keyExchange, SSLPossession[] possessions) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   474
                this.cipherSuite = cipherSuite;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   475
                this.keyExchange = keyExchange;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   476
                this.possessions = possessions;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   477
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   478
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   479
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   480
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   481
    /**
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   482
     * The "ServerHello" handshake message producer.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   483
     */
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   484
    private static final
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   485
            class T13ServerHelloProducer implements HandshakeProducer {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   486
        // Prevent instantiation of this class.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   487
        private T13ServerHelloProducer() {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   488
            // blank
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   489
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   490
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   491
        @Override
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   492
        public byte[] produce(ConnectionContext context,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   493
                HandshakeMessage message) throws IOException {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   494
            // The producing happens in server side only.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   495
            ServerHandshakeContext shc = (ServerHandshakeContext)context;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   496
            ClientHelloMessage clientHello = (ClientHelloMessage)message;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   497
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   498
            // If client hasn't specified a session we can resume, start a
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   499
            // new one and choose its cipher suite and compression options,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   500
            // unless new session creation is disabled for this connection!
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   501
            if (!shc.isResumption || shc.resumingSession == null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   502
                if (!shc.sslConfig.enableSessionCreation) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   503
                    throw new SSLException(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   504
                        "Not resumption, and no new session is allowed");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   505
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   506
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   507
                if (shc.localSupportedSignAlgs == null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   508
                    shc.localSupportedSignAlgs =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   509
                        SignatureScheme.getSupportedAlgorithms(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   510
                                shc.algorithmConstraints, shc.activeProtocols);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   511
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   512
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   513
                SSLSessionImpl session =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   514
                        new SSLSessionImpl(shc, CipherSuite.C_NULL);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   515
                session.setMaximumPacketSize(shc.sslConfig.maximumPacketSize);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   516
                shc.handshakeSession = session;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   517
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   518
                // consider the handshake extension impact
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   519
                SSLExtension[] enabledExtensions =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   520
                        shc.sslConfig.getEnabledExtensions(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   521
                            SSLHandshake.CLIENT_HELLO, shc.negotiatedProtocol);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   522
                clientHello.extensions.consumeOnTrade(shc, enabledExtensions);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   523
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   524
                // negotiate the cipher suite.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   525
                CipherSuite cipherSuite = chooseCipherSuite(shc, clientHello);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   526
                if (cipherSuite == null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   527
                    shc.conContext.fatal(Alert.HANDSHAKE_FAILURE,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   528
                            "no cipher suites in common");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   529
                    return null;    // make the compiler happy
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   530
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   531
                shc.negotiatedCipherSuite = cipherSuite;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   532
                shc.handshakeSession.setSuite(cipherSuite);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   533
                shc.handshakeHash.determine(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   534
                        shc.negotiatedProtocol, shc.negotiatedCipherSuite);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   535
            } else {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   536
                shc.handshakeSession = shc.resumingSession;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   537
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   538
                // consider the handshake extension impact
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   539
                SSLExtension[] enabledExtensions =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   540
                shc.sslConfig.getEnabledExtensions(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   541
                SSLHandshake.CLIENT_HELLO, shc.negotiatedProtocol);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   542
                clientHello.extensions.consumeOnTrade(shc, enabledExtensions);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   543
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   544
                shc.negotiatedProtocol =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   545
                        shc.resumingSession.getProtocolVersion();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   546
                shc.negotiatedCipherSuite = shc.resumingSession.getSuite();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   547
                shc.handshakeHash.determine(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   548
                        shc.negotiatedProtocol, shc.negotiatedCipherSuite);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   549
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   550
                setUpPskKD(shc,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   551
                        shc.resumingSession.consumePreSharedKey().get());
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   552
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   553
                // The session can't be resumed again---remove it from cache
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   554
                SSLSessionContextImpl sessionCache = (SSLSessionContextImpl)
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   555
                    shc.sslContext.engineGetServerSessionContext();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   556
                sessionCache.remove(shc.resumingSession.getSessionId());
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   557
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   558
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   559
            // update the responders
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   560
            shc.handshakeProducers.put(SSLHandshake.ENCRYPTED_EXTENSIONS.id,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   561
                    SSLHandshake.ENCRYPTED_EXTENSIONS);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   562
            shc.handshakeProducers.put(SSLHandshake.FINISHED.id,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   563
                    SSLHandshake.FINISHED);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   564
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   565
            // Generate the ServerHello handshake message.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   566
            ServerHelloMessage shm = new ServerHelloMessage(shc,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   567
                    ProtocolVersion.TLS12,      // use legacy version
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   568
                    clientHello.sessionId,      // echo back
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   569
                    shc.negotiatedCipherSuite,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   570
                    new RandomCookie(shc),
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   571
                    clientHello);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   572
            shc.serverHelloRandom = shm.serverRandom;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   573
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   574
            // Produce extensions for ServerHello handshake message.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   575
            SSLExtension[] serverHelloExtensions =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   576
                    shc.sslConfig.getEnabledExtensions(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   577
                        SSLHandshake.SERVER_HELLO, shc.negotiatedProtocol);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   578
            shm.extensions.produce(shc, serverHelloExtensions);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   579
            if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   580
                SSLLogger.fine("Produced ServerHello handshake message", shm);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   581
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   582
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   583
            // Output the handshake message.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   584
            shm.write(shc.handshakeOutput);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   585
            shc.handshakeOutput.flush();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   586
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   587
            // Change client/server handshake traffic secrets.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   588
            // Refresh handshake hash
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   589
            shc.handshakeHash.update();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   590
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   591
            // Change client/server handshake traffic secrets.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   592
            SSLKeyExchange ke = shc.handshakeKeyExchange;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   593
            if (ke == null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   594
                // unlikely
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   595
                shc.conContext.fatal(Alert.INTERNAL_ERROR,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   596
                        "Not negotiated key shares");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   597
                return null;    // make the compiler happy
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   598
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   599
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   600
            SSLKeyDerivation handshakeKD = ke.createKeyDerivation(shc);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   601
            SecretKey handshakeSecret = handshakeKD.deriveKey(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   602
                    "TlsHandshakeSecret", null);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   603
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   604
            SSLTrafficKeyDerivation kdg =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   605
                SSLTrafficKeyDerivation.valueOf(shc.negotiatedProtocol);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   606
            if (kdg == null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   607
                // unlikely
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   608
                shc.conContext.fatal(Alert.INTERNAL_ERROR,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   609
                        "Not supported key derivation: " +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   610
                        shc.negotiatedProtocol);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   611
                return null;    // make the compiler happy
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   612
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   613
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   614
            SSLKeyDerivation kd =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   615
                    new SSLSecretDerivation(shc, handshakeSecret);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   616
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   617
            // update the handshake traffic read keys.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   618
            SecretKey readSecret = kd.deriveKey(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   619
                    "TlsClientHandshakeTrafficSecret", null);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   620
            SSLKeyDerivation readKD =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   621
                    kdg.createKeyDerivation(shc, readSecret);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   622
            SecretKey readKey = readKD.deriveKey(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   623
                    "TlsKey", null);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   624
            SecretKey readIvSecret = readKD.deriveKey(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   625
                    "TlsIv", null);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   626
            IvParameterSpec readIv =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   627
                    new IvParameterSpec(readIvSecret.getEncoded());
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   628
            SSLReadCipher readCipher;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   629
            try {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   630
                readCipher =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   631
                    shc.negotiatedCipherSuite.bulkCipher.createReadCipher(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   632
                        Authenticator.valueOf(shc.negotiatedProtocol),
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   633
                        shc.negotiatedProtocol, readKey, readIv,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   634
                        shc.sslContext.getSecureRandom());
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   635
            } catch (GeneralSecurityException gse) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   636
                // unlikely
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   637
                shc.conContext.fatal(Alert.HANDSHAKE_FAILURE,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   638
                        "Missing cipher algorithm", gse);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   639
                return null;    // make the compiler happy
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   640
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   641
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   642
            shc.baseReadSecret = readSecret;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   643
            shc.conContext.inputRecord.changeReadCiphers(readCipher);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   644
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   645
            // update the handshake traffic write secret.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   646
            SecretKey writeSecret = kd.deriveKey(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   647
                    "TlsServerHandshakeTrafficSecret", null);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   648
            SSLKeyDerivation writeKD =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   649
                    kdg.createKeyDerivation(shc, writeSecret);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   650
            SecretKey writeKey = writeKD.deriveKey(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   651
                    "TlsKey", null);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   652
            SecretKey writeIvSecret = writeKD.deriveKey(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   653
                    "TlsIv", null);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   654
            IvParameterSpec writeIv =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   655
                    new IvParameterSpec(writeIvSecret.getEncoded());
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   656
            SSLWriteCipher writeCipher;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   657
            try {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   658
                writeCipher =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   659
                    shc.negotiatedCipherSuite.bulkCipher.createWriteCipher(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   660
                        Authenticator.valueOf(shc.negotiatedProtocol),
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   661
                        shc.negotiatedProtocol, writeKey, writeIv,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   662
                        shc.sslContext.getSecureRandom());
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   663
            } catch (GeneralSecurityException gse) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   664
                // unlikely
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   665
                shc.conContext.fatal(Alert.HANDSHAKE_FAILURE,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   666
                        "Missing cipher algorithm", gse);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   667
                return null;    //  make the compiler happy
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   668
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   669
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   670
            shc.baseWriteSecret = writeSecret;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   671
            shc.conContext.outputRecord.changeWriteCiphers(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   672
                    writeCipher, (clientHello.sessionId.length() != 0));
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   673
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   674
            // Update the context for master key derivation.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   675
            shc.handshakeKeyDerivation = kd;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   676
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   677
            // The handshake message has been delivered.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   678
            return null;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   679
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   680
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   681
        private static CipherSuite chooseCipherSuite(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   682
                ServerHandshakeContext shc,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   683
                ClientHelloMessage clientHello) throws IOException {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   684
            List<CipherSuite> preferred;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   685
            List<CipherSuite> proposed;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   686
            if (shc.sslConfig.preferLocalCipherSuites) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   687
                preferred = shc.activeCipherSuites;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   688
                proposed = clientHello.cipherSuites;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   689
            } else {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   690
                preferred = clientHello.cipherSuites;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   691
                proposed = shc.activeCipherSuites;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   692
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   693
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   694
            CipherSuite legacySuite = null;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   695
            AlgorithmConstraints legacyConstraints =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   696
                    ServerHandshakeContext.legacyAlgorithmConstraints;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   697
            for (CipherSuite cs : preferred) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   698
                if (!HandshakeContext.isNegotiable(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   699
                        proposed, shc.negotiatedProtocol, cs)) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   700
                    continue;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   701
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   702
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   703
                if ((legacySuite == null) &&
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   704
                        !legacyConstraints.permits(null, cs.name, null)) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   705
                    legacySuite = cs;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   706
                    continue;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   707
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   708
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   709
                // The cipher suite has been negotiated.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   710
                if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   711
                    SSLLogger.fine("use cipher suite " + cs.name);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   712
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   713
                return cs;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   714
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   715
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   716
            if (legacySuite != null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   717
                if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   718
                    SSLLogger.warning(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   719
                            "use legacy cipher suite " + legacySuite.name);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   720
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   721
                return legacySuite;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   722
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   723
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   724
            // no cipher suites in common
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   725
            return null;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   726
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   727
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   728
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   729
    /**
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   730
     * The "HelloRetryRequest" handshake message producer.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   731
     */
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   732
    private static final
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   733
            class T13HelloRetryRequestProducer implements HandshakeProducer {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   734
        // Prevent instantiation of this class.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   735
        private T13HelloRetryRequestProducer() {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   736
            // blank
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   737
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   738
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   739
        @Override
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   740
        public byte[] produce(ConnectionContext context,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   741
                HandshakeMessage message) throws IOException {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   742
            ServerHandshakeContext shc = (ServerHandshakeContext) context;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   743
            ClientHelloMessage clientHello = (ClientHelloMessage) message;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   744
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   745
            // negotiate the cipher suite.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   746
            CipherSuite cipherSuite =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   747
                    T13ServerHelloProducer.chooseCipherSuite(shc, clientHello);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   748
            if (cipherSuite == null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   749
                shc.conContext.fatal(Alert.HANDSHAKE_FAILURE,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   750
                        "no cipher suites in common for hello retry request");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   751
                return null;    // make the compiler happy
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   752
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   753
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   754
            ServerHelloMessage hhrm = new ServerHelloMessage(shc,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   755
                    ProtocolVersion.TLS12,      // use legacy version
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   756
                    clientHello.sessionId,      //  echo back
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   757
                    cipherSuite,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   758
                    RandomCookie.hrrRandom,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   759
                    clientHello
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   760
            );
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   761
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   762
            shc.negotiatedCipherSuite = cipherSuite;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   763
            shc.handshakeHash.determine(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   764
                    shc.negotiatedProtocol, shc.negotiatedCipherSuite);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   765
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   766
            // Produce extensions for HelloRetryRequest handshake message.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   767
            SSLExtension[] serverHelloExtensions =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   768
                shc.sslConfig.getEnabledExtensions(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   769
                    SSLHandshake.HELLO_RETRY_REQUEST, shc.negotiatedProtocol);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   770
            hhrm.extensions.produce(shc, serverHelloExtensions);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   771
            if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   772
                SSLLogger.fine(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   773
                        "Produced HelloRetryRequest handshake message", hhrm);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   774
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   775
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   776
            // Output the handshake message.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   777
            hhrm.write(shc.handshakeOutput);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   778
            shc.handshakeOutput.flush();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   779
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   780
            // Stateless, shall we clean up the handshake context as well?
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   781
            shc.handshakeHash.finish();     // forgot about the handshake hash
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   782
            shc.handshakeExtensions.clear();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   783
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   784
            // What's the expected response?
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   785
            shc.handshakeConsumers.put(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   786
                    SSLHandshake.CLIENT_HELLO.id, SSLHandshake.CLIENT_HELLO);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   787
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   788
            // The handshake message has been delivered.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   789
            return null;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   790
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   791
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   792
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   793
    /**
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   794
     * The "HelloRetryRequest" handshake message reproducer.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   795
     */
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   796
    private static final
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   797
            class T13HelloRetryRequestReproducer implements HandshakeProducer {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   798
        // Prevent instantiation of this class.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   799
        private T13HelloRetryRequestReproducer() {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   800
            // blank
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   801
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   802
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   803
        @Override
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   804
        public byte[] produce(ConnectionContext context,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   805
                HandshakeMessage message) throws IOException {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   806
            ServerHandshakeContext shc = (ServerHandshakeContext) context;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   807
            ClientHelloMessage clientHello = (ClientHelloMessage) message;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   808
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   809
            // negotiate the cipher suite.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   810
            CipherSuite cipherSuite = shc.negotiatedCipherSuite;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   811
            ServerHelloMessage hhrm = new ServerHelloMessage(shc,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   812
                    ProtocolVersion.TLS12,      // use legacy version
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   813
                    clientHello.sessionId,      //  echo back
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   814
                    cipherSuite,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   815
                    RandomCookie.hrrRandom,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   816
                    clientHello
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   817
            );
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   818
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   819
            // Produce extensions for HelloRetryRequest handshake message.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   820
            SSLExtension[] serverHelloExtensions =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   821
                shc.sslConfig.getEnabledExtensions(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   822
                    SSLHandshake.MESSAGE_HASH, shc.negotiatedProtocol);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   823
            hhrm.extensions.produce(shc, serverHelloExtensions);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   824
            if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   825
                SSLLogger.fine(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   826
                        "Reproduced HelloRetryRequest handshake message", hhrm);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   827
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   828
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   829
            HandshakeOutStream hos = new HandshakeOutStream(null);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   830
            hhrm.write(hos);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   831
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   832
            return hos.toByteArray();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   833
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   834
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   835
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   836
    /**
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   837
     * The "ServerHello" handshake message consumer.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   838
     */
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   839
    private static final
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   840
            class ServerHelloConsumer implements SSLConsumer {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   841
        // Prevent instantiation of this class.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   842
        private ServerHelloConsumer() {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   843
            // blank
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   844
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   845
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   846
        @Override
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   847
        public void consume(ConnectionContext context,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   848
                ByteBuffer message) throws IOException {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   849
            // The consuming happens in client side only.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   850
            ClientHandshakeContext chc = (ClientHandshakeContext)context;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   851
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   852
            // clean up this consumer
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   853
            chc.handshakeConsumers.remove(SSLHandshake.SERVER_HELLO.id);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   854
            if (!chc.handshakeConsumers.isEmpty()) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   855
                // DTLS 1.0/1.2
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   856
                chc.handshakeConsumers.remove(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   857
                        SSLHandshake.HELLO_VERIFY_REQUEST.id);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   858
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   859
            if (!chc.handshakeConsumers.isEmpty()) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   860
                chc.conContext.fatal(Alert.UNEXPECTED_MESSAGE,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   861
                    "No more message expected before ServerHello is processed");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   862
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   863
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   864
            ServerHelloMessage shm = new ServerHelloMessage(chc, message);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   865
            if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   866
                SSLLogger.fine("Consuming ServerHello handshake message", shm);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   867
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   868
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   869
            if (shm.serverRandom.isHelloRetryRequest()) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   870
                onHelloRetryRequest(chc, shm);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   871
            } else {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   872
                onServerHello(chc, shm);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   873
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   874
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   875
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   876
        private void onHelloRetryRequest(ClientHandshakeContext chc,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   877
                ServerHelloMessage helloRetryRequest) throws IOException {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   878
            // Negotiate protocol version.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   879
            //
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   880
            // Check and launch SupportedVersions.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   881
            SSLExtension[] extTypes = new SSLExtension[] {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   882
                    SSLExtension.HRR_SUPPORTED_VERSIONS
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   883
                };
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   884
            helloRetryRequest.extensions.consumeOnLoad(chc, extTypes);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   885
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   886
            ProtocolVersion serverVersion;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   887
            SHSupportedVersionsSpec svs =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   888
                    (SHSupportedVersionsSpec)chc.handshakeExtensions.get(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   889
                            SSLExtension.HRR_SUPPORTED_VERSIONS);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   890
            if (svs != null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   891
                serverVersion =            // could be null
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   892
                        ProtocolVersion.valueOf(svs.selectedVersion);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   893
            } else {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   894
                serverVersion = helloRetryRequest.serverVersion;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   895
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   896
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   897
            if (!chc.activeProtocols.contains(serverVersion)) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   898
                chc.conContext.fatal(Alert.PROTOCOL_VERSION,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   899
                    "The server selected protocol version " + serverVersion +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   900
                    " is not accepted by client preferences " +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   901
                    chc.activeProtocols);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   902
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   903
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   904
            if (!serverVersion.useTLS13PlusSpec()) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   905
                chc.conContext.fatal(Alert.PROTOCOL_VERSION,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   906
                    "Unexpected HelloRetryRequest for " + serverVersion.name);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   907
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   908
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   909
            chc.negotiatedProtocol = serverVersion;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   910
            if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   911
                SSLLogger.fine(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   912
                    "Negotiated protocol version: " + serverVersion.name);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   913
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   914
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   915
            // TLS 1.3 key share extension may have produced client
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   916
            // possessions for TLS 1.3 key exchanges.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   917
            //
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   918
            // Clean up before producing new client key share possessions.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   919
            chc.handshakePossessions.clear();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   920
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   921
            if (serverVersion.isDTLS) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   922
                d13HrrHandshakeConsumer.consume(chc, helloRetryRequest);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   923
            } else {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   924
                t13HrrHandshakeConsumer.consume(chc, helloRetryRequest);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   925
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   926
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   927
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   928
        private void onServerHello(ClientHandshakeContext chc,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   929
                ServerHelloMessage serverHello) throws IOException {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   930
            // Negotiate protocol version.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   931
            //
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   932
            // Check and launch SupportedVersions.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   933
            SSLExtension[] extTypes = new SSLExtension[] {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   934
                    SSLExtension.SH_SUPPORTED_VERSIONS
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   935
                };
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   936
            serverHello.extensions.consumeOnLoad(chc, extTypes);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   937
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   938
            ProtocolVersion serverVersion;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   939
            SHSupportedVersionsSpec svs =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   940
                    (SHSupportedVersionsSpec)chc.handshakeExtensions.get(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   941
                            SSLExtension.SH_SUPPORTED_VERSIONS);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   942
            if (svs != null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   943
                serverVersion =            // could be null
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   944
                        ProtocolVersion.valueOf(svs.selectedVersion);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   945
            } else {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   946
                serverVersion = serverHello.serverVersion;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   947
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   948
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   949
            if (!chc.activeProtocols.contains(serverVersion)) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   950
                chc.conContext.fatal(Alert.PROTOCOL_VERSION,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   951
                    "The server selected protocol version " + serverVersion +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   952
                    " is not accepted by client preferences " +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   953
                    chc.activeProtocols);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   954
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   955
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   956
            chc.negotiatedProtocol = serverVersion;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   957
            if (!chc.conContext.isNegotiated) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   958
                chc.conContext.protocolVersion = chc.negotiatedProtocol;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   959
                chc.conContext.outputRecord.setVersion(chc.negotiatedProtocol);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   960
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   961
            if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   962
                SSLLogger.fine(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   963
                    "Negotiated protocol version: " + serverVersion.name);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   964
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   965
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   966
            if (serverHello.serverRandom.isVersionDowngrade(chc)) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   967
                chc.conContext.fatal(Alert.ILLEGAL_PARAMETER,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   968
                    "A potential protocol version downgrade attack");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   969
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   970
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   971
            // Consume the handshake message for the specific protocol version.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   972
            if (serverVersion.isDTLS) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   973
                if (serverVersion.useTLS13PlusSpec()) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   974
                    d13HandshakeConsumer.consume(chc, serverHello);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   975
                } else {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   976
                    // TLS 1.3 key share extension may have produced client
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   977
                    // possessions for TLS 1.3 key exchanges.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   978
                    chc.handshakePossessions.clear();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   979
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   980
                    d12HandshakeConsumer.consume(chc, serverHello);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   981
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   982
            } else {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   983
                if (serverVersion.useTLS13PlusSpec()) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   984
                    t13HandshakeConsumer.consume(chc, serverHello);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   985
                } else {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   986
                    // TLS 1.3 key share extension may have produced client
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   987
                    // possessions for TLS 1.3 key exchanges.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   988
                    chc.handshakePossessions.clear();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   989
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   990
                    t12HandshakeConsumer.consume(chc, serverHello);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   991
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   992
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   993
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   994
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   995
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   996
    private static final
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   997
            class T12ServerHelloConsumer implements HandshakeConsumer {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   998
        // Prevent instantiation of this class.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
   999
        private T12ServerHelloConsumer() {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1000
            // blank
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1001
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1002
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1003
        @Override
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1004
        public void consume(ConnectionContext context,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1005
                HandshakeMessage message) throws IOException {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1006
            // The consuming happens in client side only.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1007
            ClientHandshakeContext chc = (ClientHandshakeContext)context;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1008
            ServerHelloMessage serverHello = (ServerHelloMessage)message;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1009
            if (!chc.isNegotiable(serverHello.serverVersion)) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1010
                chc.conContext.fatal(Alert.PROTOCOL_VERSION,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1011
                    "Server chose " + serverHello.serverVersion +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1012
                    ", but that protocol version is not enabled or " +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1013
                    "not supported by the client.");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1014
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1015
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1016
            // chc.negotiatedProtocol = serverHello.serverVersion;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1017
            chc.negotiatedCipherSuite = serverHello.cipherSuite;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1018
            chc.handshakeHash.determine(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1019
                    chc.negotiatedProtocol, chc.negotiatedCipherSuite);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1020
            chc.serverHelloRandom = serverHello.serverRandom;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1021
            if (chc.negotiatedCipherSuite.keyExchange == null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1022
                chc.conContext.fatal(Alert.PROTOCOL_VERSION,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1023
                    "TLS 1.2 or prior version does not support the " +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1024
                    "server cipher suite: " + chc.negotiatedCipherSuite.name);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1025
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1026
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1027
            //
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1028
            // validate
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1029
            //
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1030
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1031
            // Check and launch the "renegotiation_info" extension.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1032
            SSLExtension[] extTypes = new SSLExtension[] {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1033
                    SSLExtension.SH_RENEGOTIATION_INFO
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1034
                };
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1035
            serverHello.extensions.consumeOnLoad(chc, extTypes);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1036
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1037
            // Is it session resuming?
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1038
            if (chc.resumingSession != null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1039
                // we tried to resume, let's see what the server decided
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1040
                if (serverHello.sessionId.equals(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1041
                        chc.resumingSession.getSessionId())) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1042
                    // server resumed the session, let's make sure everything
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1043
                    // checks out
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1044
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1045
                    // Verify that the session ciphers are unchanged.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1046
                    CipherSuite sessionSuite = chc.resumingSession.getSuite();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1047
                    if (chc.negotiatedCipherSuite != sessionSuite) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1048
                        chc.conContext.fatal(Alert.PROTOCOL_VERSION,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1049
                            "Server returned wrong cipher suite for session");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1050
                    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1051
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1052
                    // verify protocol version match
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1053
                    ProtocolVersion sessionVersion =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1054
                            chc.resumingSession.getProtocolVersion();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1055
                    if (chc.negotiatedProtocol != sessionVersion) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1056
                        chc.conContext.fatal(Alert.PROTOCOL_VERSION,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1057
                            "Server resumed with wrong protocol version");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1058
                    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1059
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1060
                    // looks fine;  resume it.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1061
                    chc.isResumption = true;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1062
                    chc.resumingSession.setAsSessionResumption(true);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1063
                    chc.handshakeSession = chc.resumingSession;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1064
                } else {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1065
                    // we wanted to resume, but the server refused
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1066
                    //
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1067
                    // Invalidate the session for initial handshake in case
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1068
                    // of reusing next time.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1069
                    if (chc.resumingSession != null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1070
                        chc.resumingSession.invalidate();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1071
                        chc.resumingSession = null;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1072
                    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1073
                    chc.isResumption = false;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1074
                    if (!chc.sslConfig.enableSessionCreation) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1075
                        chc.conContext.fatal(Alert.PROTOCOL_VERSION,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1076
                            "New session creation is disabled");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1077
                    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1078
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1079
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1080
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1081
            // Check and launch ClientHello extensions.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1082
            extTypes = chc.sslConfig.getEnabledExtensions(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1083
                    SSLHandshake.SERVER_HELLO);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1084
            serverHello.extensions.consumeOnLoad(chc, extTypes);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1085
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1086
            if (!chc.isResumption) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1087
                if (chc.resumingSession != null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1088
                    // in case the resumption happens next time.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1089
                    chc.resumingSession.invalidate();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1090
                    chc.resumingSession = null;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1091
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1092
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1093
                if (!chc.sslConfig.enableSessionCreation) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1094
                    chc.conContext.fatal(Alert.PROTOCOL_VERSION,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1095
                        "New session creation is disabled");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1096
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1097
                chc.handshakeSession = new SSLSessionImpl(chc,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1098
                        chc.negotiatedCipherSuite,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1099
                        serverHello.sessionId);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1100
                chc.handshakeSession.setMaximumPacketSize(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1101
                        chc.sslConfig.maximumPacketSize);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1102
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1103
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1104
            //
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1105
            // update
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1106
            //
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1107
            serverHello.extensions.consumeOnTrade(chc, extTypes);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1108
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1109
            // update the consumers and producers
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1110
            if (chc.isResumption) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1111
                SSLTrafficKeyDerivation kdg =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1112
                        SSLTrafficKeyDerivation.valueOf(chc.negotiatedProtocol);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1113
                if (kdg == null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1114
                    // unlikely
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1115
                    chc.conContext.fatal(Alert.INTERNAL_ERROR,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1116
                            "Not supported key derivation: " +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1117
                            chc.negotiatedProtocol);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1118
                } else {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1119
                    chc.handshakeKeyDerivation = kdg.createKeyDerivation(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1120
                            chc, chc.resumingSession.getMasterSecret());
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1121
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1122
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1123
                chc.conContext.consumers.putIfAbsent(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1124
                        ContentType.CHANGE_CIPHER_SPEC.id,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1125
                        ChangeCipherSpec.t10Consumer);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1126
                chc.handshakeConsumers.put(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1127
                        SSLHandshake.FINISHED.id,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1128
                        SSLHandshake.FINISHED);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1129
            } else {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1130
                SSLKeyExchange ke = SSLKeyExchange.valueOf(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1131
                        chc.negotiatedCipherSuite.keyExchange,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1132
                        chc.negotiatedProtocol);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1133
                chc.handshakeKeyExchange = ke;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1134
                if (ke != null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1135
                    for (SSLHandshake handshake :
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1136
                            ke.getRelatedHandshakers(chc)) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1137
                        chc.handshakeConsumers.put(handshake.id, handshake);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1138
                    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1139
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1140
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1141
                chc.handshakeConsumers.put(SSLHandshake.SERVER_HELLO_DONE.id,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1142
                        SSLHandshake.SERVER_HELLO_DONE);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1143
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1144
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1145
            //
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1146
            // produce
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1147
            //
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1148
            // Need no new handshake message producers here.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1149
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1150
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1151
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1152
    private static void setUpPskKD(HandshakeContext hc,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1153
            SecretKey psk) throws SSLHandshakeException {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1154
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1155
        if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1156
            SSLLogger.fine("Using PSK to derive early secret");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1157
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1158
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1159
        try {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1160
            CipherSuite.HashAlg hashAlg = hc.negotiatedCipherSuite.hashAlg;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1161
            HKDF hkdf = new HKDF(hashAlg.name);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1162
            byte[] zeros = new byte[hashAlg.hashLength];
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1163
            SecretKey earlySecret = hkdf.extract(zeros, psk, "TlsEarlySecret");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1164
            hc.handshakeKeyDerivation =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1165
                    new SSLSecretDerivation(hc, earlySecret);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1166
        } catch  (GeneralSecurityException gse) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1167
            throw (SSLHandshakeException) new SSLHandshakeException(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1168
                "Could not generate secret").initCause(gse);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1169
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1170
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1171
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1172
    private static final
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1173
            class T13ServerHelloConsumer implements HandshakeConsumer {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1174
        // Prevent instantiation of this class.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1175
        private T13ServerHelloConsumer() {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1176
            // blank
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1177
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1178
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1179
        @Override
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1180
        public void consume(ConnectionContext context,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1181
                HandshakeMessage message) throws IOException {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1182
            // The consuming happens in client side only.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1183
            ClientHandshakeContext chc = (ClientHandshakeContext)context;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1184
            ServerHelloMessage serverHello = (ServerHelloMessage)message;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1185
            if (serverHello.serverVersion != ProtocolVersion.TLS12) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1186
                chc.conContext.fatal(Alert.PROTOCOL_VERSION,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1187
                    "The ServerHello.legacy_version field is not TLS 1.2");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1188
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1189
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1190
            chc.negotiatedCipherSuite = serverHello.cipherSuite;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1191
            chc.handshakeHash.determine(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1192
                    chc.negotiatedProtocol, chc.negotiatedCipherSuite);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1193
            chc.serverHelloRandom = serverHello.serverRandom;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1194
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1195
            //
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1196
            // validate
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1197
            //
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1198
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1199
            // Check and launch ServerHello extensions.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1200
            SSLExtension[] extTypes = chc.sslConfig.getEnabledExtensions(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1201
                    SSLHandshake.SERVER_HELLO);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1202
            serverHello.extensions.consumeOnLoad(chc, extTypes);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1203
            if (!chc.isResumption) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1204
                if (chc.resumingSession != null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1205
                    // in case the resumption happens next time.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1206
                    chc.resumingSession.invalidate();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1207
                    chc.resumingSession = null;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1208
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1209
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1210
                if (!chc.sslConfig.enableSessionCreation) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1211
                    chc.conContext.fatal(Alert.PROTOCOL_VERSION,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1212
                        "New session creation is disabled");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1213
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1214
                chc.handshakeSession = new SSLSessionImpl(chc,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1215
                        chc.negotiatedCipherSuite,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1216
                        serverHello.sessionId);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1217
                chc.handshakeSession.setMaximumPacketSize(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1218
                        chc.sslConfig.maximumPacketSize);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1219
            } else {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1220
                // The PSK is consumed to allow it to be deleted
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1221
                Optional<SecretKey> psk =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1222
                        chc.resumingSession.consumePreSharedKey();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1223
                if(!psk.isPresent()) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1224
                    chc.conContext.fatal(Alert.INTERNAL_ERROR,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1225
                    "No PSK available. Unable to resume.");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1226
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1227
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1228
                chc.handshakeSession = chc.resumingSession;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1229
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1230
                setUpPskKD(chc, psk.get());
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1231
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1232
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1233
            //
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1234
            // update
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1235
            //
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1236
            serverHello.extensions.consumeOnTrade(chc, extTypes);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1237
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1238
            // Change client/server handshake traffic secrets.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1239
            // Refresh handshake hash
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1240
            chc.handshakeHash.update();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1241
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1242
            SSLKeyExchange ke = chc.handshakeKeyExchange;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1243
            if (ke == null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1244
                // unlikely
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1245
                chc.conContext.fatal(Alert.INTERNAL_ERROR,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1246
                        "Not negotiated key shares");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1247
                return;     // make the compiler happy
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1248
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1249
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1250
            SSLKeyDerivation handshakeKD = ke.createKeyDerivation(chc);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1251
            SecretKey handshakeSecret = handshakeKD.deriveKey(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1252
                    "TlsHandshakeSecret", null);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1253
            SSLTrafficKeyDerivation kdg =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1254
                SSLTrafficKeyDerivation.valueOf(chc.negotiatedProtocol);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1255
            if (kdg == null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1256
                // unlikely
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1257
                chc.conContext.fatal(Alert.INTERNAL_ERROR,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1258
                        "Not supported key derivation: " +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1259
                        chc.negotiatedProtocol);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1260
                return;     // make the compiler happy
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1261
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1262
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1263
            SSLKeyDerivation secretKD =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1264
                    new SSLSecretDerivation(chc, handshakeSecret);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1265
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1266
            // update the handshake traffic read keys.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1267
            SecretKey readSecret = secretKD.deriveKey(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1268
                    "TlsServerHandshakeTrafficSecret", null);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1269
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1270
            SSLKeyDerivation readKD =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1271
                    kdg.createKeyDerivation(chc, readSecret);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1272
            SecretKey readKey = readKD.deriveKey(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1273
                    "TlsKey", null);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1274
            SecretKey readIvSecret = readKD.deriveKey(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1275
                    "TlsIv", null);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1276
            IvParameterSpec readIv =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1277
                    new IvParameterSpec(readIvSecret.getEncoded());
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1278
            SSLReadCipher readCipher;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1279
            try {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1280
                readCipher =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1281
                    chc.negotiatedCipherSuite.bulkCipher.createReadCipher(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1282
                        Authenticator.valueOf(chc.negotiatedProtocol),
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1283
                        chc.negotiatedProtocol, readKey, readIv,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1284
                        chc.sslContext.getSecureRandom());
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1285
            } catch (GeneralSecurityException gse) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1286
                // unlikely
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1287
                chc.conContext.fatal(Alert.HANDSHAKE_FAILURE,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1288
                        "Missing cipher algorithm", gse);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1289
                return;     // make the compiler happy
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1290
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1291
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1292
            chc.baseReadSecret = readSecret;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1293
            chc.conContext.inputRecord.changeReadCiphers(readCipher);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1294
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1295
            // update the handshake traffic write keys.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1296
            SecretKey writeSecret = secretKD.deriveKey(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1297
                    "TlsClientHandshakeTrafficSecret", null);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1298
            SSLKeyDerivation writeKD =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1299
                    kdg.createKeyDerivation(chc, writeSecret);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1300
            SecretKey writeKey = writeKD.deriveKey(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1301
                    "TlsKey", null);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1302
            SecretKey writeIvSecret = writeKD.deriveKey(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1303
                    "TlsIv", null);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1304
            IvParameterSpec writeIv =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1305
                    new IvParameterSpec(writeIvSecret.getEncoded());
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1306
            SSLWriteCipher writeCipher;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1307
            try {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1308
                writeCipher =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1309
                    chc.negotiatedCipherSuite.bulkCipher.createWriteCipher(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1310
                        Authenticator.valueOf(chc.negotiatedProtocol),
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1311
                        chc.negotiatedProtocol, writeKey, writeIv,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1312
                        chc.sslContext.getSecureRandom());
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1313
            } catch (GeneralSecurityException gse) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1314
                // unlikely
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1315
                chc.conContext.fatal(Alert.HANDSHAKE_FAILURE,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1316
                        "Missing cipher algorithm", gse);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1317
                return;     //  make the compiler happy
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1318
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1319
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1320
            chc.baseWriteSecret = writeSecret;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1321
            chc.conContext.outputRecord.changeWriteCiphers(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1322
                    writeCipher, (serverHello.sessionId.length() != 0));
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1323
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1324
            // Should use resumption_master_secret for TLS 1.3.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1325
            // chc.handshakeSession.setMasterSecret(masterSecret);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1326
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1327
            // Update the context for master key derivation.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1328
            chc.handshakeKeyDerivation = secretKD;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1329
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1330
            // update the consumers and producers
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1331
            //
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1332
            // The server sends a dummy change_cipher_spec record immediately
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1333
            // after its first handshake message.  This may either be after a
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1334
            // ServerHello or a HelloRetryRequest.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1335
            chc.conContext.consumers.putIfAbsent(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1336
                    ContentType.CHANGE_CIPHER_SPEC.id,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1337
                    ChangeCipherSpec.t13Consumer);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1338
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1339
            chc.handshakeConsumers.put(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1340
                    SSLHandshake.ENCRYPTED_EXTENSIONS.id,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1341
                    SSLHandshake.ENCRYPTED_EXTENSIONS);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1342
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1343
            // Support cert authentication only, when not PSK.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1344
            chc.handshakeConsumers.put(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1345
                    SSLHandshake.CERTIFICATE_REQUEST.id,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1346
                    SSLHandshake.CERTIFICATE_REQUEST);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1347
            chc.handshakeConsumers.put(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1348
                    SSLHandshake.CERTIFICATE.id,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1349
                    SSLHandshake.CERTIFICATE);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1350
            chc.handshakeConsumers.put(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1351
                    SSLHandshake.CERTIFICATE_VERIFY.id,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1352
                    SSLHandshake.CERTIFICATE_VERIFY);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1353
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1354
            chc.handshakeConsumers.put(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1355
                    SSLHandshake.FINISHED.id,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1356
                    SSLHandshake.FINISHED);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1357
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1358
            //
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1359
            // produce
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1360
            //
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1361
            // Need no new handshake message producers here.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1362
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1363
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1364
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1365
    private static final
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1366
            class T13HelloRetryRequestConsumer implements HandshakeConsumer {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1367
        // Prevent instantiation of this class.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1368
        private T13HelloRetryRequestConsumer() {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1369
            // blank
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1370
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1371
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1372
        @Override
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1373
        public void consume(ConnectionContext context,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1374
                HandshakeMessage message) throws IOException {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1375
            // The consuming happens in client side only.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1376
            ClientHandshakeContext chc = (ClientHandshakeContext)context;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1377
            ServerHelloMessage helloRetryRequest = (ServerHelloMessage)message;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1378
            if (helloRetryRequest.serverVersion != ProtocolVersion.TLS12) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1379
                chc.conContext.fatal(Alert.PROTOCOL_VERSION,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1380
                    "The HelloRetryRequest.legacy_version is not TLS 1.2");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1381
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1382
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1383
            chc.negotiatedCipherSuite = helloRetryRequest.cipherSuite;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1384
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1385
            //
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1386
            // validate
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1387
            //
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1388
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1389
            // Check and launch ClientHello extensions.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1390
            SSLExtension[] extTypes = chc.sslConfig.getEnabledExtensions(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1391
                    SSLHandshake.HELLO_RETRY_REQUEST);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1392
            helloRetryRequest.extensions.consumeOnLoad(chc, extTypes);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1393
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1394
            //
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1395
            // update
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1396
            //
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1397
            helloRetryRequest.extensions.consumeOnTrade(chc, extTypes);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1398
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1399
            // Change client/server handshake traffic secrets.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1400
            // Refresh handshake hash
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1401
            chc.handshakeHash.finish();     // reset the handshake hash
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1402
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1403
            // calculate the transcript hash of the 1st ClientHello message
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1404
            HandshakeOutStream hos = new HandshakeOutStream(null);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1405
            try {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1406
                chc.initialClientHelloMsg.write(hos);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1407
            } catch (IOException ioe) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1408
                // unlikely
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1409
                chc.conContext.fatal(Alert.HANDSHAKE_FAILURE,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1410
                    "Failed to construct message hash", ioe);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1411
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1412
            chc.handshakeHash.deliver(hos.toByteArray());
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1413
            chc.handshakeHash.determine(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1414
                    chc.negotiatedProtocol, chc.negotiatedCipherSuite);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1415
            byte[] clientHelloHash = chc.handshakeHash.digest();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1416
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1417
            // calculate the message_hash
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1418
            //
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1419
            // Transcript-Hash(ClientHello1, HelloRetryRequest, ... Mn) =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1420
            //   Hash(message_hash ||    /* Handshake type */
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1421
            //     00 00 Hash.length ||  /* Handshake message length (bytes) */
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1422
            //     Hash(ClientHello1) || /* Hash of ClientHello1 */
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1423
            //     HelloRetryRequest || ... || Mn)
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1424
            int hashLen = chc.negotiatedCipherSuite.hashAlg.hashLength;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1425
            byte[] hashedClientHello = new byte[4 + hashLen];
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1426
            hashedClientHello[0] = SSLHandshake.MESSAGE_HASH.id;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1427
            hashedClientHello[1] = (byte)0x00;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1428
            hashedClientHello[2] = (byte)0x00;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1429
            hashedClientHello[3] = (byte)(hashLen & 0xFF);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1430
            System.arraycopy(clientHelloHash, 0,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1431
                    hashedClientHello, 4, hashLen);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1432
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1433
            chc.handshakeHash.finish();     // reset the handshake hash
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1434
            chc.handshakeHash.deliver(hashedClientHello);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1435
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1436
            int hrrBodyLen = helloRetryRequest.handshakeRecord.remaining();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1437
            byte[] hrrMessage = new byte[4 + hrrBodyLen];
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1438
            hrrMessage[0] = SSLHandshake.HELLO_RETRY_REQUEST.id;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1439
            hrrMessage[1] = (byte)((hrrBodyLen >> 16) & 0xFF);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1440
            hrrMessage[2] = (byte)((hrrBodyLen >> 8) & 0xFF);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1441
            hrrMessage[3] = (byte)(hrrBodyLen & 0xFF);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1442
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1443
            ByteBuffer hrrBody = helloRetryRequest.handshakeRecord.duplicate();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1444
            hrrBody.get(hrrMessage, 4, hrrBodyLen);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1445
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1446
            chc.handshakeHash.receive(hrrMessage);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1447
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1448
            // Update the initial ClientHello handshake message.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1449
            chc.initialClientHelloMsg.extensions.reproduce(chc,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1450
                    new SSLExtension[] {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1451
                        SSLExtension.CH_COOKIE,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1452
                        SSLExtension.CH_KEY_SHARE,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1453
                        SSLExtension.CH_PRE_SHARED_KEY
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1454
                    });
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1455
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1456
            //
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1457
            // produce response handshake message
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1458
            //
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1459
            SSLHandshake.CLIENT_HELLO.produce(context, helloRetryRequest);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1460
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1461
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents:
diff changeset
  1462
}