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