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