jdk/src/share/classes/sun/security/ssl/EngineInputRecord.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2007 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    byte contentType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if (internalData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            return super.contentType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            return ct_application_data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
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
     * Check if there is enough inbound data in the ByteBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * to make a inbound packet.  Look for both SSLv2 and SSLv3.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @return -1 if there are not enough bytes to tell (small header),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    int bytesInCompletePacket(ByteBuffer buf) throws SSLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
         * SSLv2 length field is in bytes 0/1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
         * SSLv3/TLS length field is in bytes 3/4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (buf.remaining() < 5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        int pos = buf.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        byte byteZero = buf.get(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
         * If we have already verified previous packets, we can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
         * ignore the verifications steps, and jump right to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
         * determination.  Otherwise, try one last hueristic to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
         * see if it's SSL/TLS.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (formatVerified ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                (byteZero == ct_handshake) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                (byteZero == ct_alert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
             * Last sanity check that it's not a wild record
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            ProtocolVersion recordVersion =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                ProtocolVersion.valueOf(buf.get(pos + 1), buf.get(pos + 2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            // Check if too old (currently not possible)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            // or if the major version does not match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            // The actual version negotiation is in the handshaker classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            if ((recordVersion.v < ProtocolVersion.MIN.v)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    || (recordVersion.major > ProtocolVersion.MAX.major)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                throw new SSLException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    "Unsupported record version " + recordVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
             * Reasonably sure this is a V3, disable further checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
             * We can't do the same in the v2 check below, because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
             * read still needs to parse/handle the v2 clientHello.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            formatVerified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
             * One of the SSLv3/TLS message types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            len = ((buf.get(pos + 3) & 0xff) << 8) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                (buf.get(pos + 4) & 0xff) + headerSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
             * Must be SSLv2 or something unknown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
             * Check if it's short (2 bytes) or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
             * long (3) header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
             * Internals can warn about unsupported SSLv2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            boolean isShort = ((byteZero & 0x80) != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            if (isShort &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    ((buf.get(pos + 2) == 1) || buf.get(pos + 2) == 4)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                ProtocolVersion recordVersion =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    ProtocolVersion.valueOf(buf.get(pos + 3), buf.get(pos + 4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                // Check if too old (currently not possible)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                // or if the major version does not match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                // The actual version negotiation is in the handshaker classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                if ((recordVersion.v < ProtocolVersion.MIN.v)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                        || (recordVersion.major > ProtocolVersion.MAX.major)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    // if it's not SSLv2, we're out of here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    if (recordVersion.v != ProtocolVersion.SSL20Hello.v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                        throw new SSLException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                            "Unsupported record version " + recordVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    }
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
                 * Client or Server Hello
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                int mask = (isShort ? 0x7f : 0x3f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                len = ((byteZero & mask) << 8) + (buf.get(pos + 1) & 0xff) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    (isShort ? 2 : 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                // Gobblygook!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                throw new SSLException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    "Unrecognized SSL message, plaintext connection?");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Verifies and removes the MAC value.  Returns true if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * the MAC checks out OK.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * On entry:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *     position = beginning of app/MAC data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *     limit = end of MAC data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * On return:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *     position = beginning of app data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *     limit = end of app data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    boolean checkMAC(MAC signer, ByteBuffer bb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (internalData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            return checkMAC(signer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        int len = signer.MAClen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (len == 0) { // no mac
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
         * Grab the original limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        int lim = bb.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
         * Delineate the area to apply a MAC on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        int macData = lim - len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        bb.limit(macData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        byte[] mac = signer.compute(contentType(), bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if (len != mac.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            throw new RuntimeException("Internal MAC error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
         * Delineate the MAC values, position was already set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
         * by doing the compute above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
         * We could zero the MAC area, but not much useful information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
         * there anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        bb.position(macData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        bb.limit(lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                if (bb.get() != mac[i]) {  // No BB.equals(byte []); !
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
             * Position to the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            bb.rewind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            bb.limit(macData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * Pass the data down if it's internally cached, otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * do it here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * If internal data, data is decrypted internally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * If external data(app), return a new ByteBuffer with data to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    ByteBuffer decrypt(CipherBox box, ByteBuffer bb)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            throws BadPaddingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        if (internalData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            decrypt(box);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            return tmpBB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        box.decrypt(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        bb.rewind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        return bb.slice();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * Override the actual write below.  We do things this way to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * consistent with InputRecord.  InputRecord may try to write out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * data to the peer, and *then* throw an Exception.  This forces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * data to be generated/output before the exception is ever
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    void writeBuffer(OutputStream s, byte [] buf, int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
         * Copy data out of buffer, it's ready to go.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        ByteBuffer netBB = (ByteBuffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            (ByteBuffer.allocate(len).put(buf, 0, len).flip());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        engine.writer.putOutboundDataSync(netBB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * Delineate or read a complete packet from src.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * If internal data (hs, alert, ccs), the data is read and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * stored internally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * If external data (app), return a new ByteBuffer which points
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * to the data to process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    ByteBuffer read(ByteBuffer srcBB) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
         * Could have a src == null/dst == null check here,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
         * but that was already checked by SSLEngine.unwrap before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
         * ever attempting to read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
         * If we have anything besides application data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
         * or if we haven't even done the initial v2 verification,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
         * we send this down to be processed by the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
         * internal cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (!formatVerified ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                (srcBB.get(srcBB.position()) != ct_application_data)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            internalData = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            read(new ByteBufferInputStream(srcBB), (OutputStream) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            return tmpBB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        internalData = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        int srcPos = srcBB.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        int srcLim = srcBB.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        ProtocolVersion recordVersion = ProtocolVersion.valueOf(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                srcBB.get(srcPos + 1), srcBB.get(srcPos + 2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        // Check if too old (currently not possible)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        // or if the major version does not match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        // The actual version negotiation is in the handshaker classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if ((recordVersion.v < ProtocolVersion.MIN.v)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                || (recordVersion.major > ProtocolVersion.MAX.major)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            throw new SSLException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                "Unsupported record version " + recordVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
         * It's really application data.  How much to consume?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
         * Jump over the header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        int len = bytesInCompletePacket(srcBB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        assert(len > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        if (debug != null && Debug.isOn("packet")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                HexDumpEncoder hd = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                srcBB.limit(srcPos + len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                ByteBuffer bb = srcBB.duplicate();  // Use copy of BB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                System.out.println("[Raw read (bb)]: length = " + len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                hd.encodeBuffer(bb, System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            } catch (IOException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        // Demarcate past header to end of packet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        srcBB.position(srcPos + headerSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        srcBB.limit(srcPos + len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        // Protect remainder of buffer, create slice to actually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        // operate on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        ByteBuffer bb = srcBB.slice();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        srcBB.position(srcBB.limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        srcBB.limit(srcLim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        return bb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
}