Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/crypto/CipherOutputStream.java
38829 views
/*1* Copyright (c) 1997, 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 javax.crypto;2627import java.io.*;2829/**30* A CipherOutputStream is composed of an OutputStream and a Cipher so31* that write() methods first process the data before writing them out32* to the underlying OutputStream. The cipher must be fully33* initialized before being used by a CipherOutputStream.34*35* <p> For example, if the cipher is initialized for encryption, the36* CipherOutputStream will attempt to encrypt data before writing out the37* encrypted data.38*39* <p> This class adheres strictly to the semantics, especially the40* failure semantics, of its ancestor classes41* java.io.OutputStream and java.io.FilterOutputStream. This class42* has exactly those methods specified in its ancestor classes, and43* overrides them all. Moreover, this class catches all exceptions44* that are not thrown by its ancestor classes. In particular, this45* class catches BadPaddingException and other exceptions thrown by46* failed integrity checks during decryption. These exceptions are not47* re-thrown, so the client will not be informed that integrity checks48* failed. Because of this behavior, this class may not be suitable49* for use with decryption in an authenticated mode of operation (e.g. GCM)50* if the application requires explicit notification when authentication51* fails. Such an application can use the Cipher API directly as an52* alternative to using this class.53*54* <p> It is crucial for a programmer using this class not to use55* methods that are not defined or overriden in this class (such as a56* new method or constructor that is later added to one of the super57* classes), because the design and implementation of those methods58* are unlikely to have considered security impact with regard to59* CipherOutputStream.60*61* @author Li Gong62* @see java.io.OutputStream63* @see java.io.FilterOutputStream64* @see javax.crypto.Cipher65* @see javax.crypto.CipherInputStream66*67* @since 1.468*/6970public class CipherOutputStream extends FilterOutputStream {7172// the cipher engine to use to process stream data73private Cipher cipher;7475// the underlying output stream76private OutputStream output;7778/* the buffer holding one byte of incoming data */79private byte[] ibuffer = new byte[1];8081// the buffer holding data ready to be written out82private byte[] obuffer;8384// stream status85private boolean closed = false;8687/**88*89* Constructs a CipherOutputStream from an OutputStream and a90* Cipher.91* <br>Note: if the specified output stream or cipher is92* null, a NullPointerException may be thrown later when93* they are used.94*95* @param os the OutputStream object96* @param c an initialized Cipher object97*/98public CipherOutputStream(OutputStream os, Cipher c) {99super(os);100output = os;101cipher = c;102};103104/**105* Constructs a CipherOutputStream from an OutputStream without106* specifying a Cipher. This has the effect of constructing a107* CipherOutputStream using a NullCipher.108* <br>Note: if the specified output stream is null, a109* NullPointerException may be thrown later when it is used.110*111* @param os the OutputStream object112*/113protected CipherOutputStream(OutputStream os) {114super(os);115output = os;116cipher = new NullCipher();117}118119/**120* Writes the specified byte to this output stream.121*122* @param b the <code>byte</code>.123* @exception IOException if an I/O error occurs.124* @since JCE1.2125*/126public void write(int b) throws IOException {127ibuffer[0] = (byte) b;128obuffer = cipher.update(ibuffer, 0, 1);129if (obuffer != null) {130output.write(obuffer);131obuffer = null;132}133};134135/**136* Writes <code>b.length</code> bytes from the specified byte array137* to this output stream.138* <p>139* The <code>write</code> method of140* <code>CipherOutputStream</code> calls the <code>write</code>141* method of three arguments with the three arguments142* <code>b</code>, <code>0</code>, and <code>b.length</code>.143*144* @param b the data.145* @exception NullPointerException if <code>b</code> is null.146* @exception IOException if an I/O error occurs.147* @see javax.crypto.CipherOutputStream#write(byte[], int, int)148* @since JCE1.2149*/150public void write(byte b[]) throws IOException {151write(b, 0, b.length);152}153154/**155* Writes <code>len</code> bytes from the specified byte array156* starting at offset <code>off</code> to this output stream.157*158* @param b the data.159* @param off the start offset in the data.160* @param len the number of bytes to write.161* @exception IOException if an I/O error occurs.162* @since JCE1.2163*/164public void write(byte b[], int off, int len) throws IOException {165obuffer = cipher.update(b, off, len);166if (obuffer != null) {167output.write(obuffer);168obuffer = null;169}170}171172/**173* Flushes this output stream by forcing any buffered output bytes174* that have already been processed by the encapsulated cipher object175* to be written out.176*177* <p>Any bytes buffered by the encapsulated cipher178* and waiting to be processed by it will not be written out. For example,179* if the encapsulated cipher is a block cipher, and the total number of180* bytes written using one of the <code>write</code> methods is less than181* the cipher's block size, no bytes will be written out.182*183* @exception IOException if an I/O error occurs.184* @since JCE1.2185*/186public void flush() throws IOException {187if (obuffer != null) {188output.write(obuffer);189obuffer = null;190}191output.flush();192}193194/**195* Closes this output stream and releases any system resources196* associated with this stream.197* <p>198* This method invokes the <code>doFinal</code> method of the encapsulated199* cipher object, which causes any bytes buffered by the encapsulated200* cipher to be processed. The result is written out by calling the201* <code>flush</code> method of this output stream.202* <p>203* This method resets the encapsulated cipher object to its initial state204* and calls the <code>close</code> method of the underlying output205* stream.206*207* @exception IOException if an I/O error occurs.208* @since JCE1.2209*/210public void close() throws IOException {211if (closed) {212return;213}214215closed = true;216try {217obuffer = cipher.doFinal();218} catch (IllegalBlockSizeException | BadPaddingException e) {219obuffer = null;220}221try {222flush();223} catch (IOException ignored) {}224out.close();225}226}227228229