jdk/src/share/classes/sun/security/ssl/InputRecord.java
author xuelei
Fri, 08 Apr 2011 02:00:09 -0700
changeset 9246 c459f79af46b
parent 5506 202f599c92aa
child 14664 e71aa0962e70
permissions -rw-r--r--
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled Summary: Reorg the SSLContext implementation Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     2
 * Copyright (c) 1996, 2008, 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: 715
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: 715
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: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.SocketTimeoutException;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.misc.HexDumpEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * SSL 3.0 records, as pulled off a TCP stream.  Input records are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * basically buffers tied to a particular input stream ... a layer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * above this must map these records into the model of a continuous
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * stream of data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Since this returns SSL 3.0 records, it's the layer that needs to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * map SSL 2.0 style handshake records into SSL 3.0 ones for those
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * "old" clients that interop with both V2 and V3 servers.  Not as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * pretty as might be desired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * NOTE:  During handshaking, each message must be hashed to support
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * verification that the handshake process wasn't compromised.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
class InputRecord extends ByteArrayInputStream implements Record {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private HandshakeHash       handshakeHash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private int                 lastHashed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    boolean                     formatVerified = true;  // SSLv2 ruled out?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private boolean             isClosed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private boolean             appDataValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // The ClientHello version to accept. If set to ProtocolVersion.SSL20Hello
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // and the first message we read is a ClientHello in V2 format, we convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // it to V3. Otherwise we throw an exception when encountering a V2 hello.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private ProtocolVersion     helloVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /* Class and subclass dynamic debugging support */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    static final Debug debug = Debug.getInstance("ssl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /* The existing record length */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private int exlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /* V2 handshake message */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private byte v2Buf[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Construct the record to hold the maximum sized input record.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Data will be filled in separately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    InputRecord() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        super(new byte[maxRecordSize]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        setHelloVersion(ProtocolVersion.DEFAULT_HELLO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        pos = headerSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        count = headerSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        lastHashed = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        exlen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        v2Buf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    void setHelloVersion(ProtocolVersion helloVersion) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        this.helloVersion = helloVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    ProtocolVersion getHelloVersion() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return helloVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Enable format checks if initial handshaking hasn't completed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    void enableFormatChecks() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        formatVerified = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    // return whether the data in this record is valid, decrypted data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    boolean isAppDataValid() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return appDataValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    void setAppDataValid(boolean value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        appDataValid = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Return the content type of the record.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    byte contentType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return buf[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * For handshaking, we need to be able to hash every byte above the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * record marking layer.  This is where we're guaranteed to see those
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * bytes, so this is where we can hash them ... especially in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * case of hashing the initial V2 message!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    void setHandshakeHash(HandshakeHash handshakeHash) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        this.handshakeHash = handshakeHash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    HandshakeHash getHandshakeHash() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        return handshakeHash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Verify and remove the MAC ... used for all records.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    boolean checkMAC(MAC signer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        int len = signer.MAClen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (len == 0) { // no mac
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        int offset = count - len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (offset < headerSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            // data length would be negative, something is wrong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        byte[] mac = signer.compute(contentType(), buf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            headerSize, offset - headerSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (len != mac.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            throw new RuntimeException("Internal MAC error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            if (buf[offset + i] != mac[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        count -= len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    void decrypt(CipherBox box) throws BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        int len = count - headerSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        count = headerSize + box.decrypt(buf, headerSize, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * Well ... hello_request messages are _never_ hashed since we can't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * know when they'd appear in the sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    void ignore(int bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (bytes > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            pos += bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            lastHashed = pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * We hash the (plaintext) we've processed, but only on demand.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * There is one place where we want to access the hash in the middle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * of a record:  client cert message gets hashed, and part of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * same record is the client cert verify message which uses that hash.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * So we track how much we've read and hashed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    void doHashes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        int len = pos - lastHashed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            hashInternal(buf, lastHashed, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            lastHashed = pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * Need a helper function so we can hash the V2 hello correctly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    private void hashInternal(byte databuf [], int offset, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        if (debug != null && Debug.isOn("data")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                HexDumpEncoder hd = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                System.out.println("[read] MD5 and SHA1 hashes:  len = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    + len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                hd.encodeBuffer(new ByteArrayInputStream(databuf, offset, len),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            } catch (IOException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        handshakeHash.update(databuf, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * Handshake messages may cross record boundaries.  We "queue"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * these in big buffers if we need to cope with this problem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * This is not anticipated to be a common case; if this turns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * out to be wrong, this can readily be sped up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    void queueHandshake(InputRecord r) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
         * Hash any data that's read but unhashed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        doHashes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
         * Move any unread data to the front of the buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
         * flagging it all as unhashed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        if (pos > headerSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            len = count - pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            if (len != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                System.arraycopy(buf, pos, buf, headerSize, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            pos = headerSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            lastHashed = pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            count = headerSize + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
         * Grow "buf" if needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        len = r.available() + count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if (buf.length < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            byte        newbuf [];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            newbuf = new byte [len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            System.arraycopy(buf, 0, newbuf, 0, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            buf = newbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
         * Append the new buffer to this one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        System.arraycopy(r.buf, r.pos, buf, count, len - count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        count = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
         * Adjust lastHashed; important for now with clients which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
         * send SSL V2 client hellos.  This will go away eventually,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
         * by buffer code cleanup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        len = r.lastHashed - r.pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        if (pos == headerSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            lastHashed += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            throw new SSLProtocolException("?? confused buffer hashing ??");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        // we've read the record, advance the pointers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        r.pos = r.count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * Prevent any more data from being read into this record,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * and flag the record as holding no data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    public void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        appDataValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        isClosed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        mark = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * We may need to send this SSL v2 "No Cipher" message back, if we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * are faced with an SSLv2 "hello" that's not saying "I talk v3".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * It's the only one documented in the V2 spec as a fatal error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    private static final byte[] v2NoCipher = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        (byte)0x80, (byte)0x03, // unpadded 3 byte record
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        (byte)0x00,             // ... error message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        (byte)0x00, (byte)0x01  // ... NO_CIPHER error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    private int readFully(InputStream s, byte b[], int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        while (n < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            int readLen = s.read(b, off + n, len - n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            if (readLen < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                return readLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            if (debug != null && Debug.isOn("packet")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                    HexDumpEncoder hd = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                    ByteBuffer bb = ByteBuffer.wrap(b, off + n, readLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    System.out.println("[Raw read]: length = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                        bb.remaining());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    hd.encodeBuffer(bb, System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                } catch (IOException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            n += readLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            exlen += readLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        return n;
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
     * Read the SSL V3 record ... first time around, check to see if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * really IS a V3 record.  Handle SSL V2 clients which can talk V3.0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * as well as real V3 record format; otherwise report an error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    void read(InputStream s, OutputStream o) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        if (isClosed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
         * For SSL it really _is_ an error if the other end went away
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
         * so ungracefully as to not shut down cleanly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        if(exlen < headerSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            int really = readFully(s, buf, exlen, headerSize - exlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            if (really < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                throw new EOFException("SSL peer shut down incorrectly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            pos = headerSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            count = headerSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            lastHashed = pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
         * The first record might use some other record marking convention,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
         * typically SSL v2 header.  (PCT could also be detected here.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
         * This case is currently common -- Navigator 3.0 usually works
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
         * this way, as do IE 3.0 and other products.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        if (!formatVerified) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            formatVerified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
             * The first record must either be a handshake record or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
             * alert message. If it's not, it is either invalid or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
             * SSLv2 message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            if (buf[0] != ct_handshake && buf[0] != ct_alert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                handleUnknownRecord(s, o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                readV3Record(s, o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        } else { // formatVerified == true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            readV3Record(s, o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * Read a SSL/TLS record. Throw an IOException if the format is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    private void readV3Record(InputStream s, OutputStream o)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        ProtocolVersion recordVersion = ProtocolVersion.valueOf(buf[1], buf[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        // Check if too old (currently not possible)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        // or if the major version does not match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        // The actual version negotiation is in the handshaker classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        if ((recordVersion.v < ProtocolVersion.MIN.v)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                || (recordVersion.major > ProtocolVersion.MAX.major)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            throw new SSLException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                "Unsupported record version " + recordVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
         * Get and check length, then the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        int contentLen = ((buf[3] & 0x0ff) << 8) + (buf[4] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
         * Check for upper bound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        if (contentLen < 0 || contentLen > maxLargeRecordSize - headerSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            throw new SSLProtocolException("Bad InputRecord size"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                + ", count = " + contentLen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                + ", buf.length = " + buf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
         * Grow "buf" if needed. Since buf is maxRecordSize by default,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
         * this only occurs when we receive records which violate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
         * SSL specification. This is a workaround for a Microsoft SSL bug.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        if (contentLen > buf.length - headerSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            byte[] newbuf = new byte[contentLen + headerSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            System.arraycopy(buf, 0, newbuf, 0, headerSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            buf = newbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        if (exlen < contentLen + headerSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            int really = readFully(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                s, buf, exlen, contentLen + headerSize - exlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            if (really < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                throw new SSLException("SSL peer shut down incorrectly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            }
99
d70d3cc4dbe3 6542796: CPU issue with JSSE and tomcat
xuelei
parents: 2
diff changeset
   429
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
99
d70d3cc4dbe3 6542796: CPU issue with JSSE and tomcat
xuelei
parents: 2
diff changeset
   431
        // now we've got a complete record.
d70d3cc4dbe3 6542796: CPU issue with JSSE and tomcat
xuelei
parents: 2
diff changeset
   432
        count = contentLen + headerSize;
d70d3cc4dbe3 6542796: CPU issue with JSSE and tomcat
xuelei
parents: 2
diff changeset
   433
        exlen = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        if (debug != null && Debug.isOn("record")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            if (count < 0 || count > (maxRecordSize - headerSize)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                System.out.println(Thread.currentThread().getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                    + ", Bad InputRecord size" + ", count = " + count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            System.out.println(Thread.currentThread().getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                + ", READ: " + recordVersion + " "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                + contentName(contentType()) + ", length = " + available());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
         * then caller decrypts, verifies, and uncompresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * Deal with unknown records. Called if the first data we read on this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * connection does not look like an SSL/TLS record. It could a SSLv2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * message, or just garbage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    private void handleUnknownRecord(InputStream s, OutputStream o)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
         * No?  Oh well; does it look like a V2 "ClientHello"?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
         * That'd be an unpadded handshake message; we don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
         * bother checking length just now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        if (((buf[0] & 0x080) != 0) && buf[2] == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
             * if the user has disabled SSLv2Hello (using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
             * setEnabledProtocol) then throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
             * exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            if (helloVersion != ProtocolVersion.SSL20Hello) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                throw new SSLHandshakeException("SSLv2Hello is disabled");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            ProtocolVersion recordVersion =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                                ProtocolVersion.valueOf(buf[3], buf[4]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            if (recordVersion == ProtocolVersion.SSL20Hello) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                 * Looks like a V2 client hello, but not one saying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                 * "let's talk SSLv3".  So we send an SSLv2 error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                 * message, one that's treated as fatal by clients.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                 * (Otherwise we'll hang.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                    writeBuffer(o, v2NoCipher, 0, v2NoCipher.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                    /* NOTHING */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                throw new SSLException("Unsupported SSL v2.0 ClientHello");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
             * If we can map this into a V3 ClientHello, read and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
             * hash the rest of the V2 handshake, turn it into a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
             * V3 ClientHello message, and pass it up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            int len = ((buf[0] & 0x7f) << 8) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                (buf[1] & 0xff) - 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            if (v2Buf == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                v2Buf = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            if (exlen < len + headerSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                int really = readFully(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                        s, v2Buf, exlen - headerSize, len + headerSize - exlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                if (really < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                    throw new EOFException("SSL peer shut down incorrectly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                }
99
d70d3cc4dbe3 6542796: CPU issue with JSSE and tomcat
xuelei
parents: 2
diff changeset
   505
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
99
d70d3cc4dbe3 6542796: CPU issue with JSSE and tomcat
xuelei
parents: 2
diff changeset
   507
            // now we've got a complete record.
d70d3cc4dbe3 6542796: CPU issue with JSSE and tomcat
xuelei
parents: 2
diff changeset
   508
            exlen = 0;
d70d3cc4dbe3 6542796: CPU issue with JSSE and tomcat
xuelei
parents: 2
diff changeset
   509
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            hashInternal(buf, 2, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            hashInternal(v2Buf, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            V2toV3ClientHello(v2Buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            v2Buf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            lastHashed = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            if (debug != null && Debug.isOn("record"))  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                    Thread.currentThread().getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                    + ", READ:  SSL v2, contentType = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                    + contentName(contentType())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                    + ", translated length = " + available());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
             * Does it look like a V2 "ServerHello"?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            if (((buf [0] & 0x080) != 0) && buf [2] == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                throw new SSLException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                    "SSL V2.0 servers are not supported.");
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
             * If this is a V2 NoCipher message then this means
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
             * the other server doesn't support V3. Otherwise, we just
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
             * don't understand what it's saying.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            for (int i = 0; i < v2NoCipher.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                if (buf[i] != v2NoCipher[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                    throw new SSLException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                        "Unrecognized SSL message, plaintext connection?");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            throw new SSLException("SSL V2.0 servers are not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * Actually do the write here.  For SSLEngine's HS data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * we'll override this method and let it take the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    void writeBuffer(OutputStream s, byte [] buf, int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        s.write(buf, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        s.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * Support "old" clients which are capable of SSL V3.0 protocol ... for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * example, Navigator 3.0 clients.  The V2 message is in the header and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * the bytes passed as parameter.  This routine translates the V2 message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * into an equivalent V3 one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    private void V2toV3ClientHello(byte v2Msg []) throws SSLException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
         * Build the first part of the V3 record header from the V2 one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
         * that's now buffered up.  (Lengths are fixed up later).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        buf [0] = ct_handshake;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        buf [1] = buf [3];      // V3.x
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        buf[2] = buf[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        // header [3..4] for handshake message length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        // count = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
         * Store the generic V3 handshake header:  4 bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        buf [5] = 1;    // HandshakeMessage.ht_client_hello
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        // buf [6..8] for length of ClientHello (int24)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        // count += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
         * ClientHello header starts with SSL version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        buf [9] = buf [1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        buf [10] = buf [2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        // count += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        count = 11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
         * Start parsing the V2 message ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        int      cipherSpecLen, sessionIdLen, nonceLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        cipherSpecLen = ((v2Msg [0] & 0xff) << 8) + (v2Msg [1] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        sessionIdLen  = ((v2Msg [2] & 0xff) << 8) + (v2Msg [3] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        nonceLen   = ((v2Msg [4] & 0xff) << 8) + (v2Msg [5] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
         * Copy Random value/nonce ... if less than the 32 bytes of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
         * a V3 "Random", right justify and zero pad to the left.  Else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
         * just take the last 32 bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        int      offset = 6 + cipherSpecLen + sessionIdLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        if (nonceLen < 32) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            for (i = 0; i < (32 - nonceLen); i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                buf [count++] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            System.arraycopy(v2Msg, offset, buf, count, nonceLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            count += nonceLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            System.arraycopy(v2Msg, offset + (nonceLen - 32),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                    buf, count, 32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            count += 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
         * Copy Session ID (only one byte length!)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        offset -= sessionIdLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        buf [count++] = (byte) sessionIdLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        System.arraycopy(v2Msg, offset, buf, count, sessionIdLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        count += sessionIdLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
         * Copy and translate cipher suites ... V2 specs with first byte zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
         * are really V3 specs (in the last 2 bytes), just copy those and drop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
         * the other ones.  Preference order remains unchanged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
         * Example:  Netscape Navigator 3.0 (exportable) says:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
         * 0/3,     SSL_RSA_EXPORT_WITH_RC4_40_MD5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
         * 0/6,     SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
         * Microsoft Internet Explorer 3.0 (exportable) supports only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
         * 0/3,     SSL_RSA_EXPORT_WITH_RC4_40_MD5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        int j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        offset -= cipherSpecLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        j = count + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        for (i = 0; i < cipherSpecLen; i += 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            if (v2Msg [offset + i] != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            buf [j++] = v2Msg [offset + i + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            buf [j++] = v2Msg [offset + i + 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        j -= count + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        buf [count++] = (byte) (j >>> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        buf [count++] = (byte) j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        count += j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
         * Append compression methods (default/null only)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        buf [count++] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        buf [count++] = 0;      // Session.compression_null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
         * Fill in lengths of the messages we synthesized (nested:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
         * V3 handshake message within V3 record) and then return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        buf [3] = (byte) (count - headerSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        buf [4] = (byte) ((count - headerSize) >>> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        buf [headerSize + 1] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        buf [headerSize + 2] = (byte) (((count - headerSize) - 4) >>> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        buf [headerSize + 3] = (byte) ((count - headerSize) - 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        pos = headerSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * Return a description for the given content type. This method should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * in Record, but since that is an interface this is not possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * Called from InputRecord and OutputRecord.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    static String contentName(int contentType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        switch (contentType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        case ct_change_cipher_spec:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            return "Change Cipher Spec";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        case ct_alert:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            return "Alert";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        case ct_handshake:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            return "Handshake";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        case ct_application_data:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            return "Application Data";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            return "contentType = " + contentType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
}