bin/dkim2verify

filter: Statementstmt 92.5% 50 / 54 Branchbran 60.7% 17 / 28 Conditioncond 36.3% 4 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 81.8% 9 / 11 Podpod n/a totaltotal 72.7% 80 / 110 CC 6 SCAR 11.9
CC
6
Cov
76%
__ANON__
7.5
BEGIN
2.0
__ANON__
2.0
CRAP 3.3
« bin/validate.pl ↑ Summary lib/Mail/DKIM2/BodyHasher.pm »
1 #!/usr/bin/perl
2 #
3 # Standalone DKIM2 verifier CLI (draft-ietf-dkim-dkim2-spec-09).
4 #
5 # Reads a message, verifies its DKIM2-Signature chain and Message-Instance
6 # chain with Mail::DKIM2::Verifier, prints the result, and exits with the
7 # verdict: 0 for pass, 75 (EX_TEMPFAIL) for temperror, 1 for anything else.
8 # The counterpart of dkim2sign, and of python/dkim2verify.py and c/dkim2verify.
9 #
10 # dkim2verify [--dns-json PATH] [--ignore-timestamps] [--ignore-prefix P]...
11 # [--allow-unsigned-mi] [MESSAGE|-]
12 #
13 # Keys come from DNS unless --dns-json names an interop dns.json, in which case
14 # domains listed there are answered from the file and the rest still from DNS.
15  
16 5 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use Getopt::Long qw(GetOptions);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶use lib -d "$FindBin::Bin/../lib/Mail/DKIM2" ? "$FindBin::Bin/../lib" : (); # running from a checkout
Subroutine BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
-d "$FindBin::Bin/../lib/Mail/DKIM2" ? : 5 0 5
22  
23 5 ▶use Mail::DKIM2::Key qw(parse_dkim_pubkey);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 5 ▶use Mail::DKIM2::Verifier;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 5 my ($dns_json, $ignore_ts, $allow_unsigned, @prefixes);
27 GetOptions(
28 'dns-json=s' => \$dns_json,
29 'ignore-timestamps' => \$ignore_ts,
30 'ignore-prefix=s' => \@prefixes,
31 'allow-unsigned-mi' => \$allow_unsigned,
32 0 ▶ 'help|h' => sub { print _usage(); exit 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
33 5 ▶) or die _usage();
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("dns-json=s", \$dns_json, "ignore-timestamps", \$ignore_ts, "ignore-prefix=s", \@prefixes, "allow-unsigned-mi", \$allow_unsigned, "help|h", sub { print _usage(); exit 0; } ) or die _usage() 5 0 5
34  
35 5 ▶my $file = shift // '-';
Condition50%1/2
shift @ARGV // "-"
l!l
50
MC/DC0%0/1
shift @ARGV // "-"
shift @ARGV
Truth table50%1/2
A
A= shift @ARGV
Aresult
11
00
36 my $raw = $file eq '-'
37 2 ? do { local $/; binmode STDIN; <STDIN> }
38 5 ▶ : do { open my $fh, '<:raw', $file or die "$file: $!\n"; local $/; <$fh> };
Branch: 2 branches
BranchTrueFalseTotal
$file eq '-' ? : 2 3 5
open my $fh, "<:raw", $file or die "${file}: $!\n" 3 0 3
39 5 ▶die "empty message\n" unless defined $raw && length $raw;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $raw and length $raw 0 5 5
Condition33%1/3
defined $raw and length $raw
!ll&&!rl&&r
005
MC/DC0%0/2
defined $raw and length $raw
defined $raw length $raw
Truth table33%1/3
A and B
A= defined $rawB= length $raw
ABresult
0X0
100
111
40  
41 5 ▶my %opts = (
Branch: 3 branches
BranchTrueFalseTotal
$ignore_ts ? : 3 2 5
$allow_unsigned ? : 0 5 5
@prefixes ? : 0 5 5
42 SkipTimestampCheck => $ignore_ts ? 1 : 0,
43 AllowUnsignedMI => $allow_unsigned ? 1 : 0,
44 (@prefixes ? (IgnorePrefixes => \@prefixes) : ()),
45 );
46  
47 5 ▶if ($dns_json) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dns_json) 5 0 5
48 5 require JSON;
49 5 my $dns = do {
50 5 ▶ open my $fh, '<', $dns_json or die "$dns_json: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $dns_json or die "${dns_json}: $!\n" 5 0 5
51 5 local $/; JSON::decode_json(<$fh>);
52 };
53 4 ▶ $opts{PubkeyCallback} = sub {
Subroutine __ANON__: called CC=5 SCAR=20.2
54 4 my ($sig, $idx, $verifier) = @_;
55 4 my ($sel, $dom) = ($sig->selector($idx), $sig->domain);
56 my $txt = $dns->{$dom}{"$sel._domainkey"}[0][1]
57 4 ▶ if $dom && $sel && $dns->{$dom};
Branch: 1 branch
BranchTrueFalseTotal
if $dom and $sel and $dns->{$dom} 4 0 4
Condition33%2/6
$dom and $sel and $dns->{$dom}
!ll&&!rl&&r
004
$dom and $sel
!ll&&!rl&&r
004
MC/DC0%0/3
$dom and $sel and $dns->{$dom}
$dom $sel $dns->{$dom}
Truth table25%1/4
A and B and C
A= $domB= $selC= $dns->{$dom}
ABCresult
0XX0
10X0
1100
1111
58 4 ▶ return parse_dkim_pubkey($txt) if $txt;
Branch: 1 branch
BranchTrueFalseTotal
if $txt 4 0 4
59 0 return $verifier->fetch_public_key($sig, $idx);
60 5 };
61 }
62  
63 5 my $v = Mail::DKIM2::Verifier->new(%opts)->load($raw);
64 5 my $result = $v->result;
65 5 say $v->result_detail;
66 5 ▶exit($result eq 'pass' ? 0 : $result eq 'temperror' ? 75 : 1);
Branch: 2 branches
BranchTrueFalseTotal
$result eq 'pass' ? : 2 3 5
$result eq 'temperror' ? : 0 3 3
67  
68 0 ▶sub _usage {
Subroutine _usage: not called CC=1 SCAR=6.9
69 0 return <<"USAGE";
70 usage: $0 [options] [MESSAGE|-]
71  
72 --dns-json PATH answer key lookups for domains in this interop dns.json
73 --ignore-timestamps do not fail a signature for its t= age
74 --ignore-prefix P treat header fields starting with P as unhashed (repeatable)
75 --allow-unsigned-mi permit a Message-Instance above the top signature
76 -h, --help
77  
78 Exit status: 0 pass, 75 temperror, 1 otherwise.
79 USAGE
80 }
« bin/validate.pl ↑ Summary lib/Mail/DKIM2/BodyHasher.pm »