jdk/src/share/classes/sun/security/ssl/SSLSocketImpl.java
author malenkov
Tue, 29 Oct 2013 17:01:06 +0400
changeset 21278 ef8a3a2a72f2
parent 19823 f0fd09e20528
child 24969 afa6934dd8e8
permissions -rw-r--r--
8022746: List of spelling errors in API doc Reviewed-by: alexsch, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
     2
 * Copyright (c) 1996, 2013, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.GeneralSecurityException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.AccessControlContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.PrivilegedAction;
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    35
import java.security.AlgorithmConstraints;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.*;
100
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
    37
import java.util.concurrent.TimeUnit;
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
    38
import java.util.concurrent.locks.ReentrantLock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.crypto.BadPaddingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Implementation of an SSL socket.  This is a normal connection type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * socket, implementing SSL over some lower level socket, such as TCP.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Because it is layered over some lower level socket, it MUST override
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * all default socket methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <P> This API offers a non-traditional option for establishing SSL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * connections.  You may first establish the connection directly, then pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * that connection to the SSL socket constructor with a flag saying which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * role should be taken in the handshake protocol.  (The two ends of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * connection must not choose the same role!)  This allows setup of SSL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * proxying or tunneling, and also allows the kind of "role reversal"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * that is required for most FTP data transfers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @see javax.net.ssl.SSLSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @see SSLServerSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
final public class SSLSocketImpl extends BaseSSLSocketImpl {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * ERROR HANDLING GUIDELINES
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * (which exceptions to throw and catch and which not to throw and catch)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * . if there is an IOException (SocketException) when accessing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     *   underlying Socket, pass it through
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * . do not throw IOExceptions, throw SSLExceptions (or a subclass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * . for internal errors (things that indicate a bug in JSSE or a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *   grossly misconfigured J2RE), throw either an SSLException or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *   a RuntimeException at your convenience.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * . handshaking code (Handshaker or HandshakeMessage) should generally
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *   pass through exceptions, but can handle them if they know what to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     *   do.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * . exception chaining should be used for all new code. If you happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *   to touch old code that does not use chaining, you should change it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * . there is a top level exception handler that sits at all entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *   points from application code to SSLSocket read/write code. It
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *   makes sure that all errors are handled (see handleException()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * . JSSE internal code should generally not call close(), call
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *   closeInternal().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * There's a state machine associated with each connection, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * among other roles serves to negotiate session changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * - START with constructor, until the TCP connection's around.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * - HANDSHAKE picks session parameters before allowing traffic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *          There are many substates due to sequencing requirements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *          for handshake messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * - DATA may be transmitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * - RENEGOTIATE state allows concurrent data and handshaking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *          traffic ("same" substates as HANDSHAKE), and terminates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *          in selection of new session (and connection) parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * - ERROR state immediately precedes abortive disconnect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * - SENT_CLOSE sent a close_notify to the peer. For layered,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *          non-autoclose socket, must now read close_notify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *          from peer before closing the connection. For nonlayered or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *          non-autoclose socket, close connection and go onto
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *          cs_CLOSED state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * - CLOSED after sending close_notify alert, & socket is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *          SSL connection objects are not reused.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * - APP_CLOSED once the application calls close(). Then it behaves like
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *          a closed socket, e.g.. getInputStream() throws an Exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * State affects what SSL record types may legally be sent:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * - Handshake ... only in HANDSHAKE and RENEGOTIATE states
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * - App Data ... only in DATA and RENEGOTIATE states
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * - Alert ... in HANDSHAKE, DATA, RENEGOTIATE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Re what may be received:  same as what may be sent, except that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * HandshakeRequest handshaking messages can come from servers even
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * in the application data state, to request entry to RENEGOTIATE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * The state machine within HANDSHAKE and RENEGOTIATE states controls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * the pending session, not the connection state, until the change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * cipher spec and "Finished" handshake messages are processed and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * make the "new" session become the current one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * NOTE: details of the SMs always need to be nailed down better.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * The text above illustrates the core ideas.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *                +---->-------+------>--------->-------+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *                |            |                        |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *     <-----<    ^            ^  <-----<               v
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *START>----->HANDSHAKE>----->DATA>----->RENEGOTIATE  SENT_CLOSE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *                v            v               v        |   |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *                |            |               |        |   v
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *                +------------+---------------+        v ERROR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *                |                                     |   |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *                v                                     |   |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *               ERROR>------>----->CLOSED<--------<----+-- +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *                                     |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *                                     v
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *                                 APP_CLOSED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * ALSO, note that the the purpose of handshaking (renegotiation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * included) is to assign a different, and perhaps new, session to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * the connection.  The SSLv3 spec is a bit confusing on that new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * protocol feature.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    private static final int    cs_START = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    private static final int    cs_HANDSHAKE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    private static final int    cs_DATA = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private static final int    cs_RENEGOTIATE = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    private static final int    cs_ERROR = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private static final int   cs_SENT_CLOSE = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    private static final int    cs_CLOSED = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    private static final int    cs_APP_CLOSED = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Client authentication be off, requested, or required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Migrated to SSLEngineImpl:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *    clauth_none/cl_auth_requested/clauth_required
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Drives the protocol state machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
19223
e27cda06fe6a 8013809: deadlock in SSLSocketImpl between between write and close
xuelei
parents: 16913
diff changeset
   172
    private volatile int        connectionState;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Flag indicating if the next record we receive MUST be a Finished
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * message. Temporarily set during the handshake to ensure that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * a change cipher spec message is followed by a finished message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    private boolean             expectingFinished;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * For improved diagnostics, we detail connection closure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * If the socket is closed (connectionState >= cs_ERROR),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * closeReason != null indicates if the socket was closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * because of an error or because or normal shutdown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    private SSLException        closeReason;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * Per-connection private state that doesn't change when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * session is changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    private byte                doClientAuth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    private boolean             roleIsServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    private boolean             enableSessionCreation = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    private String              host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    private boolean             autoClose = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    private AccessControlContext acc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   200
    // The cipher suites enabled for use on this connection.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   201
    private CipherSuiteList     enabledCipherSuites;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   202
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   203
    // The endpoint identification protocol
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   204
    private String              identificationProtocol = null;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   205
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   206
    // The cryptographic algorithm constraints
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   207
    private AlgorithmConstraints    algorithmConstraints = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   209
    // The server name indication and matchers
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   210
    List<SNIServerName>         serverNames =
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   211
                                    Collections.<SNIServerName>emptyList();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   212
    Collection<SNIMatcher>      sniMatchers =
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   213
                                    Collections.<SNIMatcher>emptyList();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   214
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * READ ME * READ ME * READ ME * READ ME * READ ME * READ ME *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * IMPORTANT STUFF TO UNDERSTANDING THE SYNCHRONIZATION ISSUES.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * READ ME * READ ME * READ ME * READ ME * READ ME * READ ME *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * There are several locks here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * The primary lock is the per-instance lock used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * synchronized(this) and the synchronized methods.  It controls all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * access to things such as the connection state and variables which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * affect handshaking.  If we are inside a synchronized method, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * can access the state directly, otherwise, we must use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * synchronized equivalents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * The handshakeLock is used to ensure that only one thread performs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * the *complete initial* handshake.  If someone is handshaking, any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * stray application or startHandshake() requests who find the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * connection state is cs_HANDSHAKE will stall on handshakeLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * until handshaking is done.  Once the handshake is done, we either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * succeeded or failed, but we can never go back to the cs_HANDSHAKE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * or cs_START state again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * Note that the read/write() calls here in SSLSocketImpl are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * obviously synchronized.  In fact, it's very nonintuitive, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * requires careful examination of code paths.  Grab some coffee,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * and be careful with any code changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * There can be only three threads active at a time in the I/O
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * subsection of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *    1.  startHandshake
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *    2.  AppInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *    3.  AppOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * One thread could call startHandshake().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * AppInputStream/AppOutputStream read() and write() calls are each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * synchronized on 'this' in their respective classes, so only one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * app. thread will be doing a SSLSocketImpl.read() or .write()'s at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * a time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * If handshaking is required (state cs_HANDSHAKE), and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * getConnectionState() for some/all threads returns cs_HANDSHAKE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * only one can grab the handshakeLock, and the rest will stall
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * either on getConnectionState(), or on the handshakeLock if they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * happen to successfully race through the getConnectionState().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * If a writer is doing the initial handshaking, it must create a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * temporary reader to read the responses from the other side.  As a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * side-effect, the writer's reader will have priority over any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * other reader.  However, the writer's reader is not allowed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * consume any application data.  When handshakeLock is finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * released, we either have a cs_DATA connection, or a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * cs_CLOSED/cs_ERROR socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * The writeLock is held while writing on a socket connection and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * also to protect the MAC and cipher for their direction.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * writeLock is package private for Handshaker which holds it while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * writing the ChangeCipherSpec message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * To avoid the problem of a thread trying to change operational
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * modes on a socket while handshaking is going on, we synchronize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * on 'this'.  If handshaking has not started yet, we tell the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * handshaker to change its mode.  If handshaking has started,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * we simply store that request until the next pending session
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * is created, at which time the new handshaker's state is set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * The readLock is held during readRecord(), which is responsible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * for reading an InputRecord, decrypting it, and processing it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * The readLock ensures that these three steps are done atomically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * and that once started, no other thread can block on InputRecord.read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * This is necessary so that processing of close_notify alerts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * from the peer are handled properly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     */
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   286
    final private Object        handshakeLock = new Object();
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   287
    final ReentrantLock         writeLock = new ReentrantLock();
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   288
    final private Object        readLock = new Object();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    private InputRecord         inrec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * Crypto state that's reinitialized when the session changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     */
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   295
    private Authenticator       readAuthenticator, writeAuthenticator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    private CipherBox           readCipher, writeCipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    // NOTE: compression state would be saved here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /*
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   300
     * security parameters for secure renegotiation.
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   301
     */
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   302
    private boolean             secureRenegotiation;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   303
    private byte[]              clientVerifyData;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   304
    private byte[]              serverVerifyData;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   305
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   306
    /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * The authentication context holds all information used to establish
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * who this end of the connection is (certificate chains, private keys,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * etc) and who is trusted (e.g. as CAs or websites).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    private SSLContextImpl      sslContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * This connection is one of (potentially) many associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * any given session.  The output of the handshake protocol is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * new session ... although all the protocol description talks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * about changing the cipher spec (and it does change), in fact
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * that's incidental since it's done by changing everything that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * is associated with a session at the same time.  (TLS/IETF may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * change that to add client authentication w/o new key exchg.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     */
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   323
    private Handshaker                  handshaker;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   324
    private SSLSessionImpl              sess;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   325
    private volatile SSLSessionImpl     handshakeSession;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * If anyone wants to get notified about handshake completions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * they'll show up on this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    private HashMap<HandshakeCompletedListener, AccessControlContext>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                                                        handshakeListeners;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * Reuse the same internal input/output streams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    private InputStream         sockInput;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    private OutputStream        sockOutput;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * These input and output streams block their data in SSL records,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * and usually arrange integrity and privacy protection for those
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * records.  The guts of the SSL protocol are wrapped up in these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * streams, and in the handshaking that establishes the details of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * that integrity and privacy protection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    private AppInputStream      input;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    private AppOutputStream     output;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    /*
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   353
     * The protocol versions enabled for use on this connection.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   354
     *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   355
     * Note: we support a pseudo protocol called SSLv2Hello which when
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   356
     * set will result in an SSL v2 Hello being sent with SSL (version 3.0)
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   357
     * or TLS (version 3.1, 3.2, etc.) version info.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    private ProtocolList enabledProtocols;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * The SSL version associated with this connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    private ProtocolVersion     protocolVersion = ProtocolVersion.DEFAULT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    /* Class and subclass dynamic debugging support */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    private static final Debug debug = Debug.getInstance("ssl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
10915
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9514
diff changeset
   369
    /*
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9514
diff changeset
   370
     * Is it the first application record to write?
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9514
diff changeset
   371
     */
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9514
diff changeset
   372
    private boolean isFirstAppOutputRecord = true;
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9514
diff changeset
   373
12428
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   374
    /*
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   375
     * If AppOutputStream needs to delay writes of small packets, we
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   376
     * will use this to store the data until we actually do the write.
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   377
     */
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   378
    private ByteArrayOutputStream heldRecordBuffer = null;
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   379
19823
f0fd09e20528 7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents: 19223
diff changeset
   380
    /*
f0fd09e20528 7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents: 19223
diff changeset
   381
     * Whether local cipher suites preference in server side should be
f0fd09e20528 7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents: 19223
diff changeset
   382
     * honored during handshaking?
f0fd09e20528 7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents: 19223
diff changeset
   383
     */
f0fd09e20528 7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents: 19223
diff changeset
   384
    private boolean preferLocalCipherSuites = false;
f0fd09e20528 7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents: 19223
diff changeset
   385
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    // CONSTRUCTORS AND INITIALIZATION CODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * Constructs an SSL connection to a named host at a specified port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * using the authentication context provided.  This endpoint acts as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * the client, and may rejoin an existing SSL session if appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @param context authentication context to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * @param host name of the host with which to connect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * @param port number of the server's port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    SSLSocketImpl(SSLContextImpl context, String host, int port)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            throws IOException, UnknownHostException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        this.host = host;
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   403
        this.serverNames =
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   404
            Utilities.addToSNIServerNameList(this.serverNames, this.host);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        init(context, false);
2068
cdbc5929b91e 5067458: Loopback SSLSocketImpl createSocket is throwing an exception
xuelei
parents: 1763
diff changeset
   406
        SocketAddress socketAddress =
cdbc5929b91e 5067458: Loopback SSLSocketImpl createSocket is throwing an exception
xuelei
parents: 1763
diff changeset
   407
               host != null ? new InetSocketAddress(host, port) :
cdbc5929b91e 5067458: Loopback SSLSocketImpl createSocket is throwing an exception
xuelei
parents: 1763
diff changeset
   408
               new InetSocketAddress(InetAddress.getByName(null), port);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        connect(socketAddress, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * Constructs an SSL connection to a server at a specified address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * and TCP port, using the authentication context provided.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * endpoint acts as the client, and may rejoin an existing SSL session
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * if appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * @param context authentication context to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @param address the server's host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * @param port its port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    SSLSocketImpl(SSLContextImpl context, InetAddress host, int port)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        init(context, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        SocketAddress socketAddress = new InetSocketAddress(host, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        connect(socketAddress, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * Constructs an SSL connection to a named host at a specified port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * using the authentication context provided.  This endpoint acts as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * the client, and may rejoin an existing SSL session if appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * @param context authentication context to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * @param host name of the host with which to connect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * @param port number of the server's port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @param localAddr the local address the socket is bound to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * @param localPort the local port the socket is bound to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    SSLSocketImpl(SSLContextImpl context, String host, int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            InetAddress localAddr, int localPort)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            throws IOException, UnknownHostException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        this.host = host;
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   447
        this.serverNames =
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   448
            Utilities.addToSNIServerNameList(this.serverNames, this.host);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        init(context, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        bind(new InetSocketAddress(localAddr, localPort));
2068
cdbc5929b91e 5067458: Loopback SSLSocketImpl createSocket is throwing an exception
xuelei
parents: 1763
diff changeset
   451
        SocketAddress socketAddress =
cdbc5929b91e 5067458: Loopback SSLSocketImpl createSocket is throwing an exception
xuelei
parents: 1763
diff changeset
   452
               host != null ? new InetSocketAddress(host, port) :
cdbc5929b91e 5067458: Loopback SSLSocketImpl createSocket is throwing an exception
xuelei
parents: 1763
diff changeset
   453
               new InetSocketAddress(InetAddress.getByName(null), port);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        connect(socketAddress, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * Constructs an SSL connection to a server at a specified address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * and TCP port, using the authentication context provided.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * endpoint acts as the client, and may rejoin an existing SSL session
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * if appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * @param context authentication context to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * @param address the server's host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * @param port its port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * @param localAddr the local address the socket is bound to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * @param localPort the local port the socket is bound to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    SSLSocketImpl(SSLContextImpl context, InetAddress host, int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            InetAddress localAddr, int localPort)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        init(context, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        bind(new InetSocketAddress(localAddr, localPort));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        SocketAddress socketAddress = new InetSocketAddress(host, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        connect(socketAddress, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * Package-private constructor used ONLY by SSLServerSocket.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * java.net package accepts the TCP connection after this call is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * made.  This just initializes handshake state to use "server mode",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * giving control over the use of SSL client authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    SSLSocketImpl(SSLContextImpl context, boolean serverMode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            CipherSuiteList suites, byte clientAuth,
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   488
            boolean sessionCreation, ProtocolList protocols,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   489
            String identificationProtocol,
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   490
            AlgorithmConstraints algorithmConstraints,
19823
f0fd09e20528 7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents: 19223
diff changeset
   491
            Collection<SNIMatcher> sniMatchers,
f0fd09e20528 7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents: 19223
diff changeset
   492
            boolean preferLocalCipherSuites) throws IOException {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   493
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        doClientAuth = clientAuth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        enableSessionCreation = sessionCreation;
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   497
        this.identificationProtocol = identificationProtocol;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   498
        this.algorithmConstraints = algorithmConstraints;
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   499
        this.sniMatchers = sniMatchers;
19823
f0fd09e20528 7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents: 19223
diff changeset
   500
        this.preferLocalCipherSuites = preferLocalCipherSuites;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        init(context, serverMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
         * Override what was picked out for us.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        enabledCipherSuites = suites;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        enabledProtocols = protocols;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * Package-private constructor used to instantiate an unconnected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * socket. The java.net package will connect it, either when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * connect() call is made by the application.  This instance is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * meant to set handshake state to use "client mode".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    SSLSocketImpl(SSLContextImpl context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        init(context, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * Layer SSL traffic over an existing connection, rather than creating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * a new connection.  The existing connection may be used only for SSL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * traffic (using this SSLSocket) until the SSLSocket.close() call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * returns. However, if a protocol error is detected, that existing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * connection is automatically closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * <P> This particular constructor always uses the socket in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * role of an SSL client. It may be useful in cases which start
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * using SSL after some initial data transfers, for example in some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * SSL tunneling applications or as part of some kinds of application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * protocols which negotiate use of a SSL based security.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * @param sock the existing connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * @param context the authentication context to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    SSLSocketImpl(SSLContextImpl context, Socket sock, String host,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            int port, boolean autoClose) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        super(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        // We always layer over a connected socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        if (!sock.isConnected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            throw new SocketException("Underlying socket is not connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        this.host = host;
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   547
        this.serverNames =
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   548
            Utilities.addToSNIServerNameList(this.serverNames, this.host);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        init(context, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        this.autoClose = autoClose;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        doneConnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    /**
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   555
     * 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
   556
     * 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
   557
     * 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
   558
     * underlying {@link InputStream}.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   559
     */
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   560
    SSLSocketImpl(SSLContextImpl context, Socket sock,
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   561
            InputStream consumed, boolean autoClose) throws IOException {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   562
        super(sock, consumed);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   563
        // We always layer over a connected socket
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   564
        if (!sock.isConnected()) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   565
            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
   566
        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   567
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   568
        // In server mode, it is not necessary to set host and serverNames.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   569
        // Otherwise, would require a reverse DNS lookup to get the hostname.
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   570
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   571
        init(context, true);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   572
        this.autoClose = autoClose;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   573
        doneConnect();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   574
    }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   575
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   576
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * Initializes the client socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    private void init(SSLContextImpl context, boolean isServer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        sslContext = context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        sess = SSLSessionImpl.nullSession;
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   582
        handshakeSession = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
         * role is as specified, state is START until after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
         * the low level connection's established.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        roleIsServer = isServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        connectionState = cs_START;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
         * default read and write side cipher and MAC support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
         * Note:  compression support would go here too
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        readCipher = CipherBox.NULL;
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   597
        readAuthenticator = MAC.NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        writeCipher = CipherBox.NULL;
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   599
        writeAuthenticator = MAC.NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   601
        // initial security parameters for secure renegotiation
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   602
        secureRenegotiation = false;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   603
        clientVerifyData = new byte[0];
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   604
        serverVerifyData = new byte[0];
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   605
9246
c459f79af46b 6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents: 7043
diff changeset
   606
        enabledCipherSuites =
c459f79af46b 6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents: 7043
diff changeset
   607
                sslContext.getDefaultCipherSuiteList(roleIsServer);
c459f79af46b 6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents: 7043
diff changeset
   608
        enabledProtocols =
c459f79af46b 6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents: 7043
diff changeset
   609
                sslContext.getDefaultProtocolList(roleIsServer);
c459f79af46b 6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents: 7043
diff changeset
   610
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        inrec = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        // save the acc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        acc = AccessController.getContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        input = new AppInputStream(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        output = new AppOutputStream(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * Connects this socket to the server with a specified timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * This method is either called on an unconnected SSLSocketImpl by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * application, or it is called in the constructor of a regular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * SSLSocketImpl. If we are layering on top on another socket, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * this method should not be called, because we assume that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * underlying socket is already connected by the time it is passed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * us.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * @param   endpoint the <code>SocketAddress</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * @param   timeout  the timeout value to be used, 0 is no timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * @throws  IOException if an error occurs during the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * @throws  SocketTimeoutException if timeout expires before connecting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
   636
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    public void connect(SocketAddress endpoint, int timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   640
        if (isLayered()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            throw new SocketException("Already connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        if (!(endpoint instanceof InetSocketAddress)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            throw new SocketException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                                  "Cannot handle non-Inet socket addresses.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        super.connect(endpoint, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        doneConnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * Initialize the handshaker and socket streams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * Called by connect, the layered constructor, and SSLServerSocket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    void doneConnect() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
         * Save the input and output streams.  May be done only after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
         * java.net actually connects using the socket "self", else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
         * we get some pretty bizarre failure modes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
         */
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   664
        sockInput = super.getInputStream();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   665
        sockOutput = super.getOutputStream();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
         * Move to handshaking state, with pending session initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
         * to defaults and the appropriate kind of handshaker set up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        initHandshaker();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    synchronized private int getConnectionState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        return connectionState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    synchronized private void setConnectionState(int state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        connectionState = state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    AccessControlContext getAcc() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        return acc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    // READING AND WRITING RECORDS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    /*
12428
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   691
     * AppOutputStream calls may need to buffer multiple outbound
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   692
     * application packets.
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   693
     *
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   694
     * All other writeRecord() calls will not buffer, so do not hold
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   695
     * these records.
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   696
     */
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   697
    void writeRecord(OutputRecord r) throws IOException {
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   698
        writeRecord(r, false);
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   699
    }
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   700
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   701
    /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * Record Output. Application data can't be sent until the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * handshake establishes a session.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * NOTE:  we let empty records be written as a hook to force some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * TCP-level activity, notably handshaking, to occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     */
12428
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   708
    void writeRecord(OutputRecord r, boolean holdRecord) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
         * The loop is in case of HANDSHAKE --> ERROR transitions, etc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    loop:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        while (r.contentType() == Record.ct_application_data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
             * Not all states support passing application data.  We
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
             * synchronize access to the connection state, so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
             * synchronous handshakes can complete cleanly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            switch (getConnectionState()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
             * We've deferred the initial handshaking till just now,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
             * when presumably a thread's decided it's OK to block for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
             * longish periods of time for I/O purposes (as well as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
             * configured the cipher suites it wants to use).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            case cs_HANDSHAKE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                performInitialHandshake();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            case cs_DATA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            case cs_RENEGOTIATE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                break loop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            case cs_ERROR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                fatal(Alerts.alert_close_notify,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                    "error while writing to socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                break; // dummy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            case cs_SENT_CLOSE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            case cs_CLOSED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            case cs_APP_CLOSED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                // we should never get here (check in AppOutputStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                // this is just a fallback
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                if (closeReason != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                    throw closeReason;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                    throw new SocketException("Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
             * Else something's goofy in this state machine's use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                throw new SSLProtocolException("State error, send app data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        // Don't bother to really write empty records.  We went this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        // far to drive the handshake machinery, for correctness; not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        // writing empty records improves performance by cutting CPU
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        // time and network resource usage.  However, some protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        // implementations are fragile and don't like to see empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        // records, so this also increases robustness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        //
100
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   767
        if (!r.isEmpty()) {
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   768
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   769
            // If the record is a close notify alert, we need to honor
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   770
            // socket option SO_LINGER. Note that we will try to send
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   771
            // the close notify even if the SO_LINGER set to zero.
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   772
            if (r.isAlert(Alerts.alert_close_notify) && getSoLinger() >= 0) {
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   773
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   774
                // keep and clear the current thread interruption status.
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   775
                boolean interrupted = Thread.interrupted();
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   776
                try {
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   777
                    if (writeLock.tryLock(getSoLinger(), TimeUnit.SECONDS)) {
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   778
                        try {
12428
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   779
                            writeRecordInternal(r, holdRecord);
100
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   780
                        } finally {
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   781
                            writeLock.unlock();
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   782
                        }
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   783
                    } else {
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   784
                        SSLException ssle = new SSLException(
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   785
                                "SO_LINGER timeout," +
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   786
                                " close_notify message cannot be sent.");
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   787
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   788
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   789
                        // For layered, non-autoclose sockets, we are not
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   790
                        // able to bring them into a usable state, so we
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   791
                        // treat it as fatal error.
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   792
                        if (isLayered() && !autoClose) {
100
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   793
                            // Note that the alert description is
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   794
                            // specified as -1, so no message will be send
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   795
                            // to peer anymore.
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   796
                            fatal((byte)(-1), ssle);
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   797
                        } else if ((debug != null) && Debug.isOn("ssl")) {
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   798
                            System.out.println(
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   799
                                Thread.currentThread().getName() +
100
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   800
                                ", received Exception: " + ssle);
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   801
                        }
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   802
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   803
                        // RFC2246 requires that the session becomes
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   804
                        // unresumable if any connection is terminated
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   805
                        // without proper close_notify messages with
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   806
                        // level equal to warning.
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   807
                        //
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   808
                        // RFC4346 no longer requires that a session not be
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   809
                        // resumed if failure to properly close a connection.
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   810
                        //
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   811
                        // We choose to make the session unresumable if
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   812
                        // failed to send the close_notify message.
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   813
                        //
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   814
                        sess.invalidate();
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   815
                    }
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   816
                } catch (InterruptedException ie) {
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   817
                    // keep interrupted status
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   818
                    interrupted = true;
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   819
                }
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   820
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   821
                // restore the interrupted status
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   822
                if (interrupted) {
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   823
                    Thread.currentThread().interrupt();
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   824
                }
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   825
            } else {
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   826
                writeLock.lock();
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   827
                try {
12428
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   828
                    writeRecordInternal(r, holdRecord);
100
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   829
                } finally {
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   830
                    writeLock.unlock();
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   831
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
12428
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   836
    private void writeRecordInternal(OutputRecord r,
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   837
            boolean holdRecord) throws IOException {
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   838
100
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   839
        // r.compress(c);
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   840
        r.encrypt(writeAuthenticator, writeCipher);
12428
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   841
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   842
        if (holdRecord) {
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   843
            // If we were requested to delay the record due to possibility
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   844
            // of Nagle's being active when finally got to writing, and
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   845
            // it's actually not, we don't really need to delay it.
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   846
            if (getTcpNoDelay()) {
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   847
                holdRecord = false;
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   848
            } else {
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   849
                // We need to hold the record, so let's provide
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   850
                // a per-socket place to do it.
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   851
                if (heldRecordBuffer == null) {
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   852
                    // Likely only need 37 bytes.
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   853
                    heldRecordBuffer = new ByteArrayOutputStream(40);
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   854
                }
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   855
            }
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   856
        }
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 11026
diff changeset
   857
        r.write(sockOutput, holdRecord, heldRecordBuffer);
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   858
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   859
        /*
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   860
         * Check the sequence number state
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   861
         *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   862
         * Note that in order to maintain the connection I/O
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   863
         * properly, we check the sequence number after the last
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   864
         * record writing process. As we request renegotiation
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   865
         * or close the connection for wrapped sequence number
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   866
         * when there is enough sequence number space left to
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   867
         * handle a few more records, so the sequence number
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   868
         * of the last record cannot be wrapped.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   869
         */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   870
        if (connectionState < cs_ERROR) {
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   871
            checkSequenceNumber(writeAuthenticator, r.contentType());
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   872
        }
10915
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9514
diff changeset
   873
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9514
diff changeset
   874
        // turn off the flag of the first application record
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9514
diff changeset
   875
        if (isFirstAppOutputRecord &&
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9514
diff changeset
   876
                r.contentType() == Record.ct_application_data) {
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9514
diff changeset
   877
            isFirstAppOutputRecord = false;
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9514
diff changeset
   878
        }
100
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   879
    }
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   880
10915
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9514
diff changeset
   881
    /*
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9514
diff changeset
   882
     * Need to split the payload except the following cases:
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9514
diff changeset
   883
     *
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9514
diff changeset
   884
     * 1. protocol version is TLS 1.1 or later;
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9514
diff changeset
   885
     * 2. bulk cipher does not use CBC mode, including null bulk cipher suites.
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9514
diff changeset
   886
     * 3. the payload is the first application record of a freshly
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9514
diff changeset
   887
     *    negotiated TLS session.
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9514
diff changeset
   888
     * 4. the CBC protection is disabled;
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9514
diff changeset
   889
     *
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9514
diff changeset
   890
     * More details, please refer to AppOutputStream.write(byte[], int, int).
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9514
diff changeset
   891
     */
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9514
diff changeset
   892
    boolean needToSplitPayload() {
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9514
diff changeset
   893
        writeLock.lock();
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9514
diff changeset
   894
        try {
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9514
diff changeset
   895
            return (protocolVersion.v <= ProtocolVersion.TLS10.v) &&
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9514
diff changeset
   896
                    writeCipher.isCBCMode() && !isFirstAppOutputRecord &&
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9514
diff changeset
   897
                    Record.enableCBCProtection;
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9514
diff changeset
   898
        } finally {
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9514
diff changeset
   899
            writeLock.unlock();
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9514
diff changeset
   900
        }
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9514
diff changeset
   901
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * Read an application data record.  Alerts and handshake
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * messages are handled directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
    void readDataRecord(InputRecord r) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        if (getConnectionState() == cs_HANDSHAKE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            performInitialHandshake();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        readRecord(r, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * Clear the pipeline of records from the peer, optionally returning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * application data.   Caller is responsible for knowing that it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * possible to do this kind of clearing, if they don't want app
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * data -- e.g. since it's the initial SSL handshake.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * Don't synchronize (this) during a blocking read() since it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * protects data which is accessed on the write side as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    private void readRecord(InputRecord r, boolean needAppData)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        int state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        // readLock protects reading and processing of an InputRecord.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        // It keeps the reading from sockInput and processing of the record
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        // atomic so that no two threads can be blocked on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        // read from the same input stream at the same time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        // This is required for example when a reader thread is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        // blocked on the read and another thread is trying to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        // close the socket. For a non-autoclose, layered socket,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        // the thread performing the close needs to read the close_notify.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        // Use readLock instead of 'this' for locking because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        // 'this' also protects data accessed during writing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
      synchronized (readLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
         * Read and handle records ... return application data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
         * ONLY if it's needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        while (((state = getConnectionState()) != cs_CLOSED) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                (state != cs_ERROR) && (state != cs_APP_CLOSED)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
             * Read a record ... maybe emitting an alert if we get a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
             * comprehensible but unsupported "hello" message during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
             * format checking (e.g. V2).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                r.setAppDataValid(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                r.read(sockInput, sockOutput);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
            } catch (SSLProtocolException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                    fatal(Alerts.alert_unexpected_message, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                    // discard this exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
            } catch (EOFException eof) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                boolean handshaking = (getConnectionState() <= cs_HANDSHAKE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                boolean rethrow = requireCloseNotify || handshaking;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                if ((debug != null) && Debug.isOn("ssl")) {
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
   966
                    System.out.println(Thread.currentThread().getName() +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                        ", received EOFException: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                        + (rethrow ? "error" : "ignored"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                if (rethrow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                    SSLException e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                    if (handshaking) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                        e = new SSLHandshakeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                            ("Remote host closed connection during handshake");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                        e = new SSLProtocolException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                            ("Remote host closed connection incorrectly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                    e.initCause(eof);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                    throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                    // treat as if we had received a close_notify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                    closeInternal(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
             * The basic SSLv3 record protection involves (optional)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
             * encryption for privacy, and an integrity check ensuring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
             * data origin authentication.  We do them both here, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
             * throw a fatal alert if the integrity check fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
            try {
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   996
                r.decrypt(readAuthenticator, readCipher);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            } catch (BadPaddingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                byte alertType = (r.contentType() == Record.ct_handshake)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                                        ? Alerts.alert_handshake_failure
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                                        : Alerts.alert_bad_record_mac;
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
  1001
                fatal(alertType, e.getMessage(), e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            }
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1003
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            // if (!r.decompress(c))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            //     fatal(Alerts.alert_decompression_failure,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            //         "decompression failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
             * Process the record.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
              switch (r.contentType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                case Record.ct_handshake:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                     * Handshake messages always go to a pending session
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                     * handshaker ... if there isn't one, create one.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                     * must work asynchronously, for renegotiation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                     * NOTE that handshaking will either resume a session
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                     * which was in the cache (and which might have other
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                     * connections in it already), or else will start a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                     * session (new keys exchanged) with just this connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                     * in it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                    initHandshaker();
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1026
                    if (!handshaker.activated()) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1027
                        // prior to handshaking, activate the handshake
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1028
                        if (connectionState == cs_RENEGOTIATE) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1029
                            // don't use SSLv2Hello when renegotiating
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1030
                            handshaker.activate(protocolVersion);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1031
                        } else {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1032
                            handshaker.activate(null);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1033
                        }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1034
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                     * process the handshake record ... may contain just
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                     * a partial handshake message or multiple messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                     * The handshaker state machine will ensure that it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                     * a finished message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                    handshaker.process_record(r, expectingFinished);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                    expectingFinished = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
5182
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 2068
diff changeset
  1046
                    if (handshaker.invalidated) {
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 2068
diff changeset
  1047
                        handshaker = null;
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 2068
diff changeset
  1048
                        // if state is cs_RENEGOTIATE, revert it to cs_DATA
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 2068
diff changeset
  1049
                        if (connectionState == cs_RENEGOTIATE) {
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 2068
diff changeset
  1050
                            connectionState = cs_DATA;
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 2068
diff changeset
  1051
                        }
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 2068
diff changeset
  1052
                    } else if (handshaker.isDone()) {
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1053
                        // reset the parameters for secure renegotiation.
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1054
                        secureRenegotiation =
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1055
                                        handshaker.isSecureRenegotiation();
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1056
                        clientVerifyData = handshaker.getClientVerifyData();
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1057
                        serverVerifyData = handshaker.getServerVerifyData();
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1058
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                        sess = handshaker.getSession();
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1060
                        handshakeSession = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                        handshaker = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                        connectionState = cs_DATA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                        // Tell folk about handshake completion, but do
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                        // it in a separate thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                        if (handshakeListeners != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                            HandshakeCompletedEvent event =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                                new HandshakeCompletedEvent(this, sess);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                            Thread t = new NotifyHandshakeThread(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                                handshakeListeners.entrySet(), event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                            t.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                    }
5182
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 2068
diff changeset
  1077
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                    if (needAppData || connectionState != cs_DATA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                    }
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1081
                    break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                case Record.ct_application_data:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                    // Pass this right back up to the application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                    if (connectionState != cs_DATA
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                            && connectionState != cs_RENEGOTIATE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                            && connectionState != cs_SENT_CLOSE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                        throw new SSLProtocolException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
                            "Data received in non-data state: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                            connectionState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                    if (expectingFinished) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
                        throw new SSLProtocolException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                                ("Expecting finished message, received data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                    if (!needAppData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                        throw new SSLException("Discarding app data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                    r.setAppDataValid(true);
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1101
                    break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                case Record.ct_alert:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                    recvAlert(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                case Record.ct_change_cipher_spec:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                    if ((connectionState != cs_HANDSHAKE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
                                && connectionState != cs_RENEGOTIATE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                            || r.available() != 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                            || r.read() != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                        fatal(Alerts.alert_unexpected_message,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
                            "illegal change cipher spec msg, state = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
                            + connectionState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                    // The first message after a change_cipher_spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                    // record MUST be a "Finished" handshake record,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                    // else it's a protocol violation.  We force this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                    // to be checked by a minor tweak to the state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                    // machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
                    changeReadCiphers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
                    // next message MUST be a finished message
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                    expectingFinished = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                    // TLS requires that unrecognized records be ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                    if (debug != null && Debug.isOn("ssl")) {
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  1134
                        System.out.println(Thread.currentThread().getName() +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                            ", Received record type: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                            + r.contentType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
              } // switch
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1140
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1141
              /*
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1142
               * Check the sequence number state
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1143
               *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1144
               * Note that in order to maintain the connection I/O
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1145
               * properly, we check the sequence number after the last
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1146
               * record reading process. As we request renegotiation
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1147
               * or close the connection for wrapped sequence number
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1148
               * when there is enough sequence number space left to
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1149
               * handle a few more records, so the sequence number
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1150
               * of the last record cannot be wrapped.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1151
               */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1152
              if (connectionState < cs_ERROR) {
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
  1153
                  checkSequenceNumber(readAuthenticator, r.contentType());
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1154
              }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1155
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1156
              return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            } // synchronized (this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        // couldn't read, due to some kind of error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
        r.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
      }  // synchronized (readLock)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1168
    /**
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1169
     * Check the sequence number state
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1170
     *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1171
     * RFC 4346 states that, "Sequence numbers are of type uint64 and
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1172
     * may not exceed 2^64-1.  Sequence numbers do not wrap. If a TLS
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1173
     * implementation would need to wrap a sequence number, it must
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1174
     * renegotiate instead."
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1175
     */
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
  1176
    private void checkSequenceNumber(Authenticator authenticator, byte type)
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1177
            throws IOException {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1178
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1179
        /*
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1180
         * Don't bother to check the sequence number for error or
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1181
         * closed connections, or NULL MAC.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1182
         */
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
  1183
        if (connectionState >= cs_ERROR || authenticator == MAC.NULL) {
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1184
            return;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1185
        }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1186
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1187
        /*
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1188
         * Conservatively, close the connection immediately when the
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1189
         * sequence number is close to overflow
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1190
         */
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
  1191
        if (authenticator.seqNumOverflow()) {
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1192
            /*
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1193
             * TLS protocols do not define a error alert for sequence
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1194
             * number overflow. We use handshake_failure error alert
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1195
             * for handshaking and bad_record_mac for other records.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1196
             */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1197
            if (debug != null && Debug.isOn("ssl")) {
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  1198
                System.out.println(Thread.currentThread().getName() +
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1199
                    ", sequence number extremely close to overflow " +
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1200
                    "(2^64-1 packets). Closing connection.");
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1201
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1202
            }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1203
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1204
            fatal(Alerts.alert_handshake_failure, "sequence number overflow");
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1205
        }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1206
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1207
        /*
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1208
         * Ask for renegotiation when need to renew sequence number.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1209
         *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1210
         * Don't bother to kickstart the renegotiation when the local is
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1211
         * asking for it.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1212
         */
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
  1213
        if ((type != Record.ct_handshake) && authenticator.seqNumIsHuge()) {
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1214
            if (debug != null && Debug.isOn("ssl")) {
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  1215
                System.out.println(Thread.currentThread().getName() +
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  1216
                        ", request renegotiation " +
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1217
                        "to avoid sequence number overflow");
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1218
            }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1219
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1220
            startHandshake();
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1221
        }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1222
    }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1223
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
    // HANDSHAKE RELATED CODE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     * Return the AppInputStream. For use by Handshaker only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
    AppInputStream getAppInputStream() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        return input;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
    /**
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1236
     * Return the AppOutputStream. For use by Handshaker only.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     */
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1238
    AppOutputStream getAppOutputStream() {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1239
        return output;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     * Initialize the handshaker object. This means:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
     *  . if a handshake is already in progress (state is cs_HANDSHAKE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
     *    or cs_RENEGOTIATE), do nothing and return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
     *  . if the socket is already closed, throw an Exception (internal error)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     *  . otherwise (cs_START or cs_DATA), create the appropriate handshaker
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1251
     *    object, and advance the connection state (to cs_HANDSHAKE or
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1252
     *    cs_RENEGOTIATE, respectively).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * This method is called right after a new socket is created, when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     * starting renegotiation, or when changing client/ server mode of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     * socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
    private void initHandshaker() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        switch (connectionState) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
        // Starting a new handshake.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        case cs_START:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        case cs_DATA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
        // We're already in the middle of a handshake.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
        case cs_HANDSHAKE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        case cs_RENEGOTIATE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        // Anyone allowed to call this routine is required to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        // do so ONLY if the connection state is reasonable...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
            throw new IllegalStateException("Internal error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        // state is either cs_START or cs_DATA
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
        if (connectionState == cs_START) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
            connectionState = cs_HANDSHAKE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        } else { // cs_DATA
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
            connectionState = cs_RENEGOTIATE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
        if (roleIsServer) {
5182
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 2068
diff changeset
  1290
            handshaker = new ServerHandshaker(this, sslContext,
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1291
                    enabledProtocols, doClientAuth,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1292
                    protocolVersion, connectionState == cs_HANDSHAKE,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1293
                    secureRenegotiation, clientVerifyData, serverVerifyData);
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  1294
            handshaker.setSNIMatchers(sniMatchers);
19823
f0fd09e20528 7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents: 19223
diff changeset
  1295
            handshaker.setUseCipherSuitesOrder(preferLocalCipherSuites);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        } else {
5182
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 2068
diff changeset
  1297
            handshaker = new ClientHandshaker(this, sslContext,
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1298
                    enabledProtocols,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1299
                    protocolVersion, connectionState == cs_HANDSHAKE,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1300
                    secureRenegotiation, clientVerifyData, serverVerifyData);
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  1301
            handshaker.setSNIServerNames(serverNames);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
        }
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1303
        handshaker.setEnabledCipherSuites(enabledCipherSuites);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
        handshaker.setEnableSessionCreation(enableSessionCreation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
     * Synchronously perform the initial handshake.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
     * If the handshake is already in progress, this method blocks until it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
     * is completed. If the initial handshake has already been completed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
     * it returns immediately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
    private void performInitialHandshake() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
        // use handshakeLock and the state check to make sure only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        // one thread performs the handshake
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        synchronized (handshakeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
            if (getConnectionState() == cs_HANDSHAKE) {
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1319
                kickstartHandshake();
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1320
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                 * All initial handshaking goes through this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                 * InputRecord until we have a valid SSL connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
                 * Once initial handshaking is finished, AppInputStream's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
                 * InputRecord can handle any future renegotiation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
                 * Keep this local so that it goes out of scope and is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                 * eventually GC'd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
                if (inrec == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                    inrec = new InputRecord();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
                     * Grab the characteristics already assigned to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
                     * AppInputStream's InputRecord.  Enable checking for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
                     * SSLv2 hellos on this first handshake.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
                    inrec.setHandshakeHash(input.r.getHandshakeHash());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
                    inrec.setHelloVersion(input.r.getHelloVersion());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
                    inrec.enableFormatChecks();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
                readRecord(inrec, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                inrec = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
     * Starts an SSL handshake on this connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  1352
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
    public void startHandshake() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
        // start an ssl handshake that could be resumed from timeout exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        startHandshake(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     * Starts an ssl handshake on this connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
     * @param resumable indicates the handshake process is resumable from a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
     *          certain exception. If <code>resumable</code>, the socket will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
     *          be reserved for exceptions like timeout; otherwise, the socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
     *          will be closed, no further communications could be done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
    private void startHandshake(boolean resumable) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
        checkWrite();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
            if (getConnectionState() == cs_HANDSHAKE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
                // do initial handshake
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
                performInitialHandshake();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
                // start renegotiation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
                kickstartHandshake();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
            // shutdown and rethrow (wrapped) exception as appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
            handleException(e, resumable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
     * Kickstart the handshake if it is not already in progress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
     * This means:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
     *  . if handshaking is already underway, do nothing and return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
     *  . if the socket is not connected or already closed, throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
     *    Exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     *  . otherwise, call initHandshake() to initialize the handshaker
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     *    object and progress the state. Then, send the initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     *    handshaking message if appropriate (always on clients and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     *    on servers when renegotiating).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
    private synchronized void kickstartHandshake() throws IOException {
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1397
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        switch (connectionState) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
        case cs_HANDSHAKE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
            // handshaker already setup, proceed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
        case cs_DATA:
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1405
            if (!secureRenegotiation && !Handshaker.allowUnsafeRenegotiation) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1406
                throw new SSLHandshakeException(
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1407
                        "Insecure renegotiation is not allowed");
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1408
            }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1409
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1410
            if (!secureRenegotiation) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1411
                if (debug != null && Debug.isOn("handshake")) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1412
                    System.out.println(
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1413
                        "Warning: Using insecure renegotiation");
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1414
                }
5182
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 2068
diff changeset
  1415
            }
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 2068
diff changeset
  1416
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
            // initialize the handshaker, move to cs_RENEGOTIATE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
            initHandshaker();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
        case cs_RENEGOTIATE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
            // handshaking already in progress, return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
         * The only way to get a socket in the state is when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
         * you have an unconnected socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
        case cs_START:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
            throw new SocketException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
                "handshaking attempted on unconnected socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
            throw new SocketException("connection is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
        // Kickstart handshake state machine if we need to ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
        // Note that handshaker.kickstart() writes the message
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
        // to its HandshakeOutStream, which calls back into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        // SSLSocketImpl.writeRecord() to send it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
        //
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1444
        if (!handshaker.activated()) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1445
             // prior to handshaking, activate the handshake
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1446
            if (connectionState == cs_RENEGOTIATE) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1447
                // don't use SSLv2Hello when renegotiating
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1448
                handshaker.activate(protocolVersion);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1449
            } else {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1450
                handshaker.activate(null);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1451
            }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1452
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
            if (handshaker instanceof ClientHandshaker) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
                // send client hello
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
                handshaker.kickstart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
                if (connectionState == cs_HANDSHAKE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
                    // initial handshake, no kickstart message to send
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
                    // we want to renegotiate, send hello request
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
                    handshaker.kickstart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
                    // hello request is not included in the handshake
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
                    // hashes, reset them
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
                    handshaker.handshakeHash.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
    // CLOSURE RELATED CALLS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
     * Return whether the socket has been explicitly closed by the application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  1477
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
    public boolean isClosed() {
19223
e27cda06fe6a 8013809: deadlock in SSLSocketImpl between between write and close
xuelei
parents: 16913
diff changeset
  1479
        return connectionState == cs_APP_CLOSED;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
     * Return whether we have reached end-of-file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
     * If the socket is not connected, has been shutdown because of an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
     * or has been closed, throw an Exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
    boolean checkEOF() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
        switch (getConnectionState()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
        case cs_START:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
            throw new SocketException("Socket is not connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
        case cs_HANDSHAKE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        case cs_DATA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
        case cs_RENEGOTIATE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
        case cs_SENT_CLOSE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
        case cs_APP_CLOSED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
        case cs_ERROR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
        case cs_CLOSED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
            // either closed because of error, or normal EOF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
            if (closeReason == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
            IOException e = new SSLException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
                        ("Connection has been shutdown: " + closeReason);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
            e.initCause(closeReason);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
     * Check if we can write data to this socket. If not, throw an IOException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
    void checkWrite() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
        if (checkEOF() || (getConnectionState() == cs_SENT_CLOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
            // we are at EOF, write must throw Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
            throw new SocketException("Connection closed by remote host");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
1580
9af5946d4060 6745052: SLServerSocket file descriptor leak
xuelei
parents: 100
diff changeset
  1527
    protected void closeSocket() throws IOException {
9af5946d4060 6745052: SLServerSocket file descriptor leak
xuelei
parents: 100
diff changeset
  1528
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
        if ((debug != null) && Debug.isOn("ssl")) {
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  1530
            System.out.println(Thread.currentThread().getName() +
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  1531
                                                ", called closeSocket()");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
        }
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  1533
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  1534
        super.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
9514
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1537
    private void closeSocket(boolean selfInitiated) throws IOException {
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1538
        if ((debug != null) && Debug.isOn("ssl")) {
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  1539
            System.out.println(Thread.currentThread().getName() +
11018
be74e8b8f3eb 7111548: unexpected debug log message
xuelei
parents: 10336
diff changeset
  1540
                ", called closeSocket(" + selfInitiated + ")");
9514
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1541
        }
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  1542
        if (!isLayered() || autoClose) {
9514
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1543
            super.close();
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1544
        } else if (selfInitiated) {
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1545
            // layered && non-autoclose
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1546
            // read close_notify alert to clear input stream
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1547
            waitForClose(false);
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1548
        }
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1549
    }
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1550
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
     * Closing the connection is tricky ... we can't officially close the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
     * connection until we know the other end is ready to go away too,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
     * and if ever the connection gets aborted we must forget session
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
     * state (it becomes invalid).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
     * Closes the SSL connection.  SSL includes an application level
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
     * shutdown handshake; you should close SSL sockets explicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
     * rather than leaving it for finalization, so that your remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
     * peer does not experience a protocol error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  1564
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
        if ((debug != null) && Debug.isOn("ssl")) {
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  1567
            System.out.println(Thread.currentThread().getName() +
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  1568
                                                    ", called close()");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
        closeInternal(true);  // caller is initiating close
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
        setConnectionState(cs_APP_CLOSED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
     * Don't synchronize the whole method because waitForClose()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
     * (which calls readRecord()) might be called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
     * @param selfInitiated Indicates which party initiated the close.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
     * If selfInitiated, this side is initiating a close; for layered and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
     * non-autoclose socket, wait for close_notify response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
     * If !selfInitiated, peer sent close_notify; we reciprocate but
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
     * no need to wait for response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
    private void closeInternal(boolean selfInitiated) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
        if ((debug != null) && Debug.isOn("ssl")) {
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  1586
            System.out.println(Thread.currentThread().getName() +
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  1587
                        ", called closeInternal(" + selfInitiated + ")");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
        int state = getConnectionState();
9514
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1591
        boolean closeSocketCalled = false;
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1592
        Throwable cachedThrowable = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
            switch (state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
            case cs_START:
13370
785cedd026db 7179879: SSLSocket connect times out instead of throwing socket closed exception
coffeys
parents: 12428
diff changeset
  1596
                // unconnected socket or handshaking has not been initialized
785cedd026db 7179879: SSLSocket connect times out instead of throwing socket closed exception
coffeys
parents: 12428
diff changeset
  1597
                closeSocket(selfInitiated);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
             * If we're closing down due to error, we already sent (or else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
             * received) the fatal alert ... no niceties, blow the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
             * away as quickly as possible (even if we didn't allocate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
             * socket ourselves; it's unusable, regardless).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
            case cs_ERROR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
                closeSocket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
             * Sometimes close() gets called more than once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
            case cs_CLOSED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
            case cs_APP_CLOSED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
                 break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
             * Otherwise we indicate clean termination.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
            // case cs_HANDSHAKE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
            // case cs_DATA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
            // case cs_RENEGOTIATE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
            // case cs_SENT_CLOSE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
                synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
                    if (((state = getConnectionState()) == cs_CLOSED) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
                       (state == cs_ERROR) || (state == cs_APP_CLOSED)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
                        return;  // connection was closed while we waited
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
                    if (state != cs_SENT_CLOSE) {
9514
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1631
                        try {
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1632
                            warning(Alerts.alert_close_notify);
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1633
                            connectionState = cs_SENT_CLOSE;
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1634
                        } catch (Throwable th) {
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1635
                            // we need to ensure socket is closed out
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1636
                            // if we encounter any errors.
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1637
                            connectionState = cs_ERROR;
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1638
                            // cache this for later use
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1639
                            cachedThrowable = th;
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1640
                            closeSocketCalled = true;
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1641
                            closeSocket(selfInitiated);
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1642
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
                // If state was cs_SENT_CLOSE before, we don't do the actual
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
                // closing since it is already in progress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
                if (state == cs_SENT_CLOSE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
                    if (debug != null && Debug.isOn("ssl")) {
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  1649
                        System.out.println(Thread.currentThread().getName() +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
                            ", close invoked again; state = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
                            getConnectionState());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
                    if (selfInitiated == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
                        // We were called because a close_notify message was
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
                        // received. This may be due to another thread calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
                        // read() or due to our call to waitForClose() below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
                        // In either case, just return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
                    // Another thread explicitly called close(). We need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
                    // wait for the closing to complete before returning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
                    synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
                        while (connectionState < cs_CLOSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
                                this.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
                            } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
                                // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
                    if ((debug != null) && Debug.isOn("ssl")) {
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  1672
                        System.out.println(Thread.currentThread().getName() +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
                            ", after primary close; state = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
                            getConnectionState());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
9514
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1679
                if (!closeSocketCalled)  {
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1680
                    closeSocketCalled = true;
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1681
                    closeSocket(selfInitiated);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
                // Upon exit from this method, the state is always >= cs_CLOSED
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
                connectionState = (connectionState == cs_APP_CLOSED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
                                ? cs_APP_CLOSED : cs_CLOSED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
                // notify any threads waiting for the closing to finish
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
                this.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
            }
9514
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1694
            if (closeSocketCalled) {
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1695
                // Dispose of ciphers since we've closed socket
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1696
                disposeCiphers();
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1697
            }
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1698
            if (cachedThrowable != null) {
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1699
               /*
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1700
                * Rethrow the error to the calling method
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1701
                * The Throwable caught can only be an Error or RuntimeException
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1702
                */
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1703
                if (cachedThrowable instanceof Error)
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1704
                    throw (Error) cachedThrowable;
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1705
                if (cachedThrowable instanceof RuntimeException)
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1706
                    throw (RuntimeException) cachedThrowable;
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1707
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
     * Reads a close_notify or a fatal alert from the input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
     * Keep reading records until we get a close_notify or until
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
     * the connection is otherwise closed.  The close_notify or alert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
     * might be read by another reader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
     * which will then process the close and set the connection state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
    void waitForClose(boolean rethrow) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
        if (debug != null && Debug.isOn("ssl")) {
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  1720
            System.out.println(Thread.currentThread().getName() +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
                ", waiting for close_notify or alert: state "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
                + getConnectionState());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
            int state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
            while (((state = getConnectionState()) != cs_CLOSED) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
                   (state != cs_ERROR) && (state != cs_APP_CLOSED)) {
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 19823
diff changeset
  1730
                // create the InputRecord if it isn't initialized.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
                if (inrec == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
                    inrec = new InputRecord();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
                // Ask for app data and then throw it away
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
                    readRecord(inrec, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
                } catch (SocketTimeoutException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
                    // if time out, ignore the exception and continue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
            inrec = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
            if (debug != null && Debug.isOn("ssl")) {
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  1745
                System.out.println(Thread.currentThread().getName() +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
                    ", Exception while waiting for close " +e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
            if (rethrow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
                throw e; // pass exception up
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
9514
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1754
    /**
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1755
     * Called by closeInternal() only. Be sure to consider the
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1756
     * synchronization locks carefully before calling it elsewhere.
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1757
     */
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1758
    private void disposeCiphers() {
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1759
        // See comment in changeReadCiphers()
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1760
        synchronized (readLock) {
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1761
            readCipher.dispose();
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1762
        }
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1763
        // See comment in changeReadCiphers()
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1764
        writeLock.lock();
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1765
        try {
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1766
            writeCipher.dispose();
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1767
        } finally {
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1768
            writeLock.unlock();
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1769
        }
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1770
    }
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1771
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
    // EXCEPTION AND ALERT HANDLING
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
     * Handle an exception. This method is called by top level exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
     * handlers (in read(), write()) to make sure we always shutdown the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
     * connection correctly and do not pass runtime exception to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
     * application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
    void handleException(Exception e) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
        handleException(e, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
     * Handle an exception. This method is called by top level exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
     * handlers (in read(), write(), startHandshake()) to make sure we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
     * always shutdown the connection correctly and do not pass runtime
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
     * exception to the application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
     * This method never returns normally, it always throws an IOException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
     * We first check if the socket has already been shutdown because of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
     * error. If so, we just rethrow the exception. If the socket has not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
     * been shutdown, we sent a fatal alert and remember the exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
     * @param e the Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
     * @param resumable indicates the caller process is resumable from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
     *          exception. If <code>resumable</code>, the socket will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
     *          reserved for exceptions like timeout; otherwise, the socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
     *          will be closed, no further communications could be done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
    synchronized private void handleException(Exception e, boolean resumable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
        if ((debug != null) && Debug.isOn("ssl")) {
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  1807
            System.out.println(Thread.currentThread().getName() +
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  1808
                        ", handling exception: " + e.toString());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
        // don't close the Socket in case of timeouts or interrupts if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
        // the process is resumable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
        if (e instanceof InterruptedIOException && resumable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
            throw (IOException)e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
        // if we've already shutdown because of an error,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
        // there is nothing to do except rethrow the exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
        if (closeReason != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
            if (e instanceof IOException) { // includes SSLException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
                throw (IOException)e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
                // this is odd, not an IOException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
                // normally, this should not happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
                // if closeReason has been already been set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
                throw Alerts.getSSLException(Alerts.alert_internal_error, e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
                                      "Unexpected exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
        // need to perform error shutdown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
        boolean isSSLException = (e instanceof SSLException);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
        if ((isSSLException == false) && (e instanceof IOException)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
            // IOException from the socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
            // this means the TCP connection is already dead
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
            // we call fatal just to set the error status
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
                fatal(Alerts.alert_unexpected_message, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
            } catch (IOException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
                // ignore (IOException wrapped in SSLException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
            // rethrow original IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
            throw (IOException)e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
        // must be SSLException or RuntimeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
        byte alertType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
        if (isSSLException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
            if (e instanceof SSLHandshakeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
                alertType = Alerts.alert_handshake_failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
                alertType = Alerts.alert_unexpected_message;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
            alertType = Alerts.alert_internal_error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
        fatal(alertType, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
     * Send a warning alert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
    void warning(byte description) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
        sendAlert(Alerts.alert_warning, description);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
    synchronized void fatal(byte description, String diagnostic)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
        fatal(description, diagnostic, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
    synchronized void fatal(byte description, Throwable cause)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
        fatal(description, null, cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
     * Send a fatal alert, and throw an exception so that callers will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
     * need to stand on their heads to accidentally continue processing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
    synchronized void fatal(byte description, String diagnostic,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
            Throwable cause) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
        if ((input != null) && (input.r != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
            input.r.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
        sess.invalidate();
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1887
        if (handshakeSession != null) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1888
            handshakeSession.invalidate();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1889
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
        int oldState = connectionState;
9514
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1892
        if (connectionState < cs_ERROR) {
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1893
            connectionState = cs_ERROR;
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1894
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
         * Has there been an error received yet?  If not, remember it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
         * By RFC 2246, we don't bother waiting for a response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
         * Fatal errors require immediate shutdown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
        if (closeReason == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
             * Try to clear the kernel buffer to avoid TCP connection resets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
            if (oldState == cs_HANDSHAKE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
                sockInput.skip(sockInput.available());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
            }
100
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
  1908
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
  1909
            // If the description equals -1, the alert won't be sent to peer.
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
  1910
            if (description != -1) {
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
  1911
                sendAlert(Alerts.alert_fatal, description);
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
  1912
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
            if (cause instanceof SSLException) { // only true if != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
                closeReason = (SSLException)cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
                closeReason =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
                    Alerts.getSSLException(description, cause, diagnostic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
         * Clean up our side.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
        closeSocket();
9514
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1925
        // Another thread may have disposed the ciphers during closing
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1926
        if (connectionState < cs_CLOSED) {
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1927
            connectionState = (oldState == cs_APP_CLOSED) ? cs_APP_CLOSED
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1928
                                                              : cs_CLOSED;
1763
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 1580
diff changeset
  1929
9514
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1930
            // We should lock readLock and writeLock if no deadlock risks.
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1931
            // See comment in changeReadCiphers()
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1932
            readCipher.dispose();
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1933
            writeCipher.dispose();
bdb24db75fe8 7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents: 9246
diff changeset
  1934
        }
1763
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 1580
diff changeset
  1935
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
        throw closeReason;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
     * Process an incoming alert ... caller must already have synchronized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
     * access to "this".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
    private void recvAlert(InputRecord r) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
        byte level = (byte)r.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
        byte description = (byte)r.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
        if (description == -1) { // check for short message
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
            fatal(Alerts.alert_illegal_parameter, "Short alert message");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
        if (debug != null && (Debug.isOn("record") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
                Debug.isOn("handshake"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
            synchronized (System.out) {
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  1954
                System.out.print(Thread.currentThread().getName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
                System.out.print(", RECV " + protocolVersion + " ALERT:  ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
                if (level == Alerts.alert_fatal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
                    System.out.print("fatal, ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
                } else if (level == Alerts.alert_warning) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
                    System.out.print("warning, ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
                    System.out.print("<level " + (0x0ff & level) + ">, ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
                System.out.println(Alerts.alertDescription(description));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
        if (level == Alerts.alert_warning) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
            if (description == Alerts.alert_close_notify) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
                if (connectionState == cs_HANDSHAKE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
                    fatal(Alerts.alert_unexpected_message,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
                                "Received close_notify during handshake");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
                    closeInternal(false);  // reply to close
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
                // The other legal warnings relate to certificates,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
                // e.g. no_certificate, bad_certificate, etc; these
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
                // are important to the handshaking code, which can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
                // also handle illegal protocol alerts if needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
                if (handshaker != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
                    handshaker.handshakeAlert(description);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
        } else { // fatal or unknown level
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
            String reason = "Received fatal alert: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
                + Alerts.alertDescription(description);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
            if (closeReason == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
                closeReason = Alerts.getSSLException(description, reason);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
            fatal(Alerts.alert_unexpected_message, reason);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
     * Emit alerts.  Caller must have synchronized with "this".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
    private void sendAlert(byte level, byte description) {
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  2002
        // the connectionState cannot be cs_START
100
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
  2003
        if (connectionState >= cs_SENT_CLOSE) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  2007
        // For initial handshaking, don't send alert message to peer if
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  2008
        // handshaker has not started.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  2009
        if (connectionState == cs_HANDSHAKE &&
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  2010
            (handshaker == null || !handshaker.started())) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  2011
            return;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  2012
        }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  2013
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
        OutputRecord r = new OutputRecord(Record.ct_alert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
        r.setVersion(protocolVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
        boolean useDebug = debug != null && Debug.isOn("ssl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
        if (useDebug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
            synchronized (System.out) {
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  2020
                System.out.print(Thread.currentThread().getName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
                System.out.print(", SEND " + protocolVersion + " ALERT:  ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
                if (level == Alerts.alert_fatal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
                    System.out.print("fatal, ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
                } else if (level == Alerts.alert_warning) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
                    System.out.print("warning, ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
                    System.out.print("<level = " + (0x0ff & level) + ">, ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
                System.out.println("description = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
                        + Alerts.alertDescription(description));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
        r.write(level);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
        r.write(description);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
            writeRecord(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
            if (useDebug) {
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  2040
                System.out.println(Thread.currentThread().getName() +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
                    ", Exception sending alert: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
    // VARIOUS OTHER METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
     * When a connection finishes handshaking by enabling use of a newly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
     * negotiated session, each end learns about it in two halves (read,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
     * and write).  When both read and write ciphers have changed, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
     * last handshake message has been read, the connection has joined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
     * (rejoined) the new session.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
     * NOTE:  The SSLv3 spec is rather unclear on the concepts here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
     * Sessions don't change once they're established (including cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
     * suite and master secret) but connections can join them (and leave
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
     * them).  They're created by handshaking, though sometime handshaking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
     * causes connections to join up with pre-established sessions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
    private void changeReadCiphers() throws SSLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
        if (connectionState != cs_HANDSHAKE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
                && connectionState != cs_RENEGOTIATE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
            throw new SSLProtocolException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
                "State error, change cipher specs");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
        // ... create decompressor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
1763
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 1580
diff changeset
  2072
        CipherBox oldCipher = readCipher;
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 1580
diff changeset
  2073
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
            readCipher = handshaker.newReadCipher();
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
  2076
            readAuthenticator = handshaker.newReadAuthenticator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
            // "can't happen"
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 10128
diff changeset
  2079
            throw new SSLException("Algorithm missing:  ", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
        }
1763
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 1580
diff changeset
  2081
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 1580
diff changeset
  2082
        /*
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 1580
diff changeset
  2083
         * Dispose of any intermediate state in the underlying cipher.
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 1580
diff changeset
  2084
         * For PKCS11 ciphers, this will release any attached sessions,
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 1580
diff changeset
  2085
         * and thus make finalization faster.
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 1580
diff changeset
  2086
         *
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 1580
diff changeset
  2087
         * Since MAC's doFinal() is called for every SSL/TLS packet, it's
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 1580
diff changeset
  2088
         * not necessary to do the same with MAC's.
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 1580
diff changeset
  2089
         */
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 1580
diff changeset
  2090
        oldCipher.dispose();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
    // used by Handshaker
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
    void changeWriteCiphers() throws SSLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
        if (connectionState != cs_HANDSHAKE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
                && connectionState != cs_RENEGOTIATE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
            throw new SSLProtocolException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
                "State error, change cipher specs");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
        // ... create compressor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
1763
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 1580
diff changeset
  2103
        CipherBox oldCipher = writeCipher;
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 1580
diff changeset
  2104
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
            writeCipher = handshaker.newWriteCipher();
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
  2107
            writeAuthenticator = handshaker.newWriteAuthenticator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
            // "can't happen"
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 10128
diff changeset
  2110
            throw new SSLException("Algorithm missing:  ", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
        }
1763
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 1580
diff changeset
  2112
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 1580
diff changeset
  2113
        // See comment above.
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 1580
diff changeset
  2114
        oldCipher.dispose();
10915
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9514
diff changeset
  2115
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9514
diff changeset
  2116
        // reset the flag of the first application record
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9514
diff changeset
  2117
        isFirstAppOutputRecord = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
     * Updates the SSL version associated with this connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
     * Called from Handshaker once it has determined the negotiated version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
    synchronized void setVersion(ProtocolVersion protocolVersion) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
        this.protocolVersion = protocolVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
        output.r.setVersion(protocolVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
    synchronized String getHost() {
2068
cdbc5929b91e 5067458: Loopback SSLSocketImpl createSocket is throwing an exception
xuelei
parents: 1763
diff changeset
  2130
        // Note that the host may be null or empty for localhost.
cdbc5929b91e 5067458: Loopback SSLSocketImpl createSocket is throwing an exception
xuelei
parents: 1763
diff changeset
  2131
        if (host == null || host.length() == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
            host = getInetAddress().getHostName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
        return host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
5162
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 2068
diff changeset
  2137
    // 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
  2138
    //
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  2139
    // 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
  2140
    // SSLSocket.setSSLParameters(). Otherwise, the {@code host} parameter
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  2141
    // may override SNIHostName in the customized server name indication.
5162
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 2068
diff changeset
  2142
    synchronized public void setHost(String host) {
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 2068
diff changeset
  2143
        this.host = host;
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  2144
        this.serverNames =
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  2145
            Utilities.addToSNIServerNameList(this.serverNames, this.host);
5162
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 2068
diff changeset
  2146
    }
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 2068
diff changeset
  2147
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
     * Gets an input stream to read from the peer on the other side.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
     * Data read from this stream was always integrity protected in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
     * transit, and will usually have been confidentiality protected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  2153
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
    synchronized public InputStream getInputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
        if (isClosed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
         * Can't call isConnected() here, because the Handshakers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
         * do some initialization before we actually connect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
        if (connectionState == cs_START) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
            throw new SocketException("Socket is not connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
        return input;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
     * Gets an output stream to write to the peer on the other side.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
     * Data written on this stream is always integrity protected, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
     * will usually be confidentiality protected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  2175
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
    synchronized public OutputStream getOutputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
        if (isClosed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
         * Can't call isConnected() here, because the Handshakers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
         * do some initialization before we actually connect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
        if (connectionState == cs_START) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
            throw new SocketException("Socket is not connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
        return output;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
     * Returns the the SSL Session in use by this connection.  These can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
     * be long lived, and frequently correspond to an entire login session
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
     * for some user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  2197
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
    public SSLSession getSession() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
         * Force a synchronous handshake, if appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
        if (getConnectionState() == cs_HANDSHAKE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
                // start handshaking, if failed, the connection will be closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
                startHandshake(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
                // handshake failed. log and return a nullSession
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
                if (debug != null && Debug.isOn("handshake")) {
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  2209
                      System.out.println(Thread.currentThread().getName() +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
                          ", IOException in getSession():  " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
            return sess;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2219
    @Override
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2220
    synchronized public SSLSession getHandshakeSession() {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2221
        return handshakeSession;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2222
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2223
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2224
    synchronized void setHandshakeSession(SSLSessionImpl session) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2225
        handshakeSession = session;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2226
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2227
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
     * Controls whether new connections may cause creation of new SSL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
     * sessions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
     * As long as handshaking has not started, we can change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
     * whether we enable session creations.  Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
     * we will need to wait for the next handshake.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  2236
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
    synchronized public void setEnableSessionCreation(boolean flag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
        enableSessionCreation = flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  2240
        if ((handshaker != null) && !handshaker.activated()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
            handshaker.setEnableSessionCreation(enableSessionCreation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
     * Returns true if new connections may cause creation of new SSL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
     * sessions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  2249
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
    synchronized public boolean getEnableSessionCreation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
        return enableSessionCreation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
     * Sets the flag controlling whether a server mode socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
     * *REQUIRES* SSL client authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
     * As long as handshaking has not started, we can change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
     * whether client authentication is needed.  Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
     * we will need to wait for the next handshake.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  2263
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
    synchronized public void setNeedClientAuth(boolean flag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
        doClientAuth = (flag ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
            SSLEngineImpl.clauth_required : SSLEngineImpl.clauth_none);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
        if ((handshaker != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
                (handshaker instanceof ServerHandshaker) &&
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  2270
                !handshaker.activated()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
            ((ServerHandshaker) handshaker).setClientAuth(doClientAuth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  2275
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
    synchronized public boolean getNeedClientAuth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
        return (doClientAuth == SSLEngineImpl.clauth_required);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
     * Sets the flag controlling whether a server mode socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
     * *REQUESTS* SSL client authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
     * As long as handshaking has not started, we can change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
     * whether client authentication is requested.  Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
     * we will need to wait for the next handshake.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  2288
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
    synchronized public void setWantClientAuth(boolean flag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
        doClientAuth = (flag ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
            SSLEngineImpl.clauth_requested : SSLEngineImpl.clauth_none);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
        if ((handshaker != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
                (handshaker instanceof ServerHandshaker) &&
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  2295
                !handshaker.activated()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
            ((ServerHandshaker) handshaker).setClientAuth(doClientAuth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  2300
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
    synchronized public boolean getWantClientAuth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
        return (doClientAuth == SSLEngineImpl.clauth_requested);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
     * Sets the flag controlling whether the socket is in SSL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
     * client or server mode.  Must be called before any SSL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
     * traffic has started.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  2311
    @Override
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 10128
diff changeset
  2312
    @SuppressWarnings("fallthrough")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
    synchronized public void setUseClientMode(boolean flag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
        switch (connectionState) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
        case cs_START:
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  2317
            /*
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  2318
             * If we need to change the socket mode and the enabled
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  2319
             * protocols haven't specifically been set by the user,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  2320
             * change them to the corresponding default ones.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  2321
             */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  2322
            if (roleIsServer != (!flag) &&
9246
c459f79af46b 6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents: 7043
diff changeset
  2323
                    sslContext.isDefaultProtocolList(enabledProtocols)) {
c459f79af46b 6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents: 7043
diff changeset
  2324
                enabledProtocols = sslContext.getDefaultProtocolList(!flag);
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  2325
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
            roleIsServer = !flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
        case cs_HANDSHAKE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
             * If we have a handshaker, but haven't started
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
             * SSL traffic, we can throw away our current
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
             * handshaker, and start from scratch.  Don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
             * need to call doneConnect() again, we already
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
             * have the streams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
            assert(handshaker != null);
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  2338
            if (!handshaker.activated()) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  2339
                /*
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  2340
                 * If we need to change the socket mode and the enabled
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  2341
                 * protocols haven't specifically been set by the user,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  2342
                 * change them to the corresponding default ones.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  2343
                 */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  2344
                if (roleIsServer != (!flag) &&
9246
c459f79af46b 6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents: 7043
diff changeset
  2345
                        sslContext.isDefaultProtocolList(enabledProtocols)) {
c459f79af46b 6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents: 7043
diff changeset
  2346
                    enabledProtocols = sslContext.getDefaultProtocolList(!flag);
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  2347
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
                roleIsServer = !flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
                connectionState = cs_START;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
                initHandshaker();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
            // If handshake has started, that's an error.  Fall through...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
            if (debug != null && Debug.isOn("ssl")) {
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  2358
                System.out.println(Thread.currentThread().getName() +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
                    ", setUseClientMode() invoked in state = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
                    connectionState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
                "Cannot change mode after SSL traffic has started");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  2367
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
    synchronized public boolean getUseClientMode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
        return !roleIsServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
     * Returns the names of the cipher suites which could be enabled for use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
     * on an SSL connection.  Normally, only a subset of these will actually
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
     * be enabled by default, since this list may include cipher suites which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
     * do not support the mutual authentication of servers and clients, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
     * which do not protect data confidentiality.  Servers may also need
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
     * certain kinds of certificates to use certain cipher suites.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
     * @return an array of cipher suite names
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  2383
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
    public String[] getSupportedCipherSuites() {
13815
2de30ecf335e 7199066: Typo in method name
xuelei
parents: 13370
diff changeset
  2385
        return sslContext.getSupportedCipherSuiteList().toStringArray();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
     * Controls which particular cipher suites are enabled for use on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
     * this connection.  The cipher suites must have been listed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
     * getCipherSuites() as being supported.  Even if a suite has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
     * enabled, it might never be used if no peer supports it or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
     * requisite certificates (and private keys) are not available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
     * @param suites Names of all the cipher suites to enable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  2397
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
    synchronized public void setEnabledCipherSuites(String[] suites) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
        enabledCipherSuites = new CipherSuiteList(suites);
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  2400
        if ((handshaker != null) && !handshaker.activated()) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  2401
            handshaker.setEnabledCipherSuites(enabledCipherSuites);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
     * Returns the names of the SSL cipher suites which are currently enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
     * for use on this connection.  When an SSL socket is first created,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
     * all enabled cipher suites <em>(a)</em> protect data confidentiality,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
     * by traffic encryption, and <em>(b)</em> can mutually authenticate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
     * both clients and servers.  Thus, in some environments, this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
     * might be empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
     * @return an array of cipher suite names
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  2415
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
    synchronized public String[] getEnabledCipherSuites() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
        return enabledCipherSuites.toStringArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
     * Returns the protocols that are supported by this implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
     * A subset of the supported protocols may be enabled for this connection
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2424
     * @return an array of protocol names.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  2426
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
    public String[] getSupportedProtocols() {
9246
c459f79af46b 6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents: 7043
diff changeset
  2428
        return sslContext.getSuportedProtocolList().toStringArray();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
     * Controls which protocols are enabled for use on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
     * this connection.  The protocols must have been listed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
     * getSupportedProtocols() as being supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
     * @param protocols protocols to enable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
     * @exception IllegalArgumentException when one of the protocols
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
     *  named by the parameter is not supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  2440
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
    synchronized public void setEnabledProtocols(String[] protocols) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
        enabledProtocols = new ProtocolList(protocols);
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  2443
        if ((handshaker != null) && !handshaker.activated()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
            handshaker.setEnabledProtocols(enabledProtocols);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  2448
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
    synchronized public String[] getEnabledProtocols() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
        return enabledProtocols.toStringArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
     * Assigns the socket timeout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
     * @see java.net.Socket#setSoTimeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  2457
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
    public void setSoTimeout(int timeout) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
        if ((debug != null) && Debug.isOn("ssl")) {
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  2460
            System.out.println(Thread.currentThread().getName() +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
                ", setSoTimeout(" + timeout + ") called");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
        }
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  2463
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  2464
        super.setSoTimeout(timeout);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
     * Registers an event listener to receive notifications that an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
     * SSL handshake has completed on this connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  2471
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
    public synchronized void addHandshakeCompletedListener(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
            HandshakeCompletedListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
        if (listener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
            throw new IllegalArgumentException("listener is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
        if (handshakeListeners == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
            handshakeListeners = new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
                HashMap<HandshakeCompletedListener, AccessControlContext>(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
        handshakeListeners.put(listener, AccessController.getContext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
     * Removes a previously registered handshake completion listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  2488
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
    public synchronized void removeHandshakeCompletedListener(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
            HandshakeCompletedListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
        if (handshakeListeners == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
            throw new IllegalArgumentException("no listeners");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
        if (handshakeListeners.remove(listener) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
            throw new IllegalArgumentException("listener not registered");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
        if (handshakeListeners.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
            handshakeListeners = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
    /**
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2503
     * Returns the SSLParameters in effect for this SSLSocket.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  2505
    @Override
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2506
    synchronized public SSLParameters getSSLParameters() {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2507
        SSLParameters params = super.getSSLParameters();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2508
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2509
        // the super implementation does not handle the following parameters
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2510
        params.setEndpointIdentificationAlgorithm(identificationProtocol);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2511
        params.setAlgorithmConstraints(algorithmConstraints);
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  2512
        params.setSNIMatchers(sniMatchers);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  2513
        params.setServerNames(serverNames);
19823
f0fd09e20528 7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents: 19223
diff changeset
  2514
        params.setUseCipherSuitesOrder(preferLocalCipherSuites);
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2515
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2516
        return params;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
    /**
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2520
     * Applies SSLParameters to this socket.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  2522
    @Override
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2523
    synchronized public void setSSLParameters(SSLParameters params) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2524
        super.setSSLParameters(params);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2525
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2526
        // the super implementation does not handle the following parameters
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2527
        identificationProtocol = params.getEndpointIdentificationAlgorithm();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2528
        algorithmConstraints = params.getAlgorithmConstraints();
19823
f0fd09e20528 7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents: 19223
diff changeset
  2529
        preferLocalCipherSuites = params.getUseCipherSuitesOrder();
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  2530
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  2531
        List<SNIServerName> sniNames = params.getServerNames();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  2532
        if (sniNames != null) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  2533
            serverNames = sniNames;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  2534
        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  2535
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  2536
        Collection<SNIMatcher> matchers = params.getSNIMatchers();
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  2537
        if (matchers != null) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  2538
            sniMatchers = matchers;
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  2539
        }
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  2540
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2541
        if ((handshaker != null) && !handshaker.started()) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2542
            handshaker.setIdentificationProtocol(identificationProtocol);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2543
            handshaker.setAlgorithmConstraints(algorithmConstraints);
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  2544
            if (roleIsServer) {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  2545
                handshaker.setSNIMatchers(sniMatchers);
19823
f0fd09e20528 7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents: 19223
diff changeset
  2546
                handshaker.setUseCipherSuitesOrder(preferLocalCipherSuites);
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  2547
            } else {
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  2548
                handshaker.setSNIServerNames(serverNames);
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  2549
            }
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2550
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
    // We allocate a separate thread to deliver handshake completion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
    // events.  This ensures that the notifications don't block the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
    // protocol state machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
    private static class NotifyHandshakeThread extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
        private Set<Map.Entry<HandshakeCompletedListener,AccessControlContext>>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
                targets;        // who gets notified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
        private HandshakeCompletedEvent event;          // the notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
        NotifyHandshakeThread(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
            Set<Map.Entry<HandshakeCompletedListener,AccessControlContext>>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
            entrySet, HandshakeCompletedEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
            super("HandshakeCompletedNotify-Thread");
10128
f505a8514bd7 7065972: Some race condition may happen in SSLSocketImpl class
xuelei
parents: 9514
diff changeset
  2569
            targets = new HashSet<>(entrySet);          // clone the entry set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
            event = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  2573
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
        public void run() {
10128
f505a8514bd7 7065972: Some race condition may happen in SSLSocketImpl class
xuelei
parents: 9514
diff changeset
  2575
            // Don't need to synchronize, as it only runs in one thread.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
            for (Map.Entry<HandshakeCompletedListener,AccessControlContext>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
                entry : targets) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
                final HandshakeCompletedListener l = entry.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
                AccessControlContext acc = entry.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
                AccessController.doPrivileged(new PrivilegedAction<Void>() {
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  2582
                    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
                    public Void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
                        l.handshakeCompleted(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
                }, acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
     * Returns a printable representation of this end of the connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14194
diff changeset
  2595
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
        StringBuffer retval = new StringBuffer(80);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
        retval.append(Integer.toHexString(hashCode()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
        retval.append("[");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
        retval.append(sess.getCipherSuite());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
        retval.append(": ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 13815
diff changeset
  2604
        retval.append(super.toString());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
        retval.append("]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
        return retval.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
}