aboutsummaryrefslogtreecommitdiffstats
path: root/src/access/envelope.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/access/envelope.rs')
-rw-r--r--src/access/envelope.rs117
1 files changed, 12 insertions, 105 deletions
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<String>], 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<String>,
- 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<String>,
- 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<u8> {
- 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<u64> {
- 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<String, String> {
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<u8>, Payload, Vec<u8>), 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.<value>.<meta>.<signature>)".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<Payload, String> {
- 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<Signed, String> {
- 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]),