根據您安裝的SSL模塊,有兩種方法可以做到這一點。 LWP docs recommend installing Crypt::SSLeay。如果這就是你所做的,將HTTPS_CA_FILE
環境變量設置爲指向你的ca-bundle.crt應該可以做到。 (Crypt::SSLeay docs提到這一點,但在細節上略顯點點)。另外,根據您的設置,您可能需要設置HTTPS_CA_DIR
環境變量。
的地穴:: SSLeay的
例子:
use LWP::Simple qw(get);
$ENV{HTTPS_CA_FILE} = "/path/to/your/ca/file/ca-bundle";
$ENV{HTTPS_DEBUG} = 1;
print get("https://some-server-with-bad-certificate.com");
__END__
SSL_connect:before/connect initialization
SSL_connect:SSLv2/v3 write client hello A
SSL_connect:SSLv3 read server hello A
SSL3 alert write:fatal:unknown CA
SSL_connect:error in SSLv3 read server certificate B
SSL_connect:error in SSLv3 read server certificate B
SSL_connect:before/connect initialization
SSL_connect:SSLv3 write client hello A
SSL_connect:SSLv3 read server hello A
SSL3 alert write:fatal:bad certificate
SSL_connect:error in SSLv3 read server certificate B
SSL_connect:before/connect initialization
SSL_connect:SSLv2 write client hello A
SSL_connect:error in SSLv2 read server hello B
注意,得到不die
,但它確實返回undef
。
或者,您可以使用IO::Socket::SSL
模塊(也可從CPAN獲得)。爲了使這個驗證您需要修改SSL上下文默認的服務器證書:
use IO::Socket::SSL qw(debug3);
use Net::SSLeay;
BEGIN {
IO::Socket::SSL::set_ctx_defaults(
verify_mode => Net::SSLeay->VERIFY_PEER(),
ca_file => "/path/to/ca-bundle.crt",
# ca_path => "/alternate/path/to/cert/authority/directory"
);
}
use LWP::Simple qw(get);
warn get("https:://some-server-with-bad-certificate.com");
這個版本也導致get()
返回民主基金,但打印警告STDERR
當你執行它(以及如果一堆調試導入調試*符號從IO ::插座:: SSL):
% perl ssl_test.pl
DEBUG: .../IO/Socket/SSL.pm:1387: new ctx 139403496
DEBUG: .../IO/Socket/SSL.pm:269: socket not yet connected
DEBUG: .../IO/Socket/SSL.pm:271: socket connected
DEBUG: .../IO/Socket/SSL.pm:284: ssl handshake not started
DEBUG: .../IO/Socket/SSL.pm:327: Net::SSLeay::connect -> -1
DEBUG: .../IO/Socket/SSL.pm:1135: SSL connect attempt failed with unknown errorerror:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
DEBUG: .../IO/Socket/SSL.pm:333: fatal SSL error: SSL connect attempt failed with unknown errorerror:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
DEBUG: .../IO/Socket/SSL.pm:1422: free ctx 139403496 open=139403496
DEBUG: .../IO/Socket/SSL.pm:1425: OK free ctx 139403496
DEBUG: .../IO/Socket/SSL.pm:1135: IO::Socket::INET configuration failederror:00000000:lib(0):func(0):reason(0)
500 Can't connect to some-server-with-bad-certificate.com:443 (SSL connect attempt failed with unknown errorerror:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed)
不,接受的解決方案不會遇到這個問題。 (好吧,我自己寫了。)LWP 6在默認情況下會將通用名與主機名進行比較,如果不匹配則會中止。 (你是對的,以前版本的LWP沒有。) – cjm 2011-10-11 07:15:37
這是不正確的,我使用最新版本的LWP :: UserAgent(版本6.04)作爲SOAP :: Lite(版本0.714)的後端。 LWP :: UserAgent的後端在這臺機器上是IO :: Socket :: SSL。我發現如果沒有上面的代碼,CN既不會被檢查,也不會驗證證書鏈。使用ssl_opts()設置「verify_hostname」和「SSL_ca_path」不起作用。 – blumentopf 2013-02-04 16:45:29