DKIM/ARC/Verifier.pm

filter: Statementstmt 89.2% 299 / 335 Branchbran 79.0% 117 / 148 Conditioncond 74.3% 61 / 82 MC/DCmcdc 55.3% 26 / 47 Subroutinesub 94.4% 17 / 18 Podpod 22.2% 2 / 9 totaltotal 81.6% 522 / 639 CC 113 SCAR 29.6
CC
113
Cov
84%
add_signature
27.3
finish_body
27.2
check_signature
23.0
CRAP 19.3
« DKIM/Common.pm ↑ Summary DKIM/Policy.pm »
1 package Mail::DKIM::ARC::Verifier;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 our $VERSION = '1.20261116'; # VERSION
5 # ABSTRACT: verifies an ARC-Sealed message
6  
7 # Copyright 2017 FastMail Pty Ltd. All Rights Reserved.
8 # Bron Gondwana <brong@fastmailteam.com>
9  
10 # This program is free software; you can redistribute it and/or
11 # modify it under the same terms as Perl itself.
12  
13  
14  
15 5 ▶use base 'Mail::DKIM::Common';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use Mail::DKIM::ARC::MessageSignature;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use Mail::DKIM::ARC::Seal;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use Mail::DKIM::Canonicalization::seal;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use Mail::Address;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 our $MAX_SIGNATURES_TO_PROCESS = 50;
22  
23 193 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
24 193 my $self = shift;
25 193 $self->SUPER::init;
26 193 $self->{signatures} = [];
27 193 $self->{result} = undef; # we're done once this is set
28 }
29  
30 # @{$arc->{signatures}}
31 # array of L<Mail::DKIM::ARC::{Signature|Seal}> objects, representing all
32 # parseable message signatures and seals found in the header,
33 # ordered from the top of the header to the bottom.
34 #
35 # $arc->{signature_reject_reason}
36 # simple string listing a reason, if any, for not using a signature.
37 # This may be a helpful diagnostic if there is a signature in the header,
38 # but was found not to be valid. It will be ambiguous if there are more
39 # than one signatures that could not be used.
40 #
41 # @{$arc->{headers}}
42 # array of strings, each member is one header, in its original format.
43 #
44 # $arc->{algorithms}
45 # array of algorithms, one for each signature being verified.
46 #
47 # $arc->{result}
48 # string; the result of the verification (see the result() method)
49 #
50  
51 2722 ▶sub handle_header {
Subroutine handle_header: called CC=5 SCAR=16.1
Pod undocumented
52 2722 my $self = shift;
53 2722 my ( $field_name, $contents, $line ) = @_;
54  
55 2722 $self->SUPER::handle_header( $field_name, $contents );
56  
57 2722 ▶ if ( lc($field_name) eq 'arc-message-signature' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $field_name eq "arc-message-signature") 272 2450 2722
58 eval {
59 272 local $SIG{__DIE__};
60 272 my $signature = Mail::DKIM::ARC::MessageSignature->parse($line);
61 270 $self->add_signature($signature);
62 269 1
63 272 ▶ } || do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { local $SIG{"__DIE__"}; my $signature = "Mail::DKIM::ARC::MessageSignature"->parse($line); $self->add_signature($signature); 1 } } or do { chomp(my $E = $@); $self->{"signature_reject_reason"} = $E } 269 3 272
64  
65 # the only reason an error should be thrown is if the
66 # signature really is unparse-able
67  
68 # otherwise, invalid signatures are caught in finish_header()
69  
70 3 chomp( my $E = $@ );
71 3 $self->{signature_reject_reason} = $E;
72 };
73 }
74  
75 2722 ▶ if ( lc($field_name) eq 'arc-seal' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $field_name eq "arc-seal") 274 2448 2722
76 eval {
77 274 local $SIG{__DIE__};
78 274 my $signature = Mail::DKIM::ARC::Seal->parse($line);
79 273 $self->add_signature($signature);
80 272 1
81 274 ▶ } || do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { local $SIG{"__DIE__"}; my $signature = "Mail::DKIM::ARC::Seal"->parse($line); $self->add_signature($signature); 1 } } or do { chomp(my $E = $@); $self->{"signature_reject_reason"} = $E } 272 2 274
82  
83 # the only reason an error should be thrown is if the
84 # signature really is unparse-able
85  
86 # otherwise, invalid signatures are caught in finish_header()
87  
88 2 chomp( my $E = $@ );
89 2 $self->{signature_reject_reason} = $E;
90 };
91 }
92  
93 }
94  
95 543 ▶sub add_signature {
Subroutine add_signature: called CC=26 SCAR=33.1
Pod undocumented
96 543 my ( $self, $signature ) = @_;
97 543 ▶ croak 'wrong number of arguments' unless ( @_ == 2 );
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 0 543 543
98  
99 543 ▶ return if $self->{result}; # already failed
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"result"} 12 531 543
100  
101 # Set verification time if we have one
102 531 ▶ if ($self->{verify_time}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"verify_time"}) 0 531 531
103 0 $signature->set_verify_time($self->{verify_time});
104 }
105  
106 531 push @{ $self->{signatures} }, $signature;
107  
108 531 ▶ unless ( $self->check_signature($signature) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->check_signature($signature)) 14 517 531
109 14 $signature->result( 'invalid', $self->{signature_reject_reason} );
110 14 return;
111 }
112  
113 # check for bogus tags (should be done much earlier but better late than never)
114 # tagkeys is uniq'd via a hash, rawtaglen counts all the tags
115 517 my @tagkeys = keys %{ $signature->{tags_by_name} };
116 517 my $rawtaglen = $#{ $signature->{tags} };
117  
118 # crock: ignore empty clause after trailing semicolon
119 $rawtaglen--
120 517 ▶ if $signature->{tags}->[ $#{ $signature->{tags} } ]->{raw} =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $signature->{"tags"}[$#{$$signature{"tags"};}]{"raw"} =~ /^\s*$/ 2 515 517
121  
122 # duplicate tags
123 517 ▶ if ( $rawtaglen != $#tagkeys ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rawtaglen != $#tagkeys) 5 512 517
124 5 $self->{result} = 'fail'; # bogus
125 5 $self->{details} = 'Duplicate tag in signature';
126 5 return;
127 }
128  
129 # invalid tag name
130 512 ▶ if ( grep { !m{[a-z][a-z0-9_]*}i } @tagkeys ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {not /[a-z][a-z0-9_]*/i;} @tagkeys) 2 510 512
131 2 $self->{result} = 'fail'; # bogus
132 2 $self->{details} = 'Invalid tag in signature';
133 2 return;
134 }
135  
136 # the instance this signature will be saved at, once it has been set up
137 510 my ( $set, $instance );
138  
139 510 ▶ if ( $signature->isa('Mail::DKIM::ARC::Seal') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($signature->isa('Mail::DKIM::ARC::Seal')) { } 261 249 510
elsif ($signature->isa('Mail::DKIM::ARC::MessageSignature')) { } 249 0 249
140 261 ▶ $instance = $signature->instance() || '';
Condition100%2/2
$signature->instance || ''
l!l
2574
MC/DC100%1/1
$signature->instance || ''
$signature->instance
Truth table100%2/2
A
A= $signature->instance
Aresult
11
00
141  
142 261 ▶ if ( $instance !~ m{^\d+$} or $instance < 1 or $instance > $MAX_SIGNATURES_TO_PROCESS ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $instance =~ /^\d+$/ or $instance < 1 or $instance > $Mail::DKIM::ARC::Verifier::MAX_SIGNATURES_TO_PROCESS) 6 255 261
Condition83%5/6
not $instance =~ /^\d+$/ or $instance < 1 or $instance > $Mail::DKIM::ARC::Verifier::MAX_SIGNATURES_TO_PROCESS
l!l&&r!l&&!r
51255
not $instance =~ /^\d+$/ or $instance < 1
l!l&&r!l&&!r
50256
MC/DC66%2/3
not $instance =~ /^\d+$/ or $instance < 1 or $instance > $Mail::DKIM::ARC::Verifier::MAX_SIGNATURES_TO_PROCESS
not $instance =~ /^\d+$/ $instance < 1 $instance > $Mail::DKIM::ARC::Verifier::MAX_SIGNATURES_TO_PROCESS
Truth table75%3/4
not(A) or B or C
A= not $instance =~ /^\d+$/B= $instance < 1C= $instance > $Mail::DKIM::ARC::Verifier::MAX_SIGNATURES_TO_PROCESS
ABCresult
1XX1
01X1
0011
0000
143 6 $self->{result} = 'fail'; # bogus
144 6 $self->{details} = sprintf "Invalid ARC-Seal instance '%s'",
145 $instance;
146 6 return;
147 }
148  
149 255 ▶ if ( $self->{seals}[$instance] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"seals"}[$instance]) 3 252 255
150 3 $self->{result} = 'fail'; # dup
151 3 ▶ if ( $signature eq $self->{seals}[$instance] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($signature eq $self->{'seals'}[$instance]) { } 0 3 3
152 0 $self->{details} = sprintf 'Duplicate ARC-Seal %d', $instance;
153 }
154 else {
155 3 $self->{details} = sprintf 'Redundant ARC-Seal %d', $instance;
156 }
157 3 return;
158 }
159  
160 252 $set = 'seals';
161 }
162 elsif ( $signature->isa('Mail::DKIM::ARC::MessageSignature') ) {
163 249 ▶ $instance = $signature->instance() || '';
Condition100%2/2
$signature->instance || ''
l!l
2463
MC/DC100%1/1
$signature->instance || ''
$signature->instance
Truth table100%2/2
A
A= $signature->instance
Aresult
11
00
164  
165 249 ▶ if ( $instance !~ m{^\d+$} or $instance < 1 or $instance > $MAX_SIGNATURES_TO_PROCESS ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $instance =~ /^\d+$/ or $instance < 1 or $instance > $Mail::DKIM::ARC::Verifier::MAX_SIGNATURES_TO_PROCESS) 4 245 249
Condition66%4/6
not $instance =~ /^\d+$/ or $instance < 1 or $instance > $Mail::DKIM::ARC::Verifier::MAX_SIGNATURES_TO_PROCESS
l!l&&r!l&&!r
40245
not $instance =~ /^\d+$/ or $instance < 1
l!l&&r!l&&!r
40245
MC/DC33%1/3
not $instance =~ /^\d+$/ or $instance < 1 or $instance > $Mail::DKIM::ARC::Verifier::MAX_SIGNATURES_TO_PROCESS
not $instance =~ /^\d+$/ $instance < 1 $instance > $Mail::DKIM::ARC::Verifier::MAX_SIGNATURES_TO_PROCESS
Truth table50%2/4
not(A) or B or C
A= not $instance =~ /^\d+$/B= $instance < 1C= $instance > $Mail::DKIM::ARC::Verifier::MAX_SIGNATURES_TO_PROCESS
ABCresult
1XX1
01X1
0011
0000
166 4 $self->{result} = 'fail'; # bogus
167 $self->{details} =
168 4 sprintf "Invalid ARC-Message-Signature instance '%s'", $instance;
169 4 return;
170 }
171  
172 245 ▶ if ( $self->{messages}[$instance] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"messages"}[$instance]) 3 242 245
173 3 $self->{result} = 'fail'; # dup
174 3 ▶ if ( $signature->as_string() eq
Branch: 1 branch
BranchTrueFalseTotal
if ($signature->as_string eq $self->{'messages'}[$instance]->as_string) { } 0 3 3
175 $self->{messages}[$instance]->as_string() )
176 {
177 0 $self->{details} = sprintf 'Duplicate ARC-Message-Signature %d',
178 $instance;
179 }
180 else {
181 3 $self->{details} = sprintf 'Redundant ARC-Message-Signature %d',
182 $instance;
183 }
184 3 return;
185 }
186 242 $set = 'messages';
187 }
188  
189 # signature looks ok, go ahead and query for the public key
190 # (only now that the tags and instance number have been checked, so a
191 # bogus signature costs neither a DNS query nor a canonicalization)
192 494 $signature->fetch_public_key;
193  
194 # create a canonicalization filter and algorithm
195 492 my $algorithm_class =
196 $signature->get_algorithm_class( $signature->algorithm );
197 my $algorithm = $algorithm_class->new(
198 Signature => $signature,
199 Debug_Canonicalization => $signature->isa('Mail::DKIM::ARC::Seal')
200 ? $self->{AS_Canonicalization}
201 : $self->{AMS_Canonicalization},
202 492 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$signature->isa('Mail::DKIM::ARC::Seal') ? : 251 241 492
203  
204 # an algorithm that wants the headers prior to the signature header
205 # canonicalizes the whole header from finish_header(), so only the
206 # others need each header field as it is parsed
207 0 ▶ push @{ $self->{header_algorithms} }, $algorithm
Branch: 1 branch
BranchTrueFalseTotal
unless $algorithm->wants_pre_signature_headers 0 492 492
208 unless $algorithm->wants_pre_signature_headers;
209  
210 # save the algorithm
211 492 ▶ $self->{algorithms} ||= [];
Condition100%2/2
$self->{"algorithms"} ||= []
l!l
315177
MC/DC100%1/1
$self->{"algorithms"} ||= []
$self->{"algorithms"}
Truth table100%2/2
A
A= $self->{"algorithms"}
Aresult
11
00
212 492 push @{ $self->{algorithms} }, $algorithm;
213  
214 # fetch_public_key dies on an unusable selector or domain, so only now
215 # save the signature for the chain checks
216 492 ▶ $self->{$set}[$instance] = $signature if $set;
Branch: 1 branch
BranchTrueFalseTotal
if $set 492 0 492
217 }
218  
219 531 ▶sub check_signature {
Subroutine check_signature: called CC=17 SCAR=31.4
Pod undocumented
220 531 my $self = shift;
221 531 ▶ croak 'wrong number of arguments' unless ( @_ == 1 );
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 531 531
222 531 my ($signature) = @_;
223  
224 531 ▶ unless ( $signature->check_version ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($signature->check_version) 0 531 531
225  
226 # unsupported version
227 0 ▶ if ( defined $signature->version ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $signature->version) { } 0 0 0
228 $self->{signature_reject_reason} =
229 0 'unsupported version ' . $signature->version;
230 }
231 else {
232 0 $self->{signature_reject_reason} = 'missing v tag';
233 }
234 0 return 0;
235 }
236  
237 531 ▶ unless ( $signature->algorithm
Branch: 1 branch
BranchTrueFalseTotal
unless ($signature->algorithm and $signature->get_algorithm_class($signature->algorithm) and !$self->{"Strict"} || $signature->algorithm ne "rsa-sha1") 6 525 531
Condition77%7/9
$signature->algorithm and $signature->get_algorithm_class($signature->algorithm) and !$self->{"Strict"} || $signature->algorithm ne "rsa-sha1"
!ll&&!rl&&r
60525
$signature->algorithm and $signature->get_algorithm_class($signature->algorithm)
!ll&&!rl&&r
42525
not $self->{"Strict"} or $signature->algorithm ne "rsa-sha1"
l!l&&r!l&&!r
1673580
MC/DC50%2/4
$signature->algorithm and $signature->get_algorithm_class($signature->algorithm) and !$self->{"Strict"} || $signature->algorithm ne "rsa-sha1"
$signature->algorithm $signature->get_algorithm_class($signature->algorithm) not $self->{"Strict"} $signature->algorithm ne "rsa-sha1"
Truth table80%4/5
A and B and not(C) or D
A= $signature->algorithmB= $signature->get_algorithm_class($signature->algorithm)C= not $self->{"Strict"}D= $signature->algorithm ne "rsa-sha1"
ABCDresult
0XXX0
10XX0
11000
111X1
11011
238 && $signature->get_algorithm_class( $signature->algorithm )
239 && ( !$self->{Strict} || $signature->algorithm ne 'rsa-sha1' )
240 ) # no more SHA1 for us in strict mode
241 {
242 # unsupported algorithm
243 6 $self->{signature_reject_reason} = 'unsupported algorithm';
244 6 ▶ if ( defined $signature->algorithm ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $signature->algorithm) 4 2 6
245 4 $self->{signature_reject_reason} .= ' ' . $signature->algorithm;
246 }
247 6 return 0;
248 }
249  
250 525 ▶ unless ( $signature->check_canonicalization ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($signature->check_canonicalization) 1 524 525
251  
252 # unsupported canonicalization method
253 1 $self->{signature_reject_reason} = 'unsupported canonicalization';
254 1 ▶ if ( defined $signature->canonicalization ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $signature->canonicalization) 1 0 1
255 $self->{signature_reject_reason} .=
256 1 ' ' . $signature->canonicalization;
257 }
258 1 return 0;
259 }
260  
261 524 ▶ unless ( $signature->check_protocol ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($signature->check_protocol) 0 524 524
262  
263 # unsupported query protocol
264 $self->{signature_reject_reason} =
265 0 ▶ !defined( $signature->protocol )
Branch: 1 branch
BranchTrueFalseTotal
!defined($signature->protocol) ? : 0 0 0
266 ? 'missing q tag'
267 : 'unsupported query protocol, q=' . $signature->protocol;
268 0 return 0;
269 }
270  
271 524 ▶ unless ( $signature->check_expiration ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($signature->check_expiration) 0 524 524
272  
273 # signature has expired
274 0 $self->{signature_reject_reason} = 'signature is expired';
275 0 return 0;
276 }
277  
278 524 ▶ unless ( defined $signature->domain ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $signature->domain) 2 522 524
279  
280 # no domain specified
281 2 $self->{signature_reject_reason} = 'missing d tag';
282 2 return 0;
283 }
284  
285 522 ▶ if ( $signature->domain eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($signature->domain eq "") 2 520 522
286  
287 # blank domain
288 2 $self->{signature_reject_reason} = 'invalid domain in d tag';
289 2 return 0;
290 }
291  
292 520 ▶ unless ( defined $signature->selector ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $signature->selector) 3 517 520
293  
294 # no selector specified
295 3 $self->{signature_reject_reason} = 'missing s tag';
296 3 return 0;
297 }
298  
299 517 return 1;
300 }
301  
302 285 ▶sub check_public_key {
Subroutine check_public_key: called CC=5 SCAR=17.8
Pod undocumented
303 285 my $self = shift;
304 285 ▶ croak 'wrong number of arguments' unless ( @_ == 2 );
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 0 285 285
305 285 my ( $signature, $public_key ) = @_;
306  
307 285 my $result = 0;
308 eval {
309 285 local $SIG{__DIE__};
310 285 $@ = undef;
311  
312 # HACK- I'm indecisive here about whether I want the
313 # check_foo functions to return false or to "die"
314 # on failure
315  
316 # check public key's allowed hash algorithms
317 285 $result =
318 $public_key->check_hash_algorithm( $signature->hash_algorithm );
319  
320 # HACK- DomainKeys signatures are allowed to have an empty g=
321 # tag in the public key
322 # my $empty_g_means_wildcard = $signature->isa('Mail::DKIM::DkSignature');
323  
324 # check public key's granularity
325 285 ▶ $result &&= $public_key->check_granularity( $signature->domain, 0 );
Condition33%1/3
$result &&= $public_key->check_granularity($signature->domain, 0)
!ll&&!rl&&r
00285
MC/DC0%0/2
$result &&= $public_key->check_granularity($signature->domain, 0)
$result $public_key->check_granularity($signature->domain, 0)
Truth table33%1/3
A &&= B
A= $resultB= $public_key->check_granularity($signature->domain, 0)
ABresult
0X0
100
111
326  
327 # $signature->instance, $empty_g_means_wildcard);
328  
329 285 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 285 285
330 285 1
331 285 ▶ } || do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { local $SIG{"__DIE__"}; $@ = undef; $result = $public_key->check_hash_algorithm($signature->hash_algorithm); $result &&= $public_key->check_granularity($signature->domain, 0); die $@ if $@; 1 } } or do { my $E = $@; chomp $E; $self->{"signature_reject_reason"} = "public key: $E" } 285 0 285
332 0 my $E = $@;
333 0 chomp $E;
334 0 $self->{signature_reject_reason} = "public key: $E";
335 };
336 285 return $result;
337 }
338  
339 #
340 # called when the verifier has received the last of the message headers
341 # (body is still to come)
342 #
343 192 ▶sub finish_header {
Subroutine finish_header: called CC=16 SCAR=29.0
Pod undocumented
344 192 my $self = shift;
345  
346 # Signatures we found and were successfully parsed are stored in
347 # $self->{signatures}. If none were found, our result is "none".
348  
349 192 ▶ if ( @{ $self->{signatures} } == 0
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"signatures"};} == 0 and not defined $self->{"signature_reject_reason"}) 5 187 192
Condition66%2/3
@{$self->{"signatures"};} == 0 and not defined $self->{"signature_reject_reason"}
!ll&&!rl&&r
18705
MC/DC50%1/2
@{$self->{"signatures"};} == 0 and not defined $self->{"signature_reject_reason"}
@{$self->{"signatures"};} == 0 not defined $self->{"signature_reject_reason"}
Truth table66%2/3
A and not(B)
A= @{$self->{"signatures"};} == 0B= not defined $self->{"signature_reject_reason"}
ABresult
0X0
100
111
350 && !defined( $self->{signature_reject_reason} ) )
351 {
352 5 $self->{result} = 'none';
353 5 return;
354 }
355  
356 # already failed in add_signature: drop the algorithms, since a seal
357 # canonicalization loops up to its signature's i= value, which may be
358 # the bad instance number that failed the chain
359 187 ▶ if ( $self->{result} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"result"}) 23 164 187
360 23 $self->{algorithms} = [];
361 23 return;
362 }
363  
364 # check the AAR headers: each must have an instance the seal
365 # canonicalization finds, since one it doesn't is covered by no seal
366 # but may still be read as part of the chain (e.g. "i = 1", which
367 # RFC 8617 allows), and there must be no duplicates (dup AS and AMS
368 # checked in add_signature)
369 164 my %aars;
370 164 foreach my $hdr ( @{ $self->{headers} } ) {
371 2218 ▶ next unless $hdr =~ m{^ARC-Authentication-Results\s*:}i;
Branch: 1 branch
BranchTrueFalseTotal
unless $hdr =~ /^ARC-Authentication-Results\s*:/i 1977 241 2218
372 241 my $i = Mail::DKIM::Canonicalization::seal::_aar_instance($hdr);
373 241 ▶ if ( !defined $i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $i) 14 227 241
374 14 $self->{result} = 'fail';
375 14 $self->{details} = 'Unparseable ARC-Authentication-Results header';
376 14 return;
377 }
378 227 ▶ if ( $i < 1 or $i > $MAX_SIGNATURES_TO_PROCESS ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 1 or $i > $Mail::DKIM::ARC::Verifier::MAX_SIGNATURES_TO_PROCESS) 4 223 227
Condition100%3/3
$i < 1 or $i > $Mail::DKIM::ARC::Verifier::MAX_SIGNATURES_TO_PROCESS
l!l&&r!l&&!r
31223
MC/DC100%2/2
$i < 1 or $i > $Mail::DKIM::ARC::Verifier::MAX_SIGNATURES_TO_PROCESS
$i < 1 $i > $Mail::DKIM::ARC::Verifier::MAX_SIGNATURES_TO_PROCESS
Truth table100%3/3
A or B
A= $i < 1B= $i > $Mail::DKIM::ARC::Verifier::MAX_SIGNATURES_TO_PROCESS
ABresult
1X1
011
000
379 4 $self->{result} = 'fail';
380 $self->{details} =
381 4 "Invalid ARC-Authentication-Results instance '$i'";
382 4 return;
383 }
384 223 ▶ if ( defined $aars{$i} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $aars{$i}) 4 219 223
385 4 $self->{result} = 'fail';
386 $self->{details} =
387 4 "Duplicate ARC-Authentication-Results header $i";
388 4 return;
389 }
390 219 $aars{$i} = $hdr;
391 }
392 142 $self->{aars} = \%aars;
393  
394 # share the header indexes between the signatures
395 142 my $index = {};
396 142 foreach my $algorithm ( @{ $self->{algorithms} } ) {
397 $algorithm->finish_header(
398 Headers => $self->{headers},
399 409 HeaderIndex => $index,
400 Chain => 'pass'
401 );
402 }
403  
404 # stop processing signatures that are already known to be invalid
405 142 @{ $self->{algorithms} } = grep {
406 409 my $sig = $_->signature;
407 409 ▶ !( $sig->result && $sig->result eq 'invalid' );
Condition33%1/3
$sig->result && $sig->result eq 'invalid'
!ll&&!rl&&r
40900
MC/DC0%0/2
$sig->result && $sig->result eq 'invalid'
$sig->result $sig->result eq 'invalid'
Truth table33%1/3
A && B
A= $sig->resultB= $sig->result eq 'invalid'
ABresult
0X0
100
111
408 142 } @{ $self->{algorithms} };
409  
410 142 ▶ if ( @{ $self->{algorithms} } == 0
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"algorithms"};} == 0 and @{$self->{"signatures"};} > 0) 0 142 142
Condition33%1/3
@{$self->{"algorithms"};} == 0 and @{$self->{"signatures"};} > 0
!ll&&!rl&&r
14200
MC/DC0%0/2
@{$self->{"algorithms"};} == 0 and @{$self->{"signatures"};} > 0
@{$self->{"algorithms"};} == 0 @{$self->{"signatures"};} > 0
Truth table33%1/3
A and B
A= @{$self->{"algorithms"};} == 0B= @{$self->{"signatures"};} > 0
ABresult
0X0
100
111
411 0 && @{ $self->{signatures} } > 0 )
412 {
413 0 ▶ $self->{result} = $self->{signatures}->[0]->result || 'invalid';
Condition0%0/2
$self->{'signatures'}[0]->result || 'invalid'
l!l
00
MC/DC0%0/1
$self->{'signatures'}[0]->result || 'invalid'
$self->{'signatures'}[0]->result
Truth table0%0/2
A
A= $self->{'signatures'}[0]->result
Aresult
11
00
414 $self->{details} = $self->{signatures}->[0]->{verify_details}
415 0 ▶ || $self->{signature_reject_reason};
Condition0%0/3
$self->{'signatures'}[0]{'verify_details'} || $self->{'signature_reject_reason'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'signatures'}[0]{'verify_details'} || $self->{'signature_reject_reason'}
$self->{'signatures'}[0]{'verify_details'} $self->{'signature_reject_reason'}
Truth table0%0/3
A || B
A= $self->{'signatures'}[0]{'verify_details'}B= $self->{'signature_reject_reason'}
ABresult
1X1
011
000
416 0 return;
417 }
418 }
419  
420 409 ▶sub _check_and_verify_signature {
Subroutine _check_and_verify_signature: called CC=16 SCAR=28.1
421 409 my $self = shift;
422 409 my ($algorithm) = @_;
423  
424 # check signature
425 409 my $signature = $algorithm->signature;
426  
427 409 ▶ if ( not $signature->get_tag('d') ) { # All sigs must have a D tag
Branch: 1 branch
BranchTrueFalseTotal
unless ($signature->get_tag("d")) 0 409 409
428 0 $self->{signature_reject_reason} = 'missing D tag';
429 0 return ( 'fail', $self->{signature_reject_reason} );
430 }
431  
432 409 ▶ if ( not $signature->get_tag('b') ) { # All sigs must have a B tag
Branch: 1 branch
BranchTrueFalseTotal
unless ($signature->get_tag("b")) 4 405 409
433 4 $self->{signature_reject_reason} = 'missing B tag';
434 4 return ( 'fail', $self->{signature_reject_reason} );
435 }
436  
437 405 ▶ if ( not $signature->isa('Mail::DKIM::ARC::Seal') ) { # AMS tests
Branch: 1 branch
BranchTrueFalseTotal
unless ($signature->isa("Mail::DKIM::ARC::Seal")) 202 203 405
438 202 ▶ unless ( $signature->get_tag('bh') ) { # AMS must have a BH tag
Branch: 1 branch
BranchTrueFalseTotal
unless ($signature->get_tag("bh")) 2 200 202
439 2 $self->{signature_reject_reason} = 'missing BH tag';
440 2 return ( 'fail', $self->{signature_reject_reason} );
441 }
442  
443 # an empty or missing h= is accepted, unlike in a DKIM-Signature,
444 # as the ARC test suite expects; see "ARC-Message-Signature h= tag"
445 200 ▶ if ( ( $signature->get_tag('h') || '' ) =~ /arc-seal/i )
Branch: 1 branch
BranchTrueFalseTotal
if (($signature->get_tag("h") || "") =~ /arc-seal/i) 1 199 200
Condition100%2/2
$signature->get_tag('h') || ''
l!l
1973
MC/DC100%1/1
$signature->get_tag('h') || ''
$signature->get_tag('h')
Truth table100%2/2
A
A= $signature->get_tag('h')
Aresult
11
00
446 { # cannot cover AS
447 $self->{signature_reject_reason} =
448 1 'Arc-Message-Signature covers Arc-Seal';
449 1 return ( 'fail', $self->{signature_reject_reason} );
450 }
451 }
452  
453 # AMS signature must not
454  
455 # get public key
456 402 my $pkey;
457 eval {
458 402 local $SIG{__DIE__};
459 402 $pkey = $signature->get_public_key;
460 285 1
461 402 ▶ } || do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { local $SIG{"__DIE__"}; $pkey = $signature->get_public_key; 1 } } or do { my $E = $@; chomp $E; $self->{"signature_reject_reason"} = "public key: $E"; return "invalid", $self->{"signature_reject_reason"} } 285 117 402
462 117 my $E = $@;
463 117 chomp $E;
464 117 $self->{signature_reject_reason} = "public key: $E";
465 117 return ( 'invalid', $self->{signature_reject_reason} );
466 };
467  
468 285 ▶ unless ( $self->check_public_key( $signature, $pkey ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->check_public_key($signature, $pkey)) 0 285 285
469 0 return ( 'invalid', $self->{signature_reject_reason} );
470 }
471  
472 # make sure key is big enough
473 285 my $keysize = $pkey->cork->size * 8; # in bits
474 285 ▶ if ( $keysize < 1024 && $self->{Strict} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keysize < 1024 and $self->{"Strict"}) 2 283 285
Condition66%2/3
$keysize < 1024 and $self->{"Strict"}
!ll&&!rl&&r
28302
MC/DC50%1/2
$keysize < 1024 and $self->{"Strict"}
$keysize < 1024 $self->{"Strict"}
Truth table66%2/3
A and B
A= $keysize < 1024B= $self->{"Strict"}
ABresult
0X0
100
111
475 2 $self->{signature_reject_reason} = "Key length $keysize too short";
476 2 return ( 'fail', $self->{signature_reject_reason} );
477 }
478  
479 # verify signature
480 283 my $result;
481 my $details;
482 283 local $@ = undef;
483 eval {
484 283 local $SIG{__DIE__};
485 283 ▶ $result = $algorithm->verify() ? 'pass' : 'fail';
Branch: 1 branch
BranchTrueFalseTotal
$algorithm->verify ? : 228 49 277
486 277 ▶ $details = $algorithm->{verification_details} || $@;
Condition100%3/3
$algorithm->{'verification_details'} || $@
l!l&&r!l&&!r
445228
MC/DC100%2/2
$algorithm->{'verification_details'} || $@
$algorithm->{'verification_details'} $@
Truth table100%3/3
A || B
A= $algorithm->{'verification_details'}B= $@
ABresult
1X1
011
000
487 277 1
488 283 ▶ } || do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { local $SIG{"__DIE__"}; $result = $algorithm->verify ? "pass" : "fail"; $details = $algorithm->{"verification_details"} || $@; 1 } } or do { chomp(my $E = $@); if ($E =~ /(OpenSSL error: .*?) at /) { $E = $1; } elsif ($E =~ /^(panic:.*?) at /) { $E = "OpenSSL $1"; } ; $result = "fail"; $details = $E } 277 6 283
489  
490 # see also add_signature
491 6 chomp( my $E = $@ );
492 6 ▶ if ( $E =~ /(OpenSSL error: .*?) at / ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($E =~ /(OpenSSL error: .*?) at /) { } 6 0 6
elsif ($E =~ /^(panic:.*?) at /) { } 0 0 0
493 6 $E = $1;
494 }
495 elsif ( $E =~ /^(panic:.*?) at / ) {
496 0 $E = "OpenSSL $1";
497 }
498 6 $result = 'fail';
499 6 $details = $E;
500 };
501 283 return ( $result, $details );
502 }
503  
504 193 ▶sub finish_body {
Subroutine finish_body: called CC=26 SCAR=33.0
Pod undocumented
505 193 my $self = shift;
506  
507 193 ▶ return if $self->{result}; # already failed
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"result"} 50 143 193
508  
509 143 foreach my $algorithm ( @{ $self->{algorithms} } ) {
510  
511 # finish canonicalizing
512 409 $algorithm->finish_body;
513  
514 409 my ( $result, $details ) =
515 $self->_check_and_verify_signature($algorithm);
516  
517 # save the results of this signature verification
518 409 $algorithm->{result} = $result;
519 409 $algorithm->{details} = $details;
520 409 ▶ $self->{signature} ||= $algorithm->signature; # something if we fail
Condition66%2/3
$self->{"signature"} ||= $algorithm->signature
l!l&&r!l&&!r
2671420
MC/DC0%0/2
$self->{"signature"} ||= $algorithm->signature
$self->{"signature"} $algorithm->signature
Truth table66%2/3
A ||= B
A= $self->{"signature"}B= $algorithm->signature
ABresult
1X1
011
000
521 409 $algorithm->signature->result( $result, $details );
522 }
523  
524 143 ▶ my $seals = $self->{seals} || [];
Condition100%2/2
$self->{'seals'} || []
l!l
1349
MC/DC100%1/1
$self->{'seals'} || []
$self->{'seals'}
Truth table100%2/2
A
A= $self->{'seals'}
Aresult
11
00
525 143 ▶ my $messages = $self->{messages} || [];
Condition100%2/2
$self->{'messages'} || []
l!l
13211
MC/DC100%1/1
$self->{'messages'} || []
$self->{'messages'}
Truth table100%2/2
A
A= $self->{'messages'}
Aresult
11
00
526 143 ▶ my $aars = $self->{aars} || {};
Condition50%1/2
$self->{'aars'} || {}
l!l
1430
MC/DC0%0/1
$self->{'aars'} || {}
$self->{'aars'}
Truth table50%1/2
A
A= $self->{'aars'}
Aresult
11
00
527 143 ▶ unless ( @$seals or @$messages ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$seals or @$messages) 0 143 143
Condition66%2/3
@$seals or @$messages
l!l&&r!l&&!r
133100
MC/DC0%0/2
@$seals or @$messages
@$seals @$messages
Truth table66%2/3
A or B
A= @$sealsB= @$messages
ABresult
1X1
011
000
528 0 $self->{result} = 'none';
529 0 $self->{details} = 'no ARC headers found';
530 0 return;
531 }
532  
533 # determine if it's valid:
534 # 5.1.1.5. Determining the 'cv' Tag Value for ARC-Seal
535  
536 # In order for a series of ARC sets to be considered valid, the
537 # following statements MUST be satisfied:
538  
539 # 1. The chain of ARC sets must have structural integrity (no sets or
540 # set component header fields missing, no duplicates, excessive
541 # hops (cf. Section 5.1.1.1.1), etc.);
542  
543 143 ▶ if ( $#$seals == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seals->$#* == 0) 0 143 143
544 0 $self->{result} = 'fail';
545 0 $self->{details} = 'missing ARC-Seal 1';
546 0 return;
547 }
548 143 ▶ if ( $#$messages == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($messages->$#* == 0) 0 143 143
549 0 $self->{result} = 'fail';
550 0 $self->{details} = 'missing ARC-Message-Signature 1';
551 0 return;
552 }
553  
554 143 ▶ if ( $#$messages > $#$seals ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($messages->$#* > $seals->$#*) 11 132 143
555 11 $self->{result} = 'fail';
556 11 $self->{details} = 'missing Arc-Seal ' . $#$messages;
557 11 return;
558 }
559  
560 132 my ($max_aar) = sort { $b <=> $a } keys %$aars;
561 132 ▶ if ( defined $max_aar and $max_aar > $#$seals ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $max_aar and $max_aar > $seals->$#*) 2 130 132
Condition100%3/3
defined $max_aar and $max_aar > $seals->$#*
!ll&&!rl&&r
51252
MC/DC100%2/2
defined $max_aar and $max_aar > $seals->$#*
defined $max_aar $max_aar > $seals->$#*
Truth table100%3/3
A and B
A= defined $max_aarB= $max_aar > $seals->$#*
ABresult
0X0
100
111
562 2 $self->{result} = 'fail';
563 2 $self->{details} = 'missing Arc-Seal ' . $max_aar;
564 2 return;
565 }
566  
567 130 foreach my $i ( 1 .. $#$seals ) {
568  
569 # XXX - we should error if it's already present, but that's done above if at all
570 204 ▶ if ( !$seals->[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seals->[$i]) 1 203 204
571 1 $self->{result} = 'fail';
572 1 $self->{details} = "missing ARC-Seal $i";
573 1 return;
574 }
575 203 ▶ if ( !$messages->[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($messages->[$i]) 13 190 203
576 13 $self->{result} = 'fail';
577 13 $self->{details} = "missing ARC-Message-Signature $i";
578 13 return;
579 }
580 190 ▶ if ( !$aars->{$i} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($aars->{$i}) 4 186 190
581 4 $self->{result} = 'fail';
582 4 $self->{details} = "missing ARC-Authentication-Results $i";
583 4 return;
584 }
585 }
586  
587 # 2. All ARC-Seal header fields MUST validate;
588 112 foreach my $i ( 1 .. $#$seals ) {
589 134 my $result = $seals->[$i]->result();
590 134 ▶ if ( $result ne 'pass' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result ne "pass") 32 102 134
591 32 $self->{signature} = $seals->[$i]->signature;
592 32 $self->{result} = $result;
593 32 $self->{details} = $seals->[$i]->result_detail();
594 32 return;
595 }
596 }
597  
598 # 3. All ARC-Seal header fields MUST have a chain value (cv=) status
599 # of "pass" (except the first which MUST be "none"); and
600 80 my $cv = $seals->[1]->get_tag('cv');
601 80 ▶ if ( !defined $cv or $cv ne 'none' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $cv or $cv ne "none") 7 73 80
Condition100%3/3
not defined $cv or $cv ne "none"
l!l&&r!l&&!r
1673
MC/DC100%2/2
not defined $cv or $cv ne "none"
not defined $cv $cv ne "none"
Truth table100%3/3
not(A) or B
A= not defined $cvB= $cv ne "none"
ABresult
1X1
011
000
602 7 $self->{signature} = $seals->[1]->signature;
603 7 $self->{result} = 'fail';
604 7 $self->{details} = 'first ARC-Seal must be cv=none';
605 7 return;
606 }
607 73 foreach my $i ( 2 .. $#$seals ) {
608 18 my $cv = $seals->[$i]->get_tag('cv');
609 18 ▶ if ( !defined $cv or $cv ne 'pass' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $cv or $cv ne "pass") 3 15 18
Condition100%3/3
not defined $cv or $cv ne "pass"
l!l&&r!l&&!r
1215
MC/DC100%2/2
not defined $cv or $cv ne "pass"
not defined $cv $cv ne "pass"
Truth table100%3/3
not(A) or B
A= not defined $cvB= $cv ne "pass"
ABresult
1X1
011
000
610 3 $self->{signature} = $seals->[$i]->signature;
611 3 $self->{result} = 'fail';
612 3 $self->{details} = "wrong cv for ARC-Seal i=$i";
613 3 return;
614 }
615 }
616  
617 # 4. The newest (highest instance number (i=)) AMS header field MUST
618 # validate.
619 70 my $result = $messages->[$#$seals]->result();
620 70 ▶ if ( $result ne 'pass' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result ne "pass") 16 54 70
621 16 $self->{signature} = $messages->[$#$seals]->signature;
622 16 $self->{result} = $result;
623 16 $self->{details} = $messages->[$#$seals]->result_detail();
624 16 return;
625 }
626  
627 # Success!
628 54 $self->{signature} = $seals->[$#$seals]->signature();
629 54 $self->{result} = 'pass';
630 54 $self->{details} = $seals->[$#$seals]->result_detail();
631 }
632  
633 168 ▶sub result_detail {
Subroutine result_detail: called CC=8 SCAR=20.8
Pod documented
634 168 my $self = shift;
635  
636 168 ▶ return 'none' if $self->{result} eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"result"} eq "none" 5 163 168
637  
638 163 my @items;
639 163 foreach my $signature ( @{ $self->{signatures} } ) {
640 364 ▶ my $type =
Branch: 2 branches
BranchTrueFalseTotal
ref $signature eq 'Mail::DKIM::ARC::Seal' ? : 187 177 364
ref $signature eq 'Mail::DKIM::ARC::MessageSignature' ? : 177 0 177
641 ref($signature) eq 'Mail::DKIM::ARC::Seal' ? 'as'
642 : ref($signature) eq 'Mail::DKIM::ARC::MessageSignature' ? 'ams'
643 : ref($signature);
644 364 ▶ push @items,
Condition100%6/6
$signature->instance || ''
l!l
3577
$signature->domain || '(none)'
l!l
3604
$signature->result_detail || '?'
l!l
30658
MC/DC100%1/1
$signature->instance || ''
$signature->instance
MC/DC100%1/1
$signature->domain || '(none)'
$signature->domain
MC/DC100%1/1
$signature->result_detail || '?'
$signature->result_detail
Truth table100%2/2
A
A= $signature->instance
Aresult
11
00
Truth table100%2/2
A
A= $signature->domain
Aresult
11
00
Truth table100%2/2
A
A= $signature->result_detail
Aresult
11
00
645 "$type."
646 . ( $signature->instance() || '' ) . '.'
647 . ( $signature->domain() || '(none)' ) . '='
648 . ( $signature->result_detail() || '?' );
649 }
650  
651 163 return $self->{result} . ' (' . join( ', ', @items ) . ')';
652 }
653  
654  
655  
656 0 ▶sub signatures {
Subroutine signatures: not called CC=2 SCAR=17.9
Pod documented
657 0 my $self = shift;
658 0 ▶ croak 'unexpected argument' if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
659  
660 0 return @{ $self->{signatures} };
661 }
662  
663 5 1;
664  
665 __END__
« DKIM/Common.pm ↑ Summary DKIM/Policy.pm »