Openssl s_client: extract last certificate

opensslsed

I can ouput all the certification chain from a remote website by using the following command:

openssl s_client -connect www.google.fr:443 -servername www.google.fr -showcerts 2>/dev/null | sed --quiet '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p'

But now i would like to be able to extract only the last certificate in the chain, is there a simple way of doing this ?

Best Answer

Here is simple oneliner:

openssl s_client -connect www.google.fr:443 -servername www.google.fr -showcerts 2>/dev/null | sed --quiet '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' | csplit --prefix=outfile - "/-----END CERTIFICATE-----/+1" "{*}" --elide-empty-files --quiet

It's working on my CentOS, it's make three files outfile00, outfile01 and outfile02. So you could take certificate that you want from file.