1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-09-09 18:29:39 +02:00

Port more tests

This commit is contained in:
Paul Schaub 2025-04-02 15:37:19 +02:00
parent bb64188473
commit 9f35be1b0e
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
3 changed files with 51 additions and 48 deletions

View file

@ -18,9 +18,8 @@ import java.util.List;
import org.bouncycastle.bcpg.KeyIdentifier;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.api.OpenPGPCertificate;
import org.bouncycastle.openpgp.api.OpenPGPKey;
import org.bouncycastle.util.io.Streams;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.BeforeEach;
@ -40,18 +39,18 @@ import org.pgpainless.util.Passphrase;
public class MissingPassphraseForDecryptionTest {
private final String passphrase = "dragon123";
private PGPSecretKeyRing secretKeys;
private OpenPGPKey secretKeys;
private byte[] message;
private final PGPainless api = PGPainless.getInstance();
@BeforeEach
public void setup() throws PGPException, IOException {
secretKeys = PGPainless.generateKeyRing().modernKeyRing("Test", passphrase)
.getPGPSecretKeyRing();
PGPPublicKeyRing certificate = PGPainless.extractCertificate(secretKeys);
secretKeys = api.generateKey().modernKeyRing("Test", passphrase);
OpenPGPCertificate certificate = secretKeys.toCertificate();
ByteArrayOutputStream out = new ByteArrayOutputStream();
EncryptionStream encryptionStream = PGPainless.encryptAndOrSign()
EncryptionStream encryptionStream = api.generateMessage()
.onOutputStream(out)
.withOptions(ProducerOptions.encrypt(EncryptionOptions.encryptCommunications()
.withOptions(ProducerOptions.encrypt(EncryptionOptions.encryptCommunications(api)
.addRecipient(certificate)));
Streams.pipeAll(new ByteArrayInputStream("Hey, what's up?".getBytes(StandardCharsets.UTF_8)), encryptionStream);
@ -74,7 +73,7 @@ public class MissingPassphraseForDecryptionTest {
return true;
}
};
ConsumerOptions options = ConsumerOptions.get()
ConsumerOptions options = ConsumerOptions.get(api)
.setMissingKeyPassphraseStrategy(MissingKeyPassphraseStrategy.INTERACTIVE)
.addDecryptionKey(secretKeys, SecretKeyRingProtector.defaultSecretKeyRingProtector(callback));
@ -91,7 +90,7 @@ public class MissingPassphraseForDecryptionTest {
@Test
public void throwExceptionStrategy() throws PGPException, IOException {
KeyRingInfo info = PGPainless.inspectKeyRing(secretKeys);
KeyRingInfo info = api.inspect(secretKeys);
List<OpenPGPCertificate.OpenPGPComponentKey> encryptionKeys =
info.getEncryptionSubkeys(EncryptionPurpose.ANY);
@ -108,7 +107,7 @@ public class MissingPassphraseForDecryptionTest {
}
};
ConsumerOptions options = ConsumerOptions.get()
ConsumerOptions options = ConsumerOptions.get(api)
.setMissingKeyPassphraseStrategy(MissingKeyPassphraseStrategy.THROW_EXCEPTION)
.addDecryptionKey(secretKeys, SecretKeyRingProtector.defaultSecretKeyRingProtector(callback));
@ -121,7 +120,7 @@ public class MissingPassphraseForDecryptionTest {
assertFalse(e.getKeyIds().isEmpty());
assertEquals(encryptionKeys.size(), e.getKeyIds().size());
for (OpenPGPCertificate.OpenPGPComponentKey encryptionKey : encryptionKeys) {
assertTrue(e.getKeyIds().contains(new SubkeyIdentifier(secretKeys, encryptionKey.getKeyIdentifier())));
assertTrue(e.getKeyIds().contains(new SubkeyIdentifier(encryptionKey)));
}
}
}

View file

@ -19,7 +19,7 @@ import org.bouncycastle.bcpg.ArmoredInputStream;
import org.bouncycastle.bcpg.CompressionAlgorithmTags;
import org.bouncycastle.openpgp.PGPCompressedDataGenerator;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.api.OpenPGPKey;
import org.bouncycastle.util.io.Streams;
import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless;
@ -735,15 +735,15 @@ public class OpenPgpInputStreamTest {
@Test
public void testSignedMessageConsumption() throws PGPException, IOException {
PGPainless api = PGPainless.getInstance();
ByteArrayInputStream plaintext = new ByteArrayInputStream("Hello, World!\n".getBytes(StandardCharsets.UTF_8));
PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing()
.modernKeyRing("Sigmund <sigmund@exmplample.com>")
.getPGPSecretKeyRing();
OpenPGPKey secretKeys = api.generateKey()
.modernKeyRing("Sigmund <sigmund@exmplample.com>");
ByteArrayOutputStream signedOut = new ByteArrayOutputStream();
EncryptionStream signer = PGPainless.encryptAndOrSign()
EncryptionStream signer = api.generateMessage()
.onOutputStream(signedOut)
.withOptions(ProducerOptions.sign(SigningOptions.get()
.withOptions(ProducerOptions.sign(SigningOptions.get(api)
.addSignature(SecretKeyRingProtector.unprotectedKeys(), secretKeys))
.setAsciiArmor(false)
.overrideCompressionAlgorithm(CompressionAlgorithm.UNCOMPRESSED));

View file

@ -26,9 +26,9 @@ import org.bouncycastle.openpgp.PGPCompressedDataGenerator;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPLiteralData;
import org.bouncycastle.openpgp.PGPLiteralDataGenerator;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPSignature;
import org.bouncycastle.openpgp.api.OpenPGPCertificate;
import org.bouncycastle.openpgp.api.OpenPGPKey;
import org.bouncycastle.util.io.Streams;
import org.junit.JUtils;
import org.junit.jupiter.api.Named;
@ -162,6 +162,8 @@ public class OpenPgpMessageInputStreamTest {
"=tkTV\n" +
"-----END PGP MESSAGE-----";
private static final PGPainless api = PGPainless.getInstance();
public static void main(String[] args) throws Exception {
// genLIT();
// genLIT_LIT();
@ -237,21 +239,22 @@ public class OpenPgpMessageInputStreamTest {
armorOut.close();
}
public static void genKey() {
PGPainless.asciiArmor(
PGPainless.generateKeyRing().modernKeyRing("Alice <alice@pgpainless.org>")
.getPGPSecretKeyRing(),
System.out);
public static void genKey() throws IOException {
OpenPGPKey key = api.generateKey()
.modernKeyRing("Alice <alice@pgpainless.org>");
// CHECKSTYLE:OFF
System.out.println(key.toAsciiArmoredString());
// CHECKSTYLE:ON
}
public static void genSIG_COMP_LIT() throws PGPException, IOException {
PGPSecretKeyRing secretKeys = PGPainless.readKeyRing().secretKeyRing(KEY);
OpenPGPKey secretKeys = api.readKey().parseKey(KEY);
ByteArrayOutputStream msgOut = new ByteArrayOutputStream();
EncryptionStream signer = PGPainless.encryptAndOrSign()
EncryptionStream signer = api.generateMessage()
.onOutputStream(msgOut)
.withOptions(
ProducerOptions.sign(
SigningOptions.get()
SigningOptions.get(api)
.addDetachedSignature(SecretKeyRingProtector.unprotectedKeys(), secretKeys)
).setAsciiArmor(false)
);
@ -277,9 +280,9 @@ public class OpenPgpMessageInputStreamTest {
}
public static void genSENC_LIT() throws PGPException, IOException {
EncryptionStream enc = PGPainless.encryptAndOrSign()
EncryptionStream enc = api.generateMessage()
.onOutputStream(System.out)
.withOptions(ProducerOptions.encrypt(EncryptionOptions.get()
.withOptions(ProducerOptions.encrypt(EncryptionOptions.get(api)
.addMessagePassphrase(Passphrase.fromPassword(PASSPHRASE)))
.overrideCompressionAlgorithm(CompressionAlgorithm.UNCOMPRESSED));
enc.write(PLAINTEXT.getBytes(StandardCharsets.UTF_8));
@ -287,11 +290,11 @@ public class OpenPgpMessageInputStreamTest {
}
public static void genPENC_COMP_LIT() throws IOException, PGPException {
PGPSecretKeyRing secretKeys = PGPainless.readKeyRing().secretKeyRing(KEY);
PGPPublicKeyRing cert = PGPainless.extractCertificate(secretKeys);
EncryptionStream enc = PGPainless.encryptAndOrSign()
OpenPGPKey secretKeys = api.readKey().parseKey(KEY);
OpenPGPCertificate cert = secretKeys.toCertificate();
EncryptionStream enc = api.generateMessage()
.onOutputStream(System.out)
.withOptions(ProducerOptions.encrypt(EncryptionOptions.get()
.withOptions(ProducerOptions.encrypt(EncryptionOptions.get(api)
.addRecipient(cert))
.overrideCompressionAlgorithm(CompressionAlgorithm.ZLIB));
@ -300,11 +303,11 @@ public class OpenPgpMessageInputStreamTest {
}
public static void genOPS_LIT_SIG() throws PGPException, IOException {
PGPSecretKeyRing secretKeys = PGPainless.readKeyRing().secretKeyRing(KEY);
OpenPGPKey secretKeys = api.readKey().parseKey(KEY);
EncryptionStream enc = PGPainless.encryptAndOrSign()
EncryptionStream enc = api.generateMessage()
.onOutputStream(System.out)
.withOptions(ProducerOptions.sign(SigningOptions.get()
.withOptions(ProducerOptions.sign(SigningOptions.get(api)
.addSignature(SecretKeyRingProtector.unprotectedKeys(), secretKeys))
.overrideCompressionAlgorithm(CompressionAlgorithm.UNCOMPRESSED));
Streams.pipeAll(new ByteArrayInputStream(PLAINTEXT.getBytes(StandardCharsets.UTF_8)), enc);
@ -398,8 +401,8 @@ public class OpenPgpMessageInputStreamTest {
@MethodSource("provideMessageProcessors")
void testProcessSIG_COMP_LIT(Processor processor)
throws PGPException, IOException {
PGPPublicKeyRing cert = PGPainless.extractCertificate(
PGPainless.readKeyRing().secretKeyRing(KEY));
OpenPGPKey key = api.readKey().parseKey(KEY);
OpenPGPCertificate cert = key.toCertificate();
Result result = processor.process(SIG_COMP_LIT, ConsumerOptions.get()
.addVerificationCert(cert));
@ -431,8 +434,8 @@ public class OpenPgpMessageInputStreamTest {
@MethodSource("provideMessageProcessors")
void testProcessPENC_COMP_LIT(Processor processor)
throws IOException, PGPException {
PGPSecretKeyRing secretKeys = PGPainless.readKeyRing().secretKeyRing(KEY);
Result result = processor.process(PENC_COMP_LIT, ConsumerOptions.get()
OpenPGPKey secretKeys = api.readKey().parseKey(KEY);
Result result = processor.process(PENC_COMP_LIT, ConsumerOptions.get(api)
.addDecryptionKey(secretKeys));
String plain = result.plaintext;
assertEquals(PLAINTEXT, plain);
@ -447,7 +450,8 @@ public class OpenPgpMessageInputStreamTest {
@MethodSource("provideMessageProcessors")
void testProcessOPS_LIT_SIG(Processor processor)
throws IOException, PGPException {
PGPPublicKeyRing cert = PGPainless.extractCertificate(PGPainless.readKeyRing().secretKeyRing(KEY));
OpenPGPKey key = api.readKey().parseKey(KEY);
OpenPGPCertificate cert = key.toCertificate();
Result result = processor.process(OPS_LIT_SIG, ConsumerOptions.get()
.addVerificationCert(cert));
String plain = result.plaintext;
@ -581,10 +585,10 @@ public class OpenPgpMessageInputStreamTest {
"s7O7MH2b1YkDPsTDuLoDjBzDRoA+2vi034km9Qdcs3w8+vrydw4=\n" +
"=mdYs\n" +
"-----END PGP MESSAGE-----\n";
PGPSecretKeyRing secretKeys = PGPainless.readKeyRing().secretKeyRing(BOB_KEY);
PGPPublicKeyRing certificate = PGPainless.extractCertificate(secretKeys);
OpenPGPKey secretKeys = api.readKey().parseKey(BOB_KEY);
OpenPGPCertificate certificate = secretKeys.toCertificate();
Result result = processor.process(MSG, ConsumerOptions.get()
Result result = processor.process(MSG, ConsumerOptions.get(api)
.addVerificationCert(certificate)
.addDecryptionKey(secretKeys));
String plain = result.plaintext;
@ -646,10 +650,10 @@ public class OpenPgpMessageInputStreamTest {
"x12WVuyITVU3fCfHp6/0A6wPtJezCvoodqPlw/3fd5eSVYzb5C3v564uhz4=\n" +
"=JP9T\n" +
"-----END PGP MESSAGE-----";
PGPSecretKeyRing secretKeys = PGPainless.readKeyRing().secretKeyRing(BOB_KEY);
PGPPublicKeyRing certificate = PGPainless.extractCertificate(secretKeys);
OpenPGPKey secretKeys = api.readKey().parseKey(BOB_KEY);
OpenPGPCertificate certificate = secretKeys.toCertificate();
Result result = processor.process(MSG, ConsumerOptions.get()
Result result = processor.process(MSG, ConsumerOptions.get(api)
.addVerificationCert(certificate)
.addDecryptionKey(secretKeys));
String plain = result.plaintext;