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