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

Wrong type annotation for is_signature_valid #12301

Open
userappgate opened this issue Jan 17, 2025 · 1 comment
Open

Wrong type annotation for is_signature_valid #12301

userappgate opened this issue Jan 17, 2025 · 1 comment
Labels
waiting-on-reporter Issue is waiting on a reply from the reporter. It will be automatically cloesd if there is no reply.

Comments

@userappgate
Copy link

See this example code:

from cryptography import x509

def is_signature_valid(crl: bytes, ca_cert: bytes) -> bool:
    crl_object = x509.load_pem_x509_crl(crl)

    ca_cert_object = x509.load_pem_x509_certificate(ca_cert)

    return crl_object.is_signature_valid(ca_cert_object.public_key())

mypy fails with

test.py:8: error: Argument 1 to "is_signature_valid" of "CertificateRevocationList" has incompatible type "DSAPublicKey | RSAPublicKey | EllipticCurvePublicKey | Ed25519PublicKey | Ed448PublicKey | X25519PublicKey | X448PublicKey"; expected "DSAPublicKey | RSAPublicKey | EllipticCurvePublicKey | Ed25519PublicKey | Ed448PublicKey"  [arg-type]

I noticed that while migrating the codebast on ubuntu noble (that uses 41.0.7-4ubuntu0.1), while on ubuntu jammy (that uses 3.4.8-1ubuntu2.2) mypy does not complain.

The codebase is quite different, but the code seems to work on both versions so I think the mistake is in the type annotations (unless I am doing something wrong, of course).

Thanks

@alex
Copy link
Member

alex commented Jan 17, 2025

Unfortunately, that's correct.

A certificate can carry a public key that's not valid for issuing certificates, and the type signatures reflect this.

You can use https://cryptography.io/en/latest/hazmat/primitives/asymmetric/#cryptography.hazmat.primitives.asymmetric.types.CertificateIssuerPrivateKeyTypes to check for this (either with typing.cast() or an isinstance check.)

@alex alex added the waiting-on-reporter Issue is waiting on a reply from the reporter. It will be automatically cloesd if there is no reply. label Jan 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting-on-reporter Issue is waiting on a reply from the reporter. It will be automatically cloesd if there is no reply.
Development

No branches or pull requests

2 participants