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