src/java.base/share/classes/sun/security/ssl/SSLSocketImpl.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 57485 af4b0fc25bc4
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
53958
196ab0abc685 8219658: Deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 53067
diff changeset
     2
 * Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5195
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5195
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5195
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5195
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5195
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    28
import java.io.EOFException;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    29
import java.io.IOException;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    30
import java.io.InputStream;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    31
import java.io.InterruptedIOException;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    32
import java.io.OutputStream;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    33
import java.net.InetAddress;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    34
import java.net.InetSocketAddress;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    35
import java.net.Socket;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    36
import java.net.SocketAddress;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    37
import java.net.SocketException;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    38
import java.net.UnknownHostException;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    39
import java.nio.ByteBuffer;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    40
import java.util.List;
55411
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
    41
import java.util.concurrent.TimeUnit;
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
    42
import java.util.concurrent.locks.ReentrantLock;
42706
796cf076d69b 8170282: Enable ALPN parameters to be supplied during the TLS handshake
vinnie
parents: 38865
diff changeset
    43
import java.util.function.BiFunction;
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    44
import javax.net.ssl.HandshakeCompletedListener;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    45
import javax.net.ssl.SSLException;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    46
import javax.net.ssl.SSLHandshakeException;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    47
import javax.net.ssl.SSLParameters;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    48
import javax.net.ssl.SSLProtocolException;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    49
import javax.net.ssl.SSLServerSocket;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    50
import javax.net.ssl.SSLSession;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    51
import javax.net.ssl.SSLSocket;
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 51986
diff changeset
    52
import jdk.internal.access.JavaNetInetAddressAccess;
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 51986
diff changeset
    53
