ssl.SSLError: [SSL] PEM lib (_ssl.c:4044) while connecting through Python Code

322 views Asked by At

I have received x509 certificate (certificate.pem file) containing Private Key and Certificate in same file. I am trying to establish the connection using Python code through SSL but unfortunately getting below error :

ssl.SSLError: [SSL] PEM lib (_ssl.c:4044)

Python version I am using : 3.9.13 Open SSL Python has : 1.1

Certificate.pem entry :

-----BEGIN ENCRYPTED PRIVATE KEY-----
<...............\>
-----END ENCRYPTED PRIVATE KEY-----
-----BEGIN CERTIFICATE-----
<..........\>
-----END CERTIFICATE-----

I have reviewed the documentation at https://docs.python.org/3/library/ssl.html#ssl.SSLContext.load_cert_chain and not sure if Encrypted Private key has anything to do with this error.

Below is the code snippet I am using and it is throwing an error at line context.load_cert_chain

        context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
        context.load_cert_chain(certfile=certificate_path)
        # Make a GET request to the API
        headers = {
            'User-ID': user_id,
            'API-Key': api_key,
            'Content-Type': 'application/json',
        }
        response = requests.get(api_url, headers=headers, verify=certificate_path)

I have verified the software versions, Key and certificate, path and permission of certificate file, reviewed the _ssl.c file at https://github.com/python/cpython/blob/main/Modules/_ssl.c but could not able to understand the actual issue here.

I have already gone through many similar questions posted on this as well as on other portal but nothing is much help and therefore it would be very helpful if any help is granted on this matter.

Thank you!

0

There are 0 answers