wbond / oscrypto

Compiler-free Python crypto library backed by the OS, supporting CPython and PyPy
MIT License
318 stars 71 forks source link

Allow asn1crypto.keys.PublicKeyInfo when an instance of the Certificate or PublicKey class are required #65

Closed BrunoVernay closed 1 year ago

BrunoVernay commented 2 years ago

It does not seem logical to me to get this error TypeError: certificate_or_public_key must be an instance of the Certificate or PublicKey class, not asn1crypto.keys.PublicKeyInfo When trying to do this:

        info = cms.ContentInfo.load(f.read())
        pubkey = info['content']['certificates'][0].chosen.public_key
        asymmetric.rsa_pkcs1v15_verify(pubkey, signature_value, signed_data, 'sha256')

But maybe I am wrong (and I cannot find an easy way to convert PublicKeyInfo to PublicKey ....)

niklbird commented 1 year ago

Hey, I am trying to do the same thing, would you mind sharing how you solved this? :) Thank you!

wbond commented 1 year ago

This should do it:

public_key = oscrypto.keys.parse_public(pubkey.dump())

What you are doing is dumping the ASN.1 representation of the public key to a byte string and then loading that up into an OS-specific format that can be used with asymmetric.rsa_pkcs1v15_verify().

https://github.com/wbond/oscrypto/blob/master/docs/keys.md#parse_public-function