+ Enhance http ssl context

This commit is contained in:
garnier-quentin 2015-07-16 14:26:32 +02:00
parent 8f8eab6884
commit fb5619c7f3
3 changed files with 16 additions and 17 deletions

View File

@ -62,7 +62,7 @@ sub new {
"proxyurl:s" => { name => 'proxyurl' }, "proxyurl:s" => { name => 'proxyurl' },
"expected-string:s" => { name => 'expected_string' }, "expected-string:s" => { name => 'expected_string' },
"timeout:s" => { name => 'timeout', default => '3' }, "timeout:s" => { name => 'timeout', default => '3' },
"ssl:s" => { name => 'ssl', }, "ssl:s" => { name => 'ssl', },
"cert-file:s" => { name => 'cert_file' }, "cert-file:s" => { name => 'cert_file' },
"key-file:s" => { name => 'key_file' }, "key-file:s" => { name => 'key_file' },
"cacert-file:s" => { name => 'cacert_file' }, "cacert-file:s" => { name => 'cacert_file' },

View File

@ -68,7 +68,7 @@ sub new {
"header:s@" => { name => 'header' }, "header:s@" => { name => 'header' },
"get-param:s@" => { name => 'get_param' }, "get-param:s@" => { name => 'get_param' },
"timeout:s" => { name => 'timeout', default => 10 }, "timeout:s" => { name => 'timeout', default => 10 },
"ssl:s" => { name => 'ssl', }, "ssl:s" => { name => 'ssl', },
"cert-file:s" => { name => 'cert_file' }, "cert-file:s" => { name => 'cert_file' },
"key-file:s" => { name => 'key_file' }, "key-file:s" => { name => 'key_file' },
"cacert-file:s" => { name => 'cacert_file' }, "cacert-file:s" => { name => 'cacert_file' },

View File

@ -42,6 +42,7 @@ use warnings;
use LWP::UserAgent; use LWP::UserAgent;
use HTTP::Cookies; use HTTP::Cookies;
use URI; use URI;
use IO::Socket::SSL;
sub get_port { sub get_port {
my ($self, %options) = @_; my ($self, %options) = @_;
@ -118,28 +119,26 @@ sub connect {
$ua->proxy(['http', 'https'], $self->{option_results}->{proxyurl}); $ua->proxy(['http', 'https'], $self->{option_results}->{proxyurl});
} }
if (defined($self->{option_results}->{ssl}) && $self->{option_results}->{ssl} ne '') {
use IO::Socket::SSL;
my $context = new IO::Socket::SSL::SSL_Context(
SSL_version => $self->{option_results}->{ssl},
);
IO::Socket::SSL::set_default_context($context);
}
if (defined($self->{option_results}->{cert_pkcs12}) && $self->{option_results}->{cert_file} ne '' && $self->{option_results}->{cert_pwd} ne '') { if (defined($self->{option_results}->{cert_pkcs12}) && $self->{option_results}->{cert_file} ne '' && $self->{option_results}->{cert_pwd} ne '') {
eval "use Net::SSL"; die $@ if $@; eval "use Net::SSL"; die $@ if $@;
$ENV{HTTPS_PKCS12_FILE} = $self->{option_results}->{cert_file}; $ENV{HTTPS_PKCS12_FILE} = $self->{option_results}->{cert_file};
$ENV{HTTPS_PKCS12_PASSWORD} = $self->{option_results}->{cert_pwd}; $ENV{HTTPS_PKCS12_PASSWORD} = $self->{option_results}->{cert_pwd};
} }
my $ssl_context;
if (defined($self->{option_results}->{ssl}) && $self->{option_results}->{ssl} ne '') {
$ssl_context = { SSL_version => $self->{option_results}->{ssl} };
}
if (defined($self->{option_results}->{cert_file}) && !defined($self->{option_results}->{cert_pkcs12})) { if (defined($self->{option_results}->{cert_file}) && !defined($self->{option_results}->{cert_pkcs12})) {
use IO::Socket::SSL; $ssl_context = {} if (!defined($ssl_context));
my $context = new IO::Socket::SSL::SSL_Context( $ssl_context->{SSL_use_cert} = 1;
SSL_use_cert => 1, $ssl_context->{SSL_cert_file} = $self->{option_results}->{cert_file};
SSL_cert_file => $self->{option_results}->{cert_file}, $ssl_context->{SSL_key_file} = $self->{option_results}->{key_file} if (defined($self->{option_results}->{key_file}));
$self->{option_results}->{key_file} ? ( SSL_key_file => $self->{option_results}->{key_file} ):(), $ssl_context->{SSL_ca_file} = $self->{option_results}->{cacert_file} if (defined($self->{option_results}->{cacert_file}));
$self->{option_results}->{cacert_file} ? ( SSL_ca_file => $self->{option_results}->{cacert_file} ):(), }
);
if (defined($ssl_context)) {
my $context = new IO::Socket::SSL::SSL_Context(%{$ssl_context});
IO::Socket::SSL::set_default_context($context); IO::Socket::SSL::set_default_context($context);
} }