aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/access/envelope.rs117
-rw-r--r--src/access/sealed.rs6
-rw-r--r--src/api/store.rs2
-rw-r--r--src/api/users.rs4
-rw-r--r--src/document/mod.rs1
-rw-r--r--src/document/signed.rs171
-rw-r--r--src/document/store.rs42
-rw-r--r--src/simtest/policy.rs4
8 files changed, 208 insertions, 139 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]),
diff --git a/src/access/sealed.rs b/src/access/sealed.rs
index 545fe76..2b8085a 100644
--- a/src/access/sealed.rs
+++ b/src/access/sealed.rs
@@ -148,8 +148,8 @@ pub(crate) fn check(
let JsonValue::String(text) = value else {
return Err("a sealed value is a string (cs1.<age file>)".to_owned());
};
- let sealed = if text.starts_with(store::ENVELOPE) {
- match envelope::read(text)?.value {
+ let sealed = if let Ok(payload) = envelope::read(text) {
+ match payload.value {
JsonValue::Null => return Ok(()),
JsonValue::String(sealed) => sealed,
_ => return Err("signed, but not sealed".to_owned()),
@@ -343,7 +343,7 @@ mod tests {
let meta = json!({"swarm": "s", "path": ["notes"], "user": "alice", "at": 1, "seq": 0, "prev": ""});
format!(
"{}{}.{}.c2ln",
- store::ENVELOPE,
+ crate::document::signed::START,
B64URL.encode(value.to_string()),
B64URL.encode(meta.to_string())
)
diff --git a/src/api/store.rs b/src/api/store.rs
index a7f4686..f0b3094 100644
--- a/src/api/store.rs
+++ b/src/api/store.rs
@@ -1342,7 +1342,7 @@ mod tests {
json!({"swarm": "s", "path": ["x"], "user": "alice", "at": 1, "seq": 0, "prev": ""});
format!(
"{}{}.{}.c2ln",
- store::ENVELOPE,
+ crate::document::signed::START,
URL_SAFE_NO_PAD.encode(value.to_string()),
URL_SAFE_NO_PAD.encode(meta.to_string())
)
diff --git a/src/api/users.rs b/src/api/users.rs
index 66b6a8b..f27eff1 100644
--- a/src/api/users.rs
+++ b/src/api/users.rs
@@ -48,7 +48,7 @@ use tracing::info;
use super::ApiError;
use super::access::{Access, Before, registry_kept};
use super::auth::AuthenticatedUser;
-use crate::access::{Right, registry};
+use crate::access::{Right, envelope, registry};
use crate::document::store;
use crate::state::AppState;
@@ -153,7 +153,7 @@ pub(crate) async fn own_keys(
.flatten()
.ok_or_else(|| ApiError(StatusCode::NOT_FOUND, Some("no entry of yours".to_owned())))?;
let signed = match store::read_path(&replica.doc, &path) {
- Ok(Some(JsonValue::String(text))) if text.starts_with(store::ENVELOPE) => Some(text),
+ Ok(Some(JsonValue::String(text))) if envelope::read(&text).is_ok() => Some(text),
_ => None,
};
Ok(Json(json!({
diff --git a/src/document/mod.rs b/src/document/mod.rs
index a546d63..76349ad 100644
--- a/src/document/mod.rs
+++ b/src/document/mod.rs
@@ -57,6 +57,7 @@ pub(crate) mod lock;
pub(crate) mod meta;
pub(crate) mod replica;
pub(crate) mod signatures;
+pub(crate) mod signed;
pub(crate) mod store;
pub(crate) mod sync;
#[cfg(test)]
diff --git a/src/document/signed.rs b/src/document/signed.rs
new file mode 100644
index 0000000..fc42f53
--- /dev/null
+++ b/src/document/signed.rs
@@ -0,0 +1,171 @@
+//! What a signed value says, read the one way every part of continuum reads it: a
+//! string `cv2.<value>.<meta>.<signature>` (each base64url, unpadded), the value JSON,
+//! the meta JSON with exactly its fields, neither holding a newline, the signature
+//! decoding. A string that isn't one in every respect is a plain string, wherever it
+//! is read: the store's order and reads (`store`), the checks (`access::envelope`), the
+//! API.
+
+use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD as B64URL};
+use serde::{Deserialize, Serialize};
+use serde_json::Value as JsonValue;
+
+/// What every envelope starts with.
+pub(crate) const START: &str = "cv2.";
+
+/// 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: these fields, no other.
+#[derive(Serialize, Deserialize)]
+#[serde(deny_unknown_fields)]
+struct Meta {
+ swarm: String,
+ path: Vec<String>,
+ user: String,
+ at: u64,
+ seq: u64,
+ prev: String,
+}
+
+impl Payload {
+ /// The bytes its user signs: 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
+ }
+}
+
+/// An envelope opened, unchecked: the bytes signed, what it says, the signature's blob.
+pub(crate) struct Opened {
+ pub(crate) signed: Vec<u8>,
+ pub(crate) payload: Payload,
+ pub(crate) signature: Vec<u8>,
+}
+
+/// `text` opened as an envelope, unchecked; why not, if it isn't one.
+pub(crate) fn open(text: &str) -> Result<Opened, String> {
+ let not_one = || "not a signed value (cv2.<value>.<meta>.<signature>)".to_owned();
+ let mut parts = text.strip_prefix(START).ok_or_else(not_one)?.split('.');
+ let (Some(value), Some(meta), Some(signature), None) =
+ (parts.next(), parts.next(), parts.next(), parts.next())
+ else {
+ return Err(not_one());
+ };
+ 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(Opened {
+ signed,
+ payload,
+ signature,
+ })
+}
+
+/// What `text` says, if it is an envelope.
+pub(crate) fn read(text: &str) -> Option<Payload> {
+ open(text).ok().map(|opened| opened.payload)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use serde_json::json;
+
+ fn envelope(value: &str, meta: &str, signature: &str) -> String {
+ format!(
+ "{START}{}.{}.{}",
+ B64URL.encode(value),
+ B64URL.encode(meta),
+ B64URL.encode(signature)
+ )
+ }
+
+ const META: &str = r#"{"swarm":"s","path":["a"],"user":"alice","at":1,"seq":2,"prev":""}"#;
+
+ /// One reading: what isn't an envelope in every respect is none.
+ #[test]
+ fn what_an_envelope_is() {
+ let good = envelope("1", META, "sig");
+ let payload = read(&good).unwrap();
+ assert_eq!(payload.value, json!(1));
+ assert_eq!(payload.seq, 2);
+ for not_one in [
+ // A meta short of its fields, or with one more.
+ envelope("1", r#"{"seq":2}"#, "sig"),
+ envelope(
+ "1",
+ r#"{"swarm":"s","path":["a"],"user":"alice","at":1,"seq":2,"prev":"","more":1}"#,
+ "sig",
+ ),
+ // A field twice.
+ envelope(
+ "1",
+ r#"{"swarm":"s","path":["a"],"user":"alice","at":1,"seq":2,"seq":3,"prev":""}"#,
+ "sig",
+ ),
+ // A fifth part, a value that isn't JSON, a newline, another prefix.
+ format!("{good}.more"),
+ envelope("not json", META, "sig"),
+ envelope("1\n", META, "sig"),
+ good.replacen("cv2.", "cv1.", 1),
+ ] {
+ assert!(read(&not_one).is_none(), "{not_one}");
+ }
+ }
+}
diff --git a/src/document/store.rs b/src/document/store.rs
index aa79e47..06dcce4 100644
--- a/src/document/store.rs
+++ b/src/document/store.rs
@@ -210,13 +210,9 @@ enum Resolved {
Other(Value<'static>, ObjId),
}
-/// What every signed value starts with (`access::envelope`).
-pub(crate) const ENVELOPE: &str = "cv2.";
-
/// Where `value` stands among the values written at once at one place, if it is a
-/// signed value (`access::envelope`), unchecked: its `seq`, and its digest.
+/// signed value (`document::signed`), unchecked: its `seq`, and its digest.
fn signed_order(value: &Value<'_>) -> Option<(u64, [u8; 32])> {
- use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD};
use sha2::{Digest, Sha256};
let Value::Scalar(scalar) = value else {
return None;
@@ -224,12 +220,8 @@ fn signed_order(value: &Value<'_>) -> Option<(u64, [u8; 32])> {
let ScalarValue::Str(text) = scalar.as_ref() else {
return None;
};
- let meta = text.strip_prefix(ENVELOPE)?.split('.').nth(1)?;
- let meta: JsonValue = serde_json::from_slice(&URL_SAFE_NO_PAD.decode(meta).ok()?).ok()?;
- Some((
- meta.get("seq")?.as_u64()?,
- Sha256::digest(text.as_bytes()).into(),
- ))
+ let payload = super::signed::read(text)?;
+ Some((payload.seq, Sha256::digest(text.as_bytes()).into()))
}
/// All concurrent values of `key` across a merged set of parent maps, the one that
@@ -502,21 +494,14 @@ pub(crate) fn read_plain(
path: &[String],
) -> Result<Option<JsonValue>, StoreError> {
Ok(match read_path(doc, path)? {
- Some(JsonValue::String(text)) if text.starts_with(ENVELOPE) => {
- enveloped(&text).filter(|value| !value.is_null())
- }
+ Some(JsonValue::String(text)) => match super::signed::read(&text) {
+ Some(payload) => Some(payload.value).filter(|value| !value.is_null()),
+ None => Some(JsonValue::String(text)),
+ },
other => other,
})
}
-/// The value an envelope (`cv2.<value>.<meta>.<signature>`) holds; None if it isn't
-/// one.
-fn enveloped(text: &str) -> Option<JsonValue> {
- use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD};
- let value = text.split('.').nth(1)?;
- serde_json::from_slice(&URL_SAFE_NO_PAD.decode(value).ok()?).ok()
-}
-
/// Reads a path; the empty path is the whole store.
pub(crate) fn read_path(
doc: &impl ReadDoc,
@@ -1743,7 +1728,8 @@ mod tests {
let meta =
json!({"swarm": "s", "path": ["a"], "user": "alice", "at": 1, "seq": seq, "prev": ""});
format!(
- "{ENVELOPE}{}.{}.c2ln",
+ "{}{}.{}.c2ln",
+ crate::document::signed::START,
URL_SAFE_NO_PAD.encode(value.to_string()),
URL_SAFE_NO_PAD.encode(meta.to_string())
)
@@ -1788,7 +1774,7 @@ mod tests {
}
/// An envelope reads as the value it holds, a signed `null` as nothing, anything
- /// else as it is.
+ /// else (a string shaped like one but not one in every respect too) as it is.
#[test]
fn plain_reads_open_envelopes() {
let sealed = |value: JsonValue| envelope_of(&value, 0);
@@ -1796,7 +1782,9 @@ mod tests {
try_change(&mut doc, |doc| {
write_path(doc, &path("a"), &json!(sealed(json!({"mode": "fast"}))))?;
write_path(doc, &path("gone"), &json!(sealed(JsonValue::Null)))?;
- write_path(doc, &path("plain"), &json!("cv2-ish"))
+ write_path(doc, &path("plain"), &json!("cv2-ish"))?;
+ // Shaped like one, short of its meta's fields: a plain string.
+ write_path(doc, &path("short"), &json!("cv2.MQ.eyJzZXEiOjF9.c2ln"))
})
.unwrap();
assert_eq!(
@@ -1808,6 +1796,10 @@ mod tests {
read_plain(&doc, &path("plain")).unwrap(),
Some(json!("cv2-ish"))
);
+ assert_eq!(
+ read_plain(&doc, &path("short")).unwrap(),
+ Some(json!("cv2.MQ.eyJzZXEiOjF9.c2ln"))
+ );
assert_eq!(read_plain(&doc, &path("none")).unwrap(), None);
}
diff --git a/src/simtest/policy.rs b/src/simtest/policy.rs
index 4182b11..e5665cd 100644
--- a/src/simtest/policy.rs
+++ b/src/simtest/policy.rs
@@ -214,9 +214,7 @@ async fn write_signed(
use crate::document::typed::{TimestampFormat, Val};
let at = path(at);
let previous = match store::read_path(&node.doc.read().await.doc, &at) {
- Ok(Some(serde_json::Value::String(text))) if text.starts_with(store::ENVELOPE) => {
- Some(text)
- }
+ Ok(Some(serde_json::Value::String(text))) if envelope::read(&text).is_ok() => Some(text),
_ => None,
};
let swarm = store::read_path(&node.doc.read().await.doc, &path("_continuum/swarm"))