use std::collections::BTreeMap;
use std::path::Path;
use serde::{Deserialize, Serialize};
use crate::error::Result;
use crate::model::{CapsuleResult, GitPath, ResultKind};
use crate::verify::{VerifyOptions, verified_bundle_result};
pub const IN_TOTO_STATEMENT_TYPE: &str = "https://in-toto.io/Statement/v1";
pub const CHANGE_PREDICATE_TYPE: &str =
"https://github.com/SiliconState/change-capsule/attestation/change/v1";
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ResourceDescriptor {
pub name: String,
pub digest: BTreeMap<String, String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ProofBoundary {
pub proves: Vec<String>,
pub does_not_prove: Vec<String>,
}
impl ProofBoundary {
#[must_use]
pub fn for_receipt(has_executed_evidence: bool) -> Self {
let mut proves = vec![
"patch-bytes-match-sealed-digest".to_owned(),
"patch-applies-to-pinned-base".to_owned(),
"patch-reproduces-sealed-bytes-and-changed-paths".to_owned(),
"result-internally-consistent".to_owned(),
];
let mut does_not_prove = vec![
"human-or-agent-authorship".to_owned(),
"code-quality-or-review-approval".to_owned(),
"producing-host-was-uncompromised".to_owned(),
];
if has_executed_evidence {
proves.push("executed-evidence-ran-in-capsule-workspace".to_owned());
} else {
does_not_prove.push("evidence-command-actually-ran".to_owned());
does_not_prove.push("evidence-output-is-truthful".to_owned());
}
Self {
proves,
does_not_prove,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct RecordedEvidence {
pub command: String,
pub exit_code: i32,
pub executed: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub output_sha256: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub summary: Option<String>,
pub bound_patch_sha256: String,
pub current_for_sealed_patch: bool,
pub recorded_at_unix: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ChangePredicate {
pub result_schema_version: u32,
pub capsule_id: String,
pub kind: ResultKind,
pub base_commit: String,
pub head_commit: String,
pub patch_bytes: u64,
pub changed_paths: Vec<GitPath>,
#[serde(skip_serializing_if = "Option::is_none")]
pub ignored_content_sha256: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub label: Option<String>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub links: BTreeMap<String, String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub evidence: Vec<RecordedEvidence>,
pub created_at_unix: u64,
pub sealed_at_unix: u64,
pub proof_boundary: ProofBoundary,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Statement {
#[serde(rename = "_type")]
pub statement_type: String,
pub subject: Vec<ResourceDescriptor>,
#[serde(rename = "predicateType")]
pub predicate_type: String,
pub predicate: ChangePredicate,
}
pub fn attest_bundle(directory: impl AsRef<Path>, options: &VerifyOptions) -> Result<Statement> {
let (_report, result) = verified_bundle_result(directory.as_ref(), options)?;
Ok(change_statement(&result))
}
pub fn change_statement(result: &CapsuleResult) -> Statement {
let sealed = result.patch_sha256.as_str();
let mut subject = vec![ResourceDescriptor {
name: "result.patch".to_owned(),
digest: BTreeMap::from([("sha256".to_owned(), result.patch_sha256.clone())]),
}];
subject.push(ResourceDescriptor {
name: "head".to_owned(),
digest: BTreeMap::from([("gitCommit".to_owned(), result.head_commit.clone())]),
});
let evidence: Vec<_> = result
.evidence
.iter()
.map(|item| RecordedEvidence {
command: item.command.clone(),
exit_code: item.exit_code,
executed: item.executed,
output_sha256: item.output_sha256.clone(),
summary: item.summary.clone(),
current_for_sealed_patch: item.patch_sha256 == sealed,
bound_patch_sha256: item.patch_sha256.clone(),
recorded_at_unix: item.recorded_at_unix,
})
.collect();
let has_executed_evidence = evidence
.iter()
.any(|item| item.executed && item.exit_code == 0 && item.current_for_sealed_patch);
Statement {
statement_type: IN_TOTO_STATEMENT_TYPE.to_owned(),
subject,
predicate_type: CHANGE_PREDICATE_TYPE.to_owned(),
predicate: ChangePredicate {
result_schema_version: result.schema_version,
capsule_id: result.capsule_id.clone(),
kind: result.kind,
base_commit: result.base_commit.clone(),
head_commit: result.head_commit.clone(),
patch_bytes: result.patch_bytes,
changed_paths: result.changed_paths.clone(),
ignored_content_sha256: result.ignored_content_sha256.clone(),
label: result.label.clone(),
links: result.links.clone(),
evidence,
created_at_unix: result.created_at_unix,
sealed_at_unix: result.sealed_at_unix,
proof_boundary: ProofBoundary::for_receipt(has_executed_evidence),
},
}
}