Workaround for #159: Avoid to prevent swallowing IOExceptions

This commit is contained in:
Paul Schaub 2021-07-26 16:19:30 +02:00
parent 10bb033e40
commit fc311fe781
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
25 changed files with 216 additions and 72 deletions

View File

@ -127,7 +127,7 @@ Still it allows you to manually specify which algorithms to use of course.
).setAsciiArmor(true) // Ascii armor or not
);
Streams.pipeAll(plaintextInputStream, encryptionStream);
StreamUtil.pipeAll(plaintextInputStream, encryptionStream);
encryptionStream.close();
```
@ -147,7 +147,7 @@ This behaviour can be modified though using the `Policy` class.
.addVerificationCert(alicePubKeys)
);
Streams.pipeAll(decryptionStream, outputStream);
StreamUtil.pipeAll(decryptionStream, outputStream);
decryptionStream.close();
// Result contains information like signature status etc.

View File

@ -36,7 +36,6 @@ import com.ginsberg.junit.exit.FailOnSystemExit;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.util.io.Streams;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
@ -46,6 +45,7 @@ import org.pgpainless.cli.TestUtils;
import org.pgpainless.key.OpenPgpV4Fingerprint;
import org.pgpainless.key.info.KeyRingInfo;
import org.pgpainless.key.util.KeyRingUtils;
import org.pgpainless.util.StreamUtil;
public class SignVerifyTest {
@ -71,7 +71,7 @@ public class SignVerifyTest {
PGPSecretKeyRing aliceKeys = PGPainless.generateKeyRing()
.modernKeyRing("alice", null);
OutputStream aliceKeyOut = new FileOutputStream(aliceKeyFile);
Streams.pipeAll(new ByteArrayInputStream(aliceKeys.getEncoded()), aliceKeyOut);
StreamUtil.pipeAll(new ByteArrayInputStream(aliceKeys.getEncoded()), aliceKeyOut);
aliceKeyOut.close();
// Write alice pub key to disc
@ -79,14 +79,14 @@ public class SignVerifyTest {
assertTrue(aliceCertFile.createNewFile());
PGPPublicKeyRing alicePub = KeyRingUtils.publicKeyRingFrom(aliceKeys);
OutputStream aliceCertOut = new FileOutputStream(aliceCertFile);
Streams.pipeAll(new ByteArrayInputStream(alicePub.getEncoded()), aliceCertOut);
StreamUtil.pipeAll(new ByteArrayInputStream(alicePub.getEncoded()), aliceCertOut);
aliceCertOut.close();
// Write test data to disc
File dataFile = new File(tempDir, "data");
assertTrue(dataFile.createNewFile());
FileOutputStream dataOut = new FileOutputStream(dataFile);
Streams.pipeAll(new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)), dataOut);
StreamUtil.pipeAll(new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)), dataOut);
dataOut.close();
// Sign test data

View File

@ -32,7 +32,6 @@ import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
import org.bouncycastle.util.io.Streams;
import org.pgpainless.algorithm.HashAlgorithm;
import org.pgpainless.key.OpenPgpV4Fingerprint;
@ -143,7 +142,7 @@ public class ArmorUtils {
public static String toAsciiArmoredString(InputStream inputStream, MultiMap<String, String> additionalHeaderValues) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ArmoredOutputStream armor = toAsciiArmoredStream(out, additionalHeaderValues);
Streams.pipeAll(inputStream, armor);
StreamUtil.pipeAll(inputStream, armor);
armor.close();
return out.toString();

View File

@ -0,0 +1,47 @@
/*
* Copyright 2021 Paul Schaub.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pgpainless.util;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class StreamUtil {
/**
* Pipe all data from the given {@link InputStream} to the given {@link OutputStream}.
*
* This utility method is required, since {@link org.bouncycastle.util.io.Streams#pipeAll(InputStream, OutputStream)}
* internally uses {@link InputStream#read(byte[], int, int)} which silently swallows {@link IOException IOExceptions}.
*
* @see <a href="https://github.com/pgpainless/pgpainless/issues/159#issuecomment-886694555">Explanation</a>
* @see <a href="https://github.com/AdoptOpenJDK/openjdk-jdk11/blob/master/src/java.base/share/classes/java/io/InputStream.java#L286">
* InputStream swallowing IOExceptions</a>
*
* @param inputStream input stream
* @param outputStream output stream
* @throws IOException io exceptions
*/
public static void pipeAll(InputStream inputStream, OutputStream outputStream) throws IOException {
do {
int i = inputStream.read();
if (i == -1) {
break;
}
outputStream.write(i);
} while (true);
}
}

View File

@ -0,0 +1,98 @@
/*
* Copyright 2021 Paul Schaub.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.bouncycastle;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.bouncycastle.bcpg.ArmoredInputStream;
import org.junit.jupiter.api.Test;
import org.pgpainless.util.StreamUtil;
public class WrongArmorChecksumTest {
// According to https://github.com/FlowCrypt/flowcrypt-android/issues/1347
// this message contains a wrong CRC checksum in the ascii armor
public static final String MESSAGE_WITH_WRONG_ARMOR_CHECKSUM = "" +
"-----BEGIN PGP MESSAGE-----\n" +
"Version: FlowCrypt 5.0.4 Gmail Encryption flowcrypt.com\n" +
"Comment: Seamlessly send, receive and search encrypted email\n" +
"\n" +
"wcFMA+ADv/5v4Rg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uOm9nHFwIQeeC5bWD\n" +
"mmRlC90r2jY8gM/v3Jjy9h8PbXWxh9MUpc7/kAcTwdGlMxiVjE29p065qTRr\n" +
"Oi6sJ7pWuYTfWldZqTVmaBjlv0zuXQ8Eo8o/USvoTs+oihYIMcqReqdeqr/N\n" +
"e+sDtYKRg/LKp/JJ5nAQzVMP67DxkgwLNxx0ijBLysaQmvRlsiYWayxZB1Xd\n" +
"BxA2bjZRvsmww+hgSKNlcsiubJGBqfqvgmlebZuJHHSC1L6mdMYgcihKmYAj\n" +
"p+HFLyqgyeRVMdjRHcrEdxNPG4fJmlk1bYiVQQ4XAd72w+AHS/seZ5HzbAK0\n" +
"omuHYUD5PTEqZ1K9JObSsh3XMUkJK+z3BnrOxnTOOyG2r+4FxizH6rfz/Pgg\n" +
"sPxqxE9ELUlgQe8plcPFge6aN9tUoSe+vMtDaEAqKw9JwofBF7jlxTqMMvQC\n" +
"gWbn9x3W5o4VrnpjYGtPl8sh1QREu0A+0PUJAKL4A3GSMYRouGewLSMNJlOg\n" +
"/0pPF6qB+Fi4GJ7ju5C07tfr9z9UqRj09kDXJuoJd95NdSiCz6ndugn6gs8B\n" +
"Qf/XPxZVefeMLiB6p8pG0iZ/jcJjyYJLtTg6kA+1/ffmJPfH/76ZA9dgEJLj\n" +
"/W2u0Lp4NY8cwqcXuGKgl72TVJ34Iawl35Y0yr47k/7Y1vEQ5Q3bT7HP5A==\n" +
"=FdCC\n" +
"-----END PGP MESSAGE-----";
@Test
public void assertReadingFaultyArmorMessageFails() throws IOException {
ByteArrayInputStream bytes = new ByteArrayInputStream(MESSAGE_WITH_WRONG_ARMOR_CHECKSUM.getBytes(StandardCharsets.UTF_8));
ArmoredInputStream armorIn = new ArmoredInputStream(bytes);
ByteArrayOutputStream out = new ByteArrayOutputStream();
assertThrows(IOException.class, () -> {
StreamUtil.pipeAll(armorIn, out);
armorIn.close();
});
}
}

View File

@ -28,13 +28,13 @@ import java.nio.charset.StandardCharsets;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSignature;
import org.bouncycastle.util.io.Streams;
import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless;
import org.pgpainless.key.TestKeys;
import org.pgpainless.signature.cleartext_signatures.CleartextSignatureProcessor;
import org.pgpainless.signature.cleartext_signatures.InMemoryMultiPassStrategy;
import org.pgpainless.signature.cleartext_signatures.MultiPassStrategy;
import org.pgpainless.util.StreamUtil;
import org.pgpainless.util.TestUtils;
public class CleartextSignatureVerificationTest {
@ -116,7 +116,7 @@ public class CleartextSignatureVerificationTest {
assertEquals(signature.getKeyID(), signingKeys.getPublicKey().getKeyID());
FileInputStream fileIn = new FileInputStream(file);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
Streams.pipeAll(fileIn, bytes);
StreamUtil.pipeAll(fileIn, bytes);
fileIn.close();
assertArrayEquals(message.getBytes(StandardCharsets.UTF_8), bytes.toByteArray());
}

View File

@ -26,7 +26,6 @@ import java.nio.charset.Charset;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.util.io.Streams;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
@ -37,6 +36,7 @@ import org.pgpainless.implementation.ImplementationFactory;
import org.pgpainless.key.SubkeyIdentifier;
import org.pgpainless.key.TestKeys;
import org.pgpainless.key.util.KeyRingUtils;
import org.pgpainless.util.StreamUtil;
public class DecryptAndVerifyMessageTest {
@ -67,7 +67,7 @@ public class DecryptAndVerifyMessageTest {
.withOptions(options);
ByteArrayOutputStream toPlain = new ByteArrayOutputStream();
Streams.pipeAll(decryptor, toPlain);
StreamUtil.pipeAll(decryptor, toPlain);
decryptor.close();
toPlain.close();
OpenPgpMetadata metadata = decryptor.getResult();

View File

@ -26,7 +26,6 @@ import java.util.List;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.util.io.Streams;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.pgpainless.PGPainless;
@ -34,6 +33,7 @@ import org.pgpainless.algorithm.EncryptionPurpose;
import org.pgpainless.implementation.ImplementationFactory;
import org.pgpainless.key.SubkeyIdentifier;
import org.pgpainless.key.info.KeyRingInfo;
import org.pgpainless.util.StreamUtil;
public class DecryptHiddenRecipientMessage {
@ -148,7 +148,7 @@ public class DecryptHiddenRecipientMessage {
.withOptions(options);
ByteArrayOutputStream out = new ByteArrayOutputStream();
Streams.pipeAll(decryptionStream, out);
StreamUtil.pipeAll(decryptionStream, out);
decryptionStream.close();
OpenPgpMetadata metadata = decryptionStream.getResult();

View File

@ -28,7 +28,6 @@ import java.util.Collections;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
import org.bouncycastle.util.io.Streams;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
@ -38,6 +37,7 @@ import org.pgpainless.exception.ModificationDetectionException;
import org.pgpainless.implementation.ImplementationFactory;
import org.pgpainless.key.protection.SecretKeyRingProtector;
import org.pgpainless.util.Passphrase;
import org.pgpainless.util.StreamUtil;
public class ModificationDetectionTests {
@ -160,7 +160,7 @@ public class ModificationDetectionTests {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
assertThrows(EOFException.class, () -> {
Streams.pipeAll(decryptionStream, outputStream);
StreamUtil.pipeAll(decryptionStream, outputStream);
decryptionStream.close();
});
}
@ -192,7 +192,7 @@ public class ModificationDetectionTests {
);
ByteArrayOutputStream out = new ByteArrayOutputStream();
Streams.pipeAll(decryptionStream, out);
StreamUtil.pipeAll(decryptionStream, out);
assertThrows(ModificationDetectionException.class, decryptionStream::close);
}
@ -223,7 +223,7 @@ public class ModificationDetectionTests {
);
ByteArrayOutputStream out = new ByteArrayOutputStream();
Streams.pipeAll(decryptionStream, out);
StreamUtil.pipeAll(decryptionStream, out);
assertThrows(ModificationDetectionException.class, decryptionStream::close);
}

View File

@ -24,11 +24,11 @@ import java.nio.charset.StandardCharsets;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.util.io.Streams;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.pgpainless.PGPainless;
import org.pgpainless.implementation.ImplementationFactory;
import org.pgpainless.util.StreamUtil;
public class RecursionDepthTest {
@ -160,7 +160,7 @@ public class RecursionDepthTest {
.withOptions(new ConsumerOptions().addDecryptionKey(secretKey));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Streams.pipeAll(decryptionStream, outputStream);
StreamUtil.pipeAll(decryptionStream, outputStream);
});
}

View File

@ -32,7 +32,6 @@ import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.util.io.Streams;
import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless;
import org.pgpainless.algorithm.DocumentSignatureType;
@ -43,6 +42,7 @@ import org.pgpainless.key.TestKeys;
import org.pgpainless.key.info.KeyRingInfo;
import org.pgpainless.key.protection.SecretKeyRingProtector;
import org.pgpainless.key.util.KeyRingUtils;
import org.pgpainless.util.StreamUtil;
/**
* Test functionality of the {@link MissingPublicKeyCallback} which is called when during signature verification,
@ -66,7 +66,7 @@ public class VerifyWithMissingPublicKeyCallback {
SecretKeyRingProtector.unprotectedKeys(),
signingSecKeys, DocumentSignatureType.CANONICAL_TEXT_DOCUMENT
)));
Streams.pipeAll(new ByteArrayInputStream(msg.getBytes(StandardCharsets.UTF_8)), signingStream);
StreamUtil.pipeAll(new ByteArrayInputStream(msg.getBytes(StandardCharsets.UTF_8)), signingStream);
signingStream.close();
DecryptionStream verificationStream = PGPainless.decryptAndOrVerify()
@ -83,7 +83,7 @@ public class VerifyWithMissingPublicKeyCallback {
}));
ByteArrayOutputStream plainOut = new ByteArrayOutputStream();
Streams.pipeAll(verificationStream, plainOut);
StreamUtil.pipeAll(verificationStream, plainOut);
verificationStream.close();
assertArrayEquals(msg.getBytes(StandardCharsets.UTF_8), plainOut.toByteArray());

View File

@ -34,7 +34,6 @@ import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPSignature;
import org.bouncycastle.util.io.Streams;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
@ -59,6 +58,7 @@ import org.pgpainless.key.protection.UnprotectedKeysProtector;
import org.pgpainless.key.util.KeyRingUtils;
import org.pgpainless.policy.Policy;
import org.pgpainless.util.ArmoredOutputStreamFactory;
import org.pgpainless.util.StreamUtil;
public class EncryptDecryptTest {
@ -168,7 +168,7 @@ public class EncryptDecryptTest {
new SigningOptions().addInlineSignature(keyDecryptor, senderSec, DocumentSignatureType.BINARY_DOCUMENT)
));
Streams.pipeAll(new ByteArrayInputStream(secretMessage), encryptor);
StreamUtil.pipeAll(new ByteArrayInputStream(secretMessage), encryptor);
encryptor.close();
byte[] encryptedSecretMessage = envelope.toByteArray();
@ -193,7 +193,7 @@ public class EncryptDecryptTest {
ByteArrayOutputStream decryptedSecretMessage = new ByteArrayOutputStream();
Streams.pipeAll(decryptor, decryptedSecretMessage);
StreamUtil.pipeAll(decryptor, decryptedSecretMessage);
decryptor.close();
assertArrayEquals(secretMessage, decryptedSecretMessage.toByteArray());
@ -218,7 +218,7 @@ public class EncryptDecryptTest {
.withOptions(ProducerOptions.sign(
new SigningOptions().addDetachedSignature(keyRingProtector, signingKeys, DocumentSignatureType.BINARY_DOCUMENT)
));
Streams.pipeAll(inputStream, signer);
StreamUtil.pipeAll(inputStream, signer);
signer.close();
EncryptionResult metadata = signer.getResult();
@ -243,7 +243,7 @@ public class EncryptDecryptTest {
);
dummyOut = new ByteArrayOutputStream();
Streams.pipeAll(verifier, dummyOut);
StreamUtil.pipeAll(verifier, dummyOut);
verifier.close();
OpenPgpMetadata decryptionResult = verifier.getResult();
@ -264,7 +264,7 @@ public class EncryptDecryptTest {
SigningOptions.get()
.addInlineSignature(keyRingProtector, signingKeys, DocumentSignatureType.BINARY_DOCUMENT)
).setAsciiArmor(true));
Streams.pipeAll(inputStream, signer);
StreamUtil.pipeAll(inputStream, signer);
signer.close();
inputStream = new ByteArrayInputStream(signOut.toByteArray());
@ -274,7 +274,7 @@ public class EncryptDecryptTest {
.addVerificationCert(KeyRingUtils.publicKeyRingFrom(signingKeys))
);
signOut = new ByteArrayOutputStream();
Streams.pipeAll(verifier, signOut);
StreamUtil.pipeAll(verifier, signOut);
verifier.close();
OpenPgpMetadata metadata = verifier.getResult();

View File

@ -31,7 +31,6 @@ import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPLiteralData;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.util.io.Streams;
import org.junit.JUtils;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
@ -40,6 +39,7 @@ import org.pgpainless.algorithm.StreamEncoding;
import org.pgpainless.decryption_verification.ConsumerOptions;
import org.pgpainless.decryption_verification.DecryptionStream;
import org.pgpainless.decryption_verification.OpenPgpMetadata;
import org.pgpainless.util.StreamUtil;
public class FileInformationTest {
@ -72,7 +72,7 @@ public class FileInformationTest {
.setEncoding(encoding)
);
Streams.pipeAll(dataIn, encryptionStream);
StreamUtil.pipeAll(dataIn, encryptionStream);
encryptionStream.close();
EncryptionResult encResult = encryptionStream.getResult();
@ -87,7 +87,7 @@ public class FileInformationTest {
.onInputStream(cryptIn)
.withOptions(new ConsumerOptions()
.addDecryptionKey(secretKey));
Streams.pipeAll(decryptionStream, plainOut);
StreamUtil.pipeAll(decryptionStream, plainOut);
decryptionStream.close();
@ -110,7 +110,7 @@ public class FileInformationTest {
.addRecipient(certificate))
);
Streams.pipeAll(dataIn, encryptionStream);
StreamUtil.pipeAll(dataIn, encryptionStream);
encryptionStream.close();
EncryptionResult encResult = encryptionStream.getResult();
@ -126,7 +126,7 @@ public class FileInformationTest {
.onInputStream(cryptIn)
.withOptions(new ConsumerOptions()
.addDecryptionKey(secretKey));
Streams.pipeAll(decryptionStream, plainOut);
StreamUtil.pipeAll(decryptionStream, plainOut);
decryptionStream.close();
@ -151,7 +151,7 @@ public class FileInformationTest {
.setForYourEyesOnly()
);
Streams.pipeAll(dataIn, encryptionStream);
StreamUtil.pipeAll(dataIn, encryptionStream);
encryptionStream.close();
EncryptionResult encResult = encryptionStream.getResult();
@ -167,7 +167,7 @@ public class FileInformationTest {
.onInputStream(cryptIn)
.withOptions(new ConsumerOptions()
.addDecryptionKey(secretKey));
Streams.pipeAll(decryptionStream, plainOut);
StreamUtil.pipeAll(decryptionStream, plainOut);
decryptionStream.close();

View File

@ -33,7 +33,6 @@ import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
import org.bouncycastle.util.io.Streams;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
@ -49,6 +48,7 @@ import org.pgpainless.key.info.KeyRingInfo;
import org.pgpainless.key.protection.SecretKeyRingProtector;
import org.pgpainless.key.util.KeyRingUtils;
import org.pgpainless.util.Passphrase;
import org.pgpainless.util.StreamUtil;
public class SigningTest {
@ -81,7 +81,7 @@ public class SigningTest {
byte[] messageBytes = "This message is signed and encrypted to Romeo and Juliet.".getBytes(StandardCharsets.UTF_8);
ByteArrayInputStream message = new ByteArrayInputStream(messageBytes);
Streams.pipeAll(message, encryptionStream);
StreamUtil.pipeAll(message, encryptionStream);
encryptionStream.close();
byte[] encrypted = out.toByteArray();
@ -102,7 +102,7 @@ public class SigningTest {
ByteArrayOutputStream plaintextOut = new ByteArrayOutputStream();
Streams.pipeAll(decryptionStream, plaintextOut);
StreamUtil.pipeAll(decryptionStream, plaintextOut);
decryptionStream.close();
OpenPgpMetadata metadata = decryptionStream.getResult();

View File

@ -28,7 +28,6 @@ import java.security.NoSuchAlgorithmException;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.util.io.Streams;
import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless;
import org.pgpainless.algorithm.DocumentSignatureType;
@ -42,6 +41,7 @@ import org.pgpainless.encryption_signing.SigningOptions;
import org.pgpainless.key.protection.SecretKeyRingProtector;
import org.pgpainless.key.util.KeyRingUtils;
import org.pgpainless.util.Passphrase;
import org.pgpainless.util.StreamUtil;
public class Encrypt {
@ -82,7 +82,7 @@ public class Encrypt {
);
// Pipe data trough and CLOSE the stream (important)
Streams.pipeAll(new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8)), encryptor);
StreamUtil.pipeAll(new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8)), encryptor);
encryptor.close();
String encryptedMessage = ciphertext.toString();
@ -96,7 +96,7 @@ public class Encrypt {
ByteArrayOutputStream plaintext = new ByteArrayOutputStream();
Streams.pipeAll(decryptor, plaintext);
StreamUtil.pipeAll(decryptor, plaintext);
decryptor.close();
// Check the metadata to see how the message was encrypted/signed
@ -126,7 +126,7 @@ public class Encrypt {
).setAsciiArmor(true)
);
Streams.pipeAll(new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8)), encryptor);
StreamUtil.pipeAll(new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8)), encryptor);
encryptor.close();
String asciiCiphertext = ciphertext.toString();
@ -137,7 +137,7 @@ public class Encrypt {
.withOptions(new ConsumerOptions().addDecryptionPassphrase(Passphrase.fromPassword("p4ssphr4s3")));
ByteArrayOutputStream plaintext = new ByteArrayOutputStream();
Streams.pipeAll(decryptor, plaintext);
StreamUtil.pipeAll(decryptor, plaintext);
decryptor.close();

View File

@ -33,7 +33,6 @@ import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor;
import org.bouncycastle.openpgp.operator.PGPDigestCalculatorProvider;
import org.bouncycastle.openpgp.operator.bc.BcPBESecretKeyDecryptorBuilder;
import org.bouncycastle.openpgp.operator.bc.BcPGPDigestCalculatorProvider;
import org.bouncycastle.util.io.Streams;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.pgpainless.PGPainless;
@ -47,6 +46,7 @@ import org.pgpainless.key.protection.KeyRingProtectionSettings;
import org.pgpainless.key.protection.PasswordBasedSecretKeyRingProtector;
import org.pgpainless.key.protection.UnlockSecretKey;
import org.pgpainless.util.Passphrase;
import org.pgpainless.util.StreamUtil;
public class ChangeSecretKeyRingPassphraseTest {
@ -199,7 +199,7 @@ public class ChangeSecretKeyRingPassphraseTest {
.addInlineSignature(PasswordBasedSecretKeyRingProtector.forKey(keyRing, passphrase),
keyRing, DocumentSignatureType.BINARY_DOCUMENT)));
Streams.pipeAll(new ByteArrayInputStream(dummyMessage.getBytes()), stream);
StreamUtil.pipeAll(new ByteArrayInputStream(dummyMessage.getBytes()), stream);
stream.close();
}
}

View File

@ -30,7 +30,6 @@ import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPSignature;
import org.bouncycastle.util.io.Streams;
import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless;
import org.pgpainless.decryption_verification.ConsumerOptions;
@ -38,6 +37,7 @@ import org.pgpainless.decryption_verification.DecryptionStream;
import org.pgpainless.decryption_verification.OpenPgpMetadata;
import org.pgpainless.key.OpenPgpV4Fingerprint;
import org.pgpainless.key.util.KeyRingUtils;
import org.pgpainless.util.StreamUtil;
/**
* Test if marker packets are being ignored properly.
@ -162,7 +162,7 @@ public class IgnoreMarkerPackets {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Streams.pipeAll(decryptionStream, outputStream);
StreamUtil.pipeAll(decryptionStream, outputStream);
decryptionStream.close();
OpenPgpMetadata metadata = decryptionStream.getResult();
@ -211,7 +211,7 @@ public class IgnoreMarkerPackets {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Streams.pipeAll(decryptionStream, outputStream);
StreamUtil.pipeAll(decryptionStream, outputStream);
decryptionStream.close();
assertArrayEquals(data.getBytes(StandardCharsets.UTF_8), outputStream.toByteArray());

View File

@ -21,7 +21,6 @@ import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.util.io.Streams;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.pgpainless.PGPainless;
@ -32,6 +31,7 @@ import org.pgpainless.encryption_signing.EncryptionStream;
import org.pgpainless.encryption_signing.ProducerOptions;
import org.pgpainless.implementation.ImplementationFactory;
import org.pgpainless.util.Passphrase;
import org.pgpainless.util.StreamUtil;
public class MultiPassphraseSymmetricEncryptionTest {
@ -51,7 +51,7 @@ public class MultiPassphraseSymmetricEncryptionTest {
.addPassphrase(Passphrase.fromPassword("p2"))
).setAsciiArmor(false));
Streams.pipeAll(plaintextIn, encryptor);
StreamUtil.pipeAll(plaintextIn, encryptor);
encryptor.close();
byte[] ciphertext = ciphertextOut.toByteArray();
@ -65,7 +65,7 @@ public class MultiPassphraseSymmetricEncryptionTest {
ByteArrayOutputStream plaintextOut = new ByteArrayOutputStream();
Streams.pipeAll(decryptor, plaintextOut);
StreamUtil.pipeAll(decryptor, plaintextOut);
decryptor.close();
}

View File

@ -27,7 +27,6 @@ import java.util.Random;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
import org.bouncycastle.util.io.Streams;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.pgpainless.PGPainless;
@ -44,6 +43,7 @@ import org.pgpainless.key.protection.PasswordBasedSecretKeyRingProtector;
import org.pgpainless.key.protection.SecretKeyRingProtector;
import org.pgpainless.key.protection.passphrase_provider.SolitaryPassphraseProvider;
import org.pgpainless.util.Passphrase;
import org.pgpainless.util.StreamUtil;
/**
* Test parallel symmetric and public key encryption/decryption.
@ -68,7 +68,7 @@ public class SymmetricEncryptionTest {
.addRecipient(encryptionKey)
));
Streams.pipeAll(plaintextIn, encryptor);
StreamUtil.pipeAll(plaintextIn, encryptor);
encryptor.close();
byte[] ciphertext = ciphertextOut.toByteArray();
@ -81,7 +81,7 @@ public class SymmetricEncryptionTest {
ByteArrayOutputStream decrypted = new ByteArrayOutputStream();
Streams.pipeAll(decryptor, decrypted);
StreamUtil.pipeAll(decryptor, decrypted);
decryptor.close();
assertArrayEquals(plaintext, decrypted.toByteArray());
@ -98,7 +98,7 @@ public class SymmetricEncryptionTest {
decrypted = new ByteArrayOutputStream();
Streams.pipeAll(decryptor, decrypted);
StreamUtil.pipeAll(decryptor, decrypted);
decryptor.close();
assertArrayEquals(plaintext, decrypted.toByteArray());
@ -118,7 +118,7 @@ public class SymmetricEncryptionTest {
EncryptionOptions.encryptCommunications()
.addPassphrase(Passphrase.fromPassword("mellon"))));
Streams.pipeAll(new ByteArrayInputStream(bytes), encryptor);
StreamUtil.pipeAll(new ByteArrayInputStream(bytes), encryptor);
encryptor.close();
assertThrows(MissingDecryptionMethodException.class, () -> PGPainless.decryptAndOrVerify()

View File

@ -25,7 +25,6 @@ import java.nio.charset.StandardCharsets;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.util.io.Streams;
import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless;
import org.pgpainless.algorithm.EncryptionPurpose;
@ -35,6 +34,7 @@ import org.pgpainless.encryption_signing.EncryptionStream;
import org.pgpainless.encryption_signing.ProducerOptions;
import org.pgpainless.key.WeirdKeys;
import org.pgpainless.key.util.KeyRingUtils;
import org.pgpainless.util.StreamUtil;
public class TestTwoSubkeysEncryption {
@ -68,7 +68,7 @@ public class TestTwoSubkeysEncryption {
.setAsciiArmor(false)
);
Streams.pipeAll(getPlainIn(), encryptionStream);
StreamUtil.pipeAll(getPlainIn(), encryptionStream);
encryptionStream.close();
EncryptionResult metadata = encryptionStream.getResult();

View File

@ -23,8 +23,8 @@ import java.nio.charset.Charset;
import java.util.Arrays;
import org.bouncycastle.bcpg.ArmoredOutputStream;
import org.bouncycastle.util.io.Streams;
import org.pgpainless.util.ArmoredOutputStreamFactory;
import org.pgpainless.util.StreamUtil;
import sop.Ready;
import sop.enums.ArmorLabel;
import sop.exception.SOPGPException;
@ -57,10 +57,10 @@ public class ArmorImpl implements Armor {
int read = pbIn.read(buffer);
pbIn.unread(buffer, 0, read);
if (!allowNested && Arrays.equals(ARMOR_START, buffer)) {
Streams.pipeAll(pbIn, System.out);
StreamUtil.pipeAll(pbIn, System.out);
} else {
ArmoredOutputStream armor = ArmoredOutputStreamFactory.get(System.out);
Streams.pipeAll(pbIn, armor);
StreamUtil.pipeAll(pbIn, armor);
armor.close();
}
}

View File

@ -20,7 +20,7 @@ import java.io.InputStream;
import java.io.OutputStream;
import org.bouncycastle.openpgp.PGPUtil;
import org.bouncycastle.util.io.Streams;
import org.pgpainless.util.StreamUtil;
import sop.Ready;
import sop.operation.Dearmor;
@ -32,7 +32,7 @@ public class DearmorImpl implements Dearmor {
return new Ready() {
@Override
public void writeTo(OutputStream outputStream) throws IOException {
Streams.pipeAll(decoder, outputStream);
StreamUtil.pipeAll(decoder, outputStream);
decoder.close();
}
};

View File

@ -27,7 +27,6 @@ import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
import org.bouncycastle.openpgp.PGPSignature;
import org.bouncycastle.util.io.Streams;
import org.pgpainless.PGPainless;
import org.pgpainless.decryption_verification.ConsumerOptions;
import org.pgpainless.decryption_verification.DecryptionStream;
@ -37,6 +36,7 @@ import org.pgpainless.key.SubkeyIdentifier;
import org.pgpainless.key.info.KeyRingInfo;
import org.pgpainless.key.protection.SecretKeyRingProtector;
import org.pgpainless.util.Passphrase;
import org.pgpainless.util.StreamUtil;
import sop.DecryptionResult;
import sop.ReadyWithResult;
import sop.SessionKey;
@ -151,7 +151,7 @@ public class DecryptImpl implements Decrypt {
return new ReadyWithResult<DecryptionResult>() {
@Override
public DecryptionResult writeTo(OutputStream outputStream) throws IOException, SOPGPException.NoSignature {
Streams.pipeAll(decryptionStream, outputStream);
StreamUtil.pipeAll(decryptionStream, outputStream);
decryptionStream.close();
OpenPgpMetadata metadata = decryptionStream.getResult();

View File

@ -22,7 +22,6 @@ import java.io.OutputStream;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
import org.bouncycastle.util.io.Streams;
import org.pgpainless.PGPainless;
import org.pgpainless.algorithm.DocumentSignatureType;
import org.pgpainless.algorithm.StreamEncoding;
@ -33,11 +32,12 @@ import org.pgpainless.encryption_signing.SigningOptions;
import org.pgpainless.exception.WrongPassphraseException;
import org.pgpainless.key.protection.SecretKeyRingProtector;
import org.pgpainless.util.Passphrase;
import sop.util.ProxyOutputStream;
import org.pgpainless.util.StreamUtil;
import sop.Ready;
import sop.enums.EncryptAs;
import sop.exception.SOPGPException;
import sop.operation.Encrypt;
import sop.util.ProxyOutputStream;
public class EncryptImpl implements Encrypt {
@ -117,7 +117,7 @@ public class EncryptImpl implements Encrypt {
@Override
public void writeTo(OutputStream outputStream) throws IOException {
proxy.replaceOutputStream(outputStream);
Streams.pipeAll(plaintext, encryptionStream);
StreamUtil.pipeAll(plaintext, encryptionStream);
encryptionStream.close();
}
};

View File

@ -25,7 +25,6 @@ import java.util.List;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPSignature;
import org.bouncycastle.util.io.Streams;
import org.pgpainless.PGPainless;
import org.pgpainless.algorithm.DocumentSignatureType;
import org.pgpainless.encryption_signing.EncryptionResult;
@ -36,6 +35,7 @@ import org.pgpainless.key.SubkeyIdentifier;
import org.pgpainless.key.info.KeyRingInfo;
import org.pgpainless.key.protection.SecretKeyRingProtector;
import org.pgpainless.util.ArmoredOutputStreamFactory;
import org.pgpainless.util.StreamUtil;
import sop.Ready;
import sop.enums.SignAs;
import sop.exception.SOPGPException;
@ -92,7 +92,7 @@ public class SignImpl implements Sign {
throw new IllegalStateException("EncryptionStream is already closed.");
}
Streams.pipeAll(data, signingStream);
StreamUtil.pipeAll(data, signingStream);
signingStream.close();
EncryptionResult encryptionResult = signingStream.getResult();