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

Port more tests

This commit is contained in:
Paul Schaub 2025-04-02 15:37:19 +02:00
parent c0207f50e9
commit bad49de6aa
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.bcpg.KeyIdentifier;
import org.bouncycastle.openpgp.PGPException; 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.OpenPGPCertificate;
import org.bouncycastle.openpgp.api.OpenPGPKey;
import org.bouncycastle.util.io.Streams; import org.bouncycastle.util.io.Streams;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
@ -40,18 +39,18 @@ import org.pgpainless.util.Passphrase;
public class MissingPassphraseForDecryptionTest { public class MissingPassphraseForDecryptionTest {
private final String passphrase = "dragon123"; private final String passphrase = "dragon123";
private PGPSecretKeyRing secretKeys; private OpenPGPKey secretKeys;
private byte[] message; private byte[] message;
private final PGPainless api = PGPainless.getInstance();
@BeforeEach @BeforeEach
public void setup() throws PGPException, IOException { public void setup() throws PGPException, IOException {
secretKeys = PGPainless.generateKeyRing().modernKeyRing("Test", passphrase) secretKeys = api.generateKey().modernKeyRing("Test", passphrase);
.getPGPSecretKeyRing(); OpenPGPCertificate certificate = secretKeys.toCertificate();
PGPPublicKeyRing certificate = PGPainless.extractCertificate(secretKeys);
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
EncryptionStream encryptionStream = PGPainless.encryptAndOrSign() EncryptionStream encryptionStream = api.generateMessage()
.onOutputStream(out) .onOutputStream(out)
.withOptions(ProducerOptions.encrypt(EncryptionOptions.encryptCommunications() .withOptions(ProducerOptions.encrypt(EncryptionOptions.encryptCommunications(api)
.addRecipient(certificate))); .addRecipient(certificate)));
Streams.pipeAll(new ByteArrayInputStream("Hey, what's up?".getBytes(StandardCharsets.UTF_8)), encryptionStream); Streams.pipeAll(new ByteArrayInputStream("Hey, what's up?".getBytes(StandardCharsets.UTF_8)), encryptionStream);
@ -74,7 +73,7 @@ public class MissingPassphraseForDecryptionTest {
return true; return true;
} }
}; };
ConsumerOptions options = ConsumerOptions.get() ConsumerOptions options = ConsumerOptions.get(api)
.setMissingKeyPassphraseStrategy(MissingKeyPassphraseStrategy.INTERACTIVE) .setMissingKeyPassphraseStrategy(MissingKeyPassphraseStrategy.INTERACTIVE)
.addDecryptionKey(secretKeys, SecretKeyRingProtector.defaultSecretKeyRingProtector(callback)); .addDecryptionKey(secretKeys, SecretKeyRingProtector.defaultSecretKeyRingProtector(callback));
@ -91,7 +90,7 @@ public class MissingPassphraseForDecryptionTest {
@Test @Test
public void throwExceptionStrategy() throws PGPException, IOException { public void throwExceptionStrategy() throws PGPException, IOException {
KeyRingInfo info = PGPainless.inspectKeyRing(secretKeys); KeyRingInfo info = api.inspect(secretKeys);
List<OpenPGPCertificate.OpenPGPComponentKey> encryptionKeys = List<OpenPGPCertificate.OpenPGPComponentKey> encryptionKeys =
info.getEncryptionSubkeys(EncryptionPurpose.ANY); info.getEncryptionSubkeys(EncryptionPurpose.ANY);
@ -108,7 +107,7 @@ public class MissingPassphraseForDecryptionTest {
} }
}; };
ConsumerOptions options = ConsumerOptions.get() ConsumerOptions options = ConsumerOptions.get(api)
.setMissingKeyPassphraseStrategy(MissingKeyPassphraseStrategy.THROW_EXCEPTION) .setMissingKeyPassphraseStrategy(MissingKeyPassphraseStrategy.THROW_EXCEPTION)
.addDecryptionKey(secretKeys, SecretKeyRingProtector.defaultSecretKeyRingProtector(callback)); .addDecryptionKey(secretKeys, SecretKeyRingProtector.defaultSecretKeyRingProtector(callback));
@ -121,7 +120,7 @@ public class MissingPassphraseForDecryptionTest {
assertFalse(e.getKeyIds().isEmpty()); assertFalse(e.getKeyIds().isEmpty());
assertEquals(encryptionKeys.size(), e.getKeyIds().size()); assertEquals(encryptionKeys.size(), e.getKeyIds().size());
for (OpenPGPCertificate.OpenPGPComponentKey encryptionKey : encryptionKeys) { 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.bcpg.CompressionAlgorithmTags;
import org.bouncycastle.openpgp.PGPCompressedDataGenerator; import org.bouncycastle.openpgp.PGPCompressedDataGenerator;
import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPSecretKeyRing; import org.bouncycastle.openpgp.api.OpenPGPKey;
import org.bouncycastle.util.io.Streams; import org.bouncycastle.util.io.Streams;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless; import org.pgpainless.PGPainless;
@ -735,15 +735,15 @@ public class OpenPgpInputStreamTest {
@Test @Test
public void testSignedMessageConsumption() throws PGPException, IOException { public void testSignedMessageConsumption() throws PGPException, IOException {
PGPainless api = PGPainless.getInstance();
ByteArrayInputStream plaintext = new ByteArrayInputStream("Hello, World!\n".getBytes(StandardCharsets.UTF_8)); ByteArrayInputStream plaintext = new ByteArrayInputStream("Hello, World!\n".getBytes(StandardCharsets.UTF_8));
PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing() OpenPGPKey secretKeys = api.generateKey()
.modernKeyRing("Sigmund <sigmund@exmplample.com>") .modernKeyRing("Sigmund <sigmund@exmplample.com>");
.getPGPSecretKeyRing();
ByteArrayOutputStream signedOut = new ByteArrayOutputStream(); ByteArrayOutputStream signedOut = new ByteArrayOutputStream();
EncryptionStream signer = PGPainless.encryptAndOrSign() EncryptionStream signer = api.generateMessage()
.onOutputStream(signedOut) .onOutputStream(signedOut)
.withOptions(ProducerOptions.sign(SigningOptions.get() .withOptions(ProducerOptions.sign(SigningOptions.get(api)
.addSignature(SecretKeyRingProtector.unprotectedKeys(), secretKeys)) .addSignature(SecretKeyRingProtector.unprotectedKeys(), secretKeys))
.setAsciiArmor(false) .setAsciiArmor(false)
.overrideCompressionAlgorithm(CompressionAlgorithm.UNCOMPRESSED)); .overrideCompressionAlgorithm(CompressionAlgorithm.UNCOMPRESSED));

View file

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