From e19acb667ce648925519bb58213175c7d15b53d5 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Thu, 26 Aug 2021 19:35:25 +0200 Subject: [PATCH] Add SignatureUtils.readSignatures(byte[]) --- .../pgpainless/signature/SignatureUtils.java | 18 +++++++++-- .../CleartextSignatureVerificationTest.java | 30 +++++++++---------- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/pgpainless-core/src/main/java/org/pgpainless/signature/SignatureUtils.java b/pgpainless-core/src/main/java/org/pgpainless/signature/SignatureUtils.java index a59cefdc..25f9d9f4 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/signature/SignatureUtils.java +++ b/pgpainless-core/src/main/java/org/pgpainless/signature/SignatureUtils.java @@ -217,14 +217,28 @@ public final class SignatureUtils { } /** - * Parse an ASCII encoded list of OpenPGP signatures into a {@link PGPSignatureList}. + * Parse an ASCII encoded list of OpenPGP signatures into a {@link PGPSignatureList} + * and return it as a {@link List}. * * @param encodedSignatures ASCII armored signature list * @return signature list * @throws IOException if the signatures cannot be read */ public static List readSignatures(String encodedSignatures) throws IOException, PGPException { - InputStream inputStream = new ByteArrayInputStream(encodedSignatures.getBytes(Charset.forName("UTF8"))); + byte[] bytes = encodedSignatures.getBytes(Charset.forName("UTF8")); + return readSignatures(bytes); + } + + /** + * Read a single, or a list of {@link PGPSignature PGPSignatures} and return them as a {@link List}. + * + * @param encodedSignatures ASCII armored or binary signatures + * @return signatures + * @throws IOException if the signatures cannot be read + * @throws PGPException in case of an OpenPGP error + */ + public static List readSignatures(byte[] encodedSignatures) throws IOException, PGPException { + InputStream inputStream = new ByteArrayInputStream(encodedSignatures); return readSignatures(inputStream); } diff --git a/pgpainless-core/src/test/java/org/pgpainless/decryption_verification/CleartextSignatureVerificationTest.java b/pgpainless-core/src/test/java/org/pgpainless/decryption_verification/CleartextSignatureVerificationTest.java index 0ffb47b4..bc362185 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/decryption_verification/CleartextSignatureVerificationTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/decryption_verification/CleartextSignatureVerificationTest.java @@ -44,13 +44,13 @@ import org.pgpainless.util.TestUtils; public class CleartextSignatureVerificationTest { - public static final String MESSAGE_BODY = "Ah, Juliet, if the measure of thy joy\n" + + public static final byte[] MESSAGE_BODY = ("Ah, Juliet, if the measure of thy joy\n" + "Be heaped like mine, and that thy skill be more\n" + "To blazon it, then sweeten with thy breath\n" + "This neighbor air, and let rich music’s tongue\n" + "Unfold the imagined happiness that both\n" + - "Receive in either by this dear encounter."; - public static final String MESSAGE_SIGNED = "-----BEGIN PGP SIGNED MESSAGE-----\n" + + "Receive in either by this dear encounter.").getBytes(StandardCharsets.UTF_8); + public static final byte[] MESSAGE_SIGNED = ("-----BEGIN PGP SIGNED MESSAGE-----\n" + "Hash: SHA512\n" + "\n" + "Ah, Juliet, if the measure of thy joy\n" + @@ -65,14 +65,14 @@ public class CleartextSignatureVerificationTest { "DFRwAP9/4wMvV3WcX59Clo7mkRce6iwW3VBdiN+yMu3tjmHB2wD/RfE28Q1v4+eo\n" + "ySNgbyvqYYsNr0fnBwaG3aaj+u5ExiE=\n" + "=Z2SO\n" + - "-----END PGP SIGNATURE-----"; - public static final String SIGNATURE = "-----BEGIN PGP SIGNATURE-----\n" + + "-----END PGP SIGNATURE-----").getBytes(StandardCharsets.UTF_8); + public static final byte[] SIGNATURE = ("-----BEGIN PGP SIGNATURE-----\n" + "\n" + "iHUEARMKAB0WIQRPZlxNwsRmC8ZCXkFXNuaTGs83DAUCYJ/x5gAKCRBXNuaTGs83\n" + "DFRwAP9/4wMvV3WcX59Clo7mkRce6iwW3VBdiN+yMu3tjmHB2wD/RfE28Q1v4+eo\n" + "ySNgbyvqYYsNr0fnBwaG3aaj+u5ExiE=\n" + "=Z2SO\n" + - "-----END PGP SIGNATURE-----"; + "-----END PGP SIGNATURE-----").getBytes(StandardCharsets.UTF_8); @Test public void cleartextSignVerification_InMemoryMultiPassStrategy() throws IOException, PGPException { @@ -80,14 +80,14 @@ public class CleartextSignatureVerificationTest { InMemoryMultiPassStrategy multiPassStrategy = MultiPassStrategy.keepMessageInMemory(); CleartextSignatureProcessor processor = PGPainless.verifyCleartextSignedMessage() - .onInputStream(new ByteArrayInputStream(MESSAGE_SIGNED.getBytes(StandardCharsets.UTF_8))) + .onInputStream(new ByteArrayInputStream(MESSAGE_SIGNED)) .withStrategy(multiPassStrategy) .verifyWith(signingKeys); PGPSignature signature = processor.process(); assertEquals(signature.getKeyID(), signingKeys.getPublicKey().getKeyID()); - assertArrayEquals(MESSAGE_BODY.getBytes(StandardCharsets.UTF_8), multiPassStrategy.getBytes()); + assertArrayEquals(MESSAGE_BODY, multiPassStrategy.getBytes()); } @Test @@ -98,7 +98,7 @@ public class CleartextSignatureVerificationTest { File file = new File(tempDir, "file"); MultiPassStrategy multiPassStrategy = MultiPassStrategy.writeMessageToFile(file); CleartextSignatureProcessor processor = PGPainless.verifyCleartextSignedMessage() - .onInputStream(new ByteArrayInputStream(MESSAGE_SIGNED.getBytes(StandardCharsets.UTF_8))) + .onInputStream(new ByteArrayInputStream(MESSAGE_SIGNED)) .withStrategy(multiPassStrategy) .verifyWith(signingKeys); @@ -109,7 +109,7 @@ public class CleartextSignatureVerificationTest { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); Streams.pipeAll(fileIn, bytes); fileIn.close(); - assertArrayEquals(MESSAGE_BODY.getBytes(StandardCharsets.UTF_8), bytes.toByteArray()); + assertArrayEquals(MESSAGE_BODY, bytes.toByteArray()); } @Test @@ -119,7 +119,7 @@ public class CleartextSignatureVerificationTest { PGPSignature signature = SignatureUtils.readSignatures(SIGNATURE).get(0); PGPPublicKey signingKey = signingKeys.getPublicKey(signature.getKeyID()); - SignatureVerifier.initializeSignatureAndUpdateWithSignedData(signature, new ByteArrayInputStream(MESSAGE_BODY.getBytes(StandardCharsets.UTF_8)), signingKey); + SignatureVerifier.initializeSignatureAndUpdateWithSignedData(signature, new ByteArrayInputStream(MESSAGE_BODY), signingKey); CertificateValidator.validateCertificateAndVerifyInitializedSignature(signature, signingKeys, PGPainless.getPolicy()); } @@ -128,9 +128,9 @@ public class CleartextSignatureVerificationTest { // CHECKSTYLE:OFF PGPPublicKeyRing keys = TestKeys.getEmilPublicKeyRing(); System.out.println(ArmorUtils.toAsciiArmoredString(keys)); - System.out.println(MESSAGE_SIGNED); - System.out.println(MESSAGE_BODY); - System.out.println(SIGNATURE); + System.out.println(new String(MESSAGE_SIGNED)); + System.out.println(new String(MESSAGE_BODY)); + System.out.println(new String(SIGNATURE)); // CHECKSTYLE:ON } @@ -143,7 +143,7 @@ public class CleartextSignatureVerificationTest { .addVerificationOfDetachedSignature(signature); DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify() - .onInputStream(new ByteArrayInputStream(MESSAGE_BODY.getBytes(StandardCharsets.UTF_8))) + .onInputStream(new ByteArrayInputStream(MESSAGE_BODY)) .withOptions(options); ByteArrayOutputStream out = new ByteArrayOutputStream();