jdk/src/share/classes/sun/security/ssl/SSLEngineImpl.java
author asaha
Mon, 14 Nov 2011 11:52:09 -0800
changeset 10917 becbe249e46b
parent 10336 0bb1999251f8
parent 10915 1e20964cebf3
child 11684 c4018f1df09b
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9246
c459f79af46b 6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents: 7043
diff changeset
     2
 * Copyright (c) 2003, 2011, 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: 5182
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: 5182
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: 5182
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5182
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5182
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.crypto.BadPaddingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.net.ssl.SSLEngineResult.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Implementation of an non-blocking SSLEngine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * *Currently*, the SSLEngine code exists in parallel with the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * SSLSocket.  As such, the current implementation is using legacy code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * with many of the same abstractions.  However, it varies in many
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * areas, most dramatically in the IO handling.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * There are three main I/O threads that can be existing in parallel:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * wrap(), unwrap(), and beginHandshake().  We are encouraging users to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * not call multiple instances of wrap or unwrap, because the data could
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * appear to flow out of the SSLEngine in a non-sequential order.  We
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * take all steps we can to at least make sure the ordering remains
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * consistent, but once the calls returns, anything can happen.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * example, thread1 and thread2 both call wrap, thread1 gets the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * packet, thread2 gets the second packet, but thread2 gets control back
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * before thread1, and sends the data.  The receiving side would see an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * out-of-order error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * Handshaking is still done the same way as SSLSocket using the normal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * InputStream/OutputStream abstactions.  We create
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * ClientHandshakers/ServerHandshakers, which produce/consume the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * handshaking data.  The transfer of the data is largely handled by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * HandshakeInStream/HandshakeOutStreams.  Lastly, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * InputRecord/OutputRecords still have the same functionality, except
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * that they are overridden with EngineInputRecord/EngineOutputRecord,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * which provide SSLEngine-specific functionality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * Some of the major differences are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * EngineInputRecord/EngineOutputRecord/EngineWriter:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *      In order to avoid writing whole new control flows for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *      handshaking, and to reuse most of the same code, we kept most
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *      of the actual handshake code the same.  As usual, reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *      handshake data may trigger output of more handshake data, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *      what we do is write this data to internal buffers, and wait for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *      wrap() to be called to give that data a ride.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *      All data is routed through
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *      EngineInputRecord/EngineOutputRecord.  However, all handshake
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *      data (ct_alert/ct_change_cipher_spec/ct_handshake) are passed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *      through to the the underlying InputRecord/OutputRecord, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *      the data uses the internal buffers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *      Application data is handled slightly different, we copy the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *      directly from the src to the dst buffers, and do all operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *      on those buffers, saving the overhead of multiple copies.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *      In the case of an inbound record, unwrap passes the inbound
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *      ByteBuffer to the InputRecord.  If the data is handshake data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *      the data is read into the InputRecord's internal buffer.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *      the data is application data, the data is decoded directly into
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *      the dst buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *      In the case of an outbound record, when the write to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *      "real" OutputStream's would normally take place, instead we
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *      call back up to the EngineOutputRecord's version of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *      writeBuffer, at which time we capture the resulting output in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *      ByteBuffer, and send that back to the EngineWriter for internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *      storage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *      EngineWriter is responsible for "handling" all outbound
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *      data, be it handshake or app data, and for returning the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *      to wrap() in the proper order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * ClientHandshaker/ServerHandshaker/Handshaker:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *      Methods which relied on SSLSocket now have work on either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *      SSLSockets or SSLEngines.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * @author Brad Wetmore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
final public class SSLEngineImpl extends SSLEngine {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    // Fields and global comments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * There's a state machine associated with each connection, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * among other roles serves to negotiate session changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * - START with constructor, until the TCP connection's around.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * - HANDSHAKE picks session parameters before allowing traffic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *          There are many substates due to sequencing requirements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *          for handshake messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * - DATA may be transmitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * - RENEGOTIATE state allows concurrent data and handshaking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *          traffic ("same" substates as HANDSHAKE), and terminates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *          in selection of new session (and connection) parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * - ERROR state immediately precedes abortive disconnect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * - CLOSED when one side closes down, used to start the shutdown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *          process.  SSL connection objects are not reused.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * State affects what SSL record types may legally be sent:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * - Handshake ... only in HANDSHAKE and RENEGOTIATE states
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * - App Data ... only in DATA and RENEGOTIATE states
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * - Alert ... in HANDSHAKE, DATA, RENEGOTIATE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Re what may be received:  same as what may be sent, except that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * HandshakeRequest handshaking messages can come from servers even
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * in the application data state, to request entry to RENEGOTIATE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * The state machine within HANDSHAKE and RENEGOTIATE states controls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * the pending session, not the connection state, until the change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * cipher spec and "Finished" handshake messages are processed and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * make the "new" session become the current one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * NOTE: details of the SMs always need to be nailed down better.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * The text above illustrates the core ideas.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *                +---->-------+------>--------->-------+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *                |            |                        |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *     <-----<    ^            ^  <-----<               |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *START>----->HANDSHAKE>----->DATA>----->RENEGOTIATE    |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *                v            v               v        |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *                |            |               |        |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *                +------------+---------------+        |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *                |                                     |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *                v                                     |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *               ERROR>------>----->CLOSED<--------<----+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * ALSO, note that the the purpose of handshaking (renegotiation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * included) is to assign a different, and perhaps new, session to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * the connection.  The SSLv3 spec is a bit confusing on that new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * protocol feature.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    private int                 connectionState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    private static final int    cs_START = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    private static final int    cs_HANDSHAKE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    private static final int    cs_DATA = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    private static final int    cs_RENEGOTIATE = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    private static final int    cs_ERROR = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    private static final int    cs_CLOSED = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Once we're in state cs_CLOSED, we can continue to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * wrap/unwrap until we finish sending/receiving the messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * for close_notify.  EngineWriter handles outboundDone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    private boolean             inboundDone = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    EngineWriter                writer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * The authentication context holds all information used to establish
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * who this end of the connection is (certificate chains, private keys,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * etc) and who is trusted (e.g. as CAs or websites).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    private SSLContextImpl      sslContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * This connection is one of (potentially) many associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * any given session.  The output of the handshake protocol is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * new session ... although all the protocol description talks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * about changing the cipher spec (and it does change), in fact
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * that's incidental since it's done by changing everything that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * is associated with a session at the same time.  (TLS/IETF may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * change that to add client authentication w/o new key exchg.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     */
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   200
    private Handshaker                  handshaker;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   201
    private SSLSessionImpl              sess;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   202
    private volatile SSLSessionImpl     handshakeSession;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   203
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * Client authentication be off, requested, or required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * This will be used by both this class and SSLSocket's variants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    static final byte           clauth_none = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    static final byte           clauth_requested = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    static final byte           clauth_required = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * Flag indicating if the next record we receive MUST be a Finished
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * message. Temporarily set during the handshake to ensure that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * a change cipher spec message is followed by a finished message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    private boolean             expectingFinished;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * If someone tries to closeInbound() (say at End-Of-Stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * our engine having received a close_notify, we need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * notify the app that we may have a truncation attack underway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    private boolean             recvCN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * For improved diagnostics, we detail connection closure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * If the engine is closed (connectionState >= cs_ERROR),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * closeReason != null indicates if the engine was closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * because of an error or because or normal shutdown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    private SSLException        closeReason;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * Per-connection private state that doesn't change when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * session is changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    private byte                        doClientAuth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    private boolean                     enableSessionCreation = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    EngineInputRecord                   inputRecord;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    EngineOutputRecord                  outputRecord;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    private AccessControlContext        acc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   247
    // The cipher suites enabled for use on this connection.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   248
    private CipherSuiteList             enabledCipherSuites;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   249
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   250
    // the endpoint identification protocol
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   251
    private String                      identificationProtocol = null;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   252
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   253
    // The cryptographic algorithm constraints
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   254
    private AlgorithmConstraints        algorithmConstraints = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    // Have we been told whether we're client or server?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    private boolean                     serverModeSet = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    private boolean                     roleIsServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    /*
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   261
     * The protocol versions enabled for use on this connection.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   262
     *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   263
     * Note: we support a pseudo protocol called SSLv2Hello which when
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   264
     * set will result in an SSL v2 Hello being sent with SSL (version 3.0)
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   265
     * or TLS (version 3.1, 3.2, etc.) version info.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    private ProtocolList        enabledProtocols;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * The SSL version associated with this connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    private ProtocolVersion     protocolVersion = ProtocolVersion.DEFAULT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * Crypto state that's reinitialized when the session changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    private MAC                 readMAC, writeMAC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    private CipherBox           readCipher, writeCipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    // NOTE: compression state would be saved here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   281
    /*
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   282
     * security parameters for secure renegotiation.
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   283
     */
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   284
    private boolean             secureRenegotiation;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   285
    private byte[]              clientVerifyData;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   286
    private byte[]              serverVerifyData;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * READ ME * READ ME * READ ME * READ ME * READ ME * READ ME *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * IMPORTANT STUFF TO UNDERSTANDING THE SYNCHRONIZATION ISSUES.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * READ ME * READ ME * READ ME * READ ME * READ ME * READ ME *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * There are several locks here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * The primary lock is the per-instance lock used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * synchronized(this) and the synchronized methods.  It controls all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * access to things such as the connection state and variables which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * affect handshaking.  If we are inside a synchronized method, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * can access the state directly, otherwise, we must use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * synchronized equivalents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * Note that we must never acquire the <code>this</code> lock after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * <code>writeLock</code> or run the risk of deadlock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * Grab some coffee, and be careful with any code changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    private Object              wrapLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    private Object              unwrapLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    Object                      writeLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    /*
10915
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9246
diff changeset
   312
     * Is it the first application record to write?
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9246
diff changeset
   313
     */
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9246
diff changeset
   314
    private boolean isFirstAppOutputRecord = true;
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9246
diff changeset
   315
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9246
diff changeset
   316
    /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * Class and subclass dynamic debugging support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    private static final Debug debug = Debug.getInstance("ssl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    // Initialization/Constructors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * Constructor for an SSLEngine from SSLContext, without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * host/port hints.  This Engine will not be able to cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * sessions, but must renegotiate everything by hand.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    SSLEngineImpl(SSLContextImpl ctx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        init(ctx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * Constructor for an SSLEngine from SSLContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    SSLEngineImpl(SSLContextImpl ctx, String host, int port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        super(host, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        init(ctx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * Initializes the Engine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    private void init(SSLContextImpl ctx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        if (debug != null && Debug.isOn("ssl")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            System.out.println("Using SSLEngineImpl.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        sslContext = ctx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        sess = SSLSessionImpl.nullSession;
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
   353
        handshakeSession = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
         * State is cs_START until we initialize the handshaker.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
         * Apps using SSLEngine are probably going to be server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
         * Somewhat arbitrary choice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        roleIsServer = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        connectionState = cs_START;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
         * default read and write side cipher and MAC support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
         * Note:  compression support would go here too
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        readCipher = CipherBox.NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        readMAC = MAC.NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        writeCipher = CipherBox.NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        writeMAC = MAC.NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   374
        // default security parameters for secure renegotiation
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   375
        secureRenegotiation = false;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   376
        clientVerifyData = new byte[0];
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   377
        serverVerifyData = new byte[0];
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   378
9246
c459f79af46b 6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents: 7043
diff changeset
   379
        enabledCipherSuites =
c459f79af46b 6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents: 7043
diff changeset
   380
                sslContext.getDefaultCipherSuiteList(roleIsServer);
c459f79af46b 6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents: 7043
diff changeset
   381
        enabledProtocols =
c459f79af46b 6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents: 7043
diff changeset
   382
                sslContext.getDefaultProtocolList(roleIsServer);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        wrapLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        unwrapLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        writeLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
         * Save the Access Control Context.  This will be used later
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
         * for a couple of things, including providing a context to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
         * run tasks in, and for determining which credentials
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
         * to use for Subject based (JAAS) decisions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        acc = AccessController.getContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
         * All outbound application data goes through this OutputRecord,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
         * other data goes through their respective records created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
         * elsewhere.  All inbound data goes through this one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
         * input record.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        outputRecord =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            new EngineOutputRecord(Record.ct_application_data, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        inputRecord = new EngineInputRecord(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        inputRecord.enableFormatChecks();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        writer = new EngineWriter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * Initialize the handshaker object. This means:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *  . if a handshake is already in progress (state is cs_HANDSHAKE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *    or cs_RENEGOTIATE), do nothing and return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *  . if the engine is already closed, throw an Exception (internal error)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *  . otherwise (cs_START or cs_DATA), create the appropriate handshaker
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   419
     *    object and advance the connection state (to cs_HANDSHAKE or
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   420
     *    cs_RENEGOTIATE, respectively).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * This method is called right after a new engine is created, when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * starting renegotiation, or when changing client/server mode of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * engine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    private void initHandshaker() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        switch (connectionState) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        // Starting a new handshake.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        case cs_START:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        case cs_DATA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        // We're already in the middle of a handshake.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        case cs_HANDSHAKE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        case cs_RENEGOTIATE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        // Anyone allowed to call this routine is required to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        // do so ONLY if the connection state is reasonable...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            throw new IllegalStateException("Internal error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        // state is either cs_START or cs_DATA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        if (connectionState == cs_START) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            connectionState = cs_HANDSHAKE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        } else { // cs_DATA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            connectionState = cs_RENEGOTIATE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        if (roleIsServer) {
5182
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 1763
diff changeset
   458
            handshaker = new ServerHandshaker(this, sslContext,
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   459
                    enabledProtocols, doClientAuth,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   460
                    protocolVersion, connectionState == cs_HANDSHAKE,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   461
                    secureRenegotiation, clientVerifyData, serverVerifyData);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        } else {
5182
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 1763
diff changeset
   463
            handshaker = new ClientHandshaker(this, sslContext,
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   464
                    enabledProtocols,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   465
                    protocolVersion, connectionState == cs_HANDSHAKE,
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   466
                    secureRenegotiation, clientVerifyData, serverVerifyData);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        }
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   468
        handshaker.setEnabledCipherSuites(enabledCipherSuites);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        handshaker.setEnableSessionCreation(enableSessionCreation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * Report the current status of the Handshaker
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    private HandshakeStatus getHSStatus(HandshakeStatus hss) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        if (hss != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            return hss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            if (writer.hasOutboundData()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                return HandshakeStatus.NEED_WRAP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            } else if (handshaker != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                if (handshaker.taskOutstanding()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                    return HandshakeStatus.NEED_TASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                    return HandshakeStatus.NEED_UNWRAP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            } else if (connectionState == cs_CLOSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                 * Special case where we're closing, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                 * still need the close_notify before we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                 * can officially be closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                 * Note isOutboundDone is taken care of by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                 * hasOutboundData() above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                if (!isInboundDone()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                    return HandshakeStatus.NEED_UNWRAP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                } // else not handshaking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            return HandshakeStatus.NOT_HANDSHAKING;
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
    synchronized private void checkTaskThrown() throws SSLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        if (handshaker != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            handshaker.checkThrown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    // Handshaking and connection state code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * Provides "this" synchronization for connection state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * Otherwise, you can access it directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    synchronized private int getConnectionState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        return connectionState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    synchronized private void setConnectionState(int state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        connectionState = state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * Get the Access Control Context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * Used for a known context to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * run tasks in, and for determining which credentials
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * to use for Subject-based (JAAS) decisions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    AccessControlContext getAcc() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        return acc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * Is a handshake currently underway?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    public SSLEngineResult.HandshakeStatus getHandshakeStatus() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        return getHSStatus(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * When a connection finishes handshaking by enabling use of a newly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * negotiated session, each end learns about it in two halves (read,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * and write).  When both read and write ciphers have changed, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * last handshake message has been read, the connection has joined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * (rejoined) the new session.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * NOTE:  The SSLv3 spec is rather unclear on the concepts here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * Sessions don't change once they're established (including cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * suite and master secret) but connections can join them (and leave
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * them).  They're created by handshaking, though sometime handshaking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * causes connections to join up with pre-established sessions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * Synchronized on "this" from readRecord.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    private void changeReadCiphers() throws SSLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        if (connectionState != cs_HANDSHAKE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                && connectionState != cs_RENEGOTIATE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            throw new SSLProtocolException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                "State error, change cipher specs");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        // ... create decompressor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
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: 2
diff changeset
   572
        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: 2
diff changeset
   573
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            readCipher = handshaker.newReadCipher();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            readMAC = handshaker.newReadMAC();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            // "can't happen"
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9246
diff changeset
   579
            throw new SSLException("Algorithm missing:  ", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        }
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: 2
diff changeset
   581
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 2
diff changeset
   582
        /*
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 2
diff changeset
   583
         * 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: 2
diff changeset
   584
         * 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: 2
diff changeset
   585
         * 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: 2
diff changeset
   586
         *
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 2
diff changeset
   587
         * 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: 2
diff changeset
   588
         * 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: 2
diff changeset
   589
         */
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 2
diff changeset
   590
        oldCipher.dispose();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * used by Handshaker to change the active write cipher, follows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * the output of the CCS message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * Also synchronized on "this" from readRecord/delegatedTask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    void changeWriteCiphers() throws SSLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        if (connectionState != cs_HANDSHAKE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                && connectionState != cs_RENEGOTIATE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            throw new SSLProtocolException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                "State error, change cipher specs");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        // ... create compressor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
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: 2
diff changeset
   608
        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: 2
diff changeset
   609
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            writeCipher = handshaker.newWriteCipher();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            writeMAC = handshaker.newWriteMAC();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            // "can't happen"
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9246
diff changeset
   615
            throw new SSLException("Algorithm missing:  ", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        }
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: 2
diff changeset
   617
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 2
diff changeset
   618
        // 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: 2
diff changeset
   619
        oldCipher.dispose();
10915
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9246
diff changeset
   620
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9246
diff changeset
   621
        // reset the flag of the first application record
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9246
diff changeset
   622
        isFirstAppOutputRecord = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * Updates the SSL version associated with this connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * Called from Handshaker once it has determined the negotiated version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    synchronized void setVersion(ProtocolVersion protocolVersion) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        this.protocolVersion = protocolVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        outputRecord.setVersion(protocolVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * Kickstart the handshake if it is not already in progress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * This means:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     *  . if handshaking is already underway, do nothing and return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     *  . if the engine is not connected or already closed, throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     *    Exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     *  . otherwise, call initHandshake() to initialize the handshaker
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     *    object and progress the state. Then, send the initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     *    handshaking message if appropriate (always on clients and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     *    on servers when renegotiating).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    private synchronized void kickstartHandshake() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        switch (connectionState) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        case cs_START:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            if (!serverModeSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                throw new IllegalStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                    "Client/Server mode not yet set.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            initHandshaker();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        case cs_HANDSHAKE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            // handshaker already setup, proceed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        case cs_DATA:
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   665
            if (!secureRenegotiation && !Handshaker.allowUnsafeRenegotiation) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   666
                throw new SSLHandshakeException(
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   667
                        "Insecure renegotiation is not allowed");
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   668
            }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   669
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   670
            if (!secureRenegotiation) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   671
                if (debug != null && Debug.isOn("handshake")) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   672
                    System.out.println(
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   673
                        "Warning: Using insecure renegotiation");
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   674
                }
5182
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 1763
diff changeset
   675
            }
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 1763
diff changeset
   676
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            // initialize the handshaker, move to cs_RENEGOTIATE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            initHandshaker();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        case cs_RENEGOTIATE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            // handshaking already in progress, return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            // cs_ERROR/cs_CLOSED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            throw new SSLException("SSLEngine is closing/closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        // Kickstart handshake state machine if we need to ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        // Note that handshaker.kickstart() writes the message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        // to its HandshakeOutStream, which calls back into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        // SSLSocketImpl.writeRecord() to send it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        //
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   697
        if (!handshaker.activated()) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   698
             // prior to handshaking, activate the handshake
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   699
            if (connectionState == cs_RENEGOTIATE) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   700
                // don't use SSLv2Hello when renegotiating
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   701
                handshaker.activate(protocolVersion);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   702
            } else {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   703
                handshaker.activate(null);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   704
            }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   705
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            if (handshaker instanceof ClientHandshaker) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                // send client hello
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                handshaker.kickstart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            } else {    // instanceof ServerHandshaker
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                if (connectionState == cs_HANDSHAKE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                    // initial handshake, no kickstart message to send
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                    // we want to renegotiate, send hello request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                    handshaker.kickstart();
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
   715
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                    // hello request is not included in the handshake
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                    // hashes, reset them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                    handshaker.handshakeHash.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * Start a SSLEngine handshake
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    public void beginHandshake() throws SSLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            kickstartHandshake();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            fatal(Alerts.alert_handshake_failure,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                "Couldn't kickstart handshaking", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    // Read/unwrap side
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * Unwraps a buffer.  Does a variety of checks before grabbing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * the unwrapLock, which blocks multiple unwraps from occuring.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    public SSLEngineResult unwrap(ByteBuffer netData, ByteBuffer [] appData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            int offset, int length) throws SSLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        EngineArgs ea = new EngineArgs(netData, appData, offset, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            synchronized (unwrapLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                return readNetRecord(ea);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
             * Don't reset position so it looks like we didn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
             * consume anything.  We did consume something, and it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
             * got us into this situation, so report that much back.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
             * Our days of consuming are now over anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            fatal(Alerts.alert_internal_error,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                "problem unwrapping net record", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            return null;  // make compiler happy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
             * Just in case something failed to reset limits properly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            ea.resetLim();
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
     * Makes additional checks for unwrap, but this time more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * specific to this packet and the current state of the machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    private SSLEngineResult readNetRecord(EngineArgs ea) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        Status status = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        HandshakeStatus hsStatus = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
         * See if the handshaker needs to report back some SSLException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        checkTaskThrown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
         * Check if we are closing/closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        if (isInboundDone()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            return new SSLEngineResult(Status.CLOSED, getHSStatus(null), 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
         * If we're still in cs_HANDSHAKE, make sure it's been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
         * started.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            if ((connectionState == cs_HANDSHAKE) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                    (connectionState == cs_START)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                kickstartHandshake();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                 * If there's still outbound data to flush, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                 * can return without trying to unwrap anything.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                hsStatus = getHSStatus(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                if (hsStatus == HandshakeStatus.NEED_WRAP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                    return new SSLEngineResult(Status.OK, hsStatus, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
         * Grab a copy of this if it doesn't already exist,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
         * and we can use it several places before anything major
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
         * happens on this side.  Races aren't critical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
         * here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        if (hsStatus == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            hsStatus = getHSStatus(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
         * If we have a task outstanding, this *MUST* be done before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
         * doing any more unwrapping, because we could be in the middle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
         * of receiving a handshake message, for example, a finished
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
         * message which would change the ciphers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        if (hsStatus == HandshakeStatus.NEED_TASK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            return new SSLEngineResult(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                Status.OK, hsStatus, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
         * Check the packet to make sure enough is here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
         * This will also indirectly check for 0 len packets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        int packetLen = inputRecord.bytesInCompletePacket(ea.netData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        // Is this packet bigger than SSL/TLS normally allows?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        if (packetLen > sess.getPacketBufferSize()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            if (packetLen > Record.maxLargeRecordSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                throw new SSLProtocolException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                    "Input SSL/TLS record too big: max = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                    Record.maxLargeRecordSize +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                    " len = " + packetLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                // Expand the expected maximum packet/application buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                // sizes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                sess.expandBufferSizes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
         * Check for OVERFLOW.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
         * To be considered: We could delay enforcing the application buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
         * free space requirement until after the initial handshaking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        if ((packetLen - Record.headerSize) > ea.getAppRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            return new SSLEngineResult(Status.BUFFER_OVERFLOW, hsStatus, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        // check for UNDERFLOW.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        if ((packetLen == -1) || (ea.netData.remaining() < packetLen)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            return new SSLEngineResult(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                Status.BUFFER_UNDERFLOW, hsStatus, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
         * We're now ready to actually do the read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
         * The only result code we really need to be exactly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
         * right is the HS finished, for signaling to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
         * HandshakeCompletedListeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            hsStatus = readRecord(ea);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        } catch (SSLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        } catch (IOException e) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9246
diff changeset
   883
            throw new SSLException("readRecord", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
         * Check the various condition that we could be reporting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
         * It's *possible* something might have happened between the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
         * above and now, but it was better to minimally lock "this"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
         * during the read process.  We'll return the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
         * status, which is more representative of the current state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
         * status above should cover:  FINISHED, NEED_TASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        status = (isInboundDone() ? Status.CLOSED : Status.OK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        hsStatus = getHSStatus(hsStatus);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        return new SSLEngineResult(status, hsStatus,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            ea.deltaNet(), ea.deltaApp());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * Actually do the read record processing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * Returns a Status if it can make specific determinations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * of the engine state.  In particular, we need to signal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * that a handshake just completed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * It would be nice to be symmetrical with the write side and move
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * the majority of this to EngineInputRecord, but there's too much
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     * SSLEngine state to do that cleanly.  It must still live here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    private HandshakeStatus readRecord(EngineArgs ea) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        HandshakeStatus hsStatus = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
         * The various operations will return new sliced BB's,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
         * this will avoid having to worry about positions and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
         * limits in the netBB.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        ByteBuffer readBB = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        ByteBuffer decryptedBB = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        if (getConnectionState() != cs_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
             * Read a record ... maybe emitting an alert if we get a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
             * comprehensible but unsupported "hello" message during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
             * format checking (e.g. V2).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                readBB = inputRecord.read(ea.netData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                fatal(Alerts.alert_unexpected_message, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
             * The basic SSLv3 record protection involves (optional)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
             * encryption for privacy, and an integrity check ensuring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
             * data origin authentication.  We do them both here, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
             * throw a fatal alert if the integrity check fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                decryptedBB = inputRecord.decrypt(readCipher, readBB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            } catch (BadPaddingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                // RFC 2246 states that decryption_failed should be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                // for this purpose. However, that allows certain attacks,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                // so we just send bad record MAC. We also need to make
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                // sure to always check the MAC to avoid a timing attack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                // for the same issue. See paper by Vaudenay et al.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                // rewind the BB if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                readBB.rewind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                inputRecord.checkMAC(readMAC, readBB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                // use the same alert types as for MAC failure below
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                byte alertType = (inputRecord.contentType() ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                    Record.ct_handshake) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                        Alerts.alert_handshake_failure :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                        Alerts.alert_bad_record_mac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                fatal(alertType, "Invalid padding", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            if (!inputRecord.checkMAC(readMAC, decryptedBB)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                if (inputRecord.contentType() == Record.ct_handshake) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                    fatal(Alerts.alert_handshake_failure,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                        "bad handshake record MAC");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                    fatal(Alerts.alert_bad_record_mac, "bad record MAC");
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
            // if (!inputRecord.decompress(c))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            //     fatal(Alerts.alert_decompression_failure,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            //     "decompression failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
             * Process the record.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                switch (inputRecord.contentType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                case Record.ct_handshake:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                     * Handshake messages always go to a pending session
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                     * handshaker ... if there isn't one, create one.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                     * must work asynchronously, for renegotiation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                     * NOTE that handshaking will either resume a session
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                     * which was in the cache (and which might have other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                     * connections in it already), or else will start a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                     * session (new keys exchanged) with just this connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                     * in it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                    initHandshaker();
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1000
                    if (!handshaker.activated()) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1001
                        // prior to handshaking, activate the handshake
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1002
                        if (connectionState == cs_RENEGOTIATE) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1003
                            // don't use SSLv2Hello when renegotiating
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1004
                            handshaker.activate(protocolVersion);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1005
                        } else {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1006
                            handshaker.activate(null);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1007
                        }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1008
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                     * process the handshake record ... may contain just
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                     * a partial handshake message or multiple messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                     * The handshaker state machine will ensure that it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                     * a finished message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                    handshaker.process_record(inputRecord, expectingFinished);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                    expectingFinished = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
5182
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 1763
diff changeset
  1020
                    if (handshaker.invalidated) {
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 1763
diff changeset
  1021
                        handshaker = null;
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 1763
diff changeset
  1022
                        // if state is cs_RENEGOTIATE, revert it to cs_DATA
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 1763
diff changeset
  1023
                        if (connectionState == cs_RENEGOTIATE) {
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 1763
diff changeset
  1024
                            connectionState = cs_DATA;
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 1763
diff changeset
  1025
                        }
62836694baeb 6898739: TLS renegotiation issue
xuelei
parents: 1763
diff changeset
  1026
                    } else if (handshaker.isDone()) {
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1027
                        // reset the parameters for secure renegotiation.
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1028
                        secureRenegotiation =
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1029
                                        handshaker.isSecureRenegotiation();
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1030
                        clientVerifyData = handshaker.getClientVerifyData();
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1031
                        serverVerifyData = handshaker.getServerVerifyData();
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
  1032
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                        sess = handshaker.getSession();
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1034
                        handshakeSession = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                        if (!writer.hasOutboundData()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                            hsStatus = HandshakeStatus.FINISHED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                        handshaker = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                        connectionState = cs_DATA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                        // No handshakeListeners here.  That's a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                        // SSLSocket thing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                    } else if (handshaker.taskOutstanding()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                        hsStatus = HandshakeStatus.NEED_TASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                case Record.ct_application_data:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
                    // Pass this right back up to the application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                    if ((connectionState != cs_DATA)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                            && (connectionState != cs_RENEGOTIATE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                            && (connectionState != cs_CLOSED)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                        throw new SSLProtocolException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                            "Data received in non-data state: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                            connectionState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                    if (expectingFinished) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                        throw new SSLProtocolException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                                ("Expecting finished message, received data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                     * Don't return data once the inbound side is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                     * closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                    if (!inboundDone) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                        ea.scatter(decryptedBB.slice());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                case Record.ct_alert:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                    recvAlert();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                case Record.ct_change_cipher_spec:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                    if ((connectionState != cs_HANDSHAKE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                                && connectionState != cs_RENEGOTIATE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                            || inputRecord.available() != 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                            || inputRecord.read() != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
                        fatal(Alerts.alert_unexpected_message,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
                            "illegal change cipher spec msg, state = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                            + connectionState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                    // The first message after a change_cipher_spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                    // record MUST be a "Finished" handshake record,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
                    // else it's a protocol violation.  We force this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                    // to be checked by a minor tweak to the state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                    // machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
                    changeReadCiphers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                    // next message MUST be a finished message
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                    expectingFinished = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                    // TLS requires that unrecognized records be ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                    if (debug != null && Debug.isOn("ssl")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                        System.out.println(threadName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                            ", Received record type: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                            + inputRecord.contentType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                } // switch
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1109
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1110
                /*
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1111
                 * We only need to check the sequence number state for
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1112
                 * non-handshaking record.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1113
                 *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1114
                 * Note that in order to maintain the handshake status
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1115
                 * properly, we check the sequence number after the last
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1116
                 * record reading process. As we request renegotiation
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1117
                 * or close the connection for wrapped sequence number
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1118
                 * when there is enough sequence number space left to
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1119
                 * handle a few more records, so the sequence number
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1120
                 * of the last record cannot be wrapped.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1121
                 */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1122
                if (connectionState < cs_ERROR && !isInboundDone() &&
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1123
                        (hsStatus == HandshakeStatus.NOT_HANDSHAKING)) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1124
                    if (checkSequenceNumber(readMAC,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1125
                            inputRecord.contentType())) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1126
                        hsStatus = getHSStatus(null);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1127
                    }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1128
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            } // synchronized (this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        return hsStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
    // write/wrap side
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     * Wraps a buffer.  Does a variety of checks before grabbing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * the wrapLock, which blocks multiple wraps from occuring.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
    public SSLEngineResult wrap(ByteBuffer [] appData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
            int offset, int length, ByteBuffer netData) throws SSLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        EngineArgs ea = new EngineArgs(appData, offset, length, netData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
         * We can be smarter about using smaller buffer sizes later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
         * For now, force it to be large enough to handle any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
         * valid SSL/TLS record.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
         */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9246
diff changeset
  1155
        if (netData.remaining() < EngineOutputRecord.maxRecordSize) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
            return new SSLEngineResult(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
                Status.BUFFER_OVERFLOW, getHSStatus(null), 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
            synchronized (wrapLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                return writeAppRecord(ea);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
            ea.resetPos();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
            fatal(Alerts.alert_internal_error,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
                "problem unwrapping net record", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
            return null;  // make compiler happy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
             * Just in case something didn't reset limits properly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
            ea.resetLim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     * Makes additional checks for unwrap, but this time more
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     * specific to this packet and the current state of the machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
    private SSLEngineResult writeAppRecord(EngineArgs ea) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        Status status = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        HandshakeStatus hsStatus = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
         * See if the handshaker needs to report back some SSLException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        checkTaskThrown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
         * short circuit if we're closed/closing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        if (writer.isOutboundDone()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
            return new SSLEngineResult(Status.CLOSED, getHSStatus(null), 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
         * If we're still in cs_HANDSHAKE, make sure it's been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
         * started.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
            if ((connectionState == cs_HANDSHAKE) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                    (connectionState == cs_START)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
                kickstartHandshake();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                 * If there's no HS data available to write, we can return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                 * without trying to wrap anything.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                hsStatus = getHSStatus(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                if (hsStatus == HandshakeStatus.NEED_UNWRAP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                    return new SSLEngineResult(Status.OK, hsStatus, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
         * Grab a copy of this if it doesn't already exist,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
         * and we can use it several places before anything major
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
         * happens on this side.  Races aren't critical
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
         * here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        if (hsStatus == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            hsStatus = getHSStatus(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
         * If we have a task outstanding, this *MUST* be done before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
         * doing any more wrapping, because we could be in the middle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
         * of receiving a handshake message, for example, a finished
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
         * message which would change the ciphers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        if (hsStatus == HandshakeStatus.NEED_TASK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
            return new SSLEngineResult(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
                Status.OK, hsStatus, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
         * This will obtain any waiting outbound data, or will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
         * process the outbound appData.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
            synchronized (writeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
                hsStatus = writeRecord(outputRecord, ea);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        } catch (SSLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
        } catch (IOException e) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9246
diff changeset
  1252
            throw new SSLException("Write problems", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
         * writeRecord might have reported some status.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
         * Now check for the remaining cases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
         * status above should cover:  NEED_WRAP/FINISHED
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
        status = (isOutboundDone() ? Status.CLOSED : Status.OK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
        hsStatus = getHSStatus(hsStatus);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        return new SSLEngineResult(status, hsStatus,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
            ea.deltaApp(), ea.deltaNet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     * Central point to write/get all of the outgoing data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
    private HandshakeStatus writeRecord(EngineOutputRecord eor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
            EngineArgs ea) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        // eventually compress as well.
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1275
        HandshakeStatus hsStatus =
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1276
                writer.writeRecord(eor, ea, writeMAC, writeCipher);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1277
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1278
        /*
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1279
         * We only need to check the sequence number state for
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1280
         * non-handshaking record.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1281
         *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1282
         * Note that in order to maintain the handshake status
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1283
         * properly, we check the sequence number after the last
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1284
         * record writing process. As we request renegotiation
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1285
         * or close the connection for wrapped sequence number
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1286
         * when there is enough sequence number space left to
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1287
         * handle a few more records, so the sequence number
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1288
         * of the last record cannot be wrapped.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1289
         */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1290
        if (connectionState < cs_ERROR && !isOutboundDone() &&
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1291
                (hsStatus == HandshakeStatus.NOT_HANDSHAKING)) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1292
            if (checkSequenceNumber(writeMAC, eor.contentType())) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1293
                hsStatus = getHSStatus(null);
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1294
            }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1295
        }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1296
10915
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9246
diff changeset
  1297
        /*
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9246
diff changeset
  1298
         * turn off the flag of the first application record if we really
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9246
diff changeset
  1299
         * consumed at least byte.
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9246
diff changeset
  1300
         */
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9246
diff changeset
  1301
        if (isFirstAppOutputRecord && ea.deltaApp() > 0) {
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9246
diff changeset
  1302
            isFirstAppOutputRecord = false;
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9246
diff changeset
  1303
        }
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9246
diff changeset
  1304
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1305
        return hsStatus;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
    /*
10915
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9246
diff changeset
  1309
     * Need to split the payload except the following cases:
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9246
diff changeset
  1310
     *
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9246
diff changeset
  1311
     * 1. protocol version is TLS 1.1 or later;
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9246
diff changeset
  1312
     * 2. bulk cipher does not use CBC mode, including null bulk cipher suites.
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9246
diff changeset
  1313
     * 3. the payload is the first application record of a freshly
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9246
diff changeset
  1314
     *    negotiated TLS session.
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9246
diff changeset
  1315
     * 4. the CBC protection is disabled;
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9246
diff changeset
  1316
     *
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9246
diff changeset
  1317
     * More details, please refer to
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9246
diff changeset
  1318
     * EngineOutputRecord.write(EngineArgs, MAC, CipherBox).
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9246
diff changeset
  1319
     */
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9246
diff changeset
  1320
    boolean needToSplitPayload(CipherBox cipher, ProtocolVersion protocol) {
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9246
diff changeset
  1321
        return (protocol.v <= ProtocolVersion.TLS10.v) &&
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9246
diff changeset
  1322
                cipher.isCBCMode() && !isFirstAppOutputRecord &&
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9246
diff changeset
  1323
                Record.enableCBCProtection;
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9246
diff changeset
  1324
    }
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9246
diff changeset
  1325
1e20964cebf3 7064341: jsse/runtime security problem
xuelei
parents: 9246
diff changeset
  1326
    /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     * Non-application OutputRecords go through here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
    void writeRecord(EngineOutputRecord eor) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
        // eventually compress as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
        writer.writeRecord(eor, writeMAC, writeCipher);
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1332
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1333
        /*
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1334
         * Check the sequence number state
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1335
         *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1336
         * Note that in order to maintain the connection I/O
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1337
         * properly, we check the sequence number after the last
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1338
         * record writing process. As we request renegotiation
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1339
         * or close the connection for wrapped sequence number
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1340
         * when there is enough sequence number space left to
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1341
         * handle a few more records, so the sequence number
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1342
         * of the last record cannot be wrapped.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1343
         */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1344
        if ((connectionState < cs_ERROR) && !isOutboundDone()) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1345
            checkSequenceNumber(writeMAC, eor.contentType());
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1346
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
    // Close code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
    /**
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1354
     * Check the sequence number state
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1355
     *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1356
     * RFC 4346 states that, "Sequence numbers are of type uint64 and
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1357
     * may not exceed 2^64-1.  Sequence numbers do not wrap. If a TLS
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1358
     * implementation would need to wrap a sequence number, it must
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1359
     * renegotiate instead."
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1360
     *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1361
     * Return true if the handshake status may be changed.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1362
     */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1363
    private boolean checkSequenceNumber(MAC mac, byte type)
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1364
            throws IOException {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1365
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1366
        /*
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1367
         * Don't bother to check the sequence number for error or
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1368
         * closed connections, or NULL MAC
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1369
         */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1370
        if (connectionState >= cs_ERROR || mac == MAC.NULL) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1371
            return false;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1372
        }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1373
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1374
        /*
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1375
         * Conservatively, close the connection immediately when the
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1376
         * sequence number is close to overflow
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1377
         */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1378
        if (mac.seqNumOverflow()) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1379
            /*
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1380
             * TLS protocols do not define a error alert for sequence
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1381
             * number overflow. We use handshake_failure error alert
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1382
             * for handshaking and bad_record_mac for other records.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1383
             */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1384
            if (debug != null && Debug.isOn("ssl")) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1385
                System.out.println(threadName() +
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1386
                    ", sequence number extremely close to overflow " +
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1387
                    "(2^64-1 packets). Closing connection.");
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1388
            }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1389
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1390
            fatal(Alerts.alert_handshake_failure, "sequence number overflow");
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1391
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1392
            return true; // make the compiler happy
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1393
        }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1394
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1395
        /*
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1396
         * Ask for renegotiation when need to renew sequence number.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1397
         *
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1398
         * Don't bother to kickstart the renegotiation when the local is
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1399
         * asking for it.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1400
         */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1401
        if ((type != Record.ct_handshake) && mac.seqNumIsHuge()) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1402
            if (debug != null && Debug.isOn("ssl")) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1403
                System.out.println(threadName() + ", request renegotiation " +
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1404
                        "to avoid sequence number overflow");
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1405
            }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1406
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1407
            beginHandshake();
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1408
            return true;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1409
        }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1410
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1411
        return false;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1412
    }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1413
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1414
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
     * Signals that no more outbound application data will be sent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
     * on this <code>SSLEngine</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
    private void closeOutboundInternal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
        if ((debug != null) && Debug.isOn("ssl")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
            System.out.println(threadName() + ", closeOutboundInternal()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
         * Already closed, ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
        if (writer.isOutboundDone()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
        switch (connectionState) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
         * If we haven't even started yet, don't bother reading inbound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
        case cs_START:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
            writer.closeOutbound();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
            inboundDone = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
        case cs_ERROR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        case cs_CLOSED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
         * Otherwise we indicate clean termination.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
        // case cs_HANDSHAKE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
        // case cs_DATA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
        // case cs_RENEGOTIATE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
            warning(Alerts.alert_close_notify);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
            writer.closeOutbound();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
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: 2
diff changeset
  1457
        // 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: 2
diff changeset
  1458
        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: 2
diff changeset
  1459
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
        connectionState = cs_CLOSED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
    synchronized public void closeOutbound() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
         * Dump out a close_notify to the remote side
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
        if ((debug != null) && Debug.isOn("ssl")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
            System.out.println(threadName() + ", called closeOutbound()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
        closeOutboundInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
     * Returns the outbound application data closure state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
    public boolean isOutboundDone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
        return writer.isOutboundDone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
     * Signals that no more inbound network data will be sent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
     * to this <code>SSLEngine</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
    private void closeInboundInternal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
        if ((debug != null) && Debug.isOn("ssl")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
            System.out.println(threadName() + ", closeInboundInternal()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
         * Already closed, ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        if (inboundDone) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
        closeOutboundInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
        inboundDone = true;
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: 2
diff changeset
  1500
0a6b65d56746 6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents: 2
diff changeset
  1501
        // 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: 2
diff changeset
  1502
        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: 2
diff changeset
  1503
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
        connectionState = cs_CLOSED;
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
     * Close the inbound side of the connection.  We grab the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
     * lock here, and do the real work in the internal verison.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
     * We do check for truncation attacks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
    synchronized public void closeInbound() throws SSLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
         * Currently closes the outbound side as well.  The IETF TLS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
         * working group has expressed the opinion that 1/2 open
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
         * connections are not allowed by the spec.  May change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
         * someday in the future.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
        if ((debug != null) && Debug.isOn("ssl")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
            System.out.println(threadName() + ", called closeInbound()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
         * No need to throw an Exception if we haven't even started yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
        if ((connectionState != cs_START) && !recvCN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
            recvCN = true;  // Only receive the Exception once
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
            fatal(Alerts.alert_internal_error,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
                "Inbound closed before receiving peer's close_notify: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
                "possible truncation attack?");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
             * Currently, this is a no-op, but in case we change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
             * the close inbound code later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
            closeInboundInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
     * Returns the network inbound data closure state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
    synchronized public boolean isInboundDone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
        return inboundDone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
    // Misc stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
     * Returns the current <code>SSLSession</code> for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
     * <code>SSLEngine</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
     * These can be long lived, and frequently correspond to an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
     * entire login session for some user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
    synchronized public SSLSession getSession() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
        return sess;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1564
    @Override
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1565
    synchronized public SSLSession getHandshakeSession() {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1566
        return handshakeSession;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1567
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1568
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1569
    synchronized void setHandshakeSession(SSLSessionImpl session) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1570
        handshakeSession = session;
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1571
    }
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1572
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
     * Returns a delegated <code>Runnable</code> task for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
     * this <code>SSLEngine</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
    synchronized public Runnable getDelegatedTask() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
        if (handshaker != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
            return handshaker.getTask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
    // EXCEPTION AND ALERT HANDLING
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
     * Send a warning alert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
    void warning(byte description) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
        sendAlert(Alerts.alert_warning, description);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
    synchronized void fatal(byte description, String diagnostic)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
            throws SSLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
        fatal(description, diagnostic, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
    synchronized void fatal(byte description, Throwable cause)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
            throws SSLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
        fatal(description, null, cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
     * We've got a fatal error here, so start the shutdown process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
     * Because of the way the code was written, we have some code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
     * calling fatal directly when the "description" is known
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
     * and some throwing Exceptions which are then caught by higher
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
     * levels which then call here.  This code needs to determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
     * if one of the lower levels has already started the process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
     * We won't worry about Error's, if we have one of those,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
     * we're in worse trouble.  Note:  the networking code doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
     * deal with Errors either.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
    synchronized void fatal(byte description, String diagnostic,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
            Throwable cause) throws SSLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
         * If we have no further information, make a general-purpose
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
         * message for folks to see.  We generally have one or the other.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
        if (diagnostic == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
            diagnostic = "General SSLEngine problem";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
        if (cause == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
            cause = Alerts.getSSLException(description, cause, diagnostic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
         * If we've already shutdown because of an error,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
         * there is nothing we can do except rethrow the exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
         * Most exceptions seen here will be SSLExceptions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
         * We may find the occasional Exception which hasn't been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
         * converted to a SSLException, so we'll do it here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
        if (closeReason != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
            if ((debug != null) && Debug.isOn("ssl")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
                System.out.println(threadName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
                    ", fatal: engine already closed.  Rethrowing " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
                    cause.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
            if (cause instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
                throw (RuntimeException)cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
            } else if (cause instanceof SSLException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
                throw (SSLException)cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
            } else if (cause instanceof Exception) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9246
diff changeset
  1652
                throw new SSLException("fatal SSLEngine condition", cause);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
        if ((debug != null) && Debug.isOn("ssl")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
            System.out.println(threadName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
                        + ", fatal error: " + description +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
                        ": " + diagnostic + "\n" + cause.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
         * Ok, this engine's going down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
        int oldState = connectionState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
        connectionState = cs_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
        inboundDone = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
        sess.invalidate();
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1671
        if (handshakeSession != null) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1672
            handshakeSession.invalidate();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  1673
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
         * If we haven't even started handshaking yet, no need
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
         * to generate the fatal close alert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
        if (oldState != cs_START) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
            sendAlert(Alerts.alert_fatal, description);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
        if (cause instanceof SSLException) { // only true if != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
            closeReason = (SSLException)cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
             * Including RuntimeExceptions, but we'll throw those
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
             * down below.  The closeReason isn't used again,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
             * except for null checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
            closeReason =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
                Alerts.getSSLException(description, cause, diagnostic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
        writer.closeOutbound();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
        connectionState = cs_CLOSED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
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: 2
diff changeset
  1699
        // 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: 2
diff changeset
  1700
        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: 2
diff changeset
  1701
        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: 2
diff changeset
  1702
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
        if (cause instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
            throw (RuntimeException)cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
            throw closeReason;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
     * Process an incoming alert ... caller must already have synchronized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
     * access to "this".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
    private void recvAlert() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
        byte level = (byte)inputRecord.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
        byte description = (byte)inputRecord.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
        if (description == -1) { // check for short message
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
            fatal(Alerts.alert_illegal_parameter, "Short alert message");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
        if (debug != null && (Debug.isOn("record") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
                Debug.isOn("handshake"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
            synchronized (System.out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
                System.out.print(threadName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
                System.out.print(", RECV " + protocolVersion + " ALERT:  ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
                if (level == Alerts.alert_fatal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
                    System.out.print("fatal, ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
                } else if (level == Alerts.alert_warning) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
                    System.out.print("warning, ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
                    System.out.print("<level " + (0x0ff & level) + ">, ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
                System.out.println(Alerts.alertDescription(description));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
        if (level == Alerts.alert_warning) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
            if (description == Alerts.alert_close_notify) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
                if (connectionState == cs_HANDSHAKE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
                    fatal(Alerts.alert_unexpected_message,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
                                "Received close_notify during handshake");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
                    recvCN = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
                    closeInboundInternal();  // reply to close
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
                // The other legal warnings relate to certificates,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
                // e.g. no_certificate, bad_certificate, etc; these
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
                // are important to the handshaking code, which can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
                // also handle illegal protocol alerts if needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
                if (handshaker != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
                    handshaker.handshakeAlert(description);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
        } else { // fatal or unknown level
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
            String reason = "Received fatal alert: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
                + Alerts.alertDescription(description);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
            if (closeReason == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
                closeReason = Alerts.getSSLException(description, reason);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
            fatal(Alerts.alert_unexpected_message, reason);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
    }
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
     * Emit alerts.  Caller must have synchronized with "this".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
    private void sendAlert(byte level, byte description) {
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1773
        // the connectionState cannot be cs_START
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
        if (connectionState >= cs_CLOSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1778
        // For initial handshaking, don't send alert message to peer if
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1779
        // handshaker has not started.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1780
        if (connectionState == cs_HANDSHAKE &&
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1781
            (handshaker == null || !handshaker.started())) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1782
            return;
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1783
        }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1784
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
        EngineOutputRecord r = new EngineOutputRecord(Record.ct_alert, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
        r.setVersion(protocolVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
        boolean useDebug = debug != null && Debug.isOn("ssl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
        if (useDebug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
            synchronized (System.out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
                System.out.print(threadName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
                System.out.print(", SEND " + protocolVersion + " ALERT:  ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
                if (level == Alerts.alert_fatal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
                    System.out.print("fatal, ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
                } else if (level == Alerts.alert_warning) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
                    System.out.print("warning, ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
                    System.out.print("<level = " + (0x0ff & level) + ">, ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
                System.out.println("description = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
                        + Alerts.alertDescription(description));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
        r.write(level);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
        r.write(description);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
            writeRecord(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
            if (useDebug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
                System.out.println(threadName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
                    ", Exception sending alert: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
    // VARIOUS OTHER METHODS (COMMON TO SSLSocket)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
     * Controls whether new connections may cause creation of new SSL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
     * sessions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
     * As long as handshaking has not started, we can change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
     * whether we enable session creations.  Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
     * we will need to wait for the next handshake.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
    synchronized public void setEnableSessionCreation(boolean flag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
        enableSessionCreation = flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1834
        if ((handshaker != null) && !handshaker.activated()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
            handshaker.setEnableSessionCreation(enableSessionCreation);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
     * Returns true if new connections may cause creation of new SSL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
     * sessions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
    synchronized public boolean getEnableSessionCreation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
        return enableSessionCreation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
     * Sets the flag controlling whether a server mode engine
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
     * *REQUIRES* SSL client authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
     * As long as handshaking has not started, we can change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
     * whether client authentication is needed.  Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
     * we will need to wait for the next handshake.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
    synchronized public void setNeedClientAuth(boolean flag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
        doClientAuth = (flag ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
            SSLEngineImpl.clauth_required : SSLEngineImpl.clauth_none);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
        if ((handshaker != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
                (handshaker instanceof ServerHandshaker) &&
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1862
                !handshaker.activated()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
            ((ServerHandshaker) handshaker).setClientAuth(doClientAuth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
    synchronized public boolean getNeedClientAuth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
        return (doClientAuth == SSLEngineImpl.clauth_required);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
     * Sets the flag controlling whether a server mode engine
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
     * *REQUESTS* SSL client authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
     * As long as handshaking has not started, we can change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
     * whether client authentication is requested.  Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
     * we will need to wait for the next handshake.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
    synchronized public void setWantClientAuth(boolean flag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
        doClientAuth = (flag ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
            SSLEngineImpl.clauth_requested : SSLEngineImpl.clauth_none);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
        if ((handshaker != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
                (handshaker instanceof ServerHandshaker) &&
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1885
                !handshaker.activated()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
            ((ServerHandshaker) handshaker).setClientAuth(doClientAuth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
    synchronized public boolean getWantClientAuth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
        return (doClientAuth == SSLEngineImpl.clauth_requested);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
     * Sets the flag controlling whether the engine is in SSL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
     * client or server mode.  Must be called before any SSL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
     * traffic has started.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9246
diff changeset
  1900
    @SuppressWarnings("fallthrough")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
    synchronized public void setUseClientMode(boolean flag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
        switch (connectionState) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
        case cs_START:
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1905
            /*
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1906
             * If we need to change the engine mode and the enabled
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1907
             * protocols haven't specifically been set by the user,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1908
             * change them to the corresponding default ones.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1909
             */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1910
            if (roleIsServer != (!flag) &&
9246
c459f79af46b 6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents: 7043
diff changeset
  1911
                    sslContext.isDefaultProtocolList(enabledProtocols)) {
c459f79af46b 6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents: 7043
diff changeset
  1912
                enabledProtocols = sslContext.getDefaultProtocolList(!flag);
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1913
            }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1914
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
            roleIsServer = !flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
            serverModeSet = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
        case cs_HANDSHAKE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
             * If we have a handshaker, but haven't started
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
             * SSL traffic, we can throw away our current
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
             * handshaker, and start from scratch.  Don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
             * need to call doneConnect() again, we already
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
             * have the streams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
            assert(handshaker != null);
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1928
            if (!handshaker.activated()) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1929
                /*
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1930
                 * If we need to change the engine mode and the enabled
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1931
                 * protocols haven't specifically been set by the user,
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1932
                 * change them to the corresponding default ones.
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1933
                 */
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1934
                if (roleIsServer != (!flag) &&
9246
c459f79af46b 6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents: 7043
diff changeset
  1935
                        sslContext.isDefaultProtocolList(enabledProtocols)) {
c459f79af46b 6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents: 7043
diff changeset
  1936
                    enabledProtocols = sslContext.getDefaultProtocolList(!flag);
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1937
                }
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1938
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
                roleIsServer = !flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
                connectionState = cs_START;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
                initHandshaker();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
            // If handshake has started, that's an error.  Fall through...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
            if (debug != null && Debug.isOn("ssl")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
                System.out.println(threadName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
                    ", setUseClientMode() invoked in state = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
                    connectionState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
             * We can let them continue if they catch this correctly,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
             * we don't need to shut this down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
                "Cannot change mode after SSL traffic has started");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
    synchronized public boolean getUseClientMode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
        return !roleIsServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
     * Returns the names of the cipher suites which could be enabled for use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
     * on an SSL connection.  Normally, only a subset of these will actually
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
     * be enabled by default, since this list may include cipher suites which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
     * do not support the mutual authentication of servers and clients, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
     * which do not protect data confidentiality.  Servers may also need
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
     * certain kinds of certificates to use certain cipher suites.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
     * @return an array of cipher suite names
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
    public String[] getSupportedCipherSuites() {
9246
c459f79af46b 6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents: 7043
diff changeset
  1979
        return sslContext.getSuportedCipherSuiteList().toStringArray();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
     * Controls which particular cipher suites are enabled for use on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
     * this connection.  The cipher suites must have been listed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
     * getCipherSuites() as being supported.  Even if a suite has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
     * enabled, it might never be used if no peer supports it or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
     * requisite certificates (and private keys) are not available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
     * @param suites Names of all the cipher suites to enable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
    synchronized public void setEnabledCipherSuites(String[] suites) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
        enabledCipherSuites = new CipherSuiteList(suites);
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1993
        if ((handshaker != null) && !handshaker.activated()) {
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  1994
            handshaker.setEnabledCipherSuites(enabledCipherSuites);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
     * Returns the names of the SSL cipher suites which are currently enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
     * for use on this connection.  When an SSL engine is first created,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
     * all enabled cipher suites <em>(a)</em> protect data confidentiality,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
     * by traffic encryption, and <em>(b)</em> can mutually authenticate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
     * both clients and servers.  Thus, in some environments, this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
     * might be empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
     * @return an array of cipher suite names
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
    synchronized public String[] getEnabledCipherSuites() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
        return enabledCipherSuites.toStringArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
     * Returns the protocols that are supported by this implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
     * A subset of the supported protocols may be enabled for this connection
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2016
     * @return an array of protocol names.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
    public String[] getSupportedProtocols() {
9246
c459f79af46b 6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents: 7043
diff changeset
  2019
        return sslContext.getSuportedProtocolList().toStringArray();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
     * Controls which protocols are enabled for use on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
     * this connection.  The protocols must have been listed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
     * getSupportedProtocols() as being supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
     * @param protocols protocols to enable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
     * @exception IllegalArgumentException when one of the protocols
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
     *  named by the parameter is not supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
    synchronized public void setEnabledProtocols(String[] protocols) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
        enabledProtocols = new ProtocolList(protocols);
7039
6464c8e62a18 4873188: Support TLS 1.1
xuelei
parents: 6856
diff changeset
  2033
        if ((handshaker != null) && !handshaker.activated()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
            handshaker.setEnabledProtocols(enabledProtocols);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
    synchronized public String[] getEnabledProtocols() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
        return enabledProtocols.toStringArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
    /**
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2043
     * Returns the SSLParameters in effect for this SSLEngine.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
     */
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2045
    synchronized public SSLParameters getSSLParameters() {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2046
        SSLParameters params = super.getSSLParameters();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2047
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2048
        // the super implementation does not handle the following parameters
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2049
        params.setEndpointIdentificationAlgorithm(identificationProtocol);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2050
        params.setAlgorithmConstraints(algorithmConstraints);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2051
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2052
        return params;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
    /**
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2056
     * Applies SSLParameters to this engine.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
     */
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2058
    synchronized public void setSSLParameters(SSLParameters params) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2059
        super.setSSLParameters(params);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2060
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2061
        // the super implementation does not handle the following parameters
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2062
        identificationProtocol = params.getEndpointIdentificationAlgorithm();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2063
        algorithmConstraints = params.getAlgorithmConstraints();
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2064
        if ((handshaker != null) && !handshaker.started()) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2065
            handshaker.setIdentificationProtocol(identificationProtocol);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2066
            handshaker.setAlgorithmConstraints(algorithmConstraints);
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
  2067
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
     * Return the name of the current thread. Utility method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
    private static String threadName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
        return Thread.currentThread().getName();
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 a printable representation of this end of the connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
        StringBuilder retval = new StringBuilder(80);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
        retval.append(Integer.toHexString(hashCode()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
        retval.append("[");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
        retval.append("SSLEngine[hostname=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
        String host = getPeerHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
        retval.append((host == null) ? "null" : host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
        retval.append(" port=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
        retval.append(Integer.toString(getPeerPort()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
        retval.append("] ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
        retval.append(getSession().getCipherSuite());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
        retval.append("]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
        return retval.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
}