harn-session-store 0.10.25

Durable Harn session event store primitives
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//! Ed25519 signing for session event chains.
//!
//! The store stamps each event with a sha256 `record_hash` linking
//! back to its predecessor's hash. The `SessionSigner` finalises a
//! session by emitting a `Receipt` event whose signature covers the
//! entire chain. The same key also signs individual events when the
//! caller opts in (`append_signed`) — useful for cross-tenant audit
//! trails where every event needs independent verification.
//!
//! Key material is deterministically derived from a 32-byte seed
//! supplied by the host. In production the seed comes from the
//! configured secret store ([`harn_vm::provenance::load_or_generate_agent_signing_key`]
//! is the long-form helper); for tests we accept a literal seed so
//! the verifier can be exercised without a real KMS.

use base64::Engine as _;
use ed25519_dalek::{Signature, Signer, SigningKey, Verifier, VerifyingKey};
use sha2::{Digest, Sha256};

use super::event::{
    canonical_event_bytes, canonical_json_bytes, EventId, EventSignature, SessionEventKind,
    StoredEvent,
};
use super::store::{SessionMeta, VerifyFailure, VerifyReport};

pub const ALGORITHM: &str = "ed25519";

#[derive(Clone)]
pub struct SessionSigner {
    inner: std::sync::Arc<SigningKey>,
    key_id: String,
}

impl SessionSigner {
    /// Build a signer from a 32-byte seed. The verifying key id is
    /// computed as `"sha256:" + hex(sha256(verifying_key_bytes))[..32]`
    /// so the same seed always produces the same `signed_by.key_id`.
    pub fn from_seed(seed: [u8; 32]) -> Self {
        let key = SigningKey::from_bytes(&seed);
        let key_id = key_id_for(&key.verifying_key());
        Self {
            inner: std::sync::Arc::new(key),
            key_id,
        }
    }

    pub fn key_id(&self) -> &str {
        &self.key_id
    }

    pub fn verifying_key(&self) -> VerifyingKey {
        self.inner.verifying_key()
    }

    pub fn sign_event(&self, event: &StoredEvent) -> EventSignature {
        let bytes = canonical_event_bytes(event);
        sign_bytes(&self.inner, &self.key_id, &bytes)
    }

    /// Sign a finalisation receipt over the full event-root hash. The
    /// payload bytes are folded into the receipt event so a verifier
    /// can recompute them from the stored chain alone.
    pub fn sign_receipt(&self, receipt_root_hash: &str) -> EventSignature {
        let material = receipt_signing_material(receipt_root_hash);
        sign_bytes(&self.inner, &self.key_id, material.as_bytes())
    }
}

fn sign_bytes(key: &SigningKey, key_id: &str, bytes: &[u8]) -> EventSignature {
    let signature: Signature = key.sign(bytes);
    EventSignature {
        algorithm: ALGORITHM.to_string(),
        key_id: key_id.to_string(),
        signature: base64::engine::general_purpose::STANDARD.encode(signature.to_bytes()),
    }
}

fn receipt_signing_material(receipt_root_hash: &str) -> String {
    format!("harn.session.receipt.v1\nroot={receipt_root_hash}\n")
}

pub fn key_id_for(verifying_key: &VerifyingKey) -> String {
    let mut hasher = Sha256::new();
    hasher.update(verifying_key.as_bytes());
    let digest = hasher.finalize();
    format!("sha256:{}", &hex::encode(digest)[..32])
}

/// Compute the canonical record hash for an event with `prev_hash`
/// already populated. Result is `"sha256:<hex>"`.
pub fn compute_record_hash(event: &StoredEvent) -> String {
    let mut hasher = Sha256::new();
    hasher.update(canonical_event_bytes(event));
    finalize_sha256(hasher)
}

