Python SSL CERTIFICATE_VERIFY_FAILED - python

I'm using the following code to interact with a Magento webstore using the XMLRPC api. Magento API Python XMLRPC
Everything was working ok until we made a change on our web server to SSL
Now I'm getting the following error.
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)
I'm not sure why the certificate is failing as we have an EV certificate and all browsers are showing this as ok.
My connection string is:
How can I resolve this / over-ride the code
I'm fairly new to Python so please go easy :o)
magento = MagentoAPI("www.website.co.uk", 443, "myUsername", "myPassword", "/api/xmlrpc", True)

Python, or better the OpenSSL library it is using, can not verify the validity of the certificate of the server. There are many possible reasons: bad configuration, missing intermediate or CA certificate, wrong CN...
A first step could be to go to this site and let it test the SSL/TLS capabilities of the server: https://www.ssllabs.com/ssltest/
It will give you hints on how to solve problems as well.

Python verifies certs via its own bundle, check where it is located by
>>> import certifi
>>> certifi.where()
'/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-
packages/certifi/cacert.pem'
and add your certificates to the end of that file.

Related

How to find out what this error means: [SSL: SSLV3_ALERT_HANDSHAKE_FAILURE] sslv3 alert handshake failure (_ssl.c:1129)

From a python app attempting to make an HTTPS request I'm hitting an error which I'm struggling to understand:
[SSL: SSLV3_ALERT_HANDSHAKE_FAILURE] sslv3 alert handshake failure (_ssl.c:1129)
I presume this is coming from OpenSSL. But besides the handshake failing, it doesn't tell me what the problem is. IE: it doesn't tell me why the handshake failed or what step of it failed.
Various links on google give things to try, but give no reason why they might work. I have tried them but nothing worked so far. Based on these results I have tried:
upgrading certifi
running the certificate install script in mac python install directory
upgrading the python version
Is there any way to get more information about this error?
Steffen Ullrich pointed out that this error is server-side. This may be useful to others debugging. For us the problem was that we were not sending a client certificate when we were supposed to be.
For now, just a work around - folks on this fourm led me to try
compare openssl ciphers (identical in both my good and bad environments)
openssl s_client -connect news.somewhere.com:563 (worked)
then add the following python code based on output from the above:
import ssl
cntxt = ssl.create_default_context()
cntxt.set_ciphers("AES256-GCM-SHA384")
import nntplib
nntp = nntplib.NNTP_SSL('news.somewhere.com', ssl_context=cntxt)
which solved the SSLV3_ALERT_HANDSHAKE_FAILURE I was getting. (You will want to select the cipher that worked in that s_client test connection to your server.)
There are a dozen library and module version differences between my two environments, but in general the old environment uses python-3.9.7 and the new (broken but now running environment) uses python-3.10.5
Hopefully this narrows things down a bit.

Azure Functions: SSL: CERTIFICATE_VERIFY_FAILED

I am trying to post a data to a REST API but it is throwing the below error:
[SSL: CERTIFICATE_VERIFY_FAILED]
certificate verify failed: unable to get local issuer certificate
If I hit the REST API url using the curl --insecure dummyurl.com then it is returning something.. but I my aim is to hit the url using the azure functions only.
So my question is-- Should I seek certificate from the REST API owner?? or I have to integrate --insecure to my url ??
I am using python for the development.
Thanks for the help.
Hi you can add/manage the TLS (private/public cert) under App services depend on your TLS scenarios , refer to Add and Manage TLS cert , alternative you can disable the SSL verification in your python code but not recommended in your actual environment.

SSL Client Authentication with Python requests

I'm using Python's requests library to perform client side authentication with certificates. The scenario is the following: CA1 has issued a certificate for an intermediate CA (CA2) and CA2 has issued my client's certificate CLIENT. The server I'm connecting to trusts CA1's cert (but does not have CA2's cert). When I use:
requests.get('https:..', cert=('/path/CLIENT.cert', '/path/CLIENT.key'))
I get an error "certificate verify failed". I assumed that's because the server can not retrieve CA2's cert.
However, I'm unable to find a way to send CA2's cert to the server. If I include it in CLIENT, I get an error about private key and cert mismatch. I have also tried to include the chain of certificates in the verify parameter but there does not seem to be any difference on the result (as far as I understand, certs in the verify parameter are used for server side authentication).
Although I think this must be a quite common scenario, I'm unable to find a solution...
PD: If I verify CLIENT's cert with openssl and the full chain of certificates the validation is successful (so there is no problem with the certificates themselves).
Requests recommends using certifi as a CA bundle. Have you tried installing certifi, adding CA1, and passing the certifi bundle path to requests?

HTTPS TSL Certificate Chain Validation Using Python Requests

I'm running a windows service using python 2.7.9.
As part of it i'm trying to connect to a server using HTTPS.
I'm using requests model (2.7.0) to do it.
I'm also using wincertstore (0.2) model to read windows certificate store and use it as the CA.
the server certificate is singed using an intermediate certificate in the following order -
Root is "Go Daddy Root Certificate Authority - G2"
Intermediate is "Go Daddy Secure Certificate Authority - G2"
The server certificate "*.demoserver.com"
My problem is that the certificate validation fails with the following error - SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581).
Here is the code i'm using:
import requests
import wincertstore
ca = wincertstore.CertFile()
ca.addcerts('ROOT')
ca.addcerts('CA')
requests.get('https://server.demoserver.com', verify=ca.name)
If I open it on Chrome\Firefox\IE the verification is successful.
I did notice the following behavior:
On a fresh OS, if i open the server using a browser for the first time the intermediate certificate ("Go Daddy Secure Certificate Authority - G2") will be added to Windows certificate store, under that user, under Intermediate Certification Authorities.
If then I will run the code above from a python console the validation will work, since the certificate was added to windows store.
However, since my code is run as a service, and that means using SYSTEM user and local machine store, the certificate won't be there and the validation will fail.
My question is how do I make it work? How can I tell python to check the entire chain, I think it checks the server certificate, sees only one level up (the intermediate certificate), doesn't recognizes it and fails, even though the root certificate is found in the system store also.
I also tried using certifi as the CA which also fails

Python - SSL Client/Server Certificate

I want to create a Client/Server architecture following the python documentation.
This works well with my self-signed certificates in one pem file. (ca_root root_key ca_intermediate intermediate_key)
So my next plan is to create client certificates which can be revoked by the server if the client isn't trustworthy anymore.
So the way to go is that i create a certificate, signed with the intermediate key, and hand it out to the client.
But i still get
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)
Do i need to export the whole certificate chain to the client? This seems odd to me.
Thanks!
Most likely you're entire chain isn't in your .pem file. Just copy the text from the certificates in the right order into a single .pem file and attempt that. If that doesn't work you may be getting this error because of your server configuration. More information is need thought to provide a better answer.

Categories