Thanks to visit codestin.com
Credit goes to github.com

Skip to content

SSLContext.load_verify_locations accepts some cases of trailing data in DER #100372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
davidben opened this issue Dec 20, 2022 · 2 comments
Closed
Labels
stdlib Python modules in the Lib dir topic-SSL type-bug An unexpected behavior, bug, or error

Comments

@davidben
Copy link
Contributor

davidben commented Dec 20, 2022

Bug report

SSLContext.load_verify_locations detects EOF by looking for PEM_R_NO_START_LINE and ASN1_R_HEADER_TOO_LONG in PEM and DER, respectively. The former is correct. PEM allows arbitrary trailing data before and after and the OpenSSL API involves looking for that particular error code.

ASN1_R_HEADER_TOO_LONG, however doesn't appear anywhere in OpenSSL's documentation and isn't the right way to detect EOF for a sequence of DER elements. It's signaled whenever there weren't enough bytes to read a full ASN.1 header. That could be because of EOF, but it could also be there was one byte, or any other truncated ASN.1 header. (It could also happen inside a deeply nested ASN.1 structure, but OpenSSL happens to push ERR_R_NESTED_ASN1_ERROR in that case, so that case doesn't confuse CPython.)

To repro, add this test to test_load_verify_cadata. It should fail.

       with self.assertRaises(ssl.SSLError):
           ctx.load_verify_locations(cadata=cacert_der + b"A")

The fix is instead to stop at BIO_eof for DER, as there's no need to skip surrounding data. I'll upload a PR shortly to do that.

Your environment

  • CPython versions tested on: main
  • Operating system and architecture: Linux, x86_64

Linked PRs

@davidben davidben added the type-bug An unexpected behavior, bug, or error label Dec 20, 2022
davidben added a commit to davidben/cpython that referenced this issue Dec 20, 2022
In PEM, we need to parse until error and then suppress
PEM_R_NO_START_LINE, because PEM allows arbitrary leading and trailing
data. DER, however, does not. Parsing until error and suppressing
ASN1_R_HEADER_TOO_LONG doesn't quite work because that error also
covers some cases that should be rejected.

Instead, check BIO_eof early and stop the loop that way.
@arhadthedev arhadthedev added stdlib Python modules in the Lib dir topic-SSL labels Feb 3, 2023
miss-islington pushed a commit that referenced this issue Mar 24, 2023
In PEM, we need to parse until error and then suppress `PEM_R_NO_START_LINE`, because PEM allows arbitrary leading and trailing data. DER, however, does not. Parsing until error and suppressing `ASN1_R_HEADER_TOO_LONG` doesn't quite work because that error also covers some cases that should be rejected.

Instead, check `BIO_eof` early and stop the loop that way.

Automerge-Triggered-By: GH:Yhg1s
Fidget-Spinner pushed a commit to Fidget-Spinner/cpython that referenced this issue Mar 27, 2023
…honGH-100373)

In PEM, we need to parse until error and then suppress `PEM_R_NO_START_LINE`, because PEM allows arbitrary leading and trailing data. DER, however, does not. Parsing until error and suppressing `ASN1_R_HEADER_TOO_LONG` doesn't quite work because that error also covers some cases that should be rejected.

Instead, check `BIO_eof` early and stop the loop that way.

Automerge-Triggered-By: GH:Yhg1s
warsaw pushed a commit to warsaw/cpython that referenced this issue Apr 11, 2023
…honGH-100373)

In PEM, we need to parse until error and then suppress `PEM_R_NO_START_LINE`, because PEM allows arbitrary leading and trailing data. DER, however, does not. Parsing until error and suppressing `ASN1_R_HEADER_TOO_LONG` doesn't quite work because that error also covers some cases that should be rejected.

Instead, check `BIO_eof` early and stop the loop that way.

Automerge-Triggered-By: GH:Yhg1s
@bh1428
Copy link

bh1428 commented Sep 15, 2023

@Yhg1s: I'm having an issue in 3.11.5 (#108721) which doesn't occur in 3.12 (or main) and is solved by this PR.
I would like to request a backport of this PR to 3.11.

I'm not sure about the process around backports. Do you need a new PR? Is this normally done using this PR? If needed: I can create a new PR...

bh1428 added a commit to bh1428/cpython that referenced this issue Sep 15, 2023
In PEM, we need to parse until error and then suppress `PEM_R_NO_START_LINE`, because PEM allows arbitrary leading and trailing data. DER, however, does not. Parsing until error and suppressing `ASN1_R_HEADER_TOO_LONG` doesn't quite work because that error also covers some cases that should be rejected.

Instead, check `BIO_eof` early and stop the loop that way.
@hugovk
Copy link
Member

hugovk commented Mar 15, 2024

Closing per #100373 (comment).

Thanks for the issue and PR!

@hugovk hugovk closed this as completed Mar 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir topic-SSL type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants