HTTPS接続がCan't verify SSL peersというエラーになるときは

久しぶりにMacでとあるPerlスクリプトを実行したら OAuth::Lite::Consumer が特にエラーも出さずに失敗してしまった。


いろいろ試しているうちに、下記のようなスクリプトを実行すると

% perl -MLWP::UserAgent -e 'print LWP::UserAgent->new()->get("https://www.google.com/")->as_string'

500 Can't verify SSL peers without knowing which Certificate Authorities to trust
Content-Type: text/plain
Client-Date: Wed, 06 May 2015 04:47:19 GMT
Client-Warning: Internal response

Can't verify SSL peers without knowing which Certificate Authorities to trust

This problem can be fixed by either setting the PERL_LWP_SSL_CA_FILE
envirionment variable or by installing the Mozilla::CA module.

To disable verification of SSL peers set the PERL_LWP_SSL_VERIFY_HOSTNAME
envirionment variable to 0.  If you do this you can't be sure that you
communicate with the expected peer.

とか言われた。これは信頼出来るCAを見つけられなくてSSL接続先を検証できなかったため(そのまんま)。


OAuth::Lite::Consumer も接続先がHTTPSだったため、内部的にこのエラーで接続ができずに(タチの悪いことに無言で)失敗していた。


Macの場合、CAはKeychainで管理されているので、PERL_LWP_SSL_CA_FILE を設定するといった方法は使えない。ホスト認証を止める(PERL_LWP_SSL_VERIFY_HOSTNAME=0にする)のはいやなので、上記メッセージの通り

% sudo cpan install Mozilla::CA

Mozilla::CAを導入すれば、Mozillaが提供するCA Bundleを勝手に取得してくれ解決する。