core: decode stored keys with upstream's simple_asn1 shape plus scalar left-padding; deterministic and malformed-input tests

This commit is contained in:
omar committed 2026-10-24 06:18:41 +03:00
1 parent 456041bfde
commit ba4c7ec2af
5 files changed
+106 -83

No files matched your search

+21 -8
View File
@@ -110,11 +110,24 @@ initserver, dropping command")` would Debug-print a whole command if one arrived
(~1 in 256 keys) is written by `to_ts` but cannot be read back. The same applies to
`Identity::new_from_ts_str` (ini import). Affected users would have had a permanently refused
identity (the core never regenerates).
- Fix (`core/src/keyfmt.rs`): the core parses the libtomcrypt DER itself and left-pads the scalar
to 32 bytes; the import path de-obfuscates (same constant as tsproto-types) and then parses
the same way. The stored format is unchanged, so already-written affected files now load.
- Tests: a zero-leading key round-trips (and is asserted to still fail upstream); 2000 random keys
round-trip as plain DER and obfuscated; profile write → load → export → import with a
zero-leading key; upstream test vector. 37 unit + 3 smoke pass. Concurrent stress
6×40: 0 failures. Release `0.1.2`: 8984064 bytes, SHA256
`db045fc505db09854a4f0495340acf56cec60f1ba0f2f67212fe7f7a36b2106e`, imports only system DLLs.
- Fix (`core/src/keyfmt.rs`): upstream's own decoder shape — the same `simple_asn1::from_der`
(already in the lock through tsproto-types), the same SEQUENCE / BIT STRING / INTEGER positions —
with the single difference that the non-negative scalar is left-padded to 32 bytes. The import
path de-obfuscates (same constant as tsproto-types) and then parses the same way. The stored
format is unchanged, so already-written affected files now load; no identity is regenerated.
Malformed input returns an error, never panics. (A first hand-written TLV parser from the same
day was replaced by this after review.)
- Permanent tests are deterministic: fixed scalars with one and three leading zero bytes, a
sign-padded scalar (top bit set, 33-byte DER INTEGER) and a full-width one round-trip as plain
DER, obfuscated `…V…` and plain `…V…`; profile write → load → export → import with the
one-zero scalar; upstream test vector (known UID); malformed inputs (truncated, trailing block,
non-SEQUENCE, empty SEQUENCE, wrong first item, public-only flag, empty / 3-bit / unused-bits >
length BIT STRING, missing or non-INTEGER private slot, negative, zero, empty, ≥ group order and
33-byte scalars) are errors. `cargo test --locked`: 37 unit + 3 stdio smoke pass.
- Throwaway evidence (not kept as tests): upstream `from_ts` fails on the fixed one-zero scalar;
100 000 random keys all round-trip through the fix, 387 had a zero top byte and exactly those
387 fail upstream.
- Release `0.1.2` (root `.cargo/config.toml`, static CRT, `-j 2`): 8969216 bytes, SHA256
`6c04f1e6595740f50628374b23216d00b9e7a5bfa5c8c5cba18a8b053eb9bb57`; imports only system DLLs
(ADVAPI32, IPHLPAPI, KERNEL32, api-ms-win-core-synch, bcrypt, bcryptprimitives, ntdll, oleaut32,
ws2_32); `npm run smoke:core` passes (create → restart → same UID).