Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple DER Cert Parsing in python

Which is the best way to parse with python a binary file with X509 Certificate in DER format to extract public key.

like image 876
David A Avatar asked Sep 14 '13 22:09

David A


1 Answers

The above answers are somewhat old (as of 2017).

You can use asn1crypto to do this in a nicer way:

from asn1crypto.x509 import Certificate

with open("mycert.der", "rb") as f:
    cert = Certificate.load(f.read())

n = cert.public_key.native["public_key"]["modulus"]
e = cert.public_key.native["public_key"]["public_exponent"]

print("{:#x}".format(n))    # prints the modulus (hexadecimal)
print("{:#x}".format(e))    # same, for the public exponent

It's relatively new (from what I can see, mid-2015), provides a nicer interface than the libraries already mentioned, and is much faster than pyasn1 according to the author.

like image 187
De117 Avatar answered Oct 02 '22 15:10

De117