blib/lib/Mail/DKIM/Iterator.pm

filter: Statementstmt 82.5% 411 / 498 Branchbran 67.7% 195 / 288 Conditioncond 49.1% 83 / 169 MC/DCmcdc 21.8% 21 / 96 Subroutinesub 82.3% 42 / 51 Podpod 100% 9 / 9 totaltotal 68.5% 761 / 1111 CC 228 SCAR 39.4
CC
228
Cov
72%
parse_signature
198.1
sign
53.7
parse_dkimkey
35.0
CRAP 51.6
↑ Summary
1 package Mail::DKIM::Iterator;
2 2 ▶use v5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 our $VERSION = '1.009';
5  
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Crypt::OpenSSL::RSA;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Scalar::Util 'dualvar';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # critical header fields which should be well protected
12 2 my @critical_headers = qw(from subject content-type content-transfer-encoding);
13 2 my $critical_headers_rx = do {
14 2 my $rx = join("|",@critical_headers);
15 2 qr{$rx}i;
16 };
17 # all header fields which should be included in the signature
18 2 my @sign_headers = (@critical_headers, 'to', 'cc', 'date');
19  
20 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 our @EXPORT = qw(
22 DKIM_POLICY
23 DKIM_PERMERROR
24 DKIM_NEUTRAL
25 DKIM_TEMPERROR
26 DKIM_FAIL
27 DKIM_PASS
28 );
29  
30 use constant {
31 2 DKIM_POLICY => dualvar(-4,'policy'),
32 DKIM_PERMERROR => dualvar(-3,'permerror'),
33 DKIM_NEUTRAL => dualvar(-2,'neutral'),
34 DKIM_TEMPERROR => dualvar(-1,'temperror'),
35 DKIM_FAIL => dualvar( 0,'fail'),
36 DKIM_PASS => dualvar( 1,'pass'),
37 2 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 # compability to versions 1.003 and lower
40 2 push @EXPORT, qw(
41 DKIM_INVALID_HDR
42 DKIM_TEMPFAIL
43 DKIM_SOFTFAIL
44 DKIM_PERMFAIL
45 DKIM_SUCCESS
46 );
47  
48 use constant {
49 2 DKIM_INVALID_HDR => DKIM_PERMERROR,
50 DKIM_TEMPFAIL => DKIM_TEMPERROR,
51 DKIM_SOFTFAIL => DKIM_NEUTRAL,
52 DKIM_PERMFAIL => DKIM_FAIL,
53 DKIM_SUCCESS => DKIM_PASS,
54 2 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56  
57 # create new object
58 sub new {
59 44 ▶ my ($class,%args) = @_;
Subroutine new: called CC=9 SCAR=22.2
Pod documented
60 my $self = bless {
61 records => $args{dns} || {}, # mapping (dnsname,dkim_key)
62 extract_sig => 1, # extract signatures from mail header
63 filter => $args{filter}, # filter which signatures are relevant
64 # sig => [...], # list of signatures from header or to sign
65 44 ▶ _hdrbuf => '', # used while collecting the mail header
Condition50%1/2
$args{'dns'} || {}
l!l
044
MC/DC0%0/1
$args{'dns'} || {}
$args{'dns'}
Truth table50%1/2
A
A= $args{'dns'}
Aresult
11
00
66 }, $class;
67  
68 44 ▶ if (my $sig = delete $args{sign}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sig = delete $args{"sign"}) 22 22 44
69 # signatures given for signing, either as [{},...] or {}
70 # add to self.sig
71 22 ▶ $sig = [$sig] if ref($sig) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $sig ne "ARRAY" 22 0 22
72 22 $self->{extract_sig} = delete $args{sign_and_verify};
73 22 my $error;
74 22 for(@$sig) {
75 22 ▶ $_->{h} //= 'from' if ref($_); # minimal
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ 22 0 22
Condition100%2/2
$_->{"h"} //= "from"
l!l
220
MC/DC100%1/1
$_->{"h"} //= "from"
$_->{"h"}
Truth table100%2/2
A
A= $_->{"h"}
Aresult
11
00
76 22 my $s = parse_signature($_,\$error,1);
77 22 ▶ die "bad signature '$_': $error" if !$s;
Branch: 1 branch
BranchTrueFalseTotal
unless $s 0 22 22
78 22 ▶ $s->{h_auto} //= 1; # secure version will be detected based on mail
Condition100%2/2
$s->{"h_auto"} //= 1
l!l
220
MC/DC100%1/1
$s->{"h_auto"} //= 1
$s->{"h_auto"}
Truth table100%2/2
A
A= $s->{"h_auto"}
Aresult
11
00
79 22 push @{$self->{sig}}, $s
80 }
81 }
82 44 return $self;
83 }
84  
85 # Iterator: feed object with information and get back what to do next
86 sub next {
87 104 ▶ my $self = shift;
Subroutine next: called CC=21 SCAR=31.5
Pod documented
88 104 my $rv;
89 104 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 104 104 208
90 104 my $arg = shift;
91 104 ▶ if (ref($arg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg) { } 19 85 104
92 # ref: mapping (host,dkim_key)
93 19 ▶ while (my ($k,$v) = each %$arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$arg) 19 19 38
94 19 $self->{records}{$k} = $v;
95 }
96 19 $rv = _compute_result($self);
97 } else {
98 # string: append data from mail
99 85 ▶ if (defined $self->{_hdrbuf}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'_hdrbuf'}) { } 44 41 85
100 # header not fully read: append and try to find end of header
101 44 $self->{_hdrbuf} .= $arg;
102 44 ▶ $self->{_hdrbuf} =~m{(\r?\n)\1}g or last; # no end of header
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_hdrbuf"} =~ /(\r?\n)\1/g or last 0 44 44
103  
104 # Extract header into self.header and look for DKIM signatures
105 # inside. The rest of _hdrbuf will be appended as part of the
106 # body and the attribute _hdrbuf itself is no longer needed
107 44 $self->{header} = substr($self->{_hdrbuf},0,$+[0],'');
108 44 ▶ if ($self->{extract_sig}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"extract_sig"} and my(@sig) = _parse_header($self->{"header"})) 22 22 44
Condition66%2/3
$self->{"extract_sig"} and my(@sig) = _parse_header($self->{"header"})
!ll&&!rl&&r
22022
MC/DC50%1/2
$self->{"extract_sig"} and my(@sig) = _parse_header($self->{"header"})
$self->{"extract_sig"} my(@sig) = _parse_header($self->{"header"})
Truth table66%2/3
A and B
A= $self->{"extract_sig"}B= my(@sig) = _parse_header($self->{"header"})
ABresult
0X0
100
111
109 and my @sig = _parse_header($self->{header})) {
110 22 ▶ if (my $f = $self->{filter}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = $self->{"filter"}) 0 22 22
111 0 @sig = grep { $f->($_,$self->{header}) } @sig;
112 }
113 22 ▶ push @{$self->{sig} ||= []}, @sig if @sig;
Branch: 1 branch
BranchTrueFalseTotal
if @sig 22 0 22
Condition50%1/2
$self->{"sig"} ||= []
l!l
022
MC/DC0%0/1
$self->{"sig"} ||= []
$self->{"sig"}
Truth table50%1/2
A
A= $self->{"sig"}
Aresult
11
00
114 }
115 44 $arg = delete $self->{_hdrbuf};
116 44 ▶ _append_body($self,$arg) if $arg ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $arg ne "" 44 0 44
117  
118 } else {
119 # header already read: append as part of body
120 41 _append_body($self,$arg);
121 }
122  
123 85 ▶ if (!$self->{sig}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'sig'}) { } 0 85 85
124 # No signatures found in body -> empty return list
125 0 $rv = [];
126 } else {
127 85 $rv = _compute_result($self);
128 }
129 }
130 }
131 104 ▶ $rv = _compute_result($self) if ! @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 104 0 104
132  
133 # If we have no results yet just return that we need more data
134 104 ▶ $rv or return ([],\'');
Branch: 1 branch
BranchTrueFalseTotal
$rv or return [], \"" 0 104 104
135  
136 # Extract the DNS names for the partial results where the DKIM key is needed
137 # and return the as todo. If the body hash could not yet computed for a
138 # signature mark also that we need more data
139 104 my (%need_dns,$need_more_data);
140 104 for(@$rv) {
141 104 ▶ $_->status and next;
Branch: 1 branch
BranchTrueFalseTotal
$_->status and next 44 60 104
142 60 my $sig = $_->sig;
143  
144 # Need more data to compute the body hash?
145 60 ▶ $need_more_data = 1 if !$sig->{'bh:computed'};
Branch: 1 branch
BranchTrueFalseTotal
unless $sig->{"bh:computed"} 41 19 60
146  
147 # Need to get DKIM key to validate signature?
148 # Only if we have sig.b, i.e. an extracted signature from the header.
149 60 ▶ if ($sig->{b}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sig->{"b"}) 38 22 60
150 38 my $name = $_->dnsname;
151 38 ▶ $need_dns{$name}++ if ! $self->{records}{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"records"}{$name} 38 0 38
152 }
153 }
154  
155 # return preliminary results and @todo
156 104 ▶ return ($rv,$need_more_data ? (\''):(),sort keys %need_dns);
Branch: 1 branch
BranchTrueFalseTotal
$need_more_data ? : 41 63 104
157 }
158  
159 sub filter {
160 0 ▶ my ($self,$filter) = @_;
Subroutine filter: not called CC=3 SCAR=24.8
Pod documented
161 0 $self->{filter} = $filter;
162 0 @{$self->{sig}} = grep { $filter->($_,$self->{header}) } @{$self->{sig}}
163 0 ▶ if $self->{header} && $self->{sig};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"header"} and $self->{"sig"} 0 0 0
Condition0%0/3
$self->{"header"} and $self->{"sig"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"header"} and $self->{"sig"}
$self->{"header"} $self->{"sig"}
Truth table0%0/3
A and B
A= $self->{"header"}B= $self->{"sig"}
ABresult
0X0
100
111
164 }
165  
166 sub result {
167 0 ▶ my $self = shift;
Subroutine result: not called CC=1 SCAR=6.9
Pod documented
168 0 return $self->{_last_result};
169 }
170  
171 sub authentication_results {
172 return join(";\n",map {
173 0 my $ar = $_->authentication_results;
174 0 ▶ $ar ? (' '.$ar) : (),
Branch: 1 branch
BranchTrueFalseTotal
$ar ? : 0 0 0
175 0 ▶ } @{shift->result || []});
Subroutine authentication_results: not called CC=3 SCAR=24.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
(shift())->result or [] 0 0 0
176 }
177  
178 # Compute result based on current data.
179 # This might add more DKIM records to validate signatures.
180 sub _compute_result {
181 208 ▶ my $self = shift;
Subroutine _compute_result: called CC=23 SCAR=31.9
182 208 ▶ return if defined $self->{_hdrbuf}; # need more header
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_hdrbuf"} 0 208 208
183 208 ▶ return [] if !$self->{sig}; # nothing to verify
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"sig"} 0 208 208
184  
185 208 my @rv;
186 208 for my $sig (@{$self->{sig}}) {
187  
188 # use final result if we have one already
189 208 ▶ if ($sig->{':result'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sig->{":result"}) 44 164 208
190 44 push @rv, $sig->{':result'};
191 44 next;
192 }
193  
194 164 ▶ if ($sig->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sig->{"error"}) 1 163 164
195 # something wrong with the DKIM-Signature header, return error
196 push @rv, $sig->{':result'} =
197 Mail::DKIM::Iterator::VerifyRecord->new(
198 $sig,
199 ($sig->{s}//'UNKNOWN')."_domainkey".($sig->{d}//'UNKNOWN'),
200 DKIM_PERMERROR,
201 $sig->{error}
202 1 ▶ );
Condition50%2/4
$sig->{"s"} // "UNKNOWN"
l!l
01
$sig->{"d"} // "UNKNOWN"
l!l
01
MC/DC0%0/1
$sig->{"s"} // "UNKNOWN"
$sig->{"s"}
MC/DC0%0/1
$sig->{"d"} // "UNKNOWN"
$sig->{"d"}
Truth table50%1/2
A
A= $sig->{"s"}
Aresult
11
00
Truth table50%1/2
A
A= $sig->{"d"}
Aresult
11
00
203 1 next;
204 }
205  
206 163 ▶ if (!$sig->{b}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sig->{"b"}) 66 97 163
207 # sig is not for verification but for signing
208 66 ▶ if (!$sig->{'bh:computed'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $sig->{'bh:computed'}) { } 44 22 66
209 # incomplete: still need more data to compute signature
210 44 push @rv, Mail::DKIM::Iterator::SignRecord->new($sig);
211 } else {
212 # complete: compute signature and save it in :result
213 22 my $err;
214 22 my $dkim_sig = sign($sig,$sig->{':key'},$self->{header},\$err);
215 22 ▶ push @rv, $sig->{':result'} =
Branch: 1 branch
BranchTrueFalseTotal
$dkim_sig ? : 22 0 22
216 Mail::DKIM::Iterator::SignRecord->new(
217 $dkim_sig ? ($sig,$dkim_sig,DKIM_PASS)
218 : ($sig,undef,DKIM_FAIL,$err)
219 );
220 }
221 66 next;
222 }
223  
224 97 my $dns = "$sig->{s}._domainkey.$sig->{d}";
225  
226 97 ▶ if ($sig->{x} && $sig->{x} < time()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sig->{"x"} and $sig->{"x"} < time) 1 96 97
Condition66%2/3
$sig->{"x"} and $sig->{"x"} < time
!ll&&!rl&&r
9601
MC/DC50%1/2
$sig->{"x"} and $sig->{"x"} < time
$sig->{"x"} $sig->{"x"} < time
Truth table66%2/3
A and B
A= $sig->{"x"}B= $sig->{"x"} < time
ABresult
0X0
100
111
227 1 push @rv, $sig->{':result'} = Mail::DKIM::Iterator::VerifyRecord
228 ->new($sig,$dns, DKIM_POLICY, "signature e[x]pired");
229 1 next;
230 }
231  
232 96 my $txt = $self->{records}{$dns};
233 96 ▶ if ($txt and !ref($txt) || ref($txt) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($txt and !ref($txt) || ref $txt eq 'ARRAY') { } 18 78 96
elsif (exists $self->{'records'}{$dns} and not $txt) { } 1 154 155
Condition50%3/6
$txt and !ref($txt) || ref $txt eq "ARRAY"
!ll&&!rl&&r
78018
not ref $txt or ref $txt eq "ARRAY"
l!l&&r!l&&!r
1800
MC/DC33%1/3
$txt and !ref($txt) || ref $txt eq "ARRAY"
$txt not ref $txt ref $txt eq "ARRAY"
Truth table50%2/4
A and not(B) or C
A= $txtB= not ref $txtC= ref $txt eq "ARRAY"
ABCresult
0XX0
1000
11X1
1011
234 # Take the first syntactically valid DKIM key from the list of
235 # TXT records.
236 18 my $error = "no TXT records";
237 18 ▶ for(ref($txt) ? @$txt:$txt) {
Branch: 1 branch
BranchTrueFalseTotal
ref $txt ? : 0 18 18
238 18 ▶ if (my $r = parse_dkimkey($_,\$error)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = parse_dkimkey($_, \$error)) 17 1 18
239 17 $self->{records}{$dns} = $txt = $r;
240 17 $error = undef;
241 17 last;
242 }
243 }
244 18 ▶ $self->{records}{$dns} = $txt = { permfail => $error }
Branch: 1 branch
BranchTrueFalseTotal
if $error 1 17 18
245 if $error;
246 ▶ } elsif (exists $self->{records}{$dns} && ! $txt) {
Condition66%2/3
exists $self->{"records"}{$dns} and not $txt
!ll&&!rl&&r
7701
MC/DC50%1/2
exists $self->{"records"}{$dns} and not $txt
exists $self->{"records"}{$dns} not $txt
Truth table66%2/3
A and not(B)
A= exists $self->{"records"}{$dns}B= not $txt
ABresult
0X0
100
111
247 1 $self->{records}{$dns} = $txt = { tempfail => "dns lookup failed" }
248 }
249  
250 96 my @v = _verify_sig($sig,$txt);
251 96 push @rv, Mail::DKIM::Iterator::VerifyRecord->new($sig,$dns,@v);
252 96 ▶ $sig->{':result'} = $rv[-1] if @v; # we got a final result
Branch: 1 branch
BranchTrueFalseTotal
if @v 20 76 96
253 }
254 208 return ($self->{_last_result} = \@rv);
255 }
256  
257 # Parse DKIM-Signature value into hash and fill in necessary default values.
258 # Input can be string or hash.
259 sub parse_signature {
260 66 ▶ my ($v,$error,$for_signing) = @_;
Subroutine parse_signature: called CC=45 SCAR=52.9
Pod documented
261 66 ▶ $v = parse_taglist($v,$error) or return if !ref($v);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $v 22 44 66
Condition60%3/5
ref $v || (($v = parse_taglist($v, $error)) || (return))
l!l&&r!l&&!r
44220
$v = parse_taglist($v, $error) or return
l!l
220
MC/DC0%0/2
ref $v || (($v = parse_taglist($v, $error)) || (return))
ref $v $v = parse_taglist($v, $error)
Truth table66%2/3
A || B
A= ref $vB= $v = parse_taglist($v, $error)
ABresult
1X1
011
000
262  
263 66 ▶ if ($for_signing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($for_signing) 44 22 66
264 # some defaults
265 44 ▶ $v->{v} //= '1';
Condition100%2/2
$v->{"v"} //= 1
l!l
2222
MC/DC100%1/1
$v->{"v"} //= 1
$v->{"v"}
Truth table100%2/2
A
A= $v->{"v"}
Aresult
11
00
266 }
267  
268 66 ▶ if (($v->{v}//'') ne '1') {
Branch: 6 branches
BranchTrueFalseTotal
if (($v->{'v'} // '') ne '1') { } 1 65 66
elsif (not $v->{'d'}) { } 0 65 65
elsif (not $v->{'s'}) { } 0 65 65
elsif (not $v->{'h'}) { } 0 65 65
elsif ($v->{'l'} and not $v->{'l'} =~ /^\d{1,76}\z/) { } 0 65 65
elsif (do { $v->{'q'} = lc($v->{'q'} // 'dns/txt'); $v->{'q'} ne 'dns/txt' }) { } 0 65 65
Condition50%1/2
$v->{"v"} // ""
l!l
660
MC/DC0%0/1
$v->{"v"} // ""
$v->{"v"}
Truth table50%1/2
A
A= $v->{"v"}
Aresult
11
00
269 1 ▶ $$error = "bad DKIM signature version: ".($v->{v}||'<undef>');
Condition50%1/2
$v->{'v'} || '<undef>'
l!l
10
MC/DC0%0/1
$v->{'v'} || '<undef>'
$v->{'v'}
Truth table50%1/2
A
A= $v->{'v'}
Aresult
11
00
270 } elsif (!$v->{d}) {
271 0 $$error = "required [d]omain not given";
272 } elsif (!$v->{s}) {
273 0 $$error = "required [s]elector not given";
274 } elsif (!$v->{h}) {
275 0 $$error = "required [h]eader fields not given";
276 ▶ } elsif ($v->{l} && $v->{l} !~m{^\d{1,76}\z}) {
Condition66%2/3
$v->{"l"} and not $v->{"l"} =~ /^\d{1,76}\z/
!ll&&!rl&&r
6320
MC/DC0%0/2
$v->{"l"} and not $v->{"l"} =~ /^\d{1,76}\z/
$v->{"l"} not $v->{"l"} =~ /^\d{1,76}\z/
Truth table66%2/3
A and not(B)
A= $v->{"l"}B= not $v->{"l"} =~ /^\d{1,76}\z/
ABresult
0X0
100
111
277 0 $$error = "invalid body [l]ength";
278 } elsif (do {
279 65 ▶ $v->{q} = lc($v->{q}//'dns/txt');
Condition100%2/2
$v->{"q"} // "dns/txt"
l!l
4322
MC/DC100%1/1
$v->{"q"} // "dns/txt"
$v->{"q"}
Truth table100%2/2
A
A= $v->{"q"}
Aresult
11
00
280 65 $v->{q} ne 'dns/txt'
281 }) {
282 0 $$error = "unsupported query method $v->{q}";
283 }
284 66 ▶ return if $$error;
Branch: 1 branch
BranchTrueFalseTotal
if $$error 1 65 66
285  
286 65 $v->{d} = lc($v->{d});
287 65 ▶ $v->{a} = lc($v->{a}//'rsa-sha256');
Condition100%2/2
$v->{"a"} // "rsa-sha256"
l!l
5510
MC/DC100%1/1
$v->{"a"} // "rsa-sha256"
$v->{"a"}
Truth table100%2/2
A
A= $v->{"a"}
Aresult
11
00
288 65 ▶ $v->{c} = lc($v->{c}//'simple/simple');
Condition100%2/2
$v->{"c"} // "simple/simple"
l!l
5510
MC/DC100%1/1
$v->{"c"} // "simple/simple"
$v->{"c"}
Truth table100%2/2
A
A= $v->{"c"}
Aresult
11
00
289  
290 65 my @h = split(/\s*:\s*/,lc($v->{h}));
291 $$error = "'from' missing from [h]eader fields"
292 65 ▶ if ! grep { $_ eq 'from' } @h;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq "from";} @h 0 65 65
293 65 $v->{'h:list'} = \@h;
294  
295 65 ▶ if ($for_signing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($for_signing) { } 44 21 65
296 44 delete $v->{b};
297 44 delete $v->{bh};
298 44 ▶ $v->{t} = undef if exists $v->{t};
Branch: 1 branch
BranchTrueFalseTotal
if exists $v->{"t"} 0 44 44
299 44 ▶ if (defined $v->{x} && $v->{x} !~m{^\+?\d{1,12}\z}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v->{"x"} and not $v->{"x"} =~ /^\+?\d{1,12}\z/) 0 44 44
Condition66%2/3
defined $v->{"x"} and not $v->{"x"} =~ /^\+?\d{1,12}\z/
!ll&&!rl&&r
4220
MC/DC0%0/2
defined $v->{"x"} and not $v->{"x"} =~ /^\+?\d{1,12}\z/
defined $v->{"x"} not $v->{"x"} =~ /^\+?\d{1,12}\z/
Truth table66%2/3
A and not(B)
A= defined $v->{"x"}B= not $v->{"x"} =~ /^\+?\d{1,12}\z/
ABresult
0X0
100
111
300 0 $$error = "invalid e[x]piration time";
301 }
302 } else {
303 21 ▶ if (!$v->{b} or not $v->{'b:bin'} = _decode64($v->{b})) {
Branch: 4 branches
BranchTrueFalseTotal
if (not $v->{'b'} && ($v->{'b:bin'} = _decode64($v->{'b'}))) { } 0 21 21
elsif (not $v->{'bh'} && ($v->{'bh:bin'} = _decode64($v->{'bh'}))) { } 0 21 21
elsif ($v->{'t'} and not $v->{'t'} =~ /^\d{1,12}\z/) { } 0 21 21
elsif ($v->{'x'}) { } 1 20 21
Condition33%1/3
$v->{'b'} && ($v->{'b:bin'} = _decode64($v->{'b'}))
!ll&&!rl&&r
0021
MC/DC0%0/2
$v->{'b'} && ($v->{'b:bin'} = _decode64($v->{'b'}))
$v->{'b'} ($v->{'b:bin'} = _decode64($v->{'b'}))
Truth table33%1/3
A && B
A= $v->{'b'}B= ($v->{'b:bin'} = _decode64($v->{'b'}))
ABresult
0X0
100
111
304 0 ▶ $$error = "invalid body signature: ".($v->{b}||'<undef>');
Condition0%0/2
$v->{'b'} || '<undef>'
l!l
00
MC/DC0%0/1
$v->{'b'} || '<undef>'
$v->{'b'}
Truth table0%0/2
A
A= $v->{'b'}
Aresult
11
00
305 ▶ } elsif (!$v->{bh} or not $v->{'bh:bin'} = _decode64($v->{bh})) {
Condition33%1/3
$v->{'bh'} && ($v->{'bh:bin'} = _decode64($v->{'bh'}))
!ll&&!rl&&r
0021
MC/DC0%0/2
$v->{'bh'} && ($v->{'bh:bin'} = _decode64($v->{'bh'}))
$v->{'bh'} ($v->{'bh:bin'} = _decode64($v->{'bh'}))
Truth table33%1/3
A && B
A= $v->{'bh'}B= ($v->{'bh:bin'} = _decode64($v->{'bh'}))
ABresult
0X0
100
111
306 0 ▶ $$error = "invalid header signature: ".($v->{bh}||'<undef>');
Condition0%0/2
$v->{'bh'} || '<undef>'
l!l
00
MC/DC0%0/1
$v->{'bh'} || '<undef>'
$v->{'bh'}
Truth table0%0/2
A
A= $v->{'bh'}
Aresult
11
00
307 ▶ } elsif ($v->{t} && $v->{t} !~m{^\d{1,12}\z}) {
Condition33%1/3
$v->{"t"} and not $v->{"t"} =~ /^\d{1,12}\z/
!ll&&!rl&&r
2100
MC/DC0%0/2
$v->{"t"} and not $v->{"t"} =~ /^\d{1,12}\z/
$v->{"t"} not $v->{"t"} =~ /^\d{1,12}\z/
Truth table33%1/3
A and not(B)
A= $v->{"t"}B= not $v->{"t"} =~ /^\d{1,12}\z/
ABresult
0X0
100
111
308 0 $$error = "invalid [t]imestamp";
309 } elsif ($v->{x}) {
310 1 ▶ if ($v->{x} !~m{^\d{1,12}\z}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $v->{'x'} =~ /^\d{1,12}\z/) { } 0 1 1
elsif ($v->{'t'} and $v->{'x'} < $v->{'t'}) { } 0 2 2
311 0 $$error = "invalid e[x]piration time";
312 ▶ } elsif ($v->{t} && $v->{x} < $v->{t}) {
Condition33%1/3
$v->{"t"} and $v->{"x"} < $v->{"t"}
!ll&&!rl&&r
100
MC/DC0%0/2
$v->{"t"} and $v->{"x"} < $v->{"t"}
$v->{"t"} $v->{"x"} < $v->{"t"}
Truth table33%1/3
A and B
A= $v->{"t"}B= $v->{"x"} < $v->{"t"}
ABresult
0X0
100
111
313 0 $$error = "expiration precedes timestamp";
314 }
315 }
316  
317 21 ▶ if ($v->{i}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v->{'i'}) { } 0 21 21
318 0 $v->{i} = _decodeQP($v->{i});
319 0 ▶ if (lc($v->{i}) =~m{\@([^@]+)$}) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc($v->{'i'}) =~ /\@([^\@]+)$/) { } 0 0 0
320 0 $v->{'i:domain'} = $1;
321 $$error ||= "[i]dentity does not match [d]omain"
322 0 ▶ if $v->{'i:domain'} !~m{^(.+\.)?\Q$v->{d}\E\z};
Branch: 1 branch
BranchTrueFalseTotal
unless $v->{"i:domain"} =~ /^(.+\.)?\Q$v->{"d"}\E\z/ 0 0 0
Condition0%0/2
$$error ||= "[i]dentity does not match [d]omain"
l!l
00
MC/DC0%0/1
$$error ||= "[i]dentity does not match [d]omain"
$$error
Truth table0%0/2
A
A= $$error
Aresult
11
00
323 } else {
324 0 $$error = "no domain in identity";
325 }
326 } else {
327 21 $v->{i} = '@'.$v->{d};
328 }
329 }
330  
331 my ($hdrc,$bodyc) = $v->{c}
332 65 ▶ =~m{^(relaxed|simple)(?:/(relaxed|simple))?$} or do {
Branch: 1 branch
BranchTrueFalseTotal
() = $v->{"c"} =~ m[^(relaxed|simple)(?:/(relaxed|simple))?$] or do { $$error ||= qq[invalid canonicalization $v->{"c"}] } 0 65 65
333 0 ▶ $$error ||= "invalid canonicalization $v->{c}";
Condition0%0/2
$$error ||= qq[invalid canonicalization $v->{"c"}]
l!l
00
MC/DC0%0/1
$$error ||= qq[invalid canonicalization $v->{"c"}]
$$error
Truth table0%0/2
A
A= $$error
Aresult
11
00
334 };
335 65 ▶ $bodyc ||= 'simple';
Condition100%2/2
$bodyc ||= "simple"
l!l
5312
MC/DC100%1/1
$bodyc ||= "simple"
$bodyc
Truth table100%2/2
A
A= $bodyc
Aresult
11
00
336 65 ▶ my ($kalgo,$halgo) = $v->{a} =~m{^(rsa)-(sha(?:1|256))$} or do {
Branch: 1 branch
BranchTrueFalseTotal
() = $v->{"a"} =~ /^(rsa)-(sha(?:1|256))$/ or do { $$error ||= qq[unsupported algorithm $v->{"a"}] } 0 65 65
337 0 ▶ $$error ||= "unsupported algorithm $v->{a}";
Condition0%0/2
$$error ||= qq[unsupported algorithm $v->{"a"}]
l!l
00
MC/DC0%0/1
$$error ||= qq[unsupported algorithm $v->{"a"}]
$$error
Truth table0%0/2
A
A= $$error
Aresult
11
00
338 };
339 65 ▶ return if $$error;
Branch: 1 branch
BranchTrueFalseTotal
if $$error 0 65 65
340  
341 65 $v->{'c:hdr'} = $hdrc;
342 65 $v->{'c:body'} = $bodyc;
343 65 $v->{'a:key'} = $kalgo;
344 65 $v->{'a:hash'} = $halgo;
345  
346 # ignore: z
347 65 return $v;
348 }
349  
350 # Parse DKIM key into hash and fill in necessary default values.
351 # Input can be string or hash.
352 sub parse_dkimkey {
353 18 ▶ my ($v,$error) = @_;
Subroutine parse_dkimkey: called CC=15 SCAR=35.6
Pod documented
354 18 ▶ $v = parse_taglist($v,$error) or return if !ref($v);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $v 18 0 18
Condition40%2/5
ref $v || (($v = parse_taglist($v, $error)) || (return))
l!l&&r!l&&!r
0180
$v = parse_taglist($v, $error) or return
l!l
180
MC/DC0%0/2
ref $v || (($v = parse_taglist($v, $error)) || (return))
ref $v $v = parse_taglist($v, $error)
Truth table33%1/3
A || B
A= ref $vB= $v = parse_taglist($v, $error)
ABresult
1X1
011
000
355 18 ▶ if (!$v || !%$v) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($v and %$v) 1 17 18
Condition66%2/3
$v and %$v
!ll&&!rl&&r
0117
MC/DC50%1/2
$v and %$v
$v %$v
Truth table66%2/3
A and B
A= $vB= %$v
ABresult
0X0
100
111
356 1 $$error = "invalid or empty DKIM record";
357 1 return;
358 }
359  
360 17 ▶ if (($v->{v}||='DKIM1') ne 'DKIM1') {
Branch: 2 branches
BranchTrueFalseTotal
if (($v->{'v'} ||= 'DKIM1') ne 'DKIM1') { } 0 17 17
elsif (($v->{'k'} //= 'rsa') ne 'rsa') { } 0 17 17
Condition50%1/2
$v->{"v"} ||= "DKIM1"
l!l
170
MC/DC0%0/1
$v->{"v"} ||= "DKIM1"
$v->{"v"}
Truth table50%1/2
A
A= $v->{"v"}
Aresult
11
00
361 0 $$error = "bad DKIM record version: $v->{v}";
362 ▶ } elsif (($v->{k}//='rsa') ne 'rsa') {
Condition50%1/2
$v->{"k"} //= "rsa"
l!l
017
MC/DC0%0/1
$v->{"k"} //= "rsa"
$v->{"k"}
Truth table50%1/2
A
A= $v->{"k"}
Aresult
11
00
363 0 $$error = "unsupported key type $v->{k}";
364 } else {
365 17 ▶ if (exists $v->{g}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $v->{"g"}) 0 17 17
366 # g is deprecated in RFC 6376
367 0 if (1) {
368 delete $v->{g}
369 0 } else {
370 $v->{g} = ($v->{g}//'') =~m{^(.*)\*(.*)$}
371 ? qr{^\Q$1\E.*\Q$2\E\@[^@]+\z}
372 : qr{^\Q$v->{g}\E\@[^@]+\z};
373 }
374 }
375 17 ▶ $v->{t} = { map { $_ => 1 } split(':',lc($v->{t} || '')) };
Condition50%1/2
$v->{'t'} || ''
l!l
017
MC/DC0%0/1
$v->{'t'} || ''
$v->{'t'}
Truth table50%1/2
A
A= $v->{'t'}
Aresult
11
00
376 17 ▶ $v->{h} = { map { $_ => 1 } split(':',lc($v->{h} || 'sha1:sha256')) };
Condition50%1/2
$v->{'h'} || 'sha1:sha256'
l!l
017
MC/DC0%0/1
$v->{'h'} || 'sha1:sha256'
$v->{'h'}
Truth table50%1/2
A
A= $v->{'h'}
Aresult
11
00
377 17 ▶ $v->{s} = { map { $_ => 1 } split(':',lc($v->{s} || '*')) };
Condition50%1/2
$v->{'s'} || '*'
l!l
017
MC/DC0%0/1
$v->{'s'} || '*'
$v->{'s'}
Truth table50%1/2
A
A= $v->{'s'}
Aresult
11
00
378 17 ▶ if (!$v->{s}{'*'} && !$v->{s}{email}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($v->{"s"}{"*"} or $v->{"s"}{"email"}) 0 17 17
Condition33%1/3
$v->{"s"}{"*"} or $v->{"s"}{"email"}
l!l&&r!l&&!r
1700
MC/DC0%0/2
$v->{"s"}{"*"} or $v->{"s"}{"email"}
$v->{"s"}{"*"} $v->{"s"}{"email"}
Truth table33%1/3
A or B
A= $v->{"s"}{"*"}B= $v->{"s"}{"email"}
ABresult
1X1
011
000
379 0 $$error = "service type " . join(':',keys %{$v->{s}})
380 . " does not match";
381 0 return;
382 }
383 17 return $v;
384 }
385 0 return;
386 }
387  
388 # Finalize signature, i.e add the 'b' parameter.
389 # Input is signature (hash or string), the private key (PEM or
390 # Crypto::OpenSSL::RSA object) and the mail header.
391 # Output is "DKIM-Signature: .... " string with proper line length so that it
392 # can be inserted into the mail header.
393 sub sign {
394 22 ▶ my ($sig,$key,$hdr,$error) = @_;
Subroutine sign: called CC=38 SCAR=39.8
Pod documented
395 22 ▶ if (ref($sig) && $sig->{h_auto}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $sig and $sig->{"h_auto"}) 20 2 22
Condition66%2/3
ref $sig and $sig->{"h_auto"}
!ll&&!rl&&r
0220
MC/DC50%1/2
ref $sig and $sig->{"h_auto"}
ref $sig $sig->{"h_auto"}
Truth table66%2/3
A and B
A= ref $sigB= $sig->{"h_auto"}
ABresult
0X0
100
111
396 # add a useful default based on the header which makes sure that no all
397 # relevant headers are covered and no additional important headers can
398 # be added
399 20 my (%oh,@nh);
400 20 ▶ $oh{lc($_)}++ for split(':',$sig->{h} ||'');
Condition50%1/2
$sig->{'h'} || ''
l!l
200
MC/DC0%0/1
$sig->{'h'} || ''
$sig->{'h'}
Truth table50%1/2
A
A= $sig->{'h'}
Aresult
11
00
401 20 for my $k (@sign_headers) {
402 140 for($hdr =~m{^($k):}mgi) {
403 80 push @nh,$k; # cover each instance in header
404 }
405 140 push @nh,$k; # cover non-existance so that no instance can be added
406 140 ▶ delete $oh{$k} if exists $oh{$k} and --$oh{$k} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if exists $oh{$k} and --$oh{$k} == 0 20 120 140
Condition66%2/3
exists $oh{$k} and --$oh{$k} == 0
!ll&&!rl&&r
120020
MC/DC50%1/2
exists $oh{$k} and --$oh{$k} == 0
exists $oh{$k} --$oh{$k} == 0
Truth table66%2/3
A and B
A= exists $oh{$k}B= --$oh{$k} == 0
ABresult
0X0
100
111
407 }
408 20 push @nh,($_) x $oh{$_} for keys %oh;
409 20 $sig->{h} = join(':',@nh);
410 }
411 22 ▶ $sig = parse_signature($sig,$error,1) or return;
Branch: 1 branch
BranchTrueFalseTotal
$sig = parse_signature($sig, $error, 1) or return 0 22 22
412  
413  
414 22 my %sig = %$sig;
415 22 ▶ $sig{t} = time() if !$sig{t} && exists $sig{t};
Branch: 1 branch
BranchTrueFalseTotal
if not $sig{"t"} and exists $sig{"t"} 0 22 22
Condition33%1/3
not $sig{"t"} and exists $sig{"t"}
!ll&&!rl&&r
0220
MC/DC0%0/2
not $sig{"t"} and exists $sig{"t"}
not $sig{"t"} exists $sig{"t"}
Truth table33%1/3
not(A) and B
A= not $sig{"t"}B= exists $sig{"t"}
ABresult
0X0
100
111
416 $sig{x} = ($sig{t} || time()) + $1
417 22 ▶ if $sig{x} && $sig{x} =~m{^\+(\d+)$};
Branch: 1 branch
BranchTrueFalseTotal
if $sig{"x"} and $sig{"x"} =~ /^\+(\d+)$/ 0 22 22
Condition33%2/6
$sig{"x"} and $sig{"x"} =~ /^\+(\d+)$/
!ll&&!rl&&r
2110
$sig{'t'} || time
l!l&&r!l&&!r
000
MC/DC0%0/2
$sig{"x"} and $sig{"x"} =~ /^\+(\d+)$/
$sig{"x"} $sig{"x"} =~ /^\+(\d+)$/
MC/DC0%0/2
$sig{'t'} || time
$sig{'t'} time
Truth table66%2/3
A and B
A= $sig{"x"}B= $sig{"x"} =~ /^\+(\d+)$/
ABresult
0X0
100
111
Truth table0%0/3
A || B
A= $sig{'t'}B= time
ABresult
1X1
011
000
418 22 ▶ $sig{'a:key'} eq 'rsa' or do {
Branch: 1 branch
BranchTrueFalseTotal
$sig{"a:key"} eq "rsa" or do { $$error = 'unsupported algorithm ' . $sig{"a:key"}; return } 0 22 22
419 0 $$error = "unsupported algorithm ".$sig{'a:key'};
420 0 return;
421 };
422 22 delete $sig{b};
423 22 ▶ $sig{i} = _encodeQP($sig{':i'}) if $sig{':i'};
Branch: 1 branch
BranchTrueFalseTotal
if $sig{":i"} 0 22 22
424 22 ▶ $sig{z} = _encodeQP($sig{':z'}) if $sig{':z'};
Branch: 1 branch
BranchTrueFalseTotal
if $sig{":z"} 0 22 22
425 22 ▶ $sig{bh} = _encode64($sig{'bh:computed'} || $sig{'bh:bin'});
Condition33%1/3
$sig{'bh:computed'} || $sig{'bh:bin'}
l!l&&r!l&&!r
2200
MC/DC0%0/2
$sig{'bh:computed'} || $sig{'bh:bin'}
$sig{'bh:computed'} $sig{'bh:bin'}
Truth table33%1/3
A || B
A= $sig{'bh:computed'}B= $sig{'bh:bin'}
ABresult
1X1
011
000
426 22 $sig{h} = join(':',@{$sig{'h:list'}});
427  
428 22 my @v;
429 22 for (qw(v a c d q s t x h l i z bh)) {
430 286 ▶ my $v = delete $sig{$_} // next;
Condition100%2/2
delete $sig{$_} // (next)
l!l
178108
MC/DC100%1/1
delete $sig{$_} // (next)
delete $sig{$_}
Truth table100%2/2
A
A= delete $sig{$_}
Aresult
11
00
431 178 push @v, "$_=$v"
432 }
433 22 for(sort keys %sig) {
434 176 ▶ m{:} and next;
Branch: 1 branch
BranchTrueFalseTotal
/:/ and next 154 22 176
435 22 ▶ my $v = _encodeQP(delete $sig{$_} // next);
Condition50%1/2
delete $sig{$_} // (next)
l!l
220
MC/DC0%0/1
delete $sig{$_} // (next)
delete $sig{$_}
Truth table50%1/2
A
A= delete $sig{$_}
Aresult
11
00
436 22 push @v, "$_=$v"
437 }
438  
439 22 my @lines = shift(@v);
440 22 for(@v,"b=") {
441 200 $lines[-1] .= ';';
442 200 my $append = " $_";
443 200 ▶ my $x80 = (@lines == 1 ? 64 : 80) - length($lines[-1]);
Branch: 1 branch
BranchTrueFalseTotal
@lines == 1 ? : 114 86 200
444 200 ▶ if (length($append)<=$x80) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $append <= $x80) { } 159 41 200
elsif (length $append <= 80) { } 21 20 41
445 159 $lines[-1] .= $append;
446 } elsif (length($append)<=80) {
447 21 push @lines,$append;
448 } else {
449 20 while (1) {
450 44 ▶ if ( $x80>10) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x80 > 10) 40 4 44
451 40 $lines[-1] .= substr($append,0,$x80,'');
452 40 ▶ $append eq '' and last;
Branch: 1 branch
BranchTrueFalseTotal
$append eq "" and last 20 20 40
453 }
454 24 push @lines,' ';
455 24 $x80 = 80;
456 }
457 }
458 }
459  
460 22 my $dkh = 'DKIM-Signature: '.join("\r\n",@lines);
461 22 ▶ $sig->{'a:key'} eq 'rsa' or do {
Branch: 1 branch
BranchTrueFalseTotal
$sig->{"a:key"} eq "rsa" or do { $$error = qq[unsupported signature algorithm $sig->{"a:key"}]; return } 0 22 22
462 0 $$error = "unsupported signature algorithm $sig->{'a:key'}";
463 0 return;
464 };
465 my $hash = _compute_hdrhash($hdr,
466 22 $sig{'h:list'},$sig->{'a:hash'},$sig->{'c:hdr'},$dkh);
467  
468 22 ▶ my $priv = ref($key) ? $key : Crypt::OpenSSL::RSA->new_private_key($key);
Branch: 1 branch
BranchTrueFalseTotal
ref $key ? : 0 22 22
469 22 ▶ $priv or do {
Branch: 1 branch
BranchTrueFalseTotal
$priv or do { $$error = "using private key failed"; return } 0 22 22
470 0 $$error = "using private key failed";
471 0 return;
472 };
473 22 $priv->use_no_padding;
474  
475 my $data = _encode64($priv->decrypt(
476 22 _emsa_pkcs1_v15($sig->{'a:hash'},$hash,$priv->size)));
477  
478 22 ▶ my $x80 = 80 - ($dkh =~m{\n([^\n]+)\z} && length($1));
Condition33%1/3
$dkh =~ /\n([^\n]+)\z/ && length $1
!ll&&!rl&&r
0022
MC/DC0%0/2
$dkh =~ /\n([^\n]+)\z/ && length $1
$dkh =~ /\n([^\n]+)\z/ length $1
Truth table33%1/3
A && B
A= $dkh =~ /\n([^\n]+)\z/B= length $1
ABresult
0X0
100
111
479 22 ▶ while ($data ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($data ne "") 77 22 99
480 77 ▶ $dkh .= substr($data,0,$x80,'') if $x80>10;
Branch: 1 branch
BranchTrueFalseTotal
if $x80 > 10 66 11 77
481 77 ▶ $dkh .= "\r\n " if $data ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $data ne "" 55 22 77
482 77 $x80 = 80;
483 }
484 22 $dkh .= "\r\n";
485 22 return $dkh;
486 }
487  
488 # Verify a DKIM signature (hash from parse_signature) using a DKIM key (hash
489 # from parse_dkimkey). Output is (error_code,error_string) or simply
490 # (DKIM_PASS) in case of no error or () if no final result can be computed yet.
491 sub _verify_sig {
492 96 ▶ my ($sig,$param) = @_;
Subroutine _verify_sig: called CC=22 SCAR=32.2
493  
494 # check pre-computed hash over body if body done
495 96 ▶ if (defined $sig->{'bh:computed'}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sig->{"bh:computed"} and $sig->{"bh:computed"} ne $sig->{"bh:bin"}) 1 95 96
Condition100%3/3
defined $sig->{"bh:computed"} and $sig->{"bh:computed"} ne $sig->{"bh:bin"}
!ll&&!rl&&r
38571
MC/DC100%2/2
defined $sig->{"bh:computed"} and $sig->{"bh:computed"} ne $sig->{"bh:bin"}
defined $sig->{"bh:computed"} $sig->{"bh:computed"} ne $sig->{"bh:bin"}
Truth table100%3/3
A and B
A= defined $sig->{"bh:computed"}B= $sig->{"bh:computed"} ne $sig->{"bh:bin"}
ABresult
0X0
100
111
496 and $sig->{'bh:computed'} ne $sig->{'bh:bin'}) {
497 1 return (DKIM_FAIL, 'body hash mismatch');
498 }
499 95 ▶ return if ! $param;
Branch: 1 branch
BranchTrueFalseTotal
unless $param 76 19 95
500  
501 19 ▶ return (DKIM_PERMERROR,"none or invalid dkim record") if ! %$param;
Branch: 1 branch
BranchTrueFalseTotal
unless %$param 0 19 19
502 19 ▶ return (DKIM_TEMPERROR,$param->{tempfail}) if $param->{tempfail};
Branch: 1 branch
BranchTrueFalseTotal
if $param->{"tempfail"} 1 18 19
503 18 ▶ return (DKIM_PERMERROR,$param->{permfail}) if $param->{permfail};
Branch: 1 branch
BranchTrueFalseTotal
if $param->{"permfail"} 1 17 18
504  
505 17 ▶ my $FAIL = $param->{t}{y} ? DKIM_NEUTRAL : DKIM_FAIL;
Branch: 1 branch
BranchTrueFalseTotal
$param->{'t'}{'y'} ? : 0 17 17
506 17 ▶ return ($FAIL,"key revoked") if ! $param->{p};
Branch: 1 branch
BranchTrueFalseTotal
unless $param->{"p"} 0 17 17
507  
508 return ($FAIL,"hash algorithm not allowed")
509 17 ▶ if ! $param->{h}{$sig->{'a:hash'}};
Branch: 1 branch
BranchTrueFalseTotal
unless $param->{"h"}{$sig->{"a:hash"}} 0 17 17
510  
511 return ($FAIL,"identity does not match domain") if $param->{t}{s}
512 17 ▶ && $sig->{'i:domain'} && $sig->{'i:domain'} ne $sig->{d};
Branch: 1 branch
BranchTrueFalseTotal
if $param->{"t"}{"s"} and $sig->{"i:domain"} and $sig->{"i:domain"} ne $sig->{"d"} 0 17 17
Condition33%2/6
$param->{"t"}{"s"} and $sig->{"i:domain"} and $sig->{"i:domain"} ne $sig->{"d"}
!ll&&!rl&&r
1700
$param->{"t"}{"s"} and $sig->{"i:domain"}
!ll&&!rl&&r
1700
MC/DC0%0/3
$param->{"t"}{"s"} and $sig->{"i:domain"} and $sig->{"i:domain"} ne $sig->{"d"}
$param->{"t"}{"s"} $sig->{"i:domain"} $sig->{"i:domain"} ne $sig->{"d"}
Truth table25%1/4
A and B and C
A= $param->{"t"}{"s"}B= $sig->{"i:domain"}C= $sig->{"i:domain"} ne $sig->{"d"}
ABCresult
0XX0
10X0
1100
1111
513  
514 return ($FAIL,"identity does not match granularity")
515 17 ▶ if $param->{g} && $sig->{i} !~ $param->{g};
Branch: 1 branch
BranchTrueFalseTotal
if $param->{"g"} and not $sig->{"i"} =~ /$param->{"g"}/ 0 17 17
Condition33%1/3
$param->{"g"} and not $sig->{"i"} =~ /$param->{"g"}/
!ll&&!rl&&r
1700
MC/DC0%0/2
$param->{"g"} and not $sig->{"i"} =~ /$param->{"g"}/
$param->{"g"} not $sig->{"i"} =~ /$param->{"g"}/
Truth table33%1/3
A and not(B)
A= $param->{"g"}B= not $sig->{"i"} =~ /$param->{"g"}/
ABresult
0X0
100
111
516  
517 # needs bh:computed to continue
518 17 ▶ return if ! defined $sig->{'bh:computed'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sig->{"bh:computed"} 0 17 17
519  
520 17 ▶ if (!eval {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { my $rsa = "Crypt::OpenSSL::RSA"->new_public_key(do { local $_ = $param->{"p"}; s/\s+//g; s/(.{1,64})/$1\n/g; "-----BEGIN PUBLIC KEY-----\n$_-----END PUBLIC KEY-----\n" }) or die ["permerror", "using public key failed"]; $rsa->use_no_padding; my $bencrypt = eval { do { $rsa->encrypt($sig->{"b:bin"}) } } or die ["permerror", "header sig corrupt"]; my $expect = _emsa_pkcs1_v15($sig->{"a:hash"}, $sig->{"h:hash"}, $rsa->size); if ($expect ne $bencrypt) { die [$FAIL, "header sig mismatch"]; } ; 1 } }) 3 14 17
521 17 ▶ my $rsa = Crypt::OpenSSL::RSA->new_public_key(do {
Branch: 1 branch
BranchTrueFalseTotal
my $rsa = "Crypt::OpenSSL::RSA"->new_public_key(do { local $_ = $param->{"p"}; s/\s+//g; s/(.{1,64})/$1\n/g; "-----BEGIN PUBLIC KEY-----\n$_-----END PUBLIC KEY-----\n" }) or die ["permerror", "using public key failed"] 0 16 16
522 17 local $_ = $param->{p};
523 17 s{\s+}{}g;
524 17 s{(.{1,64})}{$1\n}g;
525 17 "-----BEGIN PUBLIC KEY-----\n$_-----END PUBLIC KEY-----\n";
526 }) or die [DKIM_PERMERROR,"using public key failed"];
527 16 $rsa->use_no_padding;
528 16 ▶ my $bencrypt = eval { $rsa->encrypt($sig->{'b:bin'}) }
Branch: 1 branch
BranchTrueFalseTotal
my $bencrypt = eval { do { $rsa->encrypt($sig->{"b:bin"}) } } or die ["permerror", "header sig corrupt"] 1 15 16
529 or die [DKIM_PERMERROR,"header sig corrupt"];
530 my $expect = _emsa_pkcs1_v15(
531 15 $sig->{'a:hash'},$sig->{'h:hash'},$rsa->size);
532 15 ▶ if ($expect ne $bencrypt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expect ne $bencrypt) 1 14 15
533 # warn "expect= "._encode64($expect)."\n";
534 # warn "encrypt="._encode64($bencrypt)."\n";
535 1 die [$FAIL,"header sig mismatch"];
536 }
537 14 1;
538 }) {
539 3 ▶ return @{$@} if ref($@);
Branch: 1 branch
BranchTrueFalseTotal
if ref $@ 2 1 3
540 1 return (DKIM_PERMERROR,"using public key failed");
541 }
542 14 ▶ return (DKIM_PASS, join(' + ', @{$sig->{':warning'} || []}));
Branch: 1 branch
BranchTrueFalseTotal
$sig->{":warning"} or [] 12 2 14
543 }
544  
545 # parse the header and extract
546 sub _parse_header {
547 22 ▶ my $hdr = shift;
Subroutine _parse_header: called CC=6 SCAR=17.9
548 22 my %all_critical = map { $_ => 0 } @critical_headers;
549 22 $all_critical{lc($_)}-- for $hdr =~m{^($critical_headers_rx):}mig;
550 22 my @sig;
551 22 ▶ while ( $hdr =~m{^(DKIM-Signature:\s*(.*\n(?:[ \t].*\n)*))}mig ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hdr =~ /^(DKIM-Signature:\s*(.*\n(?:[ \t].*\n)*))/mig) 22 22 44
552 22 my $dkh = $1; # original value to exclude it when computing hash
553  
554 22 my $error;
555 22 my $sig = parse_signature($2,\$error);
556 22 ▶ if ($sig) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sig) { } 21 1 22
557 $sig->{'h:hash'} = _compute_hdrhash($hdr,
558 21 $sig->{'h:list'},$sig->{'a:hash'},$sig->{'c:hdr'},$dkh);
559  
560 21 my %critical = %all_critical;
561 21 $critical{$_}++ for @{$sig->{'h:list'}};
562 21 ▶ if (my @h = grep { $critical{$_} < 0 } keys %critical) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@h) = grep({$critical{$_} < 0;} keys %critical)) 2 19 21
563 2 push @{$sig->{':warning'}},
564 "unprotected critical header ".join(",",sort @h);
565 }
566 } else {
567 1 $sig = { error => "invalid DKIM-Signature header: $error" };
568 }
569  
570 22 push @sig,$sig;
571 }
572 22 return @sig;
573 }
574  
575 {
576 # EMSA-PKCS1-v1_5 encapsulation, see RFC 3447 9.2
577 2 my %sig_prefix = (
578 'sha1' => pack("H*","3021300906052B0E03021A05000414"),
579 'sha256' => pack("H*","3031300d060960864801650304020105000420"),
580 );
581 sub _emsa_pkcs1_v15 {
582 37 ▶ my ($algo,$hash,$len) = @_;
Subroutine _emsa_pkcs1_v15: called CC=3 SCAR=11.3
583 37 ▶ my $t = ($sig_prefix{$algo} || die "unsupport digest $algo") . $hash;
Condition50%1/2
$sig_prefix{$algo} || die("unsupport digest $algo")
l!l
370
MC/DC0%0/1
$sig_prefix{$algo} || die("unsupport digest $algo")
$sig_prefix{$algo}
Truth table50%1/2
A
A= $sig_prefix{$algo}
Aresult
11
00
584 37 my $pad = $len - length($t) -3;
585 37 ▶ $pad < 8 and die;
Branch: 1 branch
BranchTrueFalseTotal
$pad < 8 and die 0 37 37
586 37 return "\x00\x01" . ("\xff" x $pad) . "\x00" . $t;
587 }
588 }
589  
590 {
591  
592 # simple header canonicalization:
593 2 my $simple_hdrc = sub {
594 344 ▶ my $line = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
595 344 $line =~s{(?<!\r)\n}{\r\n}g; # normalize line end
596 344 return $line;
597 2 };
598  
599 # relaxed header canonicalization:
600 my $relaxed_hdrc = sub {
601 144 ▶ my ($k,$v) = shift() =~m{\A([^:]+:[ \t]*)?(.*)\z}s;
Subroutine __ANON__: called CC=2 SCAR=6.9
602 144 $v =~s{\r?\n([ \t])}{$1}g; # unfold lines
603 144 $v =~s{[ \t]+}{ }g; # WSP+ -> SP
604 144 $v =~s{\s+\z}{\r\n}; # eliminate all WS from end, normalize line end
605 144 ▶ $k = lc($k||''); # lower case key
Condition100%2/2
$k || ''
l!l
6084
MC/DC100%1/1
$k || ''
$k
Truth table100%2/2
A
A= $k
Aresult
11
00
606 144 $k=~s{[ \t]*:[ \t]*}{:}; # remove white-space around colon
607 144 return $k.$v;
608 2 };
609  
610 2 my %hdrc = (
611 simple => $simple_hdrc,
612 relaxed => $relaxed_hdrc,
613 );
614  
615 2 ▶ use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
616 my %digest = (
617 24 ▶ sha1 => sub { Digest::SHA->new(1) },
Subroutine __ANON__: called CC=1 SCAR=0.0
618 62 ▶ sha256 => sub { Digest::SHA->new(256) },
Subroutine __ANON__: called CC=1 SCAR=0.0
619 2 );
620  
621 # compute the hash over the header
622 sub _compute_hdrhash {
623 43 ▶ my ($hdr,$headers,$hash,$canon,$dkh) = @_;
Subroutine _compute_hdrhash: called CC=8 SCAR=21.8
624 #warn "XXX $hash | $canon";
625 43 $hash = $digest{$hash}();
626 43 $canon = $hdrc{$canon};
627 43 my @hdr;
628 my %kv;
629 43 for my $k (@$headers) {
630 445 ▶ if ($k eq 'dkim-signature') {
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq 'dkim-signature') { } 0 445 445
631 0 for($hdr =~m{^($k:[^\n]*\n(?:[ \t][^\n]*\n)*)}mig) {
632 0 ▶ $_ eq $dkh and next;
Branch: 1 branch
BranchTrueFalseTotal
$_ eq $dkh and next 0 0 0
633 0 push @hdr,$_;
634 }
635 } else {
636 445 ▶ my $v = $kv{$k} ||=
Condition100%2/2
$kv{$k} ||= [$hdr =~ /^($k:[^\n]*\n(?:[ \t][^\n]*\n)*)/mig]
l!l
149296
MC/DC100%1/1
$kv{$k} ||= [$hdr =~ /^($k:[^\n]*\n(?:[ \t][^\n]*\n)*)/mig]
$kv{$k}
Truth table100%2/2
A
A= $kv{$k}
Aresult
11
00
637 [ $hdr =~m{^($k:[^\n]*\n(?:[ \t][^\n]*\n)*)}mig ];
638 # take last matching kv in mail header
639 445 ▶ push @hdr, pop(@$v) // '';
Condition100%2/2
pop @$v // ""
l!l
167278
MC/DC100%1/1
pop @$v // ""
pop @$v
Truth table100%2/2
A
A= pop @$v
Aresult
11
00
640 }
641 }
642 43 $dkh =~s{([ \t;:]b=)([a-zA-Z0-9/+= \t\r\n]+)}{$1};
643 43 $dkh =~s{[\r\n]+\z}{};
644 43 push @hdr,$dkh;
645 43 $_ = $canon->($_) for (@hdr);
646 #warn Dumper(\@hdr); use Data::Dumper;
647 43 $hash->add(@hdr);
648 43 return $hash->digest;
649 }
650  
651 # simple body canonicalization:
652 # - normalize to \r\n line end
653 # - remove all empty lines at the end
654 # - make sure that body consists at least of a single empty line
655 # relaxed body canonicalization:
656 # - like simple, but additionally...
657 # - remove any white-space at the end of a line (excluding \r\n)
658 # - compact any white-space inside the line to a single space
659  
660 my $bodyc = sub {
661 43 ▶ my $relaxed = shift;
Subroutine __ANON__: called CC=1 SCAR=0.9
662 43 my $empty = my $no_line_yet = '';
663 43 my $realdata;
664 sub {
665 43 ▶ my $data = shift;
Subroutine __ANON__: called CC=10 SCAR=31.1
666 43 ▶ if ($data eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($data eq "") 0 43 43
667 0 ▶ return $no_line_yet if $realdata;
Branch: 1 branch
BranchTrueFalseTotal
if $realdata 0 0 0
668 0 return "\r\n";
669 }
670 43 my $nl = rindex($data,"\n");
671 43 ▶ if ($nl == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nl == -1) 0 43 43
672 0 $no_line_yet .= $data;
673 0 return '';
674 }
675  
676 43 ▶ if ($nl == length($data)-1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nl == length($data) - 1) { } 43 0 43
677 # newline at end of data
678 43 ▶ $data = $no_line_yet . $data if $no_line_yet ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $no_line_yet ne "" 0 43 43
679 43 $no_line_yet = '';
680 } else {
681 # newline somewhere inside
682 0 $no_line_yet .= substr($data,0,$nl+1,'');
683 0 ($data,$no_line_yet) = ($no_line_yet,$data);
684 }
685  
686 43 $data =~s{(?<!\r)\n}{\r\n}g; # normalize line ends
687 43 ▶ if ($relaxed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($relaxed) 8 35 43
688 8 $data =~s{[ \t]+}{ }g; # compact WSP+ to SP
689 8 $data =~s{ \r\n}{\r\n}g; # remove WSP+ at eol
690 }
691  
692 43 ▶ if ($data =~m{(^|\n)(?:\r\n)+\z}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /(^|\n)(?:\r\n)+\z/) { } 0 43 43
693 0 ▶ if (!$+[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $+[1]) { } 0 0 0
694 # everything empty
695 0 $empty .= $data;
696 0 return '';
697 } else {
698 # part empty
699 0 $empty .= substr($data,0,$+[1],'');
700 0 ($empty,$data) = ($data,$empty);
701 }
702 } else {
703 # nothing empty
704 43 ▶ if ($empty ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($empty ne "") 0 43 43
705 0 $data = $empty . $data;
706 0 $empty = '';
707 }
708 }
709 43 $realdata = 1;
710 43 return $data;
711 43 };
712 2 };
713  
714 my %bodyc = (
715 35 ▶ simple => sub { $bodyc->(0) },
Subroutine __ANON__: called CC=1 SCAR=0.0
716 8 ▶ relaxed => sub { $bodyc->(1) },
Subroutine __ANON__: called CC=1 SCAR=0.0
717 2 );
718  
719 # add data to the body
720 sub _append_body {
721 85 ▶ my ($self,$buf) = @_;
Subroutine _append_body: called CC=21 SCAR=31.9
722 85 for my $sig (@{$self->{sig}}) {
723 85 ▶ $sig->{'bh:computed'} and next;
Branch: 1 branch
BranchTrueFalseTotal
$sig->{"bh:computed"} and next 0 85 85
724 85 ▶ my $bh = $sig->{'bh:collecting'} ||= do {
Condition66%2/3
$sig->{"bh:collecting"} ||= do { if (not $sig->{"error"} and my $digest = $digest{$sig->{"a:hash"}}() and my $transform = $bodyc{$sig->{"c:body"}}()) { +{"digest", $digest, "transform", $transform, $sig->{"l"} ? ("l", $sig->{"l"}) : (defined $sig->{"l"} ? ("l", \$sig->{"l"}) : ())}; } else { +{"done", 1}; } }
l!l&&r!l&&!r
41440
MC/DC0%0/2
$sig->{"bh:collecting"} ||= do { if (not $sig->{"error"} and my $digest = $digest{$sig->{"a:hash"}}() and my $transform = $bodyc{$sig->{"c:body"}}()) { +{"digest", $digest, "transform", $transform, $sig->{"l"} ? ("l", $sig->{"l"}) : (defined $sig->{"l"} ? ("l", \$sig->{"l"}) : ())}; } else { +{"done", 1}; } }
$sig->{"bh:collecting"} do { if (not $sig->{"error"} and my $digest = $digest{$sig->{"a:hash"}}() and my $transform = $bodyc{$sig->{"c:body"}}()) { +{"digest", $digest, "transform", $transform, $sig->{"l"} ? ("l", $sig->{"l"}) : (defined $sig->{"l"} ? ("l", \$sig->{"l"}) : ())}; } else { +{"done", 1}; } }
Truth table66%2/3
A ||= B
A= $sig->{"bh:collecting"}B= do { if (not $sig->{"error"} and my $digest = $digest{$sig->{"a:hash"}}() and my $transform = $bodyc{$sig->{"c:body"}}()) { +{"digest", $digest, "transform", $transform, $sig->{"l"} ? ("l", $sig->{"l"}) : (defined $sig->{"l"} ? ("l", \$sig->{"l"}) : ())}; } else { +{"done", 1}; } }
ABresult
1X1
011
000
725 44 ▶ if (!$sig->{error} and
Branch: 1 branch
BranchTrueFalseTotal
if (not $sig->{'error'} and my $digest = $digest{$sig->{'a:hash'}}() and my $transform = $bodyc{$sig->{'c:body'}}()) { } 43 1 44
Condition66%4/6
not $sig->{"error"} and my $digest = $digest{$sig->{"a:hash"}}() and my $transform = $bodyc{$sig->{"c:body"}}()
!ll&&!rl&&r
1043
not $sig->{"error"} and my $digest = $digest{$sig->{"a:hash"}}()
!ll&&!rl&&r
1043
MC/DC33%1/3
not $sig->{"error"} and my $digest = $digest{$sig->{"a:hash"}}() and my $transform = $bodyc{$sig->{"c:body"}}()
not $sig->{"error"} my $digest = $digest{$sig->{"a:hash"}}() my $transform = $bodyc{$sig->{"c:body"}}()
Truth table50%2/4
not(A) and B and C
A= not $sig->{"error"}B= my $digest = $digest{$sig->{"a:hash"}}()C= my $transform = $bodyc{$sig->{"c:body"}}()
ABCresult
0XX0
10X0
1100
1111
726 my $digest = $digest{$sig->{'a:hash'}}() and
727 my $transform = $bodyc{$sig->{'c:body'}}()
728 ) {
729 {
730 digest => $digest,
731 transform => $transform,
732 $sig->{l} ? (l => $sig->{l}) :
733 43 ▶ defined($sig->{l}) ? (l => \$sig->{l}) : # capture l
Branch: 2 branches
BranchTrueFalseTotal
$sig->{'l'} ? : 1 42 43
defined $sig->{'l'} ? : 1 41 42
734 (),
735 };
736 } else {
737 1 { done => 1 };
738 }
739 };
740  
741 85 ▶ $bh->{done} and next;
Branch: 1 branch
BranchTrueFalseTotal
$bh->{"done"} and next 1 84 85
742 84 ▶ if ($buf eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf eq "") 41 43 84
743 41 $bh->{done} = 1;
744 41 goto compute_signature;
745 }
746 43 my $tbuf = $bh->{transform}($buf);
747 43 ▶ $tbuf eq '' and next;
Branch: 1 branch
BranchTrueFalseTotal
$tbuf eq "" and next 0 43 43
748 {
749 43 ▶ defined $bh->{l} or last;
Branch: 1 branch
BranchTrueFalseTotal
defined $bh->{"l"} or last 41 2 43
750 2 ▶ if (ref $bh->{l}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $bh->{"l"}) 1 1 2
751 1 ${$bh->{l}} += length($tbuf);
752 1 next;
753 }
754 1 ▶ if ($bh->{l} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bh->{'l'} > 0) { } 1 0 1
755 1 ▶ last if ($bh->{l} -= length($tbuf))>0;
Branch: 1 branch
BranchTrueFalseTotal
if ($bh->{"l"} -= length $tbuf) > 0 0 1 1
756 $bh->{_data_after_l} ||=
757 1 ▶ substr($tbuf,$bh->{l},-$bh->{l},'') =~m{\S} & 1;
Condition33%1/3
$bh->{"_data_after_l"} ||= substr($tbuf, $bh->{"l"}, -$bh->{"l"}, "") =~ /\S/ & 1
l!l&&r!l&&!r
010
MC/DC0%0/2
$bh->{"_data_after_l"} ||= substr($tbuf, $bh->{"l"}, -$bh->{"l"}, "") =~ /\S/ & 1
$bh->{"_data_after_l"} substr($tbuf, $bh->{"l"}, -$bh->{"l"}, "") =~ /\S/ & 1
Truth table33%1/3
A ||= B
A= $bh->{"_data_after_l"}B= substr($tbuf, $bh->{"l"}, -$bh->{"l"}, "") =~ /\S/ & 1
ABresult
1X1
011
000
758 1 $bh->{l} = 0;
759 } else {
760 0 ▶ $bh->{_data_after_l} ||= $tbuf =~m{\S} & 1;
Condition0%0/3
$bh->{"_data_after_l"} ||= $tbuf =~ /\S/ & 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$bh->{"_data_after_l"} ||= $tbuf =~ /\S/ & 1
$bh->{"_data_after_l"} $tbuf =~ /\S/ & 1
Truth table0%0/3
A ||= B
A= $bh->{"_data_after_l"}B= $tbuf =~ /\S/ & 1
ABresult
1X1
011
000
761 0 $tbuf = '';
762 }
763 1 $bh->{done} = 1;
764 }
765 43 ▶ $bh->{digest}->add($tbuf) if $tbuf ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $tbuf ne "" 43 0 43
766 43 ▶ $bh->{done} or next;
Branch: 1 branch
BranchTrueFalseTotal
$bh->{"done"} or next 42 1 43
767  
768 compute_signature:
769 42 delete $sig->{'bh:collecting'};
770 42 $sig->{'bh:computed'} = $bh->{digest}->digest;
771 1 push @{$sig->{':warning'}}, 'data after signed body'
772 42 ▶ if $bh->{_data_after_l};
Branch: 1 branch
BranchTrueFalseTotal
if $bh->{"_data_after_l"} 1 41 42
773 }
774 }
775 }
776  
777 {
778  
779 # parse_taglist($val,\$error)
780 # Parse a tag-list, like in the DKIM signature and in the DKIM key.
781 # Returns a hash of the parsed list. If error occur $error will be set and
782 # undef will be returned.
783  
784 2 my $fws = qr{
785 [ \t]+ (?:\r?\n[ \t]+)? |
786 \r?\n[ \t]+
787 }x;
788 2 my $tagname = qr{[a-z]\w*}i;
789 2 my $tval = qr{[\x21-\x3a\x3c-\x7e]+};
790 2 my $tagval = qr{$tval(?:$fws$tval)*};
791 2 my $end = qr{(?:\r?\n)?\z};
792 2 my $delim_or_end = qr{ $fws? (?: $end | ; (?: $fws?$end|)) }x;
793  
794 sub parse_taglist {
795 40 ▶ my ($v,$error) = @_;
Subroutine parse_taglist: called CC=5 SCAR=18.0
Pod documented
796 40 my %v;
797 40 ▶ while ( $v =~m{\G $fws? (?:
Branch: 1 branch
BranchTrueFalseTotal
if ($v =~ /\G $fws? (?: ($tagname) $fws?=$fws? ($tagval?) $delim_or_end | | (.+) )/cgsx) 295 0 295
798 ($tagname) $fws?=$fws? ($tagval?) $delim_or_end |
799 | (.+)
800 )}xgcs) {
801 295 ▶ if (defined $3) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $3) 0 295 295
802 0 $$error = "invalid data at end: '$3'";
803 0 return;
804 }
805 295 ▶ last if ! defined $1;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $1 40 255 295
806 255 ▶ exists($v{$1}) && do {
Branch: 1 branch
BranchTrueFalseTotal
exists $v{$1} and do { $$error = "duplicate key $1"; return } 0 255 255
807 0 $$error = "duplicate key $1";
808 0 return;
809 };
810 255 $v{$1} = $2;
811 }
812 #warn Dumper(\%v); use Data::Dumper;
813 40 return \%v;
814 }
815 }
816  
817 2 sub _encode64 {
818 44 ▶ my $data = shift;
Subroutine _encode64: called CC=2 SCAR=7.0
819 44 my $pad = ( 3 - length($data) % 3 ) % 3;
820 44 $data = pack('u',$data);
821 44 $data =~s{(^.|\n)}{}mg;
822 44 $data =~tr{` -_}{AA-Za-z0-9+/};
823 44 ▶ substr($data,-$pad) = '=' x $pad if $pad;
Branch: 1 branch
BranchTrueFalseTotal
if $pad 44 0 44
824 44 return $data;
825 }
826  
827 sub _decode64 {
828 42 ▶ my $data = shift;
Subroutine _decode64: called CC=1 SCAR=0.0
829 42 $data =~s{\s+}{}g;
830 42 $data =~s{=+$}{};
831 42 $data =~tr{A-Za-z0-9+/}{`!-_};
832 42 $data =~s{(.{1,60})}{ chr(32 + length($1)*3/4) . $1 . "\n" }eg;
833 42 return unpack("u",$data);
834 }
835  
836 sub _encodeQP {
837 22 ▶ (my $data = shift)
Subroutine _encodeQP: called CC=1 SCAR=0.4
838 0 =~s{([^\x21-\x3a\x3c\x3e-\x7e])}{ sprintf('=%02X',ord($1)) }esg;
839 22 return $data;
840 }
841  
842 sub _decodeQP {
843 0 ▶ my $data = shift;
Subroutine _decodeQP: not called CC=1 SCAR=6.9
844 0 $data =~s{\s+}{}g;
845 0 $data =~s{=([0-9A-F][0-9A-F])}{ chr(hex($1)) }esg;
846 0 return $data;
847 }
848  
849  
850 # ResultRecord for verification.
851 package Mail::DKIM::Iterator::VerifyRecord;
852 sub new {
853 98 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
854 98 bless [@_],$class;
855 }
856 38 ▶sub sig { shift->[0] }
Subroutine sig: called CC=1 SCAR=0.0
857 0 ▶sub domain { shift->[0]{d} }
Subroutine domain: not called CC=1 SCAR=6.9
858 38 ▶sub dnsname { shift->[1] }
Subroutine dnsname: called CC=1 SCAR=0.0
859 90 ▶sub status { shift->[2] }
Subroutine status: called CC=1 SCAR=0.0
860 8 ▶sub error { $_[0]->[2] >0 ? undef : $_[0]->[3] }
Subroutine error: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[0][2] > 0 ? : 0 8 8
861 16 ▶sub warning { $_[0]->[2] >0 ? $_[0]->[3] : undef }
Subroutine warning: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[0][2] > 0 ? : 16 0 16
862  
863 sub authentication_results {
864 0 ▶ my $self = shift;
Subroutine authentication_results: not called CC=5 SCAR=34.0
865 0 ▶ return if ! $self->[2];
Branch: 1 branch
BranchTrueFalseTotal
unless $self->[2] 0 0 0
866 0 my $ar = "dkim=$self->[2]";
867 0 ▶ $ar .= " ($self->[3])" if defined $self->[3] and $self->[3] ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->[3] and $self->[3] ne "" 0 0 0
Condition0%0/3
defined $self->[3] and $self->[3] ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->[3] and $self->[3] ne ""
defined $self->[3] $self->[3] ne ""
Truth table0%0/3
A and B
A= defined $self->[3]B= $self->[3] ne ""
ABresult
0X0
100
111
868 0 ▶ $ar .= " header.d=".( $self->[0]{d} // 'unknown');
Condition0%0/2
$self->[0]{"d"} // "unknown"
l!l
00
MC/DC0%0/1
$self->[0]{"d"} // "unknown"
$self->[0]{"d"}
Truth table0%0/2
A
A= $self->[0]{"d"}
Aresult
11
00
869 0 return $ar;
870 }
871  
872 # ResultRecord for signing.
873 package Mail::DKIM::Iterator::SignRecord;
874 sub new {
875 66 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
876 66 bless [@_],$class;
877 }
878 22 ▶sub sig { shift->[0] }
Subroutine sig: called CC=1 SCAR=0.0
879 0 ▶sub domain { shift->[0]{d} }
Subroutine domain: not called CC=1 SCAR=6.9
880 sub dnsname {
881 0 ▶ my $sig = shift->[0];
Subroutine dnsname: not called CC=3 SCAR=24.8
882 0 ▶ return ($sig->{s} || 'UNKNOWN').'_domainkey'.($sig->{d} || 'UNKNOWN');
Condition0%0/4
$sig->{'s'} || 'UNKNOWN'
l!l
00
$sig->{'d'} || 'UNKNOWN'
l!l
00
MC/DC0%0/1
$sig->{'s'} || 'UNKNOWN'
$sig->{'s'}
MC/DC0%0/1
$sig->{'d'} || 'UNKNOWN'
$sig->{'d'}
Truth table0%0/2
A
A= $sig->{'s'}
Aresult
11
00
Truth table0%0/2
A
A= $sig->{'d'}
Aresult
11
00
883 }
884 22 ▶sub signature { shift->[1] }
Subroutine signature: called CC=1 SCAR=0.0
885 66 ▶sub status { shift->[2] }
Subroutine status: called CC=1 SCAR=0.0
886 0 ▶sub error { shift->[3] }
Subroutine error: not called CC=1 SCAR=6.9
887  
888 2 1;
889  
890 __END__
↑ Summary