Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
CipherOutputStream.close()inorg.bouncycastle.crypto.ioandorg.bouncycastle.jcajce.iofinalises the cipher on every call. Onmain,core/src/main/java/org/bouncycastle/crypto/io/CipherOutputStream.java:232and:241calldoFinalinsideclose()(:223), andprov/src/main/java/org/bouncycastle/jcajce/io/CipherOutputStream.java:114callscipher.doFinal()insideclose()(:108), with no record of an earlier close.java.io.Closeablesays "If the stream is already closed then invoking this method has no effect.",javax.crypto.CipherOutputStreamkeeps aclosedflag for this, and bothCipherInputStreamclasses here already guard with afinalizedflag.The first
close()leaves the cipher reset, so a second one finalises it again with no input and appends the result: one more padding block for a padded block cipher, one more tag for EAX, CCM, OCB and GCM-SIV. The output then fails to decrypt (pad block corrupted,mac check in EAX failed), or with ECB decrypts to different data without error. GCM throws instead (GCM cipher cannot be reused for encryption), as does aFileOutputStreamsink (Stream Closed). In decrypt mode an AEAD stream throwsInvalidCipherTextIOExceptionwith causedata too short(Data too shortfor GCM-SIV) after writing out the correct plaintext. A try-with-resources that also declares aBufferedOutputStreamover the cipher stream is enough to close it twice.Reproduced on the released bcprov-jdk18on-1.86.jar: in that pattern, a 33-byte message encrypted with AES/CBC/PKCS5Padding through the jcajce.io stream gives 64 bytes and
BadPaddingExceptionon decryption, wherejavax.crypto.CipherOutputStreamgives 48. Currentmainand the 1.87-SNAPSHOT beta (1.87.0.20727) have both sources unchanged from 1.86 and reproduce it. Stream ciphers and CTR are unaffected.This change:
closedfield to each class, set at the start ofclose()before the cipher is finalised, so a later call returns at once, as injavax.crypto.CipherOutputStream; andtestDoubleCloseto coreCipherStreamTest(AES/CBC/PKCS7, EAX, GCM) and provCipherStreamTest2(AES/CBC/PKCS5Padding, EAX, and GCM behind the JVM check the file already uses), closing an encrypting and then a decrypting stream twice and checking the output is unchanged and decrypts back.Both tests fail without the change and pass with it; the other entries of both
RegressionTest.testsarrays give the same result either way (without a bc-test-data checkout, the 21 core and 4 prov entries that need it could not run). Data written afterclose()is no longer finalised by a laterclose(), as withjavax.crypto.CipherOutputStream.Base tree only: no
META-INF/versionscopy of either class, nomodule-infoor OSGi change. The clean-roomjce/src/main/java/javax/crypto/CipherOutputStream.javahas the same unguardedclose()and is left as is. A release-note entry is included, happy to move it to another block.