src/java.base/share/classes/sun/security/ssl/TransportContext.java
author xuelei
Tue, 22 May 2018 21:46:47 -0700
branchJDK-8145252-TLS13-branch
changeset 56594 99e0f3f3f0e4
parent 56573 4a53dd94403e
child 56660 66c803c3ce32
permissions -rw-r--r--
close issues introduced in TLS 1.3 implementation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     1
/*
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     4
 *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    10
 *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    15
 * accompanied this code).
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    16
 *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    20
 *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    23
 * questions.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    24
 */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    25
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    26
package sun.security.ssl;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    27
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    28
import java.io.Closeable;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    29
import java.io.IOException;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    30
import java.security.AccessControlContext;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    31
import java.security.AccessController;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    32
import java.security.PrivilegedAction;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    33
import java.util.HashMap;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    34
import java.util.HashSet;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    35
import java.util.List;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    36
import java.util.Map;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    37
import java.util.Set;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    38
import javax.crypto.SecretKey;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    39
import javax.net.ssl.HandshakeCompletedEvent;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    40
import javax.net.ssl.HandshakeCompletedListener;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    41
import javax.net.ssl.SSLEngineResult.HandshakeStatus;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    42
import javax.net.ssl.SSLException;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    43
import javax.net.ssl.SSLSocket;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    44
import sun.security.ssl.SupportedGroupsExtension.NamedGroup;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    45
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    46
/**
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    47
 * SSL/(D)TLS transportation context.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    48
 */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    49
