jdk/src/share/classes/sun/security/ssl/HandshakeInStream.java
author lana
Tue, 23 Oct 2012 11:29:53 -0700
changeset 14229 40fbffe104bd
parent 14194 971f46db533d
parent 14212 faa4afc89a09
child 14664 e71aa0962e70
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14212
faa4afc89a09 7186286: TLS implementation to better adhere to RFC
xuelei
parents: 5506
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: 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
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.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.MessageDigest;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * InputStream for handshake data, used internally only. Contains the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * handshake message buffer and methods to parse them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Once a new handshake record arrives, it is buffered in this class until
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * processed by the Handshaker. The buffer may also contain incomplete
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * handshake messages in case the message is split across multiple records.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Handshaker.process_record deals with all that. It may also contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * handshake messages larger than the default buffer size (e.g. large
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * certificate messages). The buffer is grown dynamically to handle that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * (see InputRecord.queueHandshake()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Note that the InputRecord used as a buffer here is separate from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * AppInStream.r, which is where data from the socket is initially read
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * into. This is because once the initial handshake has been completed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * handshake and application data messages may be interleaved arbitrarily
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * and must be processed independently.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 */
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 2
diff changeset
    55
public class HandshakeInStream extends InputStream {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    InputRecord r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Construct the stream; we'll be accumulating hashes of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * input records using two sets of digests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    HandshakeInStream(HandshakeHash handshakeHash) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        r = new InputRecord();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        r.setHandshakeHash(handshakeHash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // overridden InputStream methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Return the number of bytes available for read().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Note that this returns the bytes remaining in the buffer, not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * the bytes remaining in the current handshake message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public int available() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        return r.available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Get a byte of handshake data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        int n = r.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (n == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            throw new SSLException("Unexpected end of handshake data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Get a bunch of bytes of handshake data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public int read(byte b [], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        // we read from a ByteArrayInputStream, it always returns the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        // data in a single read if enough is available
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        int n = r.read(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (n != len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            throw new SSLException("Unexpected end of handshake data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Skip some handshake data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public long skip(long n) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        return r.skip(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Mark/ reset code, implemented using InputRecord mark/ reset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Note that it currently provides only a limited mark functionality
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * and should be used with care (once a new handshake record has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * read, data that has already been consumed is lost even if marked).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public void mark(int readlimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        r.mark(readlimit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
14194
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   124
    @Override
971f46db533d 7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents: 5506
diff changeset
   125
    public void reset() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        r.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public boolean markSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    // handshake management functions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Here's an incoming record with handshake data.  Queue the contents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * it might be one or more entire messages, complete a message that's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * partly queued, or both.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    void incomingRecord(InputRecord in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        r.queueHandshake(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Hash any data we've consumed but not yet hashed.  Useful mostly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * for processing client certificate messages (so we can check the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * immediately following cert verify message) and finished messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * (so we can compute our own finished message).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    void digestNow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        r.doHashes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Do more than skip that handshake data ... totally ignore it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * The difference is that the data does not get hashed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    void ignore(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        r.ignore(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    // Message parsing methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Read 8, 16, 24, and 32 bit SSL integer data types, encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * in standard big-endian form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    int getInt8() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        return read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    int getInt16() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return (getInt8() << 8) | getInt8();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    int getInt24() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return (getInt8() << 16) | (getInt8() << 8) | getInt8();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    int getInt32() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return (getInt8() << 24) | (getInt8() << 16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
             | (getInt8() << 8) | getInt8();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Read byte vectors with 8, 16, and 24 bit length encodings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    byte[] getBytes8() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        int len = getInt8();
14212
faa4afc89a09 7186286: TLS implementation to better adhere to RFC
xuelei
parents: 5506
diff changeset
   194
        verifyLength(len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        byte b[] = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        read(b, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 2
diff changeset
   201
    public byte[] getBytes16() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        int len = getInt16();
14212
faa4afc89a09 7186286: TLS implementation to better adhere to RFC
xuelei
parents: 5506
diff changeset
   203
        verifyLength(len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        byte b[] = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        read(b, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    byte[] getBytes24() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        int len = getInt24();
14212
faa4afc89a09 7186286: TLS implementation to better adhere to RFC
xuelei
parents: 5506
diff changeset
   212
        verifyLength(len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        byte b[] = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        read(b, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
14212
faa4afc89a09 7186286: TLS implementation to better adhere to RFC
xuelei
parents: 5506
diff changeset
   219
    // Is a length greater than available bytes in the record?
faa4afc89a09 7186286: TLS implementation to better adhere to RFC
xuelei
parents: 5506
diff changeset
   220
    private void verifyLength(int len) throws SSLException {
faa4afc89a09 7186286: TLS implementation to better adhere to RFC
xuelei
parents: 5506
diff changeset
   221
        if (len > available()) {
faa4afc89a09 7186286: TLS implementation to better adhere to RFC
xuelei
parents: 5506
diff changeset
   222
            throw new SSLException(
faa4afc89a09 7186286: TLS implementation to better adhere to RFC
xuelei
parents: 5506
diff changeset
   223
                        "Not enough data to fill declared vector size");
faa4afc89a09 7186286: TLS implementation to better adhere to RFC
xuelei
parents: 5506
diff changeset
   224
        }
faa4afc89a09 7186286: TLS implementation to better adhere to RFC
xuelei
parents: 5506
diff changeset
   225
    }
faa4afc89a09 7186286: TLS implementation to better adhere to RFC
xuelei
parents: 5506
diff changeset
   226
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
}