lib/Mail/DKIM2/Verifier.pm

filter: Statementstmt 96.9% 574 / 592 Branchbran 86.3% 285 / 330 Conditioncond 71.9% 200 / 278 MC/DCmcdc 50.6% 78 / 154 Subroutinesub 95.0% 76 / 80 Podpod 100% 25 / 25 totaltotal 84.8% 1238 / 1459 CC 572 SCAR 28.9
CC
572
Cov
88%
_provisionally_verified
45.7
_provisionally_verified
45.7
finish_body
36.1
CRAP 18.0
« lib/Mail/DKIM2/HeaderPlan.pm ↑ Summary lib/Mail/DKIM2/Hop.pm »
1 package Mail::DKIM2::Verifier;
2 192 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 192 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 192 our $VERSION = '0.35';
6  
7 192 ▶use base 'Mail::DKIM2::HeaderParser';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 192 ▶use MIME::Base64 qw(decode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 192 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 192 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 192 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 192 ▶use Mail::DKIM2::Key ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 192 ▶use Mail::DKIM2::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 192 ▶use Mail::DKIM2::Common qw(MAX_SIGNATURE_ITEMS combine_sets FUTURE_LIMIT EXPIRY);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 192 ▶use Mail::DKIM2::Address qw(domain_of to_alabel relaxed_domain_match);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 192 ▶use Mail::DKIM2::Canon qw(build_signing_input check_ignore_prefixes);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 192 ▶use Mail::DKIM2::Signature;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 192 ▶use Mail::DKIM2::MessageInstance;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 192 ▶use Mail::DKIM2::BodyPlan;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 192 ▶use Mail::DKIM2::Chain;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 192 ▶use Mail::DKIM2::HeaderPlan;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 # The key class of a signing algorithm this verifier implements (-09 §3.2,
25 # §3.3; Common::signature_algorithm), or undef: an s= item of any other
26 # algorithm is a PERMERROR member of its group (§14.6).
27 1893 ▶sub _key_class {
Subroutine _key_class: called CC=2 SCAR=9.2
28 0 ▶ my ($alg) = @_;
Subroutine _key_class: not called CC=2 SCAR=9.2
29 0 ▶ my $entry = Mail::DKIM2::Key::signature_algorithm($alg) or return;
Branch: 2 branches
BranchTrueFalseTotal
my $entry = Mail::DKIM2::Key::signature_algorithm($alg) or return 1873 20 1893
my $entry = Mail::DKIM2::Key::signature_algorithm($alg) or return 0 0 0
30 0 return $entry->{key_class};
31 }
32  
33 # How long a key lookup sent in the background at the end of the headers is
34 # waited for, from when it was sent, before it is asked again the ordinary
35 # way (query(), with the resolver's retries and other nameservers). The
36 # answer normally arrives while the body is hashed; one that has not come in
37 # a second is treated as lost, not waited out.
38 192 our $PREFETCH_WAIT = 1;
39  
40 # How often a background lookup still in flight is polled while waiting.
41 192 our $PREFETCH_POLL = 0.005;
42  
43 # The most keys asked for in the background per message, the top signature's
44 # first. The prefetch runs before any field is judged, so a message naming
45 # thousands of selectors must not open thousands of sockets; a key not
46 # prefetched is asked for with query() if it is ever needed.
47 192 our $PREFETCH_MAX = 8;
48  
49 # The longest one message's DNS lookups (keys and Domain Policy Records)
50 # are waited for in all, in seconds. A message may name hundreds of keys
51 # under nameservers that never answer, each of them worth the resolver's
52 # whole timeout; once this much time has gone on waiting, the lookups not
53 # yet made fail at once, as a DNS failure does (TEMPERROR).
54 192 our $LOOKUP_BUDGET = 20;
55  
56 200 ▶sub _now { Time::HiRes::time() }
Subroutine _now: called CC=1 SCAR=0.0
57  
58  
59 1111 ▶sub known_options {
Subroutine known_options: called CC=1 SCAR=0.0
Pod documented
60 1111 return qw(SkipTimestampCheck AllowUnsignedMI MidProcess HeadersOnly
61 PubkeyCallback PolicyCallback Resolver IgnorePrefixes NextDomainOK
62 InstancesOnly MailFrom RcptTo AcceptedAt);
63 }
64  
65 1109 ▶sub init {
Subroutine init: called CC=7 SCAR=19.5
Pod documented
66 1109 my $self = shift;
67 1109 $self->SUPER::init;
68 # The chain's fields by number: m => raw field, i => { raw, sig }.
69 1109 $self->{_chain} = Mail::DKIM2::Chain->new;
70 1109 $self->{_mi_headers} = $self->{_chain}->instance_fields;
71 1109 $self->{_dk2_headers} = $self->{_chain}->signature_entries;
72 1109 $self->{result} = undef;
73 1109 $self->{details} = undef;
74 1109 $self->{diagnostic} = undef;
75 # Options the constructor was given stay; the rest get their defaults.
76 1109 ▶ $self->{$_} //= 0 for qw(SkipTimestampCheck MidProcess AllowUnsignedMI HeadersOnly
Condition100%2/2
$self->{$_} //= 0
l!l
6004945
MC/DC100%1/1
$self->{$_} //= 0
$self->{$_}
Truth table100%2/2
A
A= $self->{$_}
Aresult
11
00
77 InstancesOnly);
78 1109 check_ignore_prefixes($self->{IgnorePrefixes});
79 croak "RcptTo must be an arrayref of addresses"
80 1108 ▶ if defined $self->{RcptTo} && ref $self->{RcptTo} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"RcptTo"} and ref $self->{"RcptTo"} ne "ARRAY" 1 1107 1108
Condition100%3/3
defined $self->{"RcptTo"} and ref $self->{"RcptTo"} ne "ARRAY"
!ll&&!rl&&r
1047601
MC/DC100%2/2
defined $self->{"RcptTo"} and ref $self->{"RcptTo"} ne "ARRAY"
defined $self->{"RcptTo"} ref $self->{"RcptTo"} ne "ARRAY"
Truth table100%3/3
A and B
A= defined $self->{"RcptTo"}B= ref $self->{"RcptTo"} ne "ARRAY"
ABresult
0X0
100
111
81 croak "AcceptedAt must be a time in seconds since the epoch"
82 1107 ▶ if defined $self->{AcceptedAt} && $self->{AcceptedAt} !~ /\A[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"AcceptedAt"} and not $self->{"AcceptedAt"} =~ /\A[0-9]+\z/ 1 1106 1107
Condition100%3/3
defined $self->{"AcceptedAt"} and not $self->{"AcceptedAt"} =~ /\A[0-9]+\z/
!ll&&!rl&&r
110331
MC/DC100%2/2
defined $self->{"AcceptedAt"} and not $self->{"AcceptedAt"} =~ /\A[0-9]+\z/
defined $self->{"AcceptedAt"} not $self->{"AcceptedAt"} =~ /\A[0-9]+\z/
Truth table100%3/3
A and not(B)
A= defined $self->{"AcceptedAt"}B= not $self->{"AcceptedAt"} =~ /\A[0-9]+\z/
ABresult
0X0
100
111
83 }
84  
85 # An envelope address as -09 section 14.4 compares it: angle brackets on,
86 # the local-part exactly as written, the domain without regard to case.
87 162 ▶sub _envelope_key {
Subroutine _envelope_key: called CC=3 SCAR=11.0
88 162 my ($addr) = @_;
89 162 ▶ (my $a = $addr // '') =~ s/\A\s+|\s+\z//g;
Condition50%1/2
$addr // ""
l!l
1620
MC/DC0%0/1
$addr // ""
$addr
Truth table50%1/2
A
A= $addr
Aresult
11
00
90 162 $a =~ s/\A<(.*)>\z/$1/s;
91 162 my $at = rindex($a, '@');
92 162 ▶ $a = substr($a, 0, $at) . lc substr($a, $at) if $at >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $at >= 0 160 2 162
93 162 return "<$a>";
94 }
95  
96 # -09 section 14.4, first paragraph: MAIL FROM exactly equal to the top
97 # signature's mf=, every RCPT TO present in its rt=. Only with the envelope
98 # (MailFrom, RcptTo) -- which only the caller has -- and only for the true top.
99 509 ▶sub _verify_envelope {
Subroutine _verify_envelope: called CC=8 SCAR=20.8
100 509 my ($self, $sig, $i) = @_;
101 509 ▶ if (defined $self->{MailFrom}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"MailFrom"}) 40 469 509
102 40 my $want = _envelope_key($self->{MailFrom});
103 40 my $mf = $sig->mail_from;
104 40 ▶ return $self->_error(fail => "DKIM2-Signature i=$i MAIL FROM $want did not match")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mf and _envelope_key($mf) eq $want 5 35 40
Condition66%2/3
defined $mf and _envelope_key($mf) eq $want
!ll&&!rl&&r
0535
MC/DC50%1/2
defined $mf and _envelope_key($mf) eq $want
defined $mf _envelope_key($mf) eq $want
Truth table66%2/3
A and B
A= defined $mfB= _envelope_key($mf) eq $want
ABresult
0X0
100
111
105 unless defined $mf && _envelope_key($mf) eq $want;
106 }
107 504 ▶ if ($self->{RcptTo}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"RcptTo"}) 36 468 504
108 36 ▶ my %rt = map { _envelope_key($_) => 1 } @{ $sig->rcpt_to || [] };
Condition50%1/2
$sig->rcpt_to || []
l!l
360
MC/DC0%0/1
$sig->rcpt_to || []
$sig->rcpt_to
Truth table50%1/2
A
A= $sig->rcpt_to
Aresult
11
00
109 36 for my $r (@{ $self->{RcptTo} }) {
110 38 my $want = _envelope_key($r);
111 return $self->_error(fail => "DKIM2-Signature i=$i RCPT TO $want did not match")
112 38 ▶ unless $rt{$want};
Branch: 1 branch
BranchTrueFalseTotal
unless $rt{$want} 8 30 38
113 }
114 }
115 496 return 1;
116 }
117  
118 299 ▶sub skip_timestamp_check {
Subroutine skip_timestamp_check: called CC=2 SCAR=6.9
Pod documented
119 299 my ($self, $val) = @_;
120 299 ▶ $self->{SkipTimestampCheck} = $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 297 2 299
121 299 return $self->{SkipTimestampCheck};
122 }
123  
124 # allow_unsigned_mi: permit a Message-Instance whose m= is higher than any
125 # DKIM2-Signature's m=, which -09 §14 otherwise makes a PERMERROR (see
126 # finish_body).
127 #
128 # Set this on an OUTBOUND path. A Signer legitimately holds the new
129 # Message-Instance before the signature covering it exists, so a verify run
130 # over that in-progress state must not reject it. Do NOT set it when
131 # verifying mail that arrived from elsewhere: there the unsigned instance is
132 # exactly the accountability gap the spec is closing.
133 #
134 # mid_process implies it, because a partial chain view always looks like this:
135 # Validate.pm strips higher-numbered DKIM2-Signature headers for its top-down
136 # walk while keeping every Message-Instance.
137 206 ▶sub allow_unsigned_mi {
Subroutine allow_unsigned_mi: called CC=2 SCAR=6.9
Pod documented
138 206 my ($self, $val) = @_;
139 206 ▶ $self->{AllowUnsignedMI} = $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 205 1 206
140 206 return $self->{AllowUnsignedMI};
141 }
142  
143 # next_domain_ok: an OUTBOUND signer's own d=. A top DKIM2-Signature carrying
144 # nd= is then acceptable when nd= names that domain (case-insensitively): the
145 # caller is the hop the imaginary hop pointed at, and its signature is the one
146 # nd= promised. Any other nd= on top is refused. Unset (the default) keeps the
147 # inbound rule: a top nd= is a permerror.
148 99 ▶sub next_domain_ok {
Subroutine next_domain_ok: called CC=2 SCAR=7.1
Pod documented
149 99 my ($self, $val) = @_;
150 99 ▶ $self->{NextDomainOK} = $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 99 0 99
151 99 return $self->{NextDomainOK};
152 }
153  
154 # mid_process: set when this Verifier is being run against a partial view of
155 # the chain (e.g. Validate.pm's per-level sub-verify, which strips
156 # higher-numbered DKIM2-Signature headers before re-verifying). In that view
157 # the highest remaining i= is NOT the true top of the whole chain, so the
158 # top-nd= rejection below (only valid for a final, whole-message verify)
159 # must be suppressed. Defaults to 0: a standalone/whole-message verify still
160 # rejects a true top nd=.
161 40 ▶sub mid_process {
Subroutine mid_process: called CC=2 SCAR=6.9
Pod documented
162 40 my ($self, $val) = @_;
163 40 ▶ $self->{MidProcess} = $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 39 1 40
164 40 return $self->{MidProcess};
165 }
166  
167 # headers_only: the message being verified has no body, as with the returned
168 # original in a DSN's text/rfc822-headers part (-09 §16.4). Signatures
169 # and the chain are checked as usual; of the Message-Instance content check,
170 # only the header hash of the top instance can be, so only that is.
171 6 ▶sub headers_only {
Subroutine headers_only: called CC=2 SCAR=6.9
Pod documented
172 6 my ($self, $val) = @_;
173 6 ▶ $self->{HeadersOnly} = $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 5 1 6
174 6 return $self->{HeadersOnly};
175 }
176  
177 # instances_only: check the Message-Instance chain and stop there, for a
178 # caller that has had the signatures verified already (Gate's VerifyResult).
179 # The result is 'pass' when the chain is intact. The signatures are not
180 # checked, so an instance none of them covers is no fault here either.
181 0 ▶sub instances_only {
Subroutine instances_only: not called CC=2 SCAR=17.9
Pod documented
182 0 my ($self, $val) = @_;
183 0 ▶ $self->{InstancesOnly} = $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 0 0 0
184 0 return $self->{InstancesOnly};
185 }
186  
187 14437 ▶sub handle_header {
Subroutine handle_header: called CC=1 SCAR=0.0
Pod documented
188 14437 my ($self, $field_name, $contents, $line) = @_;
189 14437 $self->{_chain}->add_field($field_name, $contents, $line);
190 }
191  
192 # The message's Message-Instance and DKIM2-Signature fields, numbered
193 # (Mail::DKIM2::Chain). Complete once the header block has been read.
194 0 ▶sub chain { return $_[0]->{_chain} }
Subroutine chain: not called CC=1 SCAR=6.9
Pod documented
195  
196 # Verification happens in finish_body, unless the header fields alone already
197 # decide the result.
198 1081 ▶sub finish_header {
Subroutine finish_header: called CC=8 SCAR=20.8
Pod documented
199 1081 my $self = shift;
200 1081 ▶ if (my $error = $self->_structure_error) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $self->_structure_error) 93 988 1081
201 93 $self->{result} = 'permerror';
202 93 $self->{details} = $error;
203 93 $self->stop;
204 93 return;
205 }
206  
207 # With no DKIM2 field at all the verdict is 'none' already, so the body
208 # is not kept: most mail carries no DKIM2, and a verifier run on every
209 # message would otherwise hold a copy of each. Instances with no
210 # signature (AllowUnsignedMI, MidProcess, InstancesOnly: anything else
211 # has stopped above) still have their chain walked.
212 988 ▶ unless (%{$self->{_dk2_headers}} || %{$self->{_mi_headers}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%{$self->{"_dk2_headers"};} or %{$self->{"_mi_headers"};}) 33 955 988
Condition100%3/3
%{$self->{"_dk2_headers"};} or %{$self->{"_mi_headers"};}
l!l&&r!l&&!r
9005533
MC/DC100%2/2
%{$self->{"_dk2_headers"};} or %{$self->{"_mi_headers"};}
%{$self->{"_dk2_headers"};} %{$self->{"_mi_headers"};}
Truth table100%3/3
A or B
A= %{$self->{"_dk2_headers"};}B= %{$self->{"_mi_headers"};}
ABresult
1X1
011
000
213 # -09 section 14.2 (contract E1): the fifth output state.
214 33 $self->{result} = 'none';
215 33 $self->{details} = 'NONE: No DKIM2 header fields found';
216 33 $self->finish_body;
217 33 $self->stop;
218 33 return;
219 }
220  
221 $self->_prefetch_keys
222 955 ▶ if %{$self->{_dk2_headers}} && !$self->{InstancesOnly};
Branch: 1 branch
BranchTrueFalseTotal
if %{$self->{"_dk2_headers"};} and not $self->{"InstancesOnly"} 874 81 955
Condition100%3/3
%{$self->{"_dk2_headers"};} and not $self->{"InstancesOnly"}
!ll&&!rl&&r
5526874
MC/DC100%2/2
%{$self->{"_dk2_headers"};} and not $self->{"InstancesOnly"}
%{$self->{"_dk2_headers"};} not $self->{"InstancesOnly"}
Truth table100%3/3
A and not(B)
A= %{$self->{"_dk2_headers"};}B= not $self->{"InstancesOnly"}
ABresult
0X0
100
111
223  
224 955 $self->_start_plans;
225 $self->_prefetch_policies
226 955 ▶ if %{$self->{_dk2_headers}} && !$self->{InstancesOnly};
Branch: 1 branch
BranchTrueFalseTotal
if %{$self->{"_dk2_headers"};} and not $self->{"InstancesOnly"} 874 81 955
Condition100%3/3
%{$self->{"_dk2_headers"};} and not $self->{"InstancesOnly"}
!ll&&!rl&&r
5526874
MC/DC100%2/2
%{$self->{"_dk2_headers"};} and not $self->{"InstancesOnly"}
%{$self->{"_dk2_headers"};} not $self->{"InstancesOnly"}
Truth table100%3/3
A and not(B)
A= %{$self->{"_dk2_headers"};}B= not $self->{"InstancesOnly"}
ABresult
0X0
100
111
227 }
228  
229 # -09 section 14.2, structure: what the fields' names and m=/i= numbers
230 # alone decide (Mail::DKIM2::Chain::structure_error, in the interop
231 # contract's fixed order). undef if the chain's shape is sound.
232 #
233 # InstancesOnly: the signatures are the caller's, so only the instances. An
234 # instance above every signature's m= is one nothing vouches for -- but not
235 # for an outbound signer (AllowUnsignedMI: it holds the instance it is about
236 # to sign) or a partial chain view (MidProcess).
237 #
238 # Before any of that: a header section holding a line that is not a field
239 # (HeaderParser::malformed_header). No instance hashed that line, so no
240 # result about the message can stand. A message with no DKIM2 field is
241 # still 'none': there is no DKIM2 result to give.
242 2069 ▶sub _structure_error {
Subroutine _structure_error: called CC=7 SCAR=19.5
243 2069 my ($self) = @_;
244 return 'PERMERROR: malformed header field'
245 if $self->malformed_header
246 2069 ▶ && (%{$self->{_dk2_headers}} || %{$self->{_mi_headers}});
Branch: 1 branch
BranchTrueFalseTotal
if $self->malformed_header and %{$self->{"_dk2_headers"};} || %{$self->{"_mi_headers"};} 12 2057 2069
Condition83%5/6
$self->malformed_header and %{$self->{"_dk2_headers"};} || %{$self->{"_mi_headers"};}
!ll&&!rl&&r
2055212
%{$self->{"_dk2_headers"};} or %{$self->{"_mi_headers"};}
l!l&&r!l&&!r
1202
MC/DC66%2/3
$self->malformed_header and %{$self->{"_dk2_headers"};} || %{$self->{"_mi_headers"};}
$self->malformed_header %{$self->{"_dk2_headers"};} %{$self->{"_mi_headers"};}
Truth table75%3/4
A and B or C
A= $self->malformed_headerB= %{$self->{"_dk2_headers"};}C= %{$self->{"_mi_headers"};}
ABCresult
0XX0
1000
11X1
1011
247 return $self->{_chain}->structure_error(
248 Signatures => $self->{InstancesOnly} ? 0 : 1,
249 2057 ▶ AllowUnsigned => ($self->{AllowUnsignedMI} || $self->{MidProcess}) ? 1 : 0);
Branch: 2 branches
BranchTrueFalseTotal
$self->{'InstancesOnly'} ? : 144 1913 2057
$self->{'AllowUnsignedMI'} || $self->{'MidProcess'} ? : 476 1581 2057
Condition100%3/3
$self->{"AllowUnsignedMI"} or $self->{"MidProcess"}
l!l&&r!l&&!r
397791581
MC/DC100%2/2
$self->{"AllowUnsignedMI"} or $self->{"MidProcess"}
$self->{"AllowUnsignedMI"} $self->{"MidProcess"}
Truth table100%3/3
A or B
A= $self->{"AllowUnsignedMI"}B= $self->{"MidProcess"}
ABresult
1X1
011
000
250 }
251  
252 # From the header fields, work out every instance's header hash
253 # (HeaderPlan) and plan its body (BodyPlan), then hash the body as it
254 # arrives: the body is never kept, and no earlier version of the message is
255 # rebuilt. The instances are parsed once, highest m= first, stopping at one
256 # that does not parse; the chain walk reports that one in verify()'s words.
257 # HeadersOnly: there is no body to plan, and what arrives is discarded.
258 955 ▶sub _start_plans {
Subroutine _start_plans: called CC=4 SCAR=13.9
259 955 my ($self) = @_;
260 my $hplan = Mail::DKIM2::HeaderPlan->new($self->{headers},
261 955 IgnorePrefixes => $self->{IgnorePrefixes});
262 955 my (@parsed, %parsed, %parse_error);
263 955 for my $v (reverse $self->{_chain}->instance_numbers) {
264 1400 my ($obj, $error) = $self->{_chain}->instance($v);
265 1400 ▶ unless ($obj) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj) 96 1304 1400
266 96 $parse_error{$v} = $error;
267 96 last;
268 }
269 1304 push @parsed, $obj;
270 1304 $parsed{$v} = $obj;
271 }
272 955 $self->{_planned} = { parsed => \%parsed, parse_error => \%parse_error };
273 955 $self->{_header_plan} = $hplan->plan(@parsed);
274 955 ▶ if ($self->{HeadersOnly}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"HeadersOnly"}) 6 949 955
275 6 ▶ $self->{_body_sink} = sub { };
Subroutine __ANON__: called CC=1 SCAR=0.0
276 6 return;
277 }
278 949 my $plan = $self->{_body_plan} = Mail::DKIM2::BodyPlan->new(@parsed);
279 949 ▶ $self->{_body_sink} = sub { $plan->add($_[0]) };
Subroutine __ANON__: called CC=1 SCAR=0.0
280 }
281  
282 988 ▶sub finish_body {
Subroutine finish_body: called CC=35 SCAR=35.9
Pod documented
283 988 my $self = shift;
284  
285 988 my %mi_map = %{$self->{_mi_headers}};
286 988 my %dk2_map = %{$self->{_dk2_headers}};
287  
288 # -09 section 14.2, structure (also checked at the end of the headers,
289 # which stops there on a fault).
290 988 ▶ if (my $error = $self->_structure_error) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $self->_structure_error) 0 988 988
291 0 return $self->_outcome($error);
292 }
293  
294 # The Message-Instances first, then the signatures over them: a
295 # signature is worth checking, and its key worth fetching, only over
296 # instances that describe the message.
297 #
298 # Section 14.2, content: every Message-Instance on its own, by ascending
299 # m= -- its tag list, then a duplicate hash algorithm, then its Recipe,
300 # copy-range order included (MessageInstance->parse checks in that
301 # order). Only the bound a message gives a range is left for the chain
302 # walk.
303 #
304 # -09 sections 13 and 14.2 (contract E2): a null body Recipe means the
305 # checks cannot all be completed, a PERMERROR naming the instance that
306 # holds it -- made right after that instance's own Recipe checks. Not
307 # for an instance no signature covers yet when this is a signer's own
308 # view of the chain (AllowUnsignedMI, MidProcess): that one is the
309 # signer's to sign or not.
310 794 my ($top_signed) = sort { $b <=> $a } grep { defined }
311 988 map { $_->{sig}->version } values %dk2_map;
312 988 ▶ my $own_unsigned = ($self->{AllowUnsignedMI} || $self->{MidProcess}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'AllowUnsignedMI'} || $self->{'MidProcess'} ? : 231 757 988
Condition100%3/3
$self->{"AllowUnsignedMI"} or $self->{"MidProcess"}
l!l&&r!l&&!r
19239757
MC/DC100%2/2
$self->{"AllowUnsignedMI"} or $self->{"MidProcess"}
$self->{"AllowUnsignedMI"} $self->{"MidProcess"}
Truth table100%3/3
A or B
A= $self->{"AllowUnsignedMI"}B= $self->{"MidProcess"}
ABresult
1X1
011
000
313 988 for my $v (sort { $a <=> $b } keys %mi_map) {
314 1471 my ($obj, $error) = $self->{_chain}->instance($v);
315 1471 ▶ return $self->_outcome($error) unless $obj;
Branch: 1 branch
BranchTrueFalseTotal
unless $obj 96 1375 1471
316 1375 ▶ next unless $obj->unrecoverable;
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->unrecoverable 1322 53 1375
317 53 ▶ next if $own_unsigned && (!defined $top_signed || $v > $top_signed);
Branch: 1 branch
BranchTrueFalseTotal
if $own_unsigned and !defined($top_signed) || $v > $top_signed 31 22 53
Condition100%6/6
$own_unsigned and !defined($top_signed) || $v > $top_signed
!ll&&!rl&&r
121031
not defined $top_signed or $v > $top_signed
l!l&&r!l&&!r
22910
MC/DC100%3/3
$own_unsigned and !defined($top_signed) || $v > $top_signed
$own_unsigned not defined $top_signed $v > $top_signed
Truth table100%4/4
A and not(B) or C
A= $own_unsignedB= not defined $top_signedC= $v > $top_signed
ABCresult
0XX0
1000
11X1
1011
318 22 return $self->_error(permerror => "Message-Instance m=$v has a null recipe");
319 }
320  
321 # Section 14.7: walk the chain, top down, so the highest instance that
322 # fails is the one reported. Under eval: a string died with anywhere in
323 # it is a permerror result, never an exception out of CLOSE.
324 870 my $mi_chain_ok = eval { $self->_verify_mi_chain };
325 870 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 870 870
326 0 ▶ die $err if ref $err;
Branch: 1 branch
BranchTrueFalseTotal
if ref $err 0 0 0
327 0 chomp $err;
328 0 return $self->_outcome($err);
329 }
330 870 ▶ return unless $mi_chain_ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $mi_chain_ok 174 696 870
331  
332 696 ▶ if ($self->{InstancesOnly}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"InstancesOnly"}) 51 645 696
333 51 my $top = (sort { $b <=> $a } keys %mi_map)[0];
334 51 ▶ @{$self}{qw(result details)} = $top
Branch: 1 branch
BranchTrueFalseTotal
$top ? : 51 0 51
335 ? ('pass', "Message-Instance m=1..$top intact")
336 : ('none', 'no Message-Instance headers found');
337 51 return;
338 }
339 645 ▶ unless (keys %dk2_map) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %dk2_map) 42 603 645
340 42 $self->{result} = 'none';
341 42 ▶ $self->{details} = keys %mi_map ? 'no DKIM2-Signature headers found'
Branch: 1 branch
BranchTrueFalseTotal
keys %mi_map ? : 9 33 42
342 : 'NONE: No DKIM2 header fields found';
343 42 return;
344 }
345  
346 603 my $max_i = (sort { $b <=> $a } keys %dk2_map)[0];
347 603 my $signature = $dk2_map{$max_i}{sig};
348  
349 # Contract C9: the signatures are checked in -09 section 14 order,
350 # each section over every signature from i=1 up, so the first error
351 # found is the primary one. Section 14.2: each field on its own.
352 603 for my $i (1..$max_i) {
353 950 ▶ return unless $self->_check_sig_field($i);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_check_sig_field($i) 43 907 950
354 }
355  
356 # Section 14.3: the timestamps.
357 560 for my $i (1..$max_i) {
358 895 ▶ return unless $self->_check_sig_time($i);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_check_sig_time($i) 18 877 895
359 }
360  
361 # Section 14.4: the top signature's nd=, then the Chain of Custody
362 # (each signature against the one below it, and its own mf= against
363 # its d=).
364 #
365 # -09 sections 13.2 and 14.4: a message whose highest-numbered
366 # DKIM2-Signature carries nd= needs an out-of-band arrangement to be
367 # accepted, and this verifier has none: the signer that writes nd=
368 # normally writes the signature it promises too.
369 #
370 # Only valid for a FINAL, whole-message verification: $max_i here is the
371 # top of whatever chain view this Verifier was fed. During a partial/
372 # mid-chain verify (mid_process set, e.g. by Validate.pm's per-level
373 # sub-verify after stripping higher signatures) that "top" is not the
374 # real top of the chain, so this rejection must be suppressed.
375 #
376 # An outbound signer (next_domain_ok set to its own d=) may extend a chain
377 # whose top nd= names it: that is the bridge it was named to complete.
378 542 ▶ if (!$self->{MidProcess}
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"MidProcess"} and defined $signature->next_domain and length $signature->next_domain) 13 529 542
Condition83%5/6
not $self->{"MidProcess"} and defined $signature->next_domain and length $signature->next_domain
!ll&&!rl&&r
529013
not $self->{"MidProcess"} and defined $signature->next_domain
!ll&&!rl&&r
3049913
MC/DC66%2/3
not $self->{"MidProcess"} and defined $signature->next_domain and length $signature->next_domain
not $self->{"MidProcess"} defined $signature->next_domain length $signature->next_domain
Truth table75%3/4
not(A) and B and C
A= not $self->{"MidProcess"}B= defined $signature->next_domainC= length $signature->next_domain
ABCresult
0XX0
10X0
1100
1111
379 && defined $signature->next_domain && length $signature->next_domain) {
380 13 my $ok = $self->{NextDomainOK};
381 13 ▶ if (defined $ok && length $ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ok and length $ok) { } 9 4 13
Condition66%2/3
defined $ok and length $ok
!ll&&!rl&&r
409
MC/DC50%1/2
defined $ok and length $ok
defined $ok length $ok
Truth table66%2/3
A and B
A= defined $okB= length $ok
ABresult
0X0
100
111
382 9 ▶ if (lc $signature->next_domain ne lc $ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $signature->next_domain ne lc $ok) 3 6 9
383 3 return $self->_error(permerror => "not signing: top signature nd="
384 . $signature->next_domain . " names another domain");
385 }
386 # nd= names us: carry on
387 } else {
388 4 return $self->_error(fail => "DKIM2-Signature i=$max_i unexpected nd= tag");
389 }
390 }
391 # A top nd= that got this far is an outbound signer's own bridge: the
392 # envelope belongs to the signature it is about to add, not this one.
393 return unless $self->{MidProcess}
394 535 ▶ || (defined $signature->next_domain && length $signature->next_domain)
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"MidProcess"} or defined $signature->next_domain and length $signature->next_domain or $self->_verify_envelope($signature, $max_i) 11 524 535
Condition88%8/9
$self->{"MidProcess"} or defined $signature->next_domain and length $signature->next_domain or $self->_verify_envelope($signature, $max_i)
l!l&&r!l&&!r
3648811
$self->{"MidProcess"} or defined $signature->next_domain and length $signature->next_domain
l!l&&r!l&&!r
306499
defined $signature->next_domain and length $signature->next_domain
!ll&&!rl&&r
49906
MC/DC75%3/4
$self->{"MidProcess"} or defined $signature->next_domain and length $signature->next_domain or $self->_verify_envelope($signature, $max_i)
$self->{"MidProcess"} defined $signature->next_domain length $signature->next_domain $self->_verify_envelope($signature, $max_i)
Truth table66%4/6
A or B and C or D
A= $self->{"MidProcess"}B= defined $signature->next_domainC= length $signature->next_domainD= $self->_verify_envelope($signature, $max_i)
ABCDresult
1XXX1
011X1
00X11
01011
00X00
01000
395 || $self->_verify_envelope($signature, $max_i);
396 524 ▶ return unless $self->_verify_chain();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_verify_chain 24 500 524
397  
398 # Sections 14.5 and 14.6 together, signature by signature: the s=
399 # groups combine each item's key and signature results (D3).
400 500 for my $i (1..$max_i) {
401 793 ▶ return unless $self->_check_sig_items($i);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_check_sig_items($i) 57 734 791
402 }
403  
404 # -09 section 11, 14.8: every From the message has had must be
405 # Authentic for the signature that answers for it.
406 441 ▶ return unless $self->_verify_from;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_verify_from 29 412 441
407  
408 412 $self->{result} = 'pass';
409 412 $self->{details} = "i=1..$max_i verified";
410 }
411  
412 # From must be Authentic (-09 sections 11 and 14.8): the From in effect
413 # when each signature was made (see the pairs below), each version of the
414 # header read from the header plan. An instance
415 # no signature covers (AllowUnsignedMI, MidProcess) has no signature to
416 # answer for its From: the signer gate leaves that to the Signer, which
417 # checks the From it is about to sign.
418 441 ▶sub _verify_from {
Subroutine _verify_from: called CC=4 SCAR=13.9
419 441 my ($self) = @_;
420 441 my %dk2_map = %{$self->{_dk2_headers}};
421 441 for my $pair ($self->_from_pairs) {
422 502 my ($i, $from) = @$pair;
423 502 ▶ my $d = $dk2_map{$i}{sig}->domain // '';
Condition50%1/2
$dk2_map{$i}{"sig"}->domain // ""
l!l
5020
MC/DC0%0/1
$dk2_map{$i}{"sig"}->domain // ""
$dk2_map{$i}{"sig"}->domain
Truth table50%1/2
A
A= $dk2_map{$i}{"sig"}->domain
Aresult
11
00
424 502 ▶ return 0 unless $self->_from_pair_authentic($i, $d, @$from);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_from_pair_authentic($i, $d, @$from) 29 473 502
425 }
426 412 return 1;
427 }
428  
429 # The (i, From values) pairs section 14.8 checks, as [$i, \@from] in i
430 # order: worked out from the header fields alone (the From at each state
431 # comes from the Recipes), so the policy prefetch can use them too.
432 1040 ▶sub _from_pairs {
Subroutine _from_pairs: called CC=9 SCAR=22.2
433 1040 my ($self) = @_;
434 1040 my %dk2_map = %{$self->{_dk2_headers}};
435 1040 ▶ my @sigs = map { [$_, 0 + ($dk2_map{$_}{sig}->version // 0)] } sort { $a <=> $b } keys %dk2_map;
Condition50%1/2
$dk2_map{$_}{"sig"}->version // 0
l!l
17810
MC/DC0%0/1
$dk2_map{$_}{"sig"}->version // 0
$dk2_map{$_}{"sig"}->version
Truth table50%1/2
A
A= $dk2_map{$_}{"sig"}->version
Aresult
11
00
436 1040 ▶ return unless @sigs;
Branch: 1 branch
BranchTrueFalseTotal
unless @sigs 0 1040 1040
437  
438 1040 ▶ my $hplan = $self->{_header_plan} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $hplan = $self->{"_header_plan"} or return 1040 0 1040
439  
440 # -09 §11, the -07 contract C5 as corrected by Ruling 19: the From in effect when
441 # each signature was made. i=1 against the From at state m(i=1); each
442 # i >= 2 against the From at state m(i) when an instance k with
443 # m(i-1) < k <= m(i) replaced From. A From replaced before any signature
444 # covered it (an unsigned ingress m=1 a list rewrote in m=2 before
445 # signing i=1) is never checked.
446 1040 my @pairs;
447 my $prev_m;
448 1040 for my $sig (@sigs) {
449 1781 my ($i, $m) = @$sig;
450 my $due = $i == 1
451 1781 ▶ || (defined $prev_m && $self->{_chain}->instances_changing('From', $prev_m, $m));
Condition83%5/6
$i == 1 || defined $prev_m && $self->{'_chain'}->instances_changing('From', $prev_m, $m)
l!l&&r!l&&!r
1040144597
defined $prev_m and $self->{"_chain"}->instances_changing("From", $prev_m, $m)
!ll&&!rl&&r
0597144
MC/DC66%2/3
$i == 1 || defined $prev_m && $self->{'_chain'}->instances_changing('From', $prev_m, $m)
$i == 1 defined $prev_m $self->{"_chain"}->instances_changing("From", $prev_m, $m)
Truth table75%3/4
A || B and C
A= $i == 1B= defined $prev_mC= $self->{"_chain"}->instances_changing("From", $prev_m, $m)
ABCresult
1XX1
0111
00X0
0100
452 1781 $prev_m = $m;
453 1781 ▶ push @pairs, [$i, $hplan->values_at($m, 'From') // []] if $due;
Branch: 1 branch
BranchTrueFalseTotal
if $due 1184 597 1781
Condition100%2/2
$hplan->values_at($m, "From") // []
l!l
110381
MC/DC100%1/1
$hplan->values_at($m, "From") // []
$hplan->values_at($m, "From")
Truth table100%2/2
A
A= $hplan->values_at($m, "From")
Aresult
11
00
454 }
455 1040 return @pairs;
456 }
457  
458 # -09 section 14.8 for one signature and the From it is checked against
459 # (contract E3.1): 1, or 0 with the outcome recorded. The From domain
460 # relaxed-matches d= (d= is it or above it), or else d= is below the From
461 # domain and that domain's Domain Policy Record does not forbid it.
462 502 ▶sub _from_pair_authentic {
Subroutine _from_pair_authentic: called CC=2 SCAR=7.1
463 502 my ($self, $i, $d, @from) = @_;
464 15 ▶ my $e = Mail::DKIM2::Address::from_pair_error($d, sub {
Subroutine __ANON__: called CC=2 SCAR=8.1
465 15 my ($dom) = @_;
466 15 ▶ return $self->{_policy}{$dom} //= $self->_domain_policy($dom);
Condition33%1/3
$self->{"_policy"}{$dom} //= $self->_domain_policy($dom)
l!l&&r!l&&!r
0150
MC/DC0%0/2
$self->{"_policy"}{$dom} //= $self->_domain_policy($dom)
$self->{"_policy"}{$dom} $self->_domain_policy($dom)
Truth table33%1/3
A //= B
A= $self->{"_policy"}{$dom}B= $self->_domain_policy($dom)
ABresult
1X1
011
000
467 502 ▶ }, @from) or return 1;
Branch: 1 branch
BranchTrueFalseTotal
my $e = Mail::DKIM2::Address::from_pair_error($d, sub { my($dom) = @_; return $self->{"_policy"}{$dom} //= $self->_domain_policy($dom); } , @from) or return 1 29 473 502
468 29 return $self->_error($e->[0] => "DKIM2-Signature i=$i $e->[1]");
469 }
470  
471 # The Domain Policy Record for $dom (an A-label, lowercased): 'open',
472 # 'strict', 'multiple' (Common::domain_policy) or 'temperror'. From the
473 # PolicyCallback when there is one, else a TXT query of _domainkey.$dom
474 # through the Resolver, as the key fetch makes it: only when d= is below the
475 # From domain, reading the answer _prefetch_policies asked for if any.
476 15 ▶sub _domain_policy {
Subroutine _domain_policy: called CC=3 SCAR=11.2
477 15 my ($self, $dom) = @_;
478 # Without a PolicyCallback, fetch_domain_policy: it reads the answer
479 # _prefetch_policies asked for, if any, before it queries.
480 my ($p, $why) = Mail::DKIM2::Key::policy_from_source($dom,
481 5 ▶ $self->{PolicyCallback} // sub { $self->fetch_domain_policy($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
482 0 ▶ sub { $self->_resolver }, $self);
Subroutine __ANON__: not called CC=1 SCAR=0.6
Condition66%2/3
$self->{"PolicyCallback"} // sub { $self->fetch_domain_policy($_[0]); }
l!l&&r!l&&!r
1050
MC/DC0%0/2
$self->{"PolicyCallback"} // sub { $self->fetch_domain_policy($_[0]); }
$self->{"PolicyCallback"} sub { $self->fetch_domain_policy($_[0]); }
Truth table66%2/3
A // B
A= $self->{"PolicyCallback"}B= sub { $self->fetch_domain_policy($_[0]); }
ABresult
1X1
011
000
483 15 ▶ $self->{diagnostic} = $why if defined $why;
Branch: 1 branch
BranchTrueFalseTotal
if defined $why 1 14 15
484 15 return $p;
485 }
486  
487 # fetch_domain_policy($domain): the default Domain Policy Record source, the
488 # TXT records at _domainkey.$domain (each RR's strings joined) as an
489 # arrayref; [] for NXDOMAIN or no TXT. Dies with "TEMPERROR: ..." when the
490 # lookup fails otherwise. A PolicyCallback may call it for domains it does
491 # not handle itself.
492 6 ▶sub fetch_domain_policy {
Subroutine fetch_domain_policy: called CC=4 SCAR=13.9
Pod documented
493 6 my ($self, $dom) = @_;
494 6 my $fqdn = "_domainkey.$dom";
495 # The prefetched answer when there is one (_prefetch_policies), else
496 # query(): the key fetch's path.
497 6 my ($reply, $err) = @{ $self->_key_reply($fqdn) };
498 6 ▶ unless ($reply) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($reply) 3 3 6
499 3 ▶ $err //= '';
Condition50%1/2
$err //= ""
l!l
30
MC/DC0%0/1
$err //= ""
$err
Truth table50%1/2
A
A= $err
Aresult
11
00
500 3 ▶ return [] if $err =~ /^(?:NXDOMAIN|NOERROR|NODATA)\s*$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $err =~ /^(?:NXDOMAIN|NOERROR|NODATA)\s*$/i 2 1 3
501 1 croak "TEMPERROR: DNS lookup for $fqdn failed: $err";
502 }
503 3 return [ map { join '', $_->txtdata } grep { $_->type eq 'TXT' } $reply->answer ];
504 }
505  
506 # Every instance against the version of the message it describes, top down
507 # to m=1: each hash, and each Recipe's verdict, is the plans' -- nothing is
508 # rebuilt. Past an instance with a null body Recipe (the body before it is
509 # gone) only the header history is checked. HeadersOnly: only the top
510 # instance's header hash can be checked, so only that is.
511 819 ▶sub _verify_mi_chain {
Subroutine _verify_mi_chain: called CC=12 SCAR=24.9
512 819 my ($self) = @_;
513 819 my ($plan, $hplan) = @{$self}{qw(_body_plan _header_plan)};
514 819 ▶ $plan->finish if $plan;
Branch: 1 branch
BranchTrueFalseTotal
if $plan 780 39 819
515 819 my $p = $self->{_planned};
516 819 my @nums = reverse $self->{_chain}->instance_numbers;
517 819 ▶ return 1 unless @nums;
Branch: 1 branch
BranchTrueFalseTotal
unless @nums 33 786 819
518  
519 786 my $headers_only = $self->{HeadersOnly};
520 786 for my $num (@nums) {
521 1082 my $mi = $p->{parsed}{$num};
522 my ($ok, $err) = $mi
523 ? $mi->_check_hashes($num,
524 1089 ▶ sub { $hplan->header_hash($num, $_[0]) // '' },
Subroutine __ANON__: called CC=2 SCAR=7.6
Condition50%1/2
$hplan->header_hash($num, $_[0]) // ""
l!l
10890
MC/DC0%0/1
$hplan->header_hash($num, $_[0]) // ""
$hplan->header_hash($num, $_[0])
Truth table50%1/2
A
A= $hplan->header_hash($num, $_[0])
Aresult
11
00
525 985 ▶ $headers_only ? undef : sub { $plan->body_hash($num, $_[0]) // '' })
Subroutine __ANON__: called CC=2 SCAR=7.6
Condition50%1/2
$plan->body_hash($num, $_[0]) // ""
l!l
9850
MC/DC0%0/1
$plan->body_hash($num, $_[0]) // ""
$plan->body_hash($num, $_[0])
Truth table50%1/2
A
A= $plan->body_hash($num, $_[0])
Aresult
11
00
526 1082 ▶ : (0, $p->{parse_error}{$num});
Branch: 2 branches
BranchTrueFalseTotal
$mi ? : 1082 0 1082
$headers_only ? : 35 1047 1082
527 1082 ▶ return $self->_mi_mismatch($num, $err) unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 136 946 1082
528  
529 946 ▶ last if $num <= 1 || $self->{HeadersOnly};
Branch: 1 branch
BranchTrueFalseTotal
if $num <= 1 or $self->{"HeadersOnly"} 648 298 946
Condition100%3/3
$num <= 1 or $self->{"HeadersOnly"}
l!l&&r!l&&!r
6471298
MC/DC100%2/2
$num <= 1 or $self->{"HeadersOnly"}
$num <= 1 $self->{"HeadersOnly"}
Truth table100%3/3
A or B
A= $num <= 1B= $self->{"HeadersOnly"}
ABresult
1X1
011
000
530  
531 298 ▶ $headers_only = 1 if $mi->unrecoverable;
Branch: 1 branch
BranchTrueFalseTotal
if $mi->unrecoverable 29 269 298
532 298 ▶ if (my $e = $plan->recipe_error($num) // $hplan->recipe_error($num)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $plan->recipe_error($num) // $hplan->recipe_error($num)) 2 296 298
Condition100%3/3
$plan->recipe_error($num) // $hplan->recipe_error($num)
l!l&&r!l&&!r
11296
MC/DC100%2/2
$plan->recipe_error($num) // $hplan->recipe_error($num)
$plan->recipe_error($num) $hplan->recipe_error($num)
Truth table100%3/3
A // B
A= $plan->recipe_error($num)B= $hplan->recipe_error($num)
ABresult
1X1
011
000
533 2 return $self->_mi_undo_failed($num, $e);
534 }
535 }
536 648 return 1;
537 }
538  
539 # Instance $num does not match the content it describes.
540 172 ▶sub _mi_mismatch {
Subroutine _mi_mismatch: called CC=2 SCAR=7.2
541 172 my ($self, $num, $err) = @_;
542 # MessageInstance's reasons carry their state and -09 section 14.2 /
543 # 14.7 words: a malformed instance is a PERMERROR, a hash that does
544 # not match ("FAIL: Message-Instance m=N header hash sha256 mismatch")
545 # a FAIL.
546 172 ▶ return $self->_outcome($err // "FAIL: Message-Instance m=$num header hash mismatch");
Condition50%1/2
$err // "FAIL: Message-Instance m=$num header hash mismatch"
l!l
1720
MC/DC0%0/1
$err // "FAIL: Message-Instance m=$num header hash mismatch"
$err
Truth table50%1/2
A
A= $err
Aresult
11
00
547 }
548  
549 # Instance $num's Recipe does not undo: a copy range past the end of what
550 # it copies (parse has refused a bad step already, D6) is "contains invalid
551 # JSON". Which range is kept for diagnostic().
552 2 ▶sub _mi_undo_failed {
Subroutine _mi_undo_failed: called CC=3 SCAR=11.2
553 2 my ($self, $num, $err) = @_;
554 2 ▶ if (defined $err) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $err) 2 0 2
555 2 (my $why = "$err") =~ s/\s+\z//;
556 # Already a verdict (the instances do not form a chain, say).
557 2 ▶ return $self->_outcome($why) if $why =~ /\A(?:PERMERROR|FAIL|TEMPERROR): /;
Branch: 1 branch
BranchTrueFalseTotal
if $why =~ /\A(?:PERMERROR|FAIL|TEMPERROR): / 0 2 2
558 2 $self->{diagnostic} = $why;
559 }
560 2 return $self->_error(permerror => "Message-Instance m=$num contains invalid JSON");
561 }
562  
563 # Signature $i's checks, one -09 section 14 step at a time (contract C9:
564 # finish_body runs each step over every signature before the next). Each
565 # returns 1, or 0 with the outcome recorded.
566  
567 # Section 14.2: the field on its own -- its tags, mf=/rt= form, the
568 # section 10.9 duplicate and limit rules, and every implemented item's
569 # value. Keeps the items to check and the signing input for the later steps.
570 960 ▶sub _check_sig_field {
Subroutine _check_sig_field: called CC=34 SCAR=35.7
571 960 my ($self, $i) = @_;
572  
573 960 my %mi_map = %{$self->{_mi_headers}};
574 960 my %dk2_map = %{$self->{_dk2_headers}};
575 960 my $dk2_entry = $dk2_map{$i};
576 960 my $signature = $dk2_entry->{sig};
577  
578 # -09 section 8: a tag given twice makes the whole list
579 # invalid; and a field that does not parse is a syntax error -- never
580 # verified with its malformed parts dropped (follow-up review F3, F4).
581 960 ▶ return $self->_error(permerror => "DKIM2-Signature i=$i syntax error")
Branch: 1 branch
BranchTrueFalseTotal
if $signature->duplicate_tag or $signature->syntax_error 12 948 960
Condition100%3/3
$signature->duplicate_tag or $signature->syntax_error
l!l&&r!l&&!r
111948
MC/DC100%2/2
$signature->duplicate_tag or $signature->syntax_error
$signature->duplicate_tag $signature->syntax_error
Truth table100%3/3
A or B
A= $signature->duplicate_tagB= $signature->syntax_error
ABresult
1X1
011
000
582 if $signature->duplicate_tag || $signature->syntax_error;
583  
584 # Only include headers that existed when signature $i was created:
585 # - DKIM2-Sig headers with i <= $i
586 # - MI headers with m <= the version referenced by signature $i
587 # (A signature with no m= covers none, and is "tag=m missing" below.)
588 948 ▶ my $max_v = $signature->version || 0;
Condition50%1/2
$signature->version || 0
l!l
9480
MC/DC0%0/1
$signature->version || 0
$signature->version
Truth table50%1/2
A
A= $signature->version
Aresult
11
00
589  
590 my $signing_input = build_signing_input(
591 1909 instances => [ map { $mi_map{$_} } sort { $a <=> $b } grep { $_ <= $max_v } keys %mi_map ],
592 1093 signatures => [ map { $dk2_map{$_}{raw} } sort { $a <=> $b } grep { $_ < $i } keys %dk2_map ],
593 signing => $dk2_entry->{raw},
594 948 );
595  
596 # -09 section 10: every DKIM2-Signature MUST carry i=, m=, t=, d=, s=. Checked
597 # via get_tag() (not the sequence/version/timestamp/domain accessors)
598 # because those accessors just proxy get_tag() and would themselves
599 # return undef for an absent tag anyway -- get_tag() is used directly
600 # here to make explicit that "missing" means "tag truly absent", not
601 # coerced to 0/''. (Verified against TagValueList::get_tag/Signature.pm:
602 # none of these accessors coerce a missing tag to a false-but-defined
603 # value.)
604 948 for my $t (qw(i m t d s)) {
605 4735 ▶ my $present =
Branch: 3 branches
BranchTrueFalseTotal
$t eq 's' ? : 945 3790 4735
$signature->sig_count ? : 945 0 945
$t eq 'd' ? : 946 2844 3790
Condition66%2/3
defined $signature->get_tag("d") and length $signature->get_tag("d")
!ll&&!rl&&r
10945
MC/DC50%1/2
defined $signature->get_tag("d") and length $signature->get_tag("d")
defined $signature->get_tag("d") length $signature->get_tag("d")
Truth table66%2/3
A and B
A= defined $signature->get_tag("d")B= length $signature->get_tag("d")
ABresult
0X0
100
111
606 $t eq 's' ? ($signature->sig_count ? 1 : 0)
607 : $t eq 'd' ? (defined($signature->get_tag('d')) && length $signature->get_tag('d'))
608 : defined $signature->get_tag($t);
609 4735 ▶ return $self->_error(permerror => "DKIM2-Signature i=$i tag=$t missing")
Branch: 1 branch
BranchTrueFalseTotal
unless $present 3 4732 4735
610 unless $present;
611 }
612  
613  
614 # -09 section 10: a signature carries either nd= or both mf= and rt=,
615 # never both forms. nd= together with mf= or rt= makes the mf= (or rt=)
616 # the unexpected tag.
617 945 my $nd_tag = $signature->get_tag('nd');
618 945 my $mf_tag = $signature->get_tag('mf');
619 945 my $rt_tag = $signature->get_tag('rt');
620 945 ▶ if (defined $nd_tag) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $nd_tag) { } 55 890 945
621 55 ▶ return $self->_error(permerror => "DKIM2-Signature i=$i tag=mf was unexpected")
Branch: 1 branch
BranchTrueFalseTotal
if defined $mf_tag 1 54 55
622 if defined $mf_tag;
623 54 ▶ return $self->_error(permerror => "DKIM2-Signature i=$i tag=rt was unexpected")
Branch: 1 branch
BranchTrueFalseTotal
if defined $rt_tag 1 53 54
624 if defined $rt_tag;
625 } else {
626 890 ▶ return $self->_error(permerror => "DKIM2-Signature i=$i tag=mf missing")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mf_tag 1 889 890
627 unless defined $mf_tag;
628 889 ▶ return $self->_error(permerror => "DKIM2-Signature i=$i tag=rt missing")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rt_tag 1 888 889
629 unless defined $rt_tag;
630 }
631  
632 # -09 section 14.2 (contract E6): i=1 MUST NOT carry the exploded flag.
633 # A well-formedness check, before the item checks and any key fetch.
634 return $self->_error(permerror => "DKIM2-Signature i=1 has an exploded flag")
635 941 ▶ if $i == 1 && grep { $_ eq 'exploded' } @{ $signature->flags || [] };
Branch: 1 branch
BranchTrueFalseTotal
if $i == 1 and grep {$_ eq "exploded";} @{$signature->flags or [];} 3 938 941
Condition100%5/5
$i == 1 and grep {$_ eq "exploded";} @{$signature->flags or [];}
!ll&&!rl&&r
3485903
$signature->flags || []
l!l
5588
MC/DC100%2/2
$i == 1 and grep {$_ eq "exploded";} @{$signature->flags or [];}
$i == 1 grep {$_ eq "exploded";} @{$signature->flags or [];}
MC/DC100%1/1
$signature->flags || []
$signature->flags
Truth table100%3/3
A and B
A= $i == 1B= grep {$_ eq "exploded";} @{$signature->flags or [];}
ABresult
0X0
100
111
Truth table100%2/2
A
A= $signature->flags
Aresult
11
00
636  
637 # Local check: n= nonce must not exceed 64 characters (no -08 string).
638 938 my $nonce = $signature->get_tag('n');
639 938 ▶ return $self->_error(permerror => "n= nonce exceeds 64 characters at i=$i")
Branch: 1 branch
BranchTrueFalseTotal
if defined $nonce and length $nonce > 64 0 938 938
Condition33%1/3
defined $nonce and length $nonce > 64
!ll&&!rl&&r
93800
MC/DC0%0/2
defined $nonce and length $nonce > 64
defined $nonce length $nonce > 64
Truth table33%1/3
A and B
A= defined $nonceB= length $nonce > 64
ABresult
0X0
100
111
640 if defined $nonce && length($nonce) > 64;
641  
642 # t= is 1*DIGIT. A malformed value is a syntax error even when the
643 # age check is skipped; it used to numify to 0 and skip that check.
644 938 my $ts = $signature->timestamp;
645 938 ▶ return $self->_error(permerror => "DKIM2-Signature i=$i syntax error")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ts and $ts =~ /\A[0-9]+\z/ 6 932 938
Condition66%2/3
defined $ts and $ts =~ /\A[0-9]+\z/
!ll&&!rl&&r
06932
MC/DC50%1/2
defined $ts and $ts =~ /\A[0-9]+\z/
defined $ts $ts =~ /\A[0-9]+\z/
Truth table66%2/3
A and B
A= defined $tsB= $ts =~ /\A[0-9]+\z/
ABresult
0X0
100
111
646 unless defined $ts && $ts =~ /\A[0-9]+\z/;
647  
648 # -09 sections 10.5, 10.6: mf= and each rt= MUST be a bracketed RFC 5321
649 # path; one that is not is malformed.
650 932 my $mf_raw = $signature->mail_from;
651 932 ▶ return $self->_error(permerror => "DKIM2-Signature i=$i syntax error")
Branch: 1 branch
BranchTrueFalseTotal
if defined $mf_raw and length $mf_raw and not $mf_raw =~ /^<.*>$/s 3 929 932
Condition83%5/6
defined $mf_raw and length $mf_raw and not $mf_raw =~ /^<.*>$/s
!ll&&!rl&&r
538763
defined $mf_raw and length $mf_raw
!ll&&!rl&&r
530879
MC/DC66%2/3
defined $mf_raw and length $mf_raw and not $mf_raw =~ /^<.*>$/s
defined $mf_raw length $mf_raw not $mf_raw =~ /^<.*>$/s
Truth table75%3/4
A and B and not(C)
A= defined $mf_rawB= length $mf_rawC= not $mf_raw =~ /^<.*>$/s
ABCresult
0XX0
10X0
1100
1111
652 if defined $mf_raw && length $mf_raw && $mf_raw !~ /^<.*>$/s;
653 929 ▶ for my $r (@{ $signature->rcpt_to || [] }) {
Condition100%2/2
$signature->rcpt_to || []
l!l
87653
MC/DC100%1/1
$signature->rcpt_to || []
$signature->rcpt_to
Truth table100%2/2
A
A= $signature->rcpt_to
Aresult
11
00
654 897 ▶ return $self->_error(permerror => "DKIM2-Signature i=$i syntax error")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r and $r =~ /^<.*>$/s 0 897 897
Condition33%1/3
defined $r and $r =~ /^<.*>$/s
!ll&&!rl&&r
00897
MC/DC0%0/2
defined $r and $r =~ /^<.*>$/s
defined $r $r =~ /^<.*>$/s
Truth table33%1/3
A and B
A= defined $rB= $r =~ /^<.*>$/s
ABresult
0X0
100
111
655 unless defined $r && $r =~ /^<.*>$/s;
656 }
657  
658 # Every item's value must be a padded base64string (section 2.14),
659 # whether or not this verifier implements its algorithm: the grammar is
660 # the same for all. An unknown algorithm is a PERMERROR member of its
661 # group (-09 section 14.6, _check_sig_items), never verified. Names
662 # match exactly: they are case-sensitive (section 2.14), and a name
663 # that is merely like a known one must never be verified as it.
664 929 for my $idx (0 .. $signature->sig_count - 1) {
665 5215 ▶ return $self->_error(permerror => "DKIM2-Signature i=$i syntax error")
Branch: 1 branch
BranchTrueFalseTotal
unless Mail::DKIM2::Util::is_base64string($signature->signature_value($idx) // "") 6 5209 5215
Condition50%1/2
$signature->signature_value($idx) // ""
l!l
52150
MC/DC0%0/1
$signature->signature_value($idx) // ""
$signature->signature_value($idx)
Truth table50%1/2
A
A= $signature->signature_value($idx)
Aresult
11
00
666 unless Mail::DKIM2::Util::is_base64string($signature->signature_value($idx) // '');
667 }
668 # -09 section 10.9: a duplicate Selector within this signature's s=
669 # tag, in any group -- after the values' syntax, before the item limit
670 # (contract D2/D3), and before any DNS lookup or crypto work.
671 923 ▶ if (my @dup_errors = $signature->check_duplicates) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@dup_errors) = $signature->check_duplicates) 3 920 923
672 3 return $self->_outcome($dup_errors[0]);
673 }
674 # Local policy (MAX_SIGNATURE_ITEMS): before any key is fetched.
675 920 ▶ return $self->_error(permerror => "DKIM2-Signature i=$i has more than "
Branch: 1 branch
BranchTrueFalseTotal
if $signature->sig_count > 8 3 917 920
676 . MAX_SIGNATURE_ITEMS . " signatures")
677 if $signature->sig_count > MAX_SIGNATURE_ITEMS;
678 917 $dk2_entry->{signing_input} = $signing_input;
679 917 return 1;
680 }
681  
682 # Section 14.3: a signature made more than 14 days before delivery, or more
683 # than a day after it, is not Fully Verified: a FAIL under -08 (D5).
684 # "Delivery" is when the message was accepted over SMTP: AcceptedAt for a
685 # caller verifying later than that, else now.
686 895 ▶sub _check_sig_time {
Subroutine _check_sig_time: called CC=5 SCAR=16.1
687 895 my ($self, $i) = @_;
688 895 ▶ return 1 if $self->{SkipTimestampCheck};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"SkipTimestampCheck"} 606 289 895
689 289 my $ts = $self->{_dk2_headers}{$i}{sig}->timestamp;
690 289 ▶ my $now = $self->{AcceptedAt} // time();
Condition100%2/2
$self->{"AcceptedAt"} // time
l!l
3286
MC/DC100%1/1
$self->{"AcceptedAt"} // time
$self->{"AcceptedAt"}
Truth table100%2/2
A
A= $self->{"AcceptedAt"}
Aresult
11
00
691 289 ▶ return $self->_error(fail => "DKIM2-Signature i=$i signature is from the future")
Branch: 1 branch
BranchTrueFalseTotal
if $ts > $now + 86400 3 286 289
692 if $ts > $now + FUTURE_LIMIT;
693 286 ▶ return $self->_error(fail => "DKIM2-Signature i=$i signature expired")
Branch: 1 branch
BranchTrueFalseTotal
if $now > $ts + 1209600 15 271 286
694 if $now > $ts + EXPIRY;
695 271 return 1;
696 }
697  
698 # Sections 14.5 and 14.6 for signature $i: each s= item's key, then its
699 # signature, as one result per item -- -09 section 14.6 combines them by the
700 # s= groups, and an item's key error counts only if no group passes, so the
701 # two cannot be separate passes over every signature (the -08 contract, D3).
702 # An item is a PERMERROR if this verifier does not implement its algorithm
703 # (no key is fetched for it); a TEMPERROR if its key cannot be fetched; a
704 # PERMERROR if its key record is absent or unusable (section 14.5's
705 # strings); else PASS or FAIL by its signature.
706 803 ▶sub _check_sig_items {
Subroutine _check_sig_items: called CC=2 SCAR=6.9
707 803 my ($self, $i) = @_;
708 803 my $dk2_entry = $self->{_dk2_headers}{$i};
709 803 my $signature = $dk2_entry->{sig};
710 827 my @results = map { [ map { [ $self->_check_sig_item($i, $_) ] } @$_ ] }
711 803 @{ $signature->sig_groups };
712 801 my ($state, $why) = combine_sets(\@results,
713 "FAIL: DKIM2-Signature i=$i no group of signatures passed");
714 801 ▶ return 1 if $state eq 'pass';
Branch: 1 branch
BranchTrueFalseTotal
if $state eq "pass" 744 57 801
715 57 return $self->_outcome($why);
716 }
717  
718 # One s= item's [$state, $text] (see _check_sig_items).
719 869 ▶sub _check_sig_item {
Subroutine _check_sig_item: called CC=15 SCAR=27.5
720 869 my ($self, $i, $idx) = @_;
721 869 my $dk2_entry = $self->{_dk2_headers}{$i};
722 869 my $signature = $dk2_entry->{sig};
723 869 ▶ my $sel = $signature->selector($idx) // '?';
Condition50%1/2
$signature->selector($idx) // "?"
l!l
8690
MC/DC0%0/1
$signature->selector($idx) // "?"
$signature->selector($idx)
Truth table50%1/2
A
A= $signature->selector($idx)
Aresult
11
00
724 869 ▶ my $alg = $signature->algorithm($idx) // '';
Condition50%1/2
$signature->algorithm($idx) // ""
l!l
8690
MC/DC0%0/1
$signature->algorithm($idx) // ""
$signature->algorithm($idx)
Truth table50%1/2
A
A= $signature->algorithm($idx)
Aresult
11
00
725 869 ▶ return ('permerror', "PERMERROR: DKIM2-Signature i=$i selector $sel algorithm unknown")
Branch: 1 branch
BranchTrueFalseTotal
unless _key_class($alg) 20 849 869
726 unless _key_class($alg);
727  
728 # The fetch is eval'd whichever way the key is sourced: a pubkey
729 # callback may end in fetch_public_key(), which dies on transient DNS.
730 # Guarding only the no-callback branch once let that croak escape the
731 # verifier and take the caller down with it -- the reflector dropped the
732 # message outright instead of reflecting it unsigned.
733 849 my $pubkey;
734 849 my $fetched = eval {
735 $pubkey = $self->{PubkeyCallback}
736 849 ▶ ? $self->{PubkeyCallback}->($signature, $idx, $self)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'PubkeyCallback'} ? : 733 116 849
737 : $self->fetch_public_key($signature, $idx);
738 793 1;
739 };
740 849 ▶ unless ($fetched) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fetched) 56 793 849
741 56 ▶ die $@ if ref $@;
Branch: 1 branch
BranchTrueFalseTotal
if ref $@ 2 54 56
742 # Drop croak's " at FILE line N." tail: this reason may be
743 # reported in Authentication-Results on mail we send out, and our
744 # source paths are nobody else's business.
745 54 (my $why = $@) =~ s/\s+at\s+\S+\s+line\s+\d+\.?\s*\z//;
746 54 $why =~ s/\s+\z//;
747 # A key record that is there but unusable (section 14.5: several
748 # records, a syntax error, revoked, the wrong key type).
749 54 ▶ return ('permerror', "PERMERROR: DKIM2-Signature i=$i public key $sel $1")
Branch: 1 branch
BranchTrueFalseTotal
if $why =~ /\APERMERROR:\s*(.*)\z/s 10 44 54
750 if $why =~ /\APERMERROR:\s*(.*)\z/s;
751 # A transient DNS failure is a TEMPERROR -- retryable, not a
752 # permanent "does not exist", and emphatically not a FAIL, which
753 # reads as a forged signature. The resolver's reason goes to
754 # diagnostic(), not into the section 14.5 string.
755 44 $self->{diagnostic} = $why;
756 44 return ('temperror', "TEMPERROR: DKIM2-Signature i=$i public key $sel could not be fetched");
757 }
758 # Section 14.5: "a DNS result that indicates the key is absent is
759 # considered to be a PERMERROR".
760 793 ▶ return ('permerror', "PERMERROR: DKIM2-Signature i=$i public key $sel does not exist")
Branch: 1 branch
BranchTrueFalseTotal
unless $pubkey 19 774 793
761 unless $pubkey;
762  
763 # The key must be of the signature's algorithm. A PubkeyCallback
764 # hands back any key object, so check what it is.
765 774 ▶ return ('permerror', "PERMERROR: DKIM2-Signature i=$i public key $sel algorithm mismatch")
Branch: 1 branch
BranchTrueFalseTotal
unless $pubkey->isa(_key_class($alg)) 1 773 774
766 unless $pubkey->isa(_key_class($alg));
767  
768 # -09 sections 3.2 and 14.6: an RSA key under 1024 bits is too short
769 # (contract E5). A PubkeyCallback's key has not been through
770 # parse_dkim_key_record, which refuses one, so it is checked here too.
771 773 ▶ return ('permerror', "PERMERROR: DKIM2-Signature i=$i public key $sel too short")
Branch: 1 branch
BranchTrueFalseTotal
if $alg eq "rsa-sha256" and $pubkey->size * 8 < 1024 1 772 773
Condition100%3/3
$alg eq "rsa-sha256" and $pubkey->size * 8 < 1024
!ll&&!rl&&r
17711
MC/DC100%2/2
$alg eq "rsa-sha256" and $pubkey->size * 8 < 1024
$alg eq "rsa-sha256" $pubkey->size * 8 < 1024
Truth table100%3/3
A and B
A= $alg eq "rsa-sha256"B= $pubkey->size * 8 < 1024
ABresult
0X0
100
111
772 if $alg eq 'rsa-sha256' && $pubkey->size * 8 < 1024;
773  
774 772 my $signing_input = $dk2_entry->{signing_input};
775 772 my $sig_raw = decode_base64($signature->signature_value($idx));
776 772 my $verified = eval {
777 772 Mail::DKIM2::Key::signature_algorithm($alg)->{verify}->($pubkey, $signing_input, $sig_raw);
778 };
779 772 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 772 772
780 0 ▶ die $@ if ref $@;
Branch: 1 branch
BranchTrueFalseTotal
if ref $@ 0 0 0
781 0 ($self->{diagnostic} = "$@") =~ s/\s+\z//;
782 0 $verified = 0;
783 }
784 772 ▶ return $verified ? ('pass')
Branch: 1 branch
BranchTrueFalseTotal
$verified ? : 754 18 772
785 : ('fail', "FAIL: DKIM2-Signature i=$i selector $sel incorrect signature");
786 }
787  
788 # Chain of Custody (-09 section 7.1 item 1, section 14.4). Every failure is
789 # a FAIL (-08 moved them from PERMERROR: contract D5). For each signature S, from i=1 up:
790 #
791 # - S against the one below it, P (from i=2): if P carries nd=, S's d= must
792 # be that domain (without regard to case), and nothing else is compared.
793 # Otherwise some rt= address of P must relaxed-match (section 7.2) --
794 # when S has nd= or a null mf=, S's d= (the rt= domain shortened);
795 # otherwise S's mf= domain (shortened) against the rt= domain.
796 # - S's own mf= domain must relaxed-match its d=, unless the mf= is null or
797 # S has nd= (and so no mf=).
798 #
799 # Which of the two tests runs first only matters for which error is
800 # reported when both fail; the link to P comes first.
801 526 ▶sub _verify_chain {
Subroutine _verify_chain: called CC=24 SCAR=32.2
802 526 my ($self) = @_;
803 526 my %dk2_map = %{$self->{_dk2_headers}};
804 526 my @dk2_is = sort { $a <=> $b } keys %dk2_map;
805  
806 526 ▶ my $has_nd = sub { my $nd = $_[0]->next_domain; defined $nd && length $nd };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $nd and length $nd 54 1355 1409
807 526 for my $idx (0 .. $#dk2_is) {
808 855 my $i = $dk2_is[$idx];
809 855 my $sig = $dk2_map{$i}{sig};
810 855 ▶ my $d = $sig->domain // '';
Condition50%1/2
$sig->domain // ""
l!l
8550
MC/DC0%0/1
$sig->domain // ""
$sig->domain
Truth table50%1/2
A
A= $sig->domain
Aresult
11
00
811 855 my $mf = $sig->mail_from;
812 855 ▶ my $null_mf = defined $mf && $mf =~ /\A<\s*>\z/;
Condition100%3/3
defined $mf && $mf =~ /\A<\s*>\z/
!ll&&!rl&&r
4579119
MC/DC100%2/2
defined $mf && $mf =~ /\A<\s*>\z/
defined $mf $mf =~ /\A<\s*>\z/
Truth table100%3/3
A && B
A= defined $mfB= $mf =~ /\A<\s*>\z/
ABresult
0X0
100
111
813  
814 855 ▶ if ($idx > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx > 0) 329 526 855
815 329 my $p_i = $dk2_is[$idx - 1];
816 329 my $prev = $dk2_map{$p_i}{sig};
817 329 ▶ if ($has_nd->($prev)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$has_nd($prev)) { } 27 302 329
818 27 ▶ return $self->_error(fail => "DKIM2-Signature i=$p_i nd= does not match")
Branch: 1 branch
BranchTrueFalseTotal
unless to_alabel($prev->next_domain) =~ tr/A-Z/a-z/r eq to_alabel($d) =~ tr/A-Z/a-z/r 5 22 27
819 unless (to_alabel($prev->next_domain) =~ tr/A-Z/a-z/r) eq (to_alabel($d) =~ tr/A-Z/a-z/r);
820 } else {
821 302 ▶ my @rt = map { [domain_of($_)] } @{ $prev->rcpt_to || [] };
Condition100%2/2
$prev->rcpt_to || []
l!l
3011
MC/DC100%1/1
$prev->rcpt_to || []
$prev->rcpt_to
Truth table100%2/2
A
A= $prev->rcpt_to
Aresult
11
00
822 302 ▶ if ($has_nd->($sig) || $null_mf) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$has_nd($sig) or $null_mf) { } 31 271 302
Condition100%3/3
&$has_nd($sig) or $null_mf
l!l&&r!l&&!r
274271
MC/DC100%2/2
&$has_nd($sig) or $null_mf
&$has_nd($sig) $null_mf
Truth table100%3/3
A or B
A= &$has_nd($sig)B= $null_mf
ABresult
1X1
011
000
823 return $self->_error(fail =>
824 "DKIM2-Signature i=$i d= does not match the rt= of i=$p_i")
825 31 ▶ unless grep { $_->[0] && !$_->[1] && relaxed_domain_match($_->[0], $d) } @rt;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_->[0] and not $_->[1] and relaxed_domain_match($_->[0], $d);} @rt 8 23 31
Condition50%3/6
$_->[0] && !$_->[1] && relaxed_domain_match($_->[0], $d)
!ll&&!rl&&r
0823
$_->[0] and not $_->[1]
!ll&&!rl&&r
0031
MC/DC33%1/3
$_->[0] && !$_->[1] && relaxed_domain_match($_->[0], $d)
$_->[0] not $_->[1] relaxed_domain_match($_->[0], $d)
Truth table50%2/4
A and not(B) && C
A= $_->[0]B= not $_->[1]C= relaxed_domain_match($_->[0], $d)
ABCresult
0XX0
10X0
1100
1111
826 } else {
827 271 my ($mfd, $lit) = domain_of($mf);
828 return $self->_error(fail =>
829 "DKIM2-Signature i=$i mf= does not match the rt= of i=$p_i")
830 unless $mfd && !$lit
831 271 ▶ && grep { $_->[0] && !$_->[1] && relaxed_domain_match($mfd, $_->[0]) } @rt;
Branch: 1 branch
BranchTrueFalseTotal
unless $mfd and not $lit and grep {$_->[0] and not $_->[1] and relaxed_domain_match($mfd, $_->[0]);} @rt 8 263 271
Condition75%9/12
$mfd and not $lit and grep {$_->[0] and not $_->[1] and relaxed_domain_match($mfd, $_->[0]);} @rt
!ll&&!rl&&r
26263
$mfd and not $lit
!ll&&!rl&&r
11269
$_->[0] && !$_->[1] && relaxed_domain_match($mfd, $_->[0])
!ll&&!rl&&r
05263
$_->[0] and not $_->[1]
!ll&&!rl&&r
00268
MC/DC100%3/3
$mfd and not $lit and grep {$_->[0] and not $_->[1] and relaxed_domain_match($mfd, $_->[0]);} @rt
$mfd not $lit grep {$_->[0] and not $_->[1] and relaxed_domain_match($mfd, $_->[0]);} @rt
MC/DC33%1/3
$_->[0] && !$_->[1] && relaxed_domain_match($mfd, $_->[0])
$_->[0] not $_->[1] relaxed_domain_match($mfd, $_->[0])
Truth table100%4/4
A and not(B) and C
A= $mfdB= not $litC= grep {$_->[0] and not $_->[1] and relaxed_domain_match($mfd, $_->[0]);} @rt
ABCresult
0XX0
10X0
1100
1111
Truth table50%2/4
A and not(B) && C
A= $_->[0]B= not $_->[1]C= relaxed_domain_match($mfd, $_->[0])
ABCresult
0XX0
10X0
1100
1111
832 }
833 }
834 }
835  
836 834 ▶ if (defined $mf && !$null_mf && !$has_nd->($sig)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mf and not $null_mf and not &$has_nd($sig)) 778 56 834
Condition83%5/6
defined $mf and not $null_mf and not &$has_nd($sig)
!ll&&!rl&&r
560778
defined $mf and not $null_mf
!ll&&!rl&&r
3917778
MC/DC66%2/3
defined $mf and not $null_mf and not &$has_nd($sig)
defined $mf not $null_mf not &$has_nd($sig)
Truth table75%3/4
A and not(B) and not(C)
A= defined $mfB= not $null_mfC= not &$has_nd($sig)
ABCresult
0XX0
10X0
1100
1111
837 778 my ($mfd, $lit) = domain_of($mf);
838 778 ▶ return $self->_error(fail => "DKIM2-Signature i=$i mf= and d= do not match")
Branch: 1 branch
BranchTrueFalseTotal
unless $mfd and not $lit and relaxed_domain_match($mfd, $d) 5 773 778
Condition50%3/6
$mfd and not $lit and relaxed_domain_match($mfd, $d)
!ll&&!rl&&r
05773
$mfd and not $lit
!ll&&!rl&&r
00778
MC/DC33%1/3
$mfd and not $lit and relaxed_domain_match($mfd, $d)
$mfd not $lit relaxed_domain_match($mfd, $d)
Truth table50%2/4
A and not(B) and C
A= $mfdB= not $litC= relaxed_domain_match($mfd, $d)
ABCresult
0XX0
10X0
1100
1111
839 unless $mfd && !$lit && relaxed_domain_match($mfd, $d);
840 }
841 }
842  
843 500 return 1;
844 }
845  
846 # Record an outcome: result $state, details "STATE: $text" (-09 section
847 # 14.1's form). Returns 0, for "return $self->_error(...)".
848 485 ▶sub _error {
Subroutine _error: called CC=1 SCAR=0.0
849 485 my ($self, $state, $text) = @_;
850 485 $self->{result} = $state;
851 485 $self->{details} = uc($state) . ": $text";
852 485 return 0;
853 }
854  
855 # Record a reason that may already carry its state ("FAIL: ...", as
856 # MessageInstance reports a hash mismatch): that state, else PERMERROR.
857 # Returns 0.
858 328 ▶sub _outcome {
Subroutine _outcome: called CC=3 SCAR=12.0
859 328 my ($self, $reason) = @_;
860 328 ▶ $reason //= 'unknown error';
Condition50%1/2
$reason //= "unknown error"
l!l
3280
MC/DC0%0/1
$reason //= "unknown error"
$reason
Truth table50%1/2
A
A= $reason
Aresult
11
00
861 328 ▶ if ($reason =~ /\A(PERMERROR|FAIL|TEMPERROR): (.*)\z/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reason =~ /\A(PERMERROR|FAIL|TEMPERROR): (.*)\z/s) 328 0 328
862 328 return $self->_error(lc $1 => $2);
863 }
864 0 return $self->_error(permerror => $reason);
865 }
866  
867 # --- Public key lookup ---
868  
869 # fetch_public_key($signature, $idx): the default key source, a TXT lookup of
870 # <selector>._domainkey.<d=> through the Resolver option (a Net::DNS::Resolver
871 # or anything with the same query/errorstring interface; one is made if none
872 # was given). Returns a Crypt::PK object, or undef when the answer positively
873 # says there is no such record. Anything else -- a timeout, SERVFAIL, REFUSED,
874 # a network error, or an errorstring this code has never seen -- dies with a
875 # TEMPERROR: -09 §13 makes a DNS failure retryable, never a permanent "no
876 # verifiable signature items", and emphatically never a 'fail', which reads as
877 # a forged signature. _check_sig_item's eval maps the die to temperror.
878 # A record that is there but unusable dies with "PERMERROR: <why>" instead
879 # (-09 §14.5): more than one TXT record, a record that does not validate
880 # (parse_dkim_key_record), revoked, or a key type that is not the signature
881 # algorithm's -- mapped to permerror, with the selector.
882 #
883 # A PubkeyCallback replaces this; it is called as ($signature, $idx, $verifier)
884 # so a callback that only overrides some keys can fall back to
885 # $verifier->fetch_public_key($signature, $idx) and keep the classification.
886 135 ▶sub fetch_public_key {
Subroutine fetch_public_key: called CC=16 SCAR=28.2
Pod documented
887 135 my ($self, $signature, $idx) = @_;
888 135 ▶ $idx //= 0;
Condition50%1/2
$idx //= 0
l!l
1350
MC/DC0%0/1
$idx //= 0
$idx
Truth table50%1/2
A
A= $idx
Aresult
11
00
889 135 my $sel = $signature->selector($idx);
890 135 my $dom = $signature->domain;
891 135 ▶ croak "missing selector or domain" unless $sel && $dom;
Branch: 1 branch
BranchTrueFalseTotal
unless $sel and $dom 0 135 135
Condition33%1/3
$sel and $dom
!ll&&!rl&&r
00135
MC/DC0%0/2
$sel and $dom
$sel $dom
Truth table33%1/3
A and B
A= $selB= $dom
ABresult
0X0
100
111
892  
893 135 my $fqdn = "$sel._domainkey.$dom";
894 135 ▶ my ($reply, $err) = @{ $self->{_key_replies}{lc $fqdn} //= $self->_key_reply($fqdn) };
Condition66%2/3
$self->{"_key_replies"}{lc $fqdn} //= $self->_key_reply($fqdn)
l!l&&r!l&&!r
81270
MC/DC0%0/2
$self->{"_key_replies"}{lc $fqdn} //= $self->_key_reply($fqdn)
$self->{"_key_replies"}{lc $fqdn} $self->_key_reply($fqdn)
Truth table66%2/3
A //= B
A= $self->{"_key_replies"}{lc $fqdn}B= $self->_key_reply($fqdn)
ABresult
1X1
011
000
895 135 ▶ unless ($reply) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($reply) 46 89 135
896 46 ▶ $err //= '';
Condition50%1/2
$err //= ""
l!l
460
MC/DC0%0/1
$err //= ""
$err
Truth table50%1/2
A
A= $err
Aresult
11
00
897 46 ▶ return if $err =~ /^(?:NXDOMAIN|NOERROR|NODATA)\s*$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $err =~ /^(?:NXDOMAIN|NOERROR|NODATA)\s*$/i 5 41 46
898 41 croak "TEMPERROR: DNS lookup for $fqdn failed: $err";
899 }
900 # One TXT RR may hold several strings, joined with nothing between;
901 # several RRs for one selector are an error (-09 §14.5).
902 # -09 section 3.7: records beginning v= other than DKIM1 are ignored
903 # before counting (contract E4).
904 my @txt = Mail::DKIM2::Key::key_records(
905 89 map { join '', $_->txtdata } grep { $_->type eq 'TXT' } $reply->answer);
906 89 ▶ return unless @txt;
Branch: 1 branch
BranchTrueFalseTotal
unless @txt 1 88 89
907 88 ▶ croak "PERMERROR: has multiple records\n" if @txt > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @txt > 1 4 84 88
908 84 my ($key, $why) = Mail::DKIM2::Key::parse_dkim_key_record($txt[0]);
909 84 ▶ $why = 'algorithm mismatch'
Branch: 1 branch
BranchTrueFalseTotal
if $why and $why eq "has an unsupported key type" 1 83 84
Condition100%3/3
$why and $why eq "has an unsupported key type"
!ll&&!rl&&r
7761
MC/DC100%2/2
$why and $why eq "has an unsupported key type"
$why $why eq "has an unsupported key type"
Truth table100%3/3
A and B
A= $whyB= $why eq "has an unsupported key type"
ABresult
0X0
100
111
910 if $why && $why eq 'has an unsupported key type';
911 84 ▶ croak "PERMERROR: $why\n" if $why;
Branch: 1 branch
BranchTrueFalseTotal
if $why 7 77 84
912 # §10.9: the key's type must be the signature's algorithm's.
913 77 ▶ my $alg = $signature->algorithm($idx) // '';
Condition50%1/2
$signature->algorithm($idx) // ""
l!l
770
MC/DC0%0/1
$signature->algorithm($idx) // ""
$signature->algorithm($idx)
Truth table50%1/2
A
A= $signature->algorithm($idx)
Aresult
11
00
914 77 ▶ croak "PERMERROR: algorithm mismatch\n"
Branch: 1 branch
BranchTrueFalseTotal
if _key_class($alg) and not $key->isa(_key_class($alg)) 1 76 77
Condition66%2/3
_key_class($alg) and not $key->isa(_key_class($alg))
!ll&&!rl&&r
0761
MC/DC50%1/2
_key_class($alg) and not $key->isa(_key_class($alg))
_key_class($alg) not $key->isa(_key_class($alg))
Truth table66%2/3
A and not(B)
A= _key_class($alg)B= not $key->isa(_key_class($alg))
ABresult
0X0
100
111
915 if _key_class($alg) && !$key->isa(_key_class($alg));
916 76 return $key;
917 }
918  
919 802 ▶sub _resolver {
Subroutine _resolver: called CC=2 SCAR=7.0
920 802 my ($self) = @_;
921 802 ▶ return $self->{Resolver} //= do {
Condition66%2/3
$self->{"Resolver"} //= do { require Net::DNS::Resolver; "Net::DNS::Resolver"->new }
l!l&&r!l&&!r
2185840
MC/DC0%0/2
$self->{"Resolver"} //= do { require Net::DNS::Resolver; "Net::DNS::Resolver"->new }
$self->{"Resolver"} do { require Net::DNS::Resolver; "Net::DNS::Resolver"->new }
Truth table66%2/3
A //= B
A= $self->{"Resolver"}B= do { require Net::DNS::Resolver; "Net::DNS::Resolver"->new }
ABresult
1X1
011
000
922 584 require Net::DNS::Resolver;
923 584 Net::DNS::Resolver->new;
924 };
925 }
926  
927 # Whether $resolver's background calls can be used for the prefetch. A
928 # subclass of Net::DNS::Resolver that overrides send or query (its own
929 # transport, caching, metrics -- FastMTA's and authentication_milter's do)
930 # but not bgsend would have Net::DNS's bgsend bypass it, and that asks only
931 # the first nameserver; such a resolver gets every lookup through query().
932 # One that provides its own bgsend is trusted with it.
933 684 ▶sub _can_prefetch {
Subroutine _can_prefetch: called CC=9 SCAR=22.4
934 684 my ($resolver) = @_;
935 684 ▶ return 0 unless Scalar::Util::blessed($resolver);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($resolver) 0 684 684
936 684 ▶ my $bgsend = $resolver->can('bgsend') or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $bgsend = $resolver->can("bgsend") or return 0 641 43 684
937 641 require Net::DNS::Resolver;
938 641 ▶ return 1 if $bgsend != (Net::DNS::Resolver->can('bgsend') // 0);
Branch: 1 branch
BranchTrueFalseTotal
if $bgsend != ("Net::DNS::Resolver"->can("bgsend") // 0) 28 613 641
Condition50%1/2
"Net::DNS::Resolver"->can("bgsend") // 0
l!l
6410
MC/DC0%0/1
"Net::DNS::Resolver"->can("bgsend") // 0
"Net::DNS::Resolver"->can("bgsend")
Truth table50%1/2
A
A= "Net::DNS::Resolver"->can("bgsend")
Aresult
11
00
939 613 for my $method (qw(send query)) {
940 1224 ▶ my $own = $resolver->can($method) // 0;
Condition50%1/2
$resolver->can($method) // 0
l!l
12240
MC/DC0%0/1
$resolver->can($method) // 0
$resolver->can($method)
Truth table50%1/2
A
A= $resolver->can($method)
Aresult
11
00
941 1224 ▶ return 0 unless $own == (Net::DNS::Resolver->can($method) // -1);
Branch: 1 branch
BranchTrueFalseTotal
unless $own == ("Net::DNS::Resolver"->can($method) // -1) 4 1220 1224
Condition50%1/2
"Net::DNS::Resolver"->can($method) // -1
l!l
12240
MC/DC0%0/1
"Net::DNS::Resolver"->can($method) // -1
"Net::DNS::Resolver"->can($method)
Truth table50%1/2
A
A= "Net::DNS::Resolver"->can($method)
Aresult
11
00
942 }
943 609 return 1;
944 }
945  
946 # Every key the signatures name, asked for at once, in the background, as
947 # soon as the header fields are in: the answers come back while the body
948 # arrives and is hashed. Only with a resolver whose background calls are
949 # safe to use (_can_prefetch), and not when a PubkeyCallback fetches the
950 # keys. Never fatal: a key not asked for here is asked for when it is wanted.
951 874 ▶sub _prefetch_keys {
Subroutine _prefetch_keys: called CC=15 SCAR=27.6
952 874 my ($self) = @_;
953 874 ▶ return if $self->{PubkeyCallback};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"PubkeyCallback"} 813 61 874
954 61 my $resolver = $self->_resolver;
955 61 ▶ return unless _can_prefetch($resolver);
Branch: 1 branch
BranchTrueFalseTotal
unless _can_prefetch($resolver) 23 38 61
956 38 my $sent = 0;
957 38 for my $entry (sort { $b->{sig}->sequence <=> $a->{sig}->sequence }
958 38 values %{$self->{_dk2_headers}}) {
959 90 ▶ last if $sent >= $PREFETCH_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if $sent >= $Mail::DKIM2::Verifier::PREFETCH_MAX 1 89 90
960 89 my $sig = $entry->{sig};
961 89 eval {
962 89 ▶ my $dom = $sig->domain or return;
Branch: 1 branch
BranchTrueFalseTotal
my $dom = $sig->domain or return 89 0 89
963 89 ▶ for my $idx (0 .. ($sig->sig_count || 0) - 1) {
Condition50%1/2
$sig->sig_count || 0
l!l
890
MC/DC0%0/1
$sig->sig_count || 0
$sig->sig_count
Truth table50%1/2
A
A= $sig->sig_count
Aresult
11
00
964 97 ▶ return if $sent >= $PREFETCH_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if $sent >= $Mail::DKIM2::Verifier::PREFETCH_MAX 1 96 97
965 96 ▶ next unless _key_class($sig->algorithm($idx) // '');
Branch: 1 branch
BranchTrueFalseTotal
unless _key_class($sig->algorithm($idx) // "") 0 96 96
Condition50%1/2
$sig->algorithm($idx) // ""
l!l
960
MC/DC0%0/1
$sig->algorithm($idx) // ""
$sig->algorithm($idx)
Truth table50%1/2
A
A= $sig->algorithm($idx)
Aresult
11
00
966 96 ▶ my $sel = $sig->selector($idx) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $sel = $sig->selector($idx) or next 96 0 96
967 96 my $fqdn = "$sel._domainkey.$dom";
968 96 ▶ next if $self->{_key_queries}{lc $fqdn};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_key_queries"}{lc $fqdn} 10 86 96
969 86 $sent++;
970 86 ▶ my $handle = $resolver->bgsend($fqdn, 'TXT') or next;
Branch: 1 branch
BranchTrueFalseTotal
my $handle = $resolver->bgsend($fqdn, "TXT") or next 86 0 86
971 86 $self->{_key_queries}{lc $fqdn} =
972 [$handle, Time::HiRes::time() + $PREFETCH_WAIT];
973 }
974 };
975 89 ▶ die $@ if ref $@;
Branch: 1 branch
BranchTrueFalseTotal
if ref $@ 0 89 89
976 }
977 }
978  
979 # The Domain Policy Records section 14.8 may need, asked for in the
980 # background at the end of the headers with the keys (0.30): for each
981 # (signature, From) pair whose d= is below the From domain -- the only case
982 # that looks one up -- _domainkey.<From domain>, at most $PREFETCH_MAX
983 # domains. Under the same conditions as the key prefetch (_can_prefetch),
984 # and not with a PolicyCallback. Never fatal: a record not asked for here
985 # is asked for with query() when it is wanted.
986 874 ▶sub _prefetch_policies {
Subroutine _prefetch_policies: called CC=14 SCAR=26.6
987 874 my ($self) = @_;
988 874 ▶ return if $self->{PolicyCallback};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"PolicyCallback"} 251 623 874
989 623 my $resolver = $self->_resolver;
990 623 ▶ return unless _can_prefetch($resolver);
Branch: 1 branch
BranchTrueFalseTotal
unless _can_prefetch($resolver) 24 599 623
991 599 my %dk2_map = %{$self->{_dk2_headers}};
992 599 my $sent = 0;
993 599 my @pairs = eval { $self->_from_pairs };
994 599 ▶ die $@ if ref $@;
Branch: 1 branch
BranchTrueFalseTotal
if ref $@ 0 599 599
995 599 for my $pair (@pairs) {
996 681 ▶ last if $sent >= $PREFETCH_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if $sent >= $Mail::DKIM2::Verifier::PREFETCH_MAX 0 681 681
997 681 my ($i, $from) = @$pair;
998 681 ▶ my $d = $dk2_map{$i}{sig}->domain // next;
Condition100%2/2
$dk2_map{$i}{"sig"}->domain // (next)
l!l
6801
MC/DC100%1/1
$dk2_map{$i}{"sig"}->domain // (next)
$dk2_map{$i}{"sig"}->domain
Truth table100%2/2
A
A= $dk2_map{$i}{"sig"}->domain
Aresult
11
00
999 680 my ($state, $dom) = Mail::DKIM2::Address::from_mailbox(@$from);
1000 680 ▶ next unless $state eq 'ok' && length $d
Branch: 1 branch
BranchTrueFalseTotal
unless $state eq "ok" and length $d and not relaxed_domain_match($dom, $d) and relaxed_domain_match($d, $dom) 678 2 680
Condition88%8/9
$state eq "ok" and length $d and not relaxed_domain_match($dom, $d) and relaxed_domain_match($d, $dom)
!ll&&!rl&&r
67352
$state eq "ok" and length $d and not relaxed_domain_match($dom, $d)
!ll&&!rl&&r
925817
$state eq "ok" and length $d
!ll&&!rl&&r
920588
MC/DC75%3/4
$state eq "ok" and length $d and not relaxed_domain_match($dom, $d) and relaxed_domain_match($d, $dom)
$state eq "ok" length $d not relaxed_domain_match($dom, $d) relaxed_domain_match($d, $dom)
Truth table80%4/5
A and B and not(C) and D
A= $state eq "ok"B= length $dC= not relaxed_domain_match($dom, $d)D= relaxed_domain_match($d, $dom)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1001 && !relaxed_domain_match($dom, $d) && relaxed_domain_match($d, $dom);
1002 2 my $fqdn = "_domainkey.$dom";
1003 2 ▶ next if $self->{_key_queries}{lc $fqdn};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_key_queries"}{lc $fqdn} 0 2 2
1004 2 $sent++;
1005 2 my $handle = eval { $resolver->bgsend($fqdn, 'TXT') };
1006 2 ▶ die $@ if ref $@;
Branch: 1 branch
BranchTrueFalseTotal
if ref $@ 0 2 2
1007 2 ▶ next unless $handle;
Branch: 1 branch
BranchTrueFalseTotal
unless $handle 0 2 2
1008 2 $self->{_key_queries}{lc $fqdn} = [$handle, Time::HiRes::time() + $PREFETCH_WAIT];
1009 }
1010 }
1011  
1012 # The DNS answer for $fqdn as [$reply] or [undef, $error], query()'s
1013 # results: from the background query if it gave one that settles it (an
1014 # answer, NXDOMAIN, or NOERROR with none), else from query().
1015 133 ▶sub _key_reply {
Subroutine _key_reply: called CC=3 SCAR=11.0
1016 133 my ($self, $fqdn) = @_;
1017 133 ▶ if (($self->{_lookup_waited} // 0) >= $LOOKUP_BUDGET) {
Branch: 1 branch
BranchTrueFalseTotal
if (($self->{"_lookup_waited"} // 0) >= $Mail::DKIM2::Verifier::LOOKUP_BUDGET) 15 118 133
Condition100%2/2
$self->{"_lookup_waited"} // 0
l!l
7855
MC/DC100%1/1
$self->{"_lookup_waited"} // 0
$self->{"_lookup_waited"}
Truth table100%2/2
A
A= $self->{"_lookup_waited"}
Aresult
11
00
1018 15 delete $self->{_key_queries}; # nothing more is read from these
1019 15 return [undef, "this message's lookups have taken $LOOKUP_BUDGET s"];
1020 }
1021 118 my $started = _now();
1022 118 my $answer = $self->_key_reply_unbudgeted($fqdn);
1023 118 $self->{_lookup_waited} += _now() - $started;
1024 118 return $answer;
1025 }
1026  
1027 118 ▶sub _key_reply_unbudgeted {
Subroutine _key_reply_unbudgeted: called CC=13 SCAR=25.7
1028 118 my ($self, $fqdn) = @_;
1029 118 my $resolver = $self->_resolver;
1030 118 ▶ if (my $q = delete $self->{_key_queries}{lc $fqdn}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $q = delete $self->{"_key_queries"}{lc $fqdn}) 48 70 118
1031 48 my $ready = 1;
1032 48 ▶ while ($resolver->bgbusy($q->[0])) { # may swap in a TCP handle
Branch: 1 branch
BranchTrueFalseTotal
if ($resolver->bgbusy($q->[0])) 42 43 85
1033 42 my $left = $q->[1] - Time::HiRes::time();
1034 42 ▶ if ($left <= 0) { $ready = 0; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($left <= 0) 5 37 42
1035 # Sleep between polls: a resolver's bgbusy need not block.
1036 37 ▶ my $poll = $PREFETCH_POLL > 0.001 ? $PREFETCH_POLL : 0.001;
Branch: 1 branch
BranchTrueFalseTotal
$Mail::DKIM2::Verifier::PREFETCH_POLL > 0.001 ? : 37 0 37
1037 37 ▶ Time::HiRes::sleep($left < $poll ? $left : $poll);
Branch: 1 branch
BranchTrueFalseTotal
$left < $poll ? : 1 36 37
1038 }
1039 # A truncated answer (Net::DNS's TCP retry failed) settles nothing.
1040 48 ▶ my $packet = $ready && $resolver->bgread($q->[0]);
Condition100%3/3
$ready && $resolver->bgread($q->[0])
!ll&&!rl&&r
51132
MC/DC100%2/2
$ready && $resolver->bgread($q->[0])
$ready $resolver->bgread($q->[0])
Truth table100%3/3
A && B
A= $readyB= $resolver->bgread($q->[0])
ABresult
0X0
100
111
1041 48 ▶ if ($packet && !$packet->header->tc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packet and not $packet->header->tc) 27 21 48
Condition100%3/3
$packet and not $packet->header->tc
!ll&&!rl&&r
16527
MC/DC100%2/2
$packet and not $packet->header->tc
$packet not $packet->header->tc
Truth table100%3/3
A and not(B)
A= $packetB= not $packet->header->tc
ABresult
0X0
100
111
1042 27 my $rcode = $packet->header->rcode;
1043 27 ▶ return [$packet] if $packet->header->ancount;
Branch: 1 branch
BranchTrueFalseTotal
if $packet->header->ancount 19 8 27
1044 8 ▶ return [undef, $rcode] if $rcode eq 'NXDOMAIN' || $rcode eq 'NOERROR';
Branch: 1 branch
BranchTrueFalseTotal
if $rcode eq "NXDOMAIN" or $rcode eq "NOERROR" 2 6 8
Condition66%2/3
$rcode eq "NXDOMAIN" or $rcode eq "NOERROR"
l!l&&r!l&&!r
206
MC/DC50%1/2
$rcode eq "NXDOMAIN" or $rcode eq "NOERROR"
$rcode eq "NXDOMAIN" $rcode eq "NOERROR"
Truth table66%2/3
A or B
A= $rcode eq "NXDOMAIN"B= $rcode eq "NOERROR"
ABresult
1X1
011
000
1045 }
1046 }
1047 97 my $reply = $resolver->query($fqdn, 'TXT');
1048 97 ▶ return [$reply, $reply ? undef : $resolver->errorstring];
Branch: 1 branch
BranchTrueFalseTotal
$reply ? : 65 32 97
1049 }
1050  
1051 23 ▶sub resolver {
Subroutine resolver: called CC=2 SCAR=7.1
Pod documented
1052 23 my ($self, $val) = @_;
1053 23 ▶ $self->{Resolver} = $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 0 23 23
1054 23 return $self->{Resolver};
1055 }
1056  
1057 343 ▶sub set_pubkey_callback {
Subroutine set_pubkey_callback: called CC=1 SCAR=0.0
Pod documented
1058 343 my ($self, $cb) = @_;
1059 343 $self->{PubkeyCallback} = $cb;
1060 }
1061  
1062 27 ▶sub set_policy_callback {
Subroutine set_policy_callback: called CC=1 SCAR=0.0
Pod documented
1063 27 my ($self, $cb) = @_;
1064 27 $self->{PolicyCallback} = $cb;
1065 }
1066  
1067 1353 ▶sub result {
Subroutine result: called CC=2 SCAR=6.9
Pod documented
1068 1353 my $self = shift;
1069 1353 ▶ return $self->{result} || 'none';
Condition100%2/2
$self->{'result'} || 'none'
l!l
13521
MC/DC100%1/1
$self->{'result'} || 'none'
$self->{'result'}
Truth table100%2/2
A
A= $self->{'result'}
Aresult
11
00
1070 }
1071  
1072 615 ▶sub result_detail {
Subroutine result_detail: called CC=2 SCAR=7.4
Pod documented
1073 615 my $self = shift;
1074 615 my $result = $self->result;
1075 615 ▶ if ($self->{details}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"details"}) 615 0 615
1076 615 return "$result ($self->{details})";
1077 }
1078 0 return $result;
1079 }
1080  
1081 # The DKIM2-Signature fields the message carried, parsed, in ascending i=
1082 # order. Valid once the header block has been read. A host building an
1083 # Authentication-Results field takes header.d and header.i from the top one.
1084 2 ▶sub signatures { return $_[0]->{_chain}->signatures }
Subroutine signatures: called CC=1 SCAR=0.0
Pod documented
1085 25 ▶sub top_signature { return $_[0]->{_chain}->top_signature }
Subroutine top_signature: called CC=1 SCAR=0.0
Pod documented
1086  
1087 # The bare reason for the result, with no result word wrapped around it.
1088 # result_detail() is the display form ("temperror (...)"); callers that embed
1089 # the reason in a report of their own -- an Authentication-Results comment, say
1090 # -- want this one, or they end up saying the result twice.
1091 297 ▶sub details {
Subroutine details: called CC=1 SCAR=0.0
Pod documented
1092 297 my $self = shift;
1093 297 return $self->{details};
1094 }
1095  
1096 # -09 section 13 (contract D9): a pass for which every instance's hashes
1097 # were checked -- not one below a null body Recipe, whose instances' body
1098 # hashes cannot be, nor a HeadersOnly or InstancesOnly verification.
1099 23 ▶sub fully_verified {
Subroutine fully_verified: called CC=5 SCAR=16.3
Pod documented
1100 23 my ($self) = @_;
1101 return ($self->{result} // '') eq 'pass'
1102 23 ▶ && !$self->{HeadersOnly} && !$self->{InstancesOnly} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
($self->{'result'} // '') eq 'pass' && !$self->{'HeadersOnly'} && !$self->{'InstancesOnly'} ? : 8 15 23
Condition75%6/8
($self->{"result"} // "") eq "pass" and not $self->{"HeadersOnly"} and not $self->{"InstancesOnly"}
!ll&&!rl&&r
1508
($self->{"result"} // "") eq "pass" and not $self->{"HeadersOnly"}
!ll&&!rl&&r
1418
$self->{"result"} // ""
l!l
230
MC/DC33%1/3
($self->{"result"} // "") eq "pass" and not $self->{"HeadersOnly"} and not $self->{"InstancesOnly"}
$self->{"result"} not $self->{"HeadersOnly"} not $self->{"InstancesOnly"}
Truth table50%2/4
A and not(B) and not(C)
A= $self->{"result"}B= not $self->{"HeadersOnly"}C= not $self->{"InstancesOnly"}
ABCresult
0XX0
10X0
1100
1111
1103 }
1104  
1105 # provisionally_verified(): -09 section 13.2. After CLOSE, true when the
1106 # highest i= DKIM2-Signature and the highest m= Message-Instance pass that
1107 # section's checks -- the initial assessment that lets a receiver accept a
1108 # message, and the only kind of message a DSN may be sent for (section
1109 # 16.2). A pass is; otherwise the top signature and the top instance are
1110 # checked on their own, whatever went wrong lower down: the signature
1111 # covers the top instance (step 1), its s= passes (2), the instance's h=
1112 # passes (3), the envelope matches its mf= and rt= when the Verifier was
1113 # given one (4: MailFrom, RcptTo), and its d= relaxed-matches its mf=
1114 # unless that is null (5).
1115 # A top signature with nd= is not (section 13.2's last paragraph). The
1116 # verdict (result, details, diagnostic) is left as it was.
1117 22 ▶sub provisionally_verified {
Subroutine provisionally_verified: called CC=3 SCAR=11.1
Pod documented
1118 22 my ($self) = @_;
1119 22 ▶ return $self->{_provisional} //= $self->_provisionally_verified ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_provisionally_verified ? : 15 7 22
Condition50%1/2
$self->{"_provisional"} //= $self->_provisionally_verified ? 1 : 0
l!l
022
MC/DC0%0/1
$self->{"_provisional"} //= $self->_provisionally_verified ? 1 : 0
$self->{"_provisional"}
Truth table50%1/2
A
A= $self->{"_provisional"}
Aresult
11
00
1120 }
1121  
1122 22 ▶sub _provisionally_verified {
Subroutine _provisionally_verified: called CC=29 SCAR=38.2
1123 22 my ($self) = @_;
1124 # An InstancesOnly pass has checked no signature (review of 0.28).
1125 22 ▶ return 0 if $self->{InstancesOnly};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"InstancesOnly"} 1 21 22
1126 21 ▶ return 1 if ($self->{result} // '') eq 'pass';
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"result"} // "") eq "pass" 10 11 21
Condition50%1/2
$self->{"result"} // ""
l!l
210
MC/DC0%0/1
$self->{"result"} // ""
$self->{"result"}
Truth table50%1/2
A
A= $self->{"result"}
Aresult
11
00
1127 11 ▶ return 0 if $self->{_stopped} && !$self->{result};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_stopped"} and not $self->{"result"} 0 11 11
Condition33%1/3
$self->{"_stopped"} and not $self->{"result"}
!ll&&!rl&&r
1100
MC/DC0%0/2
$self->{"_stopped"} and not $self->{"result"}
$self->{"_stopped"} not $self->{"result"}
Truth table33%1/3
A and not(B)
A= $self->{"_stopped"}B= not $self->{"result"}
ABresult
0X0
100
111
1128 11 ▶ my %dk2 = %{ $self->{_dk2_headers} || {} };
Condition50%1/2
$self->{'_dk2_headers'} || {}
l!l
110
MC/DC0%0/1
$self->{'_dk2_headers'} || {}
$self->{'_dk2_headers'}
Truth table50%1/2
A
A= $self->{'_dk2_headers'}
Aresult
11
00
1129 11 ▶ my %mi = %{ $self->{_mi_headers} || {} };
Condition50%1/2
$self->{'_mi_headers'} || {}
l!l
110
MC/DC0%0/1
$self->{'_mi_headers'} || {}
$self->{'_mi_headers'}
Truth table50%1/2
A
A= $self->{'_mi_headers'}
Aresult
11
00
1130 11 my ($max_i) = sort { $b <=> $a } keys %dk2;
1131 0 my ($top_m) = sort { $b <=> $a } keys %mi;
1132 11 ▶ return 0 unless $max_i && $top_m;
Branch: 1 branch
BranchTrueFalseTotal
unless $max_i and $top_m 0 11 11
Condition33%1/3
$max_i and $top_m
!ll&&!rl&&r
0011
MC/DC0%0/2
$max_i and $top_m
$max_i $top_m
Truth table33%1/3
A and B
A= $max_iB= $top_m
ABresult
0X0
100
111
1133 11 my $sig = $dk2{$max_i}{sig};
1134 11 my $nd = $sig->next_domain;
1135 11 ▶ return 0 if defined $nd && length $nd;
Branch: 1 branch
BranchTrueFalseTotal
if defined $nd and length $nd 1 10 11
Condition66%2/3
defined $nd and length $nd
!ll&&!rl&&r
1001
MC/DC50%1/2
defined $nd and length $nd
defined $nd length $nd
Truth table66%2/3
A and B
A= defined $ndB= length $nd
ABresult
0X0
100
111
1136 10 ▶ return 0 unless ($sig->version // '') =~ /\A[0-9]+\z/ && $sig->version == $top_m;
Branch: 1 branch
BranchTrueFalseTotal
unless ($sig->version // "") =~ /\A[0-9]+\z/ and $sig->version == $top_m 0 10 10
Condition40%2/5
($sig->version // "") =~ /\A[0-9]+\z/ and $sig->version == $top_m
!ll&&!rl&&r
0010
$sig->version // ""
l!l
100
MC/DC0%0/2
($sig->version // "") =~ /\A[0-9]+\z/ and $sig->version == $top_m
$sig->version $sig->version == $top_m
Truth table33%1/3
A and B
A= $sig->versionB= $sig->version == $top_m
ABresult
0X0
100
111
1137  
1138 10 local @{$self}{qw(result details diagnostic)};
1139 # -09 section 13.2 steps 2 and 5: the top signature's s= passes, and its
1140 # d= relaxed-matches its mf= unless that is null. Timestamps are not a
1141 # step: a message too old to be Fully Verified may still be reported on.
1142 10 my $sig_ok = eval {
1143 10 ▶ $self->_check_sig_field($max_i) && $self->_check_sig_items($max_i);
Condition33%1/3
$self->_check_sig_field($max_i) && $self->_check_sig_items($max_i)
!ll&&!rl&&r
0010
MC/DC0%0/2
$self->_check_sig_field($max_i) && $self->_check_sig_items($max_i)
$self->_check_sig_field($max_i) $self->_check_sig_items($max_i)
Truth table33%1/3
A && B
A= $self->_check_sig_field($max_i)B= $self->_check_sig_items($max_i)
ABresult
0X0
100
111
1144 };
1145 10 ▶ die $@ if ref $@;
Branch: 1 branch
BranchTrueFalseTotal
if ref $@ 0 10 10
1146 10 ▶ return 0 unless $sig_ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $sig_ok 0 10 10
1147 # Step 4, for a caller that gave the envelope.
1148 10 ▶ return 0 unless $self->_verify_envelope($sig, $max_i);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_verify_envelope($sig, $max_i) 2 8 10
1149 8 my $mf = $sig->mail_from;
1150 8 ▶ if (defined $mf && $mf !~ /\A<\s*>\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mf and not $mf =~ /\A<\s*>\z/) 6 2 8
Condition66%2/3
defined $mf and not $mf =~ /\A<\s*>\z/
!ll&&!rl&&r
026
MC/DC50%1/2
defined $mf and not $mf =~ /\A<\s*>\z/
defined $mf not $mf =~ /\A<\s*>\z/
Truth table66%2/3
A and not(B)
A= defined $mfB= not $mf =~ /\A<\s*>\z/
ABresult
0X0
100
111
1151 6 my ($mfd, $lit) = domain_of($mf);
1152 6 ▶ return 0 unless $mfd && !$lit && relaxed_domain_match($mfd, $sig->domain // '');
Branch: 1 branch
BranchTrueFalseTotal
unless $mfd and not $lit and relaxed_domain_match($mfd, $sig->domain // "") 1 5 6
Condition50%4/8
$mfd and not $lit and relaxed_domain_match($mfd, $sig->domain // "")
!ll&&!rl&&r
015
$mfd and not $lit
!ll&&!rl&&r
006
$sig->domain // ""
l!l
60
MC/DC33%1/3
$mfd and not $lit and relaxed_domain_match($mfd, $sig->domain // "")
$mfd not $lit relaxed_domain_match($mfd, $sig->domain // "")
MC/DC0%0/1
$sig->domain // ""
$sig->domain
Truth table50%2/4
A and not(B) and C
A= $mfdB= not $litC= relaxed_domain_match($mfd, $sig->domain // "")
ABCresult
0XX0
10X0
1100
1111
Truth table50%1/2
A
A= $sig->domain
Aresult
11
00
1153 }
1154  
1155 7 ▶ my $obj = $self->{_chain}->instance($top_m) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $obj = $self->{"_chain"}->instance($top_m) or return 0 7 0 7
1156  
1157 # The plans' hashes: none were made if the header fields alone decided
1158 # the result.
1159 7 ▶ my $hplan = $self->{_header_plan} or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $hplan = $self->{"_header_plan"} or return 0 7 0 7
1160 7 ▶ my $header_cb = sub { $hplan->header_hash($top_m, $_[0]) // '' };
Subroutine __ANON__: called CC=2 SCAR=7.2
Condition50%1/2
$hplan->header_hash($top_m, $_[0]) // ""
l!l
70
MC/DC0%0/1
$hplan->header_hash($top_m, $_[0]) // ""
$hplan->header_hash($top_m, $_[0])
Truth table50%1/2
A
A= $hplan->header_hash($top_m, $_[0])
Aresult
11
00
1161 7 my $body_cb;
1162 7 ▶ if (my $plan = $self->{_body_plan}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $plan = $self->{"_body_plan"}) 7 0 7
1163 7 $plan->finish;
1164 7 ▶ $body_cb = sub { $plan->body_hash($top_m, $_[0]) // '' };
Subroutine __ANON__: called CC=2 SCAR=7.2
Condition50%1/2
$plan->body_hash($top_m, $_[0]) // ""
l!l
50
MC/DC0%0/1
$plan->body_hash($top_m, $_[0]) // ""
$plan->body_hash($top_m, $_[0])
Truth table50%1/2
A
A= $plan->body_hash($top_m, $_[0])
Aresult
11
00
1165 }
1166 7 my ($ok) = $obj->_check_hashes($top_m, $header_cb, $body_cb);
1167 7 ▶ return $ok ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 5 2 7
1168 }
1169  
1170 # More about the reason, for a log, where there is more: the resolver's
1171 # error behind a key that could not be fetched, the copy range behind a
1172 # Recipe that does not undo. Never part of the -08 string in details().
1173 56 ▶sub diagnostic {
Subroutine diagnostic: called CC=1 SCAR=0.0
Pod documented
1174 56 my $self = shift;
1175 56 return $self->{diagnostic};
1176 }
1177  
1178 192 1;
1179  
1180 __END__
« lib/Mail/DKIM2/HeaderPlan.pm ↑ Summary lib/Mail/DKIM2/Hop.pm »