diff --git a/crates/proto/UPSTREAM.md b/crates/proto/UPSTREAM.md index faf3d63..f9a7792 100644 --- a/crates/proto/UPSTREAM.md +++ b/crates/proto/UPSTREAM.md @@ -100,3 +100,9 @@ against or offered back to upstream. (RFC 2782 order). A CNAME among the SRV answers no longer panics. The lookups are injectable inside the module, and the tests use a fake TSDNS server and fake names; the upstream tests that need DNS and the internet are `#[ignore]`d. +12. **Private keys with leading zero bytes** (`tsproto-types/src/crypto.rs`, + "Voelin patch"). The TeamSpeak export stores the private scalar as a DER + integer, which drops leading zero bytes, and `EccKeyPrivP256::from_tomcrypt` + handed those fewer than 32 bytes to `from_short`, which refused them: one + identity in 256 (the official client's ones included) could not be imported. + The scalar is padded to 32 bytes now; a test round-trips such keys. diff --git a/crates/proto/tsproto-types/src/crypto.rs b/crates/proto/tsproto-types/src/crypto.rs index 85affd1..65d91fd 100644 --- a/crates/proto/tsproto-types/src/crypto.rs +++ b/crates/proto/tsproto-types/src/crypto.rs @@ -394,14 +394,23 @@ impl EccKeyPrivP256 { if (*len != 1 && *len != 2) || content[0] & 0x80 == 0 { return Err(Error::NoPrivateKey); } + // Voelin patch: DER drops the leading zero bytes of a scalar + // below 2^248 (one key in 256), which `from_short` refused. + let short = |i: &BigInt| -> Result { + let bytes = i.to_bytes_be().1; + let mut padded = [0u8; 32]; + let start = padded.len().checked_sub(bytes.len()).ok_or(Error::NoShortKey)?; + padded[start..].copy_from_slice(&bytes); + Self::from_short(&padded) + }; if *len == 1 { if let Some(ASN1Block::Integer(_, i)) = blocks.get(4) { - Self::from_short(&i.to_bytes_be().1) + short(i) } else { Err(Error::NoPrivateKey) } } else if let Some(ASN1Block::Integer(_, i)) = blocks.get(2) { - Self::from_short(&i.to_bytes_be().1) + short(i) } else { Err(Error::NoPrivateKey) } @@ -602,6 +611,21 @@ mod tests { assert_eq!(short, short2); } + /// Voelin patch: a private key whose first bytes are zero survives the + /// TeamSpeak export (DER stores the scalar without them). + #[test] + fn small_priv_key_round_trips() { + for leading_zeros in [1, 2, 31] { + let mut short = [0x5au8; 32]; + short[..leading_zeros].fill(0); + let key = EccKeyPrivP256::from_short(&short).unwrap(); + let back = EccKeyPrivP256::from_ts(&key.to_ts()).unwrap(); + assert_eq!(back.to_short().as_slice(), short); + let back = EccKeyPrivP256::from_ts_obfuscated(&key.to_ts_obfuscated()).unwrap(); + assert_eq!(back.to_short().as_slice(), short); + } + } + #[test] fn parse_ed25519_pub_key() { EccKeyPubEd25519::from_base64("zQ3irtRjRVCafjz9j2iz3HVVsp3M7HPNGHUPmTgSQIo=").unwrap();