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