lib/Mail/DKIM2/Verifier.pm

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