🏡 index : quine-global/quorum.git

//! Feeds: where a project's rebuilders publish their attestations, and how.
//!
//! guix-sigs (Bitcoin Core's guix.sigs):
//!   <release>/<rebuilder>/all.SHA256SUMS{,.asc}           (else noncodesigned.*)
//!   builder-keys/<rebuilder>.gpg
//!
//! in-toto (quorum's own layout, the same shape):
//!   <release>/<rebuilder>/attestation.sigstore.json       a Sigstore bundle (cosign attest-blob --bundle)
//!   <release>/<rebuilder>/attestation.dsse.json           or a bare DSSE envelope; either way an in-toto Statement
//!   builder-keys/<rebuilder>.pem                          SPKI PEM public key(s)

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};

use crate::source::Source;
use crate::sums;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, clap::ValueEnum)]
#[serde(rename_all = "kebab-case")]
pub enum FeedKind {
    GuixSigs,
    InToto,
}

pub enum Attestation {
    /// A SHA256SUMS file and its detached OpenPGP signature(s).
    PgpSums { sums: Vec<u8>, sig: Vec<u8> },
    /// A DSSE envelope.
    Dsse(Vec<u8>),
}

impl FeedKind {
    pub fn key_path(self, rebuilder: &str) -> String {
        match self {
            FeedKind::GuixSigs => format!("builder-keys/{rebuilder}.gpg"),
            FeedKind::InToto => format!("builder-keys/{rebuilder}.pem"),
        }
    }

    /// The rebuilder's attestation for `release`, if they published one.
    /// `name` picks between guix.sigs' two sums files.
    pub fn fetch(
        self,
        src: &Source,
        release: &str,
        rebuilder: &str,
        name: &str,
    ) -> Result<Option<Attestation>> {
        match self {
            FeedKind::GuixSigs => {
                // all.SHA256SUMS covers the codesigned files too (the -setup.exe
                // people download); some releases only have noncodesigned.
                let mut found = None;
                for kind in ["all", "noncodesigned"] {
                    let path = format!("{release}/{rebuilder}/{kind}.SHA256SUMS");
                    if let Some(data) = src.get(&path)? {
                        let sig = src
                            .get(&format!("{path}.asc"))?
                            .context("SHA256SUMS without its .asc")?;
                        let listed = sums::parse(&data).iter().any(|(_, n)| n == name);
                        found = Some(Attestation::PgpSums { sums: data, sig });
                        if listed {
                            break;
                        }
                    }
                }
                Ok(found)
            }
            FeedKind::InToto => {
                for f in ["attestation.sigstore.json", "attestation.dsse.json"] {
                    if let Some(d) = src.get(&format!("{release}/{rebuilder}/{f}"))? {
                        return Ok(Some(Attestation::Dsse(d)));
                    }
                }
                Ok(None)
            }
        }
    }
}