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 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 20 our $VERSION = '1.20261116'; # 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 20 ▶use base ( 'Mail::DKIM::KeyValueList', 'Mail::DKIM::Key' );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 20 *calculate_EM = \&Mail::DKIM::Key::calculate_EM;
16  
17 20 ▶use Crypt::OpenSSL::RSA;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 20 ▶use Crypt::PK::Ed25519;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 20 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 20 ▶use Mail::DKIM::DNS;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 358 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
23 358 my $type = shift;
24 358 my %prms = @_;
25  
26 358 my $self = {};
27  
28 358 $self->{'GRAN'} = $prms{'Granularity'};
29 358 $self->{'NOTE'} = $prms{'Note'};
30 358 $self->{'TEST'} = $prms{'Testing'};
31 358 ▶ $self->{'TYPE'} = ( $prms{'Type'} or 'rsa' );
Condition50%1/2
$prms{'Type'} || 'rsa'
l!l
0358
MC/DC0%0/1
$prms{'Type'} || 'rsa'
$prms{'Type'}
Truth table50%1/2
A
A= $prms{'Type'}
Aresult
11
00
32 358 $self->{'DATA'} = $prms{'Data'};
33  
34 358 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 586 ▶sub fetch_async {
Subroutine fetch_async: called CC=9 SCAR=22.7
Pod undocumented
60 586 my $class = shift;
61 586 my %prms = @_;
62  
63 586 ▶ 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" 586 0 586
Condition33%1/3
defined $prms{"Protocol"} and $prms{"Protocol"} =~ m[^dns(/txt)?$]s
!ll&&!rl&&r
00586
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 586 ▶ or die "invalid/missing Selector\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $prms{"Selector"} and length $prms{"Selector"} or die "invalid/missing Selector\n" 584 2 586
Condition66%2/3
defined $prms{"Selector"} and length $prms{"Selector"}
!ll&&!rl&&r
02584
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 584 ▶ or die "invalid/missing Domain\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $prms{"Domain"} and length $prms{"Domain"} or die "invalid/missing Domain\n" 584 0 584
Condition33%1/3
defined $prms{"Domain"} and length $prms{"Domain"}
!ll&&!rl&&r
00584
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 584 ▶ my %callbacks = %{ $prms{Callbacks} || {} };
Condition100%2/2
$prms{'Callbacks'} || {}
l!l
5822
MC/DC100%1/1
$prms{'Callbacks'} || {}
$prms{'Callbacks'}
Truth table100%2/2
A
A= $prms{'Callbacks'}
Aresult
11
00
73 584 ▶ 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
58220
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 488 ▶ $callbacks{Success} = sub {
Subroutine __ANON__: called CC=7 SCAR=19.7
75 488 my @resp = @_;
76 488 ▶ unless (@resp) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@resp) 131 357 488
77  
78 # no requested resource records or NXDOMAIN,
79 131 return $on_success->();
80 }
81  
82 357 my $strn;
83 357 foreach my $rr (@resp) {
84 357 ▶ next unless $rr->type eq 'TXT';
Branch: 1 branch
BranchTrueFalseTotal
unless $rr->type eq "TXT" 0 357 357
85  
86 # join with no intervening spaces, RFC 6376
87 357 ▶ if ( $rr->can('txtdata') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rr->can('txtdata')) { } 357 0 357
88  
89 # must call txtdata() in a list context
90 357 $strn = join '', $rr->txtdata;
91 }
92 else {
93 # char_str_list method is 'historical'
94 0 $strn = join '', $rr->char_str_list;
95 }
96 357 last;
97 }
98  
99 $strn
100 357 ▶ or return $on_success->();
Branch: 1 branch
BranchTrueFalseTotal
$strn or return &$on_success() 357 0 357
101  
102 357 my $self = $class->parse($strn);
103 354 $self->{Selector} = $prms{'Selector'};
104 354 $self->{Domain} = $prms{'Domain'};
105 354 ▶ $self->{TYPE} = $self->get_tag('k') || 'rsa';
Condition100%2/2
$self->get_tag('k') || 'rsa'
l!l
32826
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 354 $self->check;
107  
108 349 return $on_success->($self);
109 584 };
110  
111 #
112 # perform DNS query for public key...
113 #
114 584 my $host = $prms{Selector} . '._domainkey.' . $prms{Domain};
115 584 my $waiter =
116 Mail::DKIM::DNS::query_async( $host, 'TXT', Callbacks => \%callbacks, );
117 584 return $waiter;
118 }
119  
120  
121 # check syntax of the public key
122 # throw an error if any errors are detected
123 354 ▶sub check {
Subroutine check: called CC=16 SCAR=28.2
Pod undocumented
124 354 my $self = shift;
125  
126 # check public key version tag
127 354 ▶ if ( my $v = $self->get_tag('v') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $v = $self->get_tag("v")) 334 20 354
128 334 ▶ unless ( $v eq 'DKIM1' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($v eq "DKIM1") 1 333 334
129 1 die "unsupported version\n";
130 }
131 }
132  
133 # check public key granularity
134 353 my $g = $self->granularity;
135  
136 # check key type
137 353 ▶ if ( my $k = $self->get_tag('k') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $k = $self->get_tag("k")) 328 25 353
138 328 ▶ unless ( $k eq 'rsa' || $k eq 'ed25519' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($k eq "rsa" or $k eq "ed25519") 1 327 328
Condition100%3/3
$k eq "rsa" or $k eq "ed25519"
l!l&&r!l&&!r
32341
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 352 my $p = $self->data;
145 352 ▶ if ( not defined $p ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $p) 0 352 352
146 0 die "missing p= tag\n";
147 }
148 352 ▶ if ( $p eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p eq "") 1 351 352
149 1 die "revoked\n";
150 }
151 351 ▶ unless ( $p =~ /^[A-Za-z0-9\+\/\=]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($p =~ m[^[A-Za-z0-9\+/\=]+$]) 0 351 351
152 0 die "invalid data\n";
153 }
154  
155 # have OpenSSL load the key
156 eval {
157 351 local $SIG{__DIE__};
158 351 $self->cork;
159 350 1
160 351 ▶ } || 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" } 350 1 351
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 350 ▶ if ( my $s = $self->get_tag('s') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $s = $self->get_tag("s")) 22 328 350
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 349 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 346 ▶sub check_granularity {
Subroutine check_granularity: called CC=13 SCAR=25.9
Pod undocumented
199 346 my $self = shift;
200 346 my ( $identity, $empty_g_means_wildcard ) = @_;
201  
202 # check granularity
203 346 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 346 ▶ if ( $g eq '' && $empty_g_means_wildcard ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($g eq "" and $empty_g_means_wildcard) 1 345 346
Condition100%3/3
$g eq "" and $empty_g_means_wildcard
!ll&&!rl&&r
34411
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 346 my ( $local_part, $domain_part );
214 346 ▶ if ( $identity =~ /^(.*)\@([^@]*)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($identity =~ /^(.*)\@([^\@]*)$/) { } 61 285 346
215 61 $local_part = $1;
216 61 $domain_part = $2;
217 }
218 else {
219 285 $local_part = '';
220 285 $domain_part = $identity;
221 }
222  
223 346 my ( $begins, $ends ) = split /\*/, $g, 2;
224 346 ▶ if ( defined $ends ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ends) { } 345 1 346
225  
226 # the g= tag contains an asterisk
227  
228 # the local part must be at least as long as the pattern
229 345 ▶ 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 342 345
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
21342
length $local_part < length($begins) + length($ends) or substr($local_part, 0, length $begins) ne $begins
l!l&&r!l&&!r
02343
length $ends and substr($local_part, -length($ends)) ne $ends
!ll&&!rl&&r
33661
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 342 ▶ if ( $self->subdomain_flag ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->subdomain_flag) 2 340 342
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 341 return 1;
265 }
266  
267 # returns true if the actual hash algorithm used is listed by this
268 # public key; dies otherwise
269 #
270 348 ▶sub check_hash_algorithm {
Subroutine check_hash_algorithm: called CC=3 SCAR=11.0
Pod undocumented
271 348 my $self = shift;
272 348 my ($hash_algorithm) = @_;
273  
274 # check hash algorithm
275 348 ▶ if ( my $h = $self->get_tag('h') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $h = $self->get_tag("h")) 14 334 348
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 346 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 347 ▶sub _convert_rsa {
Subroutine _convert_rsa: called CC=3 SCAR=11.0
288 347 my $self = shift;
289 # have to PKCS1ify the pubkey because openssl is too finicky...
290 347 my $cert = "-----BEGIN PUBLIC KEY-----\n";
291  
292 347 ▶ for ( my $i = 0 ; $i < length $self->data ; $i += 64 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $self->data) 1331 347 1678
293 1331 $cert .= substr $self->data, $i, 64;
294 1331 $cert .= "\n";
295 }
296  
297 347 $cert .= "-----END PUBLIC KEY-----\n";
298  
299 347 ▶ 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" 347 0 347
300 or die 'unable to generate public key object';
301  
302 # segfaults on my machine
303 # $cork->check_key or
304 # return;
305  
306 347 $self->cork($cork);
307 347 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 351 ▶sub convert {
Subroutine convert: called CC=5 SCAR=16.5
Pod undocumented
324 351 my $self = shift;
325  
326 351 my $k_tag = $self->get_tag('k');
327 351 ▶ $k_tag = 'rsa' unless defined $k_tag;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k_tag 25 326 351
328  
329 351 ▶ $self->data
Branch: 1 branch
BranchTrueFalseTotal
$self->data or return 351 0 351
330 or return;
331  
332 351 ▶ return $self->_convert_rsa if $k_tag eq 'rsa';
Branch: 1 branch
BranchTrueFalseTotal
if $k_tag eq "rsa" 347 4 351
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 699 ▶sub granularity {
Subroutine granularity: called CC=3 SCAR=11.0
Pod documented
357 699 my $self = shift;
358  
359 # set new granularity if provided
360 699 ▶ (@_)
Branch: 1 branch
BranchTrueFalseTotal
@_ and $self->set_tag("g", shift()) 0 699 699
361 and $self->set_tag( 'g', shift );
362  
363 699 my $g = $self->get_tag('g');
364 699 ▶ if ( defined $g ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $g) { } 44 655 699
365 44 return $g;
366 }
367 else {
368 655 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 3716 ▶sub data {
Subroutine data: called CC=3 SCAR=11.3
Pod undocumented
382 3716 my $self = shift;
383  
384 3716 ▶ (@_)
Branch: 1 branch
BranchTrueFalseTotal
@_ and $self->set_tag("p", shift()) 0 3716 3716
385 and $self->set_tag( 'p', shift );
386  
387 3716 my $p = $self->get_tag('p');
388  
389 # remove whitespace (actually only LWSP is allowed) and double quote (long string delimiter)
390 3716 ▶ $p =~ tr/\015\012 \t"//d if defined $p;
Branch: 1 branch
BranchTrueFalseTotal
if defined $p 3716 0 3716
391 3716 return $p;
392 }
393  
394 342 ▶sub flags {
Subroutine flags: called CC=3 SCAR=11.1
Pod undocumented
395 342 my $self = shift;
396  
397 342 ▶ (@_)
Branch: 1 branch
BranchTrueFalseTotal
@_ and $self->set_tag("t", shift()) 0 342 342
398 and $self->set_tag( 't', shift );
399  
400 342 ▶ return $self->get_tag('t') || '';
Condition100%2/2
$self->get_tag('t') || ''
l!l
18324
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 342 ▶sub subdomain_flag {
Subroutine subdomain_flag: called CC=1 SCAR=0.0
Pod undocumented
408 342 my $self = shift;
409 342 my @flags = split /:/, $self->flags;
410 342 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 335 ▶sub _verify_digest_rsa {
Subroutine _verify_digest_rsa: called CC=5 SCAR=16.6
440 335 my $self = shift;
441 335 my ( $digest_algorithm, $digest, $signature ) = @_;
442  
443 335 my $rsa_pub = $self->cork;
444 335 ▶ if ( !$rsa_pub ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rsa_pub) 0 335 335
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 335 $rsa_pub->use_no_padding;
451 335 my $verify_result = $rsa_pub->encrypt($signature);
452  
453 327 my $k = $rsa_pub->size;
454 327 my $expected = calculate_EM( $digest_algorithm, $digest, $k );
455 327 ▶ return 1 if ( $verify_result eq $expected );
Branch: 1 branch
BranchTrueFalseTotal
if $verify_result eq $expected 275 52 327
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 52 my $digest_len = length $digest;
464 52 my $prefix_len = length($expected) - $digest_len;
465 52 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (substr($verify_result, 0, $prefix_len) eq substr($expected, 0, $prefix_len)) 49 3 52
466 substr( $verify_result, 0, $prefix_len ) eq
467 substr( $expected, 0, $prefix_len ) )
468 {
469 49 $@ = 'message has been altered';
470 49 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 338 ▶sub verify_digest {
Subroutine verify_digest: called CC=4 SCAR=14.3
Pod undocumented
500 338 my $self = shift;
501 338 my ( $digest_algorithm, $digest, $signature ) = @_;
502  
503 338 ▶ my $k_tag = $self->get_tag('k') || 'rsa';
Condition100%2/2
$self->get_tag('k') || 'rsa'
l!l
32117
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 338 ▶ return $self->_verify_digest_rsa($digest_algorithm, $digest, $signature) if $k_tag eq 'rsa';
Branch: 1 branch
BranchTrueFalseTotal
if $k_tag eq "rsa" 335 3 338
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 20 1;
512  
513 __END__
« DKIM/KeyValueList.pm ↑ Summary DKIM/Canonicalization/DkCommon.pm »