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