From 6159428c9af92567e4603cf9fcdb190a7bfe9cd3 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Tue, 3 Nov 2020 19:56:35 +0100 Subject: [PATCH] Add support for deleting user-ids (untested) --- .../key/modification/KeyRingEditor.java | 37 ++++++++++++++++++- .../modification/KeyRingEditorInterface.java | 6 +++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/modification/KeyRingEditor.java b/pgpainless-core/src/main/java/org/pgpainless/key/modification/KeyRingEditor.java index f594b114..0756fe52 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/modification/KeyRingEditor.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/modification/KeyRingEditor.java @@ -147,7 +147,42 @@ public class KeyRingEditor implements KeyRingEditorInterface { @Override public KeyRingEditorInterface deleteUserId(String userId, SecretKeyRingProtector protector) { - throw new NotYetImplementedException(); + PGPPublicKey publicKey = secretKeyRing.getPublicKey(); + return deleteUserId(publicKey.getKeyID(), userId, protector); + } + + @Override + public KeyRingEditorInterface deleteUserId(long keyId, String userId, SecretKeyRingProtector secretKeyRingProtector) { + List publicKeys = new ArrayList<>(); + Iterator publicKeyIterator = secretKeyRing.getPublicKeys(); + boolean foundKey = false; + while (publicKeyIterator.hasNext()) { + PGPPublicKey publicKey = publicKeyIterator.next(); + if (publicKey.getKeyID() == keyId) { + foundKey = true; + if (!hasUserId(userId, publicKey)) { + throw new NoSuchElementException("Key " + Long.toHexString(keyId) + " does not have a user-id attribute of value '" + userId + "'"); + } + publicKey = PGPPublicKey.removeCertification(publicKey, userId); + } + publicKeys.add(publicKey); + } + if (!foundKey) { + throw new NoSuchElementException("Cannot find public key with id " + Long.toHexString(keyId)); + } + PGPPublicKeyRing publicKeyRing = new PGPPublicKeyRing(publicKeys); + secretKeyRing = PGPSecretKeyRing.replacePublicKeys(secretKeyRing, publicKeyRing); + return this; + } + + private static boolean hasUserId(String userId, PGPPublicKey publicKey) { + boolean hasUserId = false; + Iterator userIdIterator = publicKey.getUserIDs(); + while (userIdIterator.hasNext()) { + hasUserId = userId.equals(userIdIterator.next()); + if (hasUserId) break; + } + return hasUserId; } @Override diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/modification/KeyRingEditorInterface.java b/pgpainless-core/src/main/java/org/pgpainless/key/modification/KeyRingEditorInterface.java index f3ca037b..717476dd 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/modification/KeyRingEditorInterface.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/modification/KeyRingEditorInterface.java @@ -50,6 +50,12 @@ public interface KeyRingEditorInterface { */ KeyRingEditorInterface deleteUserId(String userId, SecretKeyRingProtector secretKeyRingProtector); + default KeyRingEditorInterface deleteUserId(OpenPgpV4Fingerprint fingerprint, String userId, SecretKeyRingProtector secretKeyRingProtector) { + return deleteUserId(fingerprint.getKeyId(), userId, secretKeyRingProtector); + } + + KeyRingEditorInterface deleteUserId(long keyId, String userId, SecretKeyRingProtector secretKeyRingProtector); + /** * Add a subkey to the key ring. * The subkey will be generated from the provided {@link KeySpec}.