lib/Mail/DKIM2/Key.pm

filter: Statementstmt 55.8% 173 / 310 Branchbran 43.7% 91 / 208 Conditioncond 43.7% 28 / 64 MC/DCmcdc 40.0% 16 / 40 Subroutinesub 66.6% 30 / 45 Podpod 100% 20 / 20 totaltotal 52.1% 358 / 687 CC 133 SCAR 30.5
CC
133
Cov
50%
load_private_key_data
51.4
load_private_key_data
51.4
parse_dkim_key_record
38.5
CRAP 21.1
« lib/Mail/DKIM2/Gate.pm ↑ Summary lib/Mail/DKIM2/HeaderPlan.pm »
1 package Mail::DKIM2::Key;
2 222 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 222 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 222 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 222 our $VERSION = '0.34';
7  
8 222 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 222 ▶use MIME::Base64 qw(decode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 222 ▶use Crypt::PK::RSA;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 222 ▶use Crypt::PK::Ed25519;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 222 ▶use Crypt::Digest::SHA256 qw(sha256);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 222 ▶use Mail::DKIM2::TagValueList ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 222 ▶use Mail::DKIM2::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 222 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 222 our @EXPORT_OK = qw(
18 signature_algorithm
19 signature_algorithm_for_key
20 load_private_key
21 load_private_key_data
22 parse_dkim_pubkey
23 parse_dkim_key_record
24 key_records
25 domain_policy
26 query_domain_policy
27 policy_from_source
28 );
29  
30 # The signing algorithms this library implements (-09 sections 3.2, 3.3),
31 # for the Signer and the Verifier alike: the key class each needs, how it
32 # signs and verifies the signing input of section 12.8, and the smallest key
33 # it accepts. Names are case-sensitive (section 2.14) and match exactly.
34 my %SIGNATURE_ALGORITHM = (
35 'rsa-sha256' => {
36 name => 'rsa-sha256',
37 key_class => 'Crypt::PK::RSA',
38 min_bits => 1024, # section 3.2: Signers MUST use at least this
39 532 ▶ sign => sub { $_[0]->sign_message($_[1], 'SHA256', 'v1.5') },
Subroutine __ANON__: called CC=1 SCAR=0.0
40 770 ▶ verify => sub { $_[0]->verify_message($_[2], $_[1], 'SHA256', 'v1.5') },
Subroutine __ANON__: called CC=1 SCAR=0.0
41 },
42 'ed25519-sha256' => {
43 name => 'ed25519-sha256',
44 key_class => 'Crypt::PK::Ed25519',
45 # The SHA-256 of the input is what PureEdDSA signs (section 3.3).
46 2 ▶ sign => sub { $_[0]->sign_message(sha256($_[1])) },
Subroutine __ANON__: called CC=1 SCAR=0.0
47 1 ▶ verify => sub { $_[0]->verify_message($_[2], sha256($_[1])) },
Subroutine __ANON__: called CC=1 SCAR=0.0
48 },
49 222 );
50  
51 # signature_algorithm($name): the entry above for an s= item's algorithm
52 # name -- a hashref with name, key_class, sign->($key, $input) and
53 # verify->($key, $input, $signature), and min_bits for RSA -- or undef for a
54 # name this library does not implement.
55 3246 ▶sub signature_algorithm {
Subroutine signature_algorithm: called CC=2 SCAR=9.2
Pod documented
56 0 ▶ my ($name) = @_;
Subroutine signature_algorithm: not called CC=2 SCAR=9.2
Pod documented
57 0 ▶ return defined $name ? $SIGNATURE_ALGORITHM{$name} : undef;
Branch: 2 branches
BranchTrueFalseTotal
defined $name ? : 3245 1 3246
defined $name ? : 0 0 0
58 }
59  
60 # signature_algorithm_for_key($key): the name of the algorithm a key object
61 # signs with, or undef for a key of no implemented type.
62 552 ▶sub signature_algorithm_for_key {
Subroutine signature_algorithm_for_key: called CC=4 SCAR=19.1
Pod documented
63 0 ▶ my ($key) = @_;
Subroutine signature_algorithm_for_key: not called CC=4 SCAR=19.1
Pod documented
64 0 ▶ return unless Scalar::Util::blessed($key);
Branch: 2 branches
BranchTrueFalseTotal
unless builtin::blessed($key) 2 550 552
unless builtin::blessed($key) 0 0 0
65 0 for my $name (sort keys %SIGNATURE_ALGORITHM) {
66 0 ▶ return $name if $key->isa($SIGNATURE_ALGORITHM{$name}{key_class});
Branch: 2 branches
BranchTrueFalseTotal
if $key->isa($SIGNATURE_ALGORITHM{$name}{"key_class"}) 550 548 1098
if $key->isa($SIGNATURE_ALGORITHM{$name}{"key_class"}) 0 0 0
67 }
68 0 return;
69 }
70  
71 # Load a private key from a PEM file.
72 # Returns a Crypt::PK::RSA or Crypt::PK::Ed25519 object depending on key type.
73 163 ▶sub load_private_key {
Subroutine load_private_key: called CC=3 SCAR=15.0
Pod documented
74 0 ▶ my ($file) = @_;
Subroutine load_private_key: not called CC=3 SCAR=15.0
Pod documented
75 # Try RSA first, then Ed25519
76 0 my $key = eval { Crypt::PK::RSA->new($file) };
77 0 ▶ die $@ if ref $@;
Branch: 2 branches
BranchTrueFalseTotal
if ref $@ 0 163 163
if ref $@ 0 0 0
78 0 ▶ return $key if $key;
Branch: 2 branches
BranchTrueFalseTotal
if $key 159 4 163
if $key 0 0 0
79 0 return Crypt::PK::Ed25519->new($file);
80 }
81  
82 # Load a private key from key MATERIAL rather than a filename, for callers whose
83 # keys live in a database rather than on disk.
84 #
85 # Accepts PEM as stored, and also bare base64 with the armor stripped, which is
86 # how some key stores keep it. Returns a Crypt::PK::RSA or Crypt::PK::Ed25519
87 # object, or undef -- never dies, so a caller signing live mail can log and
88 # carry on rather than losing the message.
89 12 ▶sub load_private_key_data {
Subroutine load_private_key_data: called CC=14 SCAR=39.4
Pod documented
90 0 ▶ my ($data) = @_;
Subroutine load_private_key_data: not called CC=14 SCAR=39.4
Pod documented
91 0 ▶ return unless defined $data && length $data;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $data and length $data 2 10 12
unless defined $data and length $data 0 0 0
Condition50%3/6
defined $data and length $data
!ll&&!rl&&r
1110
defined $data and length $data
!ll&&!rl&&r
000
MC/DC100%2/2
defined $data and length $data
defined $data length $data
MC/DC0%0/2
defined $data and length $data
defined $data length $data
Truth table100%3/3
A and B
A= defined $dataB= length $data
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $dataB= length $data
ABresult
0X0
100
111
92  
93 0 ▶ if ($data =~ /-----BEGIN/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($data =~ /-----BEGIN/u) 4 6 10
if ($data =~ /-----BEGIN/u) 0 0 0
94 0 my $key = eval { Crypt::PK::RSA->new(\$data) };
95 0 ▶ die $@ if ref $@;
Branch: 2 branches
BranchTrueFalseTotal
if ref $@ 0 4 4
if ref $@ 0 0 0
96 0 ▶ return $key if $key;
Branch: 2 branches
BranchTrueFalseTotal
if $key 2 2 4
if $key 0 0 0
97 0 $key = eval { Crypt::PK::Ed25519->new(\$data) };
98 0 ▶ die $@ if ref $@;
Branch: 2 branches
BranchTrueFalseTotal
if ref $@ 0 2 2
if ref $@ 0 0 0
99 0 return $key;
100 }
101  
102 0 (my $b64 = $data) =~ s/\s+//g;
103 0 ▶ return unless length $b64;
Branch: 2 branches
BranchTrueFalseTotal
unless length $b64 1 5 6
unless length $b64 0 0 0
104 0 my $der = eval { decode_base64($b64) };
105 0 ▶ die $@ if ref $@;
Branch: 2 branches
BranchTrueFalseTotal
if ref $@ 0 5 5
if ref $@ 0 0 0
106 0 ▶ return unless defined $der && length $der;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $der and length $der 0 5 5
unless defined $der and length $der 0 0 0
Condition16%1/6
defined $der and length $der
!ll&&!rl&&r
005
defined $der and length $der
!ll&&!rl&&r
000
MC/DC0%0/2
defined $der and length $der
defined $der length $der
MC/DC0%0/2
defined $der and length $der
defined $der length $der
Truth table33%1/3
A and B
A= defined $derB= length $der
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $derB= length $der
ABresult
0X0
100
111
107 0 my $key = eval { Crypt::PK::RSA->new(\$der) };
108 0 ▶ die $@ if ref $@;
Branch: 2 branches
BranchTrueFalseTotal
if ref $@ 0 5 5
if ref $@ 0 0 0
109 0 ▶ return $key if $key;
Branch: 2 branches
BranchTrueFalseTotal
if $key 2 3 5
if $key 0 0 0
110 0 $key = eval { Crypt::PK::Ed25519->new(\$der) };
111 0 ▶ die $@ if ref $@;
Branch: 2 branches
BranchTrueFalseTotal
if ref $@ 0 3 3
if ref $@ 0 0 0
112 0 return $key;
113 }
114  
115 # A key object from a valid record: the key, or nothing (a key type this
116 # library does not implement, or p= that is not a key of that type).
117 622 ▶sub parse_dkim_pubkey {
Subroutine parse_dkim_pubkey: called CC=1 SCAR=1.2
Pod documented
118 0 ▶ my ($key_txt) = @_;
Subroutine parse_dkim_pubkey: not called CC=1 SCAR=1.2
Pod documented
119 0 my ($key) = parse_dkim_key_record($key_txt);
120 0 return $key;
121 }
122  
123 # Parse and validate a key record (draft-ietf-dkim-dkim2-dns §3.2, §3.4.1;
124 # -09 §14.5 says a Verifier MUST NOT use a malformed one). Returns the
125 # key object, or (undef, $why) where $why completes "public key <selector>
126 # ...": "has a syntax error", "has been revoked" or "has an unsupported key
127 # type". The whole record is checked, not searched: a repeated tag (an empty
128 # p= before a good one, say) makes it invalid, and v=, if present, must be
129 # the first tag and exactly DKIM1. Tag names are case sensitive here, as in
130 # DKIM1. Unknown and retired tags (h=, n=, s=, t=) are ignored, once their
131 # values have passed the tag-list grammar.
132 761 ▶sub parse_dkim_key_record {
Subroutine parse_dkim_key_record: called CC=14 SCAR=36.5
Pod documented
133 0 ▶ my ($txt) = @_;
Subroutine parse_dkim_key_record: not called CC=14 SCAR=36.5
Pod documented
134 0 my $syntax = 'has a syntax error';
135 0 ▶ my ($tag, $order) = _parse_dns_tag_list($txt) or return (undef, $syntax);
Branch: 2 branches
BranchTrueFalseTotal
() = _parse_dns_tag_list($txt) or return undef, $syntax 751 10 761
() = _parse_dns_tag_list($txt) or return undef, $syntax 0 0 0
136 0 my %tag = %$tag;
137 0 ▶ if (exists $tag{v}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $tag{"v"}) 750 1 751
if (exists $tag{"v"}) 0 0 0
138 0 ▶ return (undef, $syntax) unless $order->[0] eq 'v' && $tag{v} eq 'DKIM1';
Branch: 2 branches
BranchTrueFalseTotal
unless $order->[0] eq "v" and $tag{"v"} eq "DKIM1" 4 746 750
unless $order->[0] eq "v" and $tag{"v"} eq "DKIM1" 0 0 0
Condition50%3/6
$order->[0] eq "v" and $tag{"v"} eq "DKIM1"
!ll&&!rl&&r
13746
$order->[0] eq "v" and $tag{"v"} eq "DKIM1"
!ll&&!rl&&r
000
MC/DC100%2/2
$order->[0] eq "v" and $tag{"v"} eq "DKIM1"
$order->[0] eq "v" $tag{"v"} eq "DKIM1"
MC/DC0%0/2
$order->[0] eq "v" and $tag{"v"} eq "DKIM1"
$order->[0] eq "v" $tag{"v"} eq "DKIM1"
Truth table100%3/3
A and B
A= $order->[0] eq "v"B= $tag{"v"} eq "DKIM1"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $order->[0] eq "v"B= $tag{"v"} eq "DKIM1"
ABresult
0X0
100
111
139 }
140 0 ▶ return (undef, $syntax) unless exists $tag{p};
Branch: 2 branches
BranchTrueFalseTotal
unless exists $tag{"p"} 1 746 747
unless exists $tag{"p"} 0 0 0
141 0 (my $p = $tag{p}) =~ s/[ \t\r\n]//g;
142 0 ▶ return (undef, 'has been revoked') unless length $p;
Branch: 2 branches
BranchTrueFalseTotal
unless length $p 5 741 746
unless length $p 0 0 0
143 0 ▶ return (undef, $syntax) unless Mail::DKIM2::Util::is_base64string($p);
Branch: 2 branches
BranchTrueFalseTotal
unless Mail::DKIM2::Util::is_base64string($p) 2 739 741
unless Mail::DKIM2::Util::is_base64string($p) 0 0 0
144 0 ▶ my $k = $tag{k} // 'rsa';
Condition50%2/4
$tag{"k"} // "rsa"
l!l
7336
$tag{"k"} // "rsa"
l!l
00
MC/DC100%1/1
$tag{"k"} // "rsa"
$tag{"k"}
MC/DC0%0/1
$tag{"k"} // "rsa"
$tag{"k"}
Truth table100%2/2
A
A= $tag{"k"}
Aresult
11
00
Truth table0%0/2
A
A= $tag{"k"}
Aresult
11
00
145 0 my $key = _key_from_p($k, $p);
146 0 ▶ return $key if $key;
Branch: 2 branches
BranchTrueFalseTotal
if $key 723 14 737
if $key 0 0 0
147 # -09 section 14.6 (contract E5): an RSA key this verifier regards as
148 # too short.
149 0 ▶ return (undef, 'too short') if $k eq 'rsa' && _rsa_bits($p);
Branch: 2 branches
BranchTrueFalseTotal
if $k eq "rsa" and _rsa_bits($p) 3 11 14
if $k eq "rsa" and _rsa_bits($p) 0 0 0
Condition50%3/6
$k eq "rsa" and _rsa_bits($p)
!ll&&!rl&&r
923
$k eq "rsa" and _rsa_bits($p)
!ll&&!rl&&r
000
MC/DC100%2/2
$k eq "rsa" and _rsa_bits($p)
$k eq "rsa" _rsa_bits($p)
MC/DC0%0/2
$k eq "rsa" and _rsa_bits($p)
$k eq "rsa" _rsa_bits($p)
Truth table100%3/3
A and B
A= $k eq "rsa"B= _rsa_bits($p)
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $k eq "rsa"B= _rsa_bits($p)
ABresult
0X0
100
111
150 # -09 §14.5 (contract D7): base64 that is not a usable key of its type.
151 0 ▶ return (undef, $k eq 'rsa' || $k eq 'ed25519' ? 'is malformed' : 'has an unsupported key type');
Branch: 2 branches
BranchTrueFalseTotal
$k eq 'rsa' || $k eq 'ed25519' ? : 8 3 11
$k eq 'rsa' || $k eq 'ed25519' ? : 0 0 0
Condition50%3/6
$k eq "rsa" or $k eq "ed25519"
l!l&&r!l&&!r
263
$k eq "rsa" or $k eq "ed25519"
l!l&&r!l&&!r
000
MC/DC100%2/2
$k eq "rsa" or $k eq "ed25519"
$k eq "rsa" $k eq "ed25519"
MC/DC0%0/2
$k eq "rsa" or $k eq "ed25519"
$k eq "rsa" $k eq "ed25519"
Truth table100%3/3
A or B
A= $k eq "rsa"B= $k eq "ed25519"
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $k eq "rsa"B= $k eq "ed25519"
ABresult
1X1
011
000
152 }
153  
154 # A TXT record as a DNS tag=value list (DKIMKEYS -03 section 2.6: the
155 # Tag=Value rules of the spec's section 8): (\%tags, \@names_in_order), or
156 # the empty list for a syntax error -- a repeated tag, a spec that is not
157 # name=value, a value outside the tag-list grammar, or no tag at all. Tag
158 # names are case sensitive.
159 777 ▶sub _parse_dns_tag_list {
Subroutine _parse_dns_tag_list: called CC=8 SCAR=28.3
160 0 ▶ my ($txt) = @_;
Subroutine _parse_dns_tag_list: not called CC=8 SCAR=28.3
161 0 ▶ return unless defined $txt;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $txt 0 777 777
unless defined $txt 0 0 0
162 0 my (%tag, @order);
163 0 for my $spec (split /;/, $txt, -1) {
164 0 ▶ next if $spec =~ /\A[ \t\r\n]*\z/; # empty spec, e.g. after a trailing ";"
Branch: 2 branches
BranchTrueFalseTotal
if $spec =~ /\A[ \t\r\n]*\z/u 2 2307 2309
if $spec =~ /\A[ \t\r\n]*\z/u 0 0 0
165 0 ▶ my ($name, $value) = $spec =~ /\A[ \t\r\n]*([A-Za-z][A-Za-z0-9_]*)[ \t\r\n]*=(.*)\z/s
Branch: 2 branches
BranchTrueFalseTotal
() = $spec =~ /\A[ \t\r\n]*([A-Za-z][A-Za-z0-9_]*)[ \t\r\n]*=(.*)\z/su or return 2305 2 2307
() = $spec =~ /\A[ \t\r\n]*([A-Za-z][A-Za-z0-9_]*)[ \t\r\n]*=(.*)\z/su or return 0 0 0
166 or return;
167 0 ▶ return if exists $tag{$name};
Branch: 2 branches
BranchTrueFalseTotal
if exists $tag{$name} 6 2299 2305
if exists $tag{$name} 0 0 0
168 0 $value = _trim_wsp($value);
169 # Every value, unknown tags' included, must fit the tag-list grammar
170 # (RFC 6376 §3.2 VALCHAR): ignoring a tag comes after parsing it
171 # (follow-up review F5).
172 0 ▶ return unless $value =~ /\A$Mail::DKIM2::TagValueList::TAG_VALUE\z/;
Branch: 2 branches
BranchTrueFalseTotal
unless $value =~ /\A$Mail::DKIM2::TagValueList::TAG_VALUE\z/u 3 2296 2299
unless $value =~ /\A$Mail::DKIM2::TagValueList::TAG_VALUE\z/u 0 0 0
173 0 $tag{$name} = $value;
174 0 push @order, $name;
175 }
176 0 ▶ return unless @order;
Branch: 2 branches
BranchTrueFalseTotal
unless @order 1 765 766
unless @order 0 0 0
177 0 return (\%tag, \@order);
178 }
179  
180 739 ▶sub _key_from_p {
Subroutine _key_from_p: called CC=8 SCAR=27.7
181 0 ▶ my ($k, $p) = @_;
Subroutine _key_from_p: not called CC=8 SCAR=27.7
182 0 ▶ if ($k eq 'ed25519') {
Branch: 2 branches
BranchTrueFalseTotal
if ($k eq "ed25519") 14 725 739
if ($k eq "ed25519") 0 0 0
183 # -09 §14.5: an Ed25519 key is exactly 32 octets (RFC 8032), the
184 # raw key; anything else -- a DER SubjectPublicKeyInfo included --
185 # is malformed. Never die on one: return undef so the verifier
186 # reports a clean permerror instead of aborting the whole operation
187 # (the RSA branch below is likewise eval-wrapped).
188 0 my $raw = decode_base64($p);
189 0 ▶ return unless length($raw) == 32;
Branch: 2 branches
BranchTrueFalseTotal
unless length $raw == 32 6 8 14
unless length $raw == 32 0 0 0
190 0 my $key = eval {
191 0 my $pk = Crypt::PK::Ed25519->new();
192 0 $pk->import_key_raw($raw, 'public');
193 0 $pk;
194 };
195 0 ▶ die $@ if ref $@;
Branch: 2 branches
BranchTrueFalseTotal
if ref $@ 1 7 8
if ref $@ 0 0 0
196 0 return $key;
197 }
198 # Any other key type is unrecognised, and ignored: never read as RSA.
199 0 ▶ return unless $k eq 'rsa';
Branch: 2 branches
BranchTrueFalseTotal
unless $k eq "rsa" 3 722 725
unless $k eq "rsa" 0 0 0
200 # RSA: p= is base64-encoded SubjectPublicKeyInfo DER. A key under
201 # 1024 bits is too short: -09 section 3.2 leaves it to the Verifier, and
202 # we refuse it (contract E5).
203 0 my $der = decode_base64($p);
204 0 my $rsa = eval { Crypt::PK::RSA->new(\$der) };
205 0 ▶ die $@ if ref $@;
Branch: 2 branches
BranchTrueFalseTotal
if ref $@ 1 721 722
if ref $@ 0 0 0
206 0 ▶ return unless $rsa && $rsa->size * 8 >= 1024;
Branch: 2 branches
BranchTrueFalseTotal
unless $rsa and $rsa->size * 8 >= 1024 5 716 721
unless $rsa and $rsa->size * 8 >= 1024 0 0 0
Condition50%3/6
$rsa and $rsa->size * 8 >= 1024
!ll&&!rl&&r
23716
$rsa and $rsa->size * 8 >= 1024
!ll&&!rl&&r
000
MC/DC100%2/2
$rsa and $rsa->size * 8 >= 1024
$rsa $rsa->size * 8 >= 1024
MC/DC0%0/2
$rsa and $rsa->size * 8 >= 1024
$rsa $rsa->size * 8 >= 1024
Truth table100%3/3
A and B
A= $rsaB= $rsa->size * 8 >= 1024
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $rsaB= $rsa->size * 8 >= 1024
ABresult
0X0
100
111
207 0 return $rsa;
208 }
209  
210 # True when $p (base64) loads as an RSA key, of whatever size: tells a key
211 # that is too short from one that is malformed.
212 5 ▶sub _rsa_bits {
Subroutine _rsa_bits: called CC=3 SCAR=15.0
213 0 ▶ my ($p) = @_;
Subroutine _rsa_bits: not called CC=3 SCAR=15.0
214 0 my $der = decode_base64($p);
215 0 my $rsa = eval { Crypt::PK::RSA->new(\$der) };
216 0 ▶ die $@ if ref $@;
Branch: 2 branches
BranchTrueFalseTotal
if ref $@ 0 5 5
if ref $@ 0 0 0
217 0 ▶ return $rsa ? $rsa->size * 8 : 0;
Branch: 2 branches
BranchTrueFalseTotal
$rsa ? : 3 2 5
$rsa ? : 0 0 0
218 }
219  
220 # key_records(@txt): the TXT records at a selector that are candidate key
221 # records (-09 section 3.7, DKIMKEYS -03 section 3): each one but those that
222 # begin with a "v=" tag whose value is not DKIM1. More than one left is the
223 # caller's "has multiple records".
224 112 ▶sub key_records {
Subroutine key_records: called CC=2 SCAR=9.2
Pod documented
225 0 ▶ return grep { my $v = _leading_v($_); !defined $v || $v eq 'DKIM1' } @_;
Subroutine key_records: not called CC=2 SCAR=9.2
Pod documented
Condition50%3/6
!defined($v) || $v eq 'DKIM1'
l!l&&r!l&&!r
11142
!defined($v) || $v eq 'DKIM1'
l!l&&r!l&&!r
000
MC/DC100%2/2
!defined($v) || $v eq 'DKIM1'
!defined($v) $v eq 'DKIM1'
MC/DC0%0/2
!defined($v) || $v eq 'DKIM1'
!defined($v) $v eq 'DKIM1'
Truth table100%3/3
not(A) || B
A= !defined($v)B= $v eq 'DKIM1'
ABresult
1X1
011
000
Truth table0%0/3
not(A) || B
A= !defined($v)B= $v eq 'DKIM1'
ABresult
1X1
011
000
226 }
227  
228 # The value of a TXT record's first tag when that tag is "v", else undef:
229 # read without parsing the rest, since -09 section 3.7 sorts records on it
230 # before any is used.
231 145 ▶sub _leading_v {
Subroutine _leading_v: called CC=3 SCAR=15.3
232 0 ▶ my ($txt) = @_;
Subroutine _leading_v: not called CC=3 SCAR=15.3
233 0 ▶ return unless defined $txt;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $txt 0 145 145
unless defined $txt 0 0 0
234 0 ▶ $txt =~ /\A[ \t\r\n]*v[ \t\r\n]*=[ \t\r\n]*([^;]*?)[ \t\r\n]*(?:;|\z)/ or return;
Branch: 2 branches
BranchTrueFalseTotal
$txt =~ /\A[ \t\r\n]*v[ \t\r\n]*=[ \t\r\n]*([^;]*?)[ \t\r\n]*(?:;|\z)/u or return 141 4 145
$txt =~ /\A[ \t\r\n]*v[ \t\r\n]*=[ \t\r\n]*([^;]*?)[ \t\r\n]*(?:;|\z)/u or return 0 0 0
235 0 return $1;
236 }
237  
238 # $s without leading or trailing WSP, in linear time: a TXT record is
239 # whatever the signer's DNS serves (up to 64 KB), and a trailing-WSP match
240 # tried at every position of a long inner run of spaces is quadratic. The
241 # lookbehind starts it only at the beginning of a run.
242 2312 ▶sub _trim_wsp {
Subroutine _trim_wsp: called CC=1 SCAR=1.2
243 0 ▶ my ($s) = @_;
Subroutine _trim_wsp: not called CC=1 SCAR=1.2
244 0 $s =~ s/\A[ \t\r\n]+//;
245 0 $s =~ s/(?<![ \t\r\n])[ \t\r\n]+\z//;
246 0 return $s;
247 }
248  
249 # domain_policy(@txt): the Domain Policy Record held by the TXT records at
250 # _domainkey.<domain> (-09 section 3.8, DKIMKEYS -03 section 6): 'strict'
251 # when subdomains are not authorized to make that domain's From Authentic,
252 # 'open' when they are (no record, no f=, or no "strict" flag), or
253 # 'multiple' for more than one record. Only records beginning with the tag
254 # v=DKIM1 count. A record that does so but does not parse is 'strict': the
255 # spec gives it no outcome, so it fails closed (contract E3.2).
256 29 ▶sub domain_policy {
Subroutine domain_policy: called CC=9 SCAR=30.0
Pod documented
257 0 ▶ my @dpr = grep { (_leading_v($_) // '') eq 'DKIM1' } @_;
Subroutine domain_policy: not called CC=9 SCAR=30.0
Pod documented
Condition50%2/4
_leading_v($_) // ""
l!l
253
_leading_v($_) // ""
l!l
00
MC/DC100%1/1
_leading_v($_) // ""
_leading_v($_)
MC/DC0%0/1
_leading_v($_) // ""
_leading_v($_)
Truth table100%2/2
A
A= _leading_v($_)
Aresult
11
00
Truth table0%0/2
A
A= _leading_v($_)
Aresult
11
00
258 0 ▶ return 'open' unless @dpr;
Branch: 2 branches
BranchTrueFalseTotal
unless @dpr 11 18 29
unless @dpr 0 0 0
259 0 ▶ return 'multiple' if @dpr > 1;
Branch: 2 branches
BranchTrueFalseTotal
if @dpr > 1 2 16 18
if @dpr > 1 0 0 0
260 0 ▶ my ($tag) = _parse_dns_tag_list($dpr[0]) or return 'strict';
Branch: 2 branches
BranchTrueFalseTotal
my($tag) = _parse_dns_tag_list($dpr[0]) or return "strict" 14 2 16
my($tag) = _parse_dns_tag_list($dpr[0]) or return "strict" 0 0 0
261 0 my $f = $tag->{f};
262 0 ▶ return 'open' unless defined $f && length $f;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $f and length $f 2 12 14
unless defined $f and length $f 0 0 0
Condition50%3/6
defined $f and length $f
!ll&&!rl&&r
1112
defined $f and length $f
!ll&&!rl&&r
000
MC/DC100%2/2
defined $f and length $f
defined $f length $f
MC/DC0%0/2
defined $f and length $f
defined $f length $f
Truth table100%3/3
A and B
A= defined $fB= length $f
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $fB= length $f
ABresult
0X0
100
111
263 0 my @flags = map { _trim_wsp($_) } split /:/, $f, -1;
264 0 ▶ return 'strict' if grep { !/\A[A-Za-z](?:[A-Za-z0-9-]*[A-Za-z0-9])?\z/ } @flags;
Branch: 2 branches
BranchTrueFalseTotal
if grep {not /\A[A-Za-z](?:[A-Za-z0-9-]*[A-Za-z0-9])?\z/u;} @flags 0 12 12
if grep {not /\A[A-Za-z](?:[A-Za-z0-9-]*[A-Za-z0-9])?\z/u;} @flags 0 0 0
265 0 ▶ return (grep { $_ eq 'strict' } @flags) ? 'strict' : 'open';
Branch: 2 branches
BranchTrueFalseTotal
grep({$_ eq 'strict';} @flags) ? : 10 2 12
grep({$_ eq 'strict';} @flags) ? : 0 0 0
266 }
267  
268 # query_domain_policy($resolver, $domain): the TXT records at
269 # _domainkey.$domain (each RR's strings joined) through $resolver's query(),
270 # as an arrayref; [] for NXDOMAIN or no TXT. Dies "TEMPERROR: ..." for any
271 # other failure.
272 2 ▶sub query_domain_policy {
Subroutine query_domain_policy: called CC=4 SCAR=19.9
Pod documented
273 0 ▶ my ($resolver, $dom) = @_;
Subroutine query_domain_policy: not called CC=4 SCAR=19.9
Pod documented
274 0 my $fqdn = "_domainkey.$dom";
275 0 my $reply = $resolver->query($fqdn, 'TXT');
276 0 ▶ unless ($reply) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($reply) 1 1 2
unless ($reply) 0 0 0
277 0 ▶ my $err = $resolver->errorstring // '';
Condition25%1/4
$resolver->errorstring // ""
l!l
10
$resolver->errorstring // ""
l!l
00
MC/DC0%0/1
$resolver->errorstring // ""
$resolver->errorstring
MC/DC0%0/1
$resolver->errorstring // ""
$resolver->errorstring
Truth table50%1/2
A
A= $resolver->errorstring
Aresult
11
00
Truth table0%0/2
A
A= $resolver->errorstring
Aresult
11
00
278 0 ▶ return [] if $err =~ /^(?:NXDOMAIN|NOERROR|NODATA)\s*$/i;
Branch: 2 branches
BranchTrueFalseTotal
if $err =~ /^(?:NXDOMAIN|NOERROR|NODATA)\s*$/iu 1 0 1
if $err =~ /^(?:NXDOMAIN|NOERROR|NODATA)\s*$/iu 0 0 0
279 0 die "TEMPERROR: DNS lookup for $fqdn failed: $err\n";
280 }
281 0 return [ map { join '', $_->txtdata } grep { $_->type eq 'TXT' } $reply->answer ];
282 }
283  
284 # policy_from_source($dom, $callback, $resolver_sub, @cb_args): the Domain
285 # Policy Record of $dom ('open', 'strict', 'multiple' or 'temperror') from
286 # $callback->($dom, @cb_args) when given, else query_domain_policy through
287 # $resolver_sub->(); a die (a TEMPERROR) is 'temperror', with the reason
288 # returned second.
289 20 ▶sub policy_from_source {
Subroutine policy_from_source: called CC=5 SCAR=22.4
Pod documented
290 0 ▶ my ($dom, $callback, $resolver_sub, @cb_args) = @_;
Subroutine policy_from_source: not called CC=5 SCAR=22.4
Pod documented
291 0 my $txt = eval {
292 0 ▶ $callback ? $callback->($dom, @cb_args) : query_domain_policy($resolver_sub->(), $dom);
Branch: 2 branches
BranchTrueFalseTotal
$callback ? : 18 2 20
$callback ? : 0 0 0
293 };
294 0 ▶ if (my $err = $@) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $err = $@) 2 18 20
if (my $err = $@) 0 0 0
295 0 ▶ die $err if ref $err;
Branch: 2 branches
BranchTrueFalseTotal
if ref $err 0 2 2
if ref $err 0 0 0
296 0 (my $why = $err) =~ s/\s+at\s+\S+\s+line\s+\d+\.?\s*\z//;
297 0 return ('temperror', $why =~ s/\s+\z//r);
298 }
299 0 ▶ return domain_policy(@{ $txt || [] });
Condition25%1/4
$txt || []
l!l
180
$txt || []
l!l
00
MC/DC0%0/1
$txt || []
$txt
MC/DC0%0/1
$txt || []
$txt
Truth table50%1/2
A
A= $txt
Aresult
11
00
Truth table0%0/2
A
A= $txt
Aresult
11
00
300 }
301  
302 222 1;
303  
304 __END__
« lib/Mail/DKIM2/Gate.pm ↑ Summary lib/Mail/DKIM2/HeaderPlan.pm »