Skip to content
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

Support x5c chains terminating with an EC key #343

Open
Zicchio opened this issue Jan 28, 2025 · 0 comments
Open

Support x5c chains terminating with an EC key #343

Zicchio opened this issue Jan 28, 2025 · 0 comments
Assignees
Labels
enhancement Something improving existing features standardization

Comments

@Zicchio
Copy link
Collaborator

Zicchio commented Jan 28, 2025

Current strategy for "self contained" key extraction from jwt header does not support x5c chain that represent an EC key. Only RSA is supproted.

def parse_key_from_x5c(x5c: list[str]) -> JWK:
"""Parse a key from an x509 chain. This function currently
support only the parsing of public RSA key from such a chain.
The first element of the chain will contain the verifying key.
See RFC7517 https://datatracker.ietf.org/doc/html/rfc7517#section-4.7
"""
public_key = import_rsa_key(x5c[0])
key_dict = RSAKey(pub_key=public_key).to_dict()
return JWK(key_dict)

This would be the fixing code (that should be tested)

def parse_key_from_x5c(x5c: list[str]) -> JWK:
    """Parse a key from an x509 chain. This function currently
    support only the parsing of public RSA and EC key from such a chain.
    The first element of the chain will contain the verifying key.
    See RFC7517 https://datatracker.ietf.org/doc/html/rfc7517#section-4.7
    """
    try:
        # maybe RSA?
        public_key = import_rsa_key(x5c[0])
        key_dict = RSAKey(pub_key=public_key).to_dict()
        return JWK(key_dict)
    except Exception:
        # maybe EC?
        public_key = import_ec_key(x5c[0])
        key_dict = ECKey(pub_key=public_key).to_dict()
        return JWK(key_dict)
    except Exception:
        # neither RSA nor EC
        raise InvalidJwk(f"unable to parse key from x5c: {x5c}")
@Zicchio Zicchio added enhancement Something improving existing features standardization labels Jan 28, 2025
@Zicchio Zicchio self-assigned this Jan 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Something improving existing features standardization
Projects
None yet
Development

No branches or pull requests

1 participant