0N/A/*
5408N/A * Copyright (c) 1996, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A
0N/Apackage sun.security.ssl;
0N/A
0N/Aimport java.io.InputStream;
0N/Aimport java.io.IOException;
0N/Aimport java.security.MessageDigest;
0N/A
0N/Aimport javax.net.ssl.SSLException;
0N/A
0N/A/**
0N/A * InputStream for handshake data, used internally only. Contains the
0N/A * handshake message buffer and methods to parse them.
0N/A *
0N/A * Once a new handshake record arrives, it is buffered in this class until
0N/A * processed by the Handshaker. The buffer may also contain incomplete
0N/A * handshake messages in case the message is split across multiple records.
0N/A * Handshaker.process_record deals with all that. It may also contain
0N/A * handshake messages larger than the default buffer size (e.g. large
0N/A * certificate messages). The buffer is grown dynamically to handle that
0N/A * (see InputRecord.queueHandshake()).
0N/A *
0N/A * Note that the InputRecord used as a buffer here is separate from the
0N/A * AppInStream.r, which is where data from the socket is initially read
0N/A * into. This is because once the initial handshake has been completed,
0N/A * handshake and application data messages may be interleaved arbitrarily
0N/A * and must be processed independently.
0N/A *
0N/A * @author David Brownell
0N/A */
1870N/Apublic class HandshakeInStream extends InputStream {
0N/A
0N/A InputRecord r;
0N/A
0N/A /*
0N/A * Construct the stream; we'll be accumulating hashes of the
0N/A * input records using two sets of digests.
0N/A */
0N/A HandshakeInStream(HandshakeHash handshakeHash) {
0N/A r = new InputRecord();
0N/A r.setHandshakeHash(handshakeHash);
0N/A }
0N/A
0N/A
0N/A // overridden InputStream methods
0N/A
0N/A /*
0N/A * Return the number of bytes available for read().
0N/A *
0N/A * Note that this returns the bytes remaining in the buffer, not
0N/A * the bytes remaining in the current handshake message.
0N/A */
0N/A public int available() {
0N/A return r.available();
0N/A }
0N/A
0N/A /*
0N/A * Get a byte of handshake data.
0N/A */
0N/A public int read() throws IOException {
0N/A int n = r.read();
0N/A if (n == -1) {
0N/A throw new SSLException("Unexpected end of handshake data");
0N/A }
0N/A return n;
0N/A }
0N/A
0N/A /*
0N/A * Get a bunch of bytes of handshake data.
0N/A */
0N/A public int read(byte b [], int off, int len) throws IOException {
0N/A // we read from a ByteArrayInputStream, it always returns the
0N/A // data in a single read if enough is available
0N/A int n = r.read(b, off, len);
0N/A if (n != len) {
0N/A throw new SSLException("Unexpected end of handshake data");
0N/A }
0N/A return n;
0N/A }
0N/A
0N/A /*
0N/A * Skip some handshake data.
0N/A */
0N/A public long skip(long n) throws IOException {
0N/A return r.skip(n);
0N/A }
0N/A
0N/A /*
0N/A * Mark/ reset code, implemented using InputRecord mark/ reset.
0N/A *
0N/A * Note that it currently provides only a limited mark functionality
0N/A * and should be used with care (once a new handshake record has been
0N/A * read, data that has already been consumed is lost even if marked).
0N/A */
0N/A
0N/A public void mark(int readlimit) {
0N/A r.mark(readlimit);
0N/A }
0N/A
0N/A public void reset() {
0N/A r.reset();
0N/A }
0N/A
0N/A public boolean markSupported() {
0N/A return true;
0N/A }
0N/A
0N/A
0N/A // handshake management functions
0N/A
0N/A /*
0N/A * Here's an incoming record with handshake data. Queue the contents;
0N/A * it might be one or more entire messages, complete a message that's
0N/A * partly queued, or both.
0N/A */
0N/A void incomingRecord(InputRecord in) throws IOException {
0N/A r.queueHandshake(in);
0N/A }
0N/A
0N/A /*
0N/A * Hash any data we've consumed but not yet hashed. Useful mostly
0N/A * for processing client certificate messages (so we can check the
0N/A * immediately following cert verify message) and finished messages
0N/A * (so we can compute our own finished message).
0N/A */
0N/A void digestNow() {
0N/A r.doHashes();
0N/A }
0N/A
0N/A /*
0N/A * Do more than skip that handshake data ... totally ignore it.
0N/A * The difference is that the data does not get hashed.
0N/A */
0N/A void ignore(int n) {
0N/A r.ignore(n);
0N/A }
0N/A
0N/A
0N/A // Message parsing methods
0N/A
0N/A /*
0N/A * Read 8, 16, 24, and 32 bit SSL integer data types, encoded
0N/A * in standard big-endian form.
0N/A */
0N/A
0N/A int getInt8() throws IOException {
0N/A return read();
0N/A }
0N/A
0N/A int getInt16() throws IOException {
0N/A return (getInt8() << 8) | getInt8();
0N/A }
0N/A
0N/A int getInt24() throws IOException {
0N/A return (getInt8() << 16) | (getInt8() << 8) | getInt8();
0N/A }
0N/A
0N/A int getInt32() throws IOException {
0N/A return (getInt8() << 24) | (getInt8() << 16)
0N/A | (getInt8() << 8) | getInt8();
0N/A }
0N/A
0N/A /*
0N/A * Read byte vectors with 8, 16, and 24 bit length encodings.
0N/A */
0N/A
0N/A byte[] getBytes8() throws IOException {
0N/A int len = getInt8();
5408N/A verifyLength(len);
0N/A byte b[] = new byte[len];
0N/A
0N/A read(b, 0, len);
0N/A return b;
0N/A }
0N/A
1870N/A public byte[] getBytes16() throws IOException {
0N/A int len = getInt16();
5408N/A verifyLength(len);
0N/A byte b[] = new byte[len];
0N/A
0N/A read(b, 0, len);
0N/A return b;
0N/A }
0N/A
0N/A byte[] getBytes24() throws IOException {
0N/A int len = getInt24();
5408N/A verifyLength(len);
0N/A byte b[] = new byte[len];
0N/A
0N/A read(b, 0, len);
0N/A return b;
0N/A }
0N/A
5408N/A // Is a length greater than available bytes in the record?
5408N/A private void verifyLength(int len) throws SSLException {
5408N/A if (len > available()) {
5408N/A throw new SSLException(
5408N/A "Not enough data to fill declared vector size");
5408N/A }
5408N/A }
5408N/A
0N/A}