SSL/Intercept.pm

filter: Statementstmt 17.3% 17 / 98 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 35.7% 5 / 14 Podpod 66.6% 4 / 6 totaltotal 13.5% 26 / 192 CC 35 SCAR 40.1
CC
35
Cov
10%
new
132.0
DESTROY
56.0
unserialize
56.0
CRAP 54.9
« SSL/PublicSuffix.pm ↑ Summary
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 = '2.056';
10  
11  
12 sub new {
13 0 ▶ my ($class,%args) = @_;
Subroutine new: not called CC=11 SCAR=48.8
Pod documented
14  
15 0 my $cacert = delete $args{proxy_cert};
16 0 ▶ if ( ! $cacert ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cacert) 0 0 0
17 0 ▶ if ( my $f = delete $args{proxy_cert_file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = delete $args{'proxy_cert_file'}) { } 0 0 0
18 0 $cacert = PEM_file2cert($f);
19 } else {
20 0 croak "no proxy_cert or proxy_cert_file given";
21 }
22 }
23  
24 0 my $cakey = delete $args{proxy_key};
25 0 ▶ if ( ! $cakey ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cakey) 0 0 0
26 0 ▶ if ( my $f = delete $args{proxy_key_file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = delete $args{'proxy_key_file'}) { } 0 0 0
27 0 $cakey = PEM_file2key($f);
28 } else {
29 0 croak "no proxy_cert or proxy_cert_file given";
30 }
31 }
32  
33 0 my $certkey = delete $args{cert_key};
34 0 ▶ if ( ! $certkey ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($certkey) 0 0 0
35 0 ▶ if ( my $f = delete $args{cert_key_file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = delete $args{"cert_key_file"}) 0 0 0
36 0 $certkey = PEM_file2key($f);
37 }
38 }
39  
40 0 ▶ my $cache = delete $args{cache} || {};
Condition0%0/2
delete $args{'cache'} || {}
l!l
00
MC/DC0%0/1
delete $args{'cache'} || {}
delete $args{'cache'}
Truth table0%0/2
A
A= delete $args{'cache'}
Aresult
11
00
41 0 ▶ if (ref($cache) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cache eq "CODE") 0 0 0
42 # check cache type
43 0 my $type = $cache->('type');
44 0 ▶ if (!$type) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $type) { } 0 0 0
elsif ($type == 1) { } 0 0 0
45 # old cache interface - change into new interface
46 # get: $cache->(fp)
47 # set: $cache->(fp,cert,key)
48 0 my $oc = $cache;
49 $cache = sub {
50 0 ▶ my ($fp,$create_cb) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
51 0 my @ck = $oc->($fp);
52 0 ▶ $oc->($fp, @ck = &$create_cb) if !@ck;
Branch: 1 branch
BranchTrueFalseTotal
unless @ck 0 0 0
53 0 return @ck;
54 0 };
55 } elsif ($type == 1) {
56 # current interface:
57 # get/set: $cache->(fp,cb_create)
58 } else {
59 0 die "invalid type of cache: $type";
60 }
61 }
62  
63 my $self = bless {
64 cacert => $cacert,
65 cakey => $cakey,
66 certkey => $certkey,
67 cache => $cache,
68 serial => delete $args{serial},
69 0 };
70 0 return $self;
71 }
72  
73 sub DESTROY {
74 # call various ssl _free routines
75 0 ▶ my $self = shift or return;
Subroutine DESTROY: not called CC=7 SCAR=40.3
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 0 0
76 0 for ( \$self->{cacert},
77 0 ▶ map { \$_->{cert} } ref($self->{cache}) ne 'CODE' ? values %{$self->{cache}} :()) {
Branch: 1 branch
BranchTrueFalseTotal
ref $self->{'cache'} ne 'CODE' ? : 0 0 0
78 0 ▶ $$_ or next;
Branch: 1 branch
BranchTrueFalseTotal
$$_ or next 0 0 0
79 0 CERT_free($$_);
80 0 $$_ = undef;
81 }
82 0 for ( \$self->{cakey}, \$self->{pubkey} ) {
83 0 ▶ $$_ or next;
Branch: 1 branch
BranchTrueFalseTotal
$$_ or next 0 0 0
84 0 KEY_free($$_);
85 0 $$_ = undef;
86 }
87 }
88  
89 sub clone_cert {
90 0 ▶ my ($self,$old_cert,$clone_key) = @_;
Subroutine clone_cert: not called CC=4 SCAR=30.0
Pod documented
91  
92 0 my $hash = CERT_asHash($old_cert);
93 my $create_cb = sub {
94 # if not in cache create new certificate based on original
95 # copy most but not all extensions
96 0 ▶ if (my $ext = $hash->{ext}) {
Subroutine __ANON__: not called CC=5 SCAR=34.0
Branch: 1 branch
BranchTrueFalseTotal
if (my $ext = $hash->{"ext"}) 0 0 0
97 @$ext = grep {
98 0 ▶ defined($_->{sn}) && $_->{sn} !~m{^(?:
Branch: 1 branch
BranchTrueFalseTotal
defined $_->{"sn"} and not $_->{"sn"} =~ /^(?: authorityInfoAccess | subjectKeyIdentifier | authorityKeyIdentifier | certificatePolicies | crlDistributionPoints )$/x 0 0 0
99 authorityInfoAccess |
100 subjectKeyIdentifier |
101 authorityKeyIdentifier |
102 certificatePolicies |
103 crlDistributionPoints
104 )$}x
105 } @$ext;
106 }
107 my ($clone,$key) = CERT_create(
108 %$hash,
109 ignore_invalid_args => 1,
110 issuer_cert => $self->{cacert},
111 issuer_key => $self->{cakey},
112 key => $self->{certkey},
113 serial =>
114 ! defined($self->{serial}) ? (unpack('L',$hash->{x509_digest_sha256}))[0] :
115 ref($self->{serial}) eq 'CODE' ? $self->{serial}($old_cert,$hash) :
116 ++$self->{serial},
117 0 ▶ );
Branch: 2 branches
BranchTrueFalseTotal
!defined($self->{'serial'}) ? : 0 0 0
ref $self->{'serial'} eq 'CODE' ? : 0 0 0
118 0 return ($clone,$key);
119 0 };
120  
121 0 ▶ $clone_key ||= substr(unpack("H*", $hash->{x509_digest_sha256}),0,32);
Condition0%0/3
$clone_key ||= substr(unpack("H*", $hash->{"x509_digest_sha256"}), 0, 32)
l!l&&r!l&&!r
000
MC/DC0%0/2
$clone_key ||= substr(unpack("H*", $hash->{"x509_digest_sha256"}), 0, 32)
$clone_key substr(unpack("H*", $hash->{"x509_digest_sha256"}), 0, 32)
Truth table0%0/3
A ||= B
A= $clone_keyB= substr(unpack("H*", $hash->{"x509_digest_sha256"}), 0, 32)
ABresult
1X1
011
000
122 0 my $c = $self->{cache};
123 0 ▶ return $c->($clone_key,$create_cb) if ref($c) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $c eq "CODE" 0 0 0
124  
125 0 ▶ my $e = $c->{$clone_key} ||= do {
Condition0%0/3
$c->{$clone_key} ||= do { () = &$create_cb; +{"cert", $cert, "key", $key} }
l!l&&r!l&&!r
000
MC/DC0%0/2
$c->{$clone_key} ||= do { () = &$create_cb; +{"cert", $cert, "key", $key} }
$c->{$clone_key} do { () = &$create_cb; +{"cert", $cert, "key", $key} }
Truth table0%0/3
A ||= B
A= $c->{$clone_key}B= do { () = &$create_cb; +{"cert", $cert, "key", $key} }
ABresult
1X1
011
000
126 0 my ($cert,$key) = &$create_cb;
127 0 { cert => $cert, key => $key };
128 };
129 0 $e->{atime} = time();
130 0 return ($e->{cert},$e->{key});
131 }
132  
133  
134 0 ▶sub STORABLE_freeze { my $self = shift; $self->serialize() }
Subroutine STORABLE_freeze: not called CC=1 SCAR=6.9
Pod undocumented
135 0 ▶sub STORABLE_thaw { my ($class,undef,$data) = @_; $class->unserialize($data) }
Subroutine STORABLE_thaw: not called CC=1 SCAR=6.9
Pod undocumented
136  
137 sub serialize {
138 0 ▶ my $self = shift;
Subroutine serialize: not called CC=5 SCAR=34.0
Pod documented
139 0 my $data = pack("N",2); # version
140 0 $data .= pack("N/a", PEM_cert2string($self->{cacert}));
141 0 $data .= pack("N/a", PEM_key2string($self->{cakey}));
142 0 ▶ if ( $self->{certkey} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'certkey'}) { } 0 0 0
143 0 $data .= pack("N/a", PEM_key2string($self->{certkey}));
144 } else {
145 0 $data .= pack("N/a", '');
146 }
147 0 $data .= pack("N",$self->{serial});
148 0 ▶ if ( ref($self->{cache}) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"cache"} eq "HASH") 0 0 0
149 0 ▶ while ( my($k,$v) = each %{ $self->{cache}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"cache"};}) 0 0 0
150 $data .= pack("N/aN/aN/aN", $k,
151 PEM_cert2string($k->{cert}),
152 $k->{key} ? PEM_key2string($k->{key}) : '',
153 0 ▶ $k->{atime});
Branch: 1 branch
BranchTrueFalseTotal
$k->{'key'} ? : 0 0 0
154 }
155 }
156 0 return $data;
157 }
158  
159 sub unserialize {
160 0 ▶ my ($class,$data) = @_;
Subroutine unserialize: not called CC=7 SCAR=40.3
Pod documented
161 0 ▶ unpack("N",substr($data,0,4,'')) == 2 or
Branch: 1 branch
BranchTrueFalseTotal
unpack("N", substr($data, 0, 4, '')) == 2 or croak("serialized with wrong version") 0 0 0
162 croak("serialized with wrong version");
163 0 ( my $cacert,my $cakey,my $certkey,my $serial,$data)
164 = unpack("N/aN/aN/aNa*",$data);
165 0 ▶ my $self = bless {
Branch: 1 branch
BranchTrueFalseTotal
$certkey ? : 0 0 0
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
166 serial => $serial,
167 cacert => PEM_string2cert($cacert),
168 cakey => PEM_string2key($cakey),
169 $certkey ? ( certkey => PEM_string2key($certkey)):(),
170 }, ref($class)||$class;
171  
172 0 ▶ $self->{cache} = {} if $data ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $data ne "" 0 0 0
173 0 ▶ while ( $data ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data ne "") 0 0 0
174 0 (my $key,my $cert,my $certkey, my $atime,$data) = unpack("N/aN/aNa*",$data);
175 0 ▶ $self->{cache}{$key} = {
Branch: 1 branch
BranchTrueFalseTotal
$key ? : 0 0 0
176 cert => PEM_string2cert($cert),
177 $key ? ( key => PEM_string2key($certkey)):(),
178 atime => $atime
179 };
180 }
181 0 return $self;
182 }
183  
184 1 1;
185  
186 __END__
« SSL/PublicSuffix.pm ↑ Summary