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
26
src/loc.rs
26
src/loc.rs
|
|
@ -27,7 +27,7 @@ pub fn sign(identity_sk: &[u8], onion: &str, ts: i64) -> Result<Loc> {
|
|||
.try_into()
|
||||
.map_err(|_| Error("identity secret key must be 32 bytes".into()))?;
|
||||
let sk = SigningKey::from_bytes(&sk_bytes);
|
||||
let sig = sk.sign(&sign_msg(onion, ts)).to_bytes().to_vec();
|
||||
let sig = sk.sign(&sign_msg_v2(onion, ts)).to_bytes().to_vec();
|
||||
Ok(Loc {
|
||||
onion: onion.to_string(),
|
||||
ts,
|
||||
|
|
@ -50,8 +50,12 @@ pub fn verify(identity_pk: &[u8], loc: &Loc) -> bool {
|
|||
let Ok(vk) = VerifyingKey::from_bytes(&pk) else {
|
||||
return false;
|
||||
};
|
||||
vk.verify(&sign_msg(&loc.onion, loc.ts), &Signature::from_bytes(&sig))
|
||||
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()
|
||||
}
|
||||
|
||||
pub fn encode(loc: &Loc) -> Vec<u8> {
|
||||
|
|
@ -77,13 +81,29 @@ pub fn decode(pt: &[u8]) -> Option<Loc> {
|
|||
Some(Loc { onion, ts, sig })
|
||||
}
|
||||
|
||||
fn sign_msg(onion: &str, ts: i64) -> Vec<u8> {
|
||||
fn sign_msg_v2(onion: &str, ts: i64) -> Vec<u8> {
|
||||
tagged(
|
||||
b"onionwire-loc-v1",
|
||||
&[onion.as_bytes(), ts.to_string().as_bytes()],
|
||||
)
|
||||
}
|
||||
|
||||
fn sign_msg_v1(onion: &str, ts: i64) -> Vec<u8> {
|
||||
let mut msg = Vec::from(onion.as_bytes());
|
||||
msg.push(b'\n');
|
||||
msg.extend_from_slice(ts.to_string().as_bytes());
|
||||
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 to_hex(bytes: &[u8]) -> String {
|
||||
bytes.iter().map(|b| format!("{b:02x}")).collect()
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue