Skip to content

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

Description

@davidben

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

Activity

  1. added a commit that references this issue on Dec 20, 2022
  2. added a commit that references this issue on Mar 24, 2023
  3. added a commit that references this issue on Mar 27, 2023
  4. added a commit that references this issue on Apr 11, 2023
  5. bh1428 commented on Sep 15, 2023

    @bh1428

    @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...

  6. hugovk commented on Mar 15, 2024

    @hugovk
    Member

    Closing per #100373 (comment).

    Thanks for the issue and PR!

  7. added 2 commits that reference this issue on Aug 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-SSLtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions