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 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: 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 20 ▶use Mail::DKIM::Canonicalization::nowsp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 20 ▶use Mail::DKIM::Canonicalization::relaxed;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 20 ▶use Mail::DKIM::Canonicalization::simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 20 ▶use Mail::DKIM::Canonicalization::seal;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 20 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 20 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 647 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
23 647 my $class = shift;
24 647 my %args = @_;
25 647 my $self = bless \%args, $class;
26 647 $self->init;
27 647 return $self;
28 }
29  
30 626 ▶sub init {
Subroutine init: called CC=3 SCAR=11.0
Pod undocumented
31 626 my $self = shift;
32  
33 626 ▶ croak 'no signature' unless $self->{Signature};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Signature"} 0 626 626
34  
35 626 ▶ $self->{mode} = $self->{Signature}->signature ? 'verify' : 'sign';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'Signature'}->signature ? : 563 63 626
36  
37 # allows subclasses to set the header_digest and body_digest
38 # properties
39 626 $self->init_digests;
40  
41 626 my ( $header_method, $body_method ) = $self->{Signature}->canonicalization;
42  
43 626 my $header_class = $self->get_canonicalization_class($header_method);
44 626 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 626 );
50 $self->{body_canon} = $body_class->new(
51 output_digest => $self->{body_digest},
52 Signature => $self->{Signature},
53 Debug_Canonicalization => $self->{Debug_Canonicalization}
54 626 );
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 1252 ▶sub get_canonicalization_class {
Subroutine get_canonicalization_class: called CC=6 SCAR=18.4
Pod undocumented
65 1252 my $self = shift;
66 1252 ▶ croak 'wrong number of arguments' unless ( @_ == 1 );
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 1252 1252
67 1252 my ($method) = @_;
68  
69 1252 ▶ my $class =
Branch: 4 branches
BranchTrueFalseTotal
$method eq 'nowsp' ? : 0 1252 1252
$method eq 'relaxed' ? : 591 661 1252
$method eq 'simple' ? : 117 544 661
$method eq 'seal' ? : 544 0 544
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 1252 return $class;
76 }
77  
78  
79 605 ▶sub add_body {
Subroutine add_body: called CC=2 SCAR=7.0
Pod documented
80 605 my $self = shift;
81 605 ▶ my $canon = $self->{body_canon} || $self->{canon};
Condition66%2/3
$self->{'body_canon'} || $self->{'canon'}
l!l&&r!l&&!r
584210
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 605 $canon->add_body(@_);
83 }
84  
85  
86 647 ▶sub add_header {
Subroutine add_header: called CC=1 SCAR=0.0
Pod documented
87 647 my $self = shift;
88 647 $self->{canon}->add_header(@_);
89 }
90  
91  
92 564 ▶sub finish_body {
Subroutine finish_body: called CC=2 SCAR=7.0
Pod documented
93 564 my $self = shift;
94 564 ▶ my $body_canon = $self->{body_canon} || $self->{canon};
Condition66%2/3
$self->{'body_canon'} || $self->{'canon'}
l!l&&r!l&&!r
543210
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 564 $body_canon->finish_body;
96 564 $self->finish_message;
97 }
98  
99  
100 564 ▶sub finish_header {
Subroutine finish_header: called CC=1 SCAR=0.0
Pod documented
101 564 my $self = shift;
102 564 $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 264 ▶sub check_body_hash {
Subroutine check_body_hash: called CC=4 SCAR=13.9
Pod undocumented
110 264 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 264 my $sighash = $self->{Signature}->body_hash();
117 264 ▶ if ( $self->{body_hash} and $sighash ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"body_hash"} and $sighash) 152 112 264
Condition66%2/3
$self->{"body_hash"} and $sighash
!ll&&!rl&&r
0112152
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 152 my $body_hash = $self->{body_hash};
119 152 my $expected = decode_base64($sighash);
120 152 ▶ if ( $body_hash ne $expected ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body_hash ne $expected) 6 146 152
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 258 return 1;
131 }
132  
133 543 ▶sub finish_message {
Subroutine finish_message: called CC=3 SCAR=11.0
Pod undocumented
134 543 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 543 $self->{body_hash} = $self->{body_digest}->digest;
141 543 ▶ if ( $self->{mode} eq 'sign' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"mode"} eq "sign") 63 480 543
142 $self->{Signature}
143 63 ->body_hash( encode_base64( $self->{body_hash}, '' ) );
144 }
145  
146 543 ▶ if ( $self->{mode} eq 'sign' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"mode"} eq "sign") 63 480 543
147 63 $self->{Signature}->prettify;
148 }
149  
150 543 my $sig_line = $self->{Signature}->as_string_without_data;
151 543 my $canonicalized = $self->{canon}->canonicalize_header($sig_line);
152  
153 543 $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 2481 ▶sub signature {
Subroutine signature: called CC=2 SCAR=7.1
Pod documented
164 2481 my $self = shift;
165 @_
166 2481 ▶ and $self->{Signature} = shift;
Branch: 1 branch
BranchTrueFalseTotal
@_ and $self->{"Signature"} = shift() 0 2481 2481
167 2481 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 20 1;
177  
178 __END__
« DKIM/Canonicalization/dk_simple.pm ↑ Summary DKIM/Canonicalization/simple.pm »