Search code examples
pythonopensslcertificate

OpenSSL crypto error: [('PEM routines', 'PEM_read_bio', 'no start line')]


I am getting the following error when trying to read a certificate:

OpenSSL.crypto.Error: [('PEM routines', 'PEM_read_bio', 'no start line')]

when running OpenSSL.crypto.load_certificate(FILETYPE_PEM, filename). I have made some research but was not able to find an answer specific to my case.

I tried checking if the file existed with os.path.isfile(filename) which returns True, but loading the certificate ONLY raises the error above.

Also, when executing on the terminal openssl X509 -in file.pem, it works like a charm.

The file.pem looks like this:

-----BEGIN CERTIFICATE-----
<<sensitive data>>
-----END CERTIFICATE-----

It seems to be valid since I able to perform basic openssl operations on the terminal. I am running CentOS 7, if that helps.

Any ideas?

Thanks!


Solution

  • According to http://www.pyopenssl.org/en/stable/api/crypto.html#OpenSSL.crypto.load_certificate,load_certificate() takes a buffer (string will do) containing the certificate, not a filename.

    Your need to do:

    with open(filename, "r") as my_cert_file:
        my_cert_text = my_cert_file.read()
        cert = load_certificate(FILETYPE_PEM, my_cert_text)