2015年11月17日火曜日

cURL で自己署名証明書の SSL が通らないとき

コマンドラインでWebにアクセスすることができるcURLを利用して
自己署名証明書を設定したHTTPS環境にアクセスすると失敗します。
curl https://example.com/
実行すると次のメッセージが表示されてアクセスできません。
curl: (60) SSL certificate problem: self signed certificate
More details here: http://curl.haxx.se/docs/sslcerts.html

curl performs SSL certificate verification by default, using a "bundle"
 of Certificate Authority (CA) public keys (CA certs). If the default
 bundle file isn't adequate, you can specify an alternate file
 using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
 the bundle, the certificate verification probably failed due to a
 problem with the certificate (it might be expired, or the name might
 not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
 the -k (or --insecure) option.
自己署名証明書を利用しないのが最善ですが、やむを得ず使う場合
--cacert でcrtファイル(SSL証明書)を指定すればアクセスできます。
curl --cacert server.crt https://example.com/
-k または --insecure は証明書の検証をオフにしてアクセスするため
下手すると悪いことが起きるかもしれないので、気をつけます。
curl -k https://example.com/
失敗したときに表示されるメッセージを落ち着いて読めば良いのですが
英語は慣れるしかないですね。