2021-10-07 15:48:52 +02:00
|
|
|
|
// SPDX-FileCopyrightText: 2018 Paul Schaub <vanitasvitae@fsfe.org>
|
|
|
|
|
//
|
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
2020-01-11 13:11:14 +01:00
|
|
|
|
package org.pgpainless.encryption_signing;
|
2018-06-07 18:12:13 +02:00
|
|
|
|
|
2020-11-13 16:31:59 +01:00
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
2021-04-26 13:38:12 +02:00
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
2020-11-13 16:31:59 +01:00
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
2018-06-07 18:12:13 +02:00
|
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
|
import java.io.IOException;
|
2018-06-08 15:29:09 +02:00
|
|
|
|
import java.nio.charset.Charset;
|
2018-06-10 17:12:44 +02:00
|
|
|
|
import java.security.InvalidAlgorithmParameterException;
|
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
2020-08-24 14:55:06 +02:00
|
|
|
|
import java.util.Set;
|
2018-06-07 18:12:13 +02:00
|
|
|
|
|
2020-08-24 14:55:06 +02:00
|
|
|
|
import org.bouncycastle.bcpg.ArmoredOutputStream;
|
2018-06-07 18:12:13 +02:00
|
|
|
|
import org.bouncycastle.openpgp.PGPException;
|
|
|
|
|
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
|
|
|
|
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
2020-08-24 14:55:06 +02:00
|
|
|
|
import org.bouncycastle.openpgp.PGPSignature;
|
2021-07-31 20:40:31 +02:00
|
|
|
|
import org.bouncycastle.util.io.Streams;
|
2021-07-01 21:33:38 +02:00
|
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
2021-12-14 16:55:04 +01:00
|
|
|
|
import org.junit.jupiter.api.TestTemplate;
|
|
|
|
|
import org.junit.jupiter.api.extension.ExtendWith;
|
2020-01-11 13:11:14 +01:00
|
|
|
|
import org.pgpainless.PGPainless;
|
2021-05-06 00:04:03 +02:00
|
|
|
|
import org.pgpainless.algorithm.DocumentSignatureType;
|
2018-07-18 18:23:06 +02:00
|
|
|
|
import org.pgpainless.algorithm.KeyFlag;
|
|
|
|
|
import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
|
2021-06-16 15:38:02 +02:00
|
|
|
|
import org.pgpainless.decryption_verification.ConsumerOptions;
|
2018-07-18 18:23:06 +02:00
|
|
|
|
import org.pgpainless.decryption_verification.DecryptionStream;
|
2018-07-23 16:23:23 +02:00
|
|
|
|
import org.pgpainless.decryption_verification.OpenPgpMetadata;
|
2022-03-15 14:04:59 +01:00
|
|
|
|
import org.pgpainless.exception.KeyException;
|
2021-05-06 00:04:03 +02:00
|
|
|
|
import org.pgpainless.key.SubkeyIdentifier;
|
2020-01-12 18:36:03 +01:00
|
|
|
|
import org.pgpainless.key.TestKeys;
|
2018-07-18 18:23:06 +02:00
|
|
|
|
import org.pgpainless.key.generation.KeySpec;
|
2020-11-07 18:24:12 +01:00
|
|
|
|
import org.pgpainless.key.generation.type.KeyType;
|
2021-02-25 23:27:08 +01:00
|
|
|
|
import org.pgpainless.key.generation.type.elgamal.ElGamal;
|
2020-12-08 20:02:41 +01:00
|
|
|
|
import org.pgpainless.key.generation.type.elgamal.ElGamalLength;
|
|
|
|
|
import org.pgpainless.key.generation.type.rsa.RsaLength;
|
2018-07-18 18:23:06 +02:00
|
|
|
|
import org.pgpainless.key.protection.SecretKeyRingProtector;
|
|
|
|
|
import org.pgpainless.key.protection.UnprotectedKeysProtector;
|
2020-12-08 19:14:52 +01:00
|
|
|
|
import org.pgpainless.key.util.KeyRingUtils;
|
2021-07-01 21:33:38 +02:00
|
|
|
|
import org.pgpainless.policy.Policy;
|
2021-02-19 19:50:36 +01:00
|
|
|
|
import org.pgpainless.util.ArmoredOutputStreamFactory;
|
2021-12-14 16:56:29 +01:00
|
|
|
|
import org.pgpainless.util.TestAllImplementations;
|
2018-06-07 18:12:13 +02:00
|
|
|
|
|
2020-01-10 19:03:04 +01:00
|
|
|
|
public class EncryptDecryptTest {
|
2018-06-07 18:12:13 +02:00
|
|
|
|
|
2020-01-10 22:07:54 +01:00
|
|
|
|
// Don't use StandardCharsets.UTF_8 because of Android API level.
|
2021-12-28 13:32:50 +01:00
|
|
|
|
@SuppressWarnings("CharsetObjectCanBeUsed")
|
2018-06-10 17:12:44 +02:00
|
|
|
|
private static final Charset UTF8 = Charset.forName("UTF-8");
|
2018-06-07 18:12:13 +02:00
|
|
|
|
|
2020-01-10 22:07:54 +01:00
|
|
|
|
private static final String testMessage =
|
|
|
|
|
"Ah, Juliet, if the measure of thy joy\n" +
|
2021-06-16 15:38:02 +02:00
|
|
|
|
"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.";
|
2018-06-13 17:26:48 +02:00
|
|
|
|
|
2021-07-01 21:33:38 +02:00
|
|
|
|
@BeforeEach
|
|
|
|
|
public void setDefaultPolicy() {
|
|
|
|
|
PGPainless.getPolicy().setSymmetricKeyEncryptionAlgorithmPolicy(
|
|
|
|
|
Policy.SymmetricKeyAlgorithmPolicy.defaultSymmetricKeyEncryptionAlgorithmPolicy());
|
|
|
|
|
PGPainless.getPolicy().setSymmetricKeyDecryptionAlgorithmPolicy(
|
|
|
|
|
Policy.SymmetricKeyAlgorithmPolicy.defaultSymmetricKeyDecryptionAlgorithmPolicy());
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-14 16:55:04 +01:00
|
|
|
|
@TestTemplate
|
2021-12-14 16:56:29 +01:00
|
|
|
|
@ExtendWith(TestAllImplementations.class)
|
2021-12-14 16:55:04 +01:00
|
|
|
|
public void freshKeysRsaToElGamalTest()
|
2020-01-10 22:07:54 +01:00
|
|
|
|
throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, IOException {
|
2020-12-08 19:14:52 +01:00
|
|
|
|
PGPSecretKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("romeo@montague.lit", RsaLength._3072);
|
2021-11-02 12:23:05 +01:00
|
|
|
|
PGPSecretKeyRing recipient = PGPainless.buildKeyRing()
|
2021-09-20 12:30:03 +02:00
|
|
|
|
.setPrimaryKey(KeySpec.getBuilder(
|
2021-09-13 19:20:19 +02:00
|
|
|
|
KeyType.RSA(RsaLength._4096),
|
2021-09-20 12:30:03 +02:00
|
|
|
|
KeyFlag.SIGN_DATA, KeyFlag.CERTIFY_OTHER))
|
|
|
|
|
.addSubkey(KeySpec.getBuilder(
|
|
|
|
|
ElGamal.withLength(ElGamalLength._3072),
|
|
|
|
|
KeyFlag.ENCRYPT_STORAGE, KeyFlag.ENCRYPT_COMMS))
|
|
|
|
|
.addUserId("juliet@capulet.lit").build();
|
2018-06-28 13:23:40 +02:00
|
|
|
|
|
|
|
|
|
encryptDecryptForSecretKeyRings(sender, recipient);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-14 16:55:04 +01:00
|
|
|
|
@TestTemplate
|
2021-12-14 16:56:29 +01:00
|
|
|
|
@ExtendWith(TestAllImplementations.class)
|
2021-12-14 16:55:04 +01:00
|
|
|
|
public void freshKeysRsaToRsaTest()
|
2020-01-10 22:07:54 +01:00
|
|
|
|
throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, IOException {
|
2020-12-08 19:14:52 +01:00
|
|
|
|
PGPSecretKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("romeo@montague.lit", RsaLength._3072);
|
|
|
|
|
PGPSecretKeyRing recipient = PGPainless.generateKeyRing().simpleRsaKeyRing("juliet@capulet.lit", RsaLength._3072);
|
2018-06-10 17:12:44 +02:00
|
|
|
|
|
2018-06-10 17:55:51 +02:00
|
|
|
|
encryptDecryptForSecretKeyRings(sender, recipient);
|
2018-06-07 18:12:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-14 16:55:04 +01:00
|
|
|
|
@TestTemplate
|
2021-12-14 16:56:29 +01:00
|
|
|
|
@ExtendWith(TestAllImplementations.class)
|
2021-12-14 16:55:04 +01:00
|
|
|
|
public void freshKeysEcToEcTest()
|
2020-01-10 22:07:54 +01:00
|
|
|
|
throws IOException, PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException {
|
2020-12-08 19:14:52 +01:00
|
|
|
|
PGPSecretKeyRing sender = PGPainless.generateKeyRing().simpleEcKeyRing("romeo@montague.lit");
|
|
|
|
|
PGPSecretKeyRing recipient = PGPainless.generateKeyRing().simpleEcKeyRing("juliet@capulet.lit");
|
2018-06-10 17:12:44 +02:00
|
|
|
|
|
2018-06-10 17:55:51 +02:00
|
|
|
|
encryptDecryptForSecretKeyRings(sender, recipient);
|
2018-06-07 18:12:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-14 16:55:04 +01:00
|
|
|
|
@TestTemplate
|
2021-12-14 16:56:29 +01:00
|
|
|
|
@ExtendWith(TestAllImplementations.class)
|
2021-12-14 16:55:04 +01:00
|
|
|
|
public void freshKeysEcToRsaTest()
|
2020-01-10 22:07:54 +01:00
|
|
|
|
throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, IOException {
|
2020-12-08 19:14:52 +01:00
|
|
|
|
PGPSecretKeyRing sender = PGPainless.generateKeyRing().simpleEcKeyRing("romeo@montague.lit");
|
|
|
|
|
PGPSecretKeyRing recipient = PGPainless.generateKeyRing().simpleRsaKeyRing("juliet@capulet.lit", RsaLength._3072);
|
2018-06-07 18:12:13 +02:00
|
|
|
|
|
2018-06-10 17:55:51 +02:00
|
|
|
|
encryptDecryptForSecretKeyRings(sender, recipient);
|
2018-06-10 17:12:44 +02:00
|
|
|
|
}
|
2018-06-07 18:12:13 +02:00
|
|
|
|
|
2021-12-14 16:55:04 +01:00
|
|
|
|
@TestTemplate
|
2021-12-14 16:56:29 +01:00
|
|
|
|
@ExtendWith(TestAllImplementations.class)
|
2021-12-14 16:55:04 +01:00
|
|
|
|
public void freshKeysRsaToEcTest()
|
2020-01-10 22:07:54 +01:00
|
|
|
|
throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, IOException {
|
2020-12-08 19:14:52 +01:00
|
|
|
|
PGPSecretKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("romeo@montague.lit", RsaLength._3072);
|
|
|
|
|
PGPSecretKeyRing recipient = PGPainless.generateKeyRing().simpleEcKeyRing("juliet@capulet.lit");
|
2018-06-07 18:12:13 +02:00
|
|
|
|
|
2018-06-10 17:55:51 +02:00
|
|
|
|
encryptDecryptForSecretKeyRings(sender, recipient);
|
2018-06-10 17:12:44 +02:00
|
|
|
|
}
|
2018-06-07 18:12:13 +02:00
|
|
|
|
|
2021-12-14 16:55:04 +01:00
|
|
|
|
@TestTemplate
|
2021-12-14 16:56:29 +01:00
|
|
|
|
@ExtendWith(TestAllImplementations.class)
|
2021-12-14 16:55:04 +01:00
|
|
|
|
public void existingRsaKeysTest() throws IOException, PGPException {
|
2020-12-08 19:14:52 +01:00
|
|
|
|
PGPSecretKeyRing sender = TestKeys.getJulietSecretKeyRing();
|
|
|
|
|
PGPSecretKeyRing recipient = TestKeys.getRomeoSecretKeyRing();
|
2020-01-12 18:36:03 +01:00
|
|
|
|
|
|
|
|
|
encryptDecryptForSecretKeyRings(sender, recipient);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-08 19:14:52 +01:00
|
|
|
|
private void encryptDecryptForSecretKeyRings(PGPSecretKeyRing senderSec, PGPSecretKeyRing recipientSec)
|
2020-01-10 22:07:54 +01:00
|
|
|
|
throws PGPException, IOException {
|
2020-12-08 19:14:52 +01:00
|
|
|
|
|
|
|
|
|
PGPPublicKeyRing recipientPub = KeyRingUtils.publicKeyRingFrom(recipientSec);
|
|
|
|
|
PGPPublicKeyRing senderPub = KeyRingUtils.publicKeyRingFrom(senderSec);
|
2018-06-07 18:12:13 +02:00
|
|
|
|
|
2018-06-10 17:12:44 +02:00
|
|
|
|
SecretKeyRingProtector keyDecryptor = new UnprotectedKeysProtector();
|
2018-06-07 18:12:13 +02:00
|
|
|
|
|
2018-06-21 15:35:40 +02:00
|
|
|
|
byte[] secretMessage = testMessage.getBytes(UTF8);
|
2018-06-07 18:12:13 +02:00
|
|
|
|
|
2018-06-10 17:12:44 +02:00
|
|
|
|
ByteArrayOutputStream envelope = new ByteArrayOutputStream();
|
2018-06-07 18:12:13 +02:00
|
|
|
|
|
2020-11-29 15:33:54 +01:00
|
|
|
|
EncryptionStream encryptor = PGPainless.encryptAndOrSign()
|
2018-06-10 17:12:44 +02:00
|
|
|
|
.onOutputStream(envelope)
|
2021-06-29 16:43:37 +02:00
|
|
|
|
.withOptions(ProducerOptions.signAndEncrypt(
|
|
|
|
|
EncryptionOptions.encryptCommunications().addRecipient(recipientPub),
|
|
|
|
|
new SigningOptions().addInlineSignature(keyDecryptor, senderSec, DocumentSignatureType.BINARY_DOCUMENT)
|
|
|
|
|
));
|
2018-06-07 18:12:13 +02:00
|
|
|
|
|
2021-07-31 20:40:31 +02:00
|
|
|
|
Streams.pipeAll(new ByteArrayInputStream(secretMessage), encryptor);
|
2020-01-10 15:12:04 +01:00
|
|
|
|
encryptor.close();
|
|
|
|
|
byte[] encryptedSecretMessage = envelope.toByteArray();
|
|
|
|
|
|
2021-05-06 00:04:03 +02:00
|
|
|
|
EncryptionResult encryptionResult = encryptor.getResult();
|
2018-06-27 15:09:39 +02:00
|
|
|
|
|
2021-05-06 00:04:03 +02:00
|
|
|
|
assertFalse(encryptionResult.getRecipients().isEmpty());
|
|
|
|
|
for (SubkeyIdentifier encryptionKey : encryptionResult.getRecipients()) {
|
2021-05-29 12:43:31 +02:00
|
|
|
|
assertTrue(KeyRingUtils.keyRingContainsKeyWithId(recipientPub, encryptionKey.getKeyId()));
|
2018-06-27 15:09:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-10 12:42:03 +02:00
|
|
|
|
assertEquals(SymmetricKeyAlgorithm.AES_256, encryptionResult.getEncryptionAlgorithm());
|
2018-06-27 15:09:39 +02:00
|
|
|
|
|
2018-06-10 17:12:44 +02:00
|
|
|
|
// Juliet trieth to comprehend Romeos words
|
2018-06-07 18:12:13 +02:00
|
|
|
|
|
2018-06-10 17:12:44 +02:00
|
|
|
|
ByteArrayInputStream envelopeIn = new ByteArrayInputStream(encryptedSecretMessage);
|
2020-11-29 15:33:54 +01:00
|
|
|
|
DecryptionStream decryptor = PGPainless.decryptAndOrVerify()
|
2018-06-10 17:12:44 +02:00
|
|
|
|
.onInputStream(envelopeIn)
|
2021-06-16 15:38:02 +02:00
|
|
|
|
.withOptions(new ConsumerOptions()
|
|
|
|
|
.addDecryptionKey(recipientSec, keyDecryptor)
|
|
|
|
|
.addVerificationCert(senderPub)
|
|
|
|
|
);
|
2018-06-07 18:12:13 +02:00
|
|
|
|
|
2018-06-21 15:35:40 +02:00
|
|
|
|
ByteArrayOutputStream decryptedSecretMessage = new ByteArrayOutputStream();
|
2018-06-10 17:12:44 +02:00
|
|
|
|
|
2021-07-31 20:40:31 +02:00
|
|
|
|
Streams.pipeAll(decryptor, decryptedSecretMessage);
|
2018-06-07 18:12:13 +02:00
|
|
|
|
decryptor.close();
|
|
|
|
|
|
2020-01-10 22:07:54 +01:00
|
|
|
|
assertArrayEquals(secretMessage, decryptedSecretMessage.toByteArray());
|
2018-07-23 16:23:23 +02:00
|
|
|
|
OpenPgpMetadata result = decryptor.getResult();
|
2018-06-10 17:55:51 +02:00
|
|
|
|
assertTrue(result.containsVerifiedSignatureFrom(senderPub));
|
|
|
|
|
assertTrue(result.isSigned());
|
|
|
|
|
assertTrue(result.isEncrypted());
|
|
|
|
|
assertTrue(result.isVerified());
|
2018-06-07 18:12:13 +02:00
|
|
|
|
}
|
2020-08-24 14:55:06 +02:00
|
|
|
|
|
2021-12-14 16:55:04 +01:00
|
|
|
|
@TestTemplate
|
2021-12-14 16:56:29 +01:00
|
|
|
|
@ExtendWith(TestAllImplementations.class)
|
2021-12-14 16:55:04 +01:00
|
|
|
|
public void testDetachedSignatureCreationAndVerification() throws IOException, PGPException {
|
2021-02-25 23:27:08 +01:00
|
|
|
|
|
2020-12-08 19:14:52 +01:00
|
|
|
|
PGPSecretKeyRing signingKeys = TestKeys.getJulietSecretKeyRing();
|
2020-08-24 14:55:06 +02:00
|
|
|
|
SecretKeyRingProtector keyRingProtector = new UnprotectedKeysProtector();
|
|
|
|
|
byte[] data = testMessage.getBytes();
|
|
|
|
|
ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
|
|
|
|
|
ByteArrayOutputStream dummyOut = new ByteArrayOutputStream();
|
2020-11-29 15:33:54 +01:00
|
|
|
|
EncryptionStream signer = PGPainless.encryptAndOrSign().onOutputStream(dummyOut)
|
2021-06-29 16:43:37 +02:00
|
|
|
|
.withOptions(ProducerOptions.sign(
|
|
|
|
|
new SigningOptions().addDetachedSignature(keyRingProtector, signingKeys, DocumentSignatureType.BINARY_DOCUMENT)
|
|
|
|
|
));
|
2021-07-31 20:40:31 +02:00
|
|
|
|
Streams.pipeAll(inputStream, signer);
|
2020-08-24 14:55:06 +02:00
|
|
|
|
signer.close();
|
|
|
|
|
|
2021-05-06 00:04:03 +02:00
|
|
|
|
EncryptionResult metadata = signer.getResult();
|
|
|
|
|
|
|
|
|
|
Set<PGPSignature> signatureSet = metadata.getDetachedSignatures().get(metadata.getDetachedSignatures().keySet().iterator().next());
|
2020-08-24 14:55:06 +02:00
|
|
|
|
ByteArrayOutputStream sigOut = new ByteArrayOutputStream();
|
2021-02-19 19:50:36 +01:00
|
|
|
|
ArmoredOutputStream armorOut = ArmoredOutputStreamFactory.get(sigOut);
|
2020-08-24 14:55:06 +02:00
|
|
|
|
signatureSet.iterator().next().encode(armorOut);
|
|
|
|
|
armorOut.close();
|
|
|
|
|
String armorSig = sigOut.toString();
|
|
|
|
|
|
2020-08-24 16:26:29 +02:00
|
|
|
|
// CHECKSTYLE:OFF
|
2020-08-24 14:55:06 +02:00
|
|
|
|
System.out.println(armorSig);
|
2020-08-24 16:26:29 +02:00
|
|
|
|
// CHECKSTYLE:ON
|
2020-08-24 14:55:06 +02:00
|
|
|
|
|
|
|
|
|
inputStream = new ByteArrayInputStream(testMessage.getBytes());
|
2021-06-16 15:38:02 +02:00
|
|
|
|
DecryptionStream verifier = PGPainless.decryptAndOrVerify()
|
|
|
|
|
.onInputStream(inputStream)
|
|
|
|
|
.withOptions(new ConsumerOptions()
|
|
|
|
|
.addVerificationOfDetachedSignatures(new ByteArrayInputStream(armorSig.getBytes()))
|
|
|
|
|
.addVerificationCert(KeyRingUtils.publicKeyRingFrom(signingKeys))
|
|
|
|
|
);
|
|
|
|
|
|
2020-08-24 14:55:06 +02:00
|
|
|
|
dummyOut = new ByteArrayOutputStream();
|
2021-07-31 20:40:31 +02:00
|
|
|
|
Streams.pipeAll(verifier, dummyOut);
|
2020-08-24 14:55:06 +02:00
|
|
|
|
verifier.close();
|
|
|
|
|
|
2021-05-06 00:04:03 +02:00
|
|
|
|
OpenPgpMetadata decryptionResult = verifier.getResult();
|
|
|
|
|
assertFalse(decryptionResult.getVerifiedSignatures().isEmpty());
|
2020-08-24 14:55:06 +02:00
|
|
|
|
}
|
2020-08-30 13:16:47 +02:00
|
|
|
|
|
2021-12-14 16:55:04 +01:00
|
|
|
|
@TestTemplate
|
2021-12-14 16:56:29 +01:00
|
|
|
|
@ExtendWith(TestAllImplementations.class)
|
2021-12-14 16:55:04 +01:00
|
|
|
|
public void testOnePassSignatureCreationAndVerification() throws IOException, PGPException {
|
2020-12-08 19:14:52 +01:00
|
|
|
|
PGPSecretKeyRing signingKeys = TestKeys.getJulietSecretKeyRing();
|
2020-08-30 13:16:47 +02:00
|
|
|
|
SecretKeyRingProtector keyRingProtector = new UnprotectedKeysProtector();
|
|
|
|
|
byte[] data = testMessage.getBytes();
|
|
|
|
|
ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
|
|
|
|
|
ByteArrayOutputStream signOut = new ByteArrayOutputStream();
|
2020-11-29 15:33:54 +01:00
|
|
|
|
EncryptionStream signer = PGPainless.encryptAndOrSign().onOutputStream(signOut)
|
2021-06-29 16:43:37 +02:00
|
|
|
|
.withOptions(ProducerOptions.sign(
|
|
|
|
|
SigningOptions.get()
|
2021-09-13 19:20:19 +02:00
|
|
|
|
.addInlineSignature(keyRingProtector, signingKeys, DocumentSignatureType.BINARY_DOCUMENT)
|
2021-06-29 16:43:37 +02:00
|
|
|
|
).setAsciiArmor(true));
|
2021-07-31 20:40:31 +02:00
|
|
|
|
Streams.pipeAll(inputStream, signer);
|
2020-08-30 13:16:47 +02:00
|
|
|
|
signer.close();
|
|
|
|
|
|
|
|
|
|
inputStream = new ByteArrayInputStream(signOut.toByteArray());
|
2021-06-16 15:38:02 +02:00
|
|
|
|
DecryptionStream verifier = PGPainless.decryptAndOrVerify()
|
|
|
|
|
.onInputStream(inputStream)
|
|
|
|
|
.withOptions(new ConsumerOptions()
|
|
|
|
|
.addVerificationCert(KeyRingUtils.publicKeyRingFrom(signingKeys))
|
|
|
|
|
);
|
2020-08-30 13:16:47 +02:00
|
|
|
|
signOut = new ByteArrayOutputStream();
|
2021-07-31 20:40:31 +02:00
|
|
|
|
Streams.pipeAll(verifier, signOut);
|
2020-08-30 13:16:47 +02:00
|
|
|
|
verifier.close();
|
|
|
|
|
|
|
|
|
|
OpenPgpMetadata metadata = verifier.getResult();
|
|
|
|
|
assertFalse(metadata.getVerifiedSignatures().isEmpty());
|
|
|
|
|
}
|
2021-04-26 13:38:12 +02:00
|
|
|
|
|
2021-12-14 16:55:04 +01:00
|
|
|
|
@TestTemplate
|
2021-12-14 16:56:29 +01:00
|
|
|
|
@ExtendWith(TestAllImplementations.class)
|
2021-12-14 16:55:04 +01:00
|
|
|
|
public void expiredSubkeyBacksigTest() throws IOException {
|
2021-04-26 13:38:12 +02:00
|
|
|
|
String key = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n" +
|
|
|
|
|
"\n" +
|
|
|
|
|
"xsDNBF2lnPIBDAC5cL9PQoQLTMuhjbYvb4Ncuuo0bfmgPRFywX53jPhoFf4Zg6mv\n" +
|
|
|
|
|
"/seOXpgecTdOcVttfzC8ycIKrt3aQTiwOG/ctaR4Bk/t6ayNFfdUNxHWk4WCKzdz\n" +
|
|
|
|
|
"/56fW2O0F23qIRd8UUJp5IIlN4RDdRCtdhVQIAuzvp2oVy/LaS2kxQoKvph/5pQ/\n" +
|
|
|
|
|
"5whqsyroEWDJoSV0yOb25B/iwk/pLUFoyhDG9bj0kIzDxrEqW+7Ba8nocQlecMF3\n" +
|
|
|
|
|
"X5KMN5kp2zraLv9dlBBpWW43XktjcCZgMy20SouraVma8Je/ECwUWYUiAZxLIlMv\n" +
|
|
|
|
|
"9CurEOtxUw6N3RdOtLmYZS9uEnn5y1UkF88o8Nku890uk6BrewFzJyLAx5wRZ4F0\n" +
|
|
|
|
|
"qV/yq36UWQ0JB/AUGhHVPdFf6pl6eaxBwT5GXvbBUibtf8YI2og5RsgTWtXfU7eb\n" +
|
|
|
|
|
"SGXrl5ZMpbA6mbfhd0R8aPxWfmDWiIOhBufhMCvUHh1sApMKVZnvIff9/0Dca3wb\n" +
|
|
|
|
|
"vLIwa3T4CyshfT0AEQEAAc0hQm9iIEJhYmJhZ2UgPGJvYkBvcGVucGdwLmV4YW1w\n" +
|
|
|
|
|
"bGU+wsFIBBMBCgB8BYJfUbC4AgsJCRD7/MgqAV5zMEcUAAAAAAAeACBzYWx0QG5v\n" +
|
|
|
|
|
"dGF0aW9ucy5zZXF1b2lhLXBncC5vcmcEc0Prq/Ohwr794nDXrgZXDdDq38GOMsus\n" +
|
|
|
|
|
"hDqEwk/zJgMVCAoCmwECHgEWIQTRpm4aI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|
|
|
|
"kNWZhZWijw1SxPGVWlKVizi+pWZyX9NBrTAj/ES/HZrLda52PR1BKSE4kG74T/73\n" +
|
|
|
|
|
"V/jnqYp0jGI/M3y79DRq2tlO5p6Jp+OcmU2SyvItaNhoateGndLIVPZfAT69avbY\n" +
|
|
|
|
|
"tMoEbsA/biVL4xN9SqaLian4ow9/pVm/z4Ej6zSRZUC01hZBQWD02z0ntU7t0CPR\n" +
|
|
|
|
|
"R58XC9znFiEE0aZuGiOxgsmYD3iM+/zIKgFeczAAAED5C/975SfSeub9RJHilYFA\n" +
|
|
|
|
|
"eeeHU6ZaSpOy0/ZrwSUFmvDrxowiCNn7sYZEZmIBVZ/nIlfbCUUTesIF92aLkIZe\n" +
|
|
|
|
|
"EMQUiXP0/HtnAx1duQ8htdb+X/EhuWPPJ7hF5bA6AB1oXVKn3lpggHzauGSilI5m\n" +
|
|
|
|
|
"dPXXVdDUWuDQfSn459UOv4PwB52uLtGZK3iprVgYD3RzSWktHMhMvcB2GXNQlfyo\n" +
|
|
|
|
|
"yWewq9p+wwbIFUFZYMRIGjJNSc6aQcEHusIn85E+Uid/hrDIiblbvQA+7ONcoaqL\n" +
|
|
|
|
|
"DiLSL+bh1/usrmzccUK01nLMmTnG03vU3WR3yqmDlzgU/S3XfZRPECwr6AzNSXoe\n" +
|
|
|
|
|
"d4u9/SPt2VBxGtZ0yA4PXgO6PbZC6EIZqmgW5oKjSWZwkryQLGKji+vYJU1FzM+3\n" +
|
|
|
|
|
"qO6PYqLVGf97n6LS2xD10rrJ2aUq0CQ/M5ykRVsT6HifV9wPiPzR8ilcXWRT8CQ2\n" +
|
|
|
|
|
"Ks2WqI282/DM+Lq/GCSd2nXtS3/KwErTFiF1uHi/N3TwdWA=\n" +
|
|
|
|
|
"=j1TE\n" +
|
|
|
|
|
"-----END PGP PUBLIC KEY BLOCK-----\n";
|
|
|
|
|
|
|
|
|
|
PGPPublicKeyRing publicKeys = PGPainless.readKeyRing().publicKeyRing(key);
|
2021-06-29 16:43:37 +02:00
|
|
|
|
|
2022-03-15 14:04:59 +01:00
|
|
|
|
assertThrows(KeyException.UnacceptableEncryptionKeyException.class, () ->
|
2021-06-29 16:43:37 +02:00
|
|
|
|
EncryptionOptions.encryptCommunications()
|
|
|
|
|
.addRecipient(publicKeys));
|
2021-04-26 13:38:12 +02:00
|
|
|
|
}
|
2018-06-07 18:12:13 +02:00
|
|
|
|
}
|