neuhalje / bouncy-gpg

Make using Bouncy Castle with OpenPGP fun again!
https://neuhalje.github.io/bouncy-gpg/
Other
205 stars 58 forks source link

Some files cannot be decrypted with error "premature end of stream in PartialInputStream" #46

Closed jni- closed 4 years ago

jni- commented 4 years ago

Describe the bug An error occurs when decrypting some files. It seems caused by a stream closed too early (see additional information)

To Reproduce

        encryptedStream.use { cipherTextStream ->
            FileOutputStream(decryptedFile).use { bufferedOut ->
                BouncyGPG
                    .decryptAndVerifyStream()
                    .withConfig(keyRing)
                    .andIgnoreSignatures()
                    .fromEncryptedInputStream(cipherTextStream)
                    .use { plaintextStream ->
                        Streams.pipeAll(plaintextStream, bufferedOut)
                    }
            }
        }

Expected behavior File gets decrypted

System (please complete the following information):

Additional context Stacktrace :

Caused by: java.io.EOFException: premature end of stream in PartialInputStream
    at org.bouncycastle.bcpg.BCPGInputStream$PartialInputStream.read(Unknown Source)
    at org.bouncycastle.bcpg.BCPGInputStream.read(Unknown Source)
    at org.bouncycastle.openpgp.PGPCompressedData$1.fill(Unknown Source)
    at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158)
    at org.bouncycastle.bcpg.BCPGInputStream.read(Unknown Source)
    at org.bouncycastle.bcpg.BCPGInputStream$PartialInputStream.read(Unknown Source)
    at org.bouncycastle.bcpg.BCPGInputStream.read(Unknown Source)
    at org.bouncycastle.util.io.Streams.pipeAll(Unknown Source)
    at [redacted, my code].decryptIntoOutputStream(BouncyCastleFileDecryptor.kt:47)

I've isolated the breaking commit : https://github.com/neuhalje/bouncy-gpg/commit/012c3f9a14c062de60bb10bfe52c1f01264de899 (reverting to 2.1.2 works)

It seems the stream closes too soon, but I have no idea why it works with some files and not with some others.

It works well with a file I encrypted myself. However, it does not work with the file the provider creates. I have no idea why. I also have no idea how they encrypt it to be honest. I cannot share the file for privacy reasons, however if some info on it can help, let me know.

I'm sorry I can't provide more information! Let me know how I can help more

jni- commented 4 years ago

Oh, I have more info.

I've traced the code using both files. The difference is that my PGPCompressedData contains a PGPOnePassSignatureList next (that works). The buggy file is not signed, it does not contain this packet. Instead, it jumps straight the literal data, which returns the stream itself.

Not sure I understand all of it so far, but it seems that having no signature list packets break it. Not sure how to create a file to isolate this either.

Hope this helps!

ispringer commented 4 years ago

I also encountered this issue. Like @jni-, I experienced it with one particular file, but not with others. The file I saw it for was a fairly small (12K) encrypted and signed CSV file. Both the encrypted file and the signer's public key were created using a very old PGP toolkit (v2 I think), which might be relevant. The files that worked were encrypted and signed by me using BouncyGPG.

47 fixes the issue by reverting 012c3f9.