src/java.base/share/classes/sun/security/ssl/TransportContext.java
author ascarpino
Fri, 18 May 2018 19:07:49 -0700
branchJDK-8145252-TLS13-branch
changeset 56573 4a53dd94403e
parent 56561 5f23e0400f27
child 56594 99e0f3f3f0e4
permissions -rw-r--r--
DTLS fix for proper closing
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
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   217
        // initialize the handshaker if necessary
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   218
        if (handshakeContext == null) {
56544
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   219
            //  TLS1.3 post-handshake
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   220
            if (isNegotiated && protocolVersion.useTLS13PlusSpec()) {
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   221
                handshakeContext = new PostHandshakeContext(this);
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   222
            } else {
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   223
                handshakeContext = sslConfig.isClientMode ?
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   224
                        new ClientHandshakeContext(sslContext, this) :
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   225
                        new ServerHandshakeContext(sslContext, this);
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   226
                outputRecord.initHandshaker();
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   227
            }
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   228
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   229
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   230
        // kickstart the handshake if needed
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   231
        //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   232
        // Need no kickstart message on server side unless the connection
56544
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   233
        // has been established.
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   234
        if(isNegotiated || sslConfig.isClientMode) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   235
           handshakeContext.kickstart();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   236
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   237
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   238
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   239
    void keyUpdate() throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   240
        kickstart();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   241
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   242
56544
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   243
    final static byte PRE = 1;
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   244
    final static byte POST = 2;
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   245
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   246
    HandshakeContext getHandshakeContext(byte type) {
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   247
        if (handshakeContext == null) {
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   248
            return null;
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   249
        }
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   250
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   251
        if (type == PRE &&
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   252
                (handshakeContext instanceof ClientHandshakeContext ||
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   253
                        handshakeContext instanceof ServerHandshakeContext)) {
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   254
            return handshakeContext;
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   255
        }
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
        if (type == POST && handshakeContext instanceof PostHandshakeContext) {
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   258
            return handshakeContext;
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   259
        }
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
        return null;
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   262
    }
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   263
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   264
    // Note: close_notify is delivered as a warning alert.
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   265
    void warning(Alert alert) {
56544
ad120e0dfcfb start/beginHandshake and more post-handshake changes
ascarpino
parents: 56542
diff changeset
   266
        // For initial handshaking, don't send a warning alert message to peer
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   267
        // if handshaker has not started.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   268
        if (isNegotiated || handshakeContext != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   269
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   270
                outputRecord.encodeAlert(Alert.Level.WARNING.level, alert.id);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   271
            } catch (IOException ioe) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   272
                if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   273
                    SSLLogger.warning(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   274
                        "Warning: failed to send warning alert " + alert, ioe);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   275
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   276
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   277
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   278
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   279
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   280
    void fatal(Alert alert,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   281
            String diagnostic) throws SSLException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   282
        fatal(alert, diagnostic, null);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   283
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   284
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   285
    void fatal(Alert alert, Throwable cause) throws SSLException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   286
        fatal(alert, null, cause);
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
    void fatal(Alert alert,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   290
            String diagnostic, Throwable cause) throws SSLException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   291
        fatal(alert, diagnostic, false, cause);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   292
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   293
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   294
    // Note: close_notify is not delivered via fatal() methods.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   295
    void fatal(Alert alert, String diagnostic,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   296
            boolean recvFatalAlert, Throwable cause) throws SSLException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   297
        // If we've already shutdown because of an error, there is nothing we
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   298
        // can do except rethrow the exception.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   299
        //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   300
        // Most exceptions seen here will be SSLExceptions. We may find the
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   301
        // occasional Exception which hasn't been converted to a SSLException,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   302
        // so we'll do it here.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   303
        if (closeReason != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   304
            if (cause == null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   305
                if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   306
                    SSLLogger.warning(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   307
                            "Closed transport, general or untracked problem");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   308
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   309
                throw alert.createSSLException(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   310
                        "Closed transport, general or untracked problem");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   311
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   312
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   313
            if (cause instanceof SSLException) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   314
                throw (SSLException)cause;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   315
            } else {    // unlikely, but just in case.
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, rethrowing (unexpected)", cause);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   319
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   320
                throw alert.createSSLException("Unexpected rethrowing", cause);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   321
            }
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 we have no further information, make a general-purpose
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   325
        // message for folks to see.  We generally have one or the other.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   326
        if (diagnostic == null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   327
            if (cause == null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   328
                diagnostic = "General/Untracked problem";
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   329
            } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   330
                diagnostic = cause.getMessage();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   331
            }
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
        if (cause == null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   335
            cause = alert.createSSLException(diagnostic);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   336
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   337
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   338
        // shutdown the transport
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   339
        if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   340
            SSLLogger.severe("Fatal (" + alert + "): " + diagnostic, cause);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   341
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   342
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   343
        // remember the close reason
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   344
        if (cause instanceof SSLException) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   345
            closeReason = (SSLException)cause;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   346
        } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   347
            // Including RuntimeException, but we'll throw those down below.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   348
            closeReason = alert.createSSLException(diagnostic, cause);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   349
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   350
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   351
        // close inbound
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   352
        try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   353
            inputRecord.close();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   354
        } catch (IOException ioe) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   355
            if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   356
                SSLLogger.warning("Fatal: input record closure failed", ioe);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   357
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   358
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   359
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   360
        // invalidate the session
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   361
        if (conSession != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   362
            conSession.invalidate();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   363
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   364
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   365
        if (handshakeContext != null &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   366
                handshakeContext.handshakeSession != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   367
            handshakeContext.handshakeSession.invalidate();
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
        // send fatal alert
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   371
        //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   372
        // If we haven't even started handshaking yet, or we are the recipient
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   373
        // of a fatal alert, no need to generate a fatal close alert.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   374
        if (!recvFatalAlert && !isOutboundDone() && !isBroken &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   375
                (isNegotiated || handshakeContext != null)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   376
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   377
                outputRecord.encodeAlert(Alert.Level.FATAL.level, alert.id);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   378
            } catch (IOException ioe) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   379
                if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   380
                    SSLLogger.warning(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   381
                        "Fatal: failed to send fatal alert " + alert, ioe);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   382
                }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   383
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   384
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   385
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   386
        // close outbound
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   387
        try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   388
            outputRecord.close();
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("Fatal: ouput record closure failed", ioe);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   392
            }
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
        // terminal handshake context
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   396
        if (handshakeContext != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   397
            handshakeContext = null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   398
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   399
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   400
        // terminal the transport
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   401
        try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   402
            transport.shutdown();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   403
        } catch (IOException ioe) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   404
            if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   405
                SSLLogger.warning("Fatal: transport closure failed", ioe);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   406
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   407
        } finally {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   408
            isBroken = true;
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
        if (closeReason instanceof SSLException) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   412
            throw (SSLException)closeReason;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   413
        } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   414
            throw (RuntimeException)closeReason;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   415
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   416
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   417
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   418
    void setUseClientMode(boolean useClientMode) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   419
        /*
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   420
         * If we need to change the client mode and the enabled
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   421
         * protocols and cipher suites haven't specifically been
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   422
         * set by the user, change them to the corresponding
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   423
         * default ones.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   424
         */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   425
        if (sslConfig.isClientMode != useClientMode) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   426
            // Once handshaking has begun, the mode can not be reset for the
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   427
            // life of this engine.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   428
            if (handshakeContext != null || isNegotiated) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   429
                throw new IllegalArgumentException(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   430
                    "Cannot change mode after SSL traffic has started");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   431
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   432
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   433
            if (sslContext.isDefaultProtocolVesions(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   434
                    sslConfig.enabledProtocols)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   435
                sslConfig.enabledProtocols =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   436
                        sslContext.getDefaultProtocolVersions(!useClientMode);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   437
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   438
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   439
            if (sslContext.isDefaultCipherSuiteList(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   440
                    sslConfig.enabledCipherSuites)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   441
                sslConfig.enabledCipherSuites =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   442
                        sslContext.getDefaultCipherSuites(!useClientMode);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   443
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   444
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   445
            sslConfig.isClientMode = useClientMode;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   446
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   447
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   448
        isUnsureMode = false;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   449
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   450
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   451
    boolean isOutboundDone() {
56573
4a53dd94403e DTLS fix for proper closing
ascarpino
parents: 56561
diff changeset
   452
        return outputRecord.isClosed() && outputRecord.isEmpty();
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   453
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   454
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   455
    boolean isInboundDone() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   456
        return inputRecord.isClosed();
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
    boolean isClosed() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   460
        return isOutboundDone() && isInboundDone();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   461
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   462
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   463
    @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   464
    public void close() throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   465
        if (!isOutboundDone()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   466
            closeOutbound();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   467
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   468
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   469
        if (!isInboundDone()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   470
            closeInbound();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   471
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   472
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   473
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   474
    void closeInbound() throws SSLException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   475
        if (isInboundDone()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   476
            return;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   477
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   478
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   479
        try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   480
            if (isInputCloseNotified) {     // passive close
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   481
                passiveInboundClose();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   482
            } else {                        // initiative close
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   483
                initiateInboundClose();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   484
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   485
        } catch (IOException ioe) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   486
            if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   487
                SSLLogger.warning("inbound closure failed", ioe);
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
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   491
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   492
    void closeOutbound() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   493
        if (isOutboundDone()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   494
            return;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   495
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   496
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   497
        try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   498
             initiateOutboundClose();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   499
        } catch (IOException ioe) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   500
            if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   501
                SSLLogger.warning("outbound closure failed", ioe);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   502
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   503
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   504
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   505
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   506
    // Close the connection passively.  The closure could be kickoff by
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   507
    // receiving a close_notify alert or reaching end_of_file of the socket.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   508
    private void passiveInboundClose() throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   509
        if (!isInboundDone()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   510
            inputRecord.close();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   511
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   512
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   513
        // For TLS 1.3, output closure is independent from input closure.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   514
//      if (isNegotiated && protocolVersion.useTLS13PlusSpec()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   515
//          return;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   516
//      }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   517
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   518
        // For TLS 1.2 and prior version, it is required to respond with
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   519
        // a close_notify alert of its own and close down the connection
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   520
        // immediately, discarding any pending writes.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   521
        if (!isOutboundDone() && !isOutputCloseNotified) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   522
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   523
                // send a close_notify alert
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   524
                warning(Alert.CLOSE_NOTIFY);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   525
            } finally {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   526
                // any data received after a closure alert is ignored.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   527
                isOutputCloseNotified = true;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   528
                outputRecord.close();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   529
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   530
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   531
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   532
        transport.shutdown();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   533
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   534
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   535
    // Initiate a close by sending a close_notify alert.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   536
    private void initiateInboundClose() throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   537
        // TLS 1.3 does not define how to initiate and close a TLS connection
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   538
        // gracefully.  We will always send a close_notify alert, and close
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   539
        // the underlying transportation layer if needed.
56573
4a53dd94403e DTLS fix for proper closing
ascarpino
parents: 56561
diff changeset
   540
        if (!isInboundDone() && !isInputCloseNotified) {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   541
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   542
                // send a close_notify alert
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   543
                warning(Alert.CLOSE_NOTIFY);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   544
            } finally {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   545
                // any data received after a closure alert is ignored.
56573
4a53dd94403e DTLS fix for proper closing
ascarpino
parents: 56561
diff changeset
   546
                isInputCloseNotified = true;
4a53dd94403e DTLS fix for proper closing
ascarpino
parents: 56561
diff changeset
   547
                inputRecord.close();
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   548
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   549
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   550
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   551
        // For TLS 1.3, input closure is independent from output closure. Both
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   552
        // parties need not wait to receive a "close_notify" alert before
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   553
        // closing their read side of the connection.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   554
        //
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   555
        // For TLS 1.2 and prior version, it is not required for the initiator
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   556
        // of the close to wait for the responding close_notify alert before
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   557
        // closing the read side of the connection.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   558
        try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   559
            transport.shutdown();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   560
        } finally {
56573
4a53dd94403e DTLS fix for proper closing
ascarpino
parents: 56561
diff changeset
   561
            if (!isOutboundDone()) {
4a53dd94403e DTLS fix for proper closing
ascarpino
parents: 56561
diff changeset
   562
                outputRecord.close();
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   563
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   564
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   565
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   566
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   567
    // Initiate a close by sending a close_notify alert.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   568
    private void initiateOutboundClose() throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   569
        if (!isOutboundDone() && !isOutputCloseNotified) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   570
            try {     // close outputRecord
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   571
                // send a close_notify alert
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   572
                warning(Alert.CLOSE_NOTIFY);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   573
            } finally {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   574
                // any data received after a closure alert is ignored.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   575
                isOutputCloseNotified = true;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   576
                outputRecord.close();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   577
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   578
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   579
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   580
        // For TLS 1.3, output closure is independent from input closure.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   581
//
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   582
//      if (isNegotiated && protocolVersion.useTLS13PlusSpec()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   583
//          return;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   584
//      }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   585
//
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   586
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   587
        // It is not required for the initiator of the close to wait for the
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   588
        // responding close_notify alert before closing the read side of the
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   589
        // connection.  However, if the application protocol using TLS
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   590
        // provides that any data may be carried over the underlying transport
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   591
        // after the TLS connection is closed, the TLS implementation MUST
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   592
        // receive a "close_notify" alert before indicating end-of-data to the
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   593
        // application-layer.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   594
        try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   595
            transport.shutdown();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   596
        } finally {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   597
            if (!isInboundDone()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   598
                inputRecord.close();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   599
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   600
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   601
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   602
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   603
    // Note; HandshakeStatus.FINISHED status is retrieved in other places.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   604
    HandshakeStatus getHandshakeStatus() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   605
        if (!outputRecord.isEmpty()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   606
            // If no handshaking, special case to wrap alters.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   607
            return HandshakeStatus.NEED_WRAP;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   608
        } else if (handshakeContext != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   609
            if (!handshakeContext.delegatedActions.isEmpty()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   610
                return HandshakeStatus.NEED_TASK;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   611
            } else if (sslContext.isDTLS() &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   612
                    !inputRecord.isEmpty()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   613
                return HandshakeStatus.NEED_UNWRAP_AGAIN;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   614
            } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   615
                return HandshakeStatus.NEED_UNWRAP;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   616
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   617
        } else if (isOutboundDone() && !isInboundDone()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   618
            /*
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   619
             * Special case where we're closing, but
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   620
             * still need the close_notify before we
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   621
             * can officially be closed.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   622
             *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   623
             * Note isOutboundDone is taken care of by
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   624
             * hasOutboundData() above.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   625
             */
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
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   629
        return HandshakeStatus.NOT_HANDSHAKING;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   630
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   631
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   632
    HandshakeStatus finishHandshake() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   633
        if (protocolVersion.useTLS13PlusSpec()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   634
            outputRecord.tc = this;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   635
            cipherSuite = handshakeContext.negotiatedCipherSuite;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   636
            inputRecord.readCipher.baseSecret = handshakeContext.baseReadSecret;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   637
            outputRecord.writeCipher.baseSecret = handshakeContext.baseWriteSecret;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   638
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   639
        handshakeContext = null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   640
        // inputRecord and outputRecord shares the same handshakeHash
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   641
        // inputRecord.handshakeHash.finish();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   642
        outputRecord.handshakeHash.finish();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   643
        inputRecord.finishHandshake();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   644
        outputRecord.finishHandshake();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   645
        isNegotiated = true;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   646
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   647
        // Tell folk about handshake completion, but do it in a separate thread.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   648
        if (transport instanceof SSLSocket &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   649
                sslConfig.handshakeListeners != null &&
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   650
                !sslConfig.handshakeListeners.isEmpty()) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   651
            HandshakeCompletedEvent hce =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   652
                new HandshakeCompletedEvent((SSLSocket)transport, conSession);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   653
            Thread thread = new Thread(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   654
                null,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   655
                new NotifyHandshake(sslConfig.handshakeListeners, hce),
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   656
                "HandshakeCompletedNotify-Thread",
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   657
                0,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   658
                false);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   659
            thread.start();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   660
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   661
        return HandshakeStatus.FINISHED;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   662
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   663
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   664
    // A separate thread is allocated to deliver handshake completion
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   665
    // events.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   666
    private static class NotifyHandshake implements Runnable {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   667
        private Set<Map.Entry<HandshakeCompletedListener,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   668
                AccessControlContext>> targets;         // who gets notified
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   669
        private HandshakeCompletedEvent event;          // the notification
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   670
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   671
        NotifyHandshake(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   672
                Map<HandshakeCompletedListener,AccessControlContext> listeners,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   673
                HandshakeCompletedEvent event) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   674
            this.targets = new HashSet<>(listeners.entrySet());     // clone
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   675
            this.event = event;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   676
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   677
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   678
        @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   679
        public void run() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   680
            // Don't need to synchronize, as it only runs in one thread.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   681
            for (Map.Entry<HandshakeCompletedListener,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   682
                    AccessControlContext> entry : targets) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   683
                final HandshakeCompletedListener listener = entry.getKey();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   684
                AccessControlContext acc = entry.getValue();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   685
                AccessController.doPrivileged(new PrivilegedAction<Void>() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   686
                    @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   687
                    public Void run() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   688
                        listener.handshakeCompleted(event);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   689
                        return null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   690
                    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   691
                }, acc);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   692
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   693
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   694
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
   695
}