Skip to content

der: reject nested trailing data in PemReader - #2555

Merged
tarcieri merged 1 commit into
RustCrypto:masterfrom
basvandijk:der/pem-reader-nested-trailing-data
Nov 20, 2026
Merged

tarcieri merged 1 commit into
RustCrypto:masterfrom
basvandijk:der/pem-reader-nested-trailing-data

Conversation

@basvandijk

Copy link
Copy Markdown
Contributor

PemReader::read_nested didn't check that the nested value was fully consumed, unlike SliceReader::read_nested since #2401. Because Position::resume_nested restores the input length but not the position, the outer decoder then continued 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.

For example, from_der rejects this SubjectPublicKeyInfo with TrailingData { decoded: 11, remaining: 35 }, but on master from_pem accepts it and returns the BIT STRING inside the AlgorithmIdentifier as the subjectPublicKey:

SEQUENCE {                       -- SubjectPublicKeyInfo
  SEQUENCE {                     -- AlgorithmIdentifier
    OBJECT IDENTIFIER 1.3.101.112
    NULL
    BIT STRING aa..aa            -- trailing data inside the AlgorithmIdentifier
  }
}

The example in #2554, which also has a real subjectPublicKey after the AlgorithmIdentifier, is already rejected on master by the top-level check from #2531, but with a different error than from_der returns.

This returns ErrorKind::TrailingData when the closure returns Ok without consuming the whole nested value, like SliceReader does. from_pem now returns the same error as from_der for both inputs; der/tests/pem.rs tests both, and a PemReader unit test mirrors SliceReader's nested_trailing_data.

As an extra check, I compared from_der with from_pem on 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 by from_der but accepted by from_pem. With this change there are none.

Fixes #2554

🤖 Generated with Claude Code

`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
basvandijk marked this pull request as ready for review November 20, 2026 08:46
@tarcieri
tarcieri merged commit 99dd99f into RustCrypto:master Nov 20, 2026
115 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

der: PemReader::read_nested doesn't reject trailing data in nested values

2 participants