class TransportContext implements ConnectionContext, Closeable {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    50
    final SSLTransport              transport;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    51
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    52
    // registered plaintext consumers
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    53
    final Map<Byte, SSLConsumer>    consumers;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    54
    final AccessControlContext      acc;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    55
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    56
    final SSLContextImpl            sslContext;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    57
    final SSLConfiguration          sslConfig;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    58
    final InputRecord               inputRecord;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    59
    final OutputRecord              outputRecord;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    60
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    61
    // connection status
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    62
    boolean                         isUnsureMode;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    63
    boolean                         isNegotiated;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    64
    boolean                         isBroken;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    65
    boolean                         isInputCloseNotified;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    66
    boolean                         isOutputCloseNotified;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    67
    Exception                       closeReason;    //SSLException or RuntimeException
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    68
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    69
    // negotiated security parameters
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    70
    SSLSessionImpl                  conSession;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    71
    ProtocolVersion                 protocolVersion;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    72
    String                          applicationProtocol;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    73
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    74
    // handshake context
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    75
    HandshakeContext                handshakeContext;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    76
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    77
    // connection reserved status for handshake.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    78
    boolean                         secureRenegotiation;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    79
    byte[]                          clientVerifyData;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    80
    byte[]                          serverVerifyData;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    81
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    82
    // connection sensitive configuration
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    83
    List<NamedGroup>                serverRequestedNamedGroups;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    84
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    85
    SecretKey baseWriteSecret, baseReadSecret;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    86
    CipherSuite cipherSuite;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    87
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    88
    // Please never use the transport parameter other than storing a
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    89
    // reference to this object.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    90
    TransportContext(SSLContextImpl sslContext, SSLTransport transport,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    91
            InputRecord inputRecord, OutputRecord outputRecord) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    92
        this.transport = transport;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    93
        this.sslContext = sslContext;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    94
        this.inputRecord = inputRecord;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    95
        this.outputRecord = outputRecord;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    96
        this.sslConfig = new SSLConfiguration(sslContext, true);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    97
        this.sslConfig.maximumPacketSize = outputRecord.getMaxPacketSize();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    98
        this.isUnsureMode = true;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    99
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   100
        initialize();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   101
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   102
        this.acc = AccessController.getContext();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   103
        this.consumers = new HashMap<>();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   104
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   105
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   106
    // Please never use the transport parameter other than storing a
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   107
    // reference to this object.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   108
    TransportContext(SSLContextImpl sslContext, SSLTransport transport,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   109
            InputRecord inputRecord, OutputRecord outputRecord,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   110
            boolean isClientMode) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   111
        this.transport = transport;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   112
        this.sslContext = sslContext;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   113
        this.inputRecord = inputRecord;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   114
        this.outputRecord = outputRecord;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   115
        this.sslConfig = new SSLConfiguration(sslContext, isClientMode);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   116
        this.sslConfig.maximumPacketSize = outputRecord.getMaxPacketSize();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   117
        this.isUnsureMode = false;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   118
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   119
        initialize();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   120
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   121
        this.acc = AccessController.getContext();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   122
        this.consumers = new HashMap<>();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   123
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   124
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   125
    // Please never use the transport parameter other than storing a
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   126
    // reference to this object.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   127
    TransportContext(SSLContextImpl sslContext, SSLTransport transport,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   128
            SSLConfiguration sslConfig,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   129
            InputRecord inputRecord, OutputRecord outputRecord) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   130
        this.transport = transport;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   131
        this.sslContext = sslContext;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   132
        this.inputRecord = inputRecord;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   133
        this.outputRecord = outputRecord;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   134
        this.sslConfig = (SSLConfiguration)sslConfig.clone();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   135
        if (this.sslConfig.maximumPacketSize == 0) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   136
            this.sslConfig.maximumPacketSize = outputRecord.getMaxPacketSize();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   137
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   138
        this.isUnsureMode = false;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   139
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   140
        initialize();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   141
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   142
        this.acc = AccessController.getContext();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   143
        this.consumers = new HashMap<>();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   144
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   145
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   146
    // Initialize the non-final class variables.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   147
    private void initialize() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   148
        // initial security parameters
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   149
        this.conSession = SSLSessionImpl.nullSession;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   150
        this.protocolVersion = this.sslConfig.maximumProtocolVersion;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   151
        this.applicationProtocol = null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   152
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   153
        // initial handshake context
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   154
        this.handshakeContext = null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   155
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   156
        // initial security parameters for secure renegotiation
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   157
        this.secureRenegotiation = false;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   158
        this.clientVerifyData = new byte[0];
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   159
        this.serverVerifyData = new byte[0];
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   160
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   161
        this.isNegotiated = false;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   162
        this.isBroken = false;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   163
        this.isInputCloseNotified = false;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   164
        this.isOutputCloseNotified = false;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   165
        this.closeReason = null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   166
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   167
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   168
    // Dispatch plaintext to a specific consumer.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   169
    void dispatch(Plaintext plaintext) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   170
        if (plaintext == null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   171
            return;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   172
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   173
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   174
        ContentType ct = ContentType.valueOf(plaintext.contentType);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   175
        if (ct == null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   176
            fatal(Alert.UNEXPECTED_MESSAGE,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   177
                "Unknown content type: " + plaintext.contentType);
56544
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   178
            return;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   179
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   180
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   181
        switch (ct) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   182
            case HANDSHAKE:
56544
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   183
                byte type = HandshakeContext.getHandshakeType(this,
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   184
                        plaintext);
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   185
                if (handshakeContext == null) {
56561
5f23e0400f27 Use PostHandshakeContext for New Session Ticket when approriate
ascarpino
parents: 56544
diff changeset
   186
                    if (type == SSLHandshake.KEY_UPDATE.id ||
5f23e0400f27 Use PostHandshakeContext for New Session Ticket when approriate
ascarpino
parents: 56544
diff changeset
   187
                            type == SSLHandshake.NEW_SESSION_TICKET.id) {
56544
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   188
                        handshakeContext = new PostHandshakeContext(this);
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   189
                    } else {
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   190
                        handshakeContext = sslConfig.isClientMode ?
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   191
                                new ClientHandshakeContext(sslContext, this) :
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   192
                                new ServerHandshakeContext(sslContext, this);
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   193
                        outputRecord.initHandshaker();
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   194
                    }
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   195
                }
56544
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   196
                handshakeContext.dispatch(type, plaintext);
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   197
                break;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   198
            case ALERT:
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   199
                Alert.alertConsumer.consume(this, plaintext.fragment);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   200
                break;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   201
            default:
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   202
                SSLConsumer consumer = consumers.get(plaintext.contentType);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   203
                if (consumer != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   204
                    consumer.consume(this, plaintext.fragment);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   205
                } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   206
                    fatal(Alert.UNEXPECTED_MESSAGE,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   207
                        "Unexpected content: " + plaintext.contentType);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   208
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   209
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   210
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   211
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   212
    void kickstart() throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   213
        if (isUnsureMode) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   214
            throw new IllegalStateException("Client/Server mode not yet set.");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   215
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   216
56594
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 56573
diff changeset
   217
        if (outputRecord.isClosed() || inputRecord.isClosed() || isBroken) {
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 56573
diff changeset
   218
            if (closeReason != null) {
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 56573
diff changeset
   219
                throw new SSLException(
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 56573
diff changeset
   220
                        "Cannot kickstart, the connection is broken or closed",
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 56573
diff changeset
   221
                        closeReason);
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 56573
diff changeset
   222
            } else {
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 56573
diff changeset
   223
                throw new SSLException(
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 56573
diff changeset
   224
                        "Cannot kickstart, the connection is broken or closed");
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 56573
diff changeset
   225
            }
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 56573
diff changeset
   226
        }
99e0f3f3f0e4 close issues introduced in TLS 1.3 implementation
xuelei
parents: 56573
diff changeset
   227
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   228
        // initialize the handshaker if necessary
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   229
        if (handshakeContext == null) {
56544
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   230
            //  TLS1.3 post-handshake
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   231
            if (isNegotiated && protocolVersion.useTLS13PlusSpec()) {
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   232
                handshakeContext = new PostHandshakeContext(this);
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   233
            } else {
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   234
                handshakeContext = sslConfig.isClientMode ?
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   235
                        new ClientHandshakeContext(sslContext, this) :
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   236
                        new ServerHandshakeContext(sslContext, this);
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   237
                outputRecord.initHandshaker();
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   238
            }
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   239
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   240
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   241
        // kickstart the handshake if needed
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   242
        //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   243
        // Need no kickstart message on server side unless the connection
56544
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   244
        // has been established.
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   245
        if(isNegotiated || sslConfig.isClientMode) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   246
           handshakeContext.kickstart();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   247
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   248
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   249
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   250
    void keyUpdate() throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   251
        kickstart();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   252
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   253
56544
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   254
    final static byte PRE = 1;
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   255
    final static byte POST = 2;
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   256
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   257
    HandshakeContext getHandshakeContext(byte type) {
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   258
        if (handshakeContext == null) {
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   259
            return null;
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   260
        }
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   261
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   262
        if (type == PRE &&
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   263
                (handshakeContext instanceof ClientHandshakeContext ||
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   264
                        handshakeContext instanceof ServerHandshakeContext)) {
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   265
            return handshakeContext;
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   266
        }
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   267
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   268
        if (type == POST && handshakeContext instanceof PostHandshakeContext) {
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   269
            return handshakeContext;
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   270
        }
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   271
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   272
        return null;
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   273
    }
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   274
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   275
    // Note: close_notify is delivered as a warning alert.
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   276
    void warning(Alert alert) {
56544
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   277
        // For initial handshaking, don't send a warning alert message to peer
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   278
        // if handshaker has not started.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   279
        if (isNegotiated || handshakeContext != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   280
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   281
                outputRecord.encodeAlert(Alert.Level.WARNING.level, alert.id);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   282
            } catch (IOException ioe) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   283
                if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   284
                    SSLLogger.warning(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   285
                        "Warning: failed to send warning alert " + alert, ioe);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   286
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   287
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   288
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   289
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   290
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   291
    void fatal(Alert alert,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   292
            String diagnostic) throws SSLException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   293
        fatal(alert, diagnostic, null);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   294
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   295
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   296
    void fatal(Alert alert, Throwable cause) throws SSLException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   297
        fatal(alert, null, cause);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   298
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   299
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   300
    void fatal(Alert alert,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   301
            String diagnostic, Throwable cause) throws SSLException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   302
        fatal(alert, diagnostic, false, cause);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   303
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   304
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   305
    // Note: close_notify is not delivered via fatal() methods.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   306
    void fatal(Alert alert, String diagnostic,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   307
            boolean recvFatalAlert, Throwable cause) throws SSLException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   308
        // If we've already shutdown because of an error, there is nothing we
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   309
        // can do except rethrow the exception.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   310
        //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   311
        // Most exceptions seen here will be SSLExceptions. We may find the
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   312
        // occasional Exception which hasn't been converted to a SSLException,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   313
        // so we'll do it here.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   314
        if (closeReason != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   315
            if (cause == null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   316
                if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   317
                    SSLLogger.warning(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   318
                            "Closed transport, general or untracked problem");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   319
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   320
                throw alert.createSSLException(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   321
                        "Closed transport, general or untracked problem");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   322
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   323
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   324
            if (cause instanceof SSLException) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   325
                throw (SSLException)cause;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   326
            } else {    // unlikely, but just in case.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   327
                if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   328
                    SSLLogger.warning(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   329
                            "Closed transport, rethrowing (unexpected)", cause);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   330
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   331
                throw alert.createSSLException("Unexpected rethrowing", cause);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   332
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   333
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   334
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   335
        // If we have no further information, make a general-purpose
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   336
        // message for folks to see.  We generally have one or the other.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   337
        if (diagnostic == null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   338
            if (cause == null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   339
                diagnostic = "General/Untracked problem";
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   340
            } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   341
                diagnostic = cause.getMessage();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   342
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   343
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   344
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   345
        if (cause == null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   346
            cause = alert.createSSLException(diagnostic);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   347
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   348
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   349
        // shutdown the transport
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   350
        if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   351
            SSLLogger.severe("Fatal (" + alert + "): " + diagnostic, cause);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   352
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   353
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   354
        // remember the close reason
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   355
        if (cause instanceof SSLException) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   356
            closeReason = (SSLException)cause;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   357
        } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   358
            // Including RuntimeException, but we'll throw those down below.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   359
            closeReason = alert.createSSLException(diagnostic, cause);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   360
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   361
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   362
        // close inbound
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   363
        try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   364
            inputRecord.close();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   365
        } catch (IOException ioe) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   366
            if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   367
                SSLLogger.warning("Fatal: input record closure failed", ioe);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   368
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   369
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   370
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   371
        // invalidate the session
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   372
        if (conSession != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   373
            conSession.invalidate();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   374
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   375
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   376
        if (handshakeContext != null &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   377
                handshakeContext.handshakeSession != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   378
            handshakeContext.handshakeSession.invalidate();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   379
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   380
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   381
        // send fatal alert
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   382
        //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   383
        // If we haven't even started handshaking yet, or we are the recipient
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   384
        // of a fatal alert, no need to generate a fatal close alert.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   385
        if (!recvFatalAlert && !isOutboundDone() && !isBroken &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   386
                (isNegotiated || handshakeContext != null)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   387
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   388
                outputRecord.encodeAlert(Alert.Level.FATAL.level, alert.id);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   389
            } catch (IOException ioe) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   390
                if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   391
                    SSLLogger.warning(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   392
                        "Fatal: failed to send fatal alert " + alert, ioe);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   393
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   394
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   395
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   396
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   397
        // close outbound
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   398
        try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   399
            outputRecord.close();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   400
        } catch (IOException ioe) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   401
            if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   402
                SSLLogger.warning("Fatal: ouput record closure failed", ioe);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   403
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   404
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   405
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   406
        // terminal handshake context
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   407
        if (handshakeContext != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   408
            handshakeContext = null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   409
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   410
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   411
        // terminal the transport
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   412
        try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   413
            transport.shutdown();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   414
        } catch (IOException ioe) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   415
            if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   416
                SSLLogger.warning("Fatal: transport closure failed", ioe);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   417
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   418
        } finally {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   419
            isBroken = true;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   420
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   421
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   422
        if (closeReason instanceof SSLException) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   423
            throw (SSLException)closeReason;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   424
        } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   425
            throw (RuntimeException)closeReason;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   426
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   427
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   428
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   429
    void setUseClientMode(boolean useClientMode) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   430
        /*
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   431
         * If we need to change the client mode and the enabled
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   432
         * protocols and cipher suites haven't specifically been
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   433
         * set by the user, change them to the corresponding
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   434
         * default ones.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   435
         */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   436
        if (sslConfig.isClientMode != useClientMode) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   437
            // Once handshaking has begun, the mode can not be reset for the
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   438
            // life of this engine.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   439
            if (handshakeContext != null || isNegotiated) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   440
                throw new IllegalArgumentException(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   441
                    "Cannot change mode after SSL traffic has started");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   442
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   443
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   444
            if (sslContext.isDefaultProtocolVesions(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   445
                    sslConfig.enabledProtocols)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   446
                sslConfig.enabledProtocols =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   447
                        sslContext.getDefaultProtocolVersions(!useClientMode);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   448
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   449
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   450
            if (sslContext.isDefaultCipherSuiteList(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   451
                    sslConfig.enabledCipherSuites)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   452
                sslConfig.enabledCipherSuites =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   453
                        sslContext.getDefaultCipherSuites(!useClientMode);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   454
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   455
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   456
            sslConfig.isClientMode = useClientMode;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   457
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   458
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   459
        isUnsureMode = false;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   460
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   461
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   462
    boolean isOutboundDone() {
56573
4a53dd94403e DTLS fix for proper closing
ascarpino
parents: 56561
diff changeset
   463
        return outputRecord.isClosed() && outputRecord.isEmpty();
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   464
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   465
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   466
    boolean isInboundDone() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   467
        return inputRecord.isClosed();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   468
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   469
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   470
    boolean isClosed() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   471
        return isOutboundDone() && isInboundDone();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   472
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   473
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   474
    @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   475
    public void close() throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   476
        if (!isOutboundDone()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   477
            closeOutbound();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   478
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   479
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   480
        if (!isInboundDone()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   481
            closeInbound();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   482
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   483
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   484
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   485
    void closeInbound() throws SSLException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   486
        if (isInboundDone()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   487
            return;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   488
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   489
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   490
        try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   491
            if (isInputCloseNotified) {     // passive close
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   492
                passiveInboundClose();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   493
            } else {                        // initiative close
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   494
                initiateInboundClose();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   495
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   496
        } catch (IOException ioe) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   497
            if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   498
                SSLLogger.warning("inbound closure failed", ioe);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   499
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   500
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   501
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   502
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   503
    void closeOutbound() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   504
        if (isOutboundDone()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   505
            return;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   506
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   507
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   508
        try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   509
             initiateOutboundClose();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   510
        } catch (IOException ioe) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   511
            if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   512
                SSLLogger.warning("outbound closure failed", ioe);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   513
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   514
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   515
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   516
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   517
    // Close the connection passively.  The closure could be kickoff by
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   518
    // receiving a close_notify alert or reaching end_of_file of the socket.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   519
    private void passiveInboundClose() throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   520
        if (!isInboundDone()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   521
            inputRecord.close();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   522
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   523
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   524
        // For TLS 1.3, output closure is independent from input closure.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   525
//      if (isNegotiated && protocolVersion.useTLS13PlusSpec()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   526
//          return;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   527
//      }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   528
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   529
        // For TLS 1.2 and prior version, it is required to respond with
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   530
        // a close_notify alert of its own and close down the connection
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   531
        // immediately, discarding any pending writes.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   532
        if (!isOutboundDone() && !isOutputCloseNotified) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   533
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   534
                // send a close_notify alert
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   535
                warning(Alert.CLOSE_NOTIFY);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   536
            } finally {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   537
                // any data received after a closure alert is ignored.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   538
                isOutputCloseNotified = true;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   539
                outputRecord.close();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   540
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   541
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   543
        transport.shutdown();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   544
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   545
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   546
    // Initiate a close by sending a close_notify alert.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   547
    private void initiateInboundClose() throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   548
        // TLS 1.3 does not define how to initiate and close a TLS connection
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   549
        // gracefully.  We will always send a close_notify alert, and close
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   550
        // the underlying transportation layer if needed.
56573
4a53dd94403e DTLS fix for proper closing
ascarpino
parents: 56561
diff changeset
   551
        if (!isInboundDone() && !isInputCloseNotified) {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   552
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   553
                // send a close_notify alert
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   554
                warning(Alert.CLOSE_NOTIFY);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   555
            } finally {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   556
                // any data received after a closure alert is ignored.
56573
4a53dd94403e DTLS fix for proper closing
ascarpino
parents: 56561
diff changeset
   557
                isInputCloseNotified = true;
4a53dd94403e DTLS fix for proper closing
ascarpino
parents: 56561
diff changeset
   558
                inputRecord.close();
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   559
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   560
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   561
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   562
        // For TLS 1.3, input closure is independent from output closure. Both
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   563
        // parties need not wait to receive a "close_notify" alert before
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   564
        // closing their read side of the connection.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   565
        //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   566
        // For TLS 1.2 and prior version, it is not required for the initiator
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   567
        // of the close to wait for the responding close_notify alert before
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   568
        // closing the read side of the connection.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   569
        try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   570
            transport.shutdown();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   571
        } finally {
56573
4a53dd94403e DTLS fix for proper closing
ascarpino
parents: 56561
diff changeset
   572
            if (!isOutboundDone()) {
4a53dd94403e DTLS fix for proper closing
ascarpino
parents: 56561
diff changeset
   573
                outputRecord.close();
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   574
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   575
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   576
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   577
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   578
    // Initiate a close by sending a close_notify alert.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   579
    private void initiateOutboundClose() throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   580
        if (!isOutboundDone() && !isOutputCloseNotified) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   581
            try {     // close outputRecord
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   582
                // send a close_notify alert
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   583
                warning(Alert.CLOSE_NOTIFY);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   584
            } finally {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   585
                // any data received after a closure alert is ignored.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   586
                isOutputCloseNotified = true;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   587
                outputRecord.close();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   588
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   589
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   590
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   591
        // For TLS 1.3, output closure is independent from input closure.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   592
//
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   593
//      if (isNegotiated && protocolVersion.useTLS13PlusSpec()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   594
//          return;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   595
//      }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   596
//
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   597
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   598
        // It is not required for the initiator of the close to wait for the
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   599
        // responding close_notify alert before closing the read side of the
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   600
        // connection.  However, if the application protocol using TLS
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   601
        // provides that any data may be carried over the underlying transport
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   602
        // after the TLS connection is closed, the TLS implementation MUST
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   603
        // receive a "close_notify" alert before indicating end-of-data to the
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   604
        // application-layer.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   605
        try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   606
            transport.shutdown();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   607
        } finally {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   608
            if (!isInboundDone()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   609
                inputRecord.close();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   610
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   611
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   612
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   613
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   614
    // Note; HandshakeStatus.FINISHED status is retrieved in other places.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   615
    HandshakeStatus getHandshakeStatus() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   616
        if (!outputRecord.isEmpty()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   617
            // If no handshaking, special case to wrap alters.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   618
            return HandshakeStatus.NEED_WRAP;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   619
        } else if (handshakeContext != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   620
            if (!handshakeContext.delegatedActions.isEmpty()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   621
                return HandshakeStatus.NEED_TASK;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   622
            } else if (sslContext.isDTLS() &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   623
                    !inputRecord.isEmpty()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   624
                return HandshakeStatus.NEED_UNWRAP_AGAIN;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   625
            } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   626
                return HandshakeStatus.NEED_UNWRAP;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   627
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   628
        } else if (isOutboundDone() && !isInboundDone()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   629
            /*
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   630
             * Special case where we're closing, but
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   631
             * still need the close_notify before we
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   632
             * can officially be closed.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   633
             *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   634
             * Note isOutboundDone is taken care of by
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   635
             * hasOutboundData() above.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   636
             */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   637
            return HandshakeStatus.NEED_UNWRAP;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   638
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   639
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   640
        return HandshakeStatus.NOT_HANDSHAKING;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   641
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   642
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   643
    HandshakeStatus finishHandshake() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   644
        if (protocolVersion.useTLS13PlusSpec()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   645
            outputRecord.tc = this;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   646
            cipherSuite = handshakeContext.negotiatedCipherSuite;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   647
            inputRecord.readCipher.baseSecret = handshakeContext.baseReadSecret;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   648
            outputRecord.writeCipher.baseSecret = handshakeContext.baseWriteSecret;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   649
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   650
        handshakeContext = null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   651
        // inputRecord and outputRecord shares the same handshakeHash
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   652
        // inputRecord.handshakeHash.finish();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   653
        outputRecord.handshakeHash.finish();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   654
        inputRecord.finishHandshake();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   655
        outputRecord.finishHandshake();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   656
        isNegotiated = true;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   657
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   658
        // Tell folk about handshake completion, but do it in a separate thread.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   659
        if (transport instanceof SSLSocket &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   660
                sslConfig.handshakeListeners != null &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   661
                !sslConfig.handshakeListeners.isEmpty()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   662
            HandshakeCompletedEvent hce =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   663
                new HandshakeCompletedEvent((SSLSocket)transport, conSession);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   664
            Thread thread = new Thread(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   665
                null,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   666
                new NotifyHandshake(sslConfig.handshakeListeners, hce),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   667
                "HandshakeCompletedNotify-Thread",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   668
                0,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   669
                false);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   670
            thread.start();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   671
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   672
        return HandshakeStatus.FINISHED;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   673
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   674
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   675
    // A separate thread is allocated to deliver handshake completion
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   676
    // events.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   677
    private static class NotifyHandshake implements Runnable {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   678
        private Set<Map.Entry<HandshakeCompletedListener,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   679
                AccessControlContext>> targets;         // who gets notified
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   680
        private HandshakeCompletedEvent event;          // the notification
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   681
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   682
        NotifyHandshake(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   683
                Map<HandshakeCompletedListener,AccessControlContext> listeners,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   684
                HandshakeCompletedEvent event) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   685
            this.targets = new HashSet<>(listeners.entrySet());     // clone
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   686
            this.event = event;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   687
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   688
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   689
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   690
        public void run() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   691
            // Don't need to synchronize, as it only runs in one thread.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   692
            for (Map.Entry<HandshakeCompletedListener,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   693
                    AccessControlContext> entry : targets) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   694
                final HandshakeCompletedListener listener = entry.getKey();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   695
                AccessControlContext acc = entry.getValue();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   696
                AccessController.doPrivileged(new PrivilegedAction<Void>() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   697
                    @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   698
                    public Void run() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   699
                        listener.handshakeCompleted(event);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   700
                        return null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   701
                    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   702
                }, acc);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   703
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   704
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   705
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   706
}