cryptography not loading openssl certificate

with that I have created an openssl key and certificate

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

with that I would like to load the certificate

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

but failed with that exception

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

as far I can see I followed the documentation. How would I correctly create/use x509 certs with python >=3.8 ?


According to the documentation, load_pem_x509_certificate does not take a file name, but the certificate data as bytes.

This means you have to load the data from file first, and do so in binary mode (even though pem files contain text):

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