DKIM/ARC/Signer.pm

filter: Statementstmt 67.8% 209 / 308 Branchbran 60.5% 86 / 142 Conditioncond 35.1% 19 / 54 MC/DCmcdc 13.7% 4 / 29 Subroutinesub 67.8% 19 / 28 Podpod 68.7% 11 / 16 totaltotal 60.3% 348 / 577 CC 103 SCAR 43.5
CC
103
Cov
62%
process_headers_hash
380.0
finish_header
49.3
finish_body
31.7
CRAP 77.7
↑ Summary DKIM/DkimPolicy.pm »
1 package Mail::DKIM::ARC::Signer;
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: generates a DKIM signature for a 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 5 ▶use Mail::DKIM::PrivateKey;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Mail::DKIM::ARC::MessageSignature;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Mail::DKIM::ARC::Seal;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use Mail::DKIM::Canonicalization::seal;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use Mail::AuthenticationResults::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use Mail::AuthenticationResults::Header::AuthServID;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20  
21 5 ▶use base 'Mail::DKIM::Common';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 # PROPERTIES
25 #
26 # public:
27 #
28 # $signer->{Algorithm}
29 # identifies what algorithm to use when signing the message
30 # default is "rsa-sha256"
31 #
32 # $signer->{Domain}
33 # identifies what domain the message is signed for
34 #
35 # $signer->{SrvId}
36 # identifies what authserv-id is in the A-R headers
37 #
38 # $signer->{KeyFile}
39 # name of the file containing the private key used to sign
40 #
41 # $signer->{Policy}
42 # a signing policy (of type Mail::DKIM::SigningPolicy)
43 #
44 # $signer->{Selector}
45 # identifies name of the selector identifying the key
46 #
47 # $signer->{Key}
48 # the loaded private key
49 #
50 # private:
51 #
52 # $signer->{algorithms} = []
53 # an array of algorithm objects... an algorithm object is created for
54 # each signature being added to the message
55 #
56 # $signer->{result}
57 # result of the signing policy: "signed" or "skipped"
58 #
59 # $signer->{details}
60 # why we skipped this signature
61 #
62 # $signer->{signature}
63 # the created signature (of type Mail::DKIM::Signature)
64  
65 33 ▶sub init {
Subroutine init: called CC=10 SCAR=25.1
Pod undocumented
66 33 my $self = shift;
67 33 $self->SUPER::init;
68  
69 33 ▶ if ( defined $self->{KeyFile} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"KeyFile"}) 16 17 33
70 $self->{Key} ||=
71 16 ▶ Mail::DKIM::PrivateKey->load( File => $self->{KeyFile} );
Condition33%1/3
$self->{"Key"} ||= "Mail::DKIM::PrivateKey"->load("File", $self->{"KeyFile"})
l!l&&r!l&&!r
0160
MC/DC0%0/2
$self->{"Key"} ||= "Mail::DKIM::PrivateKey"->load("File", $self->{"KeyFile"})
$self->{"Key"} "Mail::DKIM::PrivateKey"->load("File", $self->{"KeyFile"})
Truth table33%1/3
A ||= B
A= $self->{"Key"}B= "Mail::DKIM::PrivateKey"->load("File", $self->{"KeyFile"})
ABresult
1X1
011
000
72 }
73  
74 33 ▶ unless ( $self->{'Algorithm'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"Algorithm"}) 0 33 33
75  
76 # use default algorithm
77 0 $self->{'Algorithm'} = 'rsa-sha256';
78 }
79 33 ▶ unless ( $self->{'Domain'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"Domain"}) 17 16 33
80  
81 # use default domain
82 17 $self->{'Domain'} = 'example.org';
83 }
84 33 ▶ unless ( $self->{'SrvId'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"SrvId"}) 5 28 33
85  
86 # use default domain
87 5 $self->{'SrvId'} = $self->{'Domain'};
88 }
89 33 ▶ unless ( $self->{'Selector'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"Selector"}) 0 33 33
90  
91 # use default selector
92 0 $self->{'Selector'} = 'unknown';
93 }
94 33 $self->{result} = '?'; # better update this before we finish
95 die 'Invalid signing algorithm'
96 33 ▶ unless $self->{Algorithm} eq 'rsa-sha256'; # add ed25519 sometime
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Algorithm"} eq "rsa-sha256" 0 33 33
97 die 'Need a valid chain value'
98 33 ▶ unless $self->{Chain} and $self->{Chain} =~ m{^(pass|fail|none|ar)$};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Chain"} and $self->{"Chain"} =~ /^(pass|fail|none|ar)$/ 0 33 33
Condition33%1/3
$self->{"Chain"} and $self->{"Chain"} =~ /^(pass|fail|none|ar)$/
!ll&&!rl&&r
0033
MC/DC0%0/2
$self->{"Chain"} and $self->{"Chain"} =~ /^(pass|fail|none|ar)$/
$self->{"Chain"} $self->{"Chain"} =~ /^(pass|fail|none|ar)$/
Truth table33%1/3
A and B
A= $self->{"Chain"}B= $self->{"Chain"} =~ /^(pass|fail|none|ar)$/
ABresult
0X0
100
111
99 }
100  
101 # instance numbers come from the message, and index the arrays below,
102 # so check them before use
103 175 ▶sub _valid_instance {
Subroutine _valid_instance: called CC=4 SCAR=17.1
104 175 my ($i) = @_;
105 175 ▶ return defined $i
Condition44%4/9
defined $i && $i =~ /^\d+$/ && $i >= 1 && $i <= $Mail::DKIM::Canonicalization::seal::MAX_INSTANCE
!ll&&!rl&&r
04171
defined $i and $i =~ /^\d+$/ and $i >= 1
!ll&&!rl&&r
00175
defined $i and $i =~ /^\d+$/
!ll&&!rl&&r
00175
MC/DC25%1/4
defined $i && $i =~ /^\d+$/ && $i >= 1 && $i <= $Mail::DKIM::Canonicalization::seal::MAX_INSTANCE
defined $i $i =~ /^\d+$/ $i >= 1 $i <= $Mail::DKIM::Canonicalization::seal::MAX_INSTANCE
Truth table40%2/5
A and B and C && D
A= defined $iB= $i =~ /^\d+$/C= $i >= 1D= $i <= $Mail::DKIM::Canonicalization::seal::MAX_INSTANCE
ABCDresult
0XXX0
10XX0
110X0
11100
11111
106 && $i =~ m{^\d+$}
107 && $i >= 1
108 && $i <= $Mail::DKIM::Canonicalization::seal::MAX_INSTANCE;
109 }
110  
111 # ARC headers come from the message, so may not parse; skip the message
112 # rather than die
113 119 ▶sub _parse_arc_header {
Subroutine _parse_arc_header: called CC=2 SCAR=6.9
114 119 my ( $self, $class, $header ) = @_;
115 119 my $parsed;
116 eval {
117 119 local $SIG{__DIE__};
118 119 $parsed = $class->parse($header);
119 117 1;
120 119 ▶ } || do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { local $SIG{"__DIE__"}; $parsed = $class->parse($header); 1 } } or do { chomp(my $E = $@); (my $name = $header) =~ s/:.*//s; $self->{"result"} = "skipped"; $self->{"details"} = "Unparseable $name header: $E"; return } 117 2 119
121 2 chomp( my $E = $@ );
122 2 ( my $name = $header ) =~ s/:.*//s;
123 2 $self->{result} = 'skipped';
124 2 $self->{details} = "Unparseable $name header: $E";
125 2 return;
126 };
127 117 return $parsed;
128 }
129  
130 33 ▶sub finish_header {
Subroutine finish_header: called CC=40 SCAR=39.0
Pod undocumented
131 33 my $self = shift;
132  
133 # add the AAR header
134 33 my @aar;
135 my @ams;
136 33 my @as;
137  
138 33 my $ar;
139 HEADER:
140 33 foreach my $header ( @{ $self->{headers} } ) {
141 374 $header =~ s/[\r\n]+$//;
142 374 ▶ if ( $header =~ m/^Authentication-Results:/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header =~ /^Authentication-Results:/) { } 39 335 374
143 39 my ( $arval ) = $header =~ m/^Authentication-Results:[^;]*;[\t ]*(.*)/is;
144 39 my $parsed;
145 eval {
146 39 $parsed= Mail::AuthenticationResults::Parser->new
147 ->parse( $header );
148 38 1
149 39 ▶ } || do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $parsed = "Mail::AuthenticationResults::Parser"->new->parse($header); 1 } } or do { my $error = $@; warn "Authentication-Results Header parse error: $error\n$header"; next HEADER } 38 1 39
150 1 my $error = $@;
151 1 warn "Authentication-Results Header parse error: $error\n$header";
152 1 next HEADER;
153 };
154 38 my $ardom = $parsed->value->value;
155  
156 next
157 38 ▶ unless "\L$ardom" eq $self->{SrvId}; # make sure it's our domain
Branch: 1 branch
BranchTrueFalseTotal
unless lc $ardom eq $self->{"SrvId"} 3 35 38
158  
159 35 $arval =~ s/;?\s*$//; # ignore trailing semicolon and whitespace
160 # preserve leading fold if there is one, otherwise set one leading space
161 35 ▶ $arval =~ s/^\s*/ / unless ($arval =~ m/^\015\012/);
Branch: 1 branch
BranchTrueFalseTotal
unless $arval =~ /^\015\012/ 33 2 35
162 35 ▶ if ($ar) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ar) { } 5 30 35
163 5 $ar .= ";$arval";
164 }
165 else {
166 30 $ar = "$ardom;$arval";
167 }
168  
169 # get chain value from A-R header
170 $self->{Chain} = $1
171 35 ▶ if $self->{Chain} eq 'ar' and $arval =~ m{\barc=(none|pass|fail)};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Chain"} eq "ar" and $arval =~ /\barc=(none|pass|fail)/ 27 8 35
Condition100%3/3
$self->{"Chain"} eq "ar" and $arval =~ /\barc=(none|pass|fail)/
!ll&&!rl&&r
5327
MC/DC100%2/2
$self->{"Chain"} eq "ar" and $arval =~ /\barc=(none|pass|fail)/
$self->{"Chain"} eq "ar" $arval =~ /\barc=(none|pass|fail)/
Truth table100%3/3
A and B
A= $self->{"Chain"} eq "ar"B= $arval =~ /\barc=(none|pass|fail)/
ABresult
0X0
100
111
172  
173 }
174 else {
175 # parse ARC headers to make sure we have completeness
176  
177 335 ▶ if ( $header =~ m/^ARC-/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header =~ /^ARC-/i) 180 155 335
178 180 ▶ if ( !$ar ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ar) 1 179 180
179 1 $self->{result} = 'skipped';
180 $self->{details} =
181 1 'ARC header seen before Authentication-Results';
182 1 return;
183 }
184 179 ▶ if ( $self->{Chain} eq 'ar' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Chain"} eq "ar") 1 178 179
185 1 $self->{result} = 'skipped';
186 $self->{details} =
187 1 'No ARC result found in Authentication-Results';
188 1 return;
189 }
190  
191 }
192  
193 333 ▶ if ( $header =~ m/^ARC-Seal:/i ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($header =~ /^ARC-Seal:/i) { } 60 273 333
elsif ($header =~ /^ARC-Message-Signature:/i) { } 59 214 273
elsif ($header =~ /^ARC-Authentication-Results\s*:/i) { } 59 155 214
194 60 ▶ my $seal =
Branch: 1 branch
BranchTrueFalseTotal
my $seal = $self->_parse_arc_header("Mail::DKIM::ARC::Seal", $header) or return 59 1 60
195 $self->_parse_arc_header( 'Mail::DKIM::ARC::Seal', $header )
196 or return;
197 59 my $i = $seal->instance;
198 59 ▶ unless ( _valid_instance($i) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_valid_instance($i)) 2 57 59
199 2 $self->{result} = 'skipped';
200 2 ▶ $self->{details} = sprintf "Invalid ARC-Seal instance '%s'",
Branch: 1 branch
BranchTrueFalseTotal
defined $i ? : 2 0 2
201 defined $i ? $i : '';
202 2 return;
203 }
204 57 ▶ if ( $as[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($as[$i]) 0 57 57
205 0 $self->{result} = 'skipped';
206 0 $self->{details} = "Duplicate ARC-Seal $i";
207 0 return;
208 }
209 57 $as[$i] = $seal;
210 }
211 elsif ( $header =~ m/^ARC-Message-Signature:/i ) {
212 59 ▶ my $sig = $self->_parse_arc_header(
Branch: 1 branch
BranchTrueFalseTotal
my $sig = $self->_parse_arc_header("Mail::DKIM::ARC::MessageSignature", $header) or return 58 1 59
213 'Mail::DKIM::ARC::MessageSignature', $header )
214 or return;
215 58 my $i = $sig->instance;
216 58 ▶ unless ( _valid_instance($i) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_valid_instance($i)) 1 57 58
217 1 $self->{result} = 'skipped';
218 $self->{details} =
219 1 ▶ sprintf "Invalid ARC-Message-Signature instance '%s'",
Branch: 1 branch
BranchTrueFalseTotal
defined $i ? : 1 0 1
220 defined $i ? $i : '';
221 1 return;
222 }
223 57 ▶ if ( $ams[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ams[$i]) 0 57 57
224 0 $self->{result} = 'skipped';
225 $self->{details} =
226 0 "Duplicate ARC-Message-Signature $i";
227 0 return;
228 }
229 57 $ams[$i] = $sig;
230 }
231 elsif ( $header =~ m/^ARC-Authentication-Results\s*:/i ) {
232  
233 # take the instance as the seal canonicalization does, so
234 # every AAR here is one the seals cover
235 59 my $i =
236 Mail::DKIM::Canonicalization::seal::_aar_instance($header);
237 59 ▶ unless ( defined $i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $i) 1 58 59
238 1 $self->{result} = 'skipped';
239 $self->{details} =
240 1 'Unparseable ARC-Authentication-Results header';
241 1 return;
242 }
243 58 ▶ unless ( _valid_instance($i) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_valid_instance($i)) 1 57 58
244 1 $self->{result} = 'skipped';
245 $self->{details} =
246 1 "Invalid ARC-Authentication-Results instance '$i'";
247 1 return;
248 }
249 57 ▶ if ( $aar[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($aar[$i]) 0 57 57
250 0 $self->{result} = 'skipped';
251 $self->{details} =
252 0 "Duplicate ARC-Authentication-Results $i";
253 0 return;
254 }
255  
256 57 $aar[$i] = $header;
257 }
258 }
259 }
260  
261 24 ▶ unless ($ar) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ar) 2 22 24
262 2 $self->{result} = 'skipped';
263 2 $self->{details} = 'No authentication results seen';
264 2 return;
265 }
266  
267 22 ▶ $self->{Chain} = 'none' if ($self->{Chain} eq 'ar');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Chain"} eq "ar" 2 20 22
268  
269 22 ▶ if ( $#ams > $#as ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#ams > $#as) 0 22 22
270 0 $self->{result} = 'skipped';
271 0 $self->{details} = 'More message signatures than seals';
272 0 return;
273 }
274 22 ▶ if ( $#aar > $#as ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#aar > $#as) 0 22 22
275 0 $self->{result} = 'skipped';
276 0 $self->{details} = 'More authentication results than seals';
277 0 return;
278 }
279  
280 22 foreach my $i ( 1 .. $#as ) {
281 57 ▶ unless ( $as[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($as[$i]) 0 57 57
282 0 $self->{result} = 'skipped';
283 0 $self->{details} = "Missing ARC-Seal $i";
284 0 return;
285 }
286 57 ▶ unless ( $ams[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ams[$i]) 0 57 57
287 0 $self->{result} = 'skipped';
288 0 $self->{details} = "Missing Arc-Message-Signature $i";
289 0 return;
290 }
291  
292 # don't care about authentication results, they are compulsary
293 }
294  
295 22 ▶ $self->{_Instance} = @as || 1; # next instance value
Condition100%2/2
@as || 1
l!l
616
MC/DC100%1/1
@as || 1
@as
Truth table100%2/2
A
A= @as
Aresult
11
00
296  
297 22 ▶ if ( $self->{_Instance} > $Mail::DKIM::Canonicalization::seal::MAX_INSTANCE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_Instance"} > $Mail::DKIM::Canonicalization::seal::MAX_INSTANCE) 1 21 22
298 1 $self->{result} = 'skipped';
299 1 $self->{details} = 'Too many ARC sets';
300 1 return;
301 }
302  
303 # first add the AAR header
304 21 $self->{_AAR} = "ARC-Authentication-Results: i=$self->{_Instance}; $ar";
305 21 unshift @{ $self->{headers} }, $self->{_AAR};
306  
307 # set up the signer for AMS
308 $self->add_signature(
309 Mail::DKIM::ARC::MessageSignature->new(
310 Algorithm => $self->{Algorithm},
311 Headers => $self->headers,
312 Instance => $self->{_Instance},
313 Method => 'relaxed/relaxed',
314 Domain => $self->{Domain},
315 Selector => $self->{Selector},
316 Key => $self->{Key},
317 KeyFile => $self->{KeyFile},
318 ( $self->{Timestamp} ? ( Timestamp => $self->{Timestamp} ) : () ),
319 ( $self->{Expiration} ? ( Expiration => $self->{Expiration} ) : () ),
320 (
321 21 ▶ $self->{'Tags'} ? ( Tags => $self->{'Tags'} )
Branch: 3 branches
BranchTrueFalseTotal
$self->{'Timestamp'} ? : 16 5 21
$self->{'Expiration'} ? : 0 21 21
$self->{'Tags'} ? : 0 21 21
322 : ()
323 ),
324 )
325 );
326  
327 21 foreach my $algorithm ( @{ $self->{algorithms} } ) {
328  
329 # output header as received so far into canonicalization
330 21 foreach my $header ( @{ $self->{headers} } ) {
331 216 $algorithm->add_header($header);
332 }
333 21 $algorithm->finish_header( Headers => $self->{headers} );
334 }
335 }
336  
337 33 ▶sub finish_body {
Subroutine finish_body: called CC=20 SCAR=34.5
Pod undocumented
338 33 my $self = shift;
339  
340 33 ▶ if ( $self->{result} eq 'skipped' ) { # already failed
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"result"} eq "skipped") 12 21 33
341 12 $self->{_AS} = undef;
342 12 return;
343 }
344  
345 21 foreach my $algorithm ( @{ $self->{algorithms} } ) {
346  
347 # finished canonicalizing
348 21 $algorithm->finish_body;
349  
350 # load the private key file if necessary
351 21 my $signature = $algorithm->signature;
352 my $key =
353 $signature->{Key}
354 || $signature->{KeyFile}
355 || $self->{Key}
356 21 ▶ || $self->{KeyFile};
Condition33%3/9
$signature->{'Key'} || $signature->{'KeyFile'} || $self->{'Key'} || $self->{'KeyFile'}
l!l&&r!l&&!r
2100
$signature->{"Key"} or $signature->{"KeyFile"} or $self->{"Key"}
l!l&&r!l&&!r
2100
$signature->{"Key"} or $signature->{"KeyFile"}
l!l&&r!l&&!r
2100
MC/DC0%0/4
$signature->{'Key'} || $signature->{'KeyFile'} || $self->{'Key'} || $self->{'KeyFile'}
$signature->{"Key"} $signature->{"KeyFile"} $self->{"Key"} $self->{'KeyFile'}
Truth table20%1/5
A or B or C || D
A= $signature->{"Key"}B= $signature->{"KeyFile"}C= $self->{"Key"}D= $self->{'KeyFile'}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
357 21 ▶ if ( defined($key) && !ref($key) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key and not ref $key) 0 21 21
Condition33%1/3
defined $key and not ref $key
!ll&&!rl&&r
0210
MC/DC0%0/2
defined $key and not ref $key
defined $key not ref $key
Truth table33%1/3
A and not(B)
A= defined $keyB= not ref $key
ABresult
0X0
100
111
358 0 $key = Mail::DKIM::PrivateKey->load( File => $key );
359 }
360 $key
361 21 ▶ or die "no key available to sign with\n";
Branch: 1 branch
BranchTrueFalseTotal
$key or die "no key available to sign with\n" 21 0 21
362  
363 # compute signature value
364 21 my $signb64 = $algorithm->sign($key);
365 21 $signature->data($signb64);
366  
367 # insert linebreaks in signature data, if desired
368 21 $signature->prettify_safe();
369  
370 21 $self->{_AMS} = $signature->as_string();
371 21 unshift @{ $self->{headers} }, $self->{_AMS};
372 }
373  
374 # reset the internal state
375 21 $self->{signatures} = [];
376 21 $self->{algorithms} = [];
377  
378 $self->add_signature(
379 Mail::DKIM::ARC::Seal->new(
380 Algorithm => $self->{Algorithm},
381 Chain => $self->{Chain},
382 Headers => $self->headers,
383 Instance => $self->{_Instance},
384 Domain => $self->{Domain},
385 Selector => $self->{Selector},
386 Key => $self->{Key},
387 KeyFile => $self->{KeyFile},
388 ( $self->{Timestamp} ? ( Timestamp => $self->{Timestamp} ) : () ),
389 ( $self->{Expiration} ? ( Expiration => $self->{Expiration} ) : () ),
390 (
391 21 ▶ $self->{'SealTags'} ? ( Tags => $self->{'SealTags'} )
Branch: 3 branches
BranchTrueFalseTotal
$self->{'Timestamp'} ? : 16 5 21
$self->{'Expiration'} ? : 0 21 21
$self->{'SealTags'} ? : 0 21 21
392 : ()
393 ),
394  
395 )
396 );
397  
398 21 foreach my $algorithm ( @{ $self->{algorithms} } ) {
399  
400 # output header as received so far into canonicalization
401 21 foreach my $header ( @{ $self->{headers} } ) {
402 237 $algorithm->add_header($header);
403 }
404  
405 # chain needed for seal canonicalization
406 $algorithm->finish_header(
407 Headers => $self->{headers},
408 Chain => $self->{Chain}
409 21 );
410  
411 # no body is required for ARC-Seal
412 # finished canonicalizing
413 21 $algorithm->finish_body;
414  
415 # load the private key file if necessary
416 21 my $signature = $algorithm->signature;
417 my $key =
418 $signature->{Key}
419 || $signature->{KeyFile}
420 || $self->{Key}
421 21 ▶ || $self->{KeyFile};
Condition33%3/9
$signature->{'Key'} || $signature->{'KeyFile'} || $self->{'Key'} || $self->{'KeyFile'}
l!l&&r!l&&!r
2100
$signature->{"Key"} or $signature->{"KeyFile"} or $self->{"Key"}
l!l&&r!l&&!r
2100
$signature->{"Key"} or $signature->{"KeyFile"}
l!l&&r!l&&!r
2100
MC/DC0%0/4
$signature->{'Key'} || $signature->{'KeyFile'} || $self->{'Key'} || $self->{'KeyFile'}
$signature->{"Key"} $signature->{"KeyFile"} $self->{"Key"} $self->{'KeyFile'}
Truth table20%1/5
A or B or C || D
A= $signature->{"Key"}B= $signature->{"KeyFile"}C= $self->{"Key"}D= $self->{'KeyFile'}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
422 21 ▶ if ( defined($key) && !ref($key) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key and not ref $key) 0 21 21
Condition33%1/3
defined $key and not ref $key
!ll&&!rl&&r
0210
MC/DC0%0/2
defined $key and not ref $key
defined $key not ref $key
Truth table33%1/3
A and not(B)
A= defined $keyB= not ref $key
ABresult
0X0
100
111
423 0 $key = Mail::DKIM::PrivateKey->load( File => $key );
424 }
425 $key
426 21 ▶ or die "no key available to sign ARC-Seal\n";
Branch: 1 branch
BranchTrueFalseTotal
$key or die "no key available to sign ARC-Seal\n" 21 0 21
427  
428 # compute signature value
429 21 my $signb64 = $algorithm->sign($key);
430 21 $signature->data($signb64);
431  
432 # insert linebreaks in signature data, if desired
433 21 $signature->prettify_safe();
434  
435 21 $self->{_AS} = $signature->as_string();
436 }
437  
438 21 $self->{result} = 'sealed';
439 }
440  
441  
442 42 ▶sub add_signature {
Subroutine add_signature: called CC=3 SCAR=11.6
Pod documented
443 42 my $self = shift;
444 42 my $signature = shift;
445  
446 # create a canonicalization filter and algorithm
447 42 ▶ my $algorithm_class =
Branch: 1 branch
BranchTrueFalseTotal
my $algorithm_class = $signature->get_algorithm_class($signature->algorithm) or die 'unsupported algorithm ' . ($signature->algorithm || "") . "\n" 42 0 42
Condition0%0/2
$signature->algorithm || ''
l!l
00
MC/DC0%0/1
$signature->algorithm || ''
$signature->algorithm
Truth table0%0/2
A
A= $signature->algorithm
Aresult
11
00
448 $signature->get_algorithm_class( $signature->algorithm )
449 or die 'unsupported algorithm ' . ( $signature->algorithm || '' ) . "\n";
450 my $algorithm = $algorithm_class->new(
451 Signature => $signature,
452 Debug_Canonicalization => $self->{Debug_Canonicalization},
453 42 );
454 42 push @{ $self->{algorithms} }, $algorithm;
455 42 return;
456 }
457  
458  
459 0 ▶sub algorithm {
Subroutine algorithm: not called CC=2 SCAR=17.9
Pod documented
460 0 my $self = shift;
461 0 ▶ if ( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 0 0 0
462 0 $self->{Algorithm} = shift;
463 }
464 0 return $self->{Algorithm};
465 }
466  
467  
468 0 ▶sub domain {
Subroutine domain: not called CC=2 SCAR=17.9
Pod documented
469 0 my $self = shift;
470 0 ▶ if ( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 0 0 0
471 0 $self->{Domain} = shift;
472 }
473 0 return $self->{Domain};
474 }
475  
476  
477  
478 # these are headers that "should" be included in the signature,
479 # according to the DKIM spec.
480 5 my @DEFAULT_HEADERS = qw(From Sender Reply-To Subject Date
481 Message-ID To Cc MIME-Version
482 Content-Type Content-Transfer-Encoding Content-ID Content-Description
483 Resent-Date Resent-From Resent-Sender Resent-To Resent-cc
484 Resent-Message-ID
485 In-Reply-To References
486 List-Id List-Help List-Unsubscribe List-Subscribe
487 List-Post List-Owner List-Archive);
488  
489 0 ▶sub process_headers_hash {
Subroutine process_headers_hash: not called CC=19 SCAR=59.4
Pod undocumented
490 0 my $self = shift;
491 0 my @headers;
492  
493 # these are the header fields we found in the message we're signing
494 0 my @found_headers = @{ $self->{header_field_names} };
495  
496 # Convert all keys to lower case
497 0 foreach my $header ( keys %{ $self->{'ExtendedHeaders'} } ) {
498 0 ▶ next if $header eq lc $header;
Branch: 1 branch
BranchTrueFalseTotal
if $header eq lc $header 0 0 0
499 0 ▶ if ( exists $self->{'ExtendedHeaders'}->{ lc $header } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'ExtendedHeaders'}{lc $header}) { } 0 0 0
500  
501 # Merge
502 0 my $first = $self->{'ExtendedHeaders'}->{ lc $header };
503 0 my $second = $self->{'ExtendedHeaders'}->{$header};
504 0 ▶ if ( $first eq '+' || $second eq '+' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($first eq '+' or $second eq '+') { } 0 0 0
elsif ($first eq '*' or $second eq '*') { } 0 0 0
Condition0%0/3
$first eq "+" or $second eq "+"
l!l&&r!l&&!r
000
MC/DC0%0/2
$first eq "+" or $second eq "+"
$first eq "+" $second eq "+"
Truth table0%0/3
A or B
A= $first eq "+"B= $second eq "+"
ABresult
1X1
011
000
505 0 $self->{'ExtendedHeaders'}->{ lc $header } = '+';
506 }
507 ▶ elsif ( $first eq '*' || $second eq '*' ) {
Condition0%0/3
$first eq "*" or $second eq "*"
l!l&&r!l&&!r
000
MC/DC0%0/2
$first eq "*" or $second eq "*"
$first eq "*" $second eq "*"
Truth table0%0/3
A or B
A= $first eq "*"B= $second eq "*"
ABresult
1X1
011
000
508 0 $self->{'ExtendedHeaders'}->{ lc $header } = '*';
509 }
510 else {
511 0 $self->{'ExtendedHeaders'}->{ lc $header } = $first + $second;
512 }
513 }
514 else {
515 # Rename
516 $self->{'ExtendedHeaders'}->{ lc $header } =
517 0 $self->{'ExtendedHeaders'}->{$header};
518 }
519 0 delete $self->{'ExtendedHeaders'}->{$header};
520 }
521  
522 # Add the default headers
523 0 ▶ if ( !$self->{'NoDefaultHeaders'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"NoDefaultHeaders"}) 0 0 0
524 0 foreach my $default (@DEFAULT_HEADERS) {
525 0 ▶ if ( !exists $self->{'ExtendedHeaders'}->{ lc $default } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"ExtendedHeaders"}{lc $default}) 0 0 0
526 0 $self->{'ExtendedHeaders'}->{ lc $default } = '*';
527 }
528 }
529 }
530  
531 # Build a count of found headers
532 0 my $header_counts = {};
533 0 foreach my $header (@found_headers) {
534 0 ▶ if ( !exists $header_counts->{ lc $header } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $$header_counts{lc $header}) { } 0 0 0
535 0 $header_counts->{ lc $header } = 1;
536 }
537 else {
538 0 $header_counts->{ lc $header } = $header_counts->{ lc $header } + 1;
539 }
540 }
541  
542 0 foreach my $header ( sort keys %{ $self->{'ExtendedHeaders'} } ) {
543 0 my $want_count = $self->{'ExtendedHeaders'}->{$header};
544 0 ▶ my $have_count = $header_counts->{ lc $header } || 0;
Condition0%0/2
$$header_counts{lc $header} || 0
l!l
00
MC/DC0%0/1
$$header_counts{lc $header} || 0
$$header_counts{lc $header}
Truth table0%0/2
A
A= $$header_counts{lc $header}
Aresult
11
00
545 0 my $add_count = 0;
546 0 ▶ if ( $want_count eq '+' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($want_count eq '+') { } 0 0 0
elsif ($want_count eq '*') { } 0 0 0
547 0 $add_count = $have_count + 1;
548 }
549 elsif ( $want_count eq '*' ) {
550 0 $add_count = $have_count;
551 }
552 else {
553 0 ▶ if ( $want_count > $have_count ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_count > $have_count) { } 0 0 0
554 0 $add_count = $have_count;
555 }
556 else {
557 0 $add_count = $want_count;
558 }
559 }
560 0 for ( 1 .. $add_count ) {
561 0 push @headers, $header;
562 }
563 }
564 0 return join( ':', @headers );
565 }
566  
567 0 ▶sub extended_headers {
Subroutine extended_headers: not called CC=1 SCAR=6.9
Pod documented
568 0 my $self = shift;
569 0 $self->{'ExtendedHeaders'} = shift;
570 0 return;
571 }
572  
573 42 ▶sub headers {
Subroutine headers: called CC=5 SCAR=16.2
Pod documented
574 42 my $self = shift;
575 42 ▶ croak 'unexpected argument' if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 42 42
576  
577 42 ▶ if ( exists $self->{'ExtendedHeaders'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"ExtendedHeaders"}) 0 42 42
578 0 return $self->process_headers_hash();
579 }
580  
581 # these are the header fields we found in the message we're signing
582 42 my @found_headers = @{ $self->{header_field_names} };
583  
584 # these are the headers we actually want to sign
585 42 my @wanted_headers;
586 42 ▶ if ( !$self->{'NoDefaultHeaders'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"NoDefaultHeaders"}) 10 32 42
587 10 @wanted_headers = @DEFAULT_HEADERS;
588 }
589 42 ▶ if ( $self->{Headers} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Headers"}) 32 10 42
590 32 push @wanted_headers, split /:/, $self->{Headers};
591 }
592  
593 my @headers =
594 grep {
595 42 my $a = $_;
596 390 scalar grep { lc($a) eq lc($_) } @wanted_headers
597 } @found_headers;
598 42 return join( ':', @headers );
599 }
600  
601 # return nonzero if this is header we should sign
602 0 ▶sub want_header {
Subroutine want_header: not called CC=1 SCAR=6.9
Pod undocumented
603 0 my $self = shift;
604 0 my ($header_name) = @_;
605  
606 #TODO- provide a way for user to specify which headers to sign
607 0 return scalar grep { lc($_) eq lc($header_name) } @DEFAULT_HEADERS;
608 }
609  
610  
611 0 ▶sub key {
Subroutine key: not called CC=2 SCAR=17.9
Pod documented
612 0 my $self = shift;
613 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
614 0 $self->{Key} = shift;
615 0 $self->{KeyFile} = undef;
616 }
617 0 return $self->{Key};
618 }
619  
620  
621 0 ▶sub key_file {
Subroutine key_file: not called CC=2 SCAR=17.9
Pod documented
622 0 my $self = shift;
623 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
624 0 $self->{Key} = undef;
625 0 $self->{KeyFile} = shift;
626 }
627 0 return $self->{KeyFile};
628 }
629  
630  
631  
632 0 ▶sub selector {
Subroutine selector: not called CC=2 SCAR=17.9
Pod documented
633 0 my $self = shift;
634 0 ▶ if ( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 0 0 0
635 0 $self->{Selector} = shift;
636 }
637 0 return $self->{Selector};
638 }
639  
640  
641 0 ▶sub signatures {
Subroutine signatures: not called CC=2 SCAR=17.9
Pod documented
642 0 my $self = shift;
643 0 ▶ croak 'no arguments allowed' if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
644 0 return map { $_->signature } @{ $self->{algorithms} };
645 }
646  
647  
648 19 ▶sub as_string {
Subroutine as_string: called CC=2 SCAR=6.9
Pod documented
649 19 my $self = shift;
650 19 ▶ return '' unless $self->{_AS}; # skipped, no signature
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_AS"} 1 18 19
651  
652 18 return join( "\015\012", $self->{_AS}, $self->{_AMS}, $self->{_AAR}, '' );
653 }
654  
655  
656 3 ▶sub as_strings {
Subroutine as_strings: called CC=1 SCAR=0.0
Pod documented
657 3 my $self = shift;
658 3 return ( $self->{_AS}, $self->{_AMS}, $self->{_AAR} );
659 }
660  
661 5 1;
662  
663 __END__
↑ Summary DKIM/DkimPolicy.pm »