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