| 1 | |||||||||||||||||||||||||||||
| 2 | package IO::Socket::SSL::Intercept; | ||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use IO::Socket::SSL::Utils; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Net::SSLeay; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | our $VERSION = '1.93'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | sub new { | ||||||||||||||||||||||||||||
| 12 | 0 | ▶ | my ($class,%args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 0 | my $cacert = delete $args{proxy_cert}; | |||||||||||||||||||||||||||
| 15 | 0 | ▶ | if ( ! $cacert ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | 0 | ▶ | if ( my $f = delete $args{proxy_cert_file} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | 0 | $cacert = PEM_file2cert($f); | |||||||||||||||||||||||||||
| 18 | } else { | ||||||||||||||||||||||||||||
| 19 | 0 | croak "no proxy_cert or proxy_cert_file given"; | |||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 0 | my $cakey = delete $args{proxy_key}; | |||||||||||||||||||||||||||
| 24 | 0 | ▶ | if ( ! $cakey ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 0 | ▶ | if ( my $f = delete $args{proxy_key_file} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 0 | $cakey = PEM_file2key($f); | |||||||||||||||||||||||||||
| 27 | } else { | ||||||||||||||||||||||||||||
| 28 | 0 | croak "no proxy_cert or proxy_cert_file given"; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 0 | my $certkey = delete $args{cert_key}; | |||||||||||||||||||||||||||
| 33 | 0 | ▶ | if ( ! $certkey ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 0 | ▶ | if ( my $f = delete $args{cert_key_file} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 0 | $certkey = PEM_file2key($f); | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | my $cache = delete $args{cache} || {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
delete $args{'cache'} || {}
MC/DC0%0/1
delete $args{'cache'} || {}
delete $args{'cache'}
Truth table0%0/2
A
A= delete $args{'cache'}
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | my $self = bless { | ||||||||||||||||||||||||||||
| 42 | cacert => $cacert, | ||||||||||||||||||||||||||||
| 43 | cakey => $cakey, | ||||||||||||||||||||||||||||
| 44 | certkey => $certkey, | ||||||||||||||||||||||||||||
| 45 | 0 | ▶ | serial => delete $args{serial} || 1, | ||||||||||||||||||||||||||
|
Condition0%0/2
delete $args{'serial'} || 1
MC/DC0%0/1
delete $args{'serial'} || 1
delete $args{'serial'}
Truth table0%0/2
A
A= delete $args{'serial'}
| |||||||||||||||||||||||||||||
| 46 | cache => $cache, | ||||||||||||||||||||||||||||
| 47 | }; | ||||||||||||||||||||||||||||
| 48 | 0 | return $self; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | sub DESTROY { | ||||||||||||||||||||||||||||
| 52 | # call various ssl _free routines | ||||||||||||||||||||||||||||
| 53 | 0 | ▶ | my $self = shift or return; | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=7 SCAR=40.3
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 0 | for ( \$self->{cacert}, | |||||||||||||||||||||||||||
| 55 | 0 | ▶ | map { \$_->{cert} } ref($self->{cache}) ne 'CODE' ? values %{$self->{cache}} :()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | $$_ or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | CERT_free($$_); | |||||||||||||||||||||||||||
| 58 | 0 | $$_ = undef; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | 0 | for ( \$self->{cakey}, \$self->{pubkey} ) { | |||||||||||||||||||||||||||
| 61 | 0 | ▶ | $$_ or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 0 | KEY_free($$_); | |||||||||||||||||||||||||||
| 63 | 0 | $$_ = undef; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | sub clone_cert { | ||||||||||||||||||||||||||||
| 68 | 0 | ▶ | my ($self,$old_cert,$clone_key) = @_; | ||||||||||||||||||||||||||
|
Subroutine
clone_cert: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | $clone_key ||= substr(unpack("H*",Net::SSLeay::X509_get_fingerprint($old_cert,'sha1')),0,16); | ||||||||||||||||||||||||||
|
Condition0%0/3
$clone_key ||= substr(unpack("H*", &Net::SSLeay::X509_get_fingerprint($old_cert, "sha1")), 0, 16)
MC/DC0%0/2
$clone_key ||= substr(unpack("H*", &Net::SSLeay::X509_get_fingerprint($old_cert, "sha1")), 0, 16)
$clone_key
substr(unpack("H*", &Net::SSLeay::X509_get_fingerprint($old_cert, "sha1")), 0, 16)
Truth table0%0/3
A ||= B
A= $clone_keyB= substr(unpack("H*", &Net::SSLeay::X509_get_fingerprint($old_cert, "sha1")), 0, 16)
| |||||||||||||||||||||||||||||
| 71 | 0 | ▶ | if ( my ($clone,$key) = _get_cached($self,$clone_key)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | return ($clone,$key); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | # create new certificate based on original | ||||||||||||||||||||||||||||
| 76 | my ($clone,$key) = CERT_create( | ||||||||||||||||||||||||||||
| 77 | 0 | %{ CERT_asHash($old_cert) }, | |||||||||||||||||||||||||||
| 78 | serial => $self->{serial}++, | ||||||||||||||||||||||||||||
| 79 | issuer_cert => $self->{cacert}, | ||||||||||||||||||||||||||||
| 80 | issuer_key => $self->{cakey}, | ||||||||||||||||||||||||||||
| 81 | key => $self->{certkey}, | ||||||||||||||||||||||||||||
| 82 | 0 | ); | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # put into cache | ||||||||||||||||||||||||||||
| 85 | 0 | _set_cached($self,$clone_key,$clone,$key); | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | return ($clone,$key); | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | sub _get_cached { | ||||||||||||||||||||||||||||
| 91 | 0 | ▶ | my ($self,$clone_key) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_get_cached: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 92 | 0 | my $c = $self->{cache}; | |||||||||||||||||||||||||||
| 93 | 0 | ▶ | return $c->($clone_key) if ref($c) eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | my $e = $c->{$clone_key} or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | $e->{atime} = time(); | |||||||||||||||||||||||||||
| 96 | 0 | ▶ | return ($e->{cert},$e->{key} || $self->{certkey}); | ||||||||||||||||||||||||||
|
Condition0%0/3
$e->{'key'} || $self->{'certkey'}
MC/DC0%0/2
$e->{'key'} || $self->{'certkey'}
$e->{'key'}
$self->{'certkey'}
Truth table0%0/3
A || B
A= $e->{'key'}B= $self->{'certkey'}
| |||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | sub _set_cached { | ||||||||||||||||||||||||||||
| 100 | 0 | ▶ | my ($self,$clone_key,$cert,$key) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_set_cached: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 101 | 0 | my $c = $self->{cache}; | |||||||||||||||||||||||||||
| 102 | 0 | ▶ | return $c->($clone_key,$cert,$key) if ref($c) eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | $c->{$clone_key} = { | ||||||||||||||||||||||||||||
| 104 | cert => $cert, | ||||||||||||||||||||||||||||
| 105 | 0 | ▶ | $self->{certkey} && $self->{certkey} == $key ? () : ( key => $key ), | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"certkey"} and $self->{"certkey"} == $key
MC/DC0%0/2
$self->{"certkey"} and $self->{"certkey"} == $key
$self->{"certkey"}
$self->{"certkey"} == $key
Truth table0%0/3
A and B
A= $self->{"certkey"}B= $self->{"certkey"} == $key
| |||||||||||||||||||||||||||||
| 106 | atime => time() | ||||||||||||||||||||||||||||
| 107 | }; | ||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | sub STORABLE_freeze { my $self = shift; $self->serialize() } | ||||||||||||||||||||||||||
|
Subroutine
STORABLE_freeze: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 112 | 0 | ▶ | sub STORABLE_thaw { my ($class,undef,$data) = @_; $class->unserialize($data) } | ||||||||||||||||||||||||||
|
Subroutine
STORABLE_thaw: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | sub serialize { | ||||||||||||||||||||||||||||
| 115 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
serialize: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 116 | 0 | my $data = pack("N",2); # version | |||||||||||||||||||||||||||
| 117 | 0 | $data .= pack("N/a", PEM_cert2string($self->{cacert})); | |||||||||||||||||||||||||||
| 118 | 0 | $data .= pack("N/a", PEM_key2string($self->{cakey})); | |||||||||||||||||||||||||||
| 119 | 0 | ▶ | if ( $self->{certkey} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 0 | $data .= pack("N/a", PEM_key2string($self->{certkey})); | |||||||||||||||||||||||||||
| 121 | } else { | ||||||||||||||||||||||||||||
| 122 | 0 | $data .= pack("N/a", ''); | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | 0 | $data .= pack("N",$self->{serial}); | |||||||||||||||||||||||||||
| 125 | 0 | ▶ | if ( ref($self->{cache}) eq 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 0 | ▶ | while ( my($k,$v) = each %{ $self->{cache}} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | $data .= pack("N/aN/aN/aN", $k, | ||||||||||||||||||||||||||||
| 128 | PEM_cert2string($k->{cert}), | ||||||||||||||||||||||||||||
| 129 | $k->{key} ? PEM_key2string($k->{key}) : '', | ||||||||||||||||||||||||||||
| 130 | 0 | ▶ | $k->{atime}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | 0 | return $data; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | sub unserialize { | ||||||||||||||||||||||||||||
| 137 | 0 | ▶ | my ($class,$data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
unserialize: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 138 | 0 | ▶ | unpack("N",substr($data,0,4,'')) == 2 or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | croak("serialized with wrong version"); | ||||||||||||||||||||||||||||
| 140 | 0 | ( my $cacert,my $cakey,my $certkey,my $serial,$data) | |||||||||||||||||||||||||||
| 141 | = unpack("N/aN/aN/aNa*",$data); | ||||||||||||||||||||||||||||
| 142 | 0 | ▶ | my $self = bless { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table0%0/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||
| 143 | serial => $serial, | ||||||||||||||||||||||||||||
| 144 | cacert => PEM_string2cert($cacert), | ||||||||||||||||||||||||||||
| 145 | cakey => PEM_string2key($cakey), | ||||||||||||||||||||||||||||
| 146 | $certkey ? ( certkey => PEM_string2key($certkey)):(), | ||||||||||||||||||||||||||||
| 147 | }, ref($class)||$class; | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 0 | ▶ | $self->{cache} = {} if $data ne ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 0 | ▶ | while ( $data ne '' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 0 | (my $key,my $cert,my $certkey, my $atime,$data) = unpack("N/aN/aNa*",$data); | |||||||||||||||||||||||||||
| 152 | 0 | ▶ | $self->{cache}{$key} = { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | cert => PEM_string2cert($cert), | ||||||||||||||||||||||||||||
| 154 | $key ? ( key => PEM_string2key($certkey)):(), | ||||||||||||||||||||||||||||
| 155 | atime => $atime | ||||||||||||||||||||||||||||
| 156 | }; | ||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | 0 | return $self; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 1 | 1; | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | __END__ | ||||||||||||||||||||||||||||