jdk/src/java.base/share/classes/sun/security/ssl/HandshakeInStream.java
author ascarpino
Wed, 08 Feb 2017 12:08:28 -0800
changeset 43701 fe8c324ba97c
parent 31538 0981099a3e54
permissions -rw-r--r--
8160655: Fix denyAfter and usage types for security properties Reviewed-by: mullan, xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
     2
 * Copyright (c) 1996, 2015, 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.ByteArrayInputStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    30
import java.nio.ByteBuffer;
2
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * InputStream for handshake data, used internally only. Contains the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * handshake message buffer and methods to parse them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Once a new handshake record arrives, it is buffered in this class until
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * processed by the Handshaker. The buffer may also contain incomplete
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * handshake messages in case the message is split across multiple records.
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    41
 * Handshaker.processRecord deals with all that. It may also contain
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * handshake messages larger than the default buffer size (e.g. large
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    43
 * certificate messages). The buffer is grown dynamically to handle that.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    45
 * Note that this class only handles Handshake messages in TLS format.
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    46
 * DTLS Handshake messages should be converted into TLS format before
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    47
 * calling into this method.
2
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
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    52
// This class is used to handle plain text handshake messages.
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    53
//
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    54
public final class HandshakeInStream extends ByteArrayInputStream {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Construct the stream; we'll be accumulating hashes of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * input records using two sets of digests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    60
    HandshakeInStream() {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    61
        super(new byte[0]);     // lazy to alloacte the internal buffer
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    64
    //
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    65
    // overridden ByteArrayInputStream methods
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    66
    //
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    67
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 14229
diff changeset
    68
    @Override
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    69
    public int read(byte[] b) throws IOException {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    70
        if (super.read(b) != b.length) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            throw new SSLException("Unexpected end of handshake data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    74
        return b.length;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    77
    //
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    78
    // handshake input stream management functions
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    79
    //
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Here's an incoming record with handshake data.  Queue the contents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * it might be one or more entire messages, complete a message that's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * partly queued, or both.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    86
    void incomingRecord(ByteBuffer in) throws IOException {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    87
        int len;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    88
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    89
        // Move any unread data to the front of the buffer.
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    90
        if (pos != 0) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    91
            len = count - pos;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    92
            if (len != 0) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    93
                System.arraycopy(buf, pos, buf, 0, len);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    94
            }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    95
            pos = 0;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    96
            count = len;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    97
        }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    98
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
    99
        // Grow buffer if needed.
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   100
        len = in.remaining() + count;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   101
        if (buf.length < len) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   102
            byte[] newbuf = new byte[len];
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   103
            if (count != 0) {
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   104
                System.arraycopy(buf, 0, newbuf, 0, count);
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   105
            }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   106
            buf = newbuf;
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   107
        }
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   108
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   109
        // Append the incoming record to the buffer
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   110
        in.get(buf, count, in.remaining());
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   111
        count = len;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   114
    //
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    // Message parsing methods
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   116
    //
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Read 8, 16, 24, and 32 bit SSL integer data types, encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * in standard big-endian form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    int getInt8() throws IOException {
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   123
        verifyLength(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    int getInt16() throws IOException {
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   128
        verifyLength(2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        return (getInt8() << 8) | getInt8();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    int getInt24() throws IOException {
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   133
        verifyLength(3);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        return (getInt8() << 16) | (getInt8() << 8) | getInt8();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    int getInt32() throws IOException {
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   138
        verifyLength(4);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        return (getInt8() << 24) | (getInt8() << 16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
             | (getInt8() << 8) | getInt8();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Read byte vectors with 8, 16, and 24 bit length encodings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    byte[] getBytes8() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        int len = getInt8();
14212
faa4afc89a09 7186286: TLS implementation to better adhere to RFC
xuelei
parents: 5506
diff changeset
   148
        verifyLength(len);
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30904
diff changeset
   149
        byte[] b = new byte[len];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   151
        read(b);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 2
diff changeset
   155
    public byte[] getBytes16() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        int len = getInt16();
14212
faa4afc89a09 7186286: TLS implementation to better adhere to RFC
xuelei
parents: 5506
diff changeset
   157
        verifyLength(len);
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30904
diff changeset
   158
        byte[] b = new byte[len];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   160
        read(b);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    byte[] getBytes24() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        int len = getInt24();
14212
faa4afc89a09 7186286: TLS implementation to better adhere to RFC
xuelei
parents: 5506
diff changeset
   166
        verifyLength(len);
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30904
diff changeset
   167
        byte[] b = new byte[len];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   169
        read(b);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
14212
faa4afc89a09 7186286: TLS implementation to better adhere to RFC
xuelei
parents: 5506
diff changeset
   173
    // Is a length greater than available bytes in the record?
faa4afc89a09 7186286: TLS implementation to better adhere to RFC
xuelei
parents: 5506
diff changeset
   174
    private void verifyLength(int len) throws SSLException {
faa4afc89a09 7186286: TLS implementation to better adhere to RFC
xuelei
parents: 5506
diff changeset
   175
        if (len > available()) {
30904
ec0224270f90 8043758: Datagram Transport Layer Security (DTLS)
xuelei
parents: 25859
diff changeset
   176
            throw new SSLException("Unexpected end of handshake data");
14212
faa4afc89a09 7186286: TLS implementation to better adhere to RFC
xuelei
parents: 5506
diff changeset
   177
        }
faa4afc89a09 7186286: TLS implementation to better adhere to RFC
xuelei
parents: 5506
diff changeset
   178
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
}