OpenSSL

よくあるOpenSSLの使い方を紹介する。OpenSSLはバージョン毎でできることが違うので注意。違いが顕著なところについてはOS毎に解説する。

SSL証明書発行関連について別項目で紹介する。

SSLサーバー動作検証

コマンド(基本形)

SSLサーバーへの接続確認。

openssl s_client -connect wiki.ninth-nine.com:443 < /dev/null

コマンド(-CAfile オプション)

接続にあたり自己署名証明書を指定する場合。

openssl s_client -connect wiki.ninth-nine.com:443 -CAfile /etc/ssl/cert.pem < /dev/null

※-CAfile で指定できるSSL証明書ファイルは自己署名証明書(ルート証明書・俺々証明書)である。

コマンド(-cipher オプション)

サーバーが対応している暗号スイート(Cipher Suite)の洗い出し。

openssl s_client -connect wiki.ninth-nine.com:443 -cipher 'ALL' < /dev/null
openssl s_client -connect wiki.ninth-nine.com:443 -cipher 'ALL!ECDHE-RSA-AES256-SHA' < /dev/null
openssl s_client -connect wiki.ninth-nine.com:443 -cipher 'ALL!ECDHE-RSA-AES256-SHA!DHE-RSA-AES256-SHA256' < /dev/null
openssl s_client -connect wiki.ninth-nine.com:443 -cipher 'ALL!ECDHE-RSA-AES256-SHA!DHE-RSA-AES256-SHA256!DHE-RSA-AES256-SHA' < /dev/null
openssl s_client -connect wiki.ninth-nine.com:443 -cipher 'ALL!ECDHE-RSA-AES256-SHA!DHE-RSA-AES256-SHA256!DHE-RSA-AES256-SHA!DHE-RSA-CAMELLIA256-SHA' < /dev/null
openssl s_client -connect wiki.ninth-nine.com:443 -cipher 'ALL!ECDHE-RSA-AES256-SHA!DHE-RSA-AES256-SHA256!DHE-RSA-AES256-SHA!DHE-RSA-CAMELLIA256-SHA!AES256-SHA256' < /dev/null
openssl s_client -connect wiki.ninth-nine.com:443 -cipher 'ALL!ECDHE-RSA-AES256-SHA!DHE-RSA-AES256-SHA256!DHE-RSA-AES256-SHA!DHE-RSA-CAMELLIA256-SHA!AES256-SHA256!AES256-SHA' < /dev/null
openssl s_client -connect wiki.ninth-nine.com:443 -cipher 'ALL!ECDHE-RSA-AES256-SHA!DHE-RSA-AES256-SHA256!DHE-RSA-AES256-SHA!DHE-RSA-CAMELLIA256-SHA!AES256-SHA256!AES256-SHA!CAMELLIA256-SHA' < /dev/null
openssl s_client -connect wiki.ninth-nine.com:443 -cipher 'ALL!ECDHE-RSA-AES256-SHA!DHE-RSA-AES256-SHA256!DHE-RSA-AES256-SHA!DHE-RSA-CAMELLIA256-SHA!AES256-SHA256!AES256-SHA!CAMELLIA256-SHA!ECDHE-RSA-AES128-GCM-SHA256' < /dev/null
openssl s_client -connect wiki.ninth-nine.com:443 -cipher 'ALL!ECDHE-RSA-AES256-SHA!DHE-RSA-AES256-SHA256!DHE-RSA-AES256-SHA!DHE-RSA-CAMELLIA256-SHA!AES256-SHA256!AES256-SHA!CAMELLIA256-SHA!ECDHE-RSA-AES128-GCM-SHA256!ECDHE-RSA-AES128-SHA256' < /dev/null
  :

コマンド(-プロトコル/-no_プロトコル オプション)

対応プロトコルを「個別」に調査する。

openssl s_client -connect wiki.ninth-nine.com:443 -ssl2    -no_ssl3 -no_tls1 -no_tls1_1 -no_tls1_2 < /dev/null
openssl s_client -connect wiki.ninth-nine.com:443 -no_ssl2 -ssl3    -no_tls1 -no_tls1_1 -no_tls1_2 < /dev/null
openssl s_client -connect wiki.ninth-nine.com:443 -no_ssl2 -no_ssl3 -tls1    -no_tls1_1 -no_tls1_2 < /dev/null
openssl s_client -connect wiki.ninth-nine.com:443 -no_ssl2 -no_ssl3 -no_tls1 -tls1_1    -no_tls1_2 < /dev/null
openssl s_client -connect wiki.ninth-nine.com:443 -no_ssl2 -no_ssl3 -no_tls1 -no_tls1_1 -tls1_2    < /dev/null

実際のブラウザで見た時の挙動とは結果が違う場合があるので注意。これは非対応プロトコルのフォールバック方法の違いに起因する(詳細はめんどくさいの解説しない)。

コマンド(-servername オプション)

Server Name Indication(SNI)の検証する。

openssl s_client -connect wiki.ninth-nine.com:443 -servername wiki.ninth-nine.com < /dev/null

署名リクエスト(CSR)の確認

コマンド(-text オプション)

CSRが有する詳細情報を表示する。

openssl req -noout -text -in CSRファイル名.csr

コマンド(-subject)

サブジェクト(主体者)のディスティングイッシュ名を取得する。

openssl req -noout -subject -in CSRファイル名.csr

コマンド(-modulus)

モジュロを取得する。取得するだけだと何する?なので、秘密鍵と対になっているか確認する。

diff -u <(openssl req  -noout -modulus -in CSRファイル名.csr) <(openssl rsa  -noout -modulus -in 秘密鍵ファイル名.key)

<(command)というイディオム(実行結果をテンポラリファイル名で渡してくれる)は zsh/bash 拡張なので、シェルスクリプト(sh/ash/ksh)中では使わないこと。

署名済公開鍵(CRT)の確認

コマンド(-text オプション)

openssl x509 -noout -text -in CRTファイル名.crt

コマンド(-subject/-issuer オプション)

サブジェクト(主体者)やイシュア(発行者)のディスティングイッシュ名を取得する。もちろんオプションは別々に指定可能。

openssl req -noout -subject -issuer -in CRTファイル名.crt

コマンド(-startdate/-enddate オプション)

有効期限(開始日時あるいは終了日時)を取得する。もちろんオプションは別々に指定可能。

openssl x509 -noout -startdate -enddate -in CRTファイル名.crt

コマンド(-modulus オプション)

モジュロを取得する。取得するだけだと何する?なので、秘密鍵と対になっているか確認する。

diff -u <(openssl x509 -noout -modulus -in CRTファイル名.crt) <(openssl rsa  -noout -modulus -in 秘密鍵ファイル名.key)

<(command)というイディオム(実行結果をテンポラリファイル名で渡してくれる)は zsh/bash 拡張なので、シェルスクリプト(sh/ash/ksh)中では使わないこと。

参考文献

OpenSSL (最終更新日時 2016-05-04 17:20:27 更新者 NorikatsuShigemura)