bin/dkim2sign

filter: Statementstmt 91.0% 92 / 101 Branchbran 68.7% 33 / 48 Conditioncond 44.8% 13 / 29 MC/DCmcdc 12.5% 2 / 16 Subroutinesub 93.7% 15 / 16 Podpod n/a totaltotal 73.8% 155 / 210 CC 11 SCAR 18.1
CC
11
Cov
78%
__ANON__
20.0
__ANON__
9.8
BEGIN
2.0
CRAP 6.1
« lib/Mail/DKIM2/Canon.pm ↑ Summary blib/lib/Mail/DKIM2/Split.pm »
1 #!/usr/bin/perl -w
2 #
3 # Standalone DKIM2 signer CLI (draft-ietf-dkim-dkim2-spec-09).
4 #
5 # Reads a message, computes a Message-Instance when the content has changed
6 # since the top instance, signs, and writes the result to stdout. The
7 # counterpart of dkim2verify, and of python/dkim2sign.py and c/dkim2sign, so a
8 # cross-implementation harness can treat them all the same way.
9 #
10 # dkim2sign -s SELECTOR -d DOMAIN -k KEYFILE [--mailfrom '<a@b>']
11 # [--rcptto '<c@d>']... [--timestamp N] [--next-domain D]
12 # [--flag F]... [--exploded] [--parent-from D]... [--allow-null-body-recipe]
13 # [--ignore-timestamps] [--dns-json PATH] [MESSAGE|-]
14 #
15 # Before extending an existing DKIM2 chain it runs the same verify-before-sign
16 # gate as dkim2-milter (Mail::DKIM2::Gate): the upstream signatures must
17 # verify and the Message-Instance chain must undo cleanly. A null body Recipe
18 # on an UNSIGNED Message-Instance (m= above every signature's m=, top or not:
19 # this hop's own) is signed; a null an upstream domain already signed fails
20 # the upstream verification. --allow-null-body-recipe is deprecated and does
21 # nothing (0.29). A top signature carrying
22 # nd= is extended only when nd= equals DOMAIN. On refusal it prints the reason
23 # on stderr and exits 1 with nothing on stdout.
24 #
25 # Per §12.1/§12.2.5 an unmodified hop adds no Message-Instance: if the top
26 # instance already matches the content, its m= is reused and only a
27 # DKIM2-Signature is added.
28  
29 27 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 27 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 27 ▶use Email::MIME;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 27 ▶use Getopt::Long qw(GetOptions);
Subroutine BEGIN: called CC=1 SCAR=0.0
34 27 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 27 ▶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" ? : 27 0 27
36  
37 27 ▶use Mail::DKIM2::Address ();
Subroutine BEGIN: called CC=1 SCAR=0.0
38 27 ▶use Mail::DKIM2::Common qw(parse_mime);
Subroutine BEGIN: called CC=1 SCAR=0.0
39 27 ▶use Mail::DKIM2::Key qw(parse_dkim_pubkey);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 27 ▶use Mail::DKIM2::Gate;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 27 ▶use Mail::DKIM2::Hop;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 27 ▶use Mail::DKIM2::Signer;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 27 my ($selector, $domain, $keyfile, $timestamp, $next_domain);
45 27 my $mailfrom = '<>';
46 27 my $hash_algs = 'sha256';
47 27 my (@rcptto, @flags, @parent_from);
48 27 my ($allow_null, $ignore_ts, $dns_json, $exploded);
49 27 ▶$dns_json = $ENV{DKIM2_DNS_JSON} if defined $ENV{DKIM2_DNS_JSON} && length $ENV{DKIM2_DNS_JSON};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"DKIM2_DNS_JSON"} and length $ENV{"DKIM2_DNS_JSON"} 26 1 27
Condition66%2/3
defined $ENV{"DKIM2_DNS_JSON"} and length $ENV{"DKIM2_DNS_JSON"}
!ll&&!rl&&r
0126
MC/DC50%1/2
defined $ENV{"DKIM2_DNS_JSON"} and length $ENV{"DKIM2_DNS_JSON"}
defined $ENV{"DKIM2_DNS_JSON"} length $ENV{"DKIM2_DNS_JSON"}
Truth table66%2/3
A and B
A= defined $ENV{"DKIM2_DNS_JSON"}B= length $ENV{"DKIM2_DNS_JSON"}
ABresult
0X0
100
111
50  
51 27 ▶GetOptions(
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("s|selector=s", \$selector, "d|domain=s", \$domain, "k|keyfile=s", \$keyfile, "mailfrom=s", \$mailfrom, "rcptto=s", \@rcptto, "timestamp=i", \$timestamp, "next-domain=s", \$next_domain, "flag=s", \@flags, "exploded", \$exploded, "parent-from=s", \@parent_from, "hash=s", \$hash_algs, "allow-null-body-recipe", \$allow_null, "ignore-timestamps", \$ignore_ts, "dns-json=s", \$dns_json) or die _usage() 27 0 27
52 's|selector=s' => \$selector,
53 'd|domain=s' => \$domain,
54 'k|keyfile=s' => \$keyfile,
55 'mailfrom=s' => \$mailfrom,
56 'rcptto=s' => \@rcptto,
57 'timestamp=i' => \$timestamp,
58 'next-domain=s' => \$next_domain,
59 'flag=s' => \@flags,
60 'exploded' => \$exploded,
61 'parent-from=s' => \@parent_from,
62 'hash=s' => \$hash_algs,
63 'allow-null-body-recipe' => \$allow_null,
64 'ignore-timestamps' => \$ignore_ts,
65 'dns-json=s' => \$dns_json,
66 ) or die _usage();
67  
68 27 ▶die _usage() unless defined $selector && defined $domain && defined $keyfile;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $selector and defined $domain and defined $keyfile 1 26 27
Condition66%4/6
defined $selector and defined $domain and defined $keyfile
!ll&&!rl&&r
1026
defined $selector and defined $domain
!ll&&!rl&&r
1026
MC/DC33%1/3
defined $selector and defined $domain and defined $keyfile
defined $selector defined $domain defined $keyfile
Truth table50%2/4
A and B and C
A= defined $selectorB= defined $domainC= defined $keyfile
ABCresult
0XX0
10X0
1100
1111
69  
70 # -09 §3.1: signer chooses one or both hash algorithms for the
71 # Message-Instance h= tag. Default MUST remain sha256 (byte-identical to
72 # pre-spec-06 output).
73 26 my %HASH_ALG_SETS = (
74 sha256 => ['sha256'],
75 sha512 => ['sha512'],
76 both => ['sha256', 'sha512'],
77 );
78 26 ▶my $algs = $HASH_ALG_SETS{$hash_algs}
Branch: 1 branch
BranchTrueFalseTotal
my $algs = $HASH_ALG_SETS{$hash_algs} or die _usage("invalid --hash value '${hash_algs}' (expected sha256|sha512|both)") 25 1 26
79 or die _usage("invalid --hash value '$hash_algs' (expected sha256|sha512|both)");
80  
81 25 ▶my $file = shift // '-';
Condition50%1/2
shift @ARGV // "-"
l!l
250
MC/DC0%0/1
shift @ARGV // "-"
shift @ARGV
Truth table50%1/2
A
A= shift @ARGV
Aresult
11
00
82 my $raw = $file eq '-'
83 0 ? do { local $/; binmode STDIN; <STDIN> }
84 25 ▶ : do { open my $fh, '<:raw', $file or die "$file: $!\n"; local $/; <$fh> };
Branch: 2 branches
BranchTrueFalseTotal
$file eq '-' ? : 0 25 25
open my $fh, "<:raw", $file or die "${file}: $!\n" 25 0 25
85 25 ▶die "empty message\n" unless defined $raw && length $raw;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $raw and length $raw 0 25 25
Condition33%1/3
defined $raw and length $raw
!ll&&!rl&&r
0025
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
86  
87 # Normalise to CRLF: the hashes are defined over CRLF line endings.
88 25 $raw =~ s/\r\n/\n/g;
89 25 $raw =~ s/\r/\n/g;
90 25 $raw =~ s/\n/\r\n/g;
91  
92 # Gate (shared with dkim2-milter, Mail::DKIM2::Gate): before extending an
93 # existing DKIM2 chain, verify it -- an unsigned top Message-Instance is the one
94 # we are about to cover, so it is allowed. A message with no chain is signed
95 # as before.
96 25 ▶warn "dkim2sign: --allow-null-body-recipe is deprecated and does nothing (0.29)\n"
Branch: 1 branch
BranchTrueFalseTotal
if $allow_null 1 24 25
97 if $allow_null;
98 25 my %gate_opts = (SkipTimestampCheck => $ignore_ts, SigningDomain => $domain);
99 25 ▶if ($dns_json) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dns_json) 25 0 25
100 25 require JSON;
101 25 my $dns = do {
102 25 ▶ open my $fh, '<', $dns_json or die "$dns_json: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $dns_json or die "${dns_json}: $!\n" 25 0 25
103 25 local $/; JSON::decode_json(<$fh>);
104 };
105 12 ▶ $gate_opts{PubkeyCallback} = sub {
Subroutine __ANON__: called CC=6 SCAR=22.8
106 12 my ($sig, $idx, $verifier) = @_;
107 12 ▶ my ($sel, $dom) = ($sig->selector($idx // 0), $sig->domain);
Condition50%1/2
$idx // 0
l!l
120
MC/DC0%0/1
$idx // 0
$idx
Truth table50%1/2
A
A= $idx
Aresult
11
00
108 my $txt = $dns->{$dom}{"$sel._domainkey"}[0][1]
109 12 ▶ if $dom && $sel && $dns->{$dom};
Branch: 1 branch
BranchTrueFalseTotal
if $dom and $sel and $dns->{$dom} 12 0 12
Condition33%2/6
$dom and $sel and $dns->{$dom}
!ll&&!rl&&r
0012
$dom and $sel
!ll&&!rl&&r
0012
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
110 12 ▶ return parse_dkim_pubkey($txt) if $txt;
Branch: 1 branch
BranchTrueFalseTotal
if $txt 12 0 12
111 0 return $verifier->fetch_public_key($sig, $idx);
112 25 };
113 # The Domain Policy Record (-09 section 3.8) from the same file, for the
114 # gate's upstream verification.
115 0 ▶ $gate_opts{PolicyCallback} = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
116 0 my ($dom, $who) = @_;
117 0 ▶ my $entry = $dns->{$dom} && $dns->{$dom}{_domainkey};
Condition0%0/3
$dns->{$dom} && $dns->{$dom}{'_domainkey'}
!ll&&!rl&&r
000
MC/DC0%0/2
$dns->{$dom} && $dns->{$dom}{'_domainkey'}
$dns->{$dom} $dns->{$dom}{'_domainkey'}
Truth table0%0/3
A && B
A= $dns->{$dom}B= $dns->{$dom}{'_domainkey'}
ABresult
0X0
100
111
118 0 ▶ return [ map { $_->[1] } @$entry ] if $entry;
Branch: 1 branch
BranchTrueFalseTotal
if $entry 0 0 0
119 0 ▶ return $dns->{$dom} ? [] : $who->fetch_domain_policy($dom);
Branch: 1 branch
BranchTrueFalseTotal
$dns->{$dom} ? : 0 0 0
120 25 };
121 }
122 25 my $gate = Mail::DKIM2::Gate->check($raw, %gate_opts);
123 25 ▶unless ($gate->{ok}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($gate->{"ok"}) 5 20 25
124 5 print STDERR "dkim2sign: not signing: $gate->{message}\n";
125 5 exit 1;
126 }
127  
128 20 my $msg = parse_mime($raw);
129  
130 # -09 section 12.1: add an instance only if the message has none. One whose
131 # top instance still describes it is signed over that; one whose top
132 # instance does not was refused by the gate above (this tool keeps no
133 # snapshot to say what changed).
134 20 my $next = Mail::DKIM2::Hop->next_instance($raw, Algs => $algs);
135 $msg->header_raw_prepend('Message-Instance', $next->{instance}->as_header_value)
136 20 ▶ if $next->{instance};
Branch: 1 branch
BranchTrueFalseTotal
if $next->{"instance"} 9 11 20
137  
138 20 ▶my $signer = Mail::DKIM2::Signer->new(
Branch: 5 branches
BranchTrueFalseTotal
$next_domain ? : 1 19 20
@rcptto ? : 19 0 19
defined $timestamp ? : 8 12 20
@flags ? : 0 20 20
$exploded ? : 2 18 20
139 Selector => $selector,
140 Domain => $domain,
141 KeyFile => $keyfile,
142 ($next_domain
143 ? (NextDomain => $next_domain)
144 : (MailFrom => $mailfrom, RcptTo => (@rcptto ? \@rcptto : ['<>']))),
145 (defined $timestamp ? (Timestamp => $timestamp) : ()),
146 (@flags ? (Flags => \@flags) : ()),
147 ($exploded ? (Exploded => 1) : ()),
148 # -09 contract E3.4: a d= below the From domain signs only for a
149 # --parent-from domain; no DNS lookup.
150 ParentFromCallback => Mail::DKIM2::Address::parent_from_matcher(@parent_from),
151 );
152 20 $signer->PRINT($msg->as_string);
153 20 $signer->CLOSE;
154  
155 20 ▶die "signing failed: " . ($signer->result_detail // 'no result') . "\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ($signer->result // "") eq "signed" 1 19 20
Condition50%2/4
$signer->result // ""
l!l
200
$signer->result_detail // "no result"
l!l
10
MC/DC0%0/1
$signer->result // ""
$signer->result
MC/DC0%0/1
$signer->result_detail // "no result"
$signer->result_detail
Truth table50%1/2
A
A= $signer->result
Aresult
11
00
Truth table50%1/2
A
A= $signer->result_detail
Aresult
11
00
156 unless ($signer->result // '') eq 'signed';
157  
158 19 (my $sig_header = $signer->as_string) =~ s/^DKIM2-Signature:\s*//;
159 19 $msg->header_raw_prepend('DKIM2-Signature', $sig_header);
160  
161 19 my $out = $msg->as_string;
162 19 $out =~ s/\r\n/\n/g;
163 19 $out =~ s/\n/\r\n/g;
164 19 binmode STDOUT;
165 19 print $out;
166  
167 2 ▶sub _usage {
Subroutine _usage: called CC=2 SCAR=6.9
168 2 my ($err) = @_;
169 2 my $usage = <<"USAGE";
170 usage: $0 -s SELECTOR -d DOMAIN -k KEYFILE [options] [MESSAGE|-]
171  
172 -s, --selector S DKIM2 selector
173 -d, --domain D signing domain
174 -k, --keyfile F PEM private key
175 --mailfrom A MAIL FROM, bracketed (default <>)
176 --rcptto A RCPT TO, bracketed; repeatable
177 --timestamp N fixed t= (default: now)
178 --next-domain D emit nd= for an imaginary forwarding hop, omitting mf=/rt=
179 --flag F f= flag; repeatable
180 --exploded this hop sends the message to more than one address: f=exploded
181 (-09 section 12.5; also set when --rcptto is given twice; never at i=1)
182 --parent-from D a parent domain whose From this d= may make Authentic
183 (-09 sections 7.1 item 4, 11.1); repeatable; no DNS lookup
184 --allow-null-body-recipe deprecated (0.29), does nothing: this hop's own
185 null body Recipe is always signed
186 --ignore-timestamps do not fail an upstream signature for its t= age
187 --dns-json PATH answer key lookups from an interop dns.json (default \$DKIM2_DNS_JSON)
188 --hash A hash algorithm(s) for Message-Instance h=: sha256|sha512|both (default sha256)
189 USAGE
190 2 ▶ return $err ? "$err\n$usage" : $usage;
Branch: 1 branch
BranchTrueFalseTotal
$err ? : 1 1 2
191 }
« lib/Mail/DKIM2/Canon.pm ↑ Summary blib/lib/Mail/DKIM2/Split.pm »