From 5266fa53c8541b985fbffea3cfb7a74ebaba5d0b Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Mon, 17 Feb 2025 14:36:43 +0100 Subject: [PATCH] Progress porting the example tests --- .../main/kotlin/org/pgpainless/PGPainless.kt | 4 +++ .../org/pgpainless/example/ConvertKeys.java | 10 +++--- .../java/org/pgpainless/example/Encrypt.java | 19 ++++++---- .../org/pgpainless/example/GenerateKeys.java | 35 +++++++++---------- 4 files changed, 38 insertions(+), 30 deletions(-) diff --git a/pgpainless-core/src/main/kotlin/org/pgpainless/PGPainless.kt b/pgpainless-core/src/main/kotlin/org/pgpainless/PGPainless.kt index d8670947..f9e009cf 100644 --- a/pgpainless-core/src/main/kotlin/org/pgpainless/PGPainless.kt +++ b/pgpainless-core/src/main/kotlin/org/pgpainless/PGPainless.kt @@ -132,6 +132,8 @@ class PGPainless( if (key is PGPSecretKeyRing) ArmorUtils.toAsciiArmoredString(key) else ArmorUtils.toAsciiArmoredString(key as PGPPublicKeyRing) + @JvmStatic fun asciiArmor(cert: OpenPGPCertificate) = asciiArmor(cert.pgpKeyRing) + /** * Wrap a key of certificate in ASCII armor and write the result into the given * [OutputStream]. @@ -204,6 +206,8 @@ class PGPainless( fun inspectKeyRing(key: PGPKeyRing, referenceTime: Date = Date()) = KeyRingInfo(key, referenceTime) + @JvmStatic + @JvmOverloads fun inspectKeyRing(key: OpenPGPCertificate, referenceTime: Date = Date()) = KeyRingInfo(key, getPolicy(), referenceTime) diff --git a/pgpainless-core/src/test/java/org/pgpainless/example/ConvertKeys.java b/pgpainless-core/src/test/java/org/pgpainless/example/ConvertKeys.java index d93fc5f4..36371ba4 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/example/ConvertKeys.java +++ b/pgpainless-core/src/test/java/org/pgpainless/example/ConvertKeys.java @@ -9,6 +9,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import org.bouncycastle.openpgp.PGPPublicKeyRing; import org.bouncycastle.openpgp.PGPSecretKeyRing; +import org.bouncycastle.openpgp.api.OpenPGPCertificate; +import org.bouncycastle.openpgp.api.OpenPGPKey; import org.junit.jupiter.api.Test; import org.pgpainless.PGPainless; import org.pgpainless.key.info.KeyRingInfo; @@ -21,11 +23,11 @@ public class ConvertKeys { @Test public void secretKeyToCertificate() { String userId = "alice@wonderland.lit"; - PGPSecretKeyRing secretKey = PGPainless.generateKeyRing() - .modernKeyRing(userId) - .getPGPSecretKeyRing(); + OpenPGPKey secretKey = PGPainless.generateKeyRing() + .modernKeyRing(userId); + // Extract certificate (public key) from secret key - PGPPublicKeyRing certificate = PGPainless.extractCertificate(secretKey); + OpenPGPCertificate certificate = secretKey.toCertificate(); KeyRingInfo secretKeyInfo = PGPainless.inspectKeyRing(secretKey); diff --git a/pgpainless-core/src/test/java/org/pgpainless/example/Encrypt.java b/pgpainless-core/src/test/java/org/pgpainless/example/Encrypt.java index d97891d8..f6e7d802 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/example/Encrypt.java +++ b/pgpainless-core/src/test/java/org/pgpainless/example/Encrypt.java @@ -15,6 +15,9 @@ import java.nio.charset.StandardCharsets; 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.openpgp.api.OpenPGPKeyReader; import org.bouncycastle.util.io.Streams; import org.junit.jupiter.api.Test; import org.pgpainless.PGPainless; @@ -133,12 +136,13 @@ public class Encrypt { @Test public void encryptAndSignMessage() throws PGPException, IOException { // Prepare keys - PGPSecretKeyRing keyAlice = PGPainless.readKeyRing().secretKeyRing(ALICE_KEY); - PGPPublicKeyRing certificateAlice = PGPainless.readKeyRing().publicKeyRing(ALICE_CERT); + OpenPGPKeyReader reader = PGPainless.getInstance().readKey(); + OpenPGPKey keyAlice = reader.parseKey(ALICE_KEY); + OpenPGPCertificate certificateAlice = reader.parseCertificate(ALICE_CERT); SecretKeyRingProtector protectorAlice = SecretKeyRingProtector.unprotectedKeys(); - PGPSecretKeyRing keyBob = PGPainless.readKeyRing().secretKeyRing(BOB_KEY); - PGPPublicKeyRing certificateBob = PGPainless.readKeyRing().publicKeyRing(BOB_CERT); + OpenPGPKey keyBob = reader.parseKey(BOB_KEY); + OpenPGPCertificate certificateBob = reader.parseCertificate(BOB_CERT); SecretKeyRingProtector protectorBob = SecretKeyRingProtector.unprotectedKeys(); // plaintext message to encrypt @@ -227,10 +231,11 @@ public class Encrypt { @Test public void encryptWithCommentHeader() throws PGPException, IOException { // Prepare keys - PGPPublicKeyRing certificateAlice = PGPainless.readKeyRing().publicKeyRing(ALICE_CERT); + OpenPGPKeyReader reader = PGPainless.getInstance().readKey(); + OpenPGPCertificate certificateAlice = reader.parseCertificate(ALICE_CERT); - PGPSecretKeyRing keyBob = PGPainless.readKeyRing().secretKeyRing(BOB_KEY); - PGPPublicKeyRing certificateBob = PGPainless.readKeyRing().publicKeyRing(BOB_CERT); + OpenPGPKey keyBob = reader.parseKey(BOB_KEY); + OpenPGPCertificate certificateBob = reader.parseCertificate(BOB_CERT); SecretKeyRingProtector protectorBob = SecretKeyRingProtector.unprotectedKeys(); // plaintext message to encrypt diff --git a/pgpainless-core/src/test/java/org/pgpainless/example/GenerateKeys.java b/pgpainless-core/src/test/java/org/pgpainless/example/GenerateKeys.java index 3954f94a..ddd74cb1 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/example/GenerateKeys.java +++ b/pgpainless-core/src/test/java/org/pgpainless/example/GenerateKeys.java @@ -9,8 +9,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Date; -import org.bouncycastle.openpgp.PGPPublicKeyRing; -import org.bouncycastle.openpgp.PGPSecretKeyRing; +import org.bouncycastle.openpgp.api.OpenPGPCertificate; +import org.bouncycastle.openpgp.api.OpenPGPKey; import org.junit.jupiter.api.Test; import org.pgpainless.PGPainless; import org.pgpainless.algorithm.CompressionAlgorithm; @@ -58,16 +58,16 @@ public class GenerateKeys { // Set a password to protect the secret key String password = "ra1nb0w"; // Generate the OpenPGP key - PGPSecretKeyRing secretKey = PGPainless.generateKeyRing() - .modernKeyRing(userId, password) - .getPGPSecretKeyRing(); + OpenPGPKey secretKey = PGPainless.generateKeyRing() + .modernKeyRing(userId, password); + // Extract public key - PGPPublicKeyRing publicKey = PGPainless.extractCertificate(secretKey); + OpenPGPCertificate publicKey = secretKey.toCertificate(); // Encode the public key to an ASCII armored string ready for sharing String asciiArmoredPublicKey = PGPainless.asciiArmor(publicKey); assertTrue(asciiArmoredPublicKey.startsWith("-----BEGIN PGP PUBLIC KEY BLOCK-----")); - KeyRingInfo keyInfo = new KeyRingInfo(secretKey); + KeyRingInfo keyInfo = PGPainless.inspectKeyRing(secretKey); assertEquals(3, keyInfo.getSecretKeys().size()); assertEquals(userId, keyInfo.getPrimaryUserId()); assertEquals(PublicKeyAlgorithm.EDDSA_LEGACY.getAlgorithmId(), @@ -91,11 +91,10 @@ public class GenerateKeys { // Set a password to protect the secret key String password = "b1angl3s"; // Generate the OpenPGP key - PGPSecretKeyRing secretKey = PGPainless.generateKeyRing() - .simpleRsaKeyRing(userId, RsaLength._4096, password) - .getPGPSecretKeyRing(); + OpenPGPKey secretKey = PGPainless.generateKeyRing() + .simpleRsaKeyRing(userId, RsaLength._4096, password); - KeyRingInfo keyInfo = new KeyRingInfo(secretKey); + KeyRingInfo keyInfo = PGPainless.inspectKeyRing(secretKey); assertEquals(1, keyInfo.getSecretKeys().size()); assertEquals(userId, keyInfo.getPrimaryUserId()); assertEquals(PublicKeyAlgorithm.RSA_GENERAL.getAlgorithmId(), keyInfo.getAlgorithm().getAlgorithmId()); @@ -115,12 +114,11 @@ public class GenerateKeys { // Set a password to protect the secret key String password = "tr4ns"; // Generate the OpenPGP key - PGPSecretKeyRing secretKey = PGPainless.generateKeyRing() - .simpleEcKeyRing(userId, password) - .getPGPSecretKeyRing(); + OpenPGPKey secretKey = PGPainless.generateKeyRing() + .simpleEcKeyRing(userId, password); - KeyRingInfo keyInfo = new KeyRingInfo(secretKey); + KeyRingInfo keyInfo = PGPainless.inspectKeyRing(secretKey); assertEquals(2, keyInfo.getSecretKeys().size()); assertEquals(userId, keyInfo.getPrimaryUserId()); } @@ -174,7 +172,7 @@ public class GenerateKeys { // It is recommended to use the Passphrase class, as it can be used to safely invalidate passwords from memory Passphrase passphrase = Passphrase.fromPassword("1nters3x"); - PGPSecretKeyRing secretKey = PGPainless.buildKeyRing() + OpenPGPKey secretKey = PGPainless.buildKeyRing() .setPrimaryKey(KeySpec.getBuilder(KeyType.EDDSA_LEGACY(EdDSALegacyCurve._Ed25519), // The primary key MUST carry the CERTIFY_OTHER flag, but CAN carry additional flags KeyFlag.CERTIFY_OTHER)) @@ -204,11 +202,10 @@ public class GenerateKeys { .addUserId(additionalUserId) // Set passphrase. Alternatively use .withoutPassphrase() to leave key unprotected. .setPassphrase(passphrase) - .build() - .getPGPSecretKeyRing(); + .build(); - KeyRingInfo keyInfo = new KeyRingInfo(secretKey); + KeyRingInfo keyInfo = PGPainless.inspectKeyRing(secretKey); assertEquals(3, keyInfo.getSecretKeys().size()); assertEquals("Morgan Carpenter (Pride!) ", keyInfo.getPrimaryUserId()); assertTrue(keyInfo.isUserIdValid(additionalUserId));