Opensslでサーバ証明書の確認をする

2023/04/23に公開

opensslでサーバ証明書の確認をしたい時TIPS

事前準備

  • DNS, NTP, Timezoneの設定は済んでると仮定
  • /etc/ssl/certs/の中に対象のサーバ証明書を署名するRootCAの公開鍵pemが入っていると仮定

証明書をDERで取得したい場合

echo "Q" | openssl s_client -connect www.google.com:443 -CApath /etc/ssl/certs/ | openssl x509 -inform PEM -text -outform DER -out out.der
# out.der

証明書の期限だけを出力したい場合

echo "Q" | openssl s_client -connect www.google.com:443 -CApath /etc/ssl/certs/ 2>/dev/null | openssl x509 -inform PEM -noout -enddate
# notAfter=Jul  5 13:28:00 2017 GMT

署名アルゴリズムを表示したい場合

echo "Q" | openssl s_client -connect www.google.com:443 -CApath /etc/ssl/certs/ 2>/dev/null | openssl x509 -inform PEM -text | perl -lane "print \$1 if /Signature\ Algorithm: (.+)\$/"
# sha256WithRSAEncryption
# sha256WithRSAEncryption

C言語にハードコーディングできるように出力したい場合

echo "Q" | openssl s_client -connect www.google.com:443 -CApath /etc/ssl/certs/ 2>/dev/null | openssl x509 -inform PEM -noout -C
# /* subject:/C=US/ST=California/L=Mountain # View/O=Google Inc/CN=www.google.com */
# /* issuer :/C=US/O=Google Inc/CN=Google
# Internet Authority G2 */
# unsigned char XXX_subject_name[106]={
# 0x30,...
# };
# unsigned char XXX_public_key[294]={
# 0x30,0x8....
# };

証明書チェイン全ての証明書に関して期限を表示する

for crt in `echo "Q" | openssl s_client -connect www.google.com:443 -CApath /etc/ssl/certs/ -showcerts 2>&1 | awk '/BEGIN/ { i++; print i ".extracted.crt" } /BEGIN/, /END/ { print > i ".extracted.crt"; }'`;do openssl x509 -in $crt -noout -issuer -dates;rm $crt; done
# issuer= /C=US/O=Google Inc/CN=Google
# Internet Authority G2
# notBefore=Apr 12 13:28:00 2017 GMT
# notAfter=Jul  5 13:28:00 2017 GMT
# issuer= /C=US/O=GeoTrust Inc./CN=GeoTrust
# Global CA
# notBefore=Apr  1 00:00:00 2015 GMT
# notAfter=Dec 31 23:59:59 2017 GMT
# issuer= /C=US/O=Equifax/OU=Equifax Secure # Certificate Authority
# notBefore=May 21 04:00:00 2002 GMT
# notAfter=Aug 21 04:00:00 2018 GMT
#多重実行しないでください

参考

Discussion