jdk/src/share/classes/sun/security/ssl/HandshakeInStream.java
author smarks
Fri, 14 Jan 2011 15:31:45 -0800
changeset 7990 57019dc81b66
parent 5506 202f599c92aa
child 14212 faa4afc89a09
child 14194 971f46db533d
permissions -rw-r--r--
7012003: diamond conversion for ssl Reviewed-by: wetmore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4236
diff changeset
     2
 * Copyright (c) 1996, 2009, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        r.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public boolean markSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    // handshake management functions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Here's an incoming record with handshake data.  Queue the contents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * it might be one or more entire messages, complete a message that's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * partly queued, or both.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    void incomingRecord(InputRecord in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        r.queueHandshake(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Hash any data we've consumed but not yet hashed.  Useful mostly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * for processing client certificate messages (so we can check the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * immediately following cert verify message) and finished messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * (so we can compute our own finished message).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    void digestNow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        r.doHashes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Do more than skip that handshake data ... totally ignore it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * The difference is that the data does not get hashed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    void ignore(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        r.ignore(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    // Message parsing methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * Read 8, 16, 24, and 32 bit SSL integer data types, encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * in standard big-endian form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    int getInt8() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    int getInt16() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return (getInt8() << 8) | getInt8();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    int getInt24() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        return (getInt8() << 16) | (getInt8() << 8) | getInt8();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    int getInt32() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        return (getInt8() << 24) | (getInt8() << 16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
             | (getInt8() << 8) | getInt8();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Read byte vectors with 8, 16, and 24 bit length encodings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    byte[] getBytes8() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        int len = getInt8();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        byte b[] = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        read(b, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents: 2
diff changeset
   199
    public byte[] getBytes16() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        int len = getInt16();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        byte b[] = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        read(b, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    byte[] getBytes24() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        int len = getInt24();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        byte b[] = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        read(b, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
}