LoginSignup
3
5

More than 5 years have passed since last update.

opensslでサーバ証明書を取得するワンライナー

Last updated at Posted at 2017-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
※多重実行厳禁

参考

- https://serverfault.com/questions/590870/how-to-view-all-ssl-certificates-in-a-bundle

3
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5