/// Wrap a finalised SHA-256 in the canonical `"sha256:<hex>"` label
/// every chain primitive prints. Centralising the format keeps the
/// algorithm tag in one place so a future cutover to a different hash
/// doesn't fan out across the module.
fn finalize_sha256(hasher: Sha256) -> String {
    format!("sha256:{}", hex::encode(hasher.finalize()))
}

/// Verify a per-event signature.
#[derive(Debug)]
pub enum VerifyError {
    NotSigned,
    UnsupportedAlgorithm(String),
    DecodeError(String),
    InvalidShape(String),
    BadSignature,
    HashMismatch { stored: String, computed: String },
}

impl std::fmt::Display for VerifyError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::NotSigned => write!(f, "event is not signed"),
            Self::UnsupportedAlgorithm(algorithm) => {
                write!(f, "unsupported signature algorithm '{algorithm}'")
            }
            Self::DecodeError(message) => {
                write!(f, "signature base64 decode failed: {message}")
            }
            Self::InvalidShape(message) => write!(f, "signature shape invalid: {message}"),
            Self::BadSignature => write!(f, "signature did not verify against the key"),
            Self::HashMismatch { stored, computed } => {
                write!(
                    f,
                    "record_hash mismatch: stored '{stored}' vs computed '{computed}'"
                )
            }
        }
    }
}

impl std::error::Error for VerifyError {}

pub fn verify_event(event: &StoredEvent, verifying_key: &VerifyingKey) -> Result<(), VerifyError> {
    if event.is_redacted_projection() {
        return Err(VerifyError::InvalidShape(format!(
            "redacted projection cannot authenticate canonical source hash '{}'",
            event.source_record_hash()
        )));
    }
    let computed = compute_record_hash(event);
    if computed != event.record_hash {
        return Err(VerifyError::HashMismatch {
            stored: event.record_hash.clone(),
            computed,
        });
    }
    let Some(signed_by) = event.signed_by.as_ref() else {
        return Err(VerifyError::NotSigned);
    };
    verify_signature(signed_by, verifying_key, &canonical_event_bytes(event))
}

pub fn verify_receipt_root(
    signed_by: &EventSignature,
    verifying_key: &VerifyingKey,
    receipt_root_hash: &str,
) -> Result<(), VerifyError> {
    let material = receipt_signing_material(receipt_root_hash);
    verify_signature(signed_by, verifying_key, material.as_bytes())
}

fn verify_signature(
    signed_by: &EventSignature,
    verifying_key: &VerifyingKey,
    bytes: &[u8],
) -> Result<(), VerifyError> {
    if signed_by.algorithm != ALGORITHM {
        return Err(VerifyError::UnsupportedAlgorithm(
            signed_by.algorithm.clone(),
        ));
    }
    let signature_bytes = base64::engine::general_purpose::STANDARD
        .decode(&signed_by.signature)
        .map_err(|error| VerifyError::DecodeError(error.to_string()))?;
    let signature = Signature::from_slice(&signature_bytes)
        .map_err(|error| VerifyError::InvalidShape(error.to_string()))?;
    verifying_key
        .verify(bytes, &signature)
        .map_err(|_| VerifyError::BadSignature)
}

/// Verify an entire stored event chain in one pass. For each event this
/// checks the record-hash link, then verifies its signature (when
/// present) against the appropriate key:
///
/// - A [`SessionEventKind::Receipt`] event's signature attests the
///   *pre-receipt chain root* (see [`SessionSigner::sign_receipt`]), not
///   the receipt event's own canonical bytes. It is verified with
///   [`verify_receipt_root`] against the chain root recomputed over every
///   event preceding it, using `receipt_verifier`.
/// - Every other signed event is verified with [`verify_event`] against
///   its own canonical bytes, using `event_verifier`.
///
/// When the relevant verifier is `None` a present signature is counted as
/// signed-but-unverified, matching the historical behaviour of a store
/// that persisted signatures but was reopened without the signing key.
///
/// Returns `(signed_event_count, failures)` where each failure is
/// `(event_id, reason)`. Both backends map that into their `VerifyReport`.
pub fn verify_event_chain(
    events: &[StoredEvent],
    event_verifier: Option<&VerifyingKey>,
    receipt_verifier: Option<&VerifyingKey>,
) -> (usize, Vec<(EventId, String)>) {
    let (signed, failures) = verify_event_chain_detailed(events, event_verifier, receipt_verifier);
    (
        signed,
        failures
            .into_iter()
            .map(|failure| (failure.event_id, failure.reason))
            .collect(),
    )
}

