diff --git a/.github/workflows/release-gate.yml b/.github/workflows/release-gate.yml index 47374bbdc44..8fc118f93cd 100644 --- a/.github/workflows/release-gate.yml +++ b/.github/workflows/release-gate.yml @@ -246,6 +246,7 @@ jobs: - "rs/moq-auth/**" - "rs/moq-cli/**" - "rs/moq-congestion/**" + - "rs/moq-crypto/**" - "rs/moq-hls/**" - "rs/moq-ice/**" - "rs/moq-json/**" diff --git a/Cargo.lock b/Cargo.lock index 8f558297c9f..714a295afa2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4486,11 +4486,11 @@ dependencies = [ "axum", "base64 0.23.1", "bench-harness", - "data-encoding", "jsonwebtoken", "kio", "moq-auth 0.2.1", "moq-auth 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "moq-crypto", "moq-pattern 0.1.2", "moq-token", "pkcs1", @@ -4704,6 +4704,7 @@ dependencies = [ "kio", "moq-net", "rind", + "serde", "serde_json", "tempfile", "thiserror 2.0.21", diff --git a/Cargo.toml b/Cargo.toml index 589c40b2216..f6ffa1c4660 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -129,6 +129,8 @@ moq-archive = { version = "0.0.11", path = "rs/moq-archive" } moq-audio = { version = "0.1.10", path = "rs/moq-audio", default-features = false } moq-auth = { version = "0.2.1", path = "rs/moq-auth" } moq-congestion = { version = "0.1.0", path = "rs/moq-congestion" } +# default-features off: moq-auth takes only the endpoint key and its text codec. +moq-crypto = { version = "0.0.11", path = "rs/moq-crypto", default-features = false } moq-hls = { version = "0.5.11", path = "rs/moq-hls", default-features = false } moq-ice = { version = "0.1.0", path = "rs/moq-ice" } moq-json = { version = "0.5.8", path = "rs/moq-json" } diff --git a/drafts/moq-sign-00.json b/drafts/moq-sign-00.json index 1399107c087..9cd524c6fe0 100644 --- a/drafts/moq-sign-00.json +++ b/drafts/moq-sign-00.json @@ -28,6 +28,11 @@ "id": "ones", "public": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "endpoint": "999999999999999999999999999999999999999999999999999o" + }, + { + "id": "zeros", + "public": "0000000000000000000000000000000000000000000000000000000000000000", + "endpoint": "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy" } ], "frames": [ @@ -405,6 +410,26 @@ { "id": "empty", "text": "" + }, + { + "id": "ones-trailing-bits", + "text": "9999999999999999999999999999999999999999999999999999" + }, + { + "id": "padding", + "text": "eyamftwcf3sck8rs5m1meugf8odwqg78473t7p3st9wxob9csz8y====" + }, + { + "id": "leading-whitespace", + "text": " eyamftwcf3sck8rs5m1meugf8odwqg78473t7p3st9wxob9csz8y" + }, + { + "id": "trailing-newline", + "text": "eyamftwcf3sck8rs5m1meugf8odwqg78473t7p3st9wxob9csz8y\n" + }, + { + "id": "base64url", + "text": "QDCyxowubMUcltrktEzFPAdHG6fXcx63No_o-Afstc4" } ] } diff --git a/drafts/moq-sign-00.ts b/drafts/moq-sign-00.ts index 2dd55a04f56..aacc14f3476 100644 --- a/drafts/moq-sign-00.ts +++ b/drafts/moq-sign-00.ts @@ -12,6 +12,7 @@ import { readFileSync, writeFileSync } from "node:fs"; import { dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; +import * as ZBase32 from "../js/crypto/src/sign/zbase32.ts"; export const PROFILE = "moq-sign-00"; export const LABEL = utf8(PROFILE); @@ -41,9 +42,6 @@ export type Identity = { timestamp: bigint; }; -// z-base-32: most significant bit first, no padding, trailing bits must be zero. -const ZBASE32 = "ybndrfg8ejkmcpqxot1uwisza345h769"; - function utf8(text: string): Uint8Array { return new TextEncoder().encode(text); } @@ -56,23 +54,6 @@ function toHex(bytes: Uint8Array): string { return Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join(""); } -function zbase32(bytes: Uint8Array): string { - let out = ""; - let buffer = 0; - let bits = 0; - for (const byte of bytes) { - buffer = (buffer << 8) | byte; - bits += 8; - while (bits >= 5) { - bits -= 5; - out += ZBASE32[(buffer >> bits) & 31]; - } - buffer &= (1 << bits) - 1; - } - if (bits > 0) out += ZBASE32[(buffer << (5 - bits)) & 31]; - return out; -} - function concat(...parts: Uint8Array[]): Uint8Array { const out = new Uint8Array(parts.reduce((sum, part) => sum + part.byteLength, 0)); let offset = 0; @@ -111,29 +92,14 @@ export function message(identity: Identity, payload: Uint8Array): Uint8Array { - if (text.length !== 52) throw new ProfileError("key"); - const out = new Uint8Array(KEY_LEN); - let buffer = 0; - let bits = 0; - let offset = 0; - for (const char of text) { - const value = ZBASE32.indexOf(char); - if (value < 0) throw new ProfileError("key"); - buffer = (buffer << 5) | value; - bits += 5; - if (bits >= 8) { - bits -= 8; - out[offset++] = (buffer >> bits) & 0xff; - } - buffer &= (1 << bits) - 1; - } - if (buffer !== 0) throw new ProfileError("key"); - return out; + const key = ZBase32.decode(text, KEY_LEN); + if (!key) throw new ProfileError("key"); + return key; } async function privateKey(seed: Uint8Array): Promise { @@ -267,6 +233,7 @@ async function generate() { { id: "other", seed: OTHER_SEED, public: toHex(await publicKey(hex(OTHER_SEED))), endpoint: otherKey }, // Every bit set: the last character carries one key bit and four zero bits. { id: "ones", public: "ff".repeat(KEY_LEN), endpoint: endpointId(new Uint8Array(KEY_LEN).fill(0xff)) }, + { id: "zeros", public: "00".repeat(KEY_LEN), endpoint: endpointId(new Uint8Array(KEY_LEN)) }, ], frames: [ await frameVector("base", SEED, base, payload), @@ -315,6 +282,11 @@ async function generate() { { id: "trailing-bits", text: `${key.slice(0, 51)}b` }, { id: "hex", text: toHex(keyBytes) }, { id: "empty", text: "" }, + { id: "ones-trailing-bits", text: "9".repeat(52) }, + { id: "padding", text: `${key}====` }, + { id: "leading-whitespace", text: ` ${key}` }, + { id: "trailing-newline", text: `${key}\n` }, + { id: "base64url", text: Buffer.from(keyBytes).toString("base64url") }, ], }; } diff --git a/quest/m1/README.md b/quest/m1/README.md index 8b9593abed6..cd35fc8a9e4 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -174,7 +174,6 @@ per-frame CPU growth since #5926. - [Poll-first async](/quest/m1/poll-first/README.md) - library crates compile no coroutines by default: async wrappers are generated from `poll_*` and gated behind an `async` feature - [Authored tracks default to a 30 s max-age](/quest/m1/max-age-defaults.md) - raw and media tracks advertise 30 s unless their author chooses otherwise, with explicit unlimited in every binding - [HLS export lists a same-codec reconfigure's first group under the new init](/quest/m1/hls-reinit-same-codec.md) - the hand-over pick, whichever of the catalog and the records arrives first, after the catalog `hls` removal is planned -- [One Ed25519 public key type with one z-base-32 codec](/quest/m1/mesh-key-codec.md) - `moq_auth::mesh::EndpointId` is merged into `moq_crypto::sign::PublicKey` - [Ordered reads can skip owed holes, and snapshot latest() does](/quest/m1/ordered-owed-holes.md) - moq-net's `Ordered` gains a read that skips past an owed hole to the newest buffered group, while keeping the reader's bounds - [A Rust relay never replays from group 0 to a lite-03/04 subscriber](/quest/m1/relay-inflight-replay.md) - A Rust relay or publisher serving a lite-03 or lite-04 SUBSCRIBE from a track copy that is still resolving starts at the newest group once the copy resolves - [Stats name the route instance's epoch with a type](/quest/m1/stats-epoch-type.md) - `GroupFrame::epoch` gets a dedicated type and tests stop mislabelling the instance diff --git a/quest/m1/crates-trusted-publisher.md b/quest/m1/crates-trusted-publisher.md index 25f65999fea..34141873a4b 100644 --- a/quest/m1/crates-trusted-publisher.md +++ b/quest/m1/crates-trusted-publisher.md @@ -33,8 +33,9 @@ moq-archive, moq-audio, moq-auth, moq-boy, moq-cli, moq-ffi, moq-gst, moq-hls, moq-json, moq-loc, moq-msf, moq-mux, moq-native, moq-net, moq-nvenc, moq-pattern, moq-relay, moq-rtc, moq-rtmp, moq-sock, moq-srt, moq-stats, moq-tokio, moq-transcode, moq-uring, moq-v4l, moq-video, qmux. Not yet -published (6), so their first publish needs the token and they get a trusted -publisher after it: kio-macros, moq-c, moq-congestion, moq-ice, moq-quic, -moq-rtsp. `CARGO_REGISTRY_TOKEN` is revoked only after all of them have one. +published (7), so their first publish needs the token and they get a trusted +publisher after it: kio-macros, moq-c, moq-congestion, moq-crypto, moq-ice, +moq-quic, moq-rtsp. moq-crypto became publishable in +[moq-dev/moq#7733](https://github.com/moq-dev/moq/pull/7733). `CARGO_REGISTRY_TOKEN` is revoked only after all of them have one. Settings pages need a crates.io login, so the check is a `release-rs.yml` run publishing through OIDC. diff --git a/quest/m1/mesh-key-codec.md b/quest/m1/mesh-key-codec.md deleted file mode 100644 index 0f9712db4c4..00000000000 --- a/quest/m1/mesh-key-codec.md +++ /dev/null @@ -1,47 +0,0 @@ -# [S] One Ed25519 public key type with one z-base-32 codec - -## Goal - -`moq_auth::mesh::EndpointId` is merged into `moq_crypto::sign::PublicKey`: one Rust -type for a mesh endpoint's Ed25519 public key, with one z-base-32 text codec -(52 lowercase characters, zero trailing bits, no other text form) that -moq-auth and moq-crypto share. JS has one codec too, used by `@moq/crypto` and -by `@moq/auth` if it grows one. The text form, vectors, and refusals stay as -both PRs specified. - -## Plan - -Decided 2026-10-31 (user, /quest-plan of the follow-ups from the 2026-10-31 -/quest-complete run), from -[moq-dev/moq#7301](https://github.com/moq-dev/moq/pull/7301) and -[moq-dev/moq#7092](https://github.com/moq-dev/moq/pull/7092). - -Milestone: ✅ m1 (moved from m2). Not chosen: m2. There is no m1 mesh epic, -so this sits at the m1 top level; the [mesh epic](/quest/m2/mesh/README.md) -is related. - -- [moq-dev/moq#7301](https://github.com/moq-dev/moq/pull/7301) (merged) added `moq_auth::mesh::EndpointId` (an Ed25519 key with z-base-32 - text, via `data-encoding` with a custom `Specification`). [moq-dev/moq#7092](https://github.com/moq-dev/moq/pull/7092) (merged) added - `rs/moq-sign` with `SecretKey`/`PublicKey` using the same specification, - and a separate JS codec in `js/sign/src/zbase32.ts`. Both chose z-base-32 - so they would share one format; whichever landed second was to dedupe. -- Merge `EndpointId` into `moq_crypto::sign::PublicKey` (moq-auth depends on - moq-crypto for it, or the type moves to a small shared crate if the - dependency direction is wrong; say which in the PR). Keep the shared - vectors in `drafts/moq-sign-00.json` as the single source of test vectors. -- Both copies are still on `main` at be39188000: - `rs/moq-auth/src/mesh/mod.rs:52` (the specification) and `:65` - (`EndpointId`), beside what is now `rs/moq-crypto/src/sign/key.rs`. -- [moq-dev/moq#7702](https://github.com/moq-dev/moq/pull/7702) merged - `moq-sign`/`@moq/sign` into `moq-crypto`/`@moq/crypto`; the JS codec is - `js/crypto/src/sign/zbase32.ts`. -- Note, not in scope: persist a browser JS `SecretKey` (a nonextractable - `CryptoKey`) in IndexedDB only once a browser mesh node needs a stable - endpoint ID. - -Public API: breaking for moq-auth (`mesh::EndpointId` becomes -`moq_crypto::sign::PublicKey` or a re-export). Wire: none. - -## Related - -- [Mesh](/quest/m2/mesh/README.md) - the epic both types serve diff --git a/rs/justfile b/rs/justfile index 8fafe882020..57ec6d9d464 100644 --- a/rs/justfile +++ b/rs/justfile @@ -236,6 +236,7 @@ features: just rs net-features --all just rs coroutines just rs quic-features --all + just rs crypto-key just rs tokio-features --all just rs media-features --all bash sh/local.sh cargo check --locked --workspace {{ no_fuzz }} --all-targets --no-default-features @@ -265,6 +266,19 @@ coroutines: RUSTDOCFLAGS="-D warnings" bash sh/local.sh cargo doc --locked -p kio --no-deps --no-default-features --features macros,runtime,manual,serde bash sh/rs/coroutines.sh moq-net --allow until --allow run_front --allow serve_front -- --no-default-features +# Prove moq-crypto's key-only build, which moq-auth depends on, leaves out the transport and aws-lc-rs, then lint and test it alone; a workspace build unifies the default features back in. +crypto-key: + #!/usr/bin/env bash + set -euo pipefail + graph=$(cargo tree --locked -p moq-crypto --no-default-features --features serde -e normal --prefix none) + if grep -E '^(aws-lc-rs|moq-net) ' <<<"$graph"; then + echo "moq-crypto without sign or e2ee must not depend on moq-net or aws-lc-rs" >&2 + exit 1 + fi + bash sh/local.sh cargo clippy --locked -p moq-crypto --no-default-features --all-targets -- -D warnings + bash sh/local.sh cargo clippy --locked -p moq-crypto --no-default-features --features serde --all-targets -- -D warnings + bash sh/rs/nextest.sh cargo nextest run --locked -p moq-crypto --no-default-features --features serde + # Prove moq-quic leaves kio's dev-only clock runtime to its consumers, and lints without its default features; a workspace build unifies both back in. [positional-arguments] quic-features *args: diff --git a/rs/moq-auth/Cargo.toml b/rs/moq-auth/Cargo.toml index afd7ff63b80..10feaf13e35 100644 --- a/rs/moq-auth/Cargo.toml +++ b/rs/moq-auth/Cargo.toml @@ -19,10 +19,10 @@ serve = ["dep:axum", "dep:tokio", "dep:tracing", "tokio/net"] [dependencies] axum = { workspace = true, optional = true } base64 = { workspace = true } -data-encoding = { workspace = true } jsonwebtoken = { workspace = true } # `async` for `Shared::wait`, until moq-auth grows an `async` feature of its own. kio = { workspace = true, features = ["async", "serde"] } +moq-crypto = { workspace = true, features = ["serde"] } moq-pattern = { workspace = true, features = ["serde"] } pkcs1 = { workspace = true, features = ["std"] } pkcs8 = { workspace = true } diff --git a/rs/moq-auth/MIGRATE.md b/rs/moq-auth/MIGRATE.md index a8589d838c0..ab52472ff57 100644 --- a/rs/moq-auth/MIGRATE.md +++ b/rs/moq-auth/MIGRATE.md @@ -4,6 +4,19 @@ How to update your code for each breaking change, newest first. ## Unreleased +### Mesh endpoints use the signing public key + +Replace `moq_auth::mesh::EndpointId` with `moq_crypto::sign::PublicKey` and +`mesh::InvalidEndpoint` with `moq_crypto::sign::InvalidKey`. +Auth depends on `moq-crypto` with default features off, so it adds only the key and its text codec, not `moq-net` or aws-lc-rs. +`Proof`, `Member`, `Device`, `Signer::endpoint`, and `Coordinator::revoke_device` +use that type directly; byte access and canonical z-base-32 text are unchanged. +Build the publish pattern with `Pattern::subtree(&format!("mesh/{endpoint}"))` +instead of `endpoint.publish()`. +For raw proof verification, use `Proof::verify`; callers verifying other messages +can pass `endpoint.as_bytes()` to their Ed25519 verifier. +Enrollment JSON, stored device IDs, tokens, and signed content keep their formats. + ### Crypto runs on rind (#7570) Signing, verifying, key generation, and the HTTP clients' TLS run on [rind](https://github.com/moq-dev/rind), a pure-Rust library, instead of aws-lc-rs, `p256`, and `p384`. diff --git a/rs/moq-auth/src/mesh/coordinator.rs b/rs/moq-auth/src/mesh/coordinator.rs index 69eb7d2c4b9..cee6b7e5206 100644 --- a/rs/moq-auth/src/mesh/coordinator.rs +++ b/rs/moq-auth/src/mesh/coordinator.rs @@ -21,10 +21,11 @@ use axum::http::StatusCode; use axum::response::{IntoResponse, Response}; use axum::routing::{delete, get, post}; use axum::{Json, Router}; +use moq_crypto::sign::PublicKey; use serde::{Deserialize, Serialize}; use serde_with::serde_as; -use super::{CHALLENGE_LEN, Challenge, EndpointId, EnrollRequest, Enrollment, ErrorCode, ErrorReply, Member, Proof}; +use super::{CHALLENGE_LEN, Challenge, EnrollRequest, Enrollment, ErrorCode, ErrorReply, Member, Proof}; use crate::{Claims, Key, Pattern}; /// How long a challenge stays valid. @@ -103,7 +104,7 @@ struct StoredKey { #[non_exhaustive] pub struct Device { /// Its endpoint ID. - pub endpoint: EndpointId, + pub endpoint: PublicKey, /// Its unique, DNS-safe name. pub name: String, /// The tags of the enrollment key it joined with. @@ -176,7 +177,7 @@ struct Inner { store: Store, /// The sequence of the last challenge each endpoint was granted with. Only an /// enrolled device gets an entry, so it grows with the devices and nothing else. - used: HashMap, + used: HashMap, } /// The mesh coordinator: issues challenges, enrolls devices, and refreshes their tokens. @@ -208,7 +209,7 @@ impl Coordinator { // than after an enrollment key is spent. config .key - .sign(&claims(EndpointId::from_bytes([0; 32]), kio::clock::wall(), None))?; + .sign(&claims(PublicKey::from_bytes([0; 32]), kio::clock::wall(), None))?; Ok(Self { config: Arc::new(config), secret: Arc::new(rind::hmac::Key::new(&rind::hmac::HMAC_SHA256, &random(32))), @@ -360,7 +361,7 @@ impl Coordinator { /// Revoke a device: its next refresh fails, and it cannot enroll again. /// Returns whether the device exists. - pub fn revoke_device(&self, endpoint: EndpointId) -> crate::Result { + pub fn revoke_device(&self, endpoint: PublicKey) -> crate::Result { let mut inner = self.inner.lock().unwrap(); let found = self.commit(&mut inner, |store| { let device = store.devices.iter_mut().find(|device| device.endpoint == endpoint); @@ -446,7 +447,7 @@ impl Coordinator { } /// Record `endpoint` as enrolled, or update its name and tags if it already is. - fn admit(&self, store: &mut Store, endpoint: EndpointId, name: Option<&str>, tags: Vec) -> Reply<()> { + fn admit(&self, store: &mut Store, endpoint: PublicKey, name: Option<&str>, tags: Vec) -> Reply<()> { refuse_revoked(store, endpoint)?; let devices = &mut store.devices; let current = devices.iter().position(|device| device.endpoint == endpoint); @@ -471,7 +472,7 @@ impl Coordinator { } /// Sign a token for an enrolled `endpoint` and list the mesh. - fn issue(&self, store: &Store, endpoint: EndpointId) -> Reply { + fn issue(&self, store: &Store, endpoint: PublicKey) -> Reply { let internal = |err: crate::Error| ErrorReply::new(ErrorCode::ServerError, err.to_string()); let now = kio::clock::wall(); // `exp` is whole seconds on the wire; truncate so the reply matches the token. @@ -524,7 +525,7 @@ impl Coordinator { } } -fn refuse_revoked(store: &Store, endpoint: EndpointId) -> Reply<()> { +fn refuse_revoked(store: &Store, endpoint: PublicKey) -> Reply<()> { if store .devices .iter() @@ -536,9 +537,9 @@ fn refuse_revoked(store: &Store, endpoint: EndpointId) -> Reply<()> { } /// The grant every mesh token carries: publish under its own ID, subscribe to the mesh. -fn claims(endpoint: EndpointId, issued: kio::clock::Wall, expires: Option) -> Claims { +fn claims(endpoint: PublicKey, issued: kio::clock::Wall, expires: Option) -> Claims { Claims::default() - .with_publish([endpoint.publish()]) + .with_publish([Pattern::subtree(&format!("mesh/{endpoint}")).expect("a z-base-32 segment is a valid path")]) .with_subscribe([Pattern::subtree("mesh").expect("a valid path")]) .with_issued(issued) .with_expires(expires) @@ -562,7 +563,7 @@ fn sanitize(name: &str) -> String { } /// `name`, or `name-2`, `name-3`, ... when another endpoint holds it. -fn unique_name(devices: &[Device], endpoint: EndpointId, name: &str) -> String { +fn unique_name(devices: &[Device], endpoint: PublicKey, name: &str) -> String { let taken = |candidate: &str| { devices .iter() @@ -699,21 +700,21 @@ mod tests { #[test] fn names_stay_unique() { let device = |byte, name: &str| Device { - endpoint: EndpointId::from_bytes([byte; 32]), + endpoint: PublicKey::from_bytes([byte; 32]), name: name.into(), tags: Vec::new(), enrolled: kio::clock::wall(), revoked: false, }; let devices = vec![device(1, "robot"), device(2, "robot-2")]; - assert_eq!(unique_name(&devices, EndpointId::from_bytes([1; 32]), "robot"), "robot"); + assert_eq!(unique_name(&devices, PublicKey::from_bytes([1; 32]), "robot"), "robot"); assert_eq!( - unique_name(&devices, EndpointId::from_bytes([3; 32]), "robot"), + unique_name(&devices, PublicKey::from_bytes([3; 32]), "robot"), "robot-3" ); let long = "a".repeat(MAX_NAME); let devices = vec![device(1, &long)]; - let name = unique_name(&devices, EndpointId::from_bytes([3; 32]), &long); + let name = unique_name(&devices, PublicKey::from_bytes([3; 32]), &long); assert_eq!(name.len(), MAX_NAME); assert!(name.ends_with("-2")); } diff --git a/rs/moq-auth/src/mesh/mod.rs b/rs/moq-auth/src/mesh/mod.rs index 93c05f36813..6c155b8cd24 100644 --- a/rs/moq-auth/src/mesh/mod.rs +++ b/rs/moq-auth/src/mesh/mod.rs @@ -20,8 +20,8 @@ //! Every refusal is a 4xx with an [`ErrorReply`]. use std::fmt; -use std::str::FromStr; +use moq_crypto::sign::PublicKey; use serde::{Deserialize, Serialize}; use serde_with::serde_as; @@ -44,96 +44,8 @@ pub const PROOF_CONTEXT: &[u8] = b"moq-mesh-enroll-00"; /// The length of a challenge in bytes. pub const CHALLENGE_LEN: usize = 32; -const KEY_LEN: usize = 32; const SIGNATURE_LEN: usize = 64; -/// z-base-32: most significant bit first, no padding, trailing bits must be zero. -static ZBASE32: std::sync::LazyLock = std::sync::LazyLock::new(|| { - let mut spec = data_encoding::Specification::new(); - spec.symbols.push_str("ybndrfg8ejkmcpqxot1uwisza345h769"); - spec.encoding().expect("a valid z-base-32 specification") -}); - -/// An endpoint's Ed25519 public key, which is also its endpoint ID. -/// -/// Its text form is the 52-character [z-base-32] of the key, the form pkarr and iroh's DNS -/// discovery use, which is also a valid DNS label. Parsing accepts only that form (lowercase, -/// zero trailing bits), so one key names exactly one `mesh/` path. -/// -/// [z-base-32]: https://philzimmermann.com/docs/human-oriented-base-32-encoding.txt -#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] -pub struct EndpointId([u8; KEY_LEN]); - -impl EndpointId { - /// The ID of the raw 32-byte Ed25519 public key. - pub fn from_bytes(bytes: [u8; KEY_LEN]) -> Self { - Self(bytes) - } - - /// The raw 32-byte Ed25519 public key. - pub fn as_bytes(&self) -> &[u8; KEY_LEN] { - &self.0 - } - - /// Whether `signature` is this key's Ed25519 signature over `message`. - pub fn verify(&self, message: &[u8], signature: &[u8]) -> bool { - use rind::signature::{PublicKey, verify}; - PublicKey::new(&verify::ED25519, &self.0).is_ok_and(|key| key.verify(message, signature).is_ok()) - } - - /// The broadcasts this endpoint may publish: `mesh//**`. - pub fn publish(&self) -> crate::Pattern { - crate::Pattern::subtree(&format!("mesh/{self}")).expect("a z-base-32 segment is a valid path") - } -} - -/// The text is not the 52-character z-base-32 of a 32-byte key. -#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)] -#[error("invalid endpoint ID: expected 52 z-base-32 characters")] -#[non_exhaustive] -pub struct InvalidEndpoint; - -impl FromStr for EndpointId { - type Err = InvalidEndpoint; - - fn from_str(text: &str) -> Result { - // The alphabet is lowercase-only and nonzero trailing bits are refused, so a key has one text form. - let mut out = [0u8; KEY_LEN]; - match ZBASE32.decode_len(text.len()) { - Ok(KEY_LEN) => ZBASE32 - .decode_mut(text.as_bytes(), &mut out) - .map_err(|_| InvalidEndpoint)?, - _ => return Err(InvalidEndpoint), - }; - Ok(Self(out)) - } -} - -impl fmt::Display for EndpointId { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str(&ZBASE32.encode(&self.0)) - } -} - -impl fmt::Debug for EndpointId { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_tuple("EndpointId").field(&format_args!("{self}")).finish() - } -} - -impl Serialize for EndpointId { - fn serialize(&self, serializer: S) -> Result { - serializer.collect_str(self) - } -} - -impl<'de> Deserialize<'de> for EndpointId { - fn deserialize>(deserializer: D) -> Result { - let text = >::deserialize(deserializer)?; - text.parse().map_err(serde::de::Error::custom) - } -} - /// The message a [`Proof`] signs: [`PROOF_CONTEXT`], a zero byte, the coordinator's base /// URL as the device addressed it, a zero byte, then the raw challenge. /// @@ -173,7 +85,7 @@ pub struct Challenge { #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct Proof { /// The endpoint ID, which is the key that signed. - pub endpoint: EndpointId, + pub endpoint: PublicKey, /// The challenge, as issued. #[serde_as(as = "serde_with::base64::Base64")] pub challenge: Vec, @@ -196,10 +108,12 @@ impl Proof { /// Whether the signature verifies for the coordinator `base`. The challenge's /// freshness is the coordinator's to check. pub fn verify(&self, base: &url::Url) -> bool { + use rind::signature::{PublicKey, verify}; self.signature.len() == SIGNATURE_LEN - && self - .endpoint - .verify(&proof_message(base, &self.challenge), &self.signature) + && PublicKey::new(&verify::ED25519, self.endpoint.as_bytes()).is_ok_and(|key| { + key.verify(&proof_message(base, &self.challenge), &self.signature) + .is_ok() + }) } } @@ -209,14 +123,14 @@ impl Proof { /// its own key type. pub trait Signer: Send + Sync { /// The endpoint ID: this key's public half. - fn endpoint(&self) -> EndpointId; + fn endpoint(&self) -> PublicKey; /// The Ed25519 signature over `message`. fn sign(&self, message: &[u8]) -> [u8; SIGNATURE_LEN]; } impl Signer for rind::signature::Ed25519KeyPair { - fn endpoint(&self) -> EndpointId { - EndpointId( + fn endpoint(&self) -> PublicKey { + PublicKey::from_bytes( self.public_key() .as_ref() .try_into() @@ -267,7 +181,7 @@ pub struct Enrollment { #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct Member { /// Its endpoint ID. - pub endpoint: EndpointId, + pub endpoint: PublicKey, /// Its unique, DNS-safe name. pub name: String, /// The tags of the enrollment key it joined with. @@ -337,30 +251,22 @@ mod tests { } #[test] - fn endpoint_ids_have_one_text_form() { - let id = signer().endpoint(); - let text = id.to_string(); - assert_eq!(text.len(), 52); - assert_eq!(text.parse::().unwrap(), id); - assert!(text.to_uppercase().parse::().is_err()); - assert!(text[..51].parse::().is_err()); - assert!(format!("{text}y").parse::().is_err()); - assert!( - data_encoding::HEXLOWER - .encode(id.as_bytes()) - .parse::() - .is_err() - ); - assert_eq!(id.publish().as_str(), format!("mesh/{text}/**")); - } - - #[test] - fn endpoint_ids_are_z_base_32() { - assert_eq!(EndpointId::from_bytes([0; KEY_LEN]).to_string(), "y".repeat(52)); - let ones = EndpointId::from_bytes([0xff; KEY_LEN]); - assert_eq!(ones.to_string(), format!("{}o", "9".repeat(51))); - // The last character carries 4 padding bits, which must be zero. - assert!(format!("{}9", "9".repeat(51)).parse::().is_err()); + fn endpoint_vectors() { + let vectors: serde_json::Value = + serde_json::from_str(include_str!("../../../../drafts/moq-sign-00.json")).unwrap(); + for row in vectors["keys"].as_array().unwrap() { + let text = row["endpoint"].as_str().unwrap(); + let member: Member = serde_json::from_value(serde_json::json!({"endpoint": text, "name": "node"})).unwrap(); + assert_eq!(member.endpoint.to_string(), text, "{}", row["id"]); + assert_eq!(serde_json::to_value(&member).unwrap()["endpoint"], text); + } + for row in vectors["invalid_ids"].as_array().unwrap() { + assert!( + serde_json::from_value::(serde_json::json!({"endpoint": row["text"], "name": "node"})).is_err(), + "{}", + row["id"] + ); + } } #[test] diff --git a/rs/moq-crypto/Cargo.toml b/rs/moq-crypto/Cargo.toml index 9950ffd9c45..31d099d9384 100644 --- a/rs/moq-crypto/Cargo.toml +++ b/rs/moq-crypto/Cargo.toml @@ -1,5 +1,4 @@ [package] -publish = false name = "moq-crypto" description = "Content signing and end-to-end encryption for MoQ broadcasts." authors = ["Luke Curley "] @@ -13,17 +12,32 @@ rust-version.workspace = true keywords = ["quic", "webtransport", "encryption", "signature", "live"] categories = ["cryptography", "multimedia", "network-programming"] +[package.metadata.docs.rs] +all-features = true + +[features] +# Without `sign` or `e2ee`, the crate holds only `sign::PublicKey`, the endpoint ID, and its +# z-base-32 text: no `moq-net` and no crypto backend, for crates that only name endpoints. +default = ["sign", "e2ee"] +# Per-frame signing: `sign::SecretKey` and the signed broadcast wrappers. +sign = ["dep:aws-lc-rs", "dep:bytes", "dep:kio", "dep:moq-net", "dep:zeroize"] +# End-to-end encryption: the `e2ee` module. +e2ee = ["dep:base64", "dep:bytes", "dep:kio", "dep:moq-net", "dep:rind", "dep:uuid", "dep:zeroize"] +# `sign::PublicKey` serializes as its canonical z-base-32 text. +serde = ["dep:serde"] + [dependencies] -aws-lc-rs = { workspace = true } -base64 = { workspace = true } -bytes = { workspace = true } +aws-lc-rs = { workspace = true, optional = true } +base64 = { workspace = true, optional = true } +bytes = { workspace = true, optional = true } data-encoding = { workspace = true } -kio = { workspace = true } -moq-net = { workspace = true } -rind = { workspace = true } +kio = { workspace = true, optional = true } +moq-net = { workspace = true, optional = true } +rind = { workspace = true, optional = true } +serde = { workspace = true, optional = true } thiserror = { workspace = true } -uuid = { workspace = true } -zeroize = { workspace = true } +uuid = { workspace = true, optional = true } +zeroize = { workspace = true, optional = true } [dev-dependencies] criterion = { package = "bench-harness", path = "../bench-harness" } @@ -37,10 +51,16 @@ workspace-hack = { path = "../workspace-hack" } [[bench]] name = "protect" harness = false +required-features = ["e2ee"] [[bench]] name = "sign" harness = false +required-features = ["sign"] + +[[test]] +name = "transport" +required-features = ["e2ee"] [lints] workspace = true diff --git a/rs/moq-crypto/MIGRATE.md b/rs/moq-crypto/MIGRATE.md index 3cd3abc1cc7..78c1ee2d257 100644 --- a/rs/moq-crypto/MIGRATE.md +++ b/rs/moq-crypto/MIGRATE.md @@ -4,6 +4,12 @@ How to update your code for each breaking change, newest first. ## Unreleased +### Features gate signing and encryption + +The `sign` and `e2ee` modules sit behind default features of the same names. +A dependency with default features off keeps only `sign::PublicKey` and `sign::InvalidKey`; enable `e2ee` or `sign` for the rest. +`sign::PublicKey` gains public `from_bytes` and `as_bytes`, and the `serde` feature serializes it as its z-base-32 text. + ### moq-e2ee and moq-sign are moq-crypto (#7702) `moq-e2ee` and the workspace-only `moq-sign` merged into `moq-crypto`, as its `e2ee` and `sign` modules. diff --git a/rs/moq-crypto/README.md b/rs/moq-crypto/README.md index 4281ab09a79..9e1fc42063f 100644 --- a/rs/moq-crypto/README.md +++ b/rs/moq-crypto/README.md @@ -2,13 +2,18 @@ # moq-crypto -Workspace-only implementation; this crate is not published. - Content signing and end-to-end encryption for [Media over QUIC](https://moq.dev): the `sign` module signs every frame so relays cannot forge it, and the `e2ee` module encrypts groups, datagrams, and track names so relays cannot read them. Both wrap `moq-net` and check timestamps with its `track::Info::rescale`, so a frame either half accepts is one `moq-net` accepts. +## Features + +`sign` and `e2ee`, both default, enable the two modules. With default features off, the crate is +only `sign::PublicKey`, the endpoint ID, with its canonical z-base-32 text: it builds without +`moq-net` or aws-lc-rs, for crates such as `moq-auth` that name endpoints but sign nothing. +`serde` adds the key's string serialization. + ## Signing Per-frame Ed25519 signatures for [Media over QUIC](https://moq.dev) broadcasts. Profile diff --git a/rs/moq-crypto/src/lib.rs b/rs/moq-crypto/src/lib.rs index e33db807d36..53f52dd8151 100644 --- a/rs/moq-crypto/src/lib.rs +++ b/rs/moq-crypto/src/lib.rs @@ -3,9 +3,14 @@ //! [`sign`] signs every grouped frame so a relay can proxy and cache a broadcast without //! forging it. [`e2ee`] encrypts groups, datagrams, and track names so a relay forwards //! ciphertext it cannot read. Both wrap `moq-net` and check a frame's timestamp with -//! [`moq_net::track::Info::rescale`], so neither accepts a frame the net track would refuse. +//! `moq_net::track::Info::rescale`, so neither accepts a frame the net track would refuse. +//! +//! The `sign` and `e2ee` features, both default, gate those halves. Without them the crate holds +//! only [`sign::PublicKey`], the endpoint ID, and its canonical z-base-32 text, with no `moq-net` +//! or crypto backend; `serde` adds its string serialization. #![warn(missing_docs)] +#[cfg(feature = "e2ee")] pub mod e2ee; pub mod sign; diff --git a/rs/moq-crypto/src/sign/broadcast.rs b/rs/moq-crypto/src/sign/broadcast.rs index 7a5c1372d01..716809ba4a9 100644 --- a/rs/moq-crypto/src/sign/broadcast.rs +++ b/rs/moq-crypto/src/sign/broadcast.rs @@ -7,9 +7,9 @@ use std::collections::HashMap; use std::sync::{Arc, Mutex}; use crate::sign::error::{Error, Result}; -use crate::sign::key::{PublicKey, SecretKey}; use crate::sign::scope::Scope; use crate::sign::track; +use crate::sign::{PublicKey, SecretKey}; /// The publishing side of a signed broadcast. /// diff --git a/rs/moq-crypto/src/sign/group.rs b/rs/moq-crypto/src/sign/group.rs index faf09b1d72e..855aa2587ce 100644 --- a/rs/moq-crypto/src/sign/group.rs +++ b/rs/moq-crypto/src/sign/group.rs @@ -7,8 +7,8 @@ use std::task::{Poll, ready}; use bytes::Bytes; use crate::sign::error::{Error, Result}; -use crate::sign::key::{PublicKey, SecretKey}; use crate::sign::scope::Scope; +use crate::sign::{PublicKey, SecretKey}; /// A grouped frame whose signature verified. #[derive(Clone, Debug, PartialEq, Eq)] diff --git a/rs/moq-crypto/src/sign/key.rs b/rs/moq-crypto/src/sign/key.rs index ff87370e844..706e018b618 100644 --- a/rs/moq-crypto/src/sign/key.rs +++ b/rs/moq-crypto/src/sign/key.rs @@ -1,20 +1,16 @@ -//! Endpoint keys: a persistent Ed25519 secret key and the public key that names its endpoint. +//! The Ed25519 public key that names an endpoint, and its z-base-32 text. +//! +//! This builds without the `sign` feature, so a crate that only names endpoints shares the one +//! codec without `moq-net` or a crypto backend. use std::fmt; -use std::io::{self, Write}; use std::str::FromStr; -use std::sync::Arc; - -use aws_lc_rs::signature::{ED25519, Ed25519KeyPair, KeyPair, UnparsedPublicKey}; -use zeroize::Zeroizing; - -use crate::sign::scope::SIGNATURE_LEN; /// Ed25519 key length in bytes, for both the secret seed and the public key. -const KEY_LEN: usize = 32; +pub(crate) const KEY_LEN: usize = 32; /// z-base-32: most significant bit first, no padding, trailing bits must be zero. -static ZBASE32: std::sync::LazyLock = std::sync::LazyLock::new(|| { +pub(crate) static ZBASE32: std::sync::LazyLock = std::sync::LazyLock::new(|| { let mut spec = data_encoding::Specification::new(); spec.symbols.push_str("ybndrfg8ejkmcpqxot1uwisza345h769"); spec.encoding().expect("a valid z-base-32 specification") @@ -31,13 +27,20 @@ static ZBASE32: std::sync::LazyLock = std::sync::LazyLo pub struct PublicKey([u8; KEY_LEN]); impl PublicKey { - #[cfg(test)] - pub(crate) fn from_bytes(bytes: [u8; KEY_LEN]) -> Self { + /// The ID of the raw 32-byte Ed25519 public key. + pub fn from_bytes(bytes: [u8; KEY_LEN]) -> Self { Self(bytes) } + /// The raw 32-byte Ed25519 public key. + pub fn as_bytes(&self) -> &[u8; KEY_LEN] { + &self.0 + } + /// Whether `signature` is this key's Ed25519 signature over `message`. - pub(crate) fn verify(&self, message: &[u8], signature: &[u8; SIGNATURE_LEN]) -> bool { + #[cfg(feature = "sign")] + pub(crate) fn verify(&self, message: &[u8], signature: &[u8; crate::sign::scope::SIGNATURE_LEN]) -> bool { + use aws_lc_rs::signature::{ED25519, UnparsedPublicKey}; UnparsedPublicKey::new(&ED25519, &self.0) .verify(message, signature) .is_ok() @@ -70,9 +73,24 @@ impl fmt::Debug for PublicKey { } } +#[cfg(feature = "serde")] +impl serde::Serialize for PublicKey { + fn serialize(&self, serializer: S) -> Result { + serializer.collect_str(self) + } +} + +#[cfg(feature = "serde")] +impl<'de> serde::Deserialize<'de> for PublicKey { + fn deserialize>(deserializer: D) -> Result { + let text = ::deserialize(deserializer)?; + text.parse().map_err(serde::de::Error::custom) + } +} + /// Decode exactly 52 z-base-32 characters. The alphabet is lowercase-only and nonzero trailing /// bits are refused, so a key has one text form. -fn decode(text: &str) -> Result<[u8; KEY_LEN], InvalidKey> { +pub(crate) fn decode(text: &str) -> Result<[u8; KEY_LEN], InvalidKey> { let mut out = [0u8; KEY_LEN]; match ZBASE32.decode_len(text.len()) { Ok(KEY_LEN) => ZBASE32.decode_mut(text.as_bytes(), &mut out).map_err(|_| InvalidKey)?, @@ -81,117 +99,51 @@ fn decode(text: &str) -> Result<[u8; KEY_LEN], InvalidKey> { Ok(out) } -struct Inner { - seed: Zeroizing<[u8; KEY_LEN]>, - pair: Ed25519KeyPair, - public: PublicKey, -} - -/// An endpoint's Ed25519 secret key: cheap to clone, redacted from `Debug`. -#[derive(Clone)] -pub struct SecretKey(Arc); +#[cfg(test)] +mod tests { + use serde_json::Value; -impl SecretKey { - /// Generate a fresh key from the system's secure random source. - pub fn generate() -> Self { - let mut seed = Zeroizing::new([0u8; KEY_LEN]); - aws_lc_rs::rand::fill(seed.as_mut()).expect("system randomness"); - Self::from_seed(seed) - } - - pub(crate) fn from_seed(seed: Zeroizing<[u8; KEY_LEN]>) -> Self { - let pair = Ed25519KeyPair::from_seed_unchecked(seed.as_ref()).expect("a 32-byte Ed25519 seed"); - let public = PublicKey( - pair.public_key() - .as_ref() - .try_into() - .expect("a 32-byte Ed25519 public key"), - ); - Self(Arc::new(Inner { seed, pair, public })) - } - - /// The public key, which is this endpoint's ID. - pub fn public(&self) -> PublicKey { - self.0.public - } - - /// Load the key stored at `path`, or generate one and store it there if the file is missing. - /// - /// The file holds the seed as 52 z-base-32 characters and a newline, created with mode - /// `0600`. A key is never replaced implicitly: an existing file that does not parse, or that - /// other users can read, is an error rather than a reason to generate a new identity. - /// - /// # Errors - /// - /// [`io::ErrorKind::InvalidData`] for a malformed or exposed file, or the I/O error reading or - /// creating it. - pub fn load_or_generate(path: impl AsRef) -> io::Result { - let path = path.as_ref(); - match Self::load(path) { - Err(err) if err.kind() == io::ErrorKind::NotFound => {} - other => return other, - } + use super::PublicKey; - let key = Self::generate(); - let mut options = std::fs::OpenOptions::new(); - options.write(true).create_new(true); - #[cfg(unix)] - std::os::unix::fs::OpenOptionsExt::mode(&mut options, 0o600); - let mut file = match options.open(path) { - Ok(file) => file, - // Another process created it first; its key wins. - Err(err) if err.kind() == io::ErrorKind::AlreadyExists => return Self::load(path), - Err(err) => return Err(err), - }; - // Sized up front, so appending the newline never leaves an unzeroized copy behind. - let mut text = Zeroizing::new(String::with_capacity(ZBASE32.encode_len(KEY_LEN) + 1)); - ZBASE32.encode_append(key.0.seed.as_ref(), &mut text); - text.push('\n'); - file.write_all(text.as_bytes())?; - file.sync_all()?; - Ok(key) + fn vectors() -> Value { + serde_json::from_str(include_str!("../../../../drafts/moq-sign-00.json")).unwrap() } - fn load(path: &std::path::Path) -> io::Result { - let file = std::fs::File::open(path)?; - #[cfg(unix)] - { - use std::os::unix::fs::PermissionsExt; - if file.metadata()?.permissions().mode() & 0o077 != 0 { - return Err(io::Error::new( - io::ErrorKind::InvalidData, - format!("key file {} is accessible by other users", path.display()), - )); + #[test] + fn vector_keys() { + for row in vectors()["keys"].as_array().unwrap() { + let id = &row["id"]; + let bytes = hex::decode(row["public"].as_str().unwrap()).unwrap(); + let endpoint: PublicKey = row["endpoint"].as_str().unwrap().parse().unwrap(); + assert_eq!( + endpoint, + PublicKey::from_bytes(bytes.clone().try_into().unwrap()), + "{id}" + ); + assert_eq!(endpoint.as_bytes().as_slice(), bytes, "{id}"); + assert_eq!(endpoint.to_string(), row["endpoint"].as_str().unwrap(), "{id}"); + #[cfg(feature = "serde")] + { + assert_eq!(serde_json::to_value(endpoint).unwrap(), row["endpoint"], "{id}"); + assert_eq!( + serde_json::from_value::(row["endpoint"].clone()).unwrap(), + endpoint, + "{id}" + ); } } - let mut text = Zeroizing::new(String::new()); - io::Read::read_to_string(&mut &file, &mut text)?; - let encoded = text.strip_suffix('\n').unwrap_or(&text); - let seed = decode(encoded).map_err(|_| { - io::Error::new( - io::ErrorKind::InvalidData, - format!("key file {} is not 52 z-base-32 characters", path.display()), - ) - })?; - Ok(Self::from_seed(Zeroizing::new(seed))) - } - - /// Sign `message` with Ed25519. - pub(crate) fn sign(&self, message: &[u8]) -> [u8; SIGNATURE_LEN] { - self.0 - .pair - .sign(message) - .as_ref() - .try_into() - .expect("a 64-byte Ed25519 signature") } -} -impl fmt::Debug for SecretKey { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("SecretKey") - .field("public", &self.0.public) - .field("seed", &"") - .finish() + #[test] + fn vector_invalid_ids() { + for row in vectors()["invalid_ids"].as_array().unwrap() { + let id = &row["id"]; + assert!(row["text"].as_str().unwrap().parse::().is_err(), "{id}"); + #[cfg(feature = "serde")] + assert!( + serde_json::from_value::(row["text"].clone()).is_err(), + "{id}" + ); + } } } diff --git a/rs/moq-crypto/src/sign/mod.rs b/rs/moq-crypto/src/sign/mod.rs index 6b0dfe621c1..6a641f845b3 100644 --- a/rs/moq-crypto/src/sign/mod.rs +++ b/rs/moq-crypto/src/sign/mod.rs @@ -16,19 +16,31 @@ //! the broadcast by the same application path, independent of where a relay mounts it. //! //! A broadcast that needs no signature, the identity level, uses `moq-net` directly. +//! +//! Without the `sign` feature, this module holds only [`PublicKey`] and its text codec. #![warn(missing_docs)] +#[cfg(feature = "sign")] pub mod broadcast; +#[cfg(feature = "sign")] pub mod group; +#[cfg(feature = "sign")] pub mod track; +#[cfg(feature = "sign")] mod error; mod key; +#[cfg(feature = "sign")] mod scope; +#[cfg(feature = "sign")] +mod secret; +#[cfg(feature = "sign")] pub use error::{Error, Result}; -pub use key::{InvalidKey, PublicKey, SecretKey}; +pub use key::{InvalidKey, PublicKey}; +#[cfg(feature = "sign")] +pub use secret::SecretKey; -#[cfg(test)] +#[cfg(all(test, feature = "sign"))] mod tests; diff --git a/rs/moq-crypto/src/sign/scope.rs b/rs/moq-crypto/src/sign/scope.rs index c256f5788f2..7fc28dc7db9 100644 --- a/rs/moq-crypto/src/sign/scope.rs +++ b/rs/moq-crypto/src/sign/scope.rs @@ -5,7 +5,7 @@ use std::sync::Arc; use bytes::{BufMut, Bytes, BytesMut}; use crate::sign::error::{Error, Result}; -use crate::sign::key::{PublicKey, SecretKey}; +use crate::sign::{PublicKey, SecretKey}; /// Length of the Ed25519 signature appended to every signed frame. pub(crate) const SIGNATURE_LEN: usize = 64; diff --git a/rs/moq-crypto/src/sign/secret.rs b/rs/moq-crypto/src/sign/secret.rs new file mode 100644 index 00000000000..bc26b0dfb69 --- /dev/null +++ b/rs/moq-crypto/src/sign/secret.rs @@ -0,0 +1,126 @@ +//! An endpoint's persistent Ed25519 secret key. + +use std::fmt; +use std::io::{self, Write}; +use std::sync::Arc; + +use aws_lc_rs::signature::{Ed25519KeyPair, KeyPair}; +use zeroize::Zeroizing; + +use crate::sign::key::{KEY_LEN, PublicKey, ZBASE32, decode}; +use crate::sign::scope::SIGNATURE_LEN; + +struct Inner { + seed: Zeroizing<[u8; KEY_LEN]>, + pair: Ed25519KeyPair, + public: PublicKey, +} + +/// An endpoint's Ed25519 secret key: cheap to clone, redacted from `Debug`. +#[derive(Clone)] +pub struct SecretKey(Arc); + +impl SecretKey { + /// Generate a fresh key from the system's secure random source. + pub fn generate() -> Self { + let mut seed = Zeroizing::new([0u8; KEY_LEN]); + aws_lc_rs::rand::fill(seed.as_mut()).expect("system randomness"); + Self::from_seed(seed) + } + + pub(crate) fn from_seed(seed: Zeroizing<[u8; KEY_LEN]>) -> Self { + let pair = Ed25519KeyPair::from_seed_unchecked(seed.as_ref()).expect("a 32-byte Ed25519 seed"); + let public = PublicKey::from_bytes( + pair.public_key() + .as_ref() + .try_into() + .expect("a 32-byte Ed25519 public key"), + ); + Self(Arc::new(Inner { seed, pair, public })) + } + + /// The public key, which is this endpoint's ID. + pub fn public(&self) -> PublicKey { + self.0.public + } + + /// Load the key stored at `path`, or generate one and store it there if the file is missing. + /// + /// The file holds the seed as 52 z-base-32 characters and a newline, created with mode + /// `0600`. A key is never replaced implicitly: an existing file that does not parse, or that + /// other users can read, is an error rather than a reason to generate a new identity. + /// + /// # Errors + /// + /// [`io::ErrorKind::InvalidData`] for a malformed or exposed file, or the I/O error reading or + /// creating it. + pub fn load_or_generate(path: impl AsRef) -> io::Result { + let path = path.as_ref(); + match Self::load(path) { + Err(err) if err.kind() == io::ErrorKind::NotFound => {} + other => return other, + } + + let key = Self::generate(); + let mut options = std::fs::OpenOptions::new(); + options.write(true).create_new(true); + #[cfg(unix)] + std::os::unix::fs::OpenOptionsExt::mode(&mut options, 0o600); + let mut file = match options.open(path) { + Ok(file) => file, + // Another process created it first; its key wins. + Err(err) if err.kind() == io::ErrorKind::AlreadyExists => return Self::load(path), + Err(err) => return Err(err), + }; + // Sized up front, so appending the newline never leaves an unzeroized copy behind. + let mut text = Zeroizing::new(String::with_capacity(ZBASE32.encode_len(KEY_LEN) + 1)); + ZBASE32.encode_append(key.0.seed.as_ref(), &mut text); + text.push('\n'); + file.write_all(text.as_bytes())?; + file.sync_all()?; + Ok(key) + } + + fn load(path: &std::path::Path) -> io::Result { + let file = std::fs::File::open(path)?; + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + if file.metadata()?.permissions().mode() & 0o077 != 0 { + return Err(io::Error::new( + io::ErrorKind::InvalidData, + format!("key file {} is accessible by other users", path.display()), + )); + } + } + let mut text = Zeroizing::new(String::new()); + io::Read::read_to_string(&mut &file, &mut text)?; + let encoded = text.strip_suffix('\n').unwrap_or(&text); + let seed = decode(encoded).map_err(|_| { + io::Error::new( + io::ErrorKind::InvalidData, + format!("key file {} is not 52 z-base-32 characters", path.display()), + ) + })?; + Ok(Self::from_seed(Zeroizing::new(seed))) + } + + /// Sign `message` with Ed25519. + pub(crate) fn sign(&self, message: &[u8]) -> [u8; SIGNATURE_LEN] { + self.0 + .pair + .sign(message) + .as_ref() + .try_into() + .expect("a 64-byte Ed25519 signature") + } +} + +impl fmt::Debug for SecretKey { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("SecretKey") + .field("public", &self.0.public) + .field("seed", &"") + .finish() + } +} diff --git a/rs/moq-crypto/src/sign/tests.rs b/rs/moq-crypto/src/sign/tests.rs index 3400c5ba22c..b9fcd0cc620 100644 --- a/rs/moq-crypto/src/sign/tests.rs +++ b/rs/moq-crypto/src/sign/tests.rs @@ -44,18 +44,10 @@ fn identity(row: &Value) -> (Arc, u64, u64, Option) { } #[test] -fn vector_keys() { +fn vector_key_seeds() { for row in vectors()["keys"].as_array().unwrap() { - let id = &row["id"]; - let endpoint = public(&row["endpoint"]); - assert_eq!( - endpoint, - PublicKey::from_bytes(hex(&row["public"]).try_into().unwrap()), - "{id}" - ); - assert_eq!(endpoint.to_string(), row["endpoint"].as_str().unwrap(), "{id}"); if !row["seed"].is_null() { - assert_eq!(seed(&row["seed"]).public(), endpoint, "{id}"); + assert_eq!(seed(&row["seed"]).public(), public(&row["endpoint"]), "{}", row["id"]); } } } @@ -91,17 +83,6 @@ fn vector_negative() { } } -#[test] -fn vector_invalid_ids() { - for row in vectors()["invalid_ids"].as_array().unwrap() { - assert!( - row["text"].as_str().unwrap().parse::().is_err(), - "{}", - row["id"] - ); - } -} - #[test] fn key_file_persists_and_never_rotates() { let dir = tempfile::tempdir().unwrap(); diff --git a/rs/moq-crypto/src/sign/track.rs b/rs/moq-crypto/src/sign/track.rs index 820344435cc..91cd5b577b6 100644 --- a/rs/moq-crypto/src/sign/track.rs +++ b/rs/moq-crypto/src/sign/track.rs @@ -6,8 +6,8 @@ use std::task::{Poll, ready}; use crate::sign::error::Result; use crate::sign::group; -use crate::sign::key::{PublicKey, SecretKey}; use crate::sign::scope::Scope; +use crate::sign::{PublicKey, SecretKey}; /// Producer for one signed track. /// diff --git a/sh/drafts/check.sh b/sh/drafts/check.sh index e5b78393bec..e1f852cba4c 100755 --- a/sh/drafts/check.sh +++ b/sh/drafts/check.sh @@ -31,6 +31,6 @@ for f in draft-*.md; do done for f in moq-e2ee-*.ts moq-sign-*.ts; do - bun tsc --ignoreConfig --noEmit --skipLibCheck --target esnext --module esnext --moduleResolution bundler --types bun "$f" + bun tsc --ignoreConfig --noEmit --skipLibCheck --target esnext --module esnext --moduleResolution bundler --allowImportingTsExtensions --types bun "$f" bun "$f" done