From 20cc18b556e50b5c931dff8c620cd2c0c7481a47 Mon Sep 17 00:00:00 2001 From: selikhov Date: Sun, 4 Oct 2026 04:50:44 +0300 Subject: [PATCH] Import identities whose private key starts with zero bytes The identity tests failed now and then: one key in 256 has a private scalar below 2^248, the TeamSpeak export stores it as a DER integer without the leading zero bytes, and EccKeyPrivP256::from_tomcrypt passed those fewer than 32 bytes to from_short, which refused them. Such an identity from the official client could not be imported at all ("no identity in this file"). The scalar is padded to 32 bytes now; a test round-trips keys with one, two and 31 leading zero bytes through both export forms. Local patch 12 in UPSTREAM.md. Co-Authored-By: Claude Opus 5.5 --- crates/proto/UPSTREAM.md | 6 +++++ crates/proto/tsproto-types/src/crypto.rs | 28 ++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) 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();