Converting a Certificate Revocation List (CRL) file from .crl to .pem extension - Python 3

折月煮酒 提交于 2021-02-08 11:38:45

问题


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)?


回答1:


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)


来源:https://stackoverflow.com/questions/51681409/converting-a-certificate-revocation-list-crl-file-from-crl-to-pem-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!