From 3152af43504bcfd6285d41f6b6ff67631d01fd0e Mon Sep 17 00:00:00 2001 From: tv Date: Sun, 15 Nov 2026 16:31:37 +0000 Subject: signed values: one reading of an envelope, everywhere Three parts read an envelope three ways: the store's order took any string with the prefix whose meta held a `seq`, its reads any whose value decoded, the checks only one with four parts and a meta of exactly its fields. A string that the first two took and the checks refused was read first, read as the registry's or the policy's value, and treated as nothing by the check of the next write there. `document::signed` reads an envelope the one way: four parts, the value JSON, the meta exactly its six fields (one more, or one twice, and it isn't one), no newline in either, the signature decoding. The store's order, its plain reads, the checks, the API and the users API all go by it. What isn't one in every respect is a plain string, wherever it is read. --- src/access/envelope.rs | 117 +++++-------------------------------------------- 1 file changed, 12 insertions(+), 105 deletions(-) (limited to 'src/access/envelope.rs') diff --git a/src/access/envelope.rs b/src/access/envelope.rs index c12fd35..f697809 100644 --- a/src/access/envelope.rs +++ b/src/access/envelope.rs @@ -9,6 +9,7 @@ //! user's, with one of its keys (`registry::User::keys`), over the value's bytes, a //! newline, and the meta's bytes (neither holds a newline), as //! `ssh-keygen -Y sign -n continuum-value` makes it (`access::sshsig`), its SSHSIG blob. +//! What is one, and what isn't, every part of continuum reads alike (`document::signed`). //! //! It travels with the value, through replays and snapshots alike, and anyone can check //! it (`check`): a node before it writes or applies one, a client reading it. An @@ -19,17 +20,15 @@ //! node never writes there but as its user signed it. use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD as B64URL}; -use serde::{Deserialize, Serialize}; use serde_json::Value as JsonValue; use sha2::{Digest, Sha256}; use super::{Right, agree, registry, sshsig}; -use crate::document::{ReadDoc, meta, store}; +pub(crate) use crate::document::signed::Payload; +use crate::document::{ReadDoc, meta, signed, store}; /// What a signature over a payload is for (`sshsig`). pub(crate) const NAMESPACE: &str = "continuum-value"; -/// What every envelope starts with. -const PREFIX: &str = "cv2"; /// Where the swarm lists the user-signed paths: a list of paths, each a list of /// segments. @@ -102,54 +101,9 @@ pub(crate) fn reaches_signed(signed: &[Vec], path: &[String]) -> bool { signed.iter().any(|pattern| agree(pattern, path)) } -/// What a signed value says: its value, and its meta. -#[derive(Debug, Clone, PartialEq)] -pub(crate) struct Payload { - pub(crate) value: JsonValue, - /// The swarm it is for: one user, with the same name and key in two swarms, signs - /// for one only. - pub(crate) swarm: String, - pub(crate) path: Vec, - pub(crate) user: String, - pub(crate) at: u64, - /// One more than that of the envelope it replaces; 0 where it replaces none. - pub(crate) seq: u64, - pub(crate) prev: String, -} - -/// An envelope's meta, as it is written. -#[derive(Serialize, Deserialize)] -struct Meta { - swarm: String, - path: Vec, - user: String, - at: u64, - seq: u64, - prev: String, -} - -impl Payload { - /// The bytes its user signs (`seal`): the value, a newline, the meta. - #[cfg(test)] - pub(crate) fn signed(&self) -> Vec { - let meta = Meta { - swarm: self.swarm.clone(), - path: self.path.clone(), - user: self.user.clone(), - at: self.at, - seq: self.seq, - prev: self.prev.clone(), - }; - let mut bytes = serde_json::to_vec(&self.value).expect("a value is JSON"); - bytes.push(b'\n'); - bytes.extend(serde_json::to_vec(&meta).expect("a meta is JSON")); - bytes - } -} - /// The `seq` of `envelope`, unchecked; None if it isn't one. pub(crate) fn seq(envelope: &str) -> Option { - read(envelope).ok().map(|payload| payload.seq) + signed::read(envelope).map(|payload| payload.seq) } /// An envelope checked (`check`): what it says, and the key that signed it. @@ -178,68 +132,17 @@ pub(crate) fn seal(signed: &[u8], armored: &str) -> Result { return Err("its value's line, then its meta's".to_owned()); }; Ok(format!( - "{PREFIX}.{}.{}.{}", + "{}{}.{}.{}", + signed::START, B64URL.encode(value), B64URL.encode(meta), B64URL.encode(blob) )) } -/// What `envelope` holds, unchecked: the bytes signed, what it says, the signature's -/// blob. -fn open(envelope: &str) -> Result<(Vec, Payload, Vec), String> { - let mut parts = envelope.split('.'); - let (Some(PREFIX), Some(value), Some(meta), Some(signature), None) = ( - parts.next(), - parts.next(), - parts.next(), - parts.next(), - parts.next(), - ) else { - return Err("not a signed value (cv2...)".to_owned()); - }; - let decoded = |part: &str, what: &str| { - let bytes = B64URL - .decode(part) - .map_err(|error| format!("its {what} doesn't decode: {error}"))?; - // One line each: the bytes signed part at the one newline between them. - if bytes.contains(&b'\n') { - return Err(format!("its {what} holds a newline")); - } - Ok(bytes) - }; - let mut signed = decoded(value, "value")?; - let value: JsonValue = serde_json::from_slice(&signed) - .map_err(|error| format!("its value isn't JSON: {error}"))?; - let meta = decoded(meta, "meta")?; - let Meta { - swarm, - path, - user, - at, - seq, - prev, - } = serde_json::from_slice(&meta).map_err(|error| format!("its meta isn't one: {error}"))?; - signed.push(b'\n'); - signed.extend(meta); - let signature = B64URL - .decode(signature) - .map_err(|error| format!("its signature doesn't decode: {error}"))?; - let payload = Payload { - value, - swarm, - path, - user, - at, - seq, - prev, - }; - Ok((signed, payload, signature)) -} - /// What `envelope` says, unchecked: for showing a value (`check` before taking it). pub(crate) fn read(envelope: &str) -> Result { - open(envelope).map(|(_, payload, _)| payload) + signed::open(envelope).map(|opened| opened.payload) } /// Checks `envelope` as the value at `path`, replacing `previous` (the envelope there @@ -252,7 +155,11 @@ pub(crate) fn check( path: &[String], previous: Option<&str>, ) -> Result { - let (bytes, payload, signature) = open(envelope)?; + let signed::Opened { + signed: bytes, + payload, + signature, + } = signed::open(envelope)?; let swarm = store::read_path( doc, &crate::document::meta::path(&[crate::document::meta::SWARM]), -- cgit v1.3.1