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