From d52968514ca8eb7710db0fbe55b3f472b8abcac6 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Sat, 24 Jun 2023 10:13:00 +0200 Subject: [PATCH] Add methods to access delegations and 3rd-party certifications from keys --- .../pgpainless/signature/SignatureUtils.java | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/pgpainless-core/src/main/java/org/pgpainless/signature/SignatureUtils.java b/pgpainless-core/src/main/java/org/pgpainless/signature/SignatureUtils.java index 4aac89df..5f713238 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/signature/SignatureUtils.java +++ b/pgpainless-core/src/main/java/org/pgpainless/signature/SignatureUtils.java @@ -23,6 +23,7 @@ import org.bouncycastle.openpgp.PGPCompressedData; import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPObjectFactory; import org.bouncycastle.openpgp.PGPPublicKey; +import org.bouncycastle.openpgp.PGPPublicKeyRing; import org.bouncycastle.openpgp.PGPSignature; import org.bouncycastle.openpgp.PGPSignatureList; import org.bouncycastle.util.encoders.Hex; @@ -307,4 +308,53 @@ public final class SignatureUtils { return Collections.unmodifiableList(signaturesByKeyId); } + + public static @Nonnull List getDelegations(PGPPublicKeyRing key) { + List delegations = new ArrayList<>(); + PGPPublicKey primaryKey = key.getPublicKey(); + Iterator signatures = primaryKey.getKeySignatures(); + outerloop: while (signatures.hasNext()) { + PGPSignature signature = signatures.next(); + Iterator subkeys = key.getPublicKeys(); + while (subkeys.hasNext()) { + if (signature.getKeyID() == subkeys.next().getKeyID()) { + continue outerloop; + } + } + delegations.add(signature); + } + + return delegations; + } + + public static @Nonnull List get3rdPartyCertificationsFor(String userId, PGPPublicKeyRing key) { + PGPPublicKey primaryKey = key.getPublicKey(); + List certifications = new ArrayList<>(); + Iterator it = primaryKey.getSignaturesForID(userId); + while (it.hasNext()) { + PGPSignature sig = it.next(); + if (sig.getKeyID() != primaryKey.getKeyID()) { + certifications.add(sig); + } + } + return certifications; + } + + public static class Certification { + private final String userId; + private final PGPSignature signature; + + public Certification(String userId, PGPSignature signature) { + this.userId = userId; + this.signature = signature; + } + + public String getUserId() { + return userId; + } + + public PGPSignature getSignature() { + return signature; + } + } }