Skip to main content

car_bundle/
lib.rs

1//! Manifest format, canonicalization, and ed25519 signing for
2//! CAR contributed-agent bundles (Parslee-ai/car#182).
3//!
4//! ## Scope
5//!
6//! This crate owns the on-disk shape and crypto primitives shared
7//! between the supervisor (loads + verifies installed agents),
8//! the CLI (publishes + signs new agents), and the registry
9//! (serves signed manifests). It is `no-runtime` — pure data,
10//! pure functions, no async, no I/O outside `read_to_string` for
11//! tests. The supervisor and CLI hold the I/O.
12//!
13//! ## Phase status
14//!
15//! - **Phase 1** (`car-registry`): the manifest format landed
16//!   inline at `car_registry::manifest`. The supervisor dual-reads
17//!   legacy `agents.json` and the new `~/.car/agents/<id>/manifest.toml`
18//!   layout. Signature verification was stubbed out.
19//! - **Phase 2** (this crate): types extracted here; ed25519
20//!   sign/verify added; manifest-level canonicalization landed.
21//!   The supervisor wires verification with warn-but-not-reject
22//!   semantics so existing setups keep working while operators
23//!   sign their agents.
24//! - **Phase 3+**: full-bundle canonicalization (multi-file:
25//!   `identity.md`, `skills.jsonl`, `policies.json`, …) per
26//!   `docs/agent-bundle-spec.md §canonicalization`. Today's
27//!   `canonical_manifest_bytes` covers only the single
28//!   `manifest.toml` file — sufficient for `external_process`
29//!   bundles which carry no auxiliary data files.
30
31use std::collections::BTreeMap;
32use std::path::PathBuf;
33
34use ed25519_dalek::{Signature, Signer, SigningKey, Verifier, VerifyingKey};
35use serde::{Deserialize, Serialize};
36use sha2::{Digest, Sha256};
37
38#[derive(Debug, thiserror::Error)]
39pub enum BundleError {
40    #[error("manifest is not valid TOML: {0}")]
41    InvalidToml(String),
42    #[error("manifest is not valid JSON: {0}")]
43    InvalidJson(String),
44    #[error("manifest validation failed: {0}")]
45    Validation(String),
46    #[error("signature verification failed: {0}")]
47    SignatureInvalid(String),
48    #[error("publisher key is malformed: {0}")]
49    KeyMalformed(String),
50    #[error("missing publisher info on signed manifest")]
51    PublisherMissing,
52    #[error("bundle I/O error: {0}")]
53    Io(#[from] std::io::Error),
54}
55
56/// `manifest.toml` top-level structure. One file per installed
57/// agent at `~/.car/agents/<id>/manifest.toml`.
58#[derive(Debug, Clone, Serialize, Deserialize)]
59pub struct AgentManifest {
60    pub agent: AgentIdentity,
61    #[serde(default, skip_serializing_if = "Option::is_none")]
62    pub publisher: Option<PublisherInfo>,
63    #[serde(default, skip_serializing_if = "Option::is_none")]
64    pub runtime: Option<RuntimeRequirements>,
65    #[serde(default, skip_serializing_if = "Option::is_none")]
66    pub lifecycle: Option<LifecyclePolicy>,
67    pub transport: TransportSpec,
68    #[serde(default, skip_serializing_if = "Option::is_none")]
69    pub capabilities: Option<CapabilityDeclarations>,
70}
71
72/// `[agent]` block.
73#[derive(Debug, Clone, Serialize, Deserialize)]
74pub struct AgentIdentity {
75    pub id: String,
76    pub name: String,
77    #[serde(default, skip_serializing_if = "Option::is_none")]
78    pub namespace: Option<String>,
79    #[serde(default, skip_serializing_if = "Option::is_none")]
80    pub version: Option<String>,
81    #[serde(default, skip_serializing_if = "Option::is_none")]
82    pub description: Option<String>,
83    #[serde(default, skip_serializing_if = "Option::is_none")]
84    pub license: Option<String>,
85    #[serde(default, skip_serializing_if = "Option::is_none")]
86    pub homepage: Option<String>,
87}
88
89/// `[publisher]` block. The `signature` field is the base64-
90/// encoded ed25519 signature over the canonicalized manifest
91/// (i.e., the manifest with `publisher.signature` cleared).
92/// `key_id` is the ed25519 public key, base64-encoded (32 bytes
93/// raw → 44 char base64).
94#[derive(Debug, Clone, Default, Serialize, Deserialize)]
95pub struct PublisherInfo {
96    #[serde(default, skip_serializing_if = "Option::is_none")]
97    pub key_id: Option<String>,
98    #[serde(default, skip_serializing_if = "Option::is_none")]
99    pub signature: Option<String>,
100}
101
102#[derive(Debug, Clone, Default, Serialize, Deserialize)]
103#[serde(rename_all = "snake_case")]
104pub struct RuntimeRequirements {
105    #[serde(default, skip_serializing_if = "Option::is_none")]
106    pub car_min_version: Option<String>,
107    #[serde(default = "default_bundle_format_version")]
108    pub bundle_format_version: u32,
109}
110
111fn default_bundle_format_version() -> u32 {
112    1
113}
114
115#[derive(Debug, Clone, Default, Serialize, Deserialize)]
116pub struct LifecyclePolicy {
117    #[serde(default)]
118    pub stateful: bool,
119    #[serde(default, skip_serializing_if = "Option::is_none")]
120    pub persistence: Option<String>,
121    #[serde(default, skip_serializing_if = "Option::is_none")]
122    pub default_inference_complexity: Option<String>,
123}
124
125#[derive(Debug, Clone, Serialize, Deserialize)]
126#[serde(tag = "kind", rename_all = "snake_case")]
127pub enum TransportSpec {
128    PureData,
129    ExternalProcess(ExternalProcessTransport),
130}
131
132#[derive(Debug, Clone, Default, Serialize, Deserialize)]
133pub struct ExternalProcessTransport {
134    #[serde(default, skip_serializing_if = "Option::is_none")]
135    pub command: Option<String>,
136    #[serde(default, skip_serializing_if = "Option::is_none")]
137    pub sha256: Option<String>,
138    /// Optional `https://` URL the publisher hosts the binary at.
139    /// When present, `car install` fetches the binary from this
140    /// URL, verifies the digest against `sha256`, and writes the
141    /// resulting file at the local `command` path before adoption
142    /// (Parslee-ai/car#182 phase 5). Mutually exclusive with
143    /// `health_url`. Locally-developed manifests can leave this
144    /// unset and ship `command` pointing at a binary the
145    /// developer placed there manually.
146    #[serde(default, skip_serializing_if = "Option::is_none")]
147    pub binary_url: Option<String>,
148    #[serde(default, skip_serializing_if = "Option::is_none")]
149    pub health_url: Option<String>,
150    #[serde(default)]
151    pub args: Vec<String>,
152    #[serde(default, skip_serializing_if = "Option::is_none")]
153    pub cwd: Option<PathBuf>,
154    #[serde(default)]
155    pub env: BTreeMap<String, String>,
156    #[serde(default)]
157    pub restart: RestartPolicy,
158    #[serde(default = "default_max_restarts")]
159    pub max_restarts: u32,
160    #[serde(default = "default_backoff")]
161    pub backoff_secs: u64,
162    #[serde(default)]
163    pub auto_start: bool,
164    #[serde(default)]
165    pub token: String,
166}
167
168fn default_max_restarts() -> u32 {
169    10
170}
171
172fn default_backoff() -> u64 {
173    5
174}
175
176/// Restart policy mirrors the supervisor's surface. Re-declared
177/// here (rather than re-exported from `car-registry`) so this
178/// crate stays standalone — `car-registry` depends on
179/// `car-bundle`, not the other way around.
180#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
181#[serde(rename_all = "snake_case")]
182pub enum RestartPolicy {
183    Never,
184    #[default]
185    OnFailure,
186    Always,
187}
188
189#[derive(Debug, Clone, Default, Serialize, Deserialize)]
190pub struct CapabilityDeclarations {
191    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
192    pub required: BTreeMap<String, Vec<String>>,
193    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
194    pub optional: BTreeMap<String, Vec<String>>,
195    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
196    pub denied: BTreeMap<String, Vec<String>>,
197}
198
199impl AgentManifest {
200    pub fn is_pure_data(&self) -> bool {
201        matches!(self.transport, TransportSpec::PureData)
202    }
203
204    pub fn is_remote_service(&self) -> bool {
205        matches!(
206            &self.transport,
207            TransportSpec::ExternalProcess(t) if t.health_url.is_some() && t.command.is_none()
208        )
209    }
210
211    /// Parse a `manifest.toml` text. Does NOT verify the signature
212    /// — pair with [`verify_signature`] when verification is
213    /// required.
214    pub fn from_toml_str(text: &str) -> Result<Self, BundleError> {
215        toml::from_str(text).map_err(|e| BundleError::InvalidToml(e.to_string()))
216    }
217
218    /// Serialize back to canonical TOML text. Round-trips through
219    /// `to_string_pretty` and back via `from_toml_str`.
220    pub fn to_toml_string(&self) -> Result<String, BundleError> {
221        toml::to_string_pretty(self).map_err(|e| BundleError::InvalidToml(e.to_string()))
222    }
223}
224
225// ---------------------------------------------------------------------
226// Canonicalization
227// ---------------------------------------------------------------------
228
229/// Produce canonical bytes of a manifest for signing / verification.
230///
231/// Canonicalization rules for the phase-2 single-file form:
232///
233/// 1. The `publisher.signature` field is cleared (a signature
234///    cannot sign itself).
235/// 2. The manifest is serialized to JSON (not TOML — TOML lacks a
236///    spec-mandated canonical form; serde_json with sorted keys
237///    via `BTreeMap` is well-defined).
238/// 3. Whitespace is stripped (no pretty-printing).
239/// 4. Output is UTF-8 bytes, LF line endings.
240///
241/// Multi-file bundle canonicalization (per
242/// `docs/agent-bundle-spec.md §canonicalization`) lands in a later
243/// phase when pure-data bundles need it.
244pub fn canonical_manifest_bytes(manifest: &AgentManifest) -> Result<Vec<u8>, BundleError> {
245    let mut cleared = manifest.clone();
246    if let Some(pub_info) = cleared.publisher.as_mut() {
247        pub_info.signature = None;
248    }
249    // JSON because it has a deterministic canonical form when
250    // keys are sorted, and the spec is unambiguous. TOML's
251    // round-trip whitespace handling is loose enough that two
252    // serializers can disagree on the bytes.
253    serde_json::to_vec(&cleared).map_err(|e| BundleError::InvalidJson(e.to_string()))
254}
255
256/// SHA-256 hex digest of the canonical manifest bytes. Useful for
257/// content-addressed lookups (registry caching, etc.) without
258/// requiring signature verification.
259pub fn manifest_digest_hex(manifest: &AgentManifest) -> Result<String, BundleError> {
260    let bytes = canonical_manifest_bytes(manifest)?;
261    let mut hasher = Sha256::new();
262    hasher.update(&bytes);
263    Ok(hex(&hasher.finalize()))
264}
265
266/// SHA-256 hex digest of an arbitrary byte slice. Used by
267/// `car install` to verify binaries fetched via
268/// `external_process.binary_url` against the manifest's
269/// `external_process.sha256` (Parslee-ai/car#182 phase 5).
270pub fn sha256_hex(bytes: &[u8]) -> String {
271    let mut hasher = Sha256::new();
272    hasher.update(bytes);
273    hex(&hasher.finalize())
274}
275
276/// Verify a byte slice matches a hex-encoded SHA-256 digest.
277/// Comparison is constant-time-ish (case-insensitive hex
278/// equality), and returns an error rather than a bool so the
279/// failure message can name the expected + actual digests.
280pub fn verify_sha256(bytes: &[u8], expected_hex: &str) -> Result<(), BundleError> {
281    let actual = sha256_hex(bytes);
282    if actual.eq_ignore_ascii_case(expected_hex) {
283        Ok(())
284    } else {
285        Err(BundleError::SignatureInvalid(format!(
286            "sha256 mismatch: expected `{expected_hex}`, got `{actual}`"
287        )))
288    }
289}
290
291fn hex(bytes: &[u8]) -> String {
292    let mut out = String::with_capacity(bytes.len() * 2);
293    for b in bytes {
294        out.push_str(&format!("{:02x}", b));
295    }
296    out
297}
298
299// ---------------------------------------------------------------------
300// Signing + verification
301// ---------------------------------------------------------------------
302
303/// Sign a manifest in place: writes the public key id into
304/// `publisher.key_id`, then serializes canonical bytes (which
305/// clear the signature but include the key_id), signs them with
306/// `key`, and writes the base64 signature into
307/// `publisher.signature`. Replaces any existing signature.
308///
309/// Ordering matters: key_id MUST be set BEFORE computing canonical
310/// bytes for signing, so verification sees the same input. A
311/// previous draft set key_id after computing the bytes and the
312/// signature failed to verify — caught by
313/// `sign_then_verify_round_trip`.
314pub fn sign_manifest(manifest: &mut AgentManifest, key: &SigningKey) -> Result<(), BundleError> {
315    let pub_info = manifest
316        .publisher
317        .get_or_insert_with(PublisherInfo::default);
318    pub_info.key_id = Some(encode_base64(key.verifying_key().as_bytes()));
319    pub_info.signature = None;
320    let bytes = canonical_manifest_bytes(manifest)?;
321    let signature = key.sign(&bytes);
322    // Re-borrow — the canonical-bytes call took an immutable view.
323    manifest.publisher.as_mut().unwrap().signature = Some(encode_base64(&signature.to_bytes()));
324    Ok(())
325}
326
327/// Verify a manifest's signature against the embedded
328/// `publisher.key_id`. Returns `Ok(())` on success, `Err(...)` on
329/// any failure (missing publisher, malformed key, mismatched
330/// signature). A manifest with no `publisher` block is treated as
331/// unsigned and rejected — callers that want to accept unsigned
332/// manifests should not call this function.
333pub fn verify_signature(manifest: &AgentManifest) -> Result<(), BundleError> {
334    let pub_info = manifest
335        .publisher
336        .as_ref()
337        .ok_or(BundleError::PublisherMissing)?;
338    let key_b64 = pub_info
339        .key_id
340        .as_deref()
341        .ok_or_else(|| BundleError::KeyMalformed("missing key_id".into()))?;
342    let sig_b64 = pub_info
343        .signature
344        .as_deref()
345        .ok_or_else(|| BundleError::SignatureInvalid("missing signature".into()))?;
346    let key_bytes = decode_base64(key_b64)
347        .map_err(|e| BundleError::KeyMalformed(format!("key_id base64: {e}")))?;
348    let key_arr: [u8; 32] = key_bytes
349        .as_slice()
350        .try_into()
351        .map_err(|_| BundleError::KeyMalformed("key_id must be 32 bytes".into()))?;
352    let verifying =
353        VerifyingKey::from_bytes(&key_arr).map_err(|e| BundleError::KeyMalformed(e.to_string()))?;
354    let sig_bytes = decode_base64(sig_b64)
355        .map_err(|e| BundleError::SignatureInvalid(format!("signature base64: {e}")))?;
356    let sig_arr: [u8; 64] = sig_bytes
357        .as_slice()
358        .try_into()
359        .map_err(|_| BundleError::SignatureInvalid("signature must be 64 bytes".into()))?;
360    let signature = Signature::from_bytes(&sig_arr);
361    let bytes = canonical_manifest_bytes(manifest)?;
362    verifying
363        .verify(&bytes, &signature)
364        .map_err(|e| BundleError::SignatureInvalid(e.to_string()))
365}
366
367fn encode_base64(bytes: &[u8]) -> String {
368    use base64::Engine;
369    base64::engine::general_purpose::STANDARD.encode(bytes)
370}
371
372fn decode_base64(s: &str) -> Result<Vec<u8>, String> {
373    use base64::Engine;
374    base64::engine::general_purpose::STANDARD
375        .decode(s)
376        .map_err(|e| e.to_string())
377}
378
379#[cfg(test)]
380mod tests {
381    use super::*;
382    use ed25519_dalek::SigningKey;
383    use rand_core::OsRng;
384
385    fn sample_manifest() -> AgentManifest {
386        AgentManifest {
387            agent: AgentIdentity {
388                id: "ui-improver".into(),
389                name: "UI Improvement".into(),
390                namespace: Some("parslee".into()),
391                version: Some("0.1.0".into()),
392                description: Some("Issues A2UI patches".into()),
393                license: Some("Apache-2.0".into()),
394                homepage: None,
395            },
396            publisher: None,
397            runtime: Some(RuntimeRequirements {
398                car_min_version: Some("0.8.0".into()),
399                bundle_format_version: 1,
400            }),
401            lifecycle: Some(LifecyclePolicy {
402                stateful: true,
403                persistence: Some("host".into()),
404                default_inference_complexity: Some("low".into()),
405            }),
406            transport: TransportSpec::ExternalProcess(ExternalProcessTransport {
407                command: Some("/usr/local/bin/ui-improver".into()),
408                binary_url: None,
409                sha256: Some("abc123".into()),
410                health_url: None,
411                args: vec!["--mode".into(), "a2ui".into()],
412                cwd: None,
413                env: BTreeMap::new(),
414                restart: RestartPolicy::OnFailure,
415                max_restarts: 10,
416                backoff_secs: 5,
417                auto_start: false,
418                token: String::new(),
419            }),
420            capabilities: None,
421        }
422    }
423
424    #[test]
425    fn round_trip_through_toml() {
426        let m = sample_manifest();
427        let text = m.to_toml_string().unwrap();
428        let round = AgentManifest::from_toml_str(&text).unwrap();
429        assert_eq!(round.agent.id, m.agent.id);
430        assert_eq!(round.agent.namespace, m.agent.namespace);
431        match (&round.transport, &m.transport) {
432            (TransportSpec::ExternalProcess(a), TransportSpec::ExternalProcess(b)) => {
433                assert_eq!(a.command, b.command);
434                assert_eq!(a.sha256, b.sha256);
435                assert_eq!(a.args, b.args);
436            }
437            _ => panic!("transport kind drift after round-trip"),
438        }
439    }
440
441    #[test]
442    fn canonical_bytes_clear_signature_for_signing() {
443        let mut m = sample_manifest();
444        m.publisher = Some(PublisherInfo {
445            key_id: Some("abc".into()),
446            signature: Some("REAL_SIG".into()),
447        });
448        let bytes = canonical_manifest_bytes(&m).unwrap();
449        let s = std::str::from_utf8(&bytes).unwrap();
450        // The signature is cleared in the canonical form — otherwise
451        // a signature couldn't sign itself.
452        assert!(!s.contains("REAL_SIG"));
453        // key_id stays in (it's a claim, not the signature).
454        assert!(s.contains("abc"));
455    }
456
457    #[test]
458    fn manifest_digest_is_stable() {
459        let m = sample_manifest();
460        let a = manifest_digest_hex(&m).unwrap();
461        let b = manifest_digest_hex(&m).unwrap();
462        assert_eq!(a, b);
463        assert_eq!(a.len(), 64); // SHA-256 = 32 bytes hex = 64 chars
464    }
465
466    #[test]
467    fn sign_then_verify_round_trip() {
468        let mut m = sample_manifest();
469        let key = SigningKey::generate(&mut OsRng);
470        sign_manifest(&mut m, &key).unwrap();
471        verify_signature(&m).expect("freshly signed manifest must verify");
472    }
473
474    #[test]
475    fn verify_fails_on_tampered_manifest() {
476        let mut m = sample_manifest();
477        let key = SigningKey::generate(&mut OsRng);
478        sign_manifest(&mut m, &key).unwrap();
479        // Tamper with a field after signing.
480        if let TransportSpec::ExternalProcess(ref mut t) = m.transport {
481            t.command = Some("/tmp/malicious".into());
482        }
483        let err = verify_signature(&m).expect_err("tampered manifest must fail verify");
484        assert!(matches!(err, BundleError::SignatureInvalid(_)));
485    }
486
487    #[test]
488    fn verify_fails_on_missing_publisher() {
489        let m = sample_manifest();
490        let err = verify_signature(&m).expect_err("unsigned manifest must error");
491        assert!(matches!(err, BundleError::PublisherMissing));
492    }
493
494    #[test]
495    fn verify_fails_on_wrong_key() {
496        let mut m = sample_manifest();
497        let key = SigningKey::generate(&mut OsRng);
498        sign_manifest(&mut m, &key).unwrap();
499        // Substitute a different key_id.
500        let other = SigningKey::generate(&mut OsRng);
501        m.publisher.as_mut().unwrap().key_id =
502            Some(encode_base64(other.verifying_key().as_bytes()));
503        let err = verify_signature(&m).expect_err("wrong key_id must fail verify");
504        assert!(matches!(err, BundleError::SignatureInvalid(_)));
505    }
506
507    #[test]
508    fn sha256_hex_matches_known_value() {
509        // Empty input → SHA-256 of zero bytes.
510        let empty = sha256_hex(b"");
511        assert_eq!(
512            empty,
513            "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
514        );
515        // Known vector for "abc".
516        let abc = sha256_hex(b"abc");
517        assert_eq!(
518            abc,
519            "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
520        );
521    }
522
523    #[test]
524    fn verify_sha256_accepts_match_rejects_mismatch_and_is_case_insensitive() {
525        let bytes = b"agent-binary-payload";
526        let digest = sha256_hex(bytes);
527        verify_sha256(bytes, &digest).expect("matching digest must verify");
528        verify_sha256(bytes, &digest.to_uppercase())
529            .expect("verify is case-insensitive on the hex digest");
530        let err = verify_sha256(bytes, "00".repeat(32).as_str())
531            .expect_err("non-matching digest must fail");
532        assert!(matches!(err, BundleError::SignatureInvalid(_)));
533    }
534
535    #[test]
536    fn signing_is_idempotent_with_same_key() {
537        // Signing the same manifest twice with the same key
538        // produces identical bytes — ed25519 is deterministic.
539        let mut a = sample_manifest();
540        let mut b = sample_manifest();
541        let key = SigningKey::generate(&mut OsRng);
542        sign_manifest(&mut a, &key).unwrap();
543        sign_manifest(&mut b, &key).unwrap();
544        assert_eq!(
545            a.publisher.as_ref().unwrap().signature,
546            b.publisher.as_ref().unwrap().signature
547        );
548    }
549}