mirror of
https://github.com/pgpainless/pgpainless.git
synced 2025-01-10 20:27:58 +01:00
Fix partial length encoding of some messages
Fixes #160 I'm not yet sure, why this issue only happened with some keys and not others, but this fix works for now.
This commit is contained in:
parent
16c3116518
commit
2d76cb5c82
2 changed files with 141 additions and 1 deletions
|
@ -52,7 +52,10 @@ public final class EncryptionStream extends OutputStream {
|
|||
private final EncryptionResult.Builder resultBuilder = EncryptionResult.builder();
|
||||
|
||||
private boolean closed = false;
|
||||
private static final int BUFFER_SIZE = 1 << 8;
|
||||
// 1 << 8 causes wrong partial body length encoding
|
||||
// 1 << 9 fixes this.
|
||||
// see https://github.com/pgpainless/pgpainless/issues/160
|
||||
private static final int BUFFER_SIZE = 1 << 9;
|
||||
|
||||
OutputStream outermostStream;
|
||||
private ArmoredOutputStream armorOutputStream = null;
|
||||
|
|
|
@ -28,6 +28,8 @@ import org.bouncycastle.util.io.Streams;
|
|||
import org.junit.jupiter.api.Test;
|
||||
import org.pgpainless.PGPainless;
|
||||
import org.pgpainless.algorithm.DocumentSignatureType;
|
||||
import org.pgpainless.decryption_verification.ConsumerOptions;
|
||||
import org.pgpainless.decryption_verification.DecryptionStream;
|
||||
import org.pgpainless.encryption_signing.EncryptionOptions;
|
||||
import org.pgpainless.encryption_signing.EncryptionStream;
|
||||
import org.pgpainless.encryption_signing.ProducerOptions;
|
||||
|
@ -128,4 +130,139 @@ public class InvestigateThunderbirdDecryption {
|
|||
System.out.println(out);
|
||||
// CHECKSTYLE:ON
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithSuspiciousKey() throws PGPException, IOException {
|
||||
String KEY = "-----BEGIN PGP PRIVATE KEY BLOCK-----\n" +
|
||||
"Comment: 4409 A346 1359 96C9 4595 510E 5A18 53D1 5656 CB7A\n" +
|
||||
"Comment: Alice (Created with pgpkeygen.com) <alice@pgpainless.or\n" +
|
||||
"\n" +
|
||||
"xcLYBGEJRbUBCADdGTnWfMDfZx0Qddc5mpNW1+qMDubfzOoE0bhDMC8U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SEOw9nPnDuGzZzcMOGwHfQ78CmoCniP44prMoRiS\n" +
|
||||
"PcfmdFeQDt5kAKg1offUe6z36Brtk/8HO3FCbKM+zim2YF7GNLuCZTfZfYp1B0Gv\n" +
|
||||
"/Ici5zjrJIFR/78vcZgjpEzUCQ2XkgFTNNuWsGJ7APm6MPzQLEyK6apVw0BoM+Mb\n" +
|
||||
"vt8V+lJV+hZNzEWQmbuiyv87onWNJGDQiu30bXktaHx6VEjjwjicPqv4qDNT3rhv\n" +
|
||||
"dsB/NddWOgRXfnZ833hVT6rBZYtm+nqb3nm8rbJOtPi/MnEFoyWFi3uT9h7oeAc=\n" +
|
||||
"=Zaty\n" +
|
||||
"-----END PGP PRIVATE KEY BLOCK-----\n";
|
||||
PGPSecretKeyRing secretKey = PGPainless.readKeyRing().secretKeyRing(KEY);
|
||||
PGPPublicKeyRing publicKey = PGPainless.extractCertificate(secretKey);
|
||||
|
||||
ByteArrayInputStream data = new ByteArrayInputStream("Hello, World!\n".getBytes(StandardCharsets.UTF_8));
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
EncryptionStream encryptionStream = PGPainless.encryptAndOrSign().onOutputStream(out)
|
||||
.withOptions(ProducerOptions.signAndEncrypt(
|
||||
EncryptionOptions.encryptCommunications()
|
||||
.addRecipient(publicKey),
|
||||
SigningOptions.get()
|
||||
.addInlineSignature(SecretKeyRingProtector.unprotectedKeys(), secretKey, DocumentSignatureType.BINARY_DOCUMENT)
|
||||
).setAsciiArmor(true));
|
||||
|
||||
Streams.pipeAll(data, encryptionStream);
|
||||
encryptionStream.close();
|
||||
// CHECKSTYLE:OFF
|
||||
System.out.println(out);
|
||||
// CHECKSTYLE:ON
|
||||
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
ByteArrayOutputStream clear = new ByteArrayOutputStream();
|
||||
|
||||
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
|
||||
.onInputStream(in)
|
||||
.withOptions(new ConsumerOptions()
|
||||
.addDecryptionKey(secretKey, SecretKeyRingProtector.unprotectedKeys())
|
||||
.addVerificationCert(publicKey));
|
||||
|
||||
Streams.pipeAll(decryptionStream, clear);
|
||||
|
||||
decryptionStream.close();
|
||||
// CHECKSTYLE:OFF
|
||||
System.out.println(clear);
|
||||
// CHECKSTYLE:ON
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue