0

I am developing a Python 3.4 application component which checks if a URL's certificate exists in the CRL provided by its CA. I am using a cryptography package to load a certificate as well as the CRL. Below is the section of the code;

from cryptography import x509  
from cryptography.hazmat.backends import default_backend
from cryptography.x509.oid import ExtensionOID
from cryptography.x509.oid import NameOID
import urllib.request

URL = "www.xxx.com"
cert_str = ssl.get_server_certificate((URL,443))
pem_data = cert_str.encode()  
cert = x509.load_pem_x509_certificate(pem_data, default_backend())
crlDistrPoints = cert.extensions.get_extension_for_oid(ExtensionOID.CRL_DISTRIBUTION_POINTS)
crlURL = crlDistrPoints.value.full_name[0].value 
crlFile = "/path...." 
urllib.request.urlretrieve(crlURL,crlFile) # downloading a .crl file and save as crlFile
# Need to convert a crlFile to PEM format for pem_crl_data below
crl = x509.load_pem_x509_crl(pem_crl_data, default_backend())

The code downloads a CRL file from the site "crlURL" and stores it locally as crlFile. The file has .crl extension. This file has to be converted to PEM format (and assigned to pem_crl_data) to get the crl object "crl". How can I do the conversion (without even saving the file locally)?

popNT
  • 31
  • 3

1 Answers1

0

Use the crypto module from pyOpenSSL:

from OpenSSL import crypto

then use this piece of code:

with open(crlFile, "rb") as in_file:    
    crl_obj = crypto.load_crl(crypto.FILETYPE_ASN1, in_file.read())
    pem_crl_data = crypto.dump_crl(crypto.FILETYPE_PEM, crl_obj)
JohnZ
  • 1
  • 1