1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-26 22:32:07 +01:00

Add support for deleting user-ids (untested)

This commit is contained in:
Paul Schaub 2020-11-03 19:56:35 +01:00
parent 92e2828885
commit 6159428c9a
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 42 additions and 1 deletions

View file

@ -147,7 +147,42 @@ public class KeyRingEditor implements KeyRingEditorInterface {
@Override @Override
public KeyRingEditorInterface deleteUserId(String userId, SecretKeyRingProtector protector) { 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<PGPPublicKey> publicKeys = new ArrayList<>();
Iterator<PGPPublicKey> 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<String> userIdIterator = publicKey.getUserIDs();
while (userIdIterator.hasNext()) {
hasUserId = userId.equals(userIdIterator.next());
if (hasUserId) break;
}
return hasUserId;
} }
@Override @Override

View file

@ -50,6 +50,12 @@ public interface KeyRingEditorInterface {
*/ */
KeyRingEditorInterface deleteUserId(String userId, SecretKeyRingProtector secretKeyRingProtector); 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. * Add a subkey to the key ring.
* The subkey will be generated from the provided {@link KeySpec}. * The subkey will be generated from the provided {@link KeySpec}.