密码学不加载 openssl 证书

cryptography not loading openssl certificate

我已经创建了一个 openssl 密钥和证书

openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout test.key -out test.cert

我想加载证书

from cryptography import x509
cert = x509.load_pem_x509_certificate('test.cert')

但因该异常而失败

line 636, in _bytes_to_bio
data_ptr = self._ffi.from_buffer(data)
TypeError: from_buffer() cannot return the address of a unicode object

据我所知,我遵循了文档。我如何使用 python >=3.8 正确 create/use x509 证书?

根据documentationload_pem_x509_certificate不取文件名,但证书数据为bytes

这意味着您必须首先从文件加载数据,并以二进制模式执行(即使 pem 文件包含文本):

with open('test.cert', 'rb') as certfile:
    certbyes = certfile.read()
    cert = x509.load_pem_x509_certificate(certbytes)