From c7ad460516954f85a635b3507604479b05652a46 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Sun, 11 Aug 2024 13:42:01 +0200 Subject: [PATCH] Add DecryptSeipdV2Test --- .../pgpainless/DecryptSeipdV2Test.java | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 pgpainless-sop/src/test/java/sop/testsuite/pgpainless/DecryptSeipdV2Test.java diff --git a/pgpainless-sop/src/test/java/sop/testsuite/pgpainless/DecryptSeipdV2Test.java b/pgpainless-sop/src/test/java/sop/testsuite/pgpainless/DecryptSeipdV2Test.java new file mode 100644 index 00000000..3aaa3c7b --- /dev/null +++ b/pgpainless-sop/src/test/java/sop/testsuite/pgpainless/DecryptSeipdV2Test.java @@ -0,0 +1,96 @@ +// SPDX-FileCopyrightText: 2024 Paul Schaub +// +// SPDX-License-Identifier: Apache-2.0 + +package sop.testsuite.pgpainless; + +import org.bouncycastle.openpgp.PGPException; +import org.bouncycastle.openpgp.PGPSecretKeyRing; +import org.bouncycastle.util.io.Streams; +import org.junit.jupiter.api.Test; +import org.pgpainless.PGPainless; +import org.pgpainless.decryption_verification.ConsumerOptions; +import org.pgpainless.decryption_verification.DecryptionStream; +import org.pgpainless.sop.SOPImpl; +import sop.ByteArrayAndResult; +import sop.SOP; +import sop.Verification; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertFalse; + +public class DecryptSeipdV2Test { + + @Test + public void testDecryptionOfPKESK6SEIPD2Message() throws IOException, PGPException { + // Test message generated using sq + String msg = "-----BEGIN PGP MESSAGE-----\n" + + "\n" + + "wW0GIQYSyD8ecG9jCP4VGkF3Q6HwM3kOk+mXhIjR2zeNqZMIhRmIA5PducqZ21f5\n" + + "YQVwrKzMnclNp2qQMk94NIFRydzgQij/XnCVsh2KiZ2KAqAaDzRAzCC44X2zcEYa\n" + + "hDlDitRJDyIy+WqJnlSw0loCCQIGMUNKfc/Fx6J4968MksBh5w7HmCamhqE6exup\n" + + "bnHBi/kGB8rz2RGCJpH06zRVBYBl/voL/frzdvttIETbPp+Dwru+exD/MyrWpS6r\n" + + "PuJNzr6MLp9ukHk=\n" + + "=Eisa\n" + + "-----END PGP MESSAGE-----\n"; + String key = "-----BEGIN PGP PRIVATE KEY BLOCK-----\n" + + "\n" + + "xUsGY4d/4xsAAAAg+U2nu0jWCmHlZ3BqZYfQMxmZu52JGggkLq2EVD34laMAGXKB\n" + + "exK+cH6NX1hs5hNhIB00TrJmosgv3mg1ditlsLfCsQYfGwoAAABCBYJjh3/jAwsJ\n" + + "BwUVCg4IDAIWAAKbAwIeCSIhBssYbE8GCaaX5NUt+mxyKwwfHifBilZwj2Ul7Ce6\n" + + "2azJBScJAgcCAAAAAK0oIBA+LX0ifsDm185Ecds2v8lwgyU2kCcUmKfvBXbAf6rh\n" + + "RYWzuQOwEn7E/aLwIwRaLsdry0+VcallHhSu4RN6HWaEQsiPlR4zxP/TP7mhfVEe\n" + + "7XWPxtnMUMtf15OyA51YBMdLBmOHf+MZAAAAIIaTJINn+eUBXbki+PSAld2nhJh/\n" + + "LVmFsS+60WyvXkQ1AE1gCk95TUR3XFeibg/u/tVY6a//1q0NWC1X+yui3O24wpsG\n" + + "GBsKAAAALAWCY4d/4wKbDCIhBssYbE8GCaaX5NUt+mxyKwwfHifBilZwj2Ul7Ce6\n" + + "2azJAAAAAAQBIKbpGG2dWTX8j+VjFM21J0hqWlEg+bdiojWnKfA5AQpWUWtnNwDE\n" + + "M0g12vYxoWM8Y81W+bHBw805I8kWVkXU6vFOi+HWvv/ira7ofJu16NnoUkhclkUr\n" + + "k0mXubZvyl4GBg==\n" + + "-----END PGP PRIVATE KEY BLOCK-----\n"; + PGPSecretKeyRing k = PGPainless.readKeyRing().secretKeyRing(key); + DecryptionStream d = PGPainless.decryptAndOrVerify() + .onInputStream(new ByteArrayInputStream(msg.getBytes(StandardCharsets.UTF_8))) + .withOptions(ConsumerOptions.get().addDecryptionKey(k)); + Streams.drain(d); + d.close(); + } + + @Test + public void inlineSignVerifyTest() throws IOException { + String KEY = "-----BEGIN PGP PRIVATE KEY BLOCK-----\n" + + "\n" + + "xUsGY4d/4xsAAAAg+U2nu0jWCmHlZ3BqZYfQMxmZu52JGggkLq2EVD34laMAGXKB\n" + + "exK+cH6NX1hs5hNhIB00TrJmosgv3mg1ditlsLfCsQYfGwoAAABCBYJjh3/jAwsJ\n" + + "BwUVCg4IDAIWAAKbAwIeCSIhBssYbE8GCaaX5NUt+mxyKwwfHifBilZwj2Ul7Ce6\n" + + "2azJBScJAgcCAAAAAK0oIBA+LX0ifsDm185Ecds2v8lwgyU2kCcUmKfvBXbAf6rh\n" + + "RYWzuQOwEn7E/aLwIwRaLsdry0+VcallHhSu4RN6HWaEQsiPlR4zxP/TP7mhfVEe\n" + + "7XWPxtnMUMtf15OyA51YBMdLBmOHf+MZAAAAIIaTJINn+eUBXbki+PSAld2nhJh/\n" + + "LVmFsS+60WyvXkQ1AE1gCk95TUR3XFeibg/u/tVY6a//1q0NWC1X+yui3O24wpsG\n" + + "GBsKAAAALAWCY4d/4wKbDCIhBssYbE8GCaaX5NUt+mxyKwwfHifBilZwj2Ul7Ce6\n" + + "2azJAAAAAAQBIKbpGG2dWTX8j+VjFM21J0hqWlEg+bdiojWnKfA5AQpWUWtnNwDE\n" + + "M0g12vYxoWM8Y81W+bHBw805I8kWVkXU6vFOi+HWvv/ira7ofJu16NnoUkhclkUr\n" + + "k0mXubZvyl4GBg==\n" + + "-----END PGP PRIVATE KEY BLOCK-----\n"; + SOP sop = new SOPImpl(); + byte[] signed = sop.inlineSign() + .key(KEY.getBytes(StandardCharsets.UTF_8)) + .data("Hello".getBytes(StandardCharsets.UTF_8)) + .getBytes(); + + byte[] cert = sop.extractCert() + .noArmor() + .key(KEY.getBytes(StandardCharsets.UTF_8)) + .getBytes(); + + ByteArrayAndResult> result = sop.inlineVerify() + .cert(cert) + .data(signed) + .toByteArrayAndResult(); + + assertFalse(result.getResult().isEmpty()); + } +}