jdk/src/java.base/share/classes/sun/security/ssl/InputRecord.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 24263 jdk/src/share/classes/sun/security/ssl/InputRecord.java@f95477ce56e4
child 30904 ec0224270f90
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
24263
f95477ce56e4 8042449: Issue for negative byte major record version
xuelei
parents: 16913
diff changeset
     2
 * Copyright (c) 1996, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.crypto.BadPaddingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.misc.HexDumpEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * SSL 3.0 records, as pulled off a TCP stream.  Input records are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * basically buffers tied to a particular input stream ... a layer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * above this must map these records into the model of a continuous
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * stream of data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Since this returns SSL 3.0 records, it's the layer that needs to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * map SSL 2.0 style handshake records into SSL 3.0 ones for those
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * "old" clients that interop with both V2 and V3 servers.  Not as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * pretty as might be desired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * NOTE:  During handshaking, each message must be hashed to support
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * verification that the handshake process wasn't compromised.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
class InputRecord extends ByteArrayInputStream implements Record {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private HandshakeHash       handshakeHash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private int                 lastHashed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    boolean                     formatVerified = true;  // SSLv2 ruled out?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private boolean             isClosed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private boolean             appDataValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // The ClientHello version to accept. If set to ProtocolVersion.SSL20Hello
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // and the first message we read is a ClientHello in V2 format, we convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // it to V3. Otherwise we throw an exception when encountering a V2 hello.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private ProtocolVersion     helloVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /* Class and subclass dynamic debugging support */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    static final Debug debug = Debug.getInstance("ssl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /* The existing record length */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private int exlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /* V2 handshake message */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private byte v2Buf[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Construct the record to hold the maximum sized input record.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Data will be filled in separately.
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
    80
     *
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
    81
     * The structure of the byte buffer looks like:
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
    82
     *
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
    83
     *     |--------+---------+---------------------------------|
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
    84
     *     | header |   IV    | content, MAC/TAG, padding, etc. |
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
    85
     *     | headerPlusIVSize |
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
    86
     *
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
    87
     * header: the header of an SSL records
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
    88
     * IV:     the optional IV/nonce field, it is only required for block
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
    89
     *         (TLS 1.1 or later) and AEAD cipher suites.
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
    90
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    InputRecord() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        super(new byte[maxRecordSize]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        setHelloVersion(ProtocolVersion.DEFAULT_HELLO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        pos = headerSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        count = headerSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        lastHashed = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        exlen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        v2Buf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    void setHelloVersion(ProtocolVersion helloVersion) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        this.helloVersion = helloVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    ProtocolVersion getHelloVersion() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        return helloVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Enable format checks if initial handshaking hasn't completed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    void enableFormatChecks() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        formatVerified = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    // return whether the data in this record is valid, decrypted data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    boolean isAppDataValid() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        return appDataValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    void setAppDataValid(boolean value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        appDataValid = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Return the content type of the record.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    byte contentType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        return buf[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * For handshaking, we need to be able to hash every byte above the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * record marking layer.  This is where we're guaranteed to see those
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * bytes, so this is where we can hash them ... especially in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * case of hashing the initial V2 message!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    void setHandshakeHash(HandshakeHash handshakeHash) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        this.handshakeHash = handshakeHash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    HandshakeHash getHandshakeHash() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return handshakeHash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   147
    void decrypt(Authenticator authenticator,
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   148
            CipherBox box) throws BadPaddingException {
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   149
        BadPaddingException reservedBPE = null;
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   150
        int tagLen =
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   151
            (authenticator instanceof MAC) ? ((MAC)authenticator).MAClen() : 0;
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   152
        int cipheredLength = count - headerSize;
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   153
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   154
        if (!box.isNullCipher()) {
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   155
            try {
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   156
                // apply explicit nonce for AEAD/CBC cipher suites if needed
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   157
                int nonceSize = box.applyExplicitNonce(authenticator,
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   158
                        contentType(), buf, headerSize, cipheredLength);
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   159
                pos = headerSize + nonceSize;
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   160
                lastHashed = pos;   // don't digest the explicit nonce
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   161
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   162
                // decrypt the content
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   163
                int offset = headerSize;
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   164
                if (box.isAEADMode()) {
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   165
                    // DON'T encrypt the nonce_explicit for AEAD mode
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   166
                    offset += nonceSize;
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   167
                }   // The explicit IV for CBC mode can be decrypted.
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   168
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   169
                // Note that the CipherBox.decrypt() does not change
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   170
                // the capacity of the buffer.
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   171
                count = offset +
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   172
                    box.decrypt(buf, offset, count - offset, tagLen);
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   173
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   174
                // Note that we don't remove the nonce from the buffer.
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   175
            } catch (BadPaddingException bpe) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   176
                // RFC 2246 states that decryption_failed should be used
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   177
                // for this purpose. However, that allows certain attacks,
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   178
                // so we just send bad record MAC. We also need to make
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   179
                // sure to always check the MAC to avoid a timing attack
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   180
                // for the same issue. See paper by Vaudenay et al and the
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   181
                // update in RFC 4346/5246.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   182
                //
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   183
                // Failover to message authentication code checking.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   184
                reservedBPE = bpe;
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   185
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   188
        // Requires message authentication code for null, stream and block
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   189
        // cipher suites.
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   190
        if (authenticator instanceof MAC && tagLen != 0) {
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   191
            MAC signer = (MAC)authenticator;
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   192
            int macOffset = count - tagLen;
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   193
            int contentLen = macOffset - pos;
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   194
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   195
            // Note that although it is not necessary, we run the same MAC
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   196
            // computation and comparison on the payload for both stream
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   197
            // cipher and CBC block cipher.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   198
            if (contentLen < 0) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   199
                // negative data length, something is wrong
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   200
                if (reservedBPE == null) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   201
                    reservedBPE = new BadPaddingException("bad record");
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   202
                }
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   203
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   204
                // set offset of the dummy MAC
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   205
                macOffset = headerSize + cipheredLength - tagLen;
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   206
                contentLen = macOffset - headerSize;
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   207
            }
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   208
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   209
            count -= tagLen;  // Set the count before any MAC checking
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   210
                              // exception occurs, so that the following
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   211
                              // process can read the actual decrypted
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   212
                              // content (minus the MAC) in the fragment
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   213
                              // if necessary.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   215
            // Run MAC computation and comparison on the payload.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   216
            if (checkMacTags(contentType(),
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   217
                    buf, pos, contentLen, signer, false)) {
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   218
                if (reservedBPE == null) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   219
                    reservedBPE = new BadPaddingException("bad record MAC");
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   220
                }
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   221
            }
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   222
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   223
            // Run MAC computation and comparison on the remainder.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   224
            //
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   225
            // It is only necessary for CBC block cipher.  It is used to get a
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   226
            // constant time of MAC computation and comparison on each record.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   227
            if (box.isCBCMode()) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   228
                int remainingLen = calculateRemainingLen(
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   229
                                        signer, cipheredLength, contentLen);
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   230
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   231
                // NOTE: remainingLen may be bigger (less than 1 block of the
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   232
                // hash algorithm of the MAC) than the cipheredLength. However,
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   233
                // We won't need to worry about it because we always use a
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   234
                // maximum buffer for every record.  We need a change here if
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   235
                // we use small buffer size in the future.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   236
                if (remainingLen > buf.length) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   237
                    // unlikely to happen, just a placehold
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   238
                    throw new RuntimeException(
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   239
                        "Internal buffer capacity error");
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   240
                }
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   241
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   242
                // Won't need to worry about the result on the remainder. And
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   243
                // then we won't need to worry about what's actual data to
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   244
                // check MAC tag on.  We start the check from the header of the
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   245
                // buffer so that we don't need to construct a new byte buffer.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   246
                checkMacTags(contentType(), buf, 0, remainingLen, signer, true);
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   247
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   250
        // Is it a failover?
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   251
        if (reservedBPE != null) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   252
            throw reservedBPE;
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   253
        }
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   254
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   256
    /*
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   257
     * Run MAC computation and comparison
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   258
     *
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   259
     * Please DON'T change the content of the byte buffer parameter!
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   260
     */
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   261
    static boolean checkMacTags(byte contentType, byte[] buffer,
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   262
            int offset, int contentLen, MAC signer, boolean isSimulated) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   263
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   264
        int tagLen = signer.MAClen();
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   265
        byte[] hash = signer.compute(
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   266
                contentType, buffer, offset, contentLen, isSimulated);
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   267
        if (hash == null || tagLen != hash.length) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   268
            // Something is wrong with MAC implementation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            throw new RuntimeException("Internal MAC error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   272
        int[] results = compareMacTags(buffer, offset + contentLen, hash);
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   273
        return (results[0] != 0);
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   274
    }
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   275
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   276
    /*
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   277
     * A constant-time comparison of the MAC tags.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   278
     *
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   279
     * Please DON'T change the content of the byte buffer parameter!
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   280
     */
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   281
    private static int[] compareMacTags(
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   282
            byte[] buffer, int offset, byte[] tag) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   283
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   284
        // An array of hits is used to prevent Hotspot optimization for
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   285
        // the purpose of a constant-time check.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   286
        int[] results = {0, 0};    // {missed #, matched #}
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   287
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   288
        // The caller ensures there are enough bytes available in the buffer.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   289
        // So we won't need to check the length of the buffer.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   290
        for (int i = 0; i < tag.length; i++) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   291
            if (buffer[offset + i] != tag[i]) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   292
                results[0]++;       // mismatched bytes
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   293
            } else {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   294
                results[1]++;       // matched bytes
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   297
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   298
        return results;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   301
    /*
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   302
     * Calculate the length of a dummy buffer to run MAC computation
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   303
     * and comparison on the remainder.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   304
     *
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   305
     * The caller MUST ensure that the fullLen is not less than usedLen.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   306
     */
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   307
    static int calculateRemainingLen(
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   308
            MAC signer, int fullLen, int usedLen) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   309
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   310
        int blockLen = signer.hashBlockLen();
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   311
        int minimalPaddingLen = signer.minimalPaddingLen();
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   312
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   313
        // (blockLen - minimalPaddingLen) is the maximum message size of
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   314
        // the last block of hash function operation. See FIPS 180-4, or
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   315
        // MD5 specification.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   316
        fullLen += 13 - (blockLen - minimalPaddingLen);
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   317
        usedLen += 13 - (blockLen - minimalPaddingLen);
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   318
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   319
        // Note: fullLen is always not less than usedLen, and blockLen
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   320
        // is always bigger than minimalPaddingLen, so we don't worry
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   321
        // about negative values. 0x01 is added to the result to ensure
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   322
        // that the return value is positive.  The extra one byte does
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   323
        // not impact the overall MAC compression function evaluations.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   324
        return 0x01 + (int)(Math.ceil(fullLen/(1.0d * blockLen)) -
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   325
                Math.ceil(usedLen/(1.0d * blockLen))) * signer.hashBlockLen();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * Well ... hello_request messages are _never_ hashed since we can't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * know when they'd appear in the sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    void ignore(int bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        if (bytes > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            pos += bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            lastHashed = pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * We hash the (plaintext) we've processed, but only on demand.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * There is one place where we want to access the hash in the middle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * of a record:  client cert message gets hashed, and part of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * same record is the client cert verify message which uses that hash.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * So we track how much we've read and hashed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    void doHashes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        int len = pos - lastHashed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        if (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            hashInternal(buf, lastHashed, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            lastHashed = pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * Need a helper function so we can hash the V2 hello correctly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    private void hashInternal(byte databuf [], int offset, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        if (debug != null && Debug.isOn("data")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                HexDumpEncoder hd = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                System.out.println("[read] MD5 and SHA1 hashes:  len = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    + len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                hd.encodeBuffer(new ByteArrayInputStream(databuf, offset, len),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            } catch (IOException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        handshakeHash.update(databuf, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * Handshake messages may cross record boundaries.  We "queue"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * these in big buffers if we need to cope with this problem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * This is not anticipated to be a common case; if this turns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * out to be wrong, this can readily be sped up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    void queueHandshake(InputRecord r) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
         * Hash any data that's read but unhashed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        doHashes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
         * Move any unread data to the front of the buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
         * flagging it all as unhashed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        if (pos > headerSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            len = count - pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            if (len != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                System.arraycopy(buf, pos, buf, headerSize, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            pos = headerSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            lastHashed = pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            count = headerSize + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
         * Grow "buf" if needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        len = r.available() + count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        if (buf.length < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            byte        newbuf [];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            newbuf = new byte [len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            System.arraycopy(buf, 0, newbuf, 0, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            buf = newbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
         * Append the new buffer to this one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        System.arraycopy(r.buf, r.pos, buf, count, len - count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        count = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
         * Adjust lastHashed; important for now with clients which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
         * send SSL V2 client hellos.  This will go away eventually,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
         * by buffer code cleanup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        len = r.lastHashed - r.pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        if (pos == headerSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            lastHashed += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            throw new SSLProtocolException("?? confused buffer hashing ??");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        // we've read the record, advance the pointers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        r.pos = r.count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * Prevent any more data from being read into this record,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * and flag the record as holding no data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
   440
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    public void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        appDataValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        isClosed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        mark = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        count = 0;
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
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * We may need to send this SSL v2 "No Cipher" message back, if we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * are faced with an SSLv2 "hello" that's not saying "I talk v3".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * It's the only one documented in the V2 spec as a fatal error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    private static final byte[] v2NoCipher = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        (byte)0x80, (byte)0x03, // unpadded 3 byte record
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        (byte)0x00,             // ... error message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        (byte)0x00, (byte)0x01  // ... NO_CIPHER error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    private int readFully(InputStream s, byte b[], int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        while (n < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            int readLen = s.read(b, off + n, len - n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            if (readLen < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                return readLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            if (debug != null && Debug.isOn("packet")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                    HexDumpEncoder hd = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                    ByteBuffer bb = ByteBuffer.wrap(b, off + n, readLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                    System.out.println("[Raw read]: length = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                        bb.remaining());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                    hd.encodeBuffer(bb, System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                } catch (IOException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            n += readLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            exlen += readLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * Read the SSL V3 record ... first time around, check to see if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * really IS a V3 record.  Handle SSL V2 clients which can talk V3.0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * as well as real V3 record format; otherwise report an error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    void read(InputStream s, OutputStream o) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        if (isClosed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
         * For SSL it really _is_ an error if the other end went away
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
         * so ungracefully as to not shut down cleanly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        if(exlen < headerSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            int really = readFully(s, buf, exlen, headerSize - exlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            if (really < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                throw new EOFException("SSL peer shut down incorrectly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            pos = headerSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            count = headerSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            lastHashed = pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
         * The first record might use some other record marking convention,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
         * typically SSL v2 header.  (PCT could also be detected here.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
         * This case is currently common -- Navigator 3.0 usually works
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
         * this way, as do IE 3.0 and other products.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        if (!formatVerified) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            formatVerified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
             * The first record must either be a handshake record or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
             * alert message. If it's not, it is either invalid or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
             * SSLv2 message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            if (buf[0] != ct_handshake && buf[0] != ct_alert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                handleUnknownRecord(s, o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                readV3Record(s, o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        } else { // formatVerified == true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            readV3Record(s, o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    /**
24263
f95477ce56e4 8042449: Issue for negative byte major record version
xuelei
parents: 16913
diff changeset
   537
     * Return true if the specified record protocol version is out of the
f95477ce56e4 8042449: Issue for negative byte major record version
xuelei
parents: 16913
diff changeset
   538
     * range of the possible supported versions.
f95477ce56e4 8042449: Issue for negative byte major record version
xuelei
parents: 16913
diff changeset
   539
     */
f95477ce56e4 8042449: Issue for negative byte major record version
xuelei
parents: 16913
diff changeset
   540
    static void checkRecordVersion(ProtocolVersion version,
f95477ce56e4 8042449: Issue for negative byte major record version
xuelei
parents: 16913
diff changeset
   541
            boolean allowSSL20Hello) throws SSLException {
f95477ce56e4 8042449: Issue for negative byte major record version
xuelei
parents: 16913
diff changeset
   542
        // Check if the record version is too old (currently not possible)
f95477ce56e4 8042449: Issue for negative byte major record version
xuelei
parents: 16913
diff changeset
   543
        // or if the major version does not match.
f95477ce56e4 8042449: Issue for negative byte major record version
xuelei
parents: 16913
diff changeset
   544
        //
f95477ce56e4 8042449: Issue for negative byte major record version
xuelei
parents: 16913
diff changeset
   545
        // The actual version negotiation is in the handshaker classes
f95477ce56e4 8042449: Issue for negative byte major record version
xuelei
parents: 16913
diff changeset
   546
        if ((version.v < ProtocolVersion.MIN.v) ||
f95477ce56e4 8042449: Issue for negative byte major record version
xuelei
parents: 16913
diff changeset
   547
            ((version.major & 0xFF) > (ProtocolVersion.MAX.major & 0xFF))) {
f95477ce56e4 8042449: Issue for negative byte major record version
xuelei
parents: 16913
diff changeset
   548
f95477ce56e4 8042449: Issue for negative byte major record version
xuelei
parents: 16913
diff changeset
   549
            // if it's not SSLv2, we're out of here.
f95477ce56e4 8042449: Issue for negative byte major record version
xuelei
parents: 16913
diff changeset
   550
            if (!allowSSL20Hello ||
f95477ce56e4 8042449: Issue for negative byte major record version
xuelei
parents: 16913
diff changeset
   551
                    (version.v != ProtocolVersion.SSL20Hello.v)) {
f95477ce56e4 8042449: Issue for negative byte major record version
xuelei
parents: 16913
diff changeset
   552
                throw new SSLException("Unsupported record version " + version);
f95477ce56e4 8042449: Issue for negative byte major record version
xuelei
parents: 16913
diff changeset
   553
            }
f95477ce56e4 8042449: Issue for negative byte major record version
xuelei
parents: 16913
diff changeset
   554
        }
f95477ce56e4 8042449: Issue for negative byte major record version
xuelei
parents: 16913
diff changeset
   555
    }
f95477ce56e4 8042449: Issue for negative byte major record version
xuelei
parents: 16913
diff changeset
   556
f95477ce56e4 8042449: Issue for negative byte major record version
xuelei
parents: 16913
diff changeset
   557
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * Read a SSL/TLS record. Throw an IOException if the format is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    private void readV3Record(InputStream s, OutputStream o)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        ProtocolVersion recordVersion = ProtocolVersion.valueOf(buf[1], buf[2]);
24263
f95477ce56e4 8042449: Issue for negative byte major record version
xuelei
parents: 16913
diff changeset
   563
f95477ce56e4 8042449: Issue for negative byte major record version
xuelei
parents: 16913
diff changeset
   564
        // check the record version
f95477ce56e4 8042449: Issue for negative byte major record version
xuelei
parents: 16913
diff changeset
   565
        checkRecordVersion(recordVersion, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
         * Get and check length, then the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        int contentLen = ((buf[3] & 0x0ff) << 8) + (buf[4] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
         * Check for upper bound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        if (contentLen < 0 || contentLen > maxLargeRecordSize - headerSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            throw new SSLProtocolException("Bad InputRecord size"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                + ", count = " + contentLen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                + ", buf.length = " + buf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
         * Grow "buf" if needed. Since buf is maxRecordSize by default,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
         * this only occurs when we receive records which violate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
         * SSL specification. This is a workaround for a Microsoft SSL bug.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        if (contentLen > buf.length - headerSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            byte[] newbuf = new byte[contentLen + headerSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            System.arraycopy(buf, 0, newbuf, 0, headerSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            buf = newbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        if (exlen < contentLen + headerSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            int really = readFully(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                s, buf, exlen, contentLen + headerSize - exlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            if (really < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                throw new SSLException("SSL peer shut down incorrectly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            }
99
d70d3cc4dbe3 6542796: CPU issue with JSSE and tomcat
xuelei
parents: 2
diff changeset
   598
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
99
d70d3cc4dbe3 6542796: CPU issue with JSSE and tomcat
xuelei
parents: 2
diff changeset
   600
        // now we've got a complete record.
d70d3cc4dbe3 6542796: CPU issue with JSSE and tomcat
xuelei
parents: 2
diff changeset
   601
        count = contentLen + headerSize;
d70d3cc4dbe3 6542796: CPU issue with JSSE and tomcat
xuelei
parents: 2
diff changeset
   602
        exlen = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        if (debug != null && Debug.isOn("record")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            if (count < 0 || count > (maxRecordSize - headerSize)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                System.out.println(Thread.currentThread().getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                    + ", Bad InputRecord size" + ", count = " + count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            System.out.println(Thread.currentThread().getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                + ", READ: " + recordVersion + " "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                + contentName(contentType()) + ", length = " + available());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
         * then caller decrypts, verifies, and uncompresses
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * Deal with unknown records. Called if the first data we read on this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * connection does not look like an SSL/TLS record. It could a SSLv2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * message, or just garbage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    private void handleUnknownRecord(InputStream s, OutputStream o)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
         * No?  Oh well; does it look like a V2 "ClientHello"?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
         * That'd be an unpadded handshake message; we don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
         * bother checking length just now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        if (((buf[0] & 0x080) != 0) && buf[2] == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
             * if the user has disabled SSLv2Hello (using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
             * setEnabledProtocol) then 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
            if (helloVersion != ProtocolVersion.SSL20Hello) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                throw new SSLHandshakeException("SSLv2Hello is disabled");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            ProtocolVersion recordVersion =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                                ProtocolVersion.valueOf(buf[3], buf[4]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            if (recordVersion == ProtocolVersion.SSL20Hello) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                 * Looks like a V2 client hello, but not one saying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                 * "let's talk SSLv3".  So we send an SSLv2 error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                 * message, one that's treated as fatal by clients.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                 * (Otherwise we'll hang.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                    writeBuffer(o, v2NoCipher, 0, v2NoCipher.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                    /* NOTHING */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                throw new SSLException("Unsupported SSL v2.0 ClientHello");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
             * If we can map this into a V3 ClientHello, read and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
             * hash the rest of the V2 handshake, turn it into a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
             * V3 ClientHello message, and pass it up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            int len = ((buf[0] & 0x7f) << 8) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                (buf[1] & 0xff) - 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            if (v2Buf == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                v2Buf = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            if (exlen < len + headerSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                int really = readFully(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                        s, v2Buf, exlen - headerSize, len + headerSize - exlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                if (really < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                    throw new EOFException("SSL peer shut down incorrectly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                }
99
d70d3cc4dbe3 6542796: CPU issue with JSSE and tomcat
xuelei
parents: 2
diff changeset
   674
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
99
d70d3cc4dbe3 6542796: CPU issue with JSSE and tomcat
xuelei
parents: 2
diff changeset
   676
            // now we've got a complete record.
d70d3cc4dbe3 6542796: CPU issue with JSSE and tomcat
xuelei
parents: 2
diff changeset
   677
            exlen = 0;
d70d3cc4dbe3 6542796: CPU issue with JSSE and tomcat
xuelei
parents: 2
diff changeset
   678
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            hashInternal(buf, 2, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            hashInternal(v2Buf, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            V2toV3ClientHello(v2Buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            v2Buf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            lastHashed = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            if (debug != null && Debug.isOn("record"))  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                    Thread.currentThread().getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                    + ", READ:  SSL v2, contentType = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                    + contentName(contentType())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                    + ", translated length = " + available());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
             * Does it look like a V2 "ServerHello"?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            if (((buf [0] & 0x080) != 0) && buf [2] == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                throw new SSLException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                    "SSL V2.0 servers are not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
             * If this is a V2 NoCipher message then this means
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
             * the other server doesn't support V3. Otherwise, we just
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
             * don't understand what it's saying.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            for (int i = 0; i < v2NoCipher.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                if (buf[i] != v2NoCipher[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                    throw new SSLException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                        "Unrecognized SSL message, plaintext connection?");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            throw new SSLException("SSL V2.0 servers are not supported.");
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
     * Actually do the write here.  For SSLEngine's HS data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * we'll override this method and let it take the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    void writeBuffer(OutputStream s, byte [] buf, int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        s.write(buf, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        s.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * Support "old" clients which are capable of SSL V3.0 protocol ... for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * example, Navigator 3.0 clients.  The V2 message is in the header and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * the bytes passed as parameter.  This routine translates the V2 message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * into an equivalent V3 one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    private void V2toV3ClientHello(byte v2Msg []) throws SSLException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
         * Build the first part of the V3 record header from the V2 one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
         * that's now buffered up.  (Lengths are fixed up later).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        buf [0] = ct_handshake;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        buf [1] = buf [3];      // V3.x
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        buf[2] = buf[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        // header [3..4] for handshake message length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        // count = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
         * Store the generic V3 handshake header:  4 bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        buf [5] = 1;    // HandshakeMessage.ht_client_hello
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        // buf [6..8] for length of ClientHello (int24)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        // count += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
         * ClientHello header starts with SSL version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        buf [9] = buf [1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        buf [10] = buf [2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        // count += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        count = 11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
         * Start parsing the V2 message ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        int      cipherSpecLen, sessionIdLen, nonceLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        cipherSpecLen = ((v2Msg [0] & 0xff) << 8) + (v2Msg [1] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        sessionIdLen  = ((v2Msg [2] & 0xff) << 8) + (v2Msg [3] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        nonceLen   = ((v2Msg [4] & 0xff) << 8) + (v2Msg [5] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
         * Copy Random value/nonce ... if less than the 32 bytes of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
         * a V3 "Random", right justify and zero pad to the left.  Else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
         * just take the last 32 bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        int      offset = 6 + cipherSpecLen + sessionIdLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        if (nonceLen < 32) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            for (i = 0; i < (32 - nonceLen); i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                buf [count++] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            System.arraycopy(v2Msg, offset, buf, count, nonceLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            count += nonceLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            System.arraycopy(v2Msg, offset + (nonceLen - 32),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                    buf, count, 32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            count += 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
         * Copy Session ID (only one byte length!)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        offset -= sessionIdLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        buf [count++] = (byte) sessionIdLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        System.arraycopy(v2Msg, offset, buf, count, sessionIdLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        count += sessionIdLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
         * Copy and translate cipher suites ... V2 specs with first byte zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
         * are really V3 specs (in the last 2 bytes), just copy those and drop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
         * the other ones.  Preference order remains unchanged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
         * Example:  Netscape Navigator 3.0 (exportable) says:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
         * 0/3,     SSL_RSA_EXPORT_WITH_RC4_40_MD5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
         * 0/6,     SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
         * Microsoft Internet Explorer 3.0 (exportable) supports only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
         * 0/3,     SSL_RSA_EXPORT_WITH_RC4_40_MD5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        int j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        offset -= cipherSpecLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        j = count + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        for (i = 0; i < cipherSpecLen; i += 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            if (v2Msg [offset + i] != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            buf [j++] = v2Msg [offset + i + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            buf [j++] = v2Msg [offset + i + 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        j -= count + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        buf [count++] = (byte) (j >>> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        buf [count++] = (byte) j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        count += j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
         * Append compression methods (default/null only)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        buf [count++] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        buf [count++] = 0;      // Session.compression_null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
         * Fill in lengths of the messages we synthesized (nested:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
         * V3 handshake message within V3 record) and then return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        buf [3] = (byte) (count - headerSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        buf [4] = (byte) ((count - headerSize) >>> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        buf [headerSize + 1] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        buf [headerSize + 2] = (byte) (((count - headerSize) - 4) >>> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        buf [headerSize + 3] = (byte) ((count - headerSize) - 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        pos = headerSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * Return a description for the given content type. This method should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * in Record, but since that is an interface this is not possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * Called from InputRecord and OutputRecord.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    static String contentName(int contentType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        switch (contentType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        case ct_change_cipher_spec:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            return "Change Cipher Spec";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        case ct_alert:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            return "Alert";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        case ct_handshake:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            return "Handshake";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        case ct_application_data:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            return "Application Data";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            return "contentType = " + contentType;
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
}