DKIM/Algorithm/Base.pm

filter: Statementstmt 96.2% 77 / 80 Branchbran 79.1% 19 / 24 Conditioncond 66.6% 6 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 85.7% 18 / 21 Podpod 53.8% 7 / 13 totaltotal 83.6% 128 / 153 CC 16 SCAR 10.3
CC
16
Cov
90%
get_canonicalization_class
6.3
check_body_hash
4.0
init
3.0
CRAP 2.8
« DKIM/Canonicalization/dk_simple.pm ↑ Summary DKIM/Canonicalization/simple.pm »
1 package Mail::DKIM::Algorithm::Base;
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: base class for DKIM "algorithms"
6  
7 # Copyright 2005-2007 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 Mail::DKIM::Canonicalization::nowsp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 15 ▶use Mail::DKIM::Canonicalization::relaxed;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 15 ▶use Mail::DKIM::Canonicalization::simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 15 ▶use Mail::DKIM::Canonicalization::seal;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 15 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 483 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
23 483 my $class = shift;
24 483 my %args = @_;
25 483 my $self = bless \%args, $class;
26 483 $self->init;
27 483 return $self;
28 }
29  
30 462 ▶sub init {
Subroutine init: called CC=3 SCAR=11.0
Pod undocumented
31 462 my $self = shift;
32  
33 462 ▶ croak 'no signature' unless $self->{Signature};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Signature"} 0 462 462
34  
35 462 ▶ $self->{mode} = $self->{Signature}->signature ? 'verify' : 'sign';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'Signature'}->signature ? : 403 59 462
36  
37 # allows subclasses to set the header_digest and body_digest
38 # properties
39 462 $self->init_digests;
40  
41 462 my ( $header_method, $body_method ) = $self->{Signature}->canonicalization;
42  
43 462 my $header_class = $self->get_canonicalization_class($header_method);
44 462 my $body_class = $self->get_canonicalization_class($body_method);
45 $self->{canon} = $header_class->new(
46 output_digest => $self->{header_digest},
47 Signature => $self->{Signature},
48 Debug_Canonicalization => $self->{Debug_Canonicalization}
49 462 );
50 $self->{body_canon} = $body_class->new(
51 output_digest => $self->{body_digest},
52 Signature => $self->{Signature},
53 Debug_Canonicalization => $self->{Debug_Canonicalization}
54 462 );
55 }
56  
57 # override this method, please...
58 # this method should set the "header_digest" and "body_digest" properties
59 0 ▶sub init_digests {
Subroutine init_digests: not called CC=1 SCAR=6.9
Pod undocumented
60 0 die 'not implemented';
61 }
62  
63 # private method - DKIM-specific
64 924 ▶sub get_canonicalization_class {
Subroutine get_canonicalization_class: called CC=6 SCAR=18.4
Pod undocumented
65 924 my $self = shift;
66 924 ▶ croak 'wrong number of arguments' unless ( @_ == 1 );
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 924 924
67 924 my ($method) = @_;
68  
69 924 ▶ my $class =
Branch: 4 branches
BranchTrueFalseTotal
$method eq 'nowsp' ? : 0 924 924
$method eq 'relaxed' ? : 409 515 924
$method eq 'simple' ? : 119 396 515
$method eq 'seal' ? : 396 0 396
70 $method eq 'nowsp' ? 'Mail::DKIM::Canonicalization::nowsp'
71 : $method eq 'relaxed' ? 'Mail::DKIM::Canonicalization::relaxed'
72 : $method eq 'simple' ? 'Mail::DKIM::Canonicalization::simple'
73 : $method eq 'seal' ? 'Mail::DKIM::Canonicalization::seal'
74 : die "unknown method $method\n";
75 924 return $class;
76 }
77  
78  
79 460 ▶sub add_body {
Subroutine add_body: called CC=2 SCAR=7.0
Pod documented
80 460 my $self = shift;
81 460 ▶ my $canon = $self->{body_canon} || $self->{canon};
Condition66%2/3
$self->{'body_canon'} || $self->{'canon'}
l!l&&r!l&&!r
439210
MC/DC0%0/2
$self->{'body_canon'} || $self->{'canon'}
$self->{'body_canon'} $self->{'canon'}
Truth table66%2/3
A || B
A= $self->{'body_canon'}B= $self->{'canon'}
ABresult
1X1
011
000
82 460 $canon->add_body(@_);
83 }
84  
85  
86 6046 ▶sub add_header {
Subroutine add_header: called CC=1 SCAR=0.0
Pod documented
87 6046 my $self = shift;
88 6046 $self->{canon}->add_header(@_);
89 }
90  
91  
92 441 ▶sub finish_body {
Subroutine finish_body: called CC=2 SCAR=7.0
Pod documented
93 441 my $self = shift;
94 441 ▶ my $body_canon = $self->{body_canon} || $self->{canon};
Condition66%2/3
$self->{'body_canon'} || $self->{'canon'}
l!l&&r!l&&!r
420210
MC/DC0%0/2
$self->{'body_canon'} || $self->{'canon'}
$self->{'body_canon'} $self->{'canon'}
Truth table66%2/3
A || B
A= $self->{'body_canon'}B= $self->{'canon'}
ABresult
1X1
011
000
95 441 $body_canon->finish_body;
96 441 $self->finish_message;
97 }
98  
99  
100 479 ▶sub finish_header {
Subroutine finish_header: called CC=1 SCAR=0.0
Pod documented
101 479 my $self = shift;
102 479 $self->{canon}->finish_header(@_);
103 }
104  
105 # checks the bh= tag of the signature to see if it has the same body
106 # hash as computed by canonicalizing/digesting the actual message body.
107 # If it doesn't match, a false value is returned, and the
108 # verification_details property is set to "body has been altered"
109 260 ▶sub check_body_hash {
Subroutine check_body_hash: called CC=4 SCAR=13.9
Pod undocumented
110 260 my $self = shift;
111  
112 # The body_hash value is set in finish_message(), if we're operating
113 # from a version of the DKIM spec that uses the bh= tag. Otherwise,
114 # the signature shouldn't have a bh= tag to check.
115  
116 260 my $sighash = $self->{Signature}->body_hash();
117 260 ▶ if ( $self->{body_hash} and $sighash ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"body_hash"} and $sighash) 150 110 260
Condition66%2/3
$self->{"body_hash"} and $sighash
!ll&&!rl&&r
0110150
MC/DC50%1/2
$self->{"body_hash"} and $sighash
$self->{"body_hash"} $sighash
Truth table66%2/3
A and B
A= $self->{"body_hash"}B= $sighash
ABresult
0X0
100
111
118 150 my $body_hash = $self->{body_hash};
119 150 my $expected = decode_base64($sighash);
120 150 ▶ if ( $body_hash ne $expected ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body_hash ne $expected) 6 144 150
121 6 $self->{verification_details} = 'body has been altered';
122  
123 # print STDERR "I calculated "
124 # . encode_base64($body_hash, "") . "\n";
125 # print STDERR "signature has "
126 # . encode_base64($expected, "") . "\n";
127 6 return;
128 }
129 }
130 254 return 1;
131 }
132  
133 420 ▶sub finish_message {
Subroutine finish_message: called CC=3 SCAR=11.0
Pod undocumented
134 420 my $self = shift;
135  
136 # DKIM requires the signature itself to be committed into the digest.
137 # But first, we need to set the bh= tag on the signature, then
138 # "prettify" it.
139  
140 420 $self->{body_hash} = $self->{body_digest}->digest;
141 420 ▶ if ( $self->{mode} eq 'sign' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"mode"} eq "sign") 59 361 420
142 $self->{Signature}
143 59 ->body_hash( encode_base64( $self->{body_hash}, '' ) );
144 }
145  
146 420 ▶ if ( $self->{mode} eq 'sign' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"mode"} eq "sign") 59 361 420
147 59 $self->{Signature}->prettify;
148 }
149  
150 420 my $sig_line = $self->{Signature}->as_string_without_data;
151 420 my $canonicalized = $self->{canon}->canonicalize_header($sig_line);
152  
153 420 $self->{canon}->output($canonicalized);
154 }
155  
156  
157 # override this method, please...
158 0 ▶sub sign {
Subroutine sign: not called CC=1 SCAR=6.9
Pod documented
159 0 die 'Not implemented';
160 }
161  
162  
163 2163 ▶sub signature {
Subroutine signature: called CC=2 SCAR=7.1
Pod documented
164 2163 my $self = shift;
165 @_
166 2163 ▶ and $self->{Signature} = shift;
Branch: 1 branch
BranchTrueFalseTotal
@_ and $self->{"Signature"} = shift() 0 2163 2163
167 2163 return $self->{Signature};
168 }
169  
170  
171 # override this method, please...
172 0 ▶sub verify {
Subroutine verify: not called CC=1 SCAR=6.9
Pod documented
173 0 die 'Not implemented';
174 }
175  
176 15 1;
177  
178 __END__
« DKIM/Canonicalization/dk_simple.pm ↑ Summary DKIM/Canonicalization/simple.pm »