DKIM/ARC/Verifier.pm

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