jdk/src/share/classes/sun/security/ssl/OutputRecord.java
author wetmore
Tue, 12 Mar 2013 15:31:49 -0700
changeset 16067 36055e4b5305
parent 16045 9d08c3b9a6a0
child 16126 aad71cf676d7
permissions -rw-r--r--
8009925: Back out AEAD CipherSuites temporarily Reviewed-by: valeriep
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
12428
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
     2
 * Copyright (c) 1996, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.*;
12428
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
    31
import java.util.Arrays;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.net.ssl.SSLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.misc.HexDumpEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * SSL 3.0 records, as written to a TCP stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Each record has a message area that starts out with data supplied by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * application.  It may grow/shrink due to compression and will be modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * in place for mac-ing and encryption.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Handshake records have additional needs, notably accumulation of a set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * of hashes which are used to establish that handshaking was done right.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Handshake records usually have several handshake messages each, and we
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * need message-level control over what's hashed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
class OutputRecord extends ByteArrayOutputStream implements Record {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private HandshakeHash       handshakeHash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private int                 lastHashed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private boolean             firstMessage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    final private byte          contentType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // current protocol version, sent as record version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    ProtocolVersion     protocolVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // version for the ClientHello message. Only relevant if this is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // client handshake record. If set to ProtocolVersion.SSL20Hello,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // the V3 client hello is converted to V2 format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private ProtocolVersion     helloVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /* Class and subclass dynamic debugging support */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    static final Debug debug = Debug.getInstance("ssl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Default constructor makes a record supporting the maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * SSL record size.  It allocates the header bytes directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @param type the content type for the record
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    OutputRecord(byte type, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        super(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        this.protocolVersion = ProtocolVersion.DEFAULT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        this.helloVersion = ProtocolVersion.DEFAULT_HELLO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        firstMessage = true;
16067
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
    80
        count = headerSize;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        contentType = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        lastHashed = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    OutputRecord(byte type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        this(type, recordSize(type));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Get the size of the buffer we need for records of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private static int recordSize(byte type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if ((type == ct_change_cipher_spec) || (type == ct_alert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            return maxAlertRecordSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            return maxRecordSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Updates the SSL version of this record.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    synchronized void setVersion(ProtocolVersion protocolVersion) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        this.protocolVersion = protocolVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Updates helloVersion of this record.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    synchronized void setHelloVersion(ProtocolVersion helloVersion) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        this.helloVersion = helloVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Reset the record so that it can be refilled, starting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * immediately after the header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 12428
diff changeset
   119
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public synchronized void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        super.reset();
16067
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   122
        count = headerSize;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        lastHashed = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * For handshaking, we need to be able to hash every byte above the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * record marking layer.  This is where we're guaranteed to see those
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * bytes, so this is where we can hash them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    void setHandshakeHash(HandshakeHash handshakeHash) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        assert(contentType == ct_handshake);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        this.handshakeHash = handshakeHash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * We hash (the plaintext) on demand.  There is one place where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * we want to access the hash in the middle of a record:  client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * cert message gets hashed, and part of the same record is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * client cert verify message which uses that hash.  So we track
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * how much of each record we've hashed so far.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    void doHashes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        int len = count - lastHashed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            hashInternal(buf, lastHashed, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            lastHashed = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Need a helper function so we can hash the V2 hello correctly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private void hashInternal(byte buf [], int offset, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (debug != null && Debug.isOn("data")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                HexDumpEncoder hd = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                System.out.println("[write] MD5 and SHA1 hashes:  len = "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    + len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                hd.encodeBuffer(new ByteArrayInputStream(buf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    lastHashed, len), System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            } catch (IOException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        handshakeHash.update(buf, lastHashed, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        lastHashed = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Return true iff the record is empty -- to avoid doing the work
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * of sending empty records over the network.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    boolean isEmpty() {
16067
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   176
        return count == headerSize;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
100
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   179
    /*
16067
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   180
     * Return true if the record is of a given alert.
100
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   181
     */
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   182
    boolean isAlert(byte description) {
16067
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   183
        // An alert is defined with a two bytes struct,
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   184
        // {byte level, byte description}, following after the header bytes.
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   185
        if (count > (headerSize + 1) && contentType == ct_alert) {
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   186
            return buf[headerSize + 1] == description;
100
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   187
        }
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   188
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   189
        return false;
01ef29ca378f 6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents: 2
diff changeset
   190
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /*
16067
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   193
     * Compute the MAC and append it to this record.  In case we
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   194
     * are automatically flushing a handshake stream, make sure we
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   195
     * have hashed the message first.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
16067
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   197
    void addMAC(MAC signer) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        // when we support compression, hashing can't go here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        // since it'll need to be done on the uncompressed data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        // and the MAC applies to the compressed data.
16067
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   202
        //
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (contentType == ct_handshake) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            doHashes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
16067
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   206
        if (signer.MAClen() != 0) {
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   207
            byte[] hash = signer.compute(contentType, buf,
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   208
                    headerSize, count - headerSize);
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   209
            write(hash);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /*
16067
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   214
     * Encrypt ... length may grow due to block cipher padding
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   215
     */
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   216
    void encrypt(CipherBox box) {
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   217
        int len = count - headerSize;
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   218
        count = headerSize + box.encrypt(buf, headerSize, len);
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   219
    }
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   220
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   221
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   222
    /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * Tell how full the buffer is ... for filling it with application or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * handshake data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    final int availableDataBytes() {
16067
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   227
        int dataSize = count - headerSize;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        return maxDataSize - dataSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /*
12428
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   232
     * Increases the capacity if necessary to ensure that it can hold
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   233
     * at least the number of elements specified by the minimum
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   234
     * capacity argument.
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   235
     *
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   236
     * Note that the increased capacity is only can be used for held
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   237
     * record buffer. Please DO NOT update the availableDataBytes()
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   238
     * according to the expended buffer capacity.
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   239
     *
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   240
     * @see availableDataBytes()
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   241
     */
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   242
    private void ensureCapacity(int minCapacity) {
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   243
        // overflow-conscious code
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   244
        if (minCapacity > buf.length) {
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   245
            buf = Arrays.copyOf(buf, minCapacity);
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   246
        }
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   247
    }
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   248
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   249
    /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * Return the type of SSL record that's buffered here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    final byte contentType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        return contentType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * Write the record out on the stream.  Note that you must have (in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * order) compressed the data, appended the MAC, and encrypted it in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * order for the record to be understood by the other end.  (Some of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * those steps will be null early in handshaking.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * Note that this does no locking for the connection, it's required
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * that synchronization be done elsewhere.  Also, this does its work
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * in a single low level write, for efficiency.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
12428
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   266
    void write(OutputStream s, boolean holdRecord,
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   267
            ByteArrayOutputStream heldRecordBuffer) throws IOException {
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   268
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
         * Don't emit content-free records.  (Even change cipher spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
         * messages have a byte of data!)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
         */
16067
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   273
        if (count == headerSize) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
16067
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   277
        int length = count - headerSize;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        // "should" really never write more than about 14 Kb...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        if (length < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            throw new SSLException("output record size too small: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                + length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        if (debug != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                && (Debug.isOn("record") || Debug.isOn("handshake"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            if ((debug != null && Debug.isOn("record"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    || contentType() == ct_change_cipher_spec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                System.out.println(Thread.currentThread().getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    // v3.0/v3.1 ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    + ", WRITE: " + protocolVersion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    + " " + InputRecord.contentName(contentType())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    + ", length = " + length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
         * If this is the initial ClientHello on this connection and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
         * we're not trying to resume a (V3) session then send a V2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
         * ClientHello instead so we can detect V2 servers cleanly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
         if (firstMessage && useV2Hello()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            byte[] v3Msg = new byte[length - 4];
16067
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   302
            System.arraycopy(buf, headerSize + 4, v3Msg, 0, v3Msg.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            V3toV2ClientHello(v3Msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            handshakeHash.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            lastHashed = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            doHashes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            if (debug != null && Debug.isOn("record"))  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    Thread.currentThread().getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                    + ", WRITE: SSLv2 client hello message"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                    + ", length = " + (count - 2)); // 2 byte SSLv2 header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
             * Fill out the header, write it and the message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
             */
16067
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   317
            buf[0] = contentType;
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   318
            buf[1] = protocolVersion.major;
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   319
            buf[2] = protocolVersion.minor;
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   320
            buf[3] = (byte)(length >> 8);
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   321
            buf[4] = (byte)(length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        firstMessage = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
12428
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   325
        /*
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   326
         * The upper levels may want us to delay sending this packet so
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   327
         * multiple TLS Records can be sent in one (or more) TCP packets.
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   328
         * If so, add this packet to the heldRecordBuffer.
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   329
         *
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   330
         * NOTE:  all writes have been synchronized by upper levels.
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   331
         */
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   332
        int debugOffset = 0;
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   333
        if (holdRecord) {
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   334
            /*
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   335
             * If holdRecord is true, we must have a heldRecordBuffer.
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   336
             *
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   337
             * Don't worry about the override of writeBuffer(), because
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   338
             * when holdRecord is true, the implementation in this class
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   339
             * will be used.
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   340
             */
16067
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   341
            writeBuffer(heldRecordBuffer, buf, 0, count, debugOffset);
12428
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   342
        } else {
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   343
            // It's time to send, do we have buffered data?
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   344
            // May or may not have a heldRecordBuffer.
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   345
            if (heldRecordBuffer != null && heldRecordBuffer.size() > 0) {
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   346
                int heldLen = heldRecordBuffer.size();
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   347
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   348
                // Ensure the capacity of this buffer.
16067
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   349
                ensureCapacity(count + heldLen);
12428
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   350
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   351
                // Slide everything in the buffer to the right.
16067
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   352
                System.arraycopy(buf, 0, buf, heldLen, count);
12428
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   353
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   354
                // Prepend the held record to the buffer.
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   355
                System.arraycopy(
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   356
                    heldRecordBuffer.toByteArray(), 0, buf, 0, heldLen);
16067
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   357
                count += heldLen;
12428
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   358
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   359
                // Clear the held buffer.
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   360
                heldRecordBuffer.reset();
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   361
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   362
                // The held buffer has been dumped, set the debug dump offset.
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   363
                debugOffset = heldLen;
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   364
            }
16067
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   365
            writeBuffer(s, buf, 0, count, debugOffset);
12428
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   366
        }
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   367
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * Actually do the write here.  For SSLEngine's HS data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * we'll override this method and let it take the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     */
12428
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   376
    void writeBuffer(OutputStream s, byte [] buf, int off, int len,
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   377
            int debugOffset) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        s.write(buf, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        s.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
12428
e9feb65d37fa 7157903: JSSE client sockets are very slow
wetmore
parents: 6856
diff changeset
   381
        // Output only the record from the specified debug offset.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        if (debug != null && Debug.isOn("packet")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                HexDumpEncoder hd = new HexDumpEncoder();
16067
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   385
                ByteBuffer bb = ByteBuffer.wrap(
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   386
                        buf, off + debugOffset, len - debugOffset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                System.out.println("[Raw write]: length = " +
16067
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   389
                    bb.remaining());
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   390
                hd.encodeBuffer(bb, System.out);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            } catch (IOException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * Return whether the buffer contains a ClientHello message that should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * be converted to V2 format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    private boolean useV2Hello() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        return firstMessage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            && (helloVersion == ProtocolVersion.SSL20Hello)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            && (contentType == ct_handshake)
16067
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   403
            && (buf[5] == HandshakeMessage.ht_client_hello)
36055e4b5305 8009925: Back out AEAD CipherSuites temporarily
wetmore
parents: 16045
diff changeset
   404
            && (buf[headerSize + 4+2+32] == 0); // V3 session ID is empty
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * Detect "old" servers which are capable of SSL V2.0 protocol ... for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * example, Netscape Commerce 1.0 servers.  The V3 message is in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * header and the bytes passed as parameter.  This routine translates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * the V3 message into an equivalent V2 one.
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   412
     *
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   413
     * Note that the translation will strip off all hello extensions as
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   414
     * SSL V2.0 does not support hello extension.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    private void V3toV2ClientHello(byte v3Msg []) throws SSLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        int v3SessionIdLenOffset = 2 + 32; // version + nonce
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        int v3SessionIdLen = v3Msg[v3SessionIdLenOffset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        int v3CipherSpecLenOffset = v3SessionIdLenOffset + 1 + v3SessionIdLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        int v3CipherSpecLen = ((v3Msg[v3CipherSpecLenOffset] & 0xff) << 8) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
          (v3Msg[v3CipherSpecLenOffset + 1] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        int cipherSpecs = v3CipherSpecLen / 2; // 2 bytes each in V3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
         * Copy over the cipher specs. We don't care about actually translating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
         * them for use with an actual V2 server since we only talk V3.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
         * Therefore, just copy over the V3 cipher spec values with a leading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
         * 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        int v3CipherSpecOffset = v3CipherSpecLenOffset + 2; // skip length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        int v2CipherSpecLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        count = 11;
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   433
        boolean containsRenegoInfoSCSV = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        for (int i = 0; i < cipherSpecs; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            byte byte1, byte2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            byte1 = v3Msg[v3CipherSpecOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            byte2 = v3Msg[v3CipherSpecOffset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            v2CipherSpecLen += V3toV2CipherSuite(byte1, byte2);
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   440
            if (!containsRenegoInfoSCSV &&
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   441
                        byte1 == (byte)0x00 && byte2 == (byte)0xFF) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   442
                containsRenegoInfoSCSV = true;
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   443
            }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   444
        }
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   445
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   446
        if (!containsRenegoInfoSCSV) {
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   447
            v2CipherSpecLen += V3toV2CipherSuite((byte)0x00, (byte)0xFF);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
         * Build the first part of the V3 record header from the V2 one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
         * that's now buffered up.  (Lengths are fixed up later).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        buf[2] = HandshakeMessage.ht_client_hello;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        buf[3] = v3Msg[0];      // major version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        buf[4] = v3Msg[1];      // minor version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        buf[5] = (byte)(v2CipherSpecLen >>> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        buf[6] = (byte)v2CipherSpecLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        buf[7] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        buf[8] = 0;             // always no session
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        buf[9] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        buf[10] = 32;           // nonce length (always 32 in V3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
         * Copy in the nonce.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        System.arraycopy(v3Msg, 2, buf, count, 32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        count += 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
         * Set the length of the message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        count -= 2; // don't include length field itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        buf[0] = (byte)(count >>> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        buf[0] |= 0x80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        buf[1] = (byte)(count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        count += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * Mappings from V3 cipher suite encodings to their pure V2 equivalents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * This is taken from the SSL V3 specification, Appendix E.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    private static int[] V3toV2CipherMap1 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        {-1, -1, -1, 0x02, 0x01, -1, 0x04, 0x05, -1, 0x06, 0x07};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    private static int[] V3toV2CipherMap3 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        {-1, -1, -1, 0x80, 0x80, -1, 0x80, 0x80, -1, 0x40, 0xC0};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * See which matching pure-V2 cipher specs we need to include.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * We are including these not because we are actually prepared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * to talk V2 but because the Oracle Web Server insists on receiving
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * at least 1 "pure V2" cipher suite that it supports and returns an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * illegal_parameter alert unless one is present. Rather than mindlessly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * claiming to implement all documented pure V2 cipher suites the code below
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * just claims to implement the V2 cipher suite that is "equivalent"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * in terms of cipher algorithm & exportability with the actual V3 cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * suite that we do support.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    private int V3toV2CipherSuite(byte byte1, byte byte2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        buf[count++] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        buf[count++] = byte1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        buf[count++] = byte2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        if (((byte2 & 0xff) > 0xA) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                (V3toV2CipherMap1[byte2] == -1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            return 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        buf[count++] = (byte)V3toV2CipherMap1[byte2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        buf[count++] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        buf[count++] = (byte)V3toV2CipherMap3[byte2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        return 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
}