jdk/src/share/classes/sun/security/ssl/EngineOutputRecord.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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.net.ssl.SSLException;
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
 * A OutputRecord class extension which uses external ByteBuffers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * or the internal ByteArrayOutputStream for data manipulations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Instead of rewriting this entire class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * to use ByteBuffers, we leave things intact, so handshake, CCS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * and alerts will continue to use the internal buffers, but application
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * data will use external buffers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author Brad Wetmore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
final class EngineOutputRecord extends OutputRecord {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private EngineWriter writer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private boolean finishedMsg = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * All handshake hashing is done by the superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * Default constructor makes a record supporting the maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * SSL record size.  It allocates the header bytes directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * @param type the content type for the record
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    EngineOutputRecord(byte type, SSLEngineImpl engine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        super(type, recordSize(type));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        writer = engine.writer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Get the size of the buffer we need for records of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Application data buffers will provide their own byte buffers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * and will not use the internal byte caching.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private static int recordSize(byte type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        case ct_change_cipher_spec:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        case ct_alert:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            return maxAlertRecordSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        case ct_handshake:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            return maxRecordSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        case ct_application_data:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        throw new RuntimeException("Unknown record type: " + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    void setFinishedMsg() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        finishedMsg = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public void flush() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        finishedMsg = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    boolean isFinishedMsg() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        return finishedMsg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Calculate the MAC value, storing the result either in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * the internal buffer, or at the end of the destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * ByteBuffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * We assume that the higher levels have assured us enough
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * room, otherwise we'll indirectly throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * BufferOverFlowException runtime exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * position should equal limit, and points to the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * free spot.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private void addMAC(MAC signer, ByteBuffer bb)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (signer.MAClen() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            byte[] hash = signer.compute(contentType(), bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
             * position was advanced to limit in compute above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
             * Mark next area as writable (above layers should have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
             * established that we have plenty of room), then write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
             * out the hash.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            bb.limit(bb.limit() + hash.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            bb.put(hash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Encrypt a ByteBuffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * We assume that the higher levels have assured us enough
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * room for the encryption (plus padding), otherwise we'll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * indirectly throw a BufferOverFlowException runtime exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * position and limit will be the same, and points to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * next free spot.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    void encrypt(CipherBox box, ByteBuffer bb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        box.encrypt(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Override the actual write below.  We do things this way to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * consistent with InputRecord.  InputRecord may try to write out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * data to the peer, and *then* throw an Exception.  This forces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * data to be generated/output before the exception is ever
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    void writeBuffer(OutputStream s, byte [] buf, int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
         * Copy data out of buffer, it's ready to go.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        ByteBuffer netBB = (ByteBuffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            ByteBuffer.allocate(len).put(buf, 0, len).flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        writer.putOutboundData(netBB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Main method for writing non-application data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * We MAC/encrypt, then send down for processing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    void write(MAC writeMAC, CipherBox writeCipher) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
         * Sanity check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        switch (contentType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        case ct_change_cipher_spec:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        case ct_alert:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        case ct_handshake:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            throw new RuntimeException("unexpected byte buffers");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
         * Don't bother to really write empty records.  We went this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
         * far to drive the handshake machinery, for correctness; not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
         * writing empty records improves performance by cutting CPU
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
         * time and network resource usage.  Also, some protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
         * implementations are fragile and don't like to see empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
         * records, so this increases robustness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
         * (Even change cipher spec messages have a byte of data!)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (!isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            // compress();              // eventually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            addMAC(writeMAC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            encrypt(writeCipher);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            write((OutputStream)null);  // send down for processing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Main wrap/write driver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    void write(EngineArgs ea, MAC writeMAC, CipherBox writeCipher)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
         * sanity check to make sure someone didn't inadvertantly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
         * send us an impossible combination we don't know how
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
         * to process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        assert(contentType() == ct_application_data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
         * Have we set the MAC's yet?  If not, we're not ready
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
         * to process application data yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        if (writeMAC == MAC.NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
         * Don't bother to really write empty records.  We went this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
         * far to drive the handshake machinery, for correctness; not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
         * writing empty records improves performance by cutting CPU
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
         * time and network resource usage.  Also, some protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
         * implementations are fragile and don't like to see empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
         * records, so this increases robustness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        int length = Math.min(ea.getAppRemaining(), maxDataSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
         * Copy out existing buffer values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        ByteBuffer dstBB = ea.netData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        int dstPos = dstBB.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        int dstLim = dstBB.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
         * Where to put the data.  Jump over the header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
         * Don't need to worry about SSLv2 rewrites, if we're here,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
         * that's long since done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        int dstData = dstPos + headerSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        dstBB.position(dstData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        ea.gather(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
         * "flip" but skip over header again, add MAC & encrypt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
         * addMAC will expand the limit to reflect the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
         * data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        dstBB.limit(dstBB.position());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        dstBB.position(dstData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        addMAC(writeMAC, dstBB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
         * Encrypt may pad, so again the limit may have changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        dstBB.limit(dstBB.position());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        dstBB.position(dstData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        encrypt(writeCipher, dstBB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        if (debug != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                && (Debug.isOn("record") || Debug.isOn("handshake"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            if ((debug != null && Debug.isOn("record"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    || contentType() == ct_change_cipher_spec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                System.out.println(Thread.currentThread().getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    // v3.0/v3.1 ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    + ", WRITE: " + protocolVersion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    + " " + InputRecord.contentName(contentType())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    + ", length = " + length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        int packetLength = dstBB.limit() - dstData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
         * Finish out the record header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        dstBB.put(dstPos, contentType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        dstBB.put(dstPos + 1, protocolVersion.major);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        dstBB.put(dstPos + 2, protocolVersion.minor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        dstBB.put(dstPos + 3, (byte)(packetLength >> 8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        dstBB.put(dstPos + 4, (byte)packetLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
         * Position was already set by encrypt() above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        dstBB.limit(dstLim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
}