Add SignatureUtils.readSignatures(byte[])

This commit is contained in:
Paul Schaub 2021-08-26 19:35:25 +02:00
parent 1a274bf91b
commit e19acb667c
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
2 changed files with 31 additions and 17 deletions

View File

@ -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<PGPSignature> 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<PGPSignature> readSignatures(byte[] encodedSignatures) throws IOException, PGPException {
InputStream inputStream = new ByteArrayInputStream(encodedSignatures);
return readSignatures(inputStream);
}

View File

@ -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 musics 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();