src/java.base/share/classes/sun/security/ssl/HandshakeOutStream.java
author xuelei
Mon, 25 Jun 2018 13:41:39 -0700
changeset 50768 68fa3d4026ea
parent 47216 71c04702a3d5
child 51407 910f7b56592f
permissions -rw-r--r--
8196584: TLS 1.3 Implementation Reviewed-by: ascarpino, coffeys, dfuchs, jjiang, jnimeh, mullan, rhalade, ssahoo, valeriep, weijun, wetmore, xuelei Contributed-by: Adam Petcher <adam.petcher@oracle.com>, Amanda Jiang <amanda.jiang@oracle.com>, Anthony Scarpino <anthony.scarpino@oracle.com>, Bradford Wetmore <bradford.wetmore@oracle.com>, Jamil Nimeh <jamil.j.nimeh@oracle.com>, John Jiang <sha.jiang@oracle.com>, Rajan Halade <rajan.halade@oracle.com>, Sibabrata Sahoo <sibabrata.sahoo@oracle.com>, Valerie Peng <valerie.peng@oracle.com>, Weijun Wang <weijun.wang@oracle.com>, Xuelei Fan <xuelei.fan@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
     2
 * Copyright (c) 1996, 2018, 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: 4236
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: 4236
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: 4236
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4236
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4236
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
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    28
import java.io.ByteArrayOutputStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Output stream for handshake data.  This is used only internally
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * to the SSL classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * MT note:  one thread at a time is presumed be writing handshake
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * messages, but (after initial connection setup) it's possible to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * have other threads reading/writing application data.  It's the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * SSLSocketImpl class that synchronizes record writes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author  David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    42
public class HandshakeOutStream extends ByteArrayOutputStream {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    44
    OutputRecord outputRecord;      // May be null if not actually used to
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    45
                                    // output handshake message records.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    47
    HandshakeOutStream(OutputRecord outputRecord) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    48
        super();
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    49
        this.outputRecord = outputRecord;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    52
    // Complete a handshakin message writing. Called by HandshakeMessage.
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    53
    void complete() throws IOException {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    54
        if (size() < 4) {       // 4: handshake message header size
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    55
            // internal_error alert will be triggered
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    56
            throw new RuntimeException("handshake message is not available");
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    57
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    59
        if (outputRecord != null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    60
            outputRecord.encodeHandshake(buf, 0, count);
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    61
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    62
            // reset the byte array output stream
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    63
            reset();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    64
        }   // otherwise, the handshake outstream is temporarily used only.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    67
    //
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    68
    // overridden ByteArrayOutputStream methods
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    69
    //
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14004
diff changeset
    71
    @Override
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    72
    public void write(byte[] b, int off, int len) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    73
        // The maximum fragment size is 24 bytes.
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    74
        checkOverflow(len, Record.OVERFLOW_OF_INT24);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    75
        super.write(b, off, len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14004
diff changeset
    78
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public void flush() throws IOException {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    80
        if (outputRecord != null) {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    81
            outputRecord.flush();
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    82
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    85
    //
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    86
    // handshake output stream management functions
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    87
    //
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Put integers encoded in standard 8, 16, 24, and 32 bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * big endian formats. Note that OutputStream.write(int) only
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * writes the least significant 8 bits and ignores the rest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    void putInt8(int i) throws IOException {
14004
611031f93e76 7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents: 5506
diff changeset
    95
        checkOverflow(i, Record.OVERFLOW_OF_INT08);
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    96
        super.write(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    void putInt16(int i) throws IOException {
14004
611031f93e76 7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents: 5506
diff changeset
   100
        checkOverflow(i, Record.OVERFLOW_OF_INT16);
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   101
        super.write(i >> 8);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   102
        super.write(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    void putInt24(int i) throws IOException {
14004
611031f93e76 7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents: 5506
diff changeset
   106
        checkOverflow(i, Record.OVERFLOW_OF_INT24);
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   107
        super.write(i >> 16);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   108
        super.write(i >> 8);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   109
        super.write(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   112
    void putInt32(int i) throws IOException {
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   113
        super.write(i >> 24);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   114
        super.write(i >> 16);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   115
        super.write(i >> 8);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   116
        super.write(i);
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   117
    }
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
   118
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Put byte arrays with length encoded as 8, 16, 24 bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * integers in big-endian format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   123
    void putBytes8(byte[] b) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            putInt8(0);
14004
611031f93e76 7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents: 5506
diff changeset
   126
        } else {
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   127
            putInt8(b.length);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   128
            super.write(b, 0, b.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30904
diff changeset
   132
    public void putBytes16(byte[] b) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            putInt16(0);
14004
611031f93e76 7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents: 5506
diff changeset
   135
        } else {
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   136
            putInt16(b.length);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   137
            super.write(b, 0, b.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30904
diff changeset
   141
    void putBytes24(byte[] b) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            putInt24(0);
14004
611031f93e76 7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents: 5506
diff changeset
   144
        } else {
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   145
            putInt24(b.length);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   146
            super.write(b, 0, b.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
14004
611031f93e76 7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents: 5506
diff changeset
   149
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   150
    /*
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   151
     * Does the specified length overflow the limitation?
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   152
     */
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   153
    private static void checkOverflow(int length, int limit) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   154
        if (length >= limit) {
14004
611031f93e76 7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents: 5506
diff changeset
   155
            // internal_error alert will be triggered
611031f93e76 7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents: 5506
diff changeset
   156
            throw new RuntimeException(
611031f93e76 7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents: 5506
diff changeset
   157
                    "Field length overflow, the field length (" +
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   158
                    length + ") should be less than " + limit);
14004
611031f93e76 7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents: 5506
diff changeset
   159
        }
611031f93e76 7200295: CertificateRequest message is wrapping when using large numbers of Certs
xuelei
parents: 5506
diff changeset
   160
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
}