From 4dd2b2f71a5a8c103ff26ddf34bc646f83648c2c Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Fri, 13 Nov 2020 16:59:55 +0100 Subject: [PATCH] Implement revoking subkeys for key-ids as well --- .../key/modification/KeyRingEditor.java | 27 ++++++++++--------- .../modification/KeyRingEditorInterface.java | 2 +- 2 files changed, 15 insertions(+), 14 deletions(-) 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 f1299b7e..116f0d5d 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 @@ -59,7 +59,6 @@ import org.pgpainless.key.protection.UnprotectedKeysProtector; import org.pgpainless.key.protection.passphrase_provider.SolitaryPassphraseProvider; import org.pgpainless.key.util.KeyRingUtils; import org.pgpainless.key.util.SignatureUtils; -import org.pgpainless.util.NotYetImplementedException; import org.pgpainless.util.Passphrase; public class KeyRingEditor implements KeyRingEditorInterface { @@ -258,15 +257,24 @@ public class KeyRingEditor implements KeyRingEditorInterface { @Override public KeyRingEditorInterface revokeSubKey(OpenPgpV4Fingerprint fingerprint, SecretKeyRingProtector protector) throws PGPException { - PGPSecretKey primaryKey = secretKeyRing.getSecretKey(); - PGPPrivateKey privateKey = primaryKey.extractPrivateKey(protector.getDecryptor(primaryKey.getKeyID())); + return revokeSubKey(fingerprint.getKeyId(), protector); + } - PGPPublicKey revokeeSubKey = secretKeyRing.getPublicKey(fingerprint.getKeyId()); + @Override + public KeyRingEditorInterface revokeSubKey(long subKeyId, SecretKeyRingProtector protector) throws PGPException { + PGPPublicKey revokeeSubKey = secretKeyRing.getPublicKey(subKeyId); if (revokeeSubKey == null) { - throw new NoSuchElementException("No subkey with fingerprint " + fingerprint + " found."); + throw new NoSuchElementException("No subkey with id " + Long.toHexString(subKeyId) + " found."); } + secretKeyRing = revokeSubKey(protector, revokeeSubKey); + return this; + } + + private PGPSecretKeyRing revokeSubKey(SecretKeyRingProtector protector, PGPPublicKey revokeeSubKey) throws PGPException { + PGPSecretKey primaryKey = secretKeyRing.getSecretKey(); PGPSignatureGenerator signatureGenerator = SignatureUtils.getSignatureGeneratorFor(primaryKey); + PGPPrivateKey privateKey = primaryKey.extractPrivateKey(protector.getDecryptor(primaryKey.getKeyID())); signatureGenerator.init(SignatureType.SUBKEY_REVOCATION.getCode(), privateKey); // Generate revocation @@ -276,14 +284,7 @@ public class KeyRingEditor implements KeyRingEditorInterface { // Inject revoked public key into key ring PGPPublicKeyRing publicKeyRing = KeyRingUtils.publicKeyRingFrom(secretKeyRing); publicKeyRing = PGPPublicKeyRing.insertPublicKey(publicKeyRing, revokeeSubKey); - secretKeyRing = PGPSecretKeyRing.replacePublicKeys(secretKeyRing, publicKeyRing); - - return this; - } - - @Override - public KeyRingEditorInterface revokeSubKey(long subKeyId, SecretKeyRingProtector protector) { - throw new NotYetImplementedException(); + return PGPSecretKeyRing.replacePublicKeys(secretKeyRing, publicKeyRing); } @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 a6290ded..c228a830 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 @@ -125,7 +125,7 @@ public interface KeyRingEditorInterface { * @param subKeyId id of the subkey * @return the builder */ - KeyRingEditorInterface revokeSubKey(long subKeyId, SecretKeyRingProtector secretKeyRingProtector); + KeyRingEditorInterface revokeSubKey(long subKeyId, SecretKeyRingProtector secretKeyRingProtector) throws PGPException; /** * Change the passphrase of the whole key ring.