1 /*
   2  * Copyright (c) 1996, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.security.ssl;
  27 
  28 import java.io.ByteArrayOutputStream;
  29 import java.io.IOException;
  30 
  31 /**
  32  * Output stream for handshake data.  This is used only internally
  33  * to the SSL classes.
  34  *
  35  * MT note:  one thread at a time is presumed be writing handshake
  36  * messages, but (after initial connection setup) it's possible to
  37  * have other threads reading/writing application data.  It's the
  38  * SSLSocketImpl class that synchronizes record writes.
  39  *
  40  * @author  David Brownell
  41  */
  42 public class HandshakeOutStream extends ByteArrayOutputStream {
  43 
  44     OutputRecord outputRecord;      // May be null if not actually used to
  45                                     // output handshake message records.
  46 
  47     HandshakeOutStream(OutputRecord outputRecord) {
  48         super();
  49         this.outputRecord = outputRecord;
  50     }
  51 
  52     // Complete a handshakin message writing. Called by HandshakeMessage.
  53     void complete() throws IOException {
  54         if (size() < 4) {       // 4: handshake message header size
  55             // internal_error alert will be triggered
  56             throw new RuntimeException("handshake message is not available");
  57         }
  58 
  59         if (outputRecord != null) {
  60             outputRecord.encodeHandshake(buf, 0, count);
  61 
  62             // reset the byte array output stream
  63             reset();
  64         }   // otherwise, the handshake outstream is temporarily used only.
  65     }
  66 
  67     //
  68     // overridden ByteArrayOutputStream methods
  69     //
  70 
  71     @Override
  72     public void write(byte[] b, int off, int len) {
  73         // The maximum fragment size is 24 bytes.
  74         checkOverflow(len, Record.OVERFLOW_OF_INT24);
  75         super.write(b, off, len);
  76     }
  77 
  78     @Override
  79     public void flush() throws IOException {
  80         if (outputRecord != null) {
  81             outputRecord.flush();
  82         }
  83     }
  84 
  85     //
  86     // handshake output stream management functions
  87     //
  88 
  89     /*
  90      * Put integers encoded in standard 8, 16, 24, and 32 bit
  91      * big endian formats. Note that OutputStream.write(int) only
  92      * writes the least significant 8 bits and ignores the rest.
  93      */
  94     void putInt8(int i) throws IOException {
  95         checkOverflow(i, Record.OVERFLOW_OF_INT08);
  96         super.write(i);
  97     }
  98 
  99     void putInt16(int i) throws IOException {
 100         checkOverflow(i, Record.OVERFLOW_OF_INT16);
 101         super.write(i >> 8);
 102         super.write(i);
 103     }
 104 
 105     void putInt24(int i) throws IOException {
 106         checkOverflow(i, Record.OVERFLOW_OF_INT24);
 107         super.write(i >> 16);
 108         super.write(i >> 8);
 109         super.write(i);
 110     }
 111 
 112     void putInt32(int i) throws IOException {
 113         super.write(i >> 24);
 114         super.write(i >> 16);
 115         super.write(i >> 8);
 116         super.write(i);
 117     }
 118 
 119     /*
 120      * Put byte arrays with length encoded as 8, 16, 24 bit
 121      * integers in big-endian format.
 122      */
 123     void putBytes8(byte[] b) throws IOException {
 124         if (b == null) {
 125             putInt8(0);
 126         } else {
 127             putInt8(b.length);
 128             super.write(b, 0, b.length);
 129         }
 130     }
 131 
 132     public void putBytes16(byte[] b) throws IOException {
 133         if (b == null) {
 134             putInt16(0);
 135         } else {
 136             putInt16(b.length);
 137             super.write(b, 0, b.length);
 138         }
 139     }
 140 
 141     void putBytes24(byte[] b) throws IOException {
 142         if (b == null) {
 143             putInt24(0);
 144         } else {
 145             putInt24(b.length);
 146             super.write(b, 0, b.length);
 147         }
 148     }
 149 
 150     /*
 151      * Does the specified length overflow the limitation?
 152      */
 153     private static void checkOverflow(int length, int limit) {
 154         if (length >= limit) {
 155             // internal_error alert will be triggered
 156             throw new RuntimeException(
 157                     "Field length overflow, the field length (" +
 158                     length + ") should be less than " + limit);
 159         }
 160     }
 161 }