Skip to content

reject encrypted PEM DEK-Info IVs of the wrong length - #15965

Open
007bsd wants to merge 1 commit into
pyca:mainfrom
007bsd:pem-iv-length
Open

007bsd wants to merge 1 commit into
pyca:mainfrom
007bsd:pem-iv-length

Conversation

@007bsd

@007bsd 007bsd commented Nov 5, 2026

Copy link
Copy Markdown

load_pem_private_key() raises PanicException for an encrypted
traditional PEM whose DEK-Info IV is 8 to 15 bytes long with an AES
cipher:

  • decrypt_pem only checks that the IV has at least 8 bytes (the key
    derivation salt), then passes it to openssl::symm::decrypt
  • rust-openssl asserts iv_len <= iv.len(), so a 16-byte-IV cipher
    with a shorter IV panics instead of returning an error
  • PanicException is not an Exception subclass, so callers catching
    ValueError or Exception for malformed key files do not catch it

Now checks the IV length against the cipher's IV length and returns the
existing PemInvalidIv error (ValueError), whose message now also
mentions the length. Includes a regression test (fails without the
fix) and a changelog entry.

AI assistance was used for the analysis and the patch; reproduced and
checked by hand.

A legacy encrypted PEM whose DEK-Info IV was 8-15 bytes long for an
AES cipher passed the existing 8-byte check (needed for the key
derivation salt) and then hit an assertion in rust-openssl, raising
PanicException instead of ValueError. Check that the IV length matches
the cipher's IV length before using it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant