Skip to content

Require PEM armor for string keys in the python-ecdsa backend - #430

Open
neeraj1235476 wants to merge 1 commit into
mpdavis:masterfrom
neeraj1235476:ecdsa-require-pem-armor
Open

neeraj1235476 wants to merge 1 commit into
mpdavis:masterfrom
neeraj1235476:ecdsa-require-pem-armor

Conversation

@neeraj1235476

Copy link
Copy Markdown

With the default install (no cryptography), ECDSAECKey loads string keys through ecdsa.VerifyingKey.from_pem(), which drops every line starting with ----- and base64-decodes the rest. An ES256 public key passed as bare base64 DER, or under a label such as EC PUBLIC KEY, loads and verifies tokens normally. HMACKey only refuses keys that is_pem_format() / is_ssh_key() recognise, so the same public key string is also accepted as an HMAC secret. This is the HS256/public-key algorithm confusion from CVE-2024-33663, reached through a key format that check does not cover:

pub = "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE..."  # SPKI without BEGIN/END lines
jwt.decode(es256_token, pub, algorithms=["ES256", "HS256"])  # ok
forged = h + "." + p + "." + b64(hmac.new(pub.encode(), (h + "." + p).encode(), sha256).digest())
jwt.decode(forged, pub, algorithms=["ES256", "HS256"])  # {'sub': 'admin'}, also with algorithms=None

The check sits in the EC loader because the cryptography backend already refuses unarmored keys, and requiring is_pem_format() there means every key either EC backend accepts is one HMACKey rejects. The new test fails on master and passes with the change. With and without cryptography, the rest of the suite gives the same results as master. The one failure, test_incorrect_public_key_hmac_signing, also fails on master with cryptography 50 (TypeError: curve must be an EllipticCurve instance).

ecdsa.VerifyingKey.from_pem() ignores the armor lines and decodes bare
base64 DER, so ECDSAECKey accepted EC public keys that HMACKey does not
recognise as asymmetric. The same public key string could then be used
as an HS256 secret to forge tokens. Only load PEM-armored keys, as the
cryptography backend already does.
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.

1 participant