From c9982ccfe642bcf48d777b00872082863438eb46 Mon Sep 17 00:00:00 2001 From: Ivan Pizhenko Date: Wed, 3 Feb 2021 23:09:02 +0200 Subject: [PATCH 1/3] Additional user id manipulation test --- .../key/modification/AddUserIdTest.java | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/pgpainless-core/src/test/java/org/pgpainless/key/modification/AddUserIdTest.java b/pgpainless-core/src/test/java/org/pgpainless/key/modification/AddUserIdTest.java index 886d4086..3ba9a268 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/key/modification/AddUserIdTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/key/modification/AddUserIdTest.java @@ -19,14 +19,18 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; +import java.io.ByteArrayInputStream; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.security.InvalidAlgorithmParameterException; import java.security.NoSuchAlgorithmException; import java.util.Iterator; import java.util.NoSuchElementException; +import org.bouncycastle.bcpg.ArmoredInputStream; import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPSecretKeyRing; +import org.bouncycastle.openpgp.jcajce.JcaPGPSecretKeyRingCollection; import org.junit.jupiter.api.Test; import org.pgpainless.PGPainless; import org.pgpainless.key.TestKeys; @@ -64,6 +68,7 @@ public class AddUserIdTest { assertFalse(userIds.hasNext()); } + @Test public void addUserId_NoSuchElementExceptionForMissingKey() throws IOException, PGPException { PGPSecretKeyRing secretKeys = TestKeys.getCryptieSecretKeyRing(); @@ -84,4 +89,53 @@ public class AddUserIdTest { assertThrows(NoSuchElementException.class, () -> PGPainless.modifyKeyRing(secretKeys) .deleteUserId(0L, TestKeys.CRYPTIE_UID, new UnprotectedKeysProtector())); } + + @Test + public void deleteExistingAndAddNewUserIdToExistingKeyRing() + throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException, IOException { + final String ARMORED_PRIVATE_KEY = + "-----BEGIN PGP PRIVATE KEY BLOCK-----\r\n\r\n" + + "xVgEX6UIExYJKwYBBAHaRw8BAQdAMfHf64wPQ2LC9In5AKYU/KT1qWvI7e7a\r\n" + + "Xr+LWeQGUKIAAQCcB3zZlHfepQT26LIwbTDn4lvQ9LuD1fk2hK6i9FXFxxO7\r\n" + + "zRI8dXNlckBleGFtcGxlLmNvbT7CjwQQFgoAIAUCX6UIEwYLCQcIAwIEFQgK\r\n" + + "AgQWAgEAAhkBAhsDAh4BACEJEEoCtcZ3snFuFiEENY1GQZqrKQqgUAXASgK1\r\n" + + "xneycW6P6AEA5iXFK+fWpj0vn3xpKEuFRqvytPKFzhwd4wEvL+IGSPEBALE/\r\n" + + "pZdMzsDoKPENiLFpboDVNVJScwFXIleKmtNaRycFx10EX6UIExIKKwYBBAGX\r\n" + + "VQEFAQEHQBDdeawWVNqYkP8c/ihLEUlVpn8cQw7rmRc/sIhdAXhfAwEIBwAA\r\n" + + "/0Jy7IelcHDjxE3OzagEzSxNrCVw8uPHNRl8s6iP+CQYEfHCeAQYFggACQUC\r\n" + + "X6UIEwIbDAAhCRBKArXGd7JxbhYhBDWNRkGaqykKoFAFwEoCtcZ3snFuWp8B\r\n" + + "AIzRBYJSfZzlvlyyPhrbXJoYSICGNy/5x7noXjp/ByeOAQDnTbQi4XwXJrU4\r\n" + + "A8Nl9eyz16ZWUzEPwfWgahIG1eQDDA==\r\n" + + "=bk4o\r\n" + + "-----END PGP PRIVATE KEY BLOCK-----\r\n"; + + PGPSecretKeyRing secretKeys = readArmoredPrivateKey(ARMORED_PRIVATE_KEY.getBytes(StandardCharsets.UTF_8)); + + Iterator userIds = secretKeys.getSecretKey().getPublicKey().getUserIDs(); + assertEquals("", userIds.next()); + assertFalse(userIds.hasNext()); + + SecretKeyRingProtector protector = new UnprotectedKeysProtector(); + secretKeys = PGPainless.modifyKeyRing(secretKeys) + .deleteUserId("", protector) + .addUserId("cheshirecat@wonderland.lit", protector) + .done(); + + userIds = secretKeys.getSecretKey().getPublicKey().getUserIDs(); + assertEquals("cheshirecat@wonderland.lit", userIds.next()); + assertFalse(userIds.hasNext()); + } + + private static PGPSecretKeyRing readArmoredPrivateKey(byte[] data) throws IOException, PGPException { + try (ByteArrayInputStream byteIn = new ByteArrayInputStream(data)) { + try (ArmoredInputStream armoredIn = new ArmoredInputStream(byteIn)) { + JcaPGPSecretKeyRingCollection keyRingCollection = new JcaPGPSecretKeyRingCollection(armoredIn); + Iterator it = keyRingCollection.getKeyRings(); + if (it.hasNext()) { + return it.next(); + } + } + } + return null; + } } From a807ddbb4e5fe2201d2d65deb6b430cdad102b29 Mon Sep 17 00:00:00 2001 From: Ivan Pizhenko Date: Thu, 4 Feb 2021 09:45:08 +0200 Subject: [PATCH 2/3] use PGPainless.readKeyRing().secretKeyRing() --- .../key/modification/AddUserIdTest.java | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/pgpainless-core/src/test/java/org/pgpainless/key/modification/AddUserIdTest.java b/pgpainless-core/src/test/java/org/pgpainless/key/modification/AddUserIdTest.java index 3ba9a268..78de7974 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/key/modification/AddUserIdTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/key/modification/AddUserIdTest.java @@ -91,8 +91,7 @@ public class AddUserIdTest { } @Test - public void deleteExistingAndAddNewUserIdToExistingKeyRing() - throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException, IOException { + public void deleteExistingAndAddNewUserIdToExistingKeyRing() throws PGPException, IOException { final String ARMORED_PRIVATE_KEY = "-----BEGIN PGP PRIVATE KEY BLOCK-----\r\n\r\n" + "xVgEX6UIExYJKwYBBAHaRw8BAQdAMfHf64wPQ2LC9In5AKYU/KT1qWvI7e7a\r\n" + @@ -109,8 +108,7 @@ public class AddUserIdTest { "=bk4o\r\n" + "-----END PGP PRIVATE KEY BLOCK-----\r\n"; - PGPSecretKeyRing secretKeys = readArmoredPrivateKey(ARMORED_PRIVATE_KEY.getBytes(StandardCharsets.UTF_8)); - + PGPSecretKeyRing secretKeys = PGPainless.readKeyRing().secretKeyRing(ARMORED_PRIVATE_KEY); Iterator userIds = secretKeys.getSecretKey().getPublicKey().getUserIDs(); assertEquals("", userIds.next()); assertFalse(userIds.hasNext()); @@ -125,17 +123,4 @@ public class AddUserIdTest { assertEquals("cheshirecat@wonderland.lit", userIds.next()); assertFalse(userIds.hasNext()); } - - private static PGPSecretKeyRing readArmoredPrivateKey(byte[] data) throws IOException, PGPException { - try (ByteArrayInputStream byteIn = new ByteArrayInputStream(data)) { - try (ArmoredInputStream armoredIn = new ArmoredInputStream(byteIn)) { - JcaPGPSecretKeyRingCollection keyRingCollection = new JcaPGPSecretKeyRingCollection(armoredIn); - Iterator it = keyRingCollection.getKeyRings(); - if (it.hasNext()) { - return it.next(); - } - } - } - return null; - } } From 72e0dea20ff74b042a34345df0d58903a5f83b86 Mon Sep 17 00:00:00 2001 From: Ivan Pizhenko Date: Thu, 4 Feb 2021 09:56:08 +0200 Subject: [PATCH 3/3] remove unused imports --- .../java/org/pgpainless/key/modification/AddUserIdTest.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pgpainless-core/src/test/java/org/pgpainless/key/modification/AddUserIdTest.java b/pgpainless-core/src/test/java/org/pgpainless/key/modification/AddUserIdTest.java index 78de7974..0594754a 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/key/modification/AddUserIdTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/key/modification/AddUserIdTest.java @@ -19,18 +19,14 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; -import java.io.ByteArrayInputStream; import java.io.IOException; -import java.nio.charset.StandardCharsets; import java.security.InvalidAlgorithmParameterException; import java.security.NoSuchAlgorithmException; import java.util.Iterator; import java.util.NoSuchElementException; -import org.bouncycastle.bcpg.ArmoredInputStream; import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPSecretKeyRing; -import org.bouncycastle.openpgp.jcajce.JcaPGPSecretKeyRingCollection; import org.junit.jupiter.api.Test; import org.pgpainless.PGPainless; import org.pgpainless.key.TestKeys;