alien-signing 3.3.72

Signed statements, signer lists, signing settings and the install ledger for installs that run only what the vendor signed
Documentation
//! The checks every enforcer runs on every statement.

use std::collections::BTreeSet;

use alien_error::AlienError;
use chrono::{DateTime, Utc};
use serde::de::DeserializeOwned;

use crate::config::ConfigBody;
use crate::envelope::{Kind, Opened, SignedStatement};
use crate::error::{ErrorData, Result};
use crate::ledger::Ledger;
use crate::release::ReleaseBody;
use crate::settings::{InstallId, SigningSettings};
use crate::signer_list::SignerChain;

/// A statement that passed every check, with its body parsed.
#[derive(Debug, Clone)]
pub struct Verified<B> {
    /// Kind.
    pub kind: Kind,
    /// Sequence.
    pub sequence: u64,
    /// `sha256:<hex>` of the payload.
    pub digest: String,
    /// Expiry.
    pub not_after: Option<DateTime<Utc>>,
    /// Whether the current signer list keeps this statement (`keep`), so no
    /// signature was needed: its signer may have left the list.
    pub kept: bool,
    /// Signer ids whose signatures counted; none for a kept statement.
    pub signers: BTreeSet<String>,
    /// The body.
    pub body: B,
}

/// What an enforcer trusts: the settings setup wrote, the install id it
/// derived itself, and the signer chain that grows from the fingerprint.
#[derive(Debug, Clone)]
pub struct Trust {
    settings: SigningSettings,
    install: InstallId,
    chain: SignerChain,
}

impl Trust {
    /// Trust `chain`, which must start at the settings' fingerprint and reach
    /// at least the signer list version `ledger` holds.
    pub fn new(
        settings: SigningSettings,
        install: InstallId,
        chain: &[SignedStatement],
        ledger: &Ledger,
    ) -> Result<Self> {
        settings.validate()?;
        install.validate()?;
        ledger.check_root(Some(&settings.fingerprint), &install)?;
        let chain = SignerChain::from_statements(&settings.fingerprint, chain)?;
        ledger.check_chain(&chain)?;
        Ok(Self {
            settings,
            install,
            chain,
        })
    }

    /// The settings.
    pub fn settings(&self) -> &SigningSettings {
        &self.settings
    }

    /// This install.
    pub fn install(&self) -> &InstallId {
        &self.install
    }

    /// The project, as signer list version 1 names it.
    pub fn project(&self) -> &str {
        self.chain.project()
    }

    /// The signer chain.
    pub fn chain(&self) -> &SignerChain {
        &self.chain
    }

    /// Accept newer signer list versions. Returns whether the chain grew.
    pub fn extend_chain(&mut self, statements: &[SignedStatement]) -> Result<bool> {
        self.chain.extend(statements)
    }

    /// Record this trust in `ledger`: the trust root and the chain's version.
    pub fn record(&self, ledger: &mut Ledger) -> Result<()> {
        ledger.check_root(Some(&self.settings.fingerprint), &self.install)?;
        ledger.check_chain(&self.chain)?;
        ledger.adopt_root(&self.settings.fingerprint, &self.install);
        ledger.record_chain(&self.chain);
        ledger.check_size()
    }

    /// Run the checks for a statement of `kind` and parse its body: project
    /// and install, the statement's own expiry, the deny-list and signatures
    /// by keys that hold the kind's role.
    ///
    /// Signatures are checked against the current signer list. A statement
    /// signed by a key that has left the list is accepted only when the
    /// current list's `keep` names its digest, kind and sequence: the admins
    /// signed that entry with the list, so the ledger and the install's
    /// history play no part, and a new machine or Operator instance accepts
    /// it the same way. The deny-list refuses a statement first. Every
    /// enforcer applies this one rule.
    pub fn verify<B: DeserializeOwned>(
        &self,
        statement: &SignedStatement,
        kind: Kind,
        now: DateTime<Utc>,
    ) -> Result<Verified<B>> {
        if kind == Kind::SignerList {
            return Err(AlienError::new(ErrorData::WrongKind {
                expected: "a statement the signer list signs".to_string(),
                actual: kind.as_str().to_string(),
            }));
        }
        let opened = statement.open()?;
        self.check_header(&opened, kind, now)?;
        let current = self.chain.current();
        if opened.signer_list_version > current.version {
            return Err(AlienError::new(ErrorData::SignerListMissing {
                needed: opened.signer_list_version,
                current: current.version,
            }));
        }
        if current.refuses(&opened) {
            return Err(AlienError::new(ErrorData::Denied {
                digest: opened.digest,
            }));
        }
        if current.keeps(&opened) {
            return finish(opened, BTreeSet::new(), true);
        }
        let signers = current.check_signatures(&opened, kind.role())?;
        finish(opened, signers, false)
    }

    /// Verify a release for this install's channel and check its rules.
    pub fn verify_release(
        &self,
        statement: &SignedStatement,
        now: DateTime<Utc>,
    ) -> Result<Verified<ReleaseBody>> {
        let verified: Verified<ReleaseBody> = self.verify(statement, Kind::Release, now)?;
        if verified.body.channel != self.settings.channel {
            return Err(AlienError::new(ErrorData::WrongChannel {
                expected: self.settings.channel.clone(),
                actual: verified.body.channel.clone(),
            }));
        }
        verified.body.validate()?;
        Ok(verified)
    }

    /// Verify a config statement for this install against the release it
    /// configures.
    pub fn verify_config(
        &self,
        statement: &SignedStatement,
        release: &ReleaseBody,
        now: DateTime<Utc>,
    ) -> Result<Verified<ConfigBody>> {
        let verified: Verified<ConfigBody> = self.verify(statement, Kind::Config, now)?;
        verified.body.check(release)?;
        Ok(verified)
    }

    fn check_header(&self, opened: &Opened, kind: Kind, now: DateTime<Utc>) -> Result<()> {
        if opened.kind != kind {
            return Err(AlienError::new(ErrorData::WrongKind {
                expected: kind.as_str().to_string(),
                actual: opened.kind.as_str().to_string(),
            }));
        }
        if opened.project != self.chain.project() {
            return Err(AlienError::new(ErrorData::WrongProject {
                expected: self.chain.project().to_string(),
                actual: opened.project.clone(),
            }));
        }
        let install = self.install.to_string();
        let expected_install = kind.is_install_bound().then_some(install.as_str());
        if opened.install.as_deref() != expected_install {
            return Err(AlienError::new(ErrorData::WrongInstall {
                expected: expected_install.unwrap_or("none").to_string(),
                actual: opened.install.clone().unwrap_or_else(|| "none".to_string()),
            }));
        }
        match opened.not_after {
            Some(not_after) if now > not_after => Err(AlienError::new(ErrorData::Expired {
                not_after: not_after.to_rfc3339(),
            })),
            _ => Ok(()),
        }
    }
}

fn finish<B: DeserializeOwned>(
    opened: Opened,
    signers: BTreeSet<String>,
    kept: bool,
) -> Result<Verified<B>> {
    let body = opened.body()?;
    Ok(Verified {
        kind: opened.kind,
        sequence: opened.sequence,
        digest: opened.digest,
        not_after: opened.not_after,
        kept,
        signers,
        body,
    })
}