SSLGlue/LWP.pm

filter: Statementstmt 49.5% 52 / 105 Branchbran 13.6% 6 / 44 Conditioncond 14.8% 4 / 27 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 76.4% 13 / 17 Podpod n/a totaltotal 35.8% 75 / 209 SCAR 54.9
CC
28
Cov
35%
configure
210.0
BEGIN
32.8
__ANON__
30.0
CRAP 241.1
« SSLGlue/FTP.pm ↑ Summary SSLGlue/Socket.pm »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Net::SSLGlue::LWP;
4 1 our $VERSION = 0.501;
5 1 ▶use LWP::UserAgent '5.822';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use IO::Socket::SSL 1.19;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # force Net::SSLGlue::LWP::Socket as superclass of Net::HTTPS, because
10 # only it can verify certificates
11 1 my $use_existent;
12 BEGIN {
13 1 ▶ require LWP::Protocol::https;
Subroutine BEGIN: called CC=10 SCAR=34.9
14 1 ▶ $use_existent = $LWP::Protocol::https::VERSION
Condition33%2/6
$LWP::Protocol::https::VERSION && $LWP::Protocol::https::VERSION >= 6.06 && $LWP::UserAgent::VERSION >= 6.06
!ll&&!rl&&r
001
$LWP::Protocol::https::VERSION and $LWP::Protocol::https::VERSION >= "6.06"
!ll&&!rl&&r
001
MC/DC0%0/3
$LWP::Protocol::https::VERSION && $LWP::Protocol::https::VERSION >= 6.06 && $LWP::UserAgent::VERSION >= 6.06
$LWP::Protocol::https::VERSION $LWP::Protocol::https::VERSION >= "6.06" $LWP::UserAgent::VERSION >= 6.06
Truth table25%1/4
A and B && C
A= $LWP::Protocol::https::VERSIONB= $LWP::Protocol::https::VERSION >= "6.06"C= $LWP::UserAgent::VERSION >= 6.06
ABCresult
0XX0
10X0
1100
1111
15 && $LWP::Protocol::https::VERSION >= 6.06
16 && $LWP::UserAgent::VERSION >= 6.06;
17 1 ▶ if ($use_existent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_existent) 1 0 1
18 my $oc = $Net::HTTPS::SSL_SOCKET_CLASS ||
19 1 ▶ $ENV{PERL_NET_HTTPS_SSL_SOCKET_CLASS};
Condition33%1/3
$Net::HTTPS::SSL_SOCKET_CLASS || $ENV{'PERL_NET_HTTPS_SSL_SOCKET_CLASS'}
l!l&&r!l&&!r
100
MC/DC0%0/2
$Net::HTTPS::SSL_SOCKET_CLASS || $ENV{'PERL_NET_HTTPS_SSL_SOCKET_CLASS'}
$Net::HTTPS::SSL_SOCKET_CLASS $ENV{'PERL_NET_HTTPS_SSL_SOCKET_CLASS'}
Truth table33%1/3
A || B
A= $Net::HTTPS::SSL_SOCKET_CLASSB= $ENV{'PERL_NET_HTTPS_SSL_SOCKET_CLASS'}
ABresult
1X1
011
000
20 1 ▶ $use_existent = 0 if $oc && $oc ne 'IO::Socket::SSL';
Branch: 1 branch
BranchTrueFalseTotal
if $oc and $oc ne "IO::Socket::SSL" 0 1 1
Condition33%1/3
$oc and $oc ne "IO::Socket::SSL"
!ll&&!rl&&r
010
MC/DC0%0/2
$oc and $oc ne "IO::Socket::SSL"
$oc $oc ne "IO::Socket::SSL"
Truth table33%1/3
A and B
A= $ocB= $oc ne "IO::Socket::SSL"
ABresult
0X0
100
111
21 }
22 1 ▶ if ($use_existent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_existent) { } 1 0 1
23 1 warn "Your LWP::UserAgent/LWP::Protocol::https looks fine.\n".
24 "Will use it instead of Net::SSLGLue::LWP\n";
25 } else {
26 0 my $oc = $Net::HTTPS::SSL_SOCKET_CLASS;
27 0 $Net::HTTPS::SSL_SOCKET_CLASS = my $need = 'Net::SSLGlue::LWP::Socket';
28 0 require Net::HTTPS;
29  
30 0 ▶ if ( ( my $oc = $Net::HTTPS::SSL_SOCKET_CLASS ) ne $need ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $oc = $Net::HTTPS::SSL_SOCKET_CLASS) ne $need) 0 0 0
31 # was probably loaded before, change ISA
32 0 grep { s{^\Q$oc\E$}{$need} } @Net::HTTPS::ISA
33 }
34 0 ▶ die "cannot force $need into Net::HTTPS"
Branch: 1 branch
BranchTrueFalseTotal
if $Net::HTTPS::SSL_SOCKET_CLASS ne $need 0 0 0
35 if $Net::HTTPS::SSL_SOCKET_CLASS ne $need;
36 }
37 }
38  
39  
40 1 our %SSLopts; # set by local and import
41 sub import {
42 1 ▶ shift;
Subroutine import: called CC=1 SCAR=0.0
43 1 %SSLopts = @_;
44 }
45  
46 1 ▶if (!$use_existent) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $use_existent) { } 0 1 1
47 # add SSL options
48 0 my $old_eso = UNIVERSAL::can( 'LWP::Protocol::https','_extra_sock_opts' );
49 1 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
50 *LWP::Protocol::https::_extra_sock_opts = sub {
51 return (
52 $old_eso ? ( $old_eso->(@_) ):(),
53 SSL_verify_mode => 1,
54 SSL_verifycn_scheme => 'http',
55 HTTPS_proxy => $_[0]->{ua}{https_proxy},
56 0 ▶ %SSLopts,
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$old_eso ? : 0 0 0
57 );
58 0 };
59  
60 # fix https_proxy handling - forward it to a variable handled by me
61 0 ▶ my $old_proxy = defined &LWP::UserAgent::proxy && \&LWP::UserAgent::proxy
Branch: 1 branch
BranchTrueFalseTotal
my $old_proxy = defined &LWP::UserAgent::proxy && \&LWP::UserAgent::proxy or die "cannot find LWP::UserAgent::proxy" 0 0 0
Condition0%0/2
defined &LWP::UserAgent::proxy && \&LWP::UserAgent::proxy
!ll
00
MC/DC0%0/1
defined &LWP::UserAgent::proxy && \&LWP::UserAgent::proxy
defined &LWP::UserAgent::proxy
Truth table0%0/2
A
A= defined &LWP::UserAgent::proxy
Aresult
00
11
62 or die "cannot find LWP::UserAgent::proxy";
63 *LWP::UserAgent::proxy = sub {
64 0 ▶ my ($self,$key,$val) = @_;
Subroutine __ANON__: not called CC=5 SCAR=34.0
65 0 ▶ goto &$old_proxy if ref($key) || $key ne 'https';
Branch: 1 branch
BranchTrueFalseTotal
if ref $key or $key ne "https" 0 0 0
Condition0%0/3
ref $key or $key ne "https"
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $key or $key ne "https"
ref $key $key ne "https"
Truth table0%0/3
A or B
A= ref $keyB= $key ne "https"
ABresult
1X1
011
000
66 0 ▶ if (@_>2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 2) 0 0 0
67 0 my $rv = &$old_proxy;
68 $self->{https_proxy} = delete $self->{proxy}{https}
69 0 ▶ || die "https proxy not set?";
Condition0%0/2
delete $self->{'proxy'}{'https'} || die('https proxy not set?')
l!l
00
MC/DC0%0/1
delete $self->{'proxy'}{'https'} || die('https proxy not set?')
delete $self->{'proxy'}{'https'}
Truth table0%0/2
A
A= delete $self->{'proxy'}{'https'}
Aresult
11
00
70 }
71 0 return $self->{https_proxy};
72 0 };
73  
74 } else {
75 # wrapper around LWP::Protocol::https::_extra_sock_opts to support %SSLopts
76 1 ▶ my $old_eso = UNIVERSAL::can( 'LWP::Protocol::https','_extra_sock_opts' )
Branch: 1 branch
BranchTrueFalseTotal
my $old_eso = UNIVERSAL::can("LWP::Protocol::https", "_extra_sock_opts") or die "no LWP::Protocol::https::_extra_sock_opts found" 0 1 1
77 or die "no LWP::Protocol::https::_extra_sock_opts found";
78 1 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
79 *LWP::Protocol::https::_extra_sock_opts = sub {
80 return (
81 0 ▶ $old_eso->(@_),
Subroutine __ANON__: not called CC=1 SCAR=6.9
82 %SSLopts,
83 );
84 1 };
85 }
86  
87 {
88  
89 package Net::SSLGlue::LWP::Socket;
90 1 ▶ use IO::Socket::SSL;
Subroutine BEGIN: called CC=1 SCAR=0.0
91 1 ▶ use base 'IO::Socket::SSL';
Subroutine BEGIN: called CC=1 SCAR=0.0
92 1 my $sockclass = 'IO::Socket::INET';
93 1 ▶ use URI::Escape 'uri_unescape';
Subroutine BEGIN: called CC=1 SCAR=0.0
94 1 ▶ use MIME::Base64 'encode_base64';
Subroutine BEGIN: called CC=1 SCAR=0.0
95 1 ▶ $sockclass .= '6' if eval "require IO::Socket::INET6";
Branch: 1 branch
BranchTrueFalseTotal
if eval "require IO::Socket::INET6" 0 1 1
96  
97 sub configure {
98 0 ▶ my ($self,$args) = @_;
Subroutine configure: not called CC=14 SCAR=53.5
99 my $phost = delete $args->{HTTPS_proxy}
100 0 ▶ or return $self->SUPER::configure($args);
Branch: 1 branch
BranchTrueFalseTotal
my $phost = delete $args->{"HTTPS_proxy"} or return $self->SUPER::configure($args) 0 0 0
101 0 ▶ $phost = URI->new($phost) if ! ref $phost;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $phost 0 0 0
102  
103 0 my $port = $args->{PeerPort};
104 0 ▶ my $host = $args->{PeerHost} || $args->{PeerAddr};
Condition0%0/3
$args->{'PeerHost'} || $args->{'PeerAddr'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{'PeerHost'} || $args->{'PeerAddr'}
$args->{'PeerHost'} $args->{'PeerAddr'}
Truth table0%0/3
A || B
A= $args->{'PeerHost'}B= $args->{'PeerAddr'}
ABresult
1X1
011
000
105 0 ▶ if ( ! $port ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($port) 0 0 0
106 0 $host =~s{:(\w+)$}{};
107 0 $port = $args->{PeerPort} = $1;
108 0 $args->{PeerHost} = $host;
109 }
110 0 ▶ if ( $phost->scheme ne 'http' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($phost->scheme ne "http") 0 0 0
111 0 $@ = "scheme ".$phost->scheme." not supported for https_proxy";
112 0 return;
113 }
114 0 my $auth = '';
115 0 ▶ if ( my ($user,$pass) = split( ':', $phost->userinfo || '' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = split(/:/, $phost->userinfo || "", 3)) 0 0 0
Condition0%0/2
$phost->userinfo || ''
l!l
00
MC/DC0%0/1
$phost->userinfo || ''
$phost->userinfo
Truth table0%0/2
A
A= $phost->userinfo
Aresult
11
00
116 0 $auth = "Proxy-authorization: Basic ".
117 encode_base64( uri_unescape($user).':'.uri_unescape($pass),'' ).
118 "\r\n";
119 }
120  
121 0 my $pport = $phost->port;
122 0 $phost = $phost->host;
123  
124 # temporally downgrade $self so that the right connect chain
125 # gets called w/o doing SSL stuff. If we don't do it it will
126 # try to call IO::Socket::SSL::connect
127 0 my $ssl_class = ref($self);
128 0 bless $self,$sockclass;
129 0 ▶ $self->configure({ %$args, PeerAddr => $phost, PeerPort => $pport }) or do {
Branch: 1 branch
BranchTrueFalseTotal
$self->configure({%$args, "PeerAddr", $phost, "PeerPort", $pport}) or do { $@ = "connect to proxy $phost port $pport failed"; return } 0 0 0
130 0 $@ = "connect to proxy $phost port $pport failed";
131 0 return;
132 };
133 0 print $self "CONNECT $host:$port HTTP/1.0\r\n$auth\r\n";
134 0 my $hdr = '';
135 0 ▶ while (<$self>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $self)) 0 0 0
136 0 $hdr .= $_;
137 0 ▶ last if $_ eq "\n" or $_ eq "\r\n";
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "\n" or $_ eq "\r\n" 0 0 0
Condition0%0/3
$_ eq "\n" or $_ eq "\r\n"
l!l&&r!l&&!r
000
MC/DC0%0/2
$_ eq "\n" or $_ eq "\r\n"
$_ eq "\n" $_ eq "\r\n"
Truth table0%0/3
A or B
A= $_ eq "\n"B= $_ eq "\r\n"
ABresult
1X1
011
000
138 }
139 0 ▶ if ( $hdr !~m{\AHTTP/1.\d 2\d\d} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hdr =~ m[\AHTTP/1.\d 2\d\d]) 0 0 0
140 # error
141 0 $@ = "non 2xx response to CONNECT: $hdr";
142 0 return;
143 }
144  
145 # and upgrade self by calling start_SSL
146 $ssl_class->start_SSL( $self,
147 SSL_verifycn_name => $host,
148 %$args
149 0 ▶ ) or do {
Branch: 1 branch
BranchTrueFalseTotal
$ssl_class->start_SSL($self, "SSL_verifycn_name", $host, %$args) or do { $@ = "start SSL failed: $Net::SSLGlue::LWP::Socket::SSL_ERROR"; return } 0 0 0
150 0 $@ = "start SSL failed: $SSL_ERROR";
151 0 return;
152 };
153 0 return $self;
154 };
155 }
156  
157 1 1;
158  
159 =head1 NAME
160  
161 Net::SSLGlue::LWP - proper certificate checking for https in LWP
162  
163 =head1 SYNOPSIS
164 u
165 use Net::SSLGlue::LWP SSL_ca_path => ...;
166 use LWP::Simple;
167 get( 'https://www....' );
168  
169 {
170 local %Net::SSLGlue::LWP::SSLopts = %Net::SSLGlue::LWP::SSLopts;
171  
172 # switch off verification
173 $Net::SSLGlue::LWP::SSLopts{SSL_verify_mode} = 0;
174  
175 # or: set different verification policy, because cert does
176 # not conform to RFC (wildcards in CN are not allowed for https,
177 # but some servers do it anyway)
178 $Net::SSLGlue::LWP::SSLopts{SSL_verifycn_scheme} = {
179 wildcards_in_cn => 'anywhere',
180 check_cn => 'always',
181 };
182 }
183  
184  
185 =head1 DESCRIPTION
186  
187 L<Net::SSLGlue::LWP> modifies L<Net::HTTPS> and L<LWP::Protocol::https> so that
188 L<Net::HTTPS> is forced to use L<IO::Socket::SSL> instead of L<Crypt::SSLeay>,
189 and that L<LWP::Protocol::https> does proper certificate checking using the
190 C<http> SSL_verify_scheme from L<IO::Socket::SSL>.
191  
192 This module should only be used for older LWP version, see B<Supported LWP
193 versions> below.
194  
195 Because L<LWP> does not have a mechanism to forward arbitrary parameters for
196 the construction of the underlying socket these parameters can be set globally
197 when including the package, or with local settings of the
198 C<%Net::SSLGlue::LWP::SSLopts> variable.
199  
200 All of the C<SSL_*> parameter from L<IO::Socket::SSL> can be used; the
201 following parameters are especially useful:
202  
203 =over 4
204  
205 =item SSL_ca_path, SSL_ca_file
206  
207 Specifies the path or a file where the CAs used for checking the certificates
208 are located. This is typically L</etc/ssl/certs> on UNIX systems.
209  
210 =item SSL_verify_mode
211  
212 If set to 0, verification of the certificate will be disabled. By default
213 it is set to 1 which means that the peer certificate is checked.
214  
215 =item SSL_verifycn_name
216  
217 Usually the name given as the hostname in the constructor is used to verify the
218 identity of the certificate. If you want to check the certificate against
219 another name you can specify it with this parameter.
220  
221 =back
222  
223 =head1 Supported LWP versions
224  
225 This module should be used for older LWP version only. Starting with version
226 6.06 it is recommended to use LWP directly. If a recent version is found
227 Net::SSLGlue::LWP will print out a warning and not monkey patch too much into
228 LWP (only as much as necessary to still support C<%Net::SSLGlue::LWP::SSLopts>).
229  
230 =head1 SEE ALSO
231  
232 IO::Socket::SSL, LWP, Net::HTTPS, LWP::Protocol::https
233  
234 =head1 COPYRIGHT
235  
236 This module is copyright (c) 2008..2015, Steffen Ullrich.
237 All Rights Reserved.
238 This module is free software. It may be used, redistributed and/or modified
239 under the same terms as Perl itself.
240  
« SSLGlue/FTP.pm ↑ Summary SSLGlue/Socket.pm »