diff --git a/docs/PROTOCOL.md b/docs/PROTOCOL.md index 72ffd93..bec89a8 100644 --- a/docs/PROTOCOL.md +++ b/docs/PROTOCOL.md @@ -312,20 +312,42 @@ prefix. ### Application-frame signatures -`loc`, `prf`, `inv`, and `rcp` each carry an ed25519 signature over the +`loc`, `prf`, `inv`, `rcp`, and `tip` each carry an ed25519 signature over the **fields**, not the 4-byte typed prefix. Encode MUST sign the domain-separated form (type tag, then each field, `0x00` separators). Verify MUST accept that form and, for mixed-version peers, the legacy untagged LF-joined layout. Anything else fails closed. Do not emit the legacy layout. -`tip` is signed over the LF-joined fields only (no domain tag on encode today). -The `tip ` prefix is not signed. +The invite string is separate and uses `onionwire-invite-v2` (see Invite). -The invite string is separate and keeps `onionwire-invite-v1` (see Invite). +Legacy acceptance is one switch, `LEGACY_SIGS`, default **on**. Every fallback +listed below reads it; with it off, verification accepts only the emitted +(tagged) form. It is a local capability constant, not a wire field: it is never +sent, and a peer cannot negotiate it. -Older builds verify only the untagged bytes, so they reject tagged signatures -(fail closed, no downgrade). Mixed-version fleets break outbound application -frames from a new sender to an old receiver until both sides are updated. +| frame | emitted | accepted inbound | +| --- | --- | --- | +| invite | `onionwire-invite-v2`, `0x00`-separated over `k`, `o`, `spk`, `kem` | same; `onionwire:v1` over `onionwire-invite-v1` `0x00`-separated `k`,`o`,`spk`, or over `k \|\| o \|\| spk` (legacy only) | +| `loc ` | `"onionwire-loc-v1"` `0x00` `onion` `0x00` `ts` | same; `onion \|\| LF \|\| ts` (legacy only) | +| `prf ` | `"onionwire-prf-v1"` `0x00` `name` `0x00` `bio` `0x00` `xmr_addr` `0x00` `ts` | same; the four fields joined by LF (not yet gated — see below) | +| `inv ` | `"onionwire-inv-v1"` `0x00` `amount_atomic` `0x00` `address` `0x00` `memo` `0x00` `ts` | same; the four fields joined by LF (legacy only) | +| `rcp ` | `"onionwire-rcp-v1"` `0x00` `txid` `0x00` `amount_atomic` `0x00` `address` `0x00` `ts` | same; the four fields joined by LF (legacy only) | +| `tip ` | `"onionwire-tip-v1"` `0x00` `txid` `0x00` `amount_atomic` `0x00` `address` `0x00` `memo` `0x00` `ts` | same; the five fields joined by LF (legacy only) | + +The tag suffixes do say `-v1`; that is the tag's own version, not the invite +string version. + +`prf ` is the one frame whose legacy fallback is not yet gated: it is accepted +unconditionally in this revision and moving it under `LEGACY_SIGS` is a +separate change. + +**Mixed-fleet consequence.** Older builds verify only the untagged bytes, so +they reject every tagged signature from a new sender — including `tip `, which +older builds emitted and verified untagged. That direction needs the peer +updated before tips flow. The reverse works while `LEGACY_SIGS` is on: a new +build verifies an old sender's untagged `loc`/`prf`/`inv`/`rcp`/`tip` and an +`onionwire:v1` invite. Turning the switch off is a fleet-wide cutover, not a +per-peer choice. ### `loc ` — locator update @@ -348,8 +370,8 @@ ed25519_sign(identity_sk, ``` **Legacy accept:** a signature over `onion || LF || ascii(ts)` is still -accepted on verify so old loc frames verify. New loc frames MUST use the -domain-separated form. Do not emit legacy. +accepted on verify while `LEGACY_SIGS` is on, so old loc frames verify. New loc +frames MUST use the domain-separated form. Do not emit legacy. Apply only if **all** of: @@ -380,7 +402,7 @@ ed25519_sign(identity_sk, **Legacy accept:** a signature over the four fields joined by LF is still accepted on verify. New profile frames MUST use the domain-separated form. -Do not emit legacy. +Do not emit legacy. This fallback is not yet read through `LEGACY_SIGS`. Apply only for an existing friend, valid signature, newer `ts`. Not a public profile. Not a directory. @@ -403,8 +425,8 @@ ed25519_sign(identity_sk, ``` **Legacy accept:** a signature over the four fields joined by LF is still -accepted on verify. New invoices MUST use the domain-separated form. Do not -emit legacy. +accepted on verify while `LEGACY_SIGS` is on. New invoices MUST use the +domain-separated form. Do not emit legacy. The frame is a signed claim, not a payment. It is one-shot: if the peer onion is down, the send fails. There is no queued invoice. @@ -425,8 +447,8 @@ ed25519_sign(identity_sk, ``` **Legacy accept:** a signature over the four fields joined by LF is still -accepted on verify. New receipts MUST use the domain-separated form. Do not -emit legacy. +accepted on verify while `LEGACY_SIGS` is on. New receipts MUST use the +domain-separated form. Do not emit legacy. Signature proves which friend sent the claim. It does **not** prove money moved. Local `verified=1` only if a user-hosted `monero-wallet-rpc` returns @@ -442,13 +464,20 @@ Sent after a chain transfer, together with `rcp `. One-shot. tip \n\n
\n\n\n ``` -The `tip ` prefix is **not** signed. Encode signs the LF-joined fields: +The `tip ` prefix is **not** signed. + +**Current signature (encode this):** ``` ed25519_sign(identity_sk, - txid || LF || amount_atomic || LF || address || LF || memo || LF || ascii(ts)) + "onionwire-tip-v1" || 0x00 || txid || 0x00 || amount_atomic || 0x00 || + address || 0x00 || memo || 0x00 || ascii(ts)) ``` +**Legacy accept:** a signature over the five fields joined by LF is still +accepted on verify while `LEGACY_SIGS` is on. New tips MUST use the +domain-separated form. Do not emit legacy. + `memo` MUST NOT contain newlines. Same address and amount rules as invoices. A peer that does not implement `tip ` MUST drop it as an unknown typed prefix, not render it as chat. @@ -880,7 +909,8 @@ signature covers `kem_ascii`, removing the field or substituting a different key fails verification — that is the anti-strip/anti-substitution property. **Legacy accept (`onionwire:v1`):** a v1 invite has no `kem` field. Decode -accepts it and yields a friend with no `kem_pk`, i.e. a classical peer. A v1 +accepts it while `LEGACY_SIGS` is on and yields a friend with no `kem_pk`, i.e. +a classical peer; with the switch off a v1 invite fails closed. A v1 signature is accepted in either historical form: the tagged `"onionwire-invite-v1" || 0x00 || k || 0x00 || o || 0x00 || spk` or the raw concatenation `k || o || spk` (no tag, no separators), so old invites keep diff --git a/src/loc.rs b/src/loc.rs index 9803f77..16d4f25 100644 --- a/src/loc.rs +++ b/src/loc.rs @@ -54,9 +54,10 @@ pub fn verify(identity_pk: &[u8], loc: &Loc) -> bool { let signature = Signature::from_bytes(&sig); vk.verify(&sign_msg_v2(&loc.onion, loc.ts), &signature) .is_ok() - || vk - .verify(&sign_msg_v1(&loc.onion, loc.ts), &signature) - .is_ok() + || (crate::pay::legacy_sigs() + && vk + .verify(&sign_msg_v1(&loc.onion, loc.ts), &signature) + .is_ok()) } pub fn encode(loc: &Loc) -> Vec { diff --git a/src/pay.rs b/src/pay.rs index 681f106..c15b670 100644 --- a/src/pay.rs +++ b/src/pay.rs @@ -1,9 +1,47 @@ use ed25519_dalek::{Signature, Signer, SigningKey, Verifier, VerifyingKey}; use sha3::{Digest, Keccak256}; +use std::cell::Cell; use zeroize::Zeroizing; pub type Result = std::result::Result; +/// Legacy signature acceptance. +/// +/// Every signed frame is **emitted** in its domain-tagged form. Inbound, the +/// historical untagged / LF-joined layout (and the `onionwire:v1` invite +/// string) is still accepted so a mixed-version fleet keeps working; that +/// fallback is gated on this one switch. Set it to `false` to fail closed on +/// legacy signatures for every frame that reads it — `loc`, the invite string, +/// `inv`, `rcp`, and `tip`. (`prf` is not yet gated; see `docs/PROTOCOL.md`.) +pub const LEGACY_SIGS: bool = true; + +thread_local! { + /// Per-thread override for tests. `None` follows [`LEGACY_SIGS`]. + static LEGACY_SIGS_OVERRIDE: Cell> = const { Cell::new(None) }; +} + +/// Whether legacy (pre-domain-tag) signatures are accepted inbound. +pub fn legacy_sigs() -> bool { + LEGACY_SIGS_OVERRIDE + .with(|o| o.get()) + .unwrap_or(LEGACY_SIGS) +} + +/// Run `f` with legacy acceptance forced to `on`, then restore the previous +/// thread state. A test hook, not a runtime setting: production code reads the +/// switch through [`legacy_sigs`] only. +#[doc(hidden)] +pub fn with_legacy_sigs(on: bool, f: impl FnOnce() -> R) -> R { + struct Restore(Option); + impl Drop for Restore { + fn drop(&mut self) { + LEGACY_SIGS_OVERRIDE.with(|o| o.set(self.0)); + } + } + let _restore = Restore(LEGACY_SIGS_OVERRIDE.with(|o| o.replace(Some(on)))); + f() +} + #[derive(Debug)] pub struct Error(String); @@ -202,11 +240,12 @@ pub fn verify_invoice(identity_pk: &[u8], inv: &Invoice) -> bool { identity_pk, &inv_sign_msg_v2(&inv.amount_atomic, &inv.address, &inv.memo, inv.ts), &inv.sig, - ) || verify_bytes( - identity_pk, - &inv_sign_msg_v1(&inv.amount_atomic, &inv.address, &inv.memo, inv.ts), - &inv.sig, - ) + ) || (legacy_sigs() + && verify_bytes( + identity_pk, + &inv_sign_msg_v1(&inv.amount_atomic, &inv.address, &inv.memo, inv.ts), + &inv.sig, + )) } pub fn encode_invoice(inv: &Invoice) -> Vec { @@ -273,11 +312,12 @@ pub fn verify_receipt(identity_pk: &[u8], rcp: &Receipt) -> bool { identity_pk, &rcp_sign_msg_v2(&rcp.txid, &rcp.amount_atomic, &rcp.address, rcp.ts), &rcp.sig, - ) || verify_bytes( - identity_pk, - &rcp_sign_msg_v1(&rcp.txid, &rcp.amount_atomic, &rcp.address, rcp.ts), - &rcp.sig, - ) + ) || (legacy_sigs() + && verify_bytes( + identity_pk, + &rcp_sign_msg_v1(&rcp.txid, &rcp.amount_atomic, &rcp.address, rcp.ts), + &rcp.sig, + )) } pub fn encode_receipt(rcp: &Receipt) -> Vec { @@ -339,6 +379,20 @@ fn check_tip_fields(txid: &str, amount_atomic: &str, address: &str, memo: &str) Ok(()) } +fn tip_sign_msg_v2(txid: &str, amount_atomic: &str, address: &str, memo: &str, ts: i64) -> Vec { + tagged( + b"onionwire-tip-v1", + &[ + txid.as_bytes(), + amount_atomic.as_bytes(), + address.as_bytes(), + memo.as_bytes(), + ts.to_string().as_bytes(), + ], + ) +} + +/// Historical untagged form. Accept-only, and only while [`LEGACY_SIGS`] is on. fn tip_sign_msg_v1(txid: &str, amount_atomic: &str, address: &str, memo: &str, ts: i64) -> Vec { let mut msg = Vec::from(txid.as_bytes()); msg.push(b'\n'); @@ -361,7 +415,10 @@ pub fn sign_tip( ts: i64, ) -> Result { check_tip_fields(txid, amount_atomic, address, memo)?; - let sig = sign_bytes(identity_sk, &tip_sign_msg_v1(txid, amount_atomic, address, memo, ts))?; + let sig = sign_bytes( + identity_sk, + &tip_sign_msg_v2(txid, amount_atomic, address, memo, ts), + )?; Ok(Tip { txid: txid.to_string(), amount_atomic: amount_atomic.to_string(), @@ -378,9 +435,26 @@ pub fn verify_tip(identity_pk: &[u8], tip: &Tip) -> bool { } verify_bytes( identity_pk, - &tip_sign_msg_v1(&tip.txid, &tip.amount_atomic, &tip.address, &tip.memo, tip.ts), + &tip_sign_msg_v2( + &tip.txid, + &tip.amount_atomic, + &tip.address, + &tip.memo, + tip.ts, + ), &tip.sig, - ) + ) || (legacy_sigs() + && verify_bytes( + identity_pk, + &tip_sign_msg_v1( + &tip.txid, + &tip.amount_atomic, + &tip.address, + &tip.memo, + tip.ts, + ), + &tip.sig, + )) } pub fn encode_tip(tip: &Tip) -> Vec { diff --git a/src/qr.rs b/src/qr.rs index cc8504d..dca9262 100644 --- a/src/qr.rs +++ b/src/qr.rs @@ -101,6 +101,11 @@ pub fn decode(raw: &str) -> Result { if kem_ascii.is_some() { return Err(Error("unknown field".into())); } + // A v1 invite only ever carries a legacy signature: with legacy + // acceptance off it fails closed here, before any verify. + if !crate::pay::legacy_sigs() { + return Err(Error("legacy invite signatures disabled".into())); + } let ok_tagged = vk .verify(&sign_msg_v1_tagged(&k, &onion, &spk), &signature) .is_ok(); diff --git a/tests/app_sig.rs b/tests/app_sig.rs index a3aaac1..523ddea 100644 --- a/tests/app_sig.rs +++ b/tests/app_sig.rs @@ -198,3 +198,188 @@ fn inv_signature_does_not_verify_as_rcp() { "inv-shaped payload must not verify as rcp" ); } + +// ---- legacy acceptance is one switch (`pay::LEGACY_SIGS`) ---- + +fn untagged_loc(sk: &SigningKey, onion: &str, ts: i64) -> Loc { + let mut msg = Vec::from(onion.as_bytes()); + msg.push(b'\n'); + msg.extend_from_slice(ts.to_string().as_bytes()); + Loc { + onion: onion.into(), + ts, + sig: sk.sign(&msg).to_bytes().to_vec(), + } +} + +fn untagged_inv(sk: &SigningKey, amount: &str, address: &str, memo: &str, ts: i64) -> Invoice { + let mut msg = Vec::from(amount.as_bytes()); + msg.push(b'\n'); + msg.extend_from_slice(address.as_bytes()); + msg.push(b'\n'); + msg.extend_from_slice(memo.as_bytes()); + msg.push(b'\n'); + msg.extend_from_slice(ts.to_string().as_bytes()); + Invoice { + amount_atomic: amount.into(), + address: address.into(), + memo: memo.into(), + ts, + sig: sk.sign(&msg).to_bytes().to_vec(), + } +} + +fn untagged_rcp(sk: &SigningKey, txid: &str, amount: &str, address: &str, ts: i64) -> Receipt { + let mut msg = Vec::from(txid.as_bytes()); + msg.push(b'\n'); + msg.extend_from_slice(amount.as_bytes()); + msg.push(b'\n'); + msg.extend_from_slice(address.as_bytes()); + msg.push(b'\n'); + msg.extend_from_slice(ts.to_string().as_bytes()); + Receipt { + txid: txid.into(), + amount_atomic: amount.into(), + address: address.into(), + ts, + sig: sk.sign(&msg).to_bytes().to_vec(), + } +} + +fn untagged_tip( + sk: &SigningKey, + txid: &str, + amount: &str, + address: &str, + memo: &str, + ts: i64, +) -> onionwire::pay::Tip { + let mut msg = Vec::from(txid.as_bytes()); + for f in [amount, address, memo] { + msg.push(b'\n'); + msg.extend_from_slice(f.as_bytes()); + } + msg.push(b'\n'); + msg.extend_from_slice(ts.to_string().as_bytes()); + onionwire::pay::Tip { + txid: txid.into(), + amount_atomic: amount.into(), + address: address.into(), + memo: memo.into(), + ts, + sig: sk.sign(&msg).to_bytes().to_vec(), + } +} + +#[test] +fn legacy_untagged_tip_still_verifies() { + let (sk, pk) = keypair(); + let tip = untagged_tip(&sk, "abc123", "1", MAINNET_STD, "coffee", 42); + assert!( + pay::verify_tip(&pk, &tip), + "legacy tip LF layout must still verify while LEGACY_SIGS is on" + ); +} + +#[test] +fn legacy_off_rejects_untagged_loc() { + let (sk, pk) = keypair(); + let legacy = untagged_loc(&sk, "abc.onion", 42); + let tagged = loc::sign(&sk.to_bytes(), "abc.onion", 42).unwrap(); + pay::with_legacy_sigs(false, || { + assert!( + !loc::verify(&pk, &legacy), + "untagged loc must be rejected with legacy off" + ); + assert!( + loc::verify(&pk, &tagged), + "tagged loc must still verify with legacy off" + ); + }); + assert!( + loc::verify(&pk, &legacy), + "the switch restores acceptance after the scoped override" + ); +} + +#[test] +fn legacy_off_rejects_untagged_inv() { + let (sk, pk) = keypair(); + let legacy = untagged_inv(&sk, "1", MAINNET_STD, "coffee", 42); + let tagged = pay::sign_invoice(&sk.to_bytes(), "1", MAINNET_STD, "coffee", 42).unwrap(); + pay::with_legacy_sigs(false, || { + assert!( + !pay::verify_invoice(&pk, &legacy), + "untagged inv must be rejected with legacy off" + ); + assert!( + pay::verify_invoice(&pk, &tagged), + "tagged inv must still verify with legacy off" + ); + }); + assert!(pay::verify_invoice(&pk, &legacy)); +} + +#[test] +fn legacy_off_rejects_untagged_rcp() { + let (sk, pk) = keypair(); + let legacy = untagged_rcp(&sk, "deadbeef", "1", MAINNET_STD, 99); + let tagged = pay::sign_receipt(&sk.to_bytes(), "deadbeef", "1", MAINNET_STD, 99).unwrap(); + pay::with_legacy_sigs(false, || { + assert!( + !pay::verify_receipt(&pk, &legacy), + "untagged rcp must be rejected with legacy off" + ); + assert!( + pay::verify_receipt(&pk, &tagged), + "tagged rcp must still verify with legacy off" + ); + }); + assert!(pay::verify_receipt(&pk, &legacy)); +} + +#[test] +fn legacy_off_rejects_untagged_tip() { + let (sk, pk) = keypair(); + let legacy = untagged_tip(&sk, "abc123", "1", MAINNET_STD, "coffee", 42); + let tagged = pay::sign_tip(&sk.to_bytes(), "abc123", "1", MAINNET_STD, "coffee", 42).unwrap(); + pay::with_legacy_sigs(false, || { + assert!( + !pay::verify_tip(&pk, &legacy), + "untagged tip must be rejected with legacy off" + ); + assert!( + pay::verify_tip(&pk, &tagged), + "tagged tip must still verify with legacy off" + ); + }); + assert!(pay::verify_tip(&pk, &legacy)); +} + +/// `sign_tip` emits the tagged form only: its signature must not verify over +/// the untagged LF layout. +#[test] +fn tip_emits_tagged_form_only() { + use ed25519_dalek::{Signature, Verifier}; + let (sk, pk) = keypair(); + let tip = pay::sign_tip(&sk.to_bytes(), "abc123", "1", MAINNET_STD, "coffee", 42).unwrap(); + assert!(pay::verify_tip(&pk, &tip)); + let sig: [u8; 64] = tip.sig.as_slice().try_into().expect("64-byte sig"); + let mut msg = Vec::from(b"abc123".as_slice()); + for f in ["1", MAINNET_STD, "coffee"] { + msg.push(b'\n'); + msg.extend_from_slice(f.as_bytes()); + } + msg.push(b'\n'); + msg.extend_from_slice(b"42"); + assert!( + sk.verifying_key() + .verify(&msg, &Signature::from_bytes(&sig)) + .is_err(), + "sign_tip must not sign the untagged layout" + ); + // And the tagged signature covers the memo: changing it fails. + let mut tampered = tip.clone(); + tampered.memo = String::new(); + assert!(!pay::verify_tip(&pk, &tampered)); +} diff --git a/tests/qr.rs b/tests/qr.rs index 3e38e0d..985b9c6 100644 --- a/tests/qr.rs +++ b/tests/qr.rs @@ -312,3 +312,25 @@ fn huge_hex_is_err_quickly() { "decode must fail closed before a multi-MB alloc" ); } + +#[test] +fn legacy_off_rejects_v1_invite_but_v2_still_decodes() { + let _home = TempHome::new(); + let store = Store::open().expect("open"); + let me = store.self_identity().expect("self"); + let sk: [u8; 32] = me.identity_sk.as_slice().try_into().expect("32-byte sk"); + let onion = v3onion("switch"); + let raw_v1 = legacy_v1(&sk, &onion, &[8u8; 32]); + let raw_v2 = qr::encode(&me.identity_sk, &onion, &[8u8; 32], &me.kem_pk).expect("encode v2"); + + onionwire::pay::with_legacy_sigs(false, || { + assert!( + qr::decode(&raw_v1).is_err(), + "a v1 invite must be rejected with legacy off" + ); + let p = qr::decode(&raw_v2).expect("v2 invite still decodes with legacy off"); + assert!(p.kem_pk.is_some(), "v2 invite keeps its PQ key"); + }); + // Legacy acceptance returns with the default switch state. + assert!(qr::decode(&raw_v1).is_ok()); +}