fix(protocol): domain-separate loc/prf/inv/rcp signatures
All checks were successful
ci / test (pull_request) Successful in 2m30s
All checks were successful
ci / test (pull_request) Successful in 2m30s
Sign application frames as tag || 0x00 || fields (onionwire-loc-v1, onionwire-prf-v1, onionwire-inv-v1, onionwire-rcp-v1). Verify accepts that form first, then the legacy untagged LF-joined layout. Encode still emits only the tagged signature; wire prefixes and field layout are unchanged. Older builds verify untagged bytes only, so they reject new tagged signatures (fail closed). Mixed-version fleets break outbound app frames from a new sender to an old receiver until both sides are updated.
This commit is contained in:
parent
cb91ba2b78
commit
c8e69732c7
5 changed files with 371 additions and 16 deletions
59
src/pay.rs
59
src/pay.rs
|
|
@ -179,7 +179,10 @@ pub fn sign_invoice(
|
|||
ts: i64,
|
||||
) -> Result<Invoice> {
|
||||
check_invoice_fields(amount_atomic, address, memo)?;
|
||||
let sig = sign_bytes(identity_sk, &inv_sign_msg(amount_atomic, address, memo, ts))?;
|
||||
let sig = sign_bytes(
|
||||
identity_sk,
|
||||
&inv_sign_msg_v2(amount_atomic, address, memo, ts),
|
||||
)?;
|
||||
Ok(Invoice {
|
||||
amount_atomic: amount_atomic.to_string(),
|
||||
address: address.to_string(),
|
||||
|
|
@ -195,7 +198,11 @@ pub fn verify_invoice(identity_pk: &[u8], inv: &Invoice) -> bool {
|
|||
}
|
||||
verify_bytes(
|
||||
identity_pk,
|
||||
&inv_sign_msg(&inv.amount_atomic, &inv.address, &inv.memo, inv.ts),
|
||||
&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,
|
||||
)
|
||||
}
|
||||
|
|
@ -243,7 +250,10 @@ pub fn sign_receipt(
|
|||
ts: i64,
|
||||
) -> Result<Receipt> {
|
||||
check_receipt_fields(txid, amount_atomic, address)?;
|
||||
let sig = sign_bytes(identity_sk, &rcp_sign_msg(txid, amount_atomic, address, ts))?;
|
||||
let sig = sign_bytes(
|
||||
identity_sk,
|
||||
&rcp_sign_msg_v2(txid, amount_atomic, address, ts),
|
||||
)?;
|
||||
Ok(Receipt {
|
||||
txid: txid.to_string(),
|
||||
amount_atomic: amount_atomic.to_string(),
|
||||
|
|
@ -259,7 +269,11 @@ pub fn verify_receipt(identity_pk: &[u8], rcp: &Receipt) -> bool {
|
|||
}
|
||||
verify_bytes(
|
||||
identity_pk,
|
||||
&rcp_sign_msg(&rcp.txid, &rcp.amount_atomic, &rcp.address, rcp.ts),
|
||||
&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,
|
||||
)
|
||||
}
|
||||
|
|
@ -316,7 +330,19 @@ fn check_receipt_fields(txid: &str, amount_atomic: &str, address: &str) -> Resul
|
|||
check_address(address)
|
||||
}
|
||||
|
||||
fn inv_sign_msg(amount_atomic: &str, address: &str, memo: &str, ts: i64) -> Vec<u8> {
|
||||
fn inv_sign_msg_v2(amount_atomic: &str, address: &str, memo: &str, ts: i64) -> Vec<u8> {
|
||||
tagged(
|
||||
b"onionwire-inv-v1",
|
||||
&[
|
||||
amount_atomic.as_bytes(),
|
||||
address.as_bytes(),
|
||||
memo.as_bytes(),
|
||||
ts.to_string().as_bytes(),
|
||||
],
|
||||
)
|
||||
}
|
||||
|
||||
fn inv_sign_msg_v1(amount_atomic: &str, address: &str, memo: &str, ts: i64) -> Vec<u8> {
|
||||
let mut msg = Vec::from(amount_atomic.as_bytes());
|
||||
msg.push(b'\n');
|
||||
msg.extend_from_slice(address.as_bytes());
|
||||
|
|
@ -327,7 +353,19 @@ fn inv_sign_msg(amount_atomic: &str, address: &str, memo: &str, ts: i64) -> Vec<
|
|||
msg
|
||||
}
|
||||
|
||||
fn rcp_sign_msg(txid: &str, amount_atomic: &str, address: &str, ts: i64) -> Vec<u8> {
|
||||
fn rcp_sign_msg_v2(txid: &str, amount_atomic: &str, address: &str, ts: i64) -> Vec<u8> {
|
||||
tagged(
|
||||
b"onionwire-rcp-v1",
|
||||
&[
|
||||
txid.as_bytes(),
|
||||
amount_atomic.as_bytes(),
|
||||
address.as_bytes(),
|
||||
ts.to_string().as_bytes(),
|
||||
],
|
||||
)
|
||||
}
|
||||
|
||||
fn rcp_sign_msg_v1(txid: &str, amount_atomic: &str, address: &str, ts: i64) -> Vec<u8> {
|
||||
let mut msg = Vec::from(txid.as_bytes());
|
||||
msg.push(b'\n');
|
||||
msg.extend_from_slice(amount_atomic.as_bytes());
|
||||
|
|
@ -338,6 +376,15 @@ fn rcp_sign_msg(txid: &str, amount_atomic: &str, address: &str, ts: i64) -> Vec<
|
|||
msg
|
||||
}
|
||||
|
||||
fn tagged(tag: &[u8], fields: &[&[u8]]) -> Vec<u8> {
|
||||
let mut msg = Vec::from(tag);
|
||||
for f in fields {
|
||||
msg.push(0);
|
||||
msg.extend_from_slice(f);
|
||||
}
|
||||
msg
|
||||
}
|
||||
|
||||
fn sign_bytes(identity_sk: &[u8], msg: &[u8]) -> Result<Vec<u8>> {
|
||||
let sk_bytes: [u8; 32] = identity_sk
|
||||
.try_into()
|
||||
|
|
|
|||
Loading…
Reference in a new issue