Benjamin Italiaander / Linux / SSL / SSL Checks

Created Mon, 08 Feb 2016 00:00:00 +0000
272 Words

Regular SSL checks

Check a Certificate Signing Request (CSR)

openssl req -text -noout -verify -in CSR.csr

Check a private key

openssl rsa -in privateKey.key -check

Check a certificate

openssl x509 -in certificate.crt -text -noout

Check a PKCS#12 file (.pfx or .p12)

openssl pkcs12 -info -in keyStore.p12

Check whether a certificate and key are a pair

You can check whether a .crt and a .key file match by comparing an MD5 hash of their modulus. If both outputs are the same, you have a matching pair.

openssl rsa -modulus -noout -in ./certificate.key | openssl md5
openssl x509 -modulus -noout -in ./certificate.crt | openssl md5

Here is a small script to check whether the files match:

#!/bin/bash
if [ $# -eq 0 ]
then
    echo "usage: $(basename $0) certfile keyfile csrfile"
    exit
fi
cert=$1
key=$2
csr=$3
echo "$cert:"
openssl x509 -noout -modulus -in $cert | openssl md5
echo "$key:"
openssl rsa -noout -modulus -in $key | openssl md5
echo "$csr:"
test ! x$csr = x && openssl req -noout -modulus -in $csr | openssl md5

Check whether a certificate and key match (any key type)

The modulus trick above only works for RSA keys. Comparing the public keys works for every type (RSA, EC, Ed25519):

openssl pkey -in ./certificate.key -pubout | sha256sum
openssl x509 -in ./certificate.crt -pubkey -noout | sha256sum

Check the certificate of a live server

# who issued it, for which names, and until when
openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null \
  | openssl x509 -noout -subject -issuer -dates -ext subjectAltName

# does it expire within 30 days? (exit code 1 = yes)
openssl x509 -in certificate.crt -noout -checkend $((30*24*3600))