/usr/share/perl5/Net/SSLGlue/LWP.pm is in libnet-sslglue-perl 1.052-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | use strict;
use warnings;
package Net::SSLGlue::LWP;
our $VERSION = 0.4;
use LWP::UserAgent '5.822';
use IO::Socket::SSL 1.19;
use URI;
# force Net::SSLGlue::LWP::Socket as superclass of Net::HTTPS, because
# only it can verify certificates
BEGIN {
my $oc = $Net::HTTPS::SSL_SOCKET_CLASS;
$Net::HTTPS::SSL_SOCKET_CLASS = my $need = 'Net::SSLGlue::LWP::Socket';
require Net::HTTPS;
require LWP::Protocol::https;
if ( ( my $oc = $Net::HTTPS::SSL_SOCKET_CLASS ) ne $need ) {
# was probably loaded before, change ISA
grep { s{^\Q$oc\E$}{$need} } @Net::HTTPS::ISA
}
die "cannot force $need into Net::HTTPS"
if $Net::HTTPS::SSL_SOCKET_CLASS ne $need;
}
our %SSLopts; # set by local and import
sub import {
shift;
%SSLopts = @_;
}
{
# add SSL options
my $old_eso = UNIVERSAL::can( 'LWP::Protocol::https','_extra_sock_opts' );
no warnings 'redefine';
*LWP::Protocol::https::_extra_sock_opts = sub {
return (
$old_eso ? ( $old_eso->(@_) ):(),
SSL_verify_mode => 1,
SSL_verifycn_scheme => 'http',
HTTPS_proxy => $_[0]->{ua}{https_proxy},
%SSLopts,
);
};
}
{
# fix https_proxy handling - forward it to a variable handled by me
my $old_proxy = defined &LWP::UserAgent::proxy && \&LWP::UserAgent::proxy
or die "cannot find LWP::UserAgent::proxy";
no warnings 'redefine';
*LWP::UserAgent::proxy = sub {
my ($self,$key,$val) = @_;
goto &$old_proxy if ref($key) || $key ne 'https';
if (@_>2) {
my $rv = &$old_proxy;
$self->{https_proxy} = delete $self->{proxy}{https}
|| die "https proxy not set?";
}
return $self->{https_proxy};
}
}
{
package Net::SSLGlue::LWP::Socket;
use IO::Socket::SSL;
use base 'IO::Socket::SSL';
my $sockclass = 'IO::Socket::INET';
use URI::Escape 'uri_unescape';
use MIME::Base64 'encode_base64';
$sockclass .= '6' if eval "require IO::Socket::INET6";
sub configure {
my ($self,$args) = @_;
my $phost = delete $args->{HTTPS_proxy}
or return $self->SUPER::configure($args);
$phost = URI->new($phost) if ! ref $phost;
my $port = $args->{PeerPort};
my $host = $args->{PeerHost} || $args->{PeerAddr};
if ( ! $port ) {
$host =~s{:(\w+)$}{};
$port = $args->{PeerPort} = $1;
$args->{PeerHost} = $host;
}
if ( $phost->scheme ne 'http' ) {
$@ = "scheme ".$phost->scheme." not supported for https_proxy";
return;
}
my $auth = '';
if ( my ($user,$pass) = split( ':', $phost->userinfo || '' ) ) {
$auth = "Proxy-authorization: Basic ".
encode_base64( uri_unescape($user).':'.uri_unescape($pass),'' ).
"\r\n";
}
my $pport = $phost->port;
$phost = $phost->host;
# temporally downgrade $self so that the right connect chain
# gets called w/o doing SSL stuff. If we don't do it it will
# try to call IO::Socket::SSL::connect
my $ssl_class = ref($self);
bless $self,$sockclass;
$self->configure({ %$args, PeerAddr => $phost, PeerPort => $pport }) or do {
$@ = "connect to proxy $phost port $pport failed";
return;
};
print $self "CONNECT $host:$port HTTP/1.0\r\n$auth\r\n";
my $hdr = '';
while (<$self>) {
$hdr .= $_;
last if $_ eq "\n" or $_ eq "\r\n";
}
if ( $hdr !~m{\AHTTP/1.\d 2\d\d} ) {
# error
$@ = "non 2xx response to CONNECT: $hdr";
return;
}
# and upgrade self by calling start_SSL
$ssl_class->start_SSL( $self,
SSL_verifycn_name => $host,
%$args
) or do {
$@ = "start SSL failed: $SSL_ERROR";
return;
};
return $self;
};
}
1;
=head1 NAME
Net::SSLGlue::LWP - proper certificate checking for https in LWP
=head1 SYNOPSIS
use Net::SSLGlue::LWP SSL_ca_path => ...;
use LWP::Simple;
get( 'https://www....' );
{
local %Net::SSLGlue::LWP::SSLopts = %Net::SSLGlue::LWP::SSLopts;
# switch off verification
$Net::SSLGlue::LWP::SSLopts{SSL_verify_mode} = 0;
# or: set different verification policy, because cert does
# not conform to RFC (wildcards in CN are not allowed for https,
# but some servers do it anyway)
$Net::SSLGlue::LWP::SSLopts{SSL_verifycn_scheme} = {
wildcards_in_cn => 'anywhere',
check_cn => 'always',
};
}
=head1 DESCRIPTION
L<Net::SSLGlue::LWP> modifies L<Net::HTTPS> and L<LWP::Protocol::https> so that
L<Net::HTTPS> is forced to use L<IO::Socket::SSL> instead of L<Crypt::SSLeay>,
and that L<LWP::Protocol::https> does proper certificate checking using the
C<http> SSL_verify_scheme from L<IO::Socket::SSL>.
Because L<LWP> does not have a mechanism to forward arbitrary parameters for
the construction of the underlying socket these parameters can be set globally
when including the package, or with local settings of the
C<%Net::SSLGlue::LWP::SSLopts> variable.
All of the C<SSL_*> parameter from L<IO::Socket::SSL> can be used; the
following parameters are especially useful:
=over 4
=item SSL_ca_path, SSL_ca_file
Specifies the path or a file where the CAs used for checking the certificates
are located. This is typically L</etc/ssl/certs> on UNIX systems.
=item SSL_verify_mode
If set to 0, verification of the certificate will be disabled. By default
it is set to 1 which means that the peer certificate is checked.
=item SSL_verifycn_name
Usually the name given as the hostname in the constructor is used to verify the
identity of the certificate. If you want to check the certificate against
another name you can specify it with this parameter.
=back
=head1 SEE ALSO
IO::Socket::SSL, LWP, Net::HTTPS, LWP::Protocol::https
=head1 COPYRIGHT
This module is copyright (c) 2008, Steffen Ullrich.
All Rights Reserved.
This module is free software. It may be used, redistributed and/or modified
under the same terms as Perl itself.
|