import jdk.internal.access.SharedSecrets;
31687
d6eb4f028c60 8067694: Improved certification checking
xuelei
parents: 30904
diff changeset
    54
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
/**
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    56
 * Implementation of an SSL socket.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    57
 * <P>
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    58
 * This is a normal connection type socket, implementing SSL over some lower
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    59
 * level socket, such as TCP.  Because it is layered over some lower level
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    60
 * socket, it MUST override all default socket methods.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    61
 * <P>
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    62
 * This API offers a non-traditional option for establishing SSL
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * connections.  You may first establish the connection directly, then pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * that connection to the SSL socket constructor with a flag saying which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * role should be taken in the handshake protocol.  (The two ends of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * connection must not choose the same role!)  This allows setup of SSL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * proxying or tunneling, and also allows the kind of "role reversal"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * that is required for most FTP data transfers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * @see javax.net.ssl.SSLSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * @see SSLServerSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 */
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    75
public final class SSLSocketImpl
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    76
        extends BaseSSLSocketImpl implements SSLTransport {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    78
    final SSLContextImpl            sslContext;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    79
    final TransportContext          conContext;
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents: 32834
diff changeset
    80
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    81
    private final AppInputStream    appInput = new AppInputStream();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    82
    private final AppOutputStream   appOutput = new AppOutputStream();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    84
    private String                  peerHost;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    85
    private boolean                 autoClose;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    86
    private boolean                 isConnected = false;
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
    87
    private volatile boolean        tlsIsClosed = false;
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29923
diff changeset
    88
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
    89
    private final ReentrantLock     socketLock = new ReentrantLock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
    90
    private final ReentrantLock     handshakeLock = new ReentrantLock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
    91
31687
d6eb4f028c60 8067694: Improved certification checking
xuelei
parents: 30904
diff changeset
    92
    /*
d6eb4f028c60 8067694: Improved certification checking
xuelei
parents: 30904
diff changeset
    93
     * Is the local name service trustworthy?
d6eb4f028c60 8067694: Improved certification checking
xuelei
parents: 30904
diff changeset
    94
     *
d6eb4f028c60 8067694: Improved certification checking
xuelei
parents: 30904
diff changeset
    95
     * If the local name service is not trustworthy, reverse host name
d6eb4f028c60 8067694: Improved certification checking
xuelei
parents: 30904
diff changeset
    96
     * resolution should not be performed for endpoint identification.
d6eb4f028c60 8067694: Improved certification checking
xuelei
parents: 30904
diff changeset
    97
     */
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    98
    private static final boolean trustNameService =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
    99
            Utilities.getBooleanProperty("jdk.tls.trustNameService", false);
31687
d6eb4f028c60 8067694: Improved certification checking
xuelei
parents: 30904
diff changeset
   100
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   101
    /**
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   102
     * Package-private constructor used to instantiate an unconnected
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   103
     * socket.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   104
     *
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   105
     * This instance is meant to set handshake state to use "client mode".
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   106
     */
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   107
    SSLSocketImpl(SSLContextImpl sslContext) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   108
        super();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   109
        this.sslContext = sslContext;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   110
        HandshakeHash handshakeHash = new HandshakeHash();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   111
        this.conContext = new TransportContext(sslContext, this,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   112
                new SSLSocketInputRecord(handshakeHash),
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   113
                new SSLSocketOutputRecord(handshakeHash), true);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   114
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   117
     * Package-private constructor used to instantiate a server socket.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   119
     * This instance is meant to set handshake state to use "server mode".
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   121
    SSLSocketImpl(SSLContextImpl sslContext, SSLConfiguration sslConfig) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        super();
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   123
        this.sslContext = sslContext;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   124
        HandshakeHash handshakeHash = new HandshakeHash();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   125
        this.conContext = new TransportContext(sslContext, this, sslConfig,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   126
                new SSLSocketInputRecord(handshakeHash),
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   127
                new SSLSocketOutputRecord(handshakeHash));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   131
     * Constructs an SSL connection to a named host at a specified
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   132
     * port, using the authentication context provided.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   134
     * This endpoint acts as the client, and may rejoin an existing SSL session
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   135
     * if appropriate.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   137
    SSLSocketImpl(SSLContextImpl sslContext, String peerHost,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   138
            int peerPort) throws IOException, UnknownHostException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        super();
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   140
        this.sslContext = sslContext;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   141
        HandshakeHash handshakeHash = new HandshakeHash();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   142
        this.conContext = new TransportContext(sslContext, this,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   143
                new SSLSocketInputRecord(handshakeHash),
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   144
                new SSLSocketOutputRecord(handshakeHash), true);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   145
        this.peerHost = peerHost;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   146
        SocketAddress socketAddress =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   147
               peerHost != null ? new InetSocketAddress(peerHost, peerPort) :
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   148
               new InetSocketAddress(InetAddress.getByName(null), peerPort);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        connect(socketAddress, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   153
     * Constructs an SSL connection to a server at a specified
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   154
     * address, and TCP port, using the authentication context
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   155
     * provided.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   157
     * This endpoint acts as the client, and may rejoin an existing SSL
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   158
     * session if appropriate.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   160
    SSLSocketImpl(SSLContextImpl sslContext,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   161
            InetAddress address, int peerPort) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        super();
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   163
        this.sslContext = sslContext;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   164
        HandshakeHash handshakeHash = new HandshakeHash();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   165
        this.conContext = new TransportContext(sslContext, this,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   166
                new SSLSocketInputRecord(handshakeHash),
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   167
                new SSLSocketOutputRecord(handshakeHash), true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   169
        SocketAddress socketAddress = new InetSocketAddress(address, peerPort);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        connect(socketAddress, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   173
    /**
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   174
     * Constructs an SSL connection to a named host at a specified
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   175
     * port, using the authentication context provided.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   176
     *
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   177
     * This endpoint acts as the client, and may rejoin an existing SSL
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   178
     * session if appropriate.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   180
    SSLSocketImpl(SSLContextImpl sslContext,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   181
            String peerHost, int peerPort, InetAddress localAddr,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   182
            int localPort) throws IOException, UnknownHostException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        super();
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   184
        this.sslContext = sslContext;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   185
        HandshakeHash handshakeHash = new HandshakeHash();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   186
        this.conContext = new TransportContext(sslContext, this,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   187
                new SSLSocketInputRecord(handshakeHash),
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   188
                new SSLSocketOutputRecord(handshakeHash), true);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   189
        this.peerHost = peerHost;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   191
        bind(new InetSocketAddress(localAddr, localPort));
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   192
        SocketAddress socketAddress =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   193
               peerHost != null ? new InetSocketAddress(peerHost, peerPort) :
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   194
               new InetSocketAddress(InetAddress.getByName(null), peerPort);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   195
        connect(socketAddress, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /**
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   199
     * Constructs an SSL connection to a server at a specified
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   200
     * address, and TCP port, using the authentication context
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   201
     * provided.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   203
     * This endpoint acts as the client, and may rejoin an existing SSL
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   204
     * session if appropriate.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   206
    SSLSocketImpl(SSLContextImpl sslContext,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   207
            InetAddress peerAddr, int peerPort,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   208
            InetAddress localAddr, int localPort) throws IOException {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   209
        super();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   210
        this.sslContext = sslContext;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   211
        HandshakeHash handshakeHash = new HandshakeHash();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   212
        this.conContext = new TransportContext(sslContext, this,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   213
                new SSLSocketInputRecord(handshakeHash),
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   214
                new SSLSocketOutputRecord(handshakeHash), true);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   215
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   216
        bind(new InetSocketAddress(localAddr, localPort));
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   217
        SocketAddress socketAddress = new InetSocketAddress(peerAddr, peerPort);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   218
        connect(socketAddress, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /**
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   222
     * Creates a server mode {@link Socket} layered over an
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   223
     * existing connected socket, and is able to read data which has
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   224
     * already been consumed/removed from the {@link Socket}'s
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   225
     * underlying {@link InputStream}.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   226
     */
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   227
    SSLSocketImpl(SSLContextImpl sslContext, Socket sock,
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   228
            InputStream consumed, boolean autoClose) throws IOException {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   229
        super(sock, consumed);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   230
        // We always layer over a connected socket
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   231
        if (!sock.isConnected()) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   232
            throw new SocketException("Underlying socket is not connected");
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   233
        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   234
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   235
        this.sslContext = sslContext;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   236
        HandshakeHash handshakeHash = new HandshakeHash();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   237
        this.conContext = new TransportContext(sslContext, this,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   238
                new SSLSocketInputRecord(handshakeHash),
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   239
                new SSLSocketOutputRecord(handshakeHash), false);
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   240
        this.autoClose = autoClose;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   241
        doneConnect();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   242
    }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   243
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   244
    /**
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   245
     * Layer SSL traffic over an existing connection, rather than
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   246
     * creating a new connection.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   247
     *
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   248
     * The existing connection may be used only for SSL traffic (using this
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   249
     * SSLSocket) until the SSLSocket.close() call returns. However, if a
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   250
     * protocol error is detected, that existing connection is automatically
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   251
     * closed.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   252
     * <p>
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   253
     * This particular constructor always uses the socket in the
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   254
     * role of an SSL client. It may be useful in cases which start
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   255
     * using SSL after some initial data transfers, for example in some
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   256
     * SSL tunneling applications or as part of some kinds of application
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   257
     * protocols which negotiate use of a SSL based security.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     */
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   259
    SSLSocketImpl(SSLContextImpl sslContext, Socket sock,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   260
            String peerHost, int port, boolean autoClose) throws IOException {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   261
        super(sock);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   262
        // We always layer over a connected socket
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   263
        if (!sock.isConnected()) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   264
            throw new SocketException("Underlying socket is not connected");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   265
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   267
        this.sslContext = sslContext;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   268
        HandshakeHash handshakeHash = new HandshakeHash();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   269
        this.conContext = new TransportContext(sslContext, this,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   270
                new SSLSocketInputRecord(handshakeHash),
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   271
                new SSLSocketOutputRecord(handshakeHash), true);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   272
        this.peerHost = peerHost;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   273
        this.autoClose = autoClose;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   274
        doneConnect();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   277
    @Override
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   278
    public void connect(SocketAddress endpoint,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   279
            int timeout) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   281
        if (isLayered()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            throw new SocketException("Already connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        if (!(endpoint instanceof InetSocketAddress)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            throw new SocketException(
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   287
                    "Cannot handle non-Inet socket addresses.");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        super.connect(endpoint, timeout);
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   291
        doneConnect();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   292
    }
37601
6517589dcc94 8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents: 36641
diff changeset
   293
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   294
    @Override
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   295
    public String[] getSupportedCipherSuites() {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   296
        return CipherSuite.namesOf(sslContext.getSupportedCipherSuites());
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   297
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   298
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   299
    @Override
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   300
    public String[] getEnabledCipherSuites() {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   301
        socketLock.lock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   302
        try {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   303
            return CipherSuite.namesOf(
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   304
                    conContext.sslConfig.enabledCipherSuites);
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   305
        } finally {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   306
            socketLock.unlock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   307
        }
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   308
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   309
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   310
    @Override
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   311
    public void setEnabledCipherSuites(String[] suites) {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   312
        socketLock.lock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   313
        try {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   314
            conContext.sslConfig.enabledCipherSuites =
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   315
                    CipherSuite.validValuesOf(suites);
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   316
        } finally {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   317
            socketLock.unlock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   318
        }
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   319
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   320
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   321
    @Override
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   322
    public String[] getSupportedProtocols() {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   323
        return ProtocolVersion.toStringArray(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   324
                sslContext.getSupportedProtocolVersions());
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   325
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   326
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   327
    @Override
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   328
    public String[] getEnabledProtocols() {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   329
        socketLock.lock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   330
        try {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   331
            return ProtocolVersion.toStringArray(
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   332
                    conContext.sslConfig.enabledProtocols);
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   333
        } finally {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   334
            socketLock.unlock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   335
        }
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   336
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   337
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   338
    @Override
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   339
    public void setEnabledProtocols(String[] protocols) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   340
        if (protocols == null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   341
            throw new IllegalArgumentException("Protocols cannot be null");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   342
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   343
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   344
        socketLock.lock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   345
        try {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   346
            conContext.sslConfig.enabledProtocols =
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   347
                    ProtocolVersion.namesOf(protocols);
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   348
        } finally {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   349
            socketLock.unlock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   350
        }
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   351
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   352
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   353
    @Override
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   354
    public SSLSession getSession() {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   355
        try {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   356
            // start handshaking, if failed, the connection will be closed.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   357
            ensureNegotiated();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   358
        } catch (IOException ioe) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   359
            if (SSLLogger.isOn && SSLLogger.isOn("handshake")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   360
                SSLLogger.severe("handshake failed", ioe);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   361
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   362
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   363
            return SSLSessionImpl.nullSession;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   364
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   365
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   366
        return conContext.conSession;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   367
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   368
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   369
    @Override
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   370
    public SSLSession getHandshakeSession() {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   371
        socketLock.lock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   372
        try {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   373
            return conContext.handshakeContext == null ?
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   374
                    null : conContext.handshakeContext.handshakeSession;
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   375
        } finally {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   376
            socketLock.unlock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   377
        }
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   378
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   379
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   380
    @Override
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   381
    public void addHandshakeCompletedListener(
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   382
            HandshakeCompletedListener listener) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   383
        if (listener == null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   384
            throw new IllegalArgumentException("listener is null");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   385
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   386
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   387
        socketLock.lock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   388
        try {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   389
            conContext.sslConfig.addHandshakeCompletedListener(listener);
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   390
        } finally {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   391
            socketLock.unlock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   392
        }
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   393
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   394
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   395
    @Override
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   396
    public void removeHandshakeCompletedListener(
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   397
            HandshakeCompletedListener listener) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   398
        if (listener == null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   399
            throw new IllegalArgumentException("listener is null");
37601
6517589dcc94 8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents: 36641
diff changeset
   400
        }
6517589dcc94 8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents: 36641
diff changeset
   401
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   402
        socketLock.lock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   403
        try {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   404
            conContext.sslConfig.removeHandshakeCompletedListener(listener);
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   405
        } finally {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   406
            socketLock.unlock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   407
        }
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   408
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   409
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   410
    @Override
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   411
    public void startHandshake() throws IOException {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   412
        if (!isConnected) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   413
            throw new SocketException("Socket is not connected");
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   414
        }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   415
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   416
        if (conContext.isBroken || conContext.isInboundClosed() ||
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   417
                conContext.isOutboundClosed()) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   418
            throw new SocketException("Socket has been closed or broken");
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   419
        }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   420
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   421
        handshakeLock.lock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   422
        try {
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   423
            // double check the context status
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   424
            if (conContext.isBroken || conContext.isInboundClosed() ||
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   425
                    conContext.isOutboundClosed()) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   426
                throw new SocketException("Socket has been closed or broken");
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   427
            }
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   428
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   429
            try {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   430
                conContext.kickstart();
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   431
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   432
                // All initial handshaking goes through this operation until we
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   433
                // have a valid SSL connection.
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   434
                //
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   435
                // Handle handshake messages only, need no application data.
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   436
                if (!conContext.isNegotiated) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   437
                    readHandshakeRecord();
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   438
                }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   439
            } catch (IOException ioe) {
53064
103ed9569fc8 8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents: 53018
diff changeset
   440
                throw conContext.fatal(Alert.HANDSHAKE_FAILURE,
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   441
                    "Couldn't kickstart handshaking", ioe);
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   442
            } catch (Exception oe) {    // including RuntimeException
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   443
                handleException(oe);
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   444
            }
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   445
        } finally {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   446
            handshakeLock.unlock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   447
        }
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   448
    }
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   449
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   450
    @Override
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   451
    public void setUseClientMode(boolean mode) {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   452
        socketLock.lock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   453
        try {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   454
            conContext.setUseClientMode(mode);
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   455
        } finally {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   456
            socketLock.unlock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   457
        }
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   458
    }
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   459
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   460
    @Override
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   461
    public boolean getUseClientMode() {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   462
        socketLock.lock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   463
        try {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   464
            return conContext.sslConfig.isClientMode;
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   465
        } finally {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   466
            socketLock.unlock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   467
        }
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   468
    }
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   469
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   470
    @Override
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   471
    public void setNeedClientAuth(boolean need) {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   472
        socketLock.lock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   473
        try {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   474
            conContext.sslConfig.clientAuthType =
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   475
                    (need ? ClientAuthType.CLIENT_AUTH_REQUIRED :
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   476
                            ClientAuthType.CLIENT_AUTH_NONE);
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   477
        } finally {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   478
            socketLock.unlock();
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   479
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   480
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   481
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   482
    @Override
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   483
    public boolean getNeedClientAuth() {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   484
        socketLock.lock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   485
        try {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   486
            return (conContext.sslConfig.clientAuthType ==
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   487
                        ClientAuthType.CLIENT_AUTH_REQUIRED);
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   488
        } finally {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   489
            socketLock.unlock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   490
        }
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   491
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   492
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   493
    @Override
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   494
    public void setWantClientAuth(boolean want) {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   495
        socketLock.lock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   496
        try {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   497
            conContext.sslConfig.clientAuthType =
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   498
                    (want ? ClientAuthType.CLIENT_AUTH_REQUESTED :
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   499
                            ClientAuthType.CLIENT_AUTH_NONE);
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   500
        } finally {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   501
            socketLock.unlock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   502
        }
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   503
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   504
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   505
    @Override
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   506
    public boolean getWantClientAuth() {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   507
        socketLock.lock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   508
        try {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   509
            return (conContext.sslConfig.clientAuthType ==
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   510
                        ClientAuthType.CLIENT_AUTH_REQUESTED);
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   511
        } finally {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   512
            socketLock.unlock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   513
        }
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   514
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   515
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   516
    @Override
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   517
    public void setEnableSessionCreation(boolean flag) {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   518
        socketLock.lock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   519
        try {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   520
            conContext.sslConfig.enableSessionCreation = flag;
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   521
        } finally {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   522
            socketLock.unlock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   523
        }
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   524
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   525
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   526
    @Override
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   527
    public boolean getEnableSessionCreation() {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   528
        socketLock.lock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   529
        try {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   530
            return conContext.sslConfig.enableSessionCreation;
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   531
        } finally {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   532
            socketLock.unlock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   533
        }
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   534
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   535
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   536
    @Override
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   537
    public boolean isClosed() {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   538
        return tlsIsClosed;
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   539
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   540
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   541
    // Please don't synchronized this method.  Otherwise, the read and close
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   542
    // locks may be deadlocked.
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   543
    @Override
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   544
    public void close() throws IOException {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   545
        if (tlsIsClosed) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   546
            return;
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   547
        }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   548
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   549
        if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   550
            SSLLogger.fine("duplex close of SSLSocket");
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   551
        }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   552
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   553
        try {
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   554
            // shutdown output bound, which may have been closed previously.
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   555
            if (!isOutputShutdown()) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   556
                duplexCloseOutput();
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   557
            }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   558
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   559
            // shutdown input bound, which may have been closed previously.
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   560
            if (!isInputShutdown()) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   561
                duplexCloseInput();
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   562
            }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   563
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   564
            if (!isClosed()) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   565
                // close the connection directly
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   566
                closeSocket(false);
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   567
            }
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   568
        } catch (IOException ioe) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   569
            // ignore the exception
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   570
            if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   571
                SSLLogger.warning("SSLSocket duplex close failed", ioe);
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   572
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   573
        } finally {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   574
            tlsIsClosed = true;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   575
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   576
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   577
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   578
    /**
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   579
     * Duplex close, start from closing outbound.
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   580
     *
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   581
     * For TLS 1.2 [RFC 5246], unless some other fatal alert has been
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   582
     * transmitted, each party is required to send a close_notify alert
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   583
     * before closing the write side of the connection.  The other party
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   584
     * MUST respond with a close_notify alert of its own and close down
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   585
     * the connection immediately, discarding any pending writes.  It is
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   586
     * not required for the initiator of the close to wait for the responding
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   587
     * close_notify alert before closing the read side of the connection.
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   588
     *
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   589
     * For TLS 1.3, Each party MUST send a close_notify alert before
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   590
     * closing its write side of the connection, unless it has already sent
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   591
     * some error alert.  This does not have any effect on its read side of
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   592
     * the connection.  Both parties need not wait to receive a close_notify
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   593
     * alert before closing their read side of the connection, though doing
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   594
     * so would introduce the possibility of truncation.
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   595
     *
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   596
     * In order to support user initiated duplex-close for TLS 1.3 connections,
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   597
     * the user_canceled alert is used together with the close_notify alert.
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   598
     */
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   599
    private void duplexCloseOutput() throws IOException {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   600
        boolean useUserCanceled = false;
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   601
        boolean hasCloseReceipt = false;
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   602
        if (conContext.isNegotiated) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   603
            if (!conContext.protocolVersion.useTLS13PlusSpec()) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   604
                hasCloseReceipt = true;
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   605
            } else {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   606
                // Use a user_canceled alert for TLS 1.3 duplex close.
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   607
                useUserCanceled = true;
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   608
            }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   609
        } else if (conContext.handshakeContext != null) {   // initial handshake
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   610
            // Use user_canceled alert regardless the protocol versions.
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   611
            useUserCanceled = true;
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   612
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   613
            // The protocol version may have been negotiated.
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   614
            ProtocolVersion pv = conContext.handshakeContext.negotiatedProtocol;
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   615
            if (pv == null || (!pv.useTLS13PlusSpec())) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   616
                hasCloseReceipt = true;
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   617
            }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   618
        }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   619
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   620
        // Need a lock here so that the user_canceled alert and the
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   621
        // close_notify alert can be delivered together.
55411
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   622
        int linger = getSoLinger();
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   623
        if (linger >= 0) {
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   624
            // don't wait more than SO_LINGER for obtaining the
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   625
            // the lock.
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   626
            //
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   627
            // keep and clear the current thread interruption status.
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   628
            boolean interrupted = Thread.interrupted();
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   629
            try {
55411
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   630
                if (conContext.outputRecord.recordLock.tryLock() ||
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   631
                        conContext.outputRecord.recordLock.tryLock(
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   632
                                linger, TimeUnit.SECONDS)) {
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   633
                    try {
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   634
                        handleClosedNotifyAlert(useUserCanceled);
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   635
                    } finally {
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   636
                        conContext.outputRecord.recordLock.unlock();
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   637
                    }
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   638
                } else {
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   639
                    // For layered, non-autoclose sockets, we are not
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   640
                    // able to bring them into a usable state, so we
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   641
                    // treat it as fatal error.
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   642
                    if (!super.isOutputShutdown()) {
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   643
                        if (isLayered() && !autoClose) {
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   644
                            throw new SSLException(
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   645
                                    "SO_LINGER timeout, " +
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   646
                                    "close_notify message cannot be sent.");
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   647
                        } else {
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   648
                            super.shutdownOutput();
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   649
                            if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   650
                                SSLLogger.warning(
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   651
                                    "SSLSocket output duplex close failed: " +
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   652
                                    "SO_LINGER timeout, " +
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   653
                                    "close_notify message cannot be sent.");
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   654
                            }
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   655
                        }
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   656
                    }
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   657
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   658
                    // RFC2246 requires that the session becomes
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   659
                    // unresumable if any connection is terminated
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   660
                    // without proper close_notify messages with
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   661
                    // level equal to warning.
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   662
                    //
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   663
                    // RFC4346 no longer requires that a session not be
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   664
                    // resumed if failure to properly close a connection.
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   665
                    //
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   666
                    // We choose to make the session unresumable if
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   667
                    // failed to send the close_notify message.
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   668
                    //
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   669
                    conContext.conSession.invalidate();
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   670
                    if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   671
                        SSLLogger.warning(
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   672
                                "Invalidate the session: SO_LINGER timeout, " +
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   673
                                "close_notify message cannot be sent.");
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   674
                    }
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   675
                }
55411
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   676
            } catch (InterruptedException ex) {
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   677
                // keep interrupted status
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   678
                interrupted = true;
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   679
            }
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   680
55411
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   681
            // restore the interrupted status
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   682
            if (interrupted) {
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   683
                Thread.currentThread().interrupt();
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   684
            }
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   685
        } else {
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   686
            conContext.outputRecord.recordLock.lock();
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   687
            try {
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   688
                handleClosedNotifyAlert(useUserCanceled);
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   689
            } finally {
55411
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   690
                conContext.outputRecord.recordLock.unlock();
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   691
            }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   692
        }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   693
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   694
        if (!isInputShutdown()) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   695
            bruteForceCloseInput(hasCloseReceipt);
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   696
        }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   697
    }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   698
55411
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   699
    private void handleClosedNotifyAlert(
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   700
            boolean useUserCanceled) throws IOException {
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   701
        try {
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   702
            // send a user_canceled alert if needed.
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   703
            if (useUserCanceled) {
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   704
                conContext.warning(Alert.USER_CANCELED);
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   705
            }
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   706
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   707
            // send a close_notify alert
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   708
            conContext.warning(Alert.CLOSE_NOTIFY);
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   709
        } finally {
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   710
            if (!conContext.isOutboundClosed()) {
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   711
                conContext.outputRecord.close();
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   712
            }
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   713
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   714
            if (!super.isOutputShutdown() &&
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   715
                    (autoClose || !isLayered())) {
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   716
                super.shutdownOutput();
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   717
            }
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   718
        }
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   719
    }
328d4a455e4b 8224829: AsyncSSLSocketClose.java has timing issue
xuelei
parents: 54718
diff changeset
   720
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   721
    /**
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   722
     * Duplex close, start from closing inbound.
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   723
     *
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   724
     * This method should only be called when the outbound has been closed,
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   725
     * but the inbound is still open.
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   726
     */
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   727
    private void duplexCloseInput() throws IOException {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   728
        boolean hasCloseReceipt = false;
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   729
        if (conContext.isNegotiated &&
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   730
                !conContext.protocolVersion.useTLS13PlusSpec()) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   731
            hasCloseReceipt = true;
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   732
        }   // No close receipt if handshake has no completed.
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   733
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   734
        bruteForceCloseInput(hasCloseReceipt);
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   735
    }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   736
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   737
    /**
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   738
     * Brute force close the input bound.
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   739
     *
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   740
     * This method should only be called when the outbound has been closed,
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   741
     * but the inbound is still open.
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   742
     */
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   743
    private void bruteForceCloseInput(
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   744
            boolean hasCloseReceipt) throws IOException {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   745
        if (hasCloseReceipt) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   746
            // It is not required for the initiator of the close to wait for
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   747
            // the responding close_notify alert before closing the read side
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   748
            // of the connection.  However, if the application protocol using
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   749
            // TLS provides that any data may be carried over the underlying
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   750
            // transport after the TLS connection is closed, the TLS
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   751
            // implementation MUST receive a "close_notify" alert before
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   752
            // indicating end-of-data to the application-layer.
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   753
            try {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   754
                this.shutdown();
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   755
            } finally {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   756
                if (!isInputShutdown()) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   757
                    shutdownInput(false);
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   758
                }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   759
            }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   760
        } else {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   761
            if (!conContext.isInboundClosed()) {
53067
8a61a04c456c 8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents: 53064
diff changeset
   762
                try (conContext.inputRecord) {
8a61a04c456c 8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents: 53064
diff changeset
   763
                    // Try the best to use up the input records and close the
8a61a04c456c 8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents: 53064
diff changeset
   764
                    // socket gracefully, without impact the performance too
8a61a04c456c 8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents: 53064
diff changeset
   765
                    // much.
8a61a04c456c 8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents: 53064
diff changeset
   766
                    appInput.deplete();
8a61a04c456c 8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents: 53064
diff changeset
   767
                }
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   768
            }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   769
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   770
            if ((autoClose || !isLayered()) && !super.isInputShutdown()) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   771
                super.shutdownInput();
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   772
            }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   773
        }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   774
    }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   775
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   776
    // Please don't synchronized this method.  Otherwise, the read and close
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   777
    // locks may be deadlocked.
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   778
    @Override
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   779
    public void shutdownInput() throws IOException {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   780
        shutdownInput(true);
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   781
    }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   782
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   783
    // It is not required to check the close_notify receipt unless an
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   784
    // application call shutdownInput() explicitly.
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   785
    private void shutdownInput(
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   786
            boolean checkCloseNotify) throws IOException {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   787
        if (isInputShutdown()) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   788
            return;
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   789
        }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   790
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   791
        if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   792
            SSLLogger.fine("close inbound of SSLSocket");
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   793
        }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   794
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   795
        // Is it ready to close inbound?
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   796
        //
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   797
        // No need to throw exception if the initial handshake is not started.
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   798
        if (checkCloseNotify && !conContext.isInputCloseNotified &&
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   799
            (conContext.isNegotiated || conContext.handshakeContext != null)) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   800
53064
103ed9569fc8 8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents: 53018
diff changeset
   801
            throw conContext.fatal(Alert.INTERNAL_ERROR,
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   802
                    "closing inbound before receiving peer's close_notify");
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   803
        }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   804
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   805
        conContext.closeInbound();
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   806
        if ((autoClose || !isLayered()) && !super.isInputShutdown()) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   807
            super.shutdownInput();
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   808
        }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   809
    }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   810
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   811
    @Override
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   812
    public boolean isInputShutdown() {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   813
        return conContext.isInboundClosed() &&
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   814
                ((autoClose || !isLayered()) ? super.isInputShutdown(): true);
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   815
    }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   816
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   817
    // Please don't synchronized this method.  Otherwise, the read and close
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   818
    // locks may be deadlocked.
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   819
    @Override
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   820
    public void shutdownOutput() throws IOException {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   821
        if (isOutputShutdown()) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   822
            return;
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   823
        }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   824
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   825
        if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   826
            SSLLogger.fine("close outbound of SSLSocket");
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   827
        }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   828
        conContext.closeOutbound();
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   829
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   830
        if ((autoClose || !isLayered()) && !super.isOutputShutdown()) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   831
            super.shutdownOutput();
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   832
        }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   833
    }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   834
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   835
    @Override
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   836
    public boolean isOutputShutdown() {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   837
        return conContext.isOutboundClosed() &&
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   838
                ((autoClose || !isLayered()) ? super.isOutputShutdown(): true);
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   839
    }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   840
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   841
    @Override
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   842
    public InputStream getInputStream() throws IOException {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   843
        socketLock.lock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   844
        try {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   845
            if (isClosed()) {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   846
                throw new SocketException("Socket is closed");
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   847
            }
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   848
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   849
            if (!isConnected) {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   850
                throw new SocketException("Socket is not connected");
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   851
            }
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   852
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   853
            if (conContext.isInboundClosed() || isInputShutdown()) {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   854
                throw new SocketException("Socket input is already shutdown");
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   855
            }
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   856
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   857
            return appInput;
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   858
        } finally {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   859
            socketLock.unlock();
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   860
        }
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   861
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   862
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   863
    private void ensureNegotiated() throws IOException {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   864
        if (conContext.isNegotiated || conContext.isBroken ||
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   865
                conContext.isInboundClosed() || conContext.isOutboundClosed()) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   866
            return;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   867
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   868
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   869
        handshakeLock.lock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   870
        try {
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   871
            // double check the context status
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   872
            if (conContext.isNegotiated || conContext.isBroken ||
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   873
                    conContext.isInboundClosed() ||
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   874
                    conContext.isOutboundClosed()) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   875
                return;
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   876
            }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   877
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   878
            startHandshake();
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   879
        } finally {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   880
            handshakeLock.unlock();
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   881
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    /**
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   885
     * InputStream for application data as returned by
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   886
     * SSLSocket.getInputStream().
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     */
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   888
    private class AppInputStream extends InputStream {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   889
        // One element array used to implement the single byte read() method
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   890
        private final byte[] oneByte = new byte[1];
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   891
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   892
        // the temporary buffer used to read network
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   893
        private ByteBuffer buffer;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   894
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   895
        // Is application data available in the stream?
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   896
        private volatile boolean appDataIsAvailable;
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   897
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   898
        // reading lock
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   899
        private final ReentrantLock readLock = new ReentrantLock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   900
54718
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
   901
        // closing status
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
   902
        private volatile boolean isClosing;
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
   903
        private volatile boolean hasDepleted;
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
   904
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   905
        AppInputStream() {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   906
            this.appDataIsAvailable = false;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   907
            this.buffer = ByteBuffer.allocate(4096);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   908
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   909
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   910
        /**
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   911
         * Return the minimum number of bytes that can be read
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   912
         * without blocking.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   913
         */
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   914
        @Override
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   915
        public int available() throws IOException {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   916
            // Currently not synchronized.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   917
            if ((!appDataIsAvailable) || checkEOF()) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   918
                return 0;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   919
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   920
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   921
            return buffer.remaining();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   922
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   923
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   924
        /**
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   925
         * Read a single byte, returning -1 on non-fault EOF status.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
         */
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   927
        @Override
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   928
        public int read() throws IOException {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   929
            int n = read(oneByte, 0, 1);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   930
            if (n <= 0) {   // EOF
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   931
                return -1;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   932
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   933
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   934
            return oneByte[0] & 0xFF;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   935
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   937
        /**
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   938
         * Reads up to {@code len} bytes of data from the input stream
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   939
         * into an array of bytes.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   940
         *
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   941
         * An attempt is made to read as many as {@code len} bytes, but a
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   942
         * smaller number may be read. The number of bytes actually read
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   943
         * is returned as an integer.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   944
         *
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   945
         * If the layer above needs more data, it asks for more, so we
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   946
         * are responsible only for blocking to fill at most one buffer,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   947
         * and returning "-1" on non-fault EOF status.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   948
         */
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   949
        @Override
54718
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
   950
        public int read(byte[] b, int off, int len) throws IOException {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   951
            if (b == null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   952
                throw new NullPointerException("the target buffer is null");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   953
            } else if (off < 0 || len < 0 || len > b.length - off) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   954
                throw new IndexOutOfBoundsException(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   955
                        "buffer length: " + b.length + ", offset; " + off +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   956
                        ", bytes to read:" + len);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   957
            } else if (len == 0) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   958
                return 0;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   959
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   960
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   961
            if (checkEOF()) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   962
                return -1;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   963
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   964
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   965
            // start handshaking if the connection has not been negotiated.
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   966
            if (!conContext.isNegotiated && !conContext.isBroken &&
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   967
                    !conContext.isInboundClosed() &&
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   968
                    !conContext.isOutboundClosed()) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   969
                ensureNegotiated();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   970
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   971
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   972
            // Check if the Socket is invalid (error or closed).
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   973
            if (!conContext.isNegotiated ||
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   974
                    conContext.isBroken || conContext.isInboundClosed()) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   975
                throw new SocketException("Connection or inbound has closed");
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   976
            }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   977
54718
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
   978
            // Check if the input stream has been depleted.
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
   979
            //
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
   980
            // Note that the "hasDepleted" rather than the isClosing
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
   981
            // filed is checked here, in case the closing process is
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
   982
            // still in progress.
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
   983
            if (hasDepleted) {
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
   984
                if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
   985
                    SSLLogger.fine("The input stream has been depleted");
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
   986
                }
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
   987
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
   988
                return -1;
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
   989
            }
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
   990
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
   991
            // Read the available bytes at first.
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   992
            //
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   993
            // Note that the receiving and processing of post-handshake message
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
   994
            // are also synchronized with the read lock.
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   995
            readLock.lock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
   996
            try {
54718
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
   997
                // Double check if the Socket is invalid (error or closed).
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
   998
                if (conContext.isBroken || conContext.isInboundClosed()) {
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
   999
                    throw new SocketException(
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1000
                            "Connection or inbound has closed");
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1001
                }
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1002
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1003
                // Double check if the input stream has been depleted.
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1004
                if (hasDepleted) {
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1005
                    if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1006
                        SSLLogger.fine("The input stream is closing");
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1007
                    }
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1008
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1009
                    return -1;
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1010
                }
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1011
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1012
                int remains = available();
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1013
                if (remains > 0) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1014
                    int howmany = Math.min(remains, len);
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1015
                    buffer.get(b, off, howmany);
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29923
diff changeset
  1016
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1017
                    return howmany;
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1018
                }
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1019
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1020
                appDataIsAvailable = false;
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1021
                try {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1022
                    ByteBuffer bb = readApplicationRecord(buffer);
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1023
                    if (bb == null) {   // EOF
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1024
                        return -1;
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1025
                    } else {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1026
                        // The buffer may be reallocated for bigger capacity.
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1027
                        buffer = bb;
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1028
                    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1029
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1030
                    bb.flip();
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1031
                    int volume = Math.min(len, bb.remaining());
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1032
                    buffer.get(b, off, volume);
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1033
                    appDataIsAvailable = true;
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1034
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1035
                    return volume;
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1036
                } catch (Exception e) {   // including RuntimeException
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1037
                    // shutdown and rethrow (wrapped) exception as appropriate
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1038
                    handleException(e);
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1039
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1040
                    // dummy for compiler
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1041
                    return -1;
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1042
                }
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1043
            } finally {
54718
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1044
                // Check if the input stream is closing.
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1045
                //
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1046
                // If the deplete() did not hold the lock, clean up the
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1047
                // input stream here.
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1048
                try {
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1049
                    if (isClosing) {
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1050
                        readLockedDeplete();
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1051
                    }
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1052
                } finally {
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1053
                    readLock.unlock();
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1054
                }
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1055
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1056
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1058
        /**
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1059
         * Skip n bytes.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1060
         *
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1061
         * This implementation is somewhat less efficient than possible, but
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1062
         * not badly so (redundant copy).  We reuse the read() code to keep
51986
c1db377f6300 8200381: Typos in javadoc - missing verb "be" and alike
igerasim
parents: 51407
diff changeset
  1063
         * things simpler.
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1064
         */
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1065
        @Override
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1066
        public long skip(long n) throws IOException {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1067
            // dummy array used to implement skip()
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1068
            byte[] skipArray = new byte[256];
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1069
            long skipped = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1071
            readLock.lock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1072
            try {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1073
                while (n > 0) {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1074
                    int len = (int)Math.min(n, skipArray.length);
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1075
                    int r = read(skipArray, 0, len);
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1076
                    if (r <= 0) {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1077
                        break;
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1078
                    }
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1079
                    n -= r;
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1080
                    skipped += r;
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1081
                }
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1082
            } finally {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1083
                readLock.unlock();
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1084
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1085
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1086
            return skipped;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1087
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1088
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1089
        @Override
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1090
        public void close() throws IOException {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1091
            if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1092
                SSLLogger.finest("Closing input stream");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1093
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1094
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1095
            try {
54551
6f8a7671afef 8216326: SSLSocket stream close() does not close the associated socket
xuelei
parents: 54443
diff changeset
  1096
                SSLSocketImpl.this.close();
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1097
            } catch (IOException ioe) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1098
                // ignore the exception
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1099
                if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1100
                    SSLLogger.warning("input stream close failed", ioe);
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1101
                }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1102
            }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1103
        }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1104
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1105
        /**
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1106
         * Return whether we have reached end-of-file.
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1107
         *
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1108
         * If the socket is not connected, has been shutdown because of an error
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1109
         * or has been closed, throw an Exception.
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1110
         */
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1111
        private boolean checkEOF() throws IOException {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1112
            if (conContext.isInboundClosed()) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1113
                return true;
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1114
            } else if (conContext.isInputCloseNotified || conContext.isBroken) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1115
                if (conContext.closeReason == null) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1116
                    return true;
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1117
                } else {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1118
                    throw new SSLException(
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1119
                        "Connection has closed: " + conContext.closeReason,
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1120
                        conContext.closeReason);
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1121
                }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1122
            }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1123
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1124
            return false;
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1125
        }
53067
8a61a04c456c 8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents: 53064
diff changeset
  1126
8a61a04c456c 8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents: 53064
diff changeset
  1127
        /**
8a61a04c456c 8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents: 53064
diff changeset
  1128
         * Try the best to use up the input records so as to close the
8a61a04c456c 8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents: 53064
diff changeset
  1129
         * socket gracefully, without impact the performance too much.
8a61a04c456c 8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents: 53064
diff changeset
  1130
         */
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1131
        private void deplete() {
54718
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1132
            if (conContext.isInboundClosed() || isClosing) {
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1133
                return;
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1134
            }
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1135
54718
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1136
            isClosing = true;
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1137
            if (readLock.tryLock()) {
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1138
                try {
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1139
                    readLockedDeplete();
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1140
                } finally {
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1141
                    readLock.unlock();
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1142
                }
54718
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1143
            }
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1144
        }
53067
8a61a04c456c 8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents: 53064
diff changeset
  1145
54718
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1146
        /**
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1147
         * Try to use up the input records.
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1148
         *
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1149
         * Please don't call this method unless the readLock is held by
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1150
         * the current thread.
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1151
         */
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1152
        private void readLockedDeplete() {
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1153
            // double check
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1154
            if (hasDepleted || conContext.isInboundClosed()) {
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1155
                return;
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1156
            }
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1157
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1158
            if (!(conContext.inputRecord instanceof SSLSocketInputRecord)) {
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1159
                return;
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1160
            }
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1161
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1162
            SSLSocketInputRecord socketInputRecord =
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1163
                    (SSLSocketInputRecord)conContext.inputRecord;
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1164
            try {
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1165
                socketInputRecord.deplete(
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1166
                    conContext.isNegotiated && (getSoTimeout() > 0));
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1167
            } catch (Exception ex) {
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1168
                if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1169
                    SSLLogger.warning(
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1170
                        "input stream close depletion failed", ex);
53067
8a61a04c456c 8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents: 53064
diff changeset
  1171
                }
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1172
            } finally {
54718
ca251ef47e0b 8219991: New fix of the deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents: 54551
diff changeset
  1173
                hasDepleted = true;
53067
8a61a04c456c 8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents: 53064
diff changeset
  1174
            }
8a61a04c456c 8209333: Socket reset issue for TLS 1.3 socket close
xuelei
parents: 53064
diff changeset
  1175
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1178
    @Override
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1179
    public OutputStream getOutputStream() throws IOException {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1180
        socketLock.lock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1181
        try {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1182
            if (isClosed()) {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1183
                throw new SocketException("Socket is closed");
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1184
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1186
            if (!isConnected) {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1187
                throw new SocketException("Socket is not connected");
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1188
            }
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1189
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1190
            if (conContext.isOutboundDone() || isOutputShutdown()) {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1191
                throw new SocketException("Socket output is already shutdown");
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1192
            }
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1193
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1194
            return appOutput;
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1195
        } finally {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1196
            socketLock.unlock();
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1197
        }
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1198
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1199
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1200
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1201
    /**
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1202
     * OutputStream for application data as returned by
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1203
     * SSLSocket.getOutputStream().
12428
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
  1204
     */
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1205
    private class AppOutputStream extends OutputStream {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1206
        // One element array used to implement the write(byte) method
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1207
        private final byte[] oneByte = new byte[1];
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1208
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1209
        @Override
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1210
        public void write(int i) throws IOException {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1211
            oneByte[0] = (byte)i;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1212
            write(oneByte, 0, 1);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1213
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1214
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1215
        @Override
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1216
        public void write(byte[] b,
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1217
                int off, int len) throws IOException {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1218
            if (b == null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1219
                throw new NullPointerException("the source buffer is null");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1220
            } else if (off < 0 || len < 0 || len > b.length - off) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1221
                throw new IndexOutOfBoundsException(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1222
                        "buffer length: " + b.length + ", offset; " + off +
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1223
                        ", bytes to read:" + len);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1224
            } else if (len == 0) {
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1225
                //
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1226
                // Don't bother to really write empty records.  We went this
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1227
                // far to drive the handshake machinery, for correctness; not
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1228
                // writing empty records improves performance by cutting CPU
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1229
                // time and network resource usage.  However, some protocol
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1230
                // implementations are fragile and don't like to see empty
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1231
                // records, so this also increases robustness.
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1232
                //
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1233
                return;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1234
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1235
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1236
            // Start handshaking if the connection has not been negotiated.
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1237
            if (!conContext.isNegotiated && !conContext.isBroken &&
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1238
                    !conContext.isInboundClosed() &&
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1239
                    !conContext.isOutboundClosed()) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1240
                ensureNegotiated();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1241
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1242
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1243
            // Check if the Socket is invalid (error or closed).
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1244
            if (!conContext.isNegotiated ||
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1245
                    conContext.isBroken || conContext.isOutboundClosed()) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1246
                throw new SocketException("Connection or outbound has closed");
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1247
            }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1248
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1249
            //
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1251
            // Delegate the writing to the underlying socket.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1252
            try {
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1253
                conContext.outputRecord.deliver(b, off, len);
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1254
            } catch (SSLHandshakeException she) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1255
                // may be record sequence number overflow
53064
103ed9569fc8 8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents: 53018
diff changeset
  1256
                throw conContext.fatal(Alert.HANDSHAKE_FAILURE, she);
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1257
            } catch (IOException e) {
53064
103ed9569fc8 8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents: 53018
diff changeset
  1258
                throw conContext.fatal(Alert.UNEXPECTED_MESSAGE, e);
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1259
            }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1260
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1261
            // Is the sequence number is nearly overflow, or has the key usage
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1262
            // limit been reached?
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1263
            if (conContext.outputRecord.seqNumIsHuge() ||
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1264
                    conContext.outputRecord.writeCipher.atKeyLimit()) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1265
                tryKeyUpdate();
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1266
            }
57485
af4b0fc25bc4 8226338: Updates to Stateless Resumption
ascarpino
parents: 55411
diff changeset
  1267
            // Check if NewSessionTicket PostHandshake message needs to be sent
af4b0fc25bc4 8226338: Updates to Stateless Resumption
ascarpino
parents: 55411
diff changeset
  1268
            if (conContext.conSession.updateNST) {
af4b0fc25bc4 8226338: Updates to Stateless Resumption
ascarpino
parents: 55411
diff changeset
  1269
                conContext.conSession.updateNST = false;
af4b0fc25bc4 8226338: Updates to Stateless Resumption
ascarpino
parents: 55411
diff changeset
  1270
                tryNewSessionTicket();
af4b0fc25bc4 8226338: Updates to Stateless Resumption
ascarpino
parents: 55411
diff changeset
  1271
            }
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1272
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1274
        @Override
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1275
        public void close() throws IOException {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1276
            if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1277
                SSLLogger.finest("Closing output stream");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1278
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1279
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1280
            try {
54551
6f8a7671afef 8216326: SSLSocket stream close() does not close the associated socket
xuelei
parents: 54443
diff changeset
  1281
                SSLSocketImpl.this.close();
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1282
            } catch (IOException ioe) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1283
                // ignore the exception
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1284
                if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1285
                    SSLLogger.warning("output stream close failed", ioe);
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1286
                }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1287
            }
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1288
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1289
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1290
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1291
    @Override
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1292
    public SSLParameters getSSLParameters() {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1293
        socketLock.lock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1294
        try {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1295
            return conContext.sslConfig.getSSLParameters();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1296
        } finally {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1297
            socketLock.unlock();
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1298
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1299
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1300
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1301
    @Override
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1302
    public void setSSLParameters(SSLParameters params) {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1303
        socketLock.lock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1304
        try {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1305
            conContext.sslConfig.setSSLParameters(params);
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1306
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1307
            if (conContext.sslConfig.maximumPacketSize != 0) {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1308
                conContext.outputRecord.changePacketSize(
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1309
                        conContext.sslConfig.maximumPacketSize);
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1310
            }
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1311
        } finally {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1312
            socketLock.unlock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1313
        }
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1314
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1315
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1316
    @Override
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1317
    public String getApplicationProtocol() {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1318
        socketLock.lock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1319
        try {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1320
            return conContext.applicationProtocol;
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1321
        } finally {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1322
            socketLock.unlock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1323
        }
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1324
    }
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1325
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1326
    @Override
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1327
    public String getHandshakeApplicationProtocol() {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1328
        socketLock.lock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1329
        try {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1330
            if (conContext.handshakeContext != null) {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1331
                return conContext.handshakeContext.applicationProtocol;
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1332
            }
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1333
        } finally {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1334
            socketLock.unlock();
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1335
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1337
        return null;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1338
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1339
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1340
    @Override
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1341
    public void setHandshakeApplicationProtocolSelector(
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1342
            BiFunction<SSLSocket, List<String>, String> selector) {
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1343
        socketLock.lock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1344
        try {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1345
            conContext.sslConfig.socketAPSelector = selector;
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1346
        } finally {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1347
            socketLock.unlock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1348
        }
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1349
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1351
    @Override
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1352
    public BiFunction<SSLSocket, List<String>, String>
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1353
            getHandshakeApplicationProtocolSelector() {
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1354
        socketLock.lock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1355
        try {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1356
            return conContext.sslConfig.socketAPSelector;
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1357
        } finally {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1358
            socketLock.unlock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1359
        }
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1360
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1361
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1362
    /**
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1363
     * Read the initial handshake records.
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1364
     */
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1365
    private int readHandshakeRecord() throws IOException {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1366
        while (!conContext.isInboundClosed()) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1367
            try {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1368
                Plaintext plainText = decode(null);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1369
                if ((plainText.contentType == ContentType.HANDSHAKE.id) &&
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1370
                        conContext.isNegotiated) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1371
                    return 0;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1372
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1373
            } catch (SSLException ssle) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1374
                throw ssle;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1375
            } catch (IOException ioe) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1376
                if (!(ioe instanceof SSLException)) {
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1377
                    throw new SSLException("readHandshakeRecord", ioe);
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1378
                } else {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1379
                    throw ioe;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1380
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1381
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
        }
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29923
diff changeset
  1383
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1384
        return -1;
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29923
diff changeset
  1385
    }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29923
diff changeset
  1386
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1387
    /**
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1388
     * Read application data record. Used by AppInputStream only, but defined
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1389
     * here so as to use the socket level synchronization.
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1390
     *
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1391
     * Note that the connection guarantees that handshake, alert, and change
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1392
     * cipher spec data streams are handled as they arrive, so we never see
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1393
     * them here.
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1394
     *
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1395
     * Note: Please be careful about the synchronization, and don't use this
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1396
     * method other than in the AppInputStream class!
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1397
     */
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1398
    private ByteBuffer readApplicationRecord(
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1399
            ByteBuffer buffer) throws IOException {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1400
        while (!conContext.isInboundClosed()) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1401
            /*
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1402
             * clean the buffer and check if it is too small, e.g. because
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1403
             * the AppInputStream did not have the chance to see the
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1404
             * current packet length but rather something like that of the
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1405
             * handshake before. In that case we return 0 at this point to
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1406
             * give the caller the chance to adjust the buffer.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1407
             */
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1408
            buffer.clear();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1409
            int inLen = conContext.inputRecord.bytesInCompletePacket();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1410
            if (inLen < 0) {    // EOF
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1411
                handleEOF(null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1413
                // if no exception thrown
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1414
                return null;
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1415
            }
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29923
diff changeset
  1416
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1417
            // Is this packet bigger than SSL/TLS normally allows?
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1418
            if (inLen > SSLRecord.maxLargeRecordSize) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1419
                throw new SSLProtocolException(
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1420
                        "Illegal packet size: " + inLen);
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1421
            }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1422
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1423
            if (inLen > buffer.remaining()) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1424
                buffer = ByteBuffer.allocate(inLen);
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1425
            }
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1426
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1427
            try {
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1428
                Plaintext plainText;
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1429
                socketLock.lock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1430
                try {
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1431
                    plainText = decode(buffer);
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1432
                } finally {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1433
                    socketLock.unlock();
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1434
                }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1435
                if (plainText.contentType == ContentType.APPLICATION_DATA.id &&
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1436
                        buffer.position() > 0) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1437
                    return buffer;
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29923
diff changeset
  1438
                }
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1439
            } catch (SSLException ssle) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1440
                throw ssle;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1441
            } catch (IOException ioe) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1442
                if (!(ioe instanceof SSLException)) {
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1443
                    throw new SSLException("readApplicationRecord", ioe);
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1444
                } else {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1445
                    throw ioe;
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29923
diff changeset
  1446
                }
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1447
            }
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1448
        }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1449
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1450
        //
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1451
        // couldn't read, due to some kind of error
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1452
        //
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1453
        return null;
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1454
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1455
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1456
    private Plaintext decode(ByteBuffer destination) throws IOException {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1457
        Plaintext plainText;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1458
        try {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1459
            if (destination == null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1460
                plainText = SSLTransport.decode(conContext,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1461
                        null, 0, 0, null, 0, 0);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1462
            } else {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1463
                plainText = SSLTransport.decode(conContext,
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1464
                        null, 0, 0, new ByteBuffer[]{destination}, 0, 1);
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1465
            }
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1466
        } catch (EOFException eofe) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1467
            // EOFException is special as it is related to close_notify.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1468
            plainText = handleEOF(eofe);
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1469
        }
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29923
diff changeset
  1470
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1471
        // Is the sequence number is nearly overflow?
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1472
        if (plainText != Plaintext.PLAINTEXT_NULL &&
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1473
                (conContext.inputRecord.seqNumIsHuge() ||
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1474
                conContext.inputRecord.readCipher.atKeyLimit())) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1475
            tryKeyUpdate();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1476
        }
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 29923
diff changeset
  1477
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1478
        return plainText;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
    /**
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1482
     * Try key update for sequence number wrap or key usage limit.
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1483
     *
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1484
     * Note that in order to maintain the handshake status properly, we check
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1485
     * the sequence number and key usage limit after the last record
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1486
     * reading/writing process.
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1487
     *
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1488
     * As we request renegotiation or close the connection for wrapped sequence
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1489
     * number when there is enough sequence number space left to handle a few
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1490
     * more records, so the sequence number of the last record cannot be
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1491
     * wrapped.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
     */
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1493
    private void tryKeyUpdate() throws IOException {
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1494
        // Don't bother to kickstart if handshaking is in progress, or if the
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1495
        // connection is not duplex-open.
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1496
        if ((conContext.handshakeContext == null) &&
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1497
                !conContext.isOutboundClosed() &&
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1498
                !conContext.isInboundClosed() &&
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1499
                !conContext.isBroken) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1500
            if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1501
                SSLLogger.finest("trigger key update");
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1502
            }
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1503
            startHandshake();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
57485
af4b0fc25bc4 8226338: Updates to Stateless Resumption
ascarpino
parents: 55411
diff changeset
  1507
    // Try to generate a PostHandshake NewSessionTicket message.  This is
af4b0fc25bc4 8226338: Updates to Stateless Resumption
ascarpino
parents: 55411
diff changeset
  1508
    // TLS 1.3 only.
af4b0fc25bc4 8226338: Updates to Stateless Resumption
ascarpino
parents: 55411
diff changeset
  1509
    private void tryNewSessionTicket() throws IOException {
af4b0fc25bc4 8226338: Updates to Stateless Resumption
ascarpino
parents: 55411
diff changeset
  1510
        // Don't bother to kickstart if handshaking is in progress, or if the
af4b0fc25bc4 8226338: Updates to Stateless Resumption
ascarpino
parents: 55411
diff changeset
  1511
        // connection is not duplex-open.
af4b0fc25bc4 8226338: Updates to Stateless Resumption
ascarpino
parents: 55411
diff changeset
  1512
        if (!conContext.sslConfig.isClientMode &&
af4b0fc25bc4 8226338: Updates to Stateless Resumption
ascarpino
parents: 55411
diff changeset
  1513
                conContext.protocolVersion.useTLS13PlusSpec() &&
af4b0fc25bc4 8226338: Updates to Stateless Resumption
ascarpino
parents: 55411
diff changeset
  1514
                conContext.handshakeContext == null &&
af4b0fc25bc4 8226338: Updates to Stateless Resumption
ascarpino
parents: 55411
diff changeset
  1515
                !conContext.isOutboundClosed() &&
af4b0fc25bc4 8226338: Updates to Stateless Resumption
ascarpino
parents: 55411
diff changeset
  1516
                !conContext.isInboundClosed() &&
af4b0fc25bc4 8226338: Updates to Stateless Resumption
ascarpino
parents: 55411
diff changeset
  1517
                !conContext.isBroken) {
af4b0fc25bc4 8226338: Updates to Stateless Resumption
ascarpino
parents: 55411
diff changeset
  1518
            if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
af4b0fc25bc4 8226338: Updates to Stateless Resumption
ascarpino
parents: 55411
diff changeset
  1519
                SSLLogger.finest("trigger new session ticket");
af4b0fc25bc4 8226338: Updates to Stateless Resumption
ascarpino
parents: 55411
diff changeset
  1520
            }
af4b0fc25bc4 8226338: Updates to Stateless Resumption
ascarpino
parents: 55411
diff changeset
  1521
            NewSessionTicket.kickstartProducer.produce(
af4b0fc25bc4 8226338: Updates to Stateless Resumption
ascarpino
parents: 55411
diff changeset
  1522
                    new PostHandshakeContext(conContext));
af4b0fc25bc4 8226338: Updates to Stateless Resumption
ascarpino
parents: 55411
diff changeset
  1523
        }
af4b0fc25bc4 8226338: Updates to Stateless Resumption
ascarpino
parents: 55411
diff changeset
  1524
    }
af4b0fc25bc4 8226338: Updates to Stateless Resumption
ascarpino
parents: 55411
diff changeset
  1525
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
    /**
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1527
     * Initialize the handshaker and socket streams.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
     *
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1529
     * Called by connect, the layered constructor, and SSLServerSocket.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
     */
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1531
    void doneConnect() throws IOException {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1532
        socketLock.lock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1533
        try {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1534
            // In server mode, it is not necessary to set host and serverNames.
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1535
            // Otherwise, would require a reverse DNS lookup to get
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1536
            // the hostname.
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1537
            if (peerHost == null || peerHost.isEmpty()) {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1538
                boolean useNameService =
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1539
                        trustNameService && conContext.sslConfig.isClientMode;
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1540
                useImplicitHost(useNameService);
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1541
            } else {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1542
                conContext.sslConfig.serverNames =
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1543
                        Utilities.addToSNIServerNameList(
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1544
                                conContext.sslConfig.serverNames, peerHost);
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1545
            }
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1546
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1547
            InputStream sockInput = super.getInputStream();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1548
            conContext.inputRecord.setReceiverStream(sockInput);
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1549
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1550
            OutputStream sockOutput = super.getOutputStream();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1551
            conContext.inputRecord.setDeliverStream(sockOutput);
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1552
            conContext.outputRecord.setDeliverStream(sockOutput);
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1553
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1554
            this.isConnected = true;
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1555
        } finally {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1556
            socketLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1560
    private void useImplicitHost(boolean useNameService) {
37601
6517589dcc94 8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents: 36641
diff changeset
  1561
        // Note: If the local name service is not trustworthy, reverse
6517589dcc94 8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents: 36641
diff changeset
  1562
        // host name resolution should not be performed for endpoint
6517589dcc94 8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents: 36641
diff changeset
  1563
        // identification.  Use the application original specified
6517589dcc94 8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents: 36641
diff changeset
  1564
        // hostname or IP address instead.
6517589dcc94 8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents: 36641
diff changeset
  1565
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 51986
diff changeset
  1566
        // Get the original hostname via jdk.internal.access.SharedSecrets
37601
6517589dcc94 8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents: 36641
diff changeset
  1567
        InetAddress inetAddress = getInetAddress();
6517589dcc94 8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents: 36641
diff changeset
  1568
        if (inetAddress == null) {      // not connected
6517589dcc94 8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents: 36641
diff changeset
  1569
            return;
31687
d6eb4f028c60 8067694: Improved certification checking
xuelei
parents: 30904
diff changeset
  1570
        }
d6eb4f028c60 8067694: Improved certification checking
xuelei
parents: 30904
diff changeset
  1571
37601
6517589dcc94 8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents: 36641
diff changeset
  1572
        JavaNetInetAddressAccess jna =
6517589dcc94 8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents: 36641
diff changeset
  1573
                SharedSecrets.getJavaNetInetAddressAccess();
6517589dcc94 8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents: 36641
diff changeset
  1574
        String originalHostname = jna.getOriginalHostName(inetAddress);
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52427
diff changeset
  1575
        if (originalHostname != null && !originalHostname.isEmpty()) {
37601
6517589dcc94 8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents: 36641
diff changeset
  1576
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1577
            this.peerHost = originalHostname;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1578
            if (conContext.sslConfig.serverNames.isEmpty() &&
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1579
                    !conContext.sslConfig.noSniExtension) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1580
                conContext.sslConfig.serverNames =
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1581
                        Utilities.addToSNIServerNameList(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1582
                                conContext.sslConfig.serverNames, peerHost);
37601
6517589dcc94 8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents: 36641
diff changeset
  1583
            }
6517589dcc94 8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents: 36641
diff changeset
  1584
6517589dcc94 8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents: 36641
diff changeset
  1585
            return;
6517589dcc94 8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents: 36641
diff changeset
  1586
        }
6517589dcc94 8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents: 36641
diff changeset
  1587
6517589dcc94 8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents: 36641
diff changeset
  1588
        // No explicitly specified hostname, no server name indication.
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1589
        if (!useNameService) {
37601
6517589dcc94 8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents: 36641
diff changeset
  1590
            // The local name service is not trustworthy, use IP address.
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1591
            this.peerHost = inetAddress.getHostAddress();
37601
6517589dcc94 8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents: 36641
diff changeset
  1592
        } else {
6517589dcc94 8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents: 36641
diff changeset
  1593
            // Use the underlying reverse host name resolution service.
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1594
            this.peerHost = getInetAddress().getHostName();
37601
6517589dcc94 8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents: 36641
diff changeset
  1595
        }
31687
d6eb4f028c60 8067694: Improved certification checking
xuelei
parents: 30904
diff changeset
  1596
    }
d6eb4f028c60 8067694: Improved certification checking
xuelei
parents: 30904
diff changeset
  1597
5162
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 2068
diff changeset
  1598
    // ONLY used by HttpsClient to setup the URI specified hostname
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  1599
    //
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  1600
    // Please NOTE that this method MUST be called before calling to
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  1601
    // SSLSocket.setSSLParameters(). Otherwise, the {@code host} parameter
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  1602
    // may override SNIHostName in the customized server name indication.
54443
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1603
    public void setHost(String host) {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1604
        socketLock.lock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1605
        try {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1606
            this.peerHost = host;
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1607
            this.conContext.sslConfig.serverNames =
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1608
                    Utilities.addToSNIServerNameList(
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1609
                            conContext.sslConfig.serverNames, host);
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1610
        } finally {
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1611
            socketLock.unlock();
dfba4e321ab3 8221882: Use fiber-friendly java.util.concurrent.locks in JSSE
xuelei
parents: 54221
diff changeset
  1612
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
    /**
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1616
     * Handle an exception.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1617
     *
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1618
     * This method is called by top level exception handlers (in read(),
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1619
     * write()) to make sure we always shutdown the connection correctly
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1620
     * and do not pass runtime exception to the application.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
     *
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1622
     * This method never returns normally, it always throws an IOException.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
     */
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1624
    private void handleException(Exception cause) throws IOException {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1625
        if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1626
            SSLLogger.warning("handling exception", cause);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1627
        }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1628
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1629
        // Don't close the Socket in case of timeouts or interrupts.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1630
        if (cause instanceof InterruptedIOException) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1631
            throw (IOException)cause;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1632
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1634
        // need to perform error shutdown
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1635
        boolean isSSLException = (cause instanceof SSLException);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1636
        Alert alert;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1637
        if (isSSLException) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1638
            if (cause instanceof SSLHandshakeException) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1639
                alert = Alert.HANDSHAKE_FAILURE;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1640
            } else {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1641
                alert = Alert.UNEXPECTED_MESSAGE;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1642
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1643
        } else {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1644
            if (cause instanceof IOException) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1645
                alert = Alert.UNEXPECTED_MESSAGE;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1646
            } else {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1647
                // RuntimeException
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1648
                alert = Alert.INTERNAL_ERROR;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1649
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
        }
53064
103ed9569fc8 8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents: 53018
diff changeset
  1651
103ed9569fc8 8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents: 53018
diff changeset
  1652
        throw conContext.fatal(alert, cause);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1655
    private Plaintext handleEOF(EOFException eofe) throws IOException {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1656
        if (requireCloseNotify || conContext.handshakeContext != null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1657
            SSLException ssle;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1658
            if (conContext.handshakeContext != null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1659
                ssle = new SSLHandshakeException(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1660
                        "Remote host terminated the handshake");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1661
            } else {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1662
                ssle = new SSLProtocolException(
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1663
                        "Remote host terminated the connection");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1664
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1666
            if (eofe != null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1667
                ssle.initCause(eofe);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1668
            }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1669
            throw ssle;
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1670
        } else {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1671
            // treat as if we had received a close_notify
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1672
            conContext.isInputCloseNotified = true;
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1673
            shutdownInput();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1675
            return Plaintext.PLAINTEXT_NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  1680
    @Override
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1681
    public String getPeerHost() {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1682
        return peerHost;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  1685
    @Override
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1686
    public int getPeerPort() {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1687
        return getPort();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  1690
    @Override
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1691
    public boolean useDelegatedTask() {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1692
        return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  1695
    @Override
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1696
    public void shutdown() throws IOException {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1697
        if (!isClosed()) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1698
            if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1699
                SSLLogger.fine("close the underlying socket");
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1700
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1702
            try {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1703
                if (conContext.isInputCloseNotified) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1704
                    // Close the connection, no wait for more peer response.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1705
                    closeSocket(false);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1706
                } else {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1707
                    // Close the connection, may wait for peer close_notify.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1708
                    closeSocket(true);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1709
                }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1710
            } finally {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47271
diff changeset
  1711
                tlsIsClosed = true;
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  1712
            }
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1713
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
    }
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1715
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1716
    private void closeSocket(boolean selfInitiated) throws IOException {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1717
        if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1718
            SSLLogger.fine("close the SSL connection " +
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1719
                (selfInitiated ? "(initiative)" : "(passive)"));
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1720
        }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1721
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1722
        if (autoClose || !isLayered()) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1723
            super.close();
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1724
        } else if (selfInitiated) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1725
            if (!conContext.isInboundClosed() && !isInputShutdown()) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1726
                // wait for close_notify alert to clear input stream.
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1727
                waitForClose();
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1728
            }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1729
        }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1730
    }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1731
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1732
   /**
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1733
    * Wait for close_notify alert for a graceful closure.
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1734
    *
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1735
    * [RFC 5246] If the application protocol using TLS provides that any
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1736
    * data may be carried over the underlying transport after the TLS
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1737
    * connection is closed, the TLS implementation must receive the responding
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1738
    * close_notify alert before indicating to the application layer that
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1739
    * the TLS connection has ended.  If the application protocol will not
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1740
    * transfer any additional data, but will only close the underlying
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1741
    * transport connection, then the implementation MAY choose to close the
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1742
    * transport without waiting for the responding close_notify.
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1743
    */
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1744
    private void waitForClose() throws IOException {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1745
        if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1746
            SSLLogger.fine("wait for close_notify or alert");
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1747
        }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1748
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1749
        while (!conContext.isInboundClosed()) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1750
            try {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1751
                Plaintext plainText = decode(null);
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1752
                // discard and continue
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1753
                if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1754
                    SSLLogger.finest(
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1755
                        "discard plaintext while waiting for close", plainText);
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1756
                }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1757
            } catch (Exception e) {   // including RuntimeException
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1758
                handleException(e);
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1759
            }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1760
        }
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50768
diff changeset
  1761
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
}