I'm trying to make a script that will test if a website is using a non-self-signed certificate valid for its domain. Meaning it would not show a warning in browser.
I have tried with
openssl s_client -connect www.example.com:443 -servername www.example.com
Even though the certificate is not valid for the domain, I still get :
Verify return code: 0 (ok)
Any idea how I can achieve that ?
If you want openssl to actually verify the certificate, you need to tell it to do so.
1. Checking whether the hostname on the certificate matches the name you want
There's a specific option for that,
-verify_hostname
. In the command below, I use it on serverfault.com but I'm checking against the hostname example.com:However, the return code of the process itself is still 0, meaming you have to look at the output instead of using the return code in a test.
2. Checking whether the certificate is from a trusted CA
I've run it against the serverfault.com website, without giving it a list of trusted CA:s to check again so that it would be guaranteed to fail verification:
However, openssl will still give you the return code 0, since the command actually executed properly, making it harder to script around.
A better way to do it would be to first download the certificate and then run
openssl verify
against it:As you see, I got the return code 18 which means "self-signed certificate". There are a number of other error codes; check the man page for
verify
for more info.