pay: domain-separate tip and gate legacy signature acceptance
`tip` was the one payment frame with no domain tag: sign_tip signed the LF-joined fields, so its signature could be confused with any other newline-joined preimage. Mirror inv/rcp: sign_tip now emits "onionwire-tip-v1" || 0x00-separated txid/amount/address/memo/ts, and verify_tip tries that form then the historical layout. Legacy acceptance is now one switch instead of nine call sites: `pay::LEGACY_SIGS` (default true) plus `pay::legacy_sigs()`. Every v1 / concat fallback reads it — the `onionwire:v1` invite string and its two historical signature forms, `loc`, `inv`, `rcp`, and `tip`. `prf` is not gated here because src/profile.rs is outside this card's file scope; the spec says so. Tests: legacy-on keeps every untagged form verifying (incl. a new tip case); legacy-off rejects each untagged form while the tagged form still verifies (loc, inv, rcp, tip, invite); sign_tip is pinned to the tagged layout. docs/PROTOCOL.md gains the emitted/accepted table per signed frame and the mixed-fleet consequence.
This commit is contained in:
parent
36969a7e95
commit
b8c52ca97d
6 changed files with 352 additions and 35 deletions
|
|
@ -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<u8> {
|
||||
|
|
|
|||
100
src/pay.rs
100
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<T> = std::result::Result<T, Error>;
|
||||
|
||||
/// 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<Option<bool>> = 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<R>(on: bool, f: impl FnOnce() -> R) -> R {
|
||||
struct Restore(Option<bool>);
|
||||
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<u8> {
|
||||
|
|
@ -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<u8> {
|
||||
|
|
@ -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<u8> {
|
||||
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<u8> {
|
||||
let mut msg = Vec::from(txid.as_bytes());
|
||||
msg.push(b'\n');
|
||||
|
|
@ -361,7 +415,10 @@ pub fn sign_tip(
|
|||
ts: i64,
|
||||
) -> Result<Tip> {
|
||||
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<u8> {
|
||||
|
|
|
|||
|
|
@ -101,6 +101,11 @@ pub fn decode(raw: &str) -> Result<QrPayload> {
|
|||
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();
|
||||
|
|
|
|||
Loading…
Reference in a new issue