Path: blob/master/src/java.base/share/classes/sun/security/ssl/HandshakeOutStream.java
41159 views
/*1* Copyright (c) 1996, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.ssl;2627import java.io.ByteArrayOutputStream;28import java.io.IOException;29import java.nio.ByteBuffer;3031/**32* Output stream for handshake data. This is used only internally33* to the SSL classes.34*35* MT note: one thread at a time is presumed be writing handshake36* messages, but (after initial connection setup) it's possible to37* have other threads reading/writing application data. It's the38* SSLSocketImpl class that synchronizes record writes.39*40* @author David Brownell41*/42public class HandshakeOutStream extends ByteArrayOutputStream {4344OutputRecord outputRecord; // May be null if not actually used to45// output handshake message records.4647HandshakeOutStream(OutputRecord outputRecord) {48super();49this.outputRecord = outputRecord;50}5152// Complete a handshaking message write. Called by HandshakeMessage.53void complete() throws IOException {54if (size() < 4) { // 4: handshake message header size55// internal_error alert will be triggered56throw new RuntimeException("handshake message is not available");57}5859if (outputRecord != null) {60if (!outputRecord.isClosed()) {61outputRecord.encodeHandshake(buf, 0, count);62} else {63if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {64SSLLogger.warning("outbound has closed, ignore outbound " +65"handshake messages", ByteBuffer.wrap(buf, 0, count));66}67}6869// reset the byte array output stream70reset();71} // otherwise, the handshake outstream is temporarily used only.72}7374//75// overridden ByteArrayOutputStream methods76//7778@Override79public void write(byte[] b, int off, int len) {80// The maximum fragment size is 24 bytes.81checkOverflow(len, Record.OVERFLOW_OF_INT24);82super.write(b, off, len);83}8485@Override86public void flush() throws IOException {87if (outputRecord != null) {88outputRecord.flush();89}90}9192//93// handshake output stream management functions94//9596/*97* Put integers encoded in standard 8, 16, 24, and 32 bit98* big endian formats. Note that OutputStream.write(int) only99* writes the least significant 8 bits and ignores the rest.100*/101void putInt8(int i) throws IOException {102checkOverflow(i, Record.OVERFLOW_OF_INT08);103super.write(i);104}105106void putInt16(int i) throws IOException {107checkOverflow(i, Record.OVERFLOW_OF_INT16);108super.write(i >> 8);109super.write(i);110}111112void putInt24(int i) throws IOException {113checkOverflow(i, Record.OVERFLOW_OF_INT24);114super.write(i >> 16);115super.write(i >> 8);116super.write(i);117}118119void putInt32(int i) throws IOException {120super.write(i >> 24);121super.write(i >> 16);122super.write(i >> 8);123super.write(i);124}125126/*127* Put byte arrays with length encoded as 8, 16, 24 bit128* integers in big-endian format.129*/130void putBytes8(byte[] b) throws IOException {131if (b == null) {132putInt8(0);133} else {134putInt8(b.length);135super.write(b, 0, b.length);136}137}138139public void putBytes16(byte[] b) throws IOException {140if (b == null) {141putInt16(0);142} else {143putInt16(b.length);144super.write(b, 0, b.length);145}146}147148void putBytes24(byte[] b) throws IOException {149if (b == null) {150putInt24(0);151} else {152putInt24(b.length);153super.write(b, 0, b.length);154}155}156157/*158* Does the specified length overflow the limitation?159*/160private static void checkOverflow(int length, int limit) {161if (length >= limit) {162// internal_error alert will be triggered163throw new RuntimeException(164"Field length overflow, the field length (" +165length + ") should be less than " + limit);166}167}168}169170171