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