Repository navigation
der: reject nested trailing data in PemReader - #2555
Merged
tarcieri merged 1 commit intoNov 20, 2026
Merged
Conversation
`PemReader::read_nested` didn't check that the nested value was fully consumed, unlike `SliceReader::read_nested` since RustCrypto#2401. Because `Position::resume_nested` restores the input length but not the position, the outer decoder would then continue reading from inside the nested value. So `DecodePem::from_pem` accepted encodings that `Decode::from_der` rejects, and could decode fields from the wrong place, e.g. a `BIT STRING` inside an `AlgorithmIdentifier` as the `subjectPublicKey` of a `SubjectPublicKeyInfo`. Return `ErrorKind::TrailingData` when the closure returns `Ok` without consuming the whole nested value, like `SliceReader` does. Fixes RustCrypto#2554 Co-Authored-By: Claude Opus 5.5 <[email protected]>
basvandijk
marked this pull request as ready for review
November 20, 2026 08:46
tarcieri
approved these changes
Nov 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PemReader::read_nesteddidn't check that the nested value was fully consumed, unlikeSliceReader::read_nestedsince #2401. BecausePosition::resume_nestedrestores the input length but not the position, the outer decoder then continued reading from inside the nested value. SoDecodePem::from_pemaccepted encodings thatDecode::from_derrejects, and could decode fields from the wrong place.For example,
from_derrejects thisSubjectPublicKeyInfowithTrailingData { decoded: 11, remaining: 35 }, but on masterfrom_pemaccepts it and returns theBIT STRINGinside theAlgorithmIdentifieras thesubjectPublicKey:The example in #2554, which also has a real
subjectPublicKeyafter theAlgorithmIdentifier, is already rejected on master by the top-level check from #2531, but with a different error thanfrom_derreturns.This returns
ErrorKind::TrailingDatawhen the closure returnsOkwithout consuming the whole nested value, likeSliceReaderdoes.from_pemnow returns the same error asfrom_derfor both inputs;der/tests/pem.rstests both, and aPemReaderunit test mirrorsSliceReader'snested_trailing_data.As an extra check, I compared
from_derwithfrom_pemon about 2M mutated versions of the workspace's test fixtures, for the owned types that can be decoded from PEM. On master, about 11,000 of them were rejected byfrom_derbut accepted byfrom_pem. With this change there are none.Fixes #2554
🤖 Generated with Claude Code