IMP/Example/LogServerCertificate.pm

filter: Statementstmt 28.0% 25 / 89 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 57.1% 8 / 14 Podpod 66.6% 2 / 3 totaltotal 24.4% 35 / 143 CC 19 SCAR 38.0
CC
19
Cov
20%
data
110.0
_read_ssl_handshake
42.0
_cert2line
12.0
CRAP 44.9
« IMP/Pattern.pm ↑ Summary IMP/Base.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  
4 package Net::IMP::Example::LogServerCertificate;
5 1 ▶use base 'Net::IMP::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Net::SSLeay;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 use fields (
9 1 'done', # done or no SSL
10 'sbuf', # buffer on server side
11 1 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use Net::IMP qw(:log :DEFAULT); # import IMP_ constants
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Net::IMP::Debug;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 sub INTERFACE {
18 return ([
19 undef,
20 0 ▶ [ IMP_PASS, IMP_PREPASS, IMP_LOG ]
Subroutine INTERFACE: not called CC=1 SCAR=6.9
Pod undocumented
21 ])
22 }
23  
24  
25 # create new analyzer object
26 sub new_analyzer {
27 0 ▶ my ($factory,%args) = @_;
Subroutine new_analyzer: not called CC=1 SCAR=6.9
Pod documented
28 0 my $self = $factory->SUPER::new_analyzer(%args);
29  
30 0 $self->run_callback(
31 # we are not interested in data from client
32 [ IMP_PASS, 0, IMP_MAXOFFSET ],
33 # and we will not change data from server, only inspect
34 [ IMP_PREPASS, 1, IMP_MAXOFFSET ],
35 );
36  
37 0 $self->{sbuf} = '';
38 0 return $self;
39 }
40  
41 sub data {
42 0 ▶ my ($self,$dir,$data) = @_;
Subroutine data: not called CC=10 SCAR=47.0
Pod documented
43 0 ▶ return if $dir == 0; # should not happen
Branch: 1 branch
BranchTrueFalseTotal
if $dir == 0 0 0 0
44 0 ▶ return if $self->{done}; # done or no SSL
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"done"} 0 0 0
45 0 ▶ return if $data eq ''; # eof from server
Branch: 1 branch
BranchTrueFalseTotal
if $data eq "" 0 0 0
46  
47 0 my $buf = $self->{sbuf} .= $data;
48  
49 0 ▶ if ( _read_ssl_handshake($self,\$buf,2) # Server Hello
Branch: 1 branch
BranchTrueFalseTotal
if (_read_ssl_handshake($self, \$buf, 2) and my $certs = _read_ssl_handshake($self, \$buf, 11)) 0 0 0
Condition0%0/3
_read_ssl_handshake($self, \$buf, 2) and my $certs = _read_ssl_handshake($self, \$buf, 11)
!ll&&!rl&&r
000
MC/DC0%0/2
_read_ssl_handshake($self, \$buf, 2) and my $certs = _read_ssl_handshake($self, \$buf, 11)
_read_ssl_handshake($self, \$buf, 2) my $certs = _read_ssl_handshake($self, \$buf, 11)
Truth table0%0/3
A and B
A= _read_ssl_handshake($self, \$buf, 2)B= my $certs = _read_ssl_handshake($self, \$buf, 11)
ABresult
0X0
100
111
50 and my $certs = _read_ssl_handshake($self,\$buf,11) # Certificates
51 ) {
52 0 $self->{done} = 1;
53  
54 0 my ($len) = unpack("xa3",substr($certs,0,4,''));
55 0 $len = unpack("N","\0$len");
56 0 substr($certs,$len) = '';
57 0 $len = unpack("N","\0".substr($certs,0,3,''));
58 0 substr($certs,$len) = '';
59 0 my $i = 0;
60 0 ▶ while ($certs ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($certs ne "") 0 0 0
61 0 my $clen = unpack("N","\0".substr($certs,0,3,''));
62 0 my $cert = substr($certs,0,$clen,'');
63 0 ▶ length($cert) == $clen or
Branch: 1 branch
BranchTrueFalseTotal
length $cert == $clen or die "invalid certificate length ($clen vs. " . length($cert) . ')' 0 0 0
64 die "invalid certificate length ($clen vs. ".length($cert).")";
65 0 ▶ if ( my $line = eval { _cert2line($cert) } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = eval { do { _cert2line($cert) } }) { } 0 0 0
66 0 $self->run_callback([ IMP_LOG,1,0,0,IMP_LOG_INFO,
67 sprintf("chain[%d]: %s",$i,$line)]);
68 } else {
69 0 warn "failed to convert cert to string: $@";
70 }
71 0 $i++;
72 }
73 }
74  
75 $self->run_callback([ IMP_PASS,1,IMP_MAXOFFSET ])
76 0 ▶ if $self->{done};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"done"} 0 0 0
77 }
78  
79 sub _cert2line {
80 0 ▶ my $der = shift;
Subroutine _cert2line: not called CC=3 SCAR=24.8
81 0 my $bio = Net::SSLeay::BIO_new( Net::SSLeay::BIO_s_mem());
82 0 Net::SSLeay::BIO_write($bio,$der);
83 0 my $cert = Net::SSLeay::d2i_X509_bio($bio);
84 0 Net::SSLeay::BIO_free($bio);
85 0 ▶ $cert or die "cannot parse certificate: ".
Branch: 1 branch
BranchTrueFalseTotal
$cert or die 'cannot parse certificate: ' . &Net::SSLeay::ERR_error_string(scalar &Net::SSLeay::ERR_get_error()) 0 0 0
86 Net::SSLeay::ERR_error_string(Net::SSLeay::ERR_get_error());
87 0 my $not_before = Net::SSLeay::X509_get_notBefore($cert);
88 0 my $not_after = Net::SSLeay::X509_get_notAfter($cert);
89 0 $_ = Net::SSLeay::P_ASN1_TIME_put2string($_) for($not_before,$not_after);
90 0 my $subject = Net::SSLeay::X509_NAME_oneline(
91 Net::SSLeay::X509_get_subject_name($cert));
92 0 return "$subject | $not_before - $not_after";
93 }
94  
95  
96 sub _read_ssl_handshake {
97 0 ▶ my ($self,$buf,$expect_htype) = @_;
Subroutine _read_ssl_handshake: not called CC=6 SCAR=37.4
98 0 ▶ return if length($$buf) < 22; # need way more data
Branch: 1 branch
BranchTrueFalseTotal
if length $$buf < 22 0 0 0
99  
100 0 my ($ctype,$version,$len,$htype) = unpack('CnnC',$$buf);
101 0 ▶ if ($ctype != 22) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ctype != 22) { } 0 0 0
elsif ($len > 16384.0) { } 0 0 0
elsif ($htype != $expect_htype) { } 0 0 0
102 0 debug("no SSL >=3.0 handshake record");
103 0 goto bad;
104 } elsif ( $len > 2**14 ) {
105 0 debug("length looks way too big - assuming no ssl");
106 0 goto bad;
107 } elsif ( $htype != $expect_htype ) {
108 0 debug("unexpected handshake type $htype - assuming no ssl");
109 0 goto bad;
110 }
111  
112 0 ▶ length($$buf)-5 >= $len or return; # need more data
Branch: 1 branch
BranchTrueFalseTotal
length($$buf) - 5 >= $len or return 0 0 0
113 0 substr($$buf,0,5,'');
114 0 debug("got handshake type $htype length $len");
115 0 return substr($$buf,0,$len,'');
116  
117 bad:
118 0 $self->{done} = 1;
119 0 return;
120 }
121  
122  
123 # debugging stuff
124 sub _hexdump {
125 0 ▶ my ($buf,$len) = @_;
Subroutine _hexdump: not called CC=3 SCAR=24.8
126 0 ▶ $buf = substr($buf,0,$len) if $len;
Branch: 1 branch
BranchTrueFalseTotal
if $len 0 0 0
127 0 my @hx = map { sprintf("%02x",$_) } unpack('C*',$buf);
128 0 my $t = '';
129 0 ▶ while (@hx) {
Branch: 1 branch
BranchTrueFalseTotal
if (@hx) 0 0 0
130 0 $t .= join(' ',splice(@hx,0,16))."\n";
131 }
132 0 return $t;
133 }
134  
135  
136 1 1;
137  
138 __END__
« IMP/Pattern.pm ↑ Summary IMP/Base.pm »