Skip to content

Commit

Permalink
Add deprecated utility methods for deleting user-ids from keys/certif…
Browse files Browse the repository at this point in the history
…icates
  • Loading branch information
vanitasvitae committed Nov 12, 2021
1 parent 1b12cc1 commit 0383a44
Showing 1 changed file with 46 additions and 0 deletions.
Expand Up @@ -154,4 +154,50 @@ public static boolean keyRingContainsKeyWithId(@Nonnull PGPPublicKeyRing ring,
long keyId) {
return ring.getPublicKey(keyId) != null;
}

/**
* Delete the given user-id and its certification signatures from the given key.
*
* @deprecated Deleting user-ids is highly discouraged, since it might lead to all sorts of problems
* (e.g. lost key properties).
* Instead, user-ids should only be revoked.
*
* @param secretKeys secret keys
* @param userId user-id
* @return modified secret keys
*/
@Deprecated
public PGPSecretKeyRing deleteUserIdFromSecretKeyRing(PGPSecretKeyRing secretKeys, String userId) {
PGPSecretKey secretKey = secretKeys.getSecretKey(); // user-ids are located on primary key only
PGPPublicKey publicKey = secretKey.getPublicKey(); // user-ids are placed on the public key part
publicKey = PGPPublicKey.removeCertification(publicKey, userId);
if (publicKey == null) {
throw new NoSuchElementException("User-ID " + userId + " not found on the key.");
}
secretKey = PGPSecretKey.replacePublicKey(secretKey, publicKey);
secretKeys = PGPSecretKeyRing.insertSecretKey(secretKeys, secretKey);
return secretKeys;
}

/**
* Delete the given user-id and its certification signatures from the given certificate.
*
* @deprecated Deleting user-ids is highly discouraged, since it might lead to all sorts of problems
* (e.g. lost key properties).
* Instead, user-ids should only be revoked.
*
* @param publicKeys certificate
* @param userId user-id
* @return modified secret keys
*/
@Deprecated
public PGPPublicKeyRing deleteUserIdFromPublicKeyRing(PGPPublicKeyRing publicKeys, String userId) {
PGPPublicKey publicKey = publicKeys.getPublicKey(); // user-ids are located on primary key only
publicKey = PGPPublicKey.removeCertification(publicKey, userId);
if (publicKey == null) {
throw new NoSuchElementException("User-ID " + userId + " not found on the key.");
}
publicKeys = PGPPublicKeyRing.insertPublicKey(publicKeys, publicKey);
return publicKeys;
}
}

0 comments on commit 0383a44

Please sign in to comment.