jdk/src/share/classes/sun/security/ssl/EngineInputRecord.java
author lana
Tue, 12 Mar 2013 19:04:49 -0700
changeset 16126 aad71cf676d7
parent 16067 36055e4b5305
parent 16113 946ec9b22004
child 16913 a6f4d1626ad9
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
     2
 * Copyright (c) 2003, 2013, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.crypto.BadPaddingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.misc.HexDumpEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Wrapper class around InputRecord.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Application data is kept external to the InputRecord,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * but handshake data (alert/change_cipher_spec/handshake) will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * be kept internally in the ByteArrayInputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author Brad Wetmore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
final class EngineInputRecord extends InputRecord {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private SSLEngineImpl engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * A dummy ByteBuffer we'll pass back even when the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * is stored internally.  It'll never actually be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    static private ByteBuffer tmpBB = ByteBuffer.allocate(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * Flag to tell whether the last read/parsed data resides
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * internal in the ByteArrayInputStream, or in the external
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * buffers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private boolean internalData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    EngineInputRecord(SSLEngineImpl engine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        this.engine = engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
    67
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    byte contentType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        if (internalData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            return super.contentType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            return ct_application_data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * Check if there is enough inbound data in the ByteBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * to make a inbound packet.  Look for both SSLv2 and SSLv3.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @return -1 if there are not enough bytes to tell (small header),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    int bytesInCompletePacket(ByteBuffer buf) throws SSLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
         * SSLv2 length field is in bytes 0/1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
         * SSLv3/TLS length field is in bytes 3/4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if (buf.remaining() < 5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        int pos = buf.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        byte byteZero = buf.get(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
         * If we have already verified previous packets, we can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
         * ignore the verifications steps, and jump right to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
         * determination.  Otherwise, try one last hueristic to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
         * see if it's SSL/TLS.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (formatVerified ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                (byteZero == ct_handshake) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                (byteZero == ct_alert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
             * Last sanity check that it's not a wild record
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            ProtocolVersion recordVersion =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                ProtocolVersion.valueOf(buf.get(pos + 1), buf.get(pos + 2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            // Check if too old (currently not possible)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            // or if the major version does not match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            // The actual version negotiation is in the handshaker classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            if ((recordVersion.v < ProtocolVersion.MIN.v)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    || (recordVersion.major > ProtocolVersion.MAX.major)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                throw new SSLException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    "Unsupported record version " + recordVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
             * Reasonably sure this is a V3, disable further checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
             * We can't do the same in the v2 check below, because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
             * read still needs to parse/handle the v2 clientHello.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            formatVerified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
             * One of the SSLv3/TLS message types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            len = ((buf.get(pos + 3) & 0xff) << 8) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                (buf.get(pos + 4) & 0xff) + headerSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
             * Must be SSLv2 or something unknown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
             * Check if it's short (2 bytes) or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
             * long (3) header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
             * Internals can warn about unsupported SSLv2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            boolean isShort = ((byteZero & 0x80) != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            if (isShort &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    ((buf.get(pos + 2) == 1) || buf.get(pos + 2) == 4)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                ProtocolVersion recordVersion =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    ProtocolVersion.valueOf(buf.get(pos + 3), buf.get(pos + 4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                // Check if too old (currently not possible)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                // or if the major version does not match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                // The actual version negotiation is in the handshaker classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                if ((recordVersion.v < ProtocolVersion.MIN.v)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                        || (recordVersion.major > ProtocolVersion.MAX.major)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    // if it's not SSLv2, we're out of here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    if (recordVersion.v != ProtocolVersion.SSL20Hello.v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                        throw new SSLException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                            "Unsupported record version " + recordVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                 * Client or Server Hello
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                int mask = (isShort ? 0x7f : 0x3f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                len = ((byteZero & mask) << 8) + (buf.get(pos + 1) & 0xff) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    (isShort ? 2 : 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                // Gobblygook!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                throw new SSLException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    "Unrecognized SSL message, plaintext connection?");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Pass the data down if it's internally cached, otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * do it here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * If internal data, data is decrypted internally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * If external data(app), return a new ByteBuffer with data to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   189
    ByteBuffer decrypt(MAC signer,
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   190
            CipherBox box, ByteBuffer bb) throws BadPaddingException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (internalData) {
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   193
            decrypt(signer, box);   // MAC is checked during decryption
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            return tmpBB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   197
        BadPaddingException reservedBPE = null;
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   198
        int tagLen = signer.MAClen();
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   199
        int cipheredLength = bb.remaining();
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   200
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   201
        if (!box.isNullCipher()) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   202
            // sanity check length of the ciphertext
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   203
            if (!box.sanityCheck(tagLen, cipheredLength)) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   204
                throw new BadPaddingException(
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   205
                    "ciphertext sanity check failed");
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   206
            }
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
            try {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   209
                // Note that the CipherBox.decrypt() does not change
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   210
                // the capacity of the buffer.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   211
                box.decrypt(bb, tagLen);
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   212
            } catch (BadPaddingException bpe) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   213
                // RFC 2246 states that decryption_failed should be used
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   214
                // for this purpose. However, that allows certain attacks,
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   215
                // 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
   216
                // sure to always check the MAC to avoid a timing attack
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   217
                // 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
   218
                // update in RFC 4346/5246.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   219
                //
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   220
                // Failover to message authentication code checking.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   221
                reservedBPE = bpe;
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   222
            } finally {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   223
                bb.rewind();
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
        }
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   226
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   227
        if (tagLen != 0) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   228
            int macOffset = bb.limit() - tagLen;
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   229
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   230
            // 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
   231
            // computation and comparison on the payload for both stream
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   232
            // cipher and CBC block cipher.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   233
            if (bb.remaining() < tagLen) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   234
                // negative data length, something is wrong
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   235
                if (reservedBPE == null) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   236
                    reservedBPE = new BadPaddingException("bad record");
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   237
                }
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   238
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   239
                // set offset of the dummy MAC
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   240
                macOffset = cipheredLength - tagLen;
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   241
                bb.limit(cipheredLength);
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   242
            }
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   243
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   244
            // Run MAC computation and comparison on the payload.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   245
            if (checkMacTags(contentType(), bb, signer, false)) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   246
                if (reservedBPE == null) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   247
                    reservedBPE = new BadPaddingException("bad record MAC");
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   248
                }
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   249
            }
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   250
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   251
            // Run MAC computation and comparison on the remainder.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   252
            //
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   253
            // 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
   254
            // constant time of MAC computation and comparison on each record.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   255
            if (box.isCBCMode()) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   256
                int remainingLen = calculateRemainingLen(
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   257
                                        signer, cipheredLength, macOffset);
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
                // NOTE: here we use the InputRecord.buf because I did not find
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   260
                // an effective way to work on ByteBuffer when its capacity is
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   261
                // less than remainingLen.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   262
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   263
                // NOTE: remainingLen may be bigger (less than 1 block of the
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   264
                // hash algorithm of the MAC) than the cipheredLength. However,
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   265
                // 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
   266
                // maximum buffer for every record.  We need a change here if
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   267
                // we use small buffer size in the future.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   268
                if (remainingLen > buf.length) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   269
                    // unlikely to happen, just a placehold
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   270
                    throw new RuntimeException(
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   271
                        "Internal buffer capacity error");
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   272
                }
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   273
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   274
                // 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
   275
                // 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
   276
                // 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
   277
                // 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
   278
                checkMacTags(contentType(), buf, 0, remainingLen, signer, true);
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   279
            }
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
            bb.limit(macOffset);
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   282
        }
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
        // Is it a failover?
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   285
        if (reservedBPE != null) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   286
            throw reservedBPE;
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   287
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        return bb.slice();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    /*
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   293
     * Run MAC computation and comparison
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   294
     *
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   295
     * Please DON'T change the content of the ByteBuffer parameter!
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   296
     */
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   297
    private static boolean checkMacTags(byte contentType, ByteBuffer bb,
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   298
            MAC signer, boolean isSimulated) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   299
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   300
        int tagLen = signer.MAClen();
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   301
        int lim = bb.limit();
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   302
        int macData = lim - tagLen;
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   303
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   304
        bb.limit(macData);
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   305
        byte[] hash = signer.compute(contentType, bb, isSimulated);
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   306
        if (hash == null || tagLen != hash.length) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   307
            // Something is wrong with MAC implementation.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   308
            throw new RuntimeException("Internal MAC error");
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
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   311
        bb.position(macData);
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   312
        bb.limit(lim);
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   313
        try {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   314
            int[] results = compareMacTags(bb, hash);
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   315
            return (results[0] != 0);
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   316
        } finally {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   317
            bb.rewind();
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   318
            bb.limit(macData);
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   319
        }
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   320
    }
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   321
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   322
    /*
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   323
     * A constant-time comparison of the MAC tags.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   324
     *
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   325
     * Please DON'T change the content of the ByteBuffer parameter!
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   326
     */
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   327
    private static int[] compareMacTags(ByteBuffer bb, byte[] tag) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   328
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   329
        // An array of hits is used to prevent Hotspot optimization for
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   330
        // the purpose of a constant-time check.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   331
        int[] results = {0, 0};     // {missed #, matched #}
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   332
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   333
        // The caller ensures there are enough bytes available in the buffer.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   334
        // So we won't need to check the remaining of the buffer.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   335
        for (int i = 0; i < tag.length; i++) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   336
            if (bb.get() != tag[i]) {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   337
                results[0]++;       // mismatched bytes
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   338
            } else {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   339
                results[1]++;       // matched bytes
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   340
            }
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   341
        }
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   342
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   343
        return results;
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   344
    }
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   345
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14664
diff changeset
   346
    /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * Override the actual write below.  We do things this way to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * consistent with InputRecord.  InputRecord may try to write out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * data to the peer, and *then* throw an Exception.  This forces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * data to be generated/output before the exception is ever
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 5506
diff changeset
   353
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    void writeBuffer(OutputStream s, byte [] buf, int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
         * Copy data out of buffer, it's ready to go.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        ByteBuffer netBB = (ByteBuffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            (ByteBuffer.allocate(len).put(buf, 0, len).flip());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        engine.writer.putOutboundDataSync(netBB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * Delineate or read a complete packet from src.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * If internal data (hs, alert, ccs), the data is read and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * stored internally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * If external data (app), return a new ByteBuffer which points
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * to the data to process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    ByteBuffer read(ByteBuffer srcBB) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
         * Could have a src == null/dst == null check here,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
         * but that was already checked by SSLEngine.unwrap before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
         * ever attempting to read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
         * If we have anything besides application data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
         * or if we haven't even done the initial v2 verification,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
         * we send this down to be processed by the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
         * internal cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        if (!formatVerified ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                (srcBB.get(srcBB.position()) != ct_application_data)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            internalData = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            read(new ByteBufferInputStream(srcBB), (OutputStream) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            return tmpBB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        internalData = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        int srcPos = srcBB.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        int srcLim = srcBB.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        ProtocolVersion recordVersion = ProtocolVersion.valueOf(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                srcBB.get(srcPos + 1), srcBB.get(srcPos + 2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        // Check if too old (currently not possible)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        // or if the major version does not match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        // The actual version negotiation is in the handshaker classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        if ((recordVersion.v < ProtocolVersion.MIN.v)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                || (recordVersion.major > ProtocolVersion.MAX.major)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            throw new SSLException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                "Unsupported record version " + recordVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
         * It's really application data.  How much to consume?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
         * Jump over the header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        int len = bytesInCompletePacket(srcBB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        assert(len > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        if (debug != null && Debug.isOn("packet")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                HexDumpEncoder hd = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                srcBB.limit(srcPos + len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                ByteBuffer bb = srcBB.duplicate();  // Use copy of BB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                System.out.println("[Raw read (bb)]: length = " + len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                hd.encodeBuffer(bb, System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            } catch (IOException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        // Demarcate past header to end of packet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        srcBB.position(srcPos + headerSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        srcBB.limit(srcPos + len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        // Protect remainder of buffer, create slice to actually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        // operate on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        ByteBuffer bb = srcBB.slice();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        srcBB.position(srcBB.limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        srcBB.limit(srcLim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        return bb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
}