Simplify the active_certification utility function.

- `active_certification` takes a fingerprint, and looks up the
    corresponding certificate in the certificate store.  But, all
    callers already have the certificate.  Avoid a lookup by taking a
    reference to the certificate.
This commit is contained in:
Neal H. Walfield 2024-10-09 15:03:45 +02:00
parent d8f8d167a6
commit 0e8ae33b22
No known key found for this signature in database
GPG Key ID: 6863C9AD5B4D22D3
3 changed files with 21 additions and 29 deletions

View File

@ -3,7 +3,7 @@ use std::time::SystemTime;
use sequoia_openpgp as openpgp;
use openpgp::cert::prelude::*;
use openpgp::{Cert, Fingerprint, KeyID, Result};
use openpgp::{Cert, KeyID, Result};
use openpgp::packet::prelude::*;
use openpgp::parse::stream::*;
use openpgp::policy::HashAlgoSecurity;
@ -75,33 +75,15 @@ pub fn dispatch(sq: Sq, command: SqCommand) -> Result<()>
/// Returns the active certification, if any, for the specified bindings.
///
/// The certificate is looked up in the certificate store.
///
/// Note: if `n` User IDs are provided, then the returned vector has
/// `n` elements.
fn active_certification(sq: &Sq,
cert: &Fingerprint, userids: Vec<UserID>,
issuer: &Key<openpgp::packet::key::PublicParts,
openpgp::packet::key::UnspecifiedRole>)
fn active_certification(
sq: &Sq,
cert: &Cert, userids: Vec<UserID>,
issuer: &Key<openpgp::packet::key::PublicParts,
openpgp::packet::key::UnspecifiedRole>)
-> Vec<(UserID, Option<Signature>)>
{
// Look up the cert and find the certifications for the specified
// User ID, if any.
let lc = sq.cert_store_or_else()
.and_then(|cert_store| cert_store.lookup_by_cert_fpr(cert));
let lc = match lc {
Ok(lc) => lc,
Err(_) => {
return userids.into_iter().map(|userid| (userid, None)).collect();
}
};
let cert = match lc.to_cert() {
Ok(cert) => cert,
Err(_) => {
return userids.into_iter().map(|userid| (userid, None)).collect();
}
};
let issuer_kh = issuer.key_handle();
userids.into_iter().map(|userid| {

View File

@ -240,7 +240,7 @@ fn certify(sq: &Sq,
}
let certifications = active_certification(
sq, &cert.fingerprint(),
sq, cert,
userids.iter().cloned().collect(),
signer.public())
.into_iter()

View File

@ -485,7 +485,7 @@ pub fn add(sq: Sq, c: link::AddCommand)
.context("Looking up local trust root")?;
let certifications = active_certification(
&sq, &vc.fingerprint(), userids,
&sq, &cert, userids,
signer.public())
.into_iter()
.map(|(userid, active_certification)| {
@ -650,7 +650,7 @@ pub fn retract(sq: Sq, c: link::RetractCommand)
.context("Looking up local trust root")?;
let certifications = active_certification(
&sq, &cert.fingerprint(), userids, signer.public())
&sq, &cert, userids, signer.public())
.into_iter()
.map(|(userid, active_certification)| {
let userid_str = || String::from_utf8_lossy(userid.value());
@ -770,9 +770,19 @@ pub fn list(sq: Sq, c: link::ListCommand)
let cert_store = sq.cert_store_or_else()?;
for cert in cert_store.certs() {
let cert = if let Ok(cert) = cert.to_cert() {
cert
} else {
// Invalid cert. Skip it.
continue;
};
let userids = cert.userids()
.map(|ua| ua.userid().clone())
.collect::<Vec<_>>();
for (userid, certification) in active_certification(
&sq, &cert.fingerprint(), cert.userids().collect(),
trust_root_key)
&sq, &cert, userids, trust_root_key)
.into_iter()
.filter_map(|(user, certification)| {
if let Some(certification) = certification {