Repository navigation
Commit 1604e8c
authored
fix(crypto): reading and writing a private key as PEM zeroizes every buffer (#638)
## What and why
Exporting a private key as PEM could leave copies of the key in freed
memory. `Pem::encode` built the key's base64 and its 64-character lines
in plain `String`s that weren't zeroized when dropped. Only the final
`private_key_pem` result was `Zeroizing`, and `format!` built that
result by growing a buffer, so each reallocation freed an earlier
partial copy without wiping it.
Now the base64 is `Zeroizing`, and the PEM is written into one
`Zeroizing<String>` allocated at its exact final size, so it never
reallocates. `Pem::encode` returns that buffer, `private_key_pem`
returns it unchanged, and `PublicKey::pem` copies the public key's PEM
out into the `String` it already returned. The public API doesn't
change.
Decoding had the same gap: `Pem::decode` collected the base64 with
`split_whitespace().collect()`, which reallocates as it grows. It now
gathers it into a `Zeroizing` buffer allocated at the body's size.
Closes #630
## Testing
**Why no test fails on the leak itself:** whether freed memory was wiped
can't be observed from safe Rust. Instead, the new
`private_key_pem_writes_back_the_pem_it_was_read_from` test checks two
things:
- **The output is byte-identical:** the SDK vectors' `key-2025.pem`
fixture (written by `openssl`), read and exported again, comes back
exactly as the file. This already passed on `main`, so the change
doesn't alter the bytes.
- **The PEM buffer never reallocated:** its capacity equals its length.
On `main` this fails (`left: 3356, right: 1704`), because `format!` grew
the result, leaving freed copies. With the fix it passes.
Also:
- `just test -p sourceorigin-crypto --all-features`: 18/18 pass,
including the existing PEM round trip in
`jwt_persistence_and_trust_boundaries` and the SDK vectors.
- Clippy with and without `--all-features` (`-D warnings`), rustdoc with
`-D warnings`, `just rust unused`: clean.
- `cargo semver-checks -p sourceorigin-crypto --baseline-rev
origin/main`: 202 checks pass, no semver update required.
## Checklist
- [x] Every surface gets the feature: no surface changes.
- [ ] `just validate` passes: I ran the package-scoped checks above, not
the whole workspace.
- [x] Generated files are regenerated in the same commit: none are
affected.
> [!NOTE]
> Written by Claude Opus 5.5 in Claude Code and reviewed by Claude Opus
5.5 (via T3 Code)
🤖 Generated with [Claude Code](https://claude.com/claude-code)1 parent 0053454 commit 1604e8c
2 files changed
Lines changed: 38 additions & 13 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
125 | 125 | | |
126 | 126 | | |
127 | 127 | | |
| 128 | + | |
| 129 | + | |
128 | 130 | | |
129 | 131 | | |
130 | 132 | | |
| |||
244 | 246 | | |
245 | 247 | | |
246 | 248 | | |
247 | | - | |
248 | | - | |
249 | | - | |
250 | | - | |
| 249 | + | |
251 | 250 | | |
252 | 251 | | |
253 | 252 | | |
| |||
423 | 422 | | |
424 | 423 | | |
425 | 424 | | |
426 | | - | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
427 | 428 | | |
428 | 429 | | |
429 | 430 | | |
430 | 431 | | |
431 | | - | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
432 | 436 | | |
433 | 437 | | |
434 | 438 | | |
435 | | - | |
436 | | - | |
437 | | - | |
438 | | - | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
439 | 451 | | |
440 | 452 | | |
441 | 453 | | |
442 | 454 | | |
443 | | - | |
444 | | - | |
| 455 | + | |
| 456 | + | |
445 | 457 | | |
446 | 458 | | |
447 | | - | |
| 459 | + | |
| 460 | + | |
448 | 461 | | |
449 | 462 | | |
450 | 463 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
154 | 154 | | |
155 | 155 | | |
156 | 156 | | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
0 commit comments