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/// `transport.binary_url` against the manifest's
269/// `transport.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
367/// Verify a **detached** ed25519 signature over raw `bytes` against a
368/// base64-encoded 32-byte public key and base64-encoded 64-byte
369/// signature. Used to authenticate documents that aren't manifests (e.g.
370/// the refreshable model catalog in `car-inference`): the source signs
371/// the exact file bytes, so no canonicalization is needed.
372pub fn verify_detached(
373    bytes: &[u8],
374    signature_b64: &str,
375    public_key_b64: &str,
376) -> Result<(), BundleError> {
377    let key_bytes = decode_base64(public_key_b64)
378        .map_err(|e| BundleError::KeyMalformed(format!("public key base64: {e}")))?;
379    let key_arr: [u8; 32] = key_bytes
380        .as_slice()
381        .try_into()
382        .map_err(|_| BundleError::KeyMalformed("public key must be 32 bytes".into()))?;
383    let verifying =
384        VerifyingKey::from_bytes(&key_arr).map_err(|e| BundleError::KeyMalformed(e.to_string()))?;
385    let sig_bytes = decode_base64(signature_b64)
386        .map_err(|e| BundleError::SignatureInvalid(format!("signature base64: {e}")))?;
387    let sig_arr: [u8; 64] = sig_bytes
388        .as_slice()
389        .try_into()
390        .map_err(|_| BundleError::SignatureInvalid("signature must be 64 bytes".into()))?;
391    let signature = Signature::from_bytes(&sig_arr);
392    verifying
393        .verify(bytes, &signature)
394        .map_err(|e| BundleError::SignatureInvalid(e.to_string()))
395}
396
397fn encode_base64(bytes: &[u8]) -> String {
398    use base64::Engine;
399    base64::engine::general_purpose::STANDARD.encode(bytes)
400}
401
402fn decode_base64(s: &str) -> Result<Vec<u8>, String> {
403    use base64::Engine;
404    base64::engine::general_purpose::STANDARD
405        .decode(s)
406        .map_err(|e| e.to_string())
407}
408
409#[cfg(test)]
410mod tests {
411    use super::*;
412    use ed25519_dalek::SigningKey;
413    use rand_core::OsRng;
414
415    fn sample_manifest() -> AgentManifest {
416        AgentManifest {
417            agent: AgentIdentity {
418                id: "ui-improver".into(),
419                name: "UI Improvement".into(),
420                namespace: Some("parslee".into()),
421                version: Some("0.1.0".into()),
422                description: Some("Issues A2UI patches".into()),
423                license: Some("Apache-2.0".into()),
424                homepage: None,
425            },
426            publisher: None,
427            runtime: Some(RuntimeRequirements {
428                car_min_version: Some("0.8.0".into()),
429                bundle_format_version: 1,
430            }),
431            lifecycle: Some(LifecyclePolicy {
432                stateful: true,
433                persistence: Some("host".into()),
434                default_inference_complexity: Some("low".into()),
435            }),
436            transport: TransportSpec::ExternalProcess(ExternalProcessTransport {
437                command: Some("/usr/local/bin/ui-improver".into()),
438                binary_url: None,
439                sha256: Some("abc123".into()),
440                health_url: None,
441                args: vec!["--mode".into(), "a2ui".into()],
442                cwd: None,
443                env: BTreeMap::new(),
444                restart: RestartPolicy::OnFailure,
445                max_restarts: 10,
446                backoff_secs: 5,
447                auto_start: false,
448                token: String::new(),
449            }),
450            capabilities: None,
451        }
452    }
453
454    #[test]
455    fn round_trip_through_toml() {
456        let m = sample_manifest();
457        let text = m.to_toml_string().unwrap();
458        let round = AgentManifest::from_toml_str(&text).unwrap();
459        assert_eq!(round.agent.id, m.agent.id);
460        assert_eq!(round.agent.namespace, m.agent.namespace);
461        match (&round.transport, &m.transport) {
462            (TransportSpec::ExternalProcess(a), TransportSpec::ExternalProcess(b)) => {
463                assert_eq!(a.command, b.command);
464                assert_eq!(a.sha256, b.sha256);
465                assert_eq!(a.args, b.args);
466            }
467            _ => panic!("transport kind drift after round-trip"),
468        }
469    }
470
471    #[test]
472    fn contrib_template_manifest_parses_external_process_command() {
473        let text = include_str!("../../../examples/contrib-template/manifest.toml");
474        let manifest = AgentManifest::from_toml_str(text).unwrap();
475
476        match manifest.transport {
477            TransportSpec::ExternalProcess(transport) => {
478                assert_eq!(
479                    transport.command.as_deref(),
480                    Some("/absolute/path/to/contrib-template/agent.sh")
481                );
482            }
483            TransportSpec::PureData => panic!("contrib template must use external_process"),
484        }
485    }
486
487    #[test]
488    fn canonical_bytes_clear_signature_for_signing() {
489        let mut m = sample_manifest();
490        m.publisher = Some(PublisherInfo {
491            key_id: Some("abc".into()),
492            signature: Some("REAL_SIG".into()),
493        });
494        let bytes = canonical_manifest_bytes(&m).unwrap();
495        let s = std::str::from_utf8(&bytes).unwrap();
496        // The signature is cleared in the canonical form — otherwise
497        // a signature couldn't sign itself.
498        assert!(!s.contains("REAL_SIG"));
499        // key_id stays in (it's a claim, not the signature).
500        assert!(s.contains("abc"));
501    }
502
503    #[test]
504    fn manifest_digest_is_stable() {
505        let m = sample_manifest();
506        let a = manifest_digest_hex(&m).unwrap();
507        let b = manifest_digest_hex(&m).unwrap();
508        assert_eq!(a, b);
509        assert_eq!(a.len(), 64); // SHA-256 = 32 bytes hex = 64 chars
510    }
511
512    #[test]
513    fn sign_then_verify_round_trip() {
514        let mut m = sample_manifest();
515        let key = SigningKey::generate(&mut OsRng);
516        sign_manifest(&mut m, &key).unwrap();
517        verify_signature(&m).expect("freshly signed manifest must verify");
518    }
519
520    #[test]
521    fn verify_fails_on_tampered_manifest() {
522        let mut m = sample_manifest();
523        let key = SigningKey::generate(&mut OsRng);
524        sign_manifest(&mut m, &key).unwrap();
525        // Tamper with a field after signing.
526        if let TransportSpec::ExternalProcess(ref mut t) = m.transport {
527            t.command = Some("/tmp/malicious".into());
528        }
529        let err = verify_signature(&m).expect_err("tampered manifest must fail verify");
530        assert!(matches!(err, BundleError::SignatureInvalid(_)));
531    }
532
533    #[test]
534    fn verify_fails_on_missing_publisher() {
535        let m = sample_manifest();
536        let err = verify_signature(&m).expect_err("unsigned manifest must error");
537        assert!(matches!(err, BundleError::PublisherMissing));
538    }
539
540    #[test]
541    fn verify_fails_on_wrong_key() {
542        let mut m = sample_manifest();
543        let key = SigningKey::generate(&mut OsRng);
544        sign_manifest(&mut m, &key).unwrap();
545        // Substitute a different key_id.
546        let other = SigningKey::generate(&mut OsRng);
547        m.publisher.as_mut().unwrap().key_id =
548            Some(encode_base64(other.verifying_key().as_bytes()));
549        let err = verify_signature(&m).expect_err("wrong key_id must fail verify");
550        assert!(matches!(err, BundleError::SignatureInvalid(_)));
551    }
552
553    #[test]
554    fn sha256_hex_matches_known_value() {
555        // Empty input → SHA-256 of zero bytes.
556        let empty = sha256_hex(b"");
557        assert_eq!(
558            empty,
559            "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
560        );
561        // Known vector for "abc".
562        let abc = sha256_hex(b"abc");
563        assert_eq!(
564            abc,
565            "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
566        );
567    }
568
569    #[test]
570    fn verify_sha256_accepts_match_rejects_mismatch_and_is_case_insensitive() {
571        let bytes = b"agent-binary-payload";
572        let digest = sha256_hex(bytes);
573        verify_sha256(bytes, &digest).expect("matching digest must verify");
574        verify_sha256(bytes, &digest.to_uppercase())
575            .expect("verify is case-insensitive on the hex digest");
576        let err = verify_sha256(bytes, "00".repeat(32).as_str())
577            .expect_err("non-matching digest must fail");
578        assert!(matches!(err, BundleError::SignatureInvalid(_)));
579    }
580
581    #[test]
582    fn signing_is_idempotent_with_same_key() {
583        // Signing the same manifest twice with the same key
584        // produces identical bytes — ed25519 is deterministic.
585        let mut a = sample_manifest();
586        let mut b = sample_manifest();
587        let key = SigningKey::generate(&mut OsRng);
588        sign_manifest(&mut a, &key).unwrap();
589        sign_manifest(&mut b, &key).unwrap();
590        assert_eq!(
591            a.publisher.as_ref().unwrap().signature,
592            b.publisher.as_ref().unwrap().signature
593        );
594    }
595}