indygreg / indygreg/cryptography-rs
Integer overflow panic in GeneralizedTime fractional seconds parsing
- Dominant language
- Rust
- Stars
- 21
- Forks
- 32
- PR merge metrics
- No merged PRs in 30d
Description
## Summary
The ASN.1 time parser panics when parsing `GeneralizedTime` values with more than 9 fractional second digits, due to integer underflow in the padding calculation.
## Location
`x509-certificate/src/asn1time.rs` line 216
## Bug Description
When parsing fractional seconds in GeneralizedTime values, the code computes padding to extend fractional seconds to exactly 9 digits (nanosecond precision):
```rust
let digits_count = nondigit_offset - 1;
// ...
digits.extend(std::iter::repeat_n('0', 9 - digits_count)); // ← Panics if digits_count > 9
```
If `digits_count > 9`, the expression `9 - digits_count` causes integer underflow, resulting in a panic.
## How to Reproduce
Parse any X.509 certificate with a GeneralizedTime containing >9 fractional second digits:
```
20220130204612.2013600332201~303Z
```
This input has 13 fractional digits (.`2013600332201`), causing `digits_count = 13` and triggering the underflow.
## Impact
**Severity: Medium** - Denial of Service
- Any application parsing X.509 certificates from untrusted sources can be crashed
- Affects certificate validation in TLS libraries and PKI infrastructure
- The documentation explicitly states the ASN.1 parser isn't hardened against malicious inputs, making this a known risk area
## Expected Behavior
The parser should return an error when fractional seconds exceed the valid precision, rather than panicking.
## Discovered By
Found through fuzzing with libFuzzer/cargo-fuzz as part of security testing.
## Fix
I have a fix ready that adds a bounds check before the subtraction. Opening a PR shortly.
Contributor guide
No contributing guide indexed for this repository
Research direction
Start in x509-certificate/src/asn1time.rs around line 216 and reproduce the panic with the GeneralizedTime value 20220130204612.2013600332201~303Z. Verify that fractional seconds longer than 9 digits return an error instead of panicking, and check the parser behavior against the expected invalid-precision case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- cryptography, security
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100