1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-12-23 11:27:57 +01:00

Improve SignatureUtils.wasIssuedBy() by adding support for v5 fingerprints

This commit is contained in:
Paul Schaub 2022-05-18 14:21:22 +02:00
parent 9921fc6ff6
commit 70a861611c
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311

View file

@ -14,6 +14,7 @@ import java.util.Date;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import javax.annotation.Nonnull;
import org.bouncycastle.bcpg.sig.IssuerKeyID; import org.bouncycastle.bcpg.sig.IssuerKeyID;
import org.bouncycastle.bcpg.sig.KeyExpirationTime; import org.bouncycastle.bcpg.sig.KeyExpirationTime;
@ -36,14 +37,11 @@ import org.pgpainless.algorithm.SignatureType;
import org.pgpainless.algorithm.negotiation.HashAlgorithmNegotiator; import org.pgpainless.algorithm.negotiation.HashAlgorithmNegotiator;
import org.pgpainless.implementation.ImplementationFactory; import org.pgpainless.implementation.ImplementationFactory;
import org.pgpainless.key.OpenPgpFingerprint; import org.pgpainless.key.OpenPgpFingerprint;
import org.pgpainless.key.OpenPgpV4Fingerprint;
import org.pgpainless.key.util.OpenPgpKeyAttributeUtil; import org.pgpainless.key.util.OpenPgpKeyAttributeUtil;
import org.pgpainless.key.util.RevocationAttributes; import org.pgpainless.key.util.RevocationAttributes;
import org.pgpainless.signature.subpackets.SignatureSubpacketsUtil; import org.pgpainless.signature.subpackets.SignatureSubpacketsUtil;
import org.pgpainless.util.ArmorUtils; import org.pgpainless.util.ArmorUtils;
import javax.annotation.Nonnull;
/** /**
* Utility methods related to signatures. * Utility methods related to signatures.
*/ */
@ -325,17 +323,17 @@ public final class SignatureUtils {
} }
public static boolean wasIssuedBy(byte[] fingerprint, PGPSignature signature) { public static boolean wasIssuedBy(byte[] fingerprint, PGPSignature signature) {
if (fingerprint.length != 20) { try {
OpenPgpFingerprint fp = OpenPgpFingerprint.parseFromBinary(fingerprint);
OpenPgpFingerprint issuerFp = SignatureSubpacketsUtil.getIssuerFingerprintAsOpenPgpFingerprint(signature);
if (issuerFp == null) {
return fp.getKeyId() == signature.getKeyID();
}
return fp.equals(issuerFp);
} catch (IllegalArgumentException e) {
// Unknown fingerprint length // Unknown fingerprint length
return false; return false;
} }
OpenPgpV4Fingerprint fp = new OpenPgpV4Fingerprint(fingerprint);
OpenPgpFingerprint issuerFp = SignatureSubpacketsUtil.getIssuerFingerprintAsOpenPgpFingerprint(signature);
if (issuerFp == null) {
return fp.getKeyId() == signature.getKeyID();
}
return fp.equals(issuerFp);
} }
/** /**
@ -354,7 +352,7 @@ public final class SignatureUtils {
List<PGPSignature> signaturesByKeyId = new ArrayList<>(); List<PGPSignature> signaturesByKeyId = new ArrayList<>();
Iterator<PGPSignature> userIdSignatures = key.getSignaturesForID(userId); Iterator<PGPSignature> userIdSignatures = key.getSignaturesForID(userId);
// getSignaturesForID() is nullable -.- // getSignaturesForID() is nullable for some reason -.-
if (userIdSignatures == null) { if (userIdSignatures == null) {
return signaturesByKeyId; return signaturesByKeyId;
} }