struct ChainFailure {
    event_id: EventId,
    reason: String,
}

fn verify_event_chain_detailed(
    events: &[StoredEvent],
    event_verifier: Option<&VerifyingKey>,
    receipt_verifier: Option<&VerifyingKey>,
) -> (usize, Vec<ChainFailure>) {
    let mut signed = 0usize;
    let mut failures = Vec::new();
    let mut expected_prev_hash: Option<&str> = None;
    for (index, event) in events.iter().enumerate() {
        let expected_event_id = index as EventId + 1;
        if event.event_id != expected_event_id {
            failures.push(ChainFailure {
                event_id: event.event_id,
                reason: format!(
                    "event_id sequence gap: expected {expected_event_id}, found {}",
                    event.event_id
                ),
            });
        }
        if event.prev_hash.as_deref() != expected_prev_hash {
            failures.push(ChainFailure {
                event_id: event.event_id,
                reason: "prev_hash chain break".to_string(),
            });
        }
        if event.is_redacted_projection() {
            failures.push(ChainFailure {
                event_id: event.event_id,
                reason: format!(
                    "redacted projection cannot authenticate canonical source hash '{}'",
                    event.source_record_hash()
                ),
            });
            expected_prev_hash = Some(event.source_record_hash());
            continue;
        }
        let recomputed = compute_record_hash(event);
        if recomputed != event.record_hash {
            failures.push(ChainFailure {
                event_id: event.event_id,
                reason: format!(
                    "record_hash mismatch: stored '{stored}' vs computed '{recomputed}'",
                    stored = event.record_hash
                ),
            });
            expected_prev_hash = Some(event.record_hash.as_str());
            continue;
        }
        expected_prev_hash = Some(event.record_hash.as_str());
        let Some(signed_by) = event.signed_by.as_ref() else {
            continue;
        };
        let is_receipt = matches!(event.kind, SessionEventKind::Receipt);
        let verifier = if is_receipt {
            receipt_verifier
        } else {
            event_verifier
        };
        let Some(verifier) = verifier else {
            signed += 1;
            continue;
        };
        let result = if is_receipt {
            let pre_receipt_root = chain_root_hash(&events[..index]);
            verify_receipt_root(signed_by, verifier, &pre_receipt_root)
        } else {
            verify_event(event, verifier)
        };
        match result {
            Ok(()) => signed += 1,
            Err(error) => failures.push(ChainFailure {
                event_id: event.event_id,
                reason: error.to_string(),
            }),
        }
    }
    (signed, failures)
}

/// Verify event bytes, sequence/linkage, signatures, and the persisted session
/// counters/root as one contract shared by every backend.
pub fn verify_session_chain(
    meta: &SessionMeta,
    events: &[StoredEvent],
    event_verifier: Option<&VerifyingKey>,
    receipt_verifier: Option<&VerifyingKey>,
) -> VerifyReport {
    let chain_root = chain_root_hash(events);
    let (signed_event_count, mut failures) =
        verify_event_chain_detailed(events, event_verifier, receipt_verifier);
    let tail_id = events.last().map(|event| event.event_id).unwrap_or(0);
    if meta.event_count != events.len() {
        failures.push(ChainFailure {
            event_id: tail_id,
            reason: format!(
                "session event_count mismatch: stored {}, found {}",
                meta.event_count,
                events.len()
            ),
        });
    }
    if meta.last_event_id != events.last().map(|event| event.event_id) {
        failures.push(ChainFailure {
            event_id: tail_id,
            reason: "session last_event_id mismatch".to_string(),
        });
    }
    let expected_root = if events.is_empty() {
        None
    } else {
        Some(chain_root.as_str())
    };
    if meta.chain_root_hash.as_deref() != expected_root {
        failures.push(ChainFailure {
            event_id: tail_id,
            reason: "session chain_root_hash mismatch".to_string(),
        });
    }
    VerifyReport {
        session_id: meta.id.clone(),
        chain_root_hash: chain_root,
        event_count: events.len(),
        signed_event_count,
        failures: failures
            .into_iter()
            .map(|failure| VerifyFailure {
                event_id: failure.event_id,
                reason: failure.reason,
            })
            .collect(),
    }
}

