1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-15 17:02:06 +01:00
pgpainless/pgpainless-core/src/test/java/investigations/CanonicalizedDataEncryptionTest.java

220 lines
11 KiB
Java
Raw Normal View History

// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package investigations;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
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.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless;
import org.pgpainless.algorithm.DocumentSignatureType;
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.encryption_signing.EncryptionOptions;
import org.pgpainless.encryption_signing.EncryptionStream;
import org.pgpainless.encryption_signing.ProducerOptions;
import org.pgpainless.encryption_signing.SigningOptions;
import org.pgpainless.key.protection.SecretKeyRingProtector;
public class CanonicalizedDataEncryptionTest {
private static final String KEY = "-----BEGIN PGP PRIVATE KEY BLOCK-----\n" +
"Version: PGPainless\n" +
"Comment: 9AF4 29C4 C389 CC11 1739 98E9 9F8E E9C5 3AE5 C1A4\n" +
"Comment: Test <test@vanitasvi.tae>\n" +
"\n" +
"lQcYBGI8Y/cBEACHIx1hfYeTHZ39UGM5kuJBuvJOZXR60DppIkgjPWyc+p2mxXY5\n" +
"tOl+xVSzWHudogtxM1kbpYghPXWOj7ssh7V+4OI1JIi3ODEuWozRN1HjqyY11ORg\n" +
"ky6lmbZ0/YupTFbZ6H4yMoHbLPugN2fA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QJvbvLgisl11VFWR7RNhJ9xNPbUTknCw1Ftp0nSXEnS\n" +
"gl/0Z7KWoiY8sAn3o45KZRnq8uiF19kYXdrRWIFo1LtG68hjOYYRG5ejmCt6zx53\n" +
"Zd+AyZA+lkh8uI921Nnio2g70zVVSKEVaJcWTlkVyKge2iV/YkQ=\n" +
"=EyDf\n" +
"-----END PGP PRIVATE KEY BLOCK-----\n";
String message = "Hello, World!\n";
private static PGPSecretKeyRing secretKeys;
private static PGPPublicKeyRing publicKeys;
@BeforeAll
public static void readKeys() throws IOException {
secretKeys = PGPainless.readKeyRing().secretKeyRing(KEY);
publicKeys = PGPainless.extractCertificate(secretKeys);
System.out.println(PGPainless.asciiArmor(secretKeys));
}
@Test
public void binaryDataBinarySig() throws PGPException, IOException {
String msg = encryptAndSign(message, DocumentSignatureType.BINARY_DOCUMENT, StreamEncoding.BINARY);
OpenPgpMetadata metadata = decryptAndVerify(msg);
if (!metadata.isVerified()) {
System.out.println("Not verified. Session-Key: " + metadata.getSessionKey());
System.out.println(msg);
fail();
}
}
@Test
public void binaryDataTextSig() throws PGPException, IOException {
String msg = encryptAndSign(message, DocumentSignatureType.CANONICAL_TEXT_DOCUMENT, StreamEncoding.BINARY);
OpenPgpMetadata metadata = decryptAndVerify(msg);
if (!metadata.isVerified()) {
System.out.println("Not verified. Session-Key: " + metadata.getSessionKey());
System.out.println(msg);
fail();
}
}
@Test
@Disabled("Fails")
public void textDataBinarySig() throws PGPException, IOException {
String msg = encryptAndSign(message, DocumentSignatureType.BINARY_DOCUMENT, StreamEncoding.TEXT);
OpenPgpMetadata metadata = decryptAndVerify(msg);
if (!metadata.isVerified()) {
System.out.println("Not verified. Session-Key: " + metadata.getSessionKey());
System.out.println(msg);
fail();
}
}
@Test
public void textDataTextSig() throws PGPException, IOException {
System.out.println("SignatureType: Text, LiteralData: Text");
String msg = encryptAndSign(message, DocumentSignatureType.CANONICAL_TEXT_DOCUMENT, StreamEncoding.TEXT);
OpenPgpMetadata metadata = decryptAndVerify(msg);
if (!metadata.isVerified()) {
System.out.println("Not verified. Session-Key: " + metadata.getSessionKey());
System.out.println(msg);
fail();
}
}
@Test
@Disabled("Fails")
public void utf8DataBinarySig() throws PGPException, IOException {
System.out.println("SignatureType: Binary, LiteralData: UTF8");
String msg = encryptAndSign(message, DocumentSignatureType.BINARY_DOCUMENT, StreamEncoding.UTF8);
OpenPgpMetadata metadata = decryptAndVerify(msg);
if (!metadata.isVerified()) {
System.out.println("Not verified. Session-Key: " + metadata.getSessionKey());
System.out.println(msg);
fail();
}
}
@Test
public void utf8DataTextSig() throws PGPException, IOException {
String msg = encryptAndSign(message, DocumentSignatureType.CANONICAL_TEXT_DOCUMENT, StreamEncoding.UTF8);
OpenPgpMetadata metadata = decryptAndVerify(msg);
if (!metadata.isVerified()) {
System.out.println("Not verified. Session-Key: " + metadata.getSessionKey());
System.out.println(msg);
fail();
}
}
private String encryptAndSign(String message, DocumentSignatureType sigType, StreamEncoding dataFormat)
throws PGPException, IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
EncryptionStream encryptionStream = PGPainless.encryptAndOrSign()
.onOutputStream(out)
.withOptions(ProducerOptions
.signAndEncrypt(
EncryptionOptions.encryptCommunications()
.addRecipient(publicKeys),
SigningOptions.get()
.addInlineSignature(SecretKeyRingProtector.unprotectedKeys(), secretKeys, sigType)
)
.setEncoding(dataFormat)
);
ByteArrayInputStream inputStream = new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8));
Streams.pipeAll(inputStream, encryptionStream);
encryptionStream.close();
String msg = out.toString();
return msg;
}
private OpenPgpMetadata decryptAndVerify(String msg) throws PGPException, IOException {
ByteArrayInputStream in = new ByteArrayInputStream(msg.getBytes(StandardCharsets.UTF_8));
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
.onInputStream(in)
.withOptions(new ConsumerOptions()
.addDecryptionKey(secretKeys, SecretKeyRingProtector.unprotectedKeys())
.addVerificationCert(publicKeys));
Streams.drain(decryptionStream);
decryptionStream.close();
return decryptionStream.getResult();
}
}