I'd like to print all x509 information from a certificate. Here's what have been done:
Following the official python doc for ssl, I created a socket client for IPv4/6 dual stack. And added the certificate bundle from www.python.org. The certchain is downloaded and saved to separate files using instructions from another SO about local issuer error message This client works. I added the code "
context.load_verify_locations('path/to/cabundle.pem')
" to the sample. Thus the certificates are correct and valid.Based on the answer from the SO for printing x509, the code snippet is: "
import asn1tools; foo = asn1tools.compile_files("x509.asn"); output = foo.decode("Certificate", cert)
". This needs an asn file.The another SO about ASN.1 for X.509 suggested to download an asn from rfc, or from the ITU page for "ITU-T X.509 (08/1997) Recommendations". I've downloaded the zip and extracted three files:
AlgorithmObjectIdentifiers.asn
,AuthenticationFramework.asn
,CertificateExtensions.asn
. Since the first file imports elements in other files, I guess it is the top-level file, so I give it to the code.foo = asn1tools.compile_files("AlgorithmObjectIdentifiers.asn") output = foo.decode("Certificate", cert) print("Cert output: ", output)
It's spiting out an error:
File ".../test-print-ssl.py", line 47, in cert_print_asn1
output = foo.decode("Certificate", cert)
File "...\lib\site-packages\asn1tools\compiler.py", line 161, in decode
"Type '{}' not found in types dictionary.".format(name))
asn1tools.errors.DecodeError: Type 'Certificate' not found in types dictionary.
A few questions:
- Where can you find a valid asn file to be used in this code?
- Is there another way to use the downloaded asn files? Since those are three files, do I need to combine them? How do you combine them?
- How to debug this issue further?
Note that I've used command openssl x509 -in website.com.pem -text
to print the same cert from file. That works. I guess I can invoke using os.system()
to achieve the same. I'd like to have a solution to decode and print in python.
I've seen the question that failing compilation, but in my case it is failing decoding, that is different.