/// Initial chain root before any events have been appended. The prefix
/// is versioned so a future schema change doesn't silently re-validate
/// against an old chain.
pub fn chain_root_init() -> String {
    let mut hasher = Sha256::new();
    hasher.update(b"harn.session.chain.v2");
    finalize_sha256(hasher)
}

/// Fold a single event's `record_hash` into the running chain root.
/// Composing folds in sequence reproduces [`chain_root_hash`] without
/// re-hashing the entire history on every append.
pub fn chain_root_fold(prev_root: &str, record_hash: &str) -> String {
    let mut hasher = Sha256::new();
    hasher.update(prev_root.as_bytes());
    hasher.update(b"\n");
    hasher.update(record_hash.as_bytes());
    finalize_sha256(hasher)
}

/// Build the canonical source-chain root for stored events or redacted
/// retrieval projections. Projection markers retain the canonical source
/// hash, so snapshot metadata and valid receipt signatures do not become
/// false corruption reports merely because presentation bytes were scrubbed.
/// The hot append path uses [`chain_root_fold`] directly.
pub fn chain_root_hash(events: &[StoredEvent]) -> String {
    events.iter().fold(chain_root_init(), |root, event| {
        chain_root_fold(&root, event.source_record_hash())
    })
}

/// Re-anchor a chain of events on a new owning session id. The
/// `session_id` field is rewritten on each event and `prev_hash` +
/// `record_hash` are recomputed sequentially, so the resulting chain
/// is bytewise-verifiable as a standalone session. Used by
/// [`crate::SessionStore::fork`] to give the child session
/// a chain that `verify` can attest without the parent.
pub fn re_anchor_events(events: &[StoredEvent], new_session_id: &str) -> Vec<StoredEvent> {
    let mut rewritten = Vec::with_capacity(events.len());
    let mut prev_hash: Option<String> = None;
    for event in events {
        let mut copied = event.clone();
        copied.session_id = new_session_id.to_string();
        copied.prev_hash = prev_hash.clone();
        copied.record_hash = compute_record_hash(&copied);
        // Per-event signatures are detached over the canonical bytes;
        // since both session_id and prev_hash changed, the parent's
        // signature no longer attests this event. Drop it — the
        // session-close path will mint a fresh receipt covering the
        // re-anchored chain.
        copied.signed_by = None;
        prev_hash = Some(copied.record_hash.clone());
        rewritten.push(copied);
    }
    rewritten
}

/// Helper for receipt payloads built outside the signer.
pub fn canonical_receipt_payload(
    session_id: &str,
    last_event_id: super::event::EventId,
    chain_root: &str,
) -> serde_json::Value {
    serde_json::json!({
        "schema": "harn.session.receipt.v1",
        "session_id": session_id,
        "last_event_id": last_event_id,
        "chain_root": chain_root,
    })
}

/// Canonicalise a [`serde_json::Value`] for use in tests that compare
/// hashing inputs. Public surface kept tight; mostly used by the
/// integration tests that round-trip events.
pub fn canonical_value_hash(value: &serde_json::Value) -> String {
    let mut hasher = Sha256::new();
    hasher.update(canonical_json_bytes(value));
    finalize_sha256(hasher)
}