Skip to content

Validate PEM format in Certificate::from_pem for rustls#2970

Open
veeceey wants to merge 1 commit intoseanmonstar:masterfrom
veeceey:fix/issue-1858-certificate-from-pem-validation
Open

Validate PEM format in Certificate::from_pem for rustls#2970
veeceey wants to merge 1 commit intoseanmonstar:masterfrom
veeceey:fix/issue-1858-certificate-from-pem-validation

Conversation

@veeceey
Copy link
Copy Markdown

@veeceey veeceey commented Feb 23, 2026

Fixes #1858

Certificate::from_pem with the rustls backend was silently accepting DER-encoded input, only failing later during the TLS handshake with a confusing error. This happened because the rustls path just stored the raw bytes without any validation.

Now it eagerly parses the PEM data upfront using rustls_pemfile::certs and returns a clear error if no valid PEM certificates are found. The native-tls backend already validated eagerly, so this brings rustls in line.

Added two tests to verify the behavior:

  • certificate_from_pem_invalid_rustls - rejects garbage input
  • certificate_from_pem_rejects_der - rejects DER-encoded bytes
running 7 tests
test tls::tests::certificate_from_pem ... ok
test tls::tests::certificate_from_pem_invalid_rustls ... ok
test tls::tests::certificate_from_pem_rejects_der ... ok
test tls::tests::certificate_from_der ... ok
test tls::tests::identity_from_pkcs12_der ... ok
test tls::tests::identity_from_pkcs8_pem ... ok
test tls::tests::identity_from_pem ... ok

Certificate::from_pem with the rustls backend was silently accepting
DER-encoded input. The bytes were stored as-is and only failed later
when added to the root certificate store, making it hard to debug.

Now eagerly validates that the input contains at least one valid PEM
certificate at construction time, matching the behavior of the
native-tls backend which already rejects non-PEM input.

Fixes seanmonstar#1858
@veeceey
Copy link
Copy Markdown
Author

veeceey commented Mar 10, 2026

friendly bump! happy to make changes if anything needs updating

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.

reqwest::Certificate::from_der and reqwest::Certificate::from_pem are inconsistent

1 participant