Bash
openssl
0

シェルからお手軽にSSL証明書の失効期限をチェックする方法

More than 1 year has passed since last update.

Webサーバで現在絶賛使用中のSSL証明書の期限をチェックしたくなった場合以下のコマンドを実行すると良いらしいです。

$ echo | openssl s_client -connect [site:port] 2>/dev/null | openssl x509 -noout -dates

具体的には以下のような結果が返ってきます。

$ echo | openssl s_client -connect qiita.com:443 2>/dev/null | openssl x509 -noout -dates
notBefore=Mar 15 03:55:03 2015 GMT
notAfter=May 16 01:59:04 2017 GMT
  • notBefore には証明書の有効期間の開始日  
  • notAfter には証明書の有効期間の終了日

がそれぞれ表示されます。

また、シェルスクリプトで証明書の失効するx日前であるかどうかをチェックしたいという場合には以下のコマンドが役に立ちます。

$ echo | openssl s_client -connect [domain:port] 2>/dev/null | openssl x509 -checkend [seconds] -noout

上記のコマンドの終了コードをチェックすることによって結果を知ることができます。

  • 0 が返ってきた場合は [seconds] 秒以内には証明書は失効しません
  • 1 が返ってきた場合は [seconds] 秒以内に証明書が失効します

例えば失効の 30日 前になったときに警告を出したいといったときにはワンライナーで以下のように記述をすることができます。

echo | openssl s_client -connect qiita.com:443 2>/dev/null | openssl x509 -checkend $((30 * 24 * 60 * 60)) -noout  || echo "この証明書は30日以内に失効します" >&2

以上、Qiita初投稿でした。

You can use Qiita more conveniently by logging in!
Article posting, slide mode, trend, stock, etc
What you can do with signing up
Why do not you register as a user and use Qiita more conveniently?
You seem to be reading articles frequently this month. You can use this function by logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away