4
I know that I can dump the entire information from a PEM certificate file with this command:
openssl x509 -in certfile -noout -text
And I've already found another direct parameter to show me only the expiry date of a certificate:
openssl x509 -in certfile -noout -enddate
But is there also a shortcut to get only the alternative names? Like when a certificate can be used for example.com as well as www.example.com. In the full dump, it's here:
Certificate:
Data:
X509v3 extensions:
X509v3 Subject Alternative Name:
DNS:www.example.com, DNS:example.com
I'd just like to save me the hassle to parse this output and get the domain names only. Is that possible? Otherwise, what would be best practices to parse this output? What can be assumed, what may change? Could I use a regexp like X509v3 Subject Alternative Name:\s*DNS:(\S+)(?:, DNS:(\S+))*
?
2I think it's worth pointing out that "tr -d "DNS:" can be dangerous, it means delete all instances of the characters D,N,S and :, anywhere, in any order, in the input. It does NOT delete "DNS:" as a complete string. For example, echo "MY DOMAIN NAME SERVER: bla" | tr -d "DNS:" would output "MY OMAI AME ERVER bla" – Hamid – 2016-02-25T13:22:33.940
I should also point out that a better solution is to add multiple additional gsubs to do it properly. "gsub(/DNS:/,"",$0);gsub(/IPAddress:/,"",$0)" will remove both "DNS:" and "IPAddress:" (for IP type SANs). – Hamid – 2016-02-25T13:24:50.090