DKIM/PublicKey.pm

filter: Statementstmt 81.7% 193 / 236 Branchbran 66.9% 75 / 112 Conditioncond 80.0% 32 / 40 MC/DCmcdc 60.8% 14 / 23 Subroutinesub 83.3% 25 / 30 Podpod 11.7% 2 / 17 totaltotal 74.4% 341 / 458 CC 73 SCAR 20.4
CC
73
Cov
77%
check
16.7
check_granularity
13.3
fetch_async
9.6
CRAP 7.7
« DKIM/KeyValueList.pm ↑ Summary DKIM/Canonicalization/DkCommon.pm »
1 package Mail::DKIM::PublicKey;
2 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 our $VERSION = '1.20240923'; # VERSION
5 # ABSTRACT: Represents a DKIM key
6  
7 # Copyright 2005 Messiah College. All rights reserved.
8 # Jason Long <jlong@messiah.edu>
9  
10 # Copyright (c) 2004 Anthony D. Urso. All rights reserved.
11 # This program is free software; you can redistribute it and/or
12 # modify it under the same terms as Perl itself.
13  
14 15 ▶use base ( 'Mail::DKIM::KeyValueList', 'Mail::DKIM::Key' );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 15 *calculate_EM = \&Mail::DKIM::Key::calculate_EM;
16  
17 15 ▶use Crypt::OpenSSL::RSA;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 15 ▶use Crypt::PK::Ed25519;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 15 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 15 ▶use Mail::DKIM::DNS;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 364 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
23 364 my $type = shift;
24 364 my %prms = @_;
25  
26 364 my $self = {};
27  
28 364 $self->{'GRAN'} = $prms{'Granularity'};
29 364 $self->{'NOTE'} = $prms{'Note'};
30 364 $self->{'TEST'} = $prms{'Testing'};
31 364 ▶ $self->{'TYPE'} = ( $prms{'Type'} or 'rsa' );
Condition50%1/2
$prms{'Type'} || 'rsa'
l!l
0364
MC/DC0%0/1
$prms{'Type'} || 'rsa'
$prms{'Type'}
Truth table50%1/2
A
A= $prms{'Type'}
Aresult
11
00
32 364 $self->{'DATA'} = $prms{'Data'};
33  
34 364 bless $self, $type;
35 }
36  
37  
38 2 ▶sub fetch {
Subroutine fetch: called CC=1 SCAR=0.0
Pod documented
39 2 my $class = shift;
40 2 my $waiter = $class->fetch_async(@_);
41 2 my $self = $waiter->();
42 2 return $self;
43 }
44  
45 # fetch_async() - asynchronously tries fetching a specific public key
46 # using a specific protocol.
47 #
48 # Usage:
49 # my $fut = Mail::DKIM::PublicKey->fetch_async(
50 # Protocol => 'dns/txt',
51 # Selector => 'selector1',
52 # Domain => 'example.org',
53 # Callbacks => { ... }, #optional
54 # );
55 #
56 # # some later time
57 # my $pubkey = $fut->(); # blocks until the public key is returned
58 #
59 426 ▶sub fetch_async {
Subroutine fetch_async: called CC=9 SCAR=22.7
Pod undocumented
60 426 my $class = shift;
61 426 my %prms = @_;
62  
63 426 ▶ defined( $prms{Protocol} ) && $prms{Protocol} =~ m{^dns(/txt)?$}s
Branch: 1 branch
BranchTrueFalseTotal
defined $prms{"Protocol"} and $prms{"Protocol"} =~ m[^dns(/txt)?$]s or die "invalid/missing Protocol\n" 426 0 426
Condition33%1/3
defined $prms{"Protocol"} and $prms{"Protocol"} =~ m[^dns(/txt)?$]s
!ll&&!rl&&r
00426
MC/DC0%0/2
defined $prms{"Protocol"} and $prms{"Protocol"} =~ m[^dns(/txt)?$]s
defined $prms{"Protocol"} $prms{"Protocol"} =~ m[^dns(/txt)?$]s
Truth table33%1/3
A and B
A= defined $prms{"Protocol"}B= $prms{"Protocol"} =~ m[^dns(/txt)?$]s
ABresult
0X0
100
111
64 or die "invalid/missing Protocol\n";
65  
66 defined( $prms{Selector} ) && length( $prms{Selector} )
67 426 ▶ or die "invalid/missing Selector\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $prms{"Selector"} and length $prms{"Selector"} or die "invalid/missing Selector\n" 424 2 426
Condition66%2/3
defined $prms{"Selector"} and length $prms{"Selector"}
!ll&&!rl&&r
02424
MC/DC50%1/2
defined $prms{"Selector"} and length $prms{"Selector"}
defined $prms{"Selector"} length $prms{"Selector"}
Truth table66%2/3
A and B
A= defined $prms{"Selector"}B= length $prms{"Selector"}
ABresult
0X0
100
111
68  
69 defined( $prms{Domain} ) && length( $prms{Domain} )
70 424 ▶ or die "invalid/missing Domain\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $prms{"Domain"} and length $prms{"Domain"} or die "invalid/missing Domain\n" 424 0 424
Condition33%1/3
defined $prms{"Domain"} and length $prms{"Domain"}
!ll&&!rl&&r
00424
MC/DC0%0/2
defined $prms{"Domain"} and length $prms{"Domain"}
defined $prms{"Domain"} length $prms{"Domain"}
Truth table33%1/3
A and B
A= defined $prms{"Domain"}B= length $prms{"Domain"}
ABresult
0X0
100
111
71  
72 424 ▶ my %callbacks = %{ $prms{Callbacks} || {} };
Condition100%2/2
$prms{'Callbacks'} || {}
l!l
4222
MC/DC100%1/1
$prms{'Callbacks'} || {}
$prms{'Callbacks'}
Truth table100%2/2
A
A= $prms{'Callbacks'}
Aresult
11
00
73 424 ▶ my $on_success = $callbacks{Success} || sub { $_[0] };
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$callbacks{'Success'} || sub { $_[0]; }
l!l&&r!l&&!r
42220
MC/DC0%0/2
$callbacks{'Success'} || sub { $_[0]; }
$callbacks{'Success'} sub { $_[0]; }
Truth table66%2/3
A || B
A= $callbacks{'Success'}B= sub { $_[0]; }
ABresult
1X1
011
000
74 369 ▶ $callbacks{Success} = sub {
Subroutine __ANON__: called CC=7 SCAR=19.7
75 369 my @resp = @_;
76 369 ▶ unless (@resp) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@resp) 5 364 369
77  
78 # no requested resource records or NXDOMAIN,
79 5 return $on_success->();
80 }
81  
82 364 my $strn;
83 364 foreach my $rr (@resp) {
84 364 ▶ next unless $rr->type eq 'TXT';
Branch: 1 branch
BranchTrueFalseTotal
unless $rr->type eq "TXT" 0 364 364
85  
86 # join with no intervening spaces, RFC 6376
87 364 ▶ if ( $rr->can('txtdata') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rr->can('txtdata')) { } 364 0 364
88  
89 # must call txtdata() in a list context
90 364 $strn = join '', $rr->txtdata;
91 }
92 else {
93 # char_str_list method is 'historical'
94 0 $strn = join '', $rr->char_str_list;
95 }
96 364 last;
97 }
98  
99 $strn
100 364 ▶ or return $on_success->();
Branch: 1 branch
BranchTrueFalseTotal
$strn or return &$on_success() 364 0 364
101  
102 364 my $self = $class->parse($strn);
103 361 $self->{Selector} = $prms{'Selector'};
104 361 $self->{Domain} = $prms{'Domain'};
105 361 ▶ $self->{TYPE} = $self->get_tag('k') || 'rsa';
Condition100%2/2
$self->get_tag('k') || 'rsa'
l!l
33526
MC/DC100%1/1
$self->get_tag('k') || 'rsa'
$self->get_tag('k')
Truth table100%2/2
A
A= $self->get_tag('k')
Aresult
11
00
106 361 $self->check;
107  
108 356 return $on_success->($self);
109 424 };
110  
111 #
112 # perform DNS query for public key...
113 #
114 424 my $host = $prms{Selector} . '._domainkey.' . $prms{Domain};
115 424 my $waiter =
116 Mail::DKIM::DNS::query_async( $host, 'TXT', Callbacks => \%callbacks, );
117 424 return $waiter;
118 }
119  
120  
121 # check syntax of the public key
122 # throw an error if any errors are detected
123 361 ▶sub check {
Subroutine check: called CC=16 SCAR=28.2
Pod undocumented
124 361 my $self = shift;
125  
126 # check public key version tag
127 361 ▶ if ( my $v = $self->get_tag('v') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $v = $self->get_tag("v")) 340 21 361
128 340 ▶ unless ( $v eq 'DKIM1' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($v eq "DKIM1") 1 339 340
129 1 die "unsupported version\n";
130 }
131 }
132  
133 # check public key granularity
134 360 my $g = $self->granularity;
135  
136 # check key type
137 360 ▶ if ( my $k = $self->get_tag('k') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $k = $self->get_tag("k")) 335 25 360
138 335 ▶ unless ( $k eq 'rsa' || $k eq 'ed25519' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($k eq "rsa" or $k eq "ed25519") 1 334 335
Condition100%3/3
$k eq "rsa" or $k eq "ed25519"
l!l&&r!l&&!r
33041
MC/DC100%2/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
139 1 die "unsupported key type\n";
140 }
141 }
142  
143 # check public-key data
144 359 my $p = $self->data;
145 359 ▶ if ( not defined $p ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $p) 0 359 359
146 0 die "missing p= tag\n";
147 }
148 359 ▶ if ( $p eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p eq "") 1 358 359
149 1 die "revoked\n";
150 }
151 358 ▶ unless ( $p =~ /^[A-Za-z0-9\+\/\=]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($p =~ m[^[A-Za-z0-9\+/\=]+$]) 0 358 358
152 0 die "invalid data\n";
153 }
154  
155 # have OpenSSL load the key
156 eval {
157 358 local $SIG{__DIE__};
158 358 $self->cork;
159 357 1
160 358 ▶ } || do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { local $SIG{"__DIE__"}; $self->cork; 1 } } or do { chomp(my $E = $@); if ($E =~ /(OpenSSL error: .*?) at /) { $E = "$1"; } elsif ($E =~ /^(panic:.*?) at /) { $E = "OpenSSL $1"; } elsif ($E =~ /^FATAL: (.*) at /) { $E = "Ed25519 $1"; } ; die "$E\n" } 357 1 358
161  
162 # see also finish_body
163 1 chomp( my $E = $@ );
164 1 ▶ if ( $E =~ /(OpenSSL error: .*?) at / ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($E =~ /(OpenSSL error: .*?) at /) { } 0 1 1
elsif ($E =~ /^(panic:.*?) at /) { } 0 1 1
elsif ($E =~ /^FATAL: (.*) at /) { } 1 0 1
165 0 $E = "$1";
166 }
167 elsif ( $E =~ /^(panic:.*?) at / ) {
168 0 $E = "OpenSSL $1";
169 }
170 elsif ( $E =~ /^FATAL: (.*) at / ) {
171 1 $E = "Ed25519 $1";
172 }
173 1 die "$E\n";
174 };
175  
176 # check service type
177 357 ▶ if ( my $s = $self->get_tag('s') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $s = $self->get_tag("s")) 22 335 357
178 22 my @list = split( /:/, $s );
179 22 ▶ unless ( grep { $_ eq '*' || $_ eq 'email' } @list ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq "*" or $_ eq "email";} @list) 1 21 22
Condition100%3/3
$_ eq '*' || $_ eq 'email'
l!l&&r!l&&!r
11105
MC/DC100%2/2
$_ eq '*' || $_ eq 'email'
$_ eq '*' $_ eq 'email'
Truth table100%3/3
A || B
A= $_ eq '*'B= $_ eq 'email'
ABresult
1X1
011
000
180 1 die "does not support email\n";
181 }
182 }
183  
184 356 return 1;
185 }
186  
187 # check_granularity() - check whether this key matches signature identity
188 #
189 # a public key record can restrict what identities it may sign with,
190 # g=, granularity, restricts the local part of the identity
191 # t=s, restricts whether subdomains can be used
192 #
193 # This method returns true if the given identity is allowed by this
194 # public key; it returns false otherwise.
195 # If false is returned, you can check C<$@> for an explanation of
196 # why.
197 #
198 353 ▶sub check_granularity {
Subroutine check_granularity: called CC=13 SCAR=25.9
Pod undocumented
199 353 my $self = shift;
200 353 my ( $identity, $empty_g_means_wildcard ) = @_;
201  
202 # check granularity
203 353 my $g = $self->granularity;
204  
205 # yuck- what is this $empty_g_means_wildcard parameter?
206 # well, it turns out that with DomainKeys signatures,
207 # an empty g= is the same as g=*
208 353 ▶ if ( $g eq '' && $empty_g_means_wildcard ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($g eq "" and $empty_g_means_wildcard) 1 352 353
Condition100%3/3
$g eq "" and $empty_g_means_wildcard
!ll&&!rl&&r
35111
MC/DC100%2/2
$g eq "" and $empty_g_means_wildcard
$g eq "" $empty_g_means_wildcard
Truth table100%3/3
A and B
A= $g eq ""B= $empty_g_means_wildcard
ABresult
0X0
100
111
209 1 $g = '*';
210 }
211  
212 # split i= value into a "local part" and a "domain part"
213 353 my ( $local_part, $domain_part );
214 353 ▶ if ( $identity =~ /^(.*)\@([^@]*)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($identity =~ /^(.*)\@([^\@]*)$/) { } 58 295 353
215 58 $local_part = $1;
216 58 $domain_part = $2;
217 }
218 else {
219 295 $local_part = '';
220 295 $domain_part = $identity;
221 }
222  
223 353 my ( $begins, $ends ) = split /\*/, $g, 2;
224 353 ▶ if ( defined $ends ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ends) { } 352 1 353
225  
226 # the g= tag contains an asterisk
227  
228 # the local part must be at least as long as the pattern
229 352 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (length $local_part < length($begins) + length($ends) or substr($local_part, 0, length $begins) ne $begins or length $ends and substr($local_part, -length($ends)) ne $ends) 3 349 352
Condition88%8/9
length $local_part < length($begins) + length($ends) or substr($local_part, 0, length $begins) ne $begins or length $ends and substr($local_part, -length($ends)) ne $ends
l!l&&r!l&&!r
21349
length $local_part < length($begins) + length($ends) or substr($local_part, 0, length $begins) ne $begins
l!l&&r!l&&!r
02350
length $ends and substr($local_part, -length($ends)) ne $ends
!ll&&!rl&&r
34361
MC/DC75%3/4
length $local_part < length($begins) + length($ends) or substr($local_part, 0, length $begins) ne $begins or length $ends and substr($local_part, -length($ends)) ne $ends
length $local_part < length($begins) + length($ends) substr($local_part, 0, length $begins) ne $begins length $ends substr($local_part, -length($ends)) ne $ends
Truth table80%4/5
A or B or C and D
A= length $local_part < length($begins) + length($ends)B= substr($local_part, 0, length $begins) ne $beginsC= length $endsD= substr($local_part, -length($ends)) ne $ends
ABCDresult
1XXX1
01XX1
00111
000X0
00100
230 length($local_part) < length($begins) + length($ends)
231 or
232  
233 # the local part must begin with $begins
234 substr( $local_part, 0, length($begins) ) ne $begins
235 or
236  
237 # the local part must end with $ends
238 ( length($ends) && substr( $local_part, -length($ends) ) ne $ends )
239 )
240 {
241 3 $@ = "granularity mismatch\n";
242 3 return;
243 }
244 }
245 else {
246 1 ▶ if ( $g eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($g eq "") 1 0 1
247 1 $@ = "granularity is empty\n";
248 1 return;
249 }
250 0 ▶ unless ( $local_part eq $begins ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($local_part eq $begins) 0 0 0
251 0 $@ = "granularity mismatch\n";
252 0 return;
253 }
254 }
255  
256 # check subdomains
257 349 ▶ if ( $self->subdomain_flag ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->subdomain_flag) 2 347 349
258 2 ▶ unless ( lc( $domain_part ) eq lc( $self->{'Domain'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (lc $domain_part eq lc $self->{"Domain"}) 1 1 2
259 1 $@ = "does not support signing subdomains\n";
260 1 return;
261 }
262 }
263  
264 348 return 1;
265 }
266  
267 # returns true if the actual hash algorithm used is listed by this
268 # public key; dies otherwise
269 #
270 355 ▶sub check_hash_algorithm {
Subroutine check_hash_algorithm: called CC=3 SCAR=11.0
Pod undocumented
271 355 my $self = shift;
272 355 my ($hash_algorithm) = @_;
273  
274 # check hash algorithm
275 355 ▶ if ( my $h = $self->get_tag('h') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $h = $self->get_tag("h")) 14 341 355
276 14 my @list = split( /:/, $h );
277 14 ▶ unless ( grep { $_ eq $hash_algorithm } @list ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $hash_algorithm;} @list) 2 12 14
278 2 die "does not support hash algorithm '$hash_algorithm'\n";
279 }
280 }
281 353 return 1;
282 }
283  
284 # Create an OpenSSL public key object from the Base64-encoded data
285 # found in this public key's DNS record. The OpenSSL object is saved
286 # in the "cork" property.
287 354 ▶sub _convert_rsa {
Subroutine _convert_rsa: called CC=3 SCAR=11.0
288 354 my $self = shift;
289 # have to PKCS1ify the pubkey because openssl is too finicky...
290 354 my $cert = "-----BEGIN PUBLIC KEY-----\n";
291  
292 354 ▶ for ( my $i = 0 ; $i < length $self->data ; $i += 64 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $self->data) 1359 354 1713
293 1359 $cert .= substr $self->data, $i, 64;
294 1359 $cert .= "\n";
295 }
296  
297 354 $cert .= "-----END PUBLIC KEY-----\n";
298  
299 354 ▶ my $cork = Crypt::OpenSSL::RSA->new_public_key($cert)
Branch: 1 branch
BranchTrueFalseTotal
my $cork = "Crypt::OpenSSL::RSA"->new_public_key($cert) or die "unable to generate public key object" 354 0 354
300 or die 'unable to generate public key object';
301  
302 # segfaults on my machine
303 # $cork->check_key or
304 # return;
305  
306 354 $self->cork($cork);
307 354 return 1;
308 }
309  
310 4 ▶sub _convert_ed25519 {
Subroutine _convert_ed25519: called CC=3 SCAR=11.2
311 4 my $self = shift;
312 4 ▶ my $cork = Crypt::PK::Ed25519->new
Branch: 1 branch
BranchTrueFalseTotal
my $cork = "Crypt::PK::Ed25519"->new or die "unable to generate Ed25519 public key object" 4 0 4
313 or die 'unable to generate Ed25519 public key object';
314  
315 4 my $keybin = decode_base64($self->data);
316 4 ▶ $cork->import_key_raw($keybin, 'public')
Branch: 1 branch
BranchTrueFalseTotal
$cork->import_key_raw($keybin, "public") or die "failed to load Ed25519 public key" 3 0 3
317 or die 'failed to load Ed25519 public key';
318  
319 3 $self->cork($cork);
320 3 return 1;
321 }
322  
323 358 ▶sub convert {
Subroutine convert: called CC=5 SCAR=16.5
Pod undocumented
324 358 my $self = shift;
325  
326 358 my $k_tag = $self->get_tag('k');
327 358 ▶ $k_tag = 'rsa' unless defined $k_tag;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k_tag 25 333 358
328  
329 358 ▶ $self->data
Branch: 1 branch
BranchTrueFalseTotal
$self->data or return 358 0 358
330 or return;
331  
332 358 ▶ return $self->_convert_rsa if $k_tag eq 'rsa';
Branch: 1 branch
BranchTrueFalseTotal
if $k_tag eq "rsa" 354 4 358
333 4 ▶ return $self->_convert_ed25519 if $k_tag eq 'ed25519';
Branch: 1 branch
BranchTrueFalseTotal
if $k_tag eq "ed25519" 4 0 4
334 0 die 'unsupported key type';
335 }
336  
337 0 ▶sub verify {
Subroutine verify: not called CC=2 SCAR=17.9
Pod undocumented
338 0 my $self = shift;
339 0 my %prms = @_;
340  
341 0 my $rtrn;
342  
343 eval {
344 0 local $SIG{__DIE__};
345 0 $rtrn = $self->cork->verify( $prms{'Text'}, $prms{'Signature'} );
346 0 1
347 0 ▶ } || do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { local $SIG{"__DIE__"}; $rtrn = $self->cork->verify($prms{"Text"}, $prms{"Signature"}); 1 } } or do { $self->errorstr($@); return } 0 0 0
348 0 $self->errorstr($@);
349 0 return;
350 };
351  
352 0 return $rtrn;
353 }
354  
355  
356 713 ▶sub granularity {
Subroutine granularity: called CC=3 SCAR=11.0
Pod documented
357 713 my $self = shift;
358  
359 # set new granularity if provided
360 713 ▶ (@_)
Branch: 1 branch
BranchTrueFalseTotal
@_ and $self->set_tag("g", shift()) 0 713 713
361 and $self->set_tag( 'g', shift );
362  
363 713 my $g = $self->get_tag('g');
364 713 ▶ if ( defined $g ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $g) { } 44 669 713
365 44 return $g;
366 }
367 else {
368 669 return '*';
369 }
370 }
371  
372 0 ▶sub notes {
Subroutine notes: not called CC=2 SCAR=17.9
Pod undocumented
373 0 my $self = shift;
374  
375 0 ▶ (@_)
Branch: 1 branch
BranchTrueFalseTotal
@_ and $self->set_tag("n", shift()) 0 0 0
376 and $self->set_tag( 'n', shift );
377  
378 0 return $self->get_tag('n');
379 }
380  
381 3793 ▶sub data {
Subroutine data: called CC=3 SCAR=11.3
Pod undocumented
382 3793 my $self = shift;
383  
384 3793 ▶ (@_)
Branch: 1 branch
BranchTrueFalseTotal
@_ and $self->set_tag("p", shift()) 0 3793 3793
385 and $self->set_tag( 'p', shift );
386  
387 3793 my $p = $self->get_tag('p');
388  
389 # remove whitespace (actually only LWSP is allowed) and double quote (long string delimiter)
390 3793 ▶ $p =~ tr/\015\012 \t"//d if defined $p;
Branch: 1 branch
BranchTrueFalseTotal
if defined $p 3793 0 3793
391 3793 return $p;
392 }
393  
394 349 ▶sub flags {
Subroutine flags: called CC=3 SCAR=11.1
Pod undocumented
395 349 my $self = shift;
396  
397 349 ▶ (@_)
Branch: 1 branch
BranchTrueFalseTotal
@_ and $self->set_tag("t", shift()) 0 349 349
398 and $self->set_tag( 't', shift );
399  
400 349 ▶ return $self->get_tag('t') || '';
Condition100%2/2
$self->get_tag('t') || ''
l!l
18331
MC/DC100%1/1
$self->get_tag('t') || ''
$self->get_tag('t')
Truth table100%2/2
A
A= $self->get_tag('t')
Aresult
11
00
401 }
402  
403 # subdomain_flag() - checks whether "s" is specified in flags
404 #
405 # returns true if "s" is found, false otherwise
406 #
407 349 ▶sub subdomain_flag {
Subroutine subdomain_flag: called CC=1 SCAR=0.0
Pod undocumented
408 349 my $self = shift;
409 349 my @flags = split /:/, $self->flags;
410 349 return grep { $_ eq 's' } @flags;
411 }
412  
413 0 ▶sub revoked {
Subroutine revoked: not called CC=2 SCAR=17.9
Pod undocumented
414 0 my $self = shift;
415  
416 0 ▶ $self->data
Branch: 1 branch
BranchTrueFalseTotal
$self->data or return 1 0 0 0
417 or return 1;
418  
419 0 return;
420 }
421  
422 0 ▶sub testing {
Subroutine testing: not called CC=2 SCAR=17.9
Pod undocumented
423 0 my $self = shift;
424  
425 0 my $flags = $self->flags;
426 0 my @flaglist = split( /:/, $flags );
427 0 ▶ if ( grep { $_ eq 'y' } @flaglist ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq "y";} @flaglist) 0 0 0
428 0 return 1;
429 }
430 0 return undef;
431 }
432  
433 0 ▶sub verify_sha1_digest {
Subroutine verify_sha1_digest: not called CC=1 SCAR=6.9
Pod undocumented
434 0 my $self = shift;
435 0 my ( $digest, $signature ) = @_;
436 0 return $self->verify_digest( 'SHA-1', $digest, $signature );
437 }
438  
439 342 ▶sub _verify_digest_rsa {
Subroutine _verify_digest_rsa: called CC=5 SCAR=16.6
440 342 my $self = shift;
441 342 my ( $digest_algorithm, $digest, $signature ) = @_;
442  
443 342 my $rsa_pub = $self->cork;
444 342 ▶ if ( !$rsa_pub ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rsa_pub) 0 342 342
445 0 ▶ $@ = $@ ne '' ? "RSA failed: $@" : 'RSA unknown problem';
Branch: 1 branch
BranchTrueFalseTotal
$@ ne '' ? : 0 0 0
446 0 $@ .= ", s=$self->{Selector} d=$self->{Domain}";
447 0 return;
448 }
449  
450 342 $rsa_pub->use_no_padding;
451 342 my $verify_result = $rsa_pub->encrypt($signature);
452  
453 334 my $k = $rsa_pub->size;
454 334 my $expected = calculate_EM( $digest_algorithm, $digest, $k );
455 334 ▶ return 1 if ( $verify_result eq $expected );
Branch: 1 branch
BranchTrueFalseTotal
if $verify_result eq $expected 271 63 334
456  
457 # well, the RSA verification failed; I wonder if the RSA signing
458 # was performed on a different digest value? I think we can check...
459  
460 # basically, if the $verify_result has the same prefix as $expected,
461 # then only the digest was different
462  
463 63 my $digest_len = length $digest;
464 63 my $prefix_len = length($expected) - $digest_len;
465 63 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (substr($verify_result, 0, $prefix_len) eq substr($expected, 0, $prefix_len)) 60 3 63
466 substr( $verify_result, 0, $prefix_len ) eq
467 substr( $expected, 0, $prefix_len ) )
468 {
469 60 $@ = 'message has been altered';
470 60 return;
471 }
472  
473 3 $@ = 'bad RSA signature';
474 3 return;
475 }
476  
477 3 ▶sub _verify_digest_ed25519 {
Subroutine _verify_digest_ed25519: called CC=4 SCAR=15.5
478 3 my $self = shift;
479 3 my ( $digest_algorithm, $digest, $signature ) = @_;
480  
481 3 my $ed = $self->cork;
482 3 ▶ if ( !$ed ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ed) 0 3 3
483 0 ▶ $@ = $@ ne '' ? "Ed25519 failed: $@" : 'Ed25519 unknown problem';
Branch: 1 branch
BranchTrueFalseTotal
$@ ne '' ? : 0 0 0
484 0 $@ .= ", s=$self->{Selector} d=$self->{Domain}";
485 0 return;
486 }
487  
488 3 my $verify_result = $ed->verify_message($signature, $digest);
489 3 ▶ return $verify_result if ($verify_result == 1);
Branch: 1 branch
BranchTrueFalseTotal
if $verify_result == 1 2 1 3
490  
491 1 $@ = 'bad Ed25519 signature';
492 1 return;
493 }
494  
495 # verify_digest() - returns true if the digest verifies, false otherwise
496 #
497 # if false, $@ is set to a description of the problem
498 #
499 345 ▶sub verify_digest {
Subroutine verify_digest: called CC=4 SCAR=14.3
Pod undocumented
500 345 my $self = shift;
501 345 my ( $digest_algorithm, $digest, $signature ) = @_;
502  
503 345 ▶ my $k_tag = $self->get_tag('k') || 'rsa';
Condition100%2/2
$self->get_tag('k') || 'rsa'
l!l
32817
MC/DC100%1/1
$self->get_tag('k') || 'rsa'
$self->get_tag('k')
Truth table100%2/2
A
A= $self->get_tag('k')
Aresult
11
00
504  
505 345 ▶ return $self->_verify_digest_rsa($digest_algorithm, $digest, $signature) if $k_tag eq 'rsa';
Branch: 1 branch
BranchTrueFalseTotal
if $k_tag eq "rsa" 342 3 345
506 3 ▶ return $self->_verify_digest_ed25519($digest_algorithm, $digest, $signature) if $k_tag eq 'ed25519';
Branch: 1 branch
BranchTrueFalseTotal
if $k_tag eq "ed25519" 3 0 3
507 0 $@ = 'unsupported key type';
508 0 return;
509 }
510  
511 15 1;
512  
513 __END__
« DKIM/KeyValueList.pm ↑ Summary DKIM/Canonicalization/DkCommon.pm »