From c35154813a7ee0580cf0442a80a7a28ca46af24f Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Thu, 21 Jan 2021 14:35:33 +0100 Subject: [PATCH] More code cleanup and tests --- .../main/java/org/pgpainless/util/BCUtil.java | 20 -------------- .../pgpainless/key/info/KeyRingInfoTest.java | 26 +++++++++++++++++++ .../java/org/pgpainless/util/BCUtilTest.java | 12 +++++++++ 3 files changed, 38 insertions(+), 20 deletions(-) diff --git a/pgpainless-core/src/main/java/org/pgpainless/util/BCUtil.java b/pgpainless-core/src/main/java/org/pgpainless/util/BCUtil.java index 21020dc4..6ffe5b41 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/util/BCUtil.java +++ b/pgpainless-core/src/main/java/org/pgpainless/util/BCUtil.java @@ -38,10 +38,8 @@ import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; import org.bouncycastle.openpgp.PGPSignature; import org.bouncycastle.openpgp.PGPSignatureSubpacketVector; import org.bouncycastle.openpgp.PGPUtil; -import org.bouncycastle.openpgp.operator.KeyFingerPrintCalculator; import org.bouncycastle.util.io.Streams; import org.pgpainless.algorithm.KeyFlag; -import org.pgpainless.implementation.ImplementationFactory; import org.pgpainless.key.selection.key.PublicKeySelectionStrategy; import org.pgpainless.key.selection.key.impl.NoRevocation; import org.pgpainless.key.selection.key.impl.SignedByMasterKey; @@ -185,24 +183,6 @@ public class BCUtil { return cleaned; } - /** - * Return the {@link PGPPublicKey} which is the master key of the key ring. - * - * @param ring key ring - * @return master key - */ - public static PGPPublicKey getMasterKeyFrom(@Nonnull PGPPublicKeyRing ring) { - Iterator it = ring.getPublicKeys(); - while (it.hasNext()) { - PGPPublicKey k = it.next(); - if (k.isMasterKey()) { - // There can only be one master key, so we can immediately return - return k; - } - } - return null; - } - public static PGPPublicKey getMasterKeyFrom(@Nonnull PGPKeyRing ring) { Iterator it = ring.getPublicKeys(); while (it.hasNext()) { diff --git a/pgpainless-core/src/test/java/org/pgpainless/key/info/KeyRingInfoTest.java b/pgpainless-core/src/test/java/org/pgpainless/key/info/KeyRingInfoTest.java index 863f4ccd..2947d1b2 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/key/info/KeyRingInfoTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/key/info/KeyRingInfoTest.java @@ -33,6 +33,8 @@ import org.pgpainless.PGPainless; import org.pgpainless.algorithm.PublicKeyAlgorithm; import org.pgpainless.key.TestKeys; import org.pgpainless.key.protection.UnprotectedKeysProtector; +import org.pgpainless.key.util.KeyRingUtils; +import org.pgpainless.util.Passphrase; public class KeyRingInfoTest { @@ -79,4 +81,28 @@ public class KeyRingInfoTest { assertEquals(revocationDate.getTime(), rInfo.getRevocationDate().getTime(), 1000); assertEquals(revocationDate.getTime(), rInfo.getLastModified().getTime(), 1000); } + + @Test + public void testIsFullyDecrypted() throws IOException, PGPException { + PGPSecretKeyRing secretKeys = TestKeys.getEmilSecretKeyRing(); + KeyRingInfo info = PGPainless.inspectKeyRing(secretKeys); + + assertTrue(info.isFullyDecrypted()); + + secretKeys = PGPainless.modifyKeyRing(secretKeys) + .changePassphraseFromOldPassphrase(null) + .withSecureDefaultSettings() + .toNewPassphrase(Passphrase.fromPassword("sw0rdf1sh")) + .done(); + info = PGPainless.inspectKeyRing(secretKeys); + + assertFalse(info.isFullyDecrypted()); + } + + @Test + public void testGetSecretKey() throws IOException, PGPException { + PGPSecretKeyRing secretKeys = TestKeys.getCryptieSecretKeyRing(); + PGPPublicKeyRing publicKeys = KeyRingUtils.publicKeyRingFrom(secretKeys); + + } } diff --git a/pgpainless-core/src/test/java/org/pgpainless/util/BCUtilTest.java b/pgpainless-core/src/test/java/org/pgpainless/util/BCUtilTest.java index 3db27b64..453e7e56 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/util/BCUtilTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/util/BCUtilTest.java @@ -36,6 +36,8 @@ import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; import org.junit.jupiter.api.Test; import org.pgpainless.PGPainless; import org.pgpainless.algorithm.KeyFlag; +import org.pgpainless.key.OpenPgpV4Fingerprint; +import org.pgpainless.key.TestKeys; import org.pgpainless.key.generation.KeySpec; import org.pgpainless.key.generation.type.KeyType; import org.pgpainless.key.generation.type.rsa.RsaLength; @@ -133,4 +135,14 @@ public class BCUtilTest { PGPSecretKeyRing cleaned = BCUtil.removeUnassociatedKeysFromKeyRing(alice_mallory, alice.getPublicKey()); assertNull(cleaned.getSecretKey(subKey.getKeyID())); } + + @Test + public void getMasterKeyFromRingTest() throws IOException, PGPException { + PGPSecretKeyRing secretKeys = TestKeys.getCryptieSecretKeyRing(); + + PGPPublicKey primaryKey = BCUtil.getMasterKeyFrom(secretKeys); + + assertNotNull(primaryKey); + assertEquals(TestKeys.CRYPTIE_FINGERPRINT, new OpenPgpV4Fingerprint(primaryKey)); + } }