lib/Mail/DKIM2/Signer.pm

filter: Statementstmt 96.5% 141 / 146 Branchbran 91.9% 57 / 62 Conditioncond 83.6% 51 / 61 MC/DCmcdc 63.6% 21 / 33 Subroutinesub 94.2% 33 / 35 Podpod 100% 13 / 13 totaltotal 90.2% 316 / 350 CC 56 SCAR 20.8
CC
56
Cov
93%
_new_signature
15.1
finish_header
15.0
init
12.3
CRAP 8.0
« lib/Mail/Milter/Authentication/Handler/DKIM2Verify.pm ↑ Summary lib/Mail/DKIM2/Canon.pm »
1 package Mail::DKIM2::Signer;
2 158 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 158 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 158 our $VERSION = '0.35';
6  
7 158 ▶use base 'Mail::DKIM2::HeaderParser';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 158 ▶use MIME::Base64 qw(encode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 158 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 158 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 158 ▶use Mail::DKIM2::Address ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 158 ▶use Mail::DKIM2::Common qw(chain_length_error);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 158 ▶use Mail::DKIM2::Canon qw(build_signing_input);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 158 ▶use Mail::DKIM2::Key qw(load_private_key signature_algorithm signature_algorithm_for_key);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 158 ▶use Mail::DKIM2::Chain;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 158 ▶use Mail::DKIM2::Signature;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 158 ▶use Mail::DKIM2::MessageInstance;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 158 ▶use Mail::DKIM2::HeaderPlan;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 585 ▶sub known_options {
Subroutine known_options: called CC=1 SCAR=0.0
Pod documented
22 585 return qw(Domain Selector KeyFile Key Algorithm MailFrom RcptTo
23 Nonce Flags Exploded Timestamp NextDomain ParentFromCallback
24 PolicyCallback Resolver);
25 }
26  
27 583 ▶sub init {
Subroutine init: called CC=12 SCAR=25.1
Pod documented
28 583 my $self = shift;
29 583 $self->SUPER::init;
30 583 ▶ croak "Domain required" unless $self->{Domain};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Domain"} 1 582 583
31 582 ▶ croak "Selector required" unless $self->{Selector};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Selector"} 0 582 582
32 582 ▶ croak "KeyFile or Key required" unless $self->{KeyFile} || $self->{Key};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"KeyFile"} or $self->{"Key"} 0 582 582
Condition66%2/3
$self->{"KeyFile"} or $self->{"Key"}
l!l&&r!l&&!r
345480
MC/DC0%0/2
$self->{"KeyFile"} or $self->{"Key"}
$self->{"KeyFile"} $self->{"Key"}
Truth table66%2/3
A or B
A= $self->{"KeyFile"}B= $self->{"Key"}
ABresult
1X1
011
000
33  
34 582 ▶ if ($self->{KeyFile} && !$self->{Key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"KeyFile"} and not $self->{"Key"}) 34 548 582
Condition66%2/3
$self->{"KeyFile"} and not $self->{"Key"}
!ll&&!rl&&r
548034
MC/DC50%1/2
$self->{"KeyFile"} and not $self->{"Key"}
$self->{"KeyFile"} not $self->{"Key"}
Truth table66%2/3
A and not(B)
A= $self->{"KeyFile"}B= not $self->{"Key"}
ABresult
0X0
100
111
35 34 $self->{Key} = load_private_key($self->{KeyFile});
36 }
37  
38 # The algorithm is the key's unless the caller names one; a name this
39 # library does not implement, or one the key cannot sign with, is a
40 # mistake in the call -- never a signature labelled as something it is
41 # not, or an exception from inside CLOSE.
42 582 ▶ $self->{Algorithm} ||= signature_algorithm_for_key($self->{Key}) || 'rsa-sha256';
Condition80%4/5
$self->{"Algorithm"} ||= signature_algorithm_for_key($self->{"Key"}) || "rsa-sha256"
l!l&&r!l&&!r
305520
signature_algorithm_for_key($self->{'Key'}) || 'rsa-sha256'
l!l
5511
MC/DC0%0/2
$self->{"Algorithm"} ||= signature_algorithm_for_key($self->{"Key"}) || "rsa-sha256"
$self->{"Algorithm"} signature_algorithm_for_key($self->{'Key'})
Truth table66%2/3
A ||= B
A= $self->{"Algorithm"}B= signature_algorithm_for_key($self->{'Key'})
ABresult
1X1
011
000
43 my $alg = signature_algorithm($self->{Algorithm})
44 582 ▶ or croak "unsupported Algorithm $self->{Algorithm}";
Branch: 1 branch
BranchTrueFalseTotal
my $alg = signature_algorithm($self->{"Algorithm"}) or croak(qq[unsupported Algorithm $self->{"Algorithm"}]) 578 4 582
45 croak "Algorithm $self->{Algorithm} needs a $alg->{key_class} key"
46 578 ▶ unless Scalar::Util::blessed($self->{Key}) && $self->{Key}->isa($alg->{key_class});
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($self->{"Key"}) and $self->{"Key"}->isa($alg->{"key_class"}) 3 575 578
Condition100%3/3
builtin::blessed($self->{"Key"}) and $self->{"Key"}->isa($alg->{"key_class"})
!ll&&!rl&&r
12575
MC/DC100%2/2
builtin::blessed($self->{"Key"}) and $self->{"Key"}->isa($alg->{"key_class"})
builtin::blessed($self->{"Key"}) $self->{"Key"}->isa($alg->{"key_class"})
Truth table100%3/3
A and B
A= builtin::blessed($self->{"Key"})B= $self->{"Key"}->isa($alg->{"key_class"})
ABresult
0X0
100
111
47 575 $self->{_alg} = $alg;
48 575 $self->{_chain} = Mail::DKIM2::Chain->new;
49 }
50  
51 5258 ▶sub handle_header {
Subroutine handle_header: called CC=1 SCAR=0.0
Pod documented
52 5258 my ($self, $field_name, $contents, $line) = @_;
53 5258 $self->{_chain}->add_field($field_name, $contents, $line);
54 }
55  
56 # The message's Message-Instance and DKIM2-Signature fields, numbered
57 # (Mail::DKIM2::Chain). Complete once the header block has been read.
58 0 ▶sub chain { return $_[0]->{_chain} }
Subroutine chain: not called CC=1 SCAR=6.9
Pod documented
59  
60 566 ▶sub finish_header {
Subroutine finish_header: called CC=15 SCAR=27.1
Pod documented
61 566 my $self = shift;
62  
63 566 my $chain = $self->{_chain};
64 566 ▶ my $next_i = ($chain->top_signature_number // 0) + 1;
Condition100%2/2
$chain->top_signature_number // 0
l!l
234332
MC/DC100%1/1
$chain->top_signature_number // 0
$chain->top_signature_number
Truth table100%2/2
A
A= $chain->top_signature_number
Aresult
11
00
65  
66 # A chain no verifier can number is a PERMERROR to all of them, and so
67 # is one a signature would take past the limit: do not sign over it.
68 # Whether the chain is otherwise worth extending -- its signatures, its
69 # instances against the content -- is Mail::DKIM2::Gate's to decide.
70 # A line that is not a field is in no header hash (HeaderParser).
71 566 ▶ my $error = ($self->malformed_header ? 'malformed header field' : undef)
Branch: 1 branch
BranchTrueFalseTotal
$self->malformed_header ? : 1 565 566
Condition100%6/6
(($self->malformed_header ? "malformed header field" : undef) // $chain->numbering_error) // chain_length_error({"dkim2-signature", $next_i})
l!l&&r!l&&!r
242540
($self->malformed_header ? "malformed header field" : undef) // $chain->numbering_error
l!l&&r!l&&!r
123542
MC/DC100%3/3
(($self->malformed_header ? "malformed header field" : undef) // $chain->numbering_error) // chain_length_error({"dkim2-signature", $next_i})
($self->malformed_header ? "malformed header field" : undef) $chain->numbering_error chain_length_error({"dkim2-signature", $next_i})
Truth table100%4/4
A // B // C
A= ($self->malformed_header ? "malformed header field" : undef)B= $chain->numbering_errorC= chain_length_error({"dkim2-signature", $next_i})
ABCresult
1XX1
01X1
0011
0000
72 // $chain->numbering_error
73 // chain_length_error({ 'dkim2-signature' => $next_i });
74 # A signature names the Message-Instance it covers in m=, and a verifier
75 # rejects one that names none (tag=m missing), so a message with no
76 # instance cannot be signed: compute one first (dkim2sign does).
77 566 ▶ $error //= 'no Message-Instance field to sign over' unless $chain->instance_numbers;
Branch: 1 branch
BranchTrueFalseTotal
unless $chain->instance_numbers 6 560 566
Condition100%2/2
$error //= "no Message-Instance field to sign over"
l!l
51
MC/DC100%1/1
$error //= "no Message-Instance field to sign over"
$error
Truth table100%2/2
A
A= $error
Aresult
11
00
78 # -09 section 3.2: a Signer MUST use an RSA key of at least 1024 bits,
79 # and every verifier calls a shorter one "too short".
80 566 ▶ if (my $min = $self->{_alg}{min_bits}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $min = $self->{"_alg"}{"min_bits"}) 564 2 566
81 $error //= "signing key is shorter than $min bits"
82 564 ▶ if $self->{Key}->size * 8 < $min;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Key"}->size * 8 < $min 1 563 564
Condition50%1/2
$error //= "signing key is shorter than $min bits"
l!l
01
MC/DC0%0/1
$error //= "signing key is shorter than $min bits"
$error
Truth table50%1/2
A
A= $error
Aresult
11
00
83 }
84 # -09 sections 11 and 12.4: a hop that originates the message, or whose
85 # instances changed From, signs with a d= the From is Authentic for.
86 # -09 contract E3.4: d= may also be below the From domain when the
87 # caller says this d= may sign for it (ParentFromCallback), or, as an
88 # explicit opt-in, when that domain's Domain Policy Record allows it.
89 # Without either the upward case is refused: no live DNS by default.
90 566 ▶ if (!$error && $self->_from_must_match) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $error and $self->_from_must_match) 341 225 566
Condition100%3/3
not $error and $self->_from_must_match
!ll&&!rl&&r
28197341
MC/DC100%2/2
not $error and $self->_from_must_match
not $error $self->_from_must_match
Truth table100%3/3
not(A) and B
A= not $errorB= $self->_from_must_match
ABresult
0X0
100
111
91 my $e = Mail::DKIM2::Address::from_pair_error($self->{Domain},
92 14 ▶ sub { $self->_parent_policy(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
93 343 map { $_->[1] } grep { lc $_->[0] eq 'from' }
94 341 @{ Mail::DKIM2::HeaderPlan::fields($self->{headers}) });
95 341 ▶ $error = "From header field is not Authentic for d=$self->{Domain}" if $e;
Branch: 1 branch
BranchTrueFalseTotal
if $e 13 328 341
96 }
97 566 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 41 525 566
98 # A protocol outcome, not a programming error: reported through
99 # result/details like the Verifier's, so a streaming host is not
100 # handed an exception from inside PRINT. The body is discarded.
101 41 $self->{result} = 'fail';
102 41 $self->{details} = $error;
103 41 $self->stop;
104 41 return;
105 }
106 525 $self->{_mi_version} = $chain->top_instance;
107 525 ▶ $self->{_timestamp} = $self->{Timestamp} || time();
Condition100%2/2
$self->{'Timestamp'} || time
l!l
327198
MC/DC100%1/1
$self->{'Timestamp'} || time
$self->{'Timestamp'}
Truth table100%2/2
A
A= $self->{'Timestamp'}
Aresult
11
00
108 525 $self->{_next_i} = $next_i;
109 525 $self->{_signature} = $self->_new_signature($self->{RcptTo});
110 # Section 12.8 signs the Message-Instance and DKIM2-Signature fields
111 # alone, so the body is not kept.
112 525 ▶ $self->{_body_sink} = sub {};
Subroutine __ANON__: called CC=1 SCAR=0.0
113 }
114  
115 # The signature object for this hop with rt= set to $rcpt (an arrayref, or
116 # undef for none). -09 §12.3: when NextDomain is set this is an
117 # imaginary forwarding hop -- emit nd= and omit mf=/rt= (Signature->new
118 # enforces the exclusion).
119 #
120 # f= (-09 section 12.5): "exploded" MUST be set by a hop sending the message
121 # to more than one address and MUST NOT be set at i=1. So it is set when
122 # i > 1 and the caller asked for it (Exploded, or "exploded" among its
123 # Flags) or rt= holds more than one address, and dropped at i=1 whatever
124 # the caller said. Built afresh for each recipient set, since the flag
125 # depends on rt=.
126 535 ▶sub _new_signature {
Subroutine _new_signature: called CC=15 SCAR=27.1
127 535 my ($self, $rcpt) = @_;
128 535 my $i = $self->{_next_i};
129 535 ▶ my @rcpt = !defined $rcpt ? () : ref $rcpt eq 'ARRAY' ? @$rcpt : ($rcpt);
Branch: 2 branches
BranchTrueFalseTotal
!defined($rcpt) ? : 30 505 535
ref $rcpt eq 'ARRAY' ? : 505 0 505
130 535 ▶ my @flags = grep { $_ ne 'exploded' } @{ $self->{Flags} || [] };
Condition100%2/2
$self->{'Flags'} || []
l!l
1534
MC/DC100%1/1
$self->{'Flags'} || []
$self->{'Flags'}
Truth table100%2/2
A
A= $self->{'Flags'}
Aresult
11
00
131 my $asked = $self->{Exploded}
132 535 ▶ || grep { $_ eq 'exploded' } @{ $self->{Flags} || [] };
Condition100%5/5
$self->{'Exploded'} || grep({$_ eq 'exploded';} @{$self->{'Flags'} or [];})
l!l&&r!l&&!r
171517
$self->{'Flags'} || []
l!l
1517
MC/DC100%2/2
$self->{'Exploded'} || grep({$_ eq 'exploded';} @{$self->{'Flags'} or [];})
$self->{'Exploded'} grep({$_ eq 'exploded';} @{$self->{'Flags'} or [];})
MC/DC100%1/1
$self->{'Flags'} || []
$self->{'Flags'}
Truth table100%3/3
A || B
A= $self->{'Exploded'}B= grep({$_ eq 'exploded';} @{$self->{'Flags'} or [];})
ABresult
1X1
011
000
Truth table100%2/2
A
A= $self->{'Flags'}
Aresult
11
00
133 push @flags, 'exploded'
134 535 ▶ if $i > 1 && ($asked || (!$self->{NextDomain} && @rcpt > 1));
Branch: 1 branch
BranchTrueFalseTotal
if $i > 1 and $asked || !$self->{"NextDomain"} && @rcpt > 1 15 520 535
Condition100%9/9
$i > 1 and $asked || !$self->{"NextDomain"} && @rcpt > 1
!ll&&!rl&&r
31820215
$asked or not $self->{"NextDomain"} and @rcpt > 1
l!l&&r!l&&!r
132202
not $self->{"NextDomain"} and @rcpt > 1
!ll&&!rl&&r
111912
MC/DC100%4/4
$i > 1 and $asked || !$self->{"NextDomain"} && @rcpt > 1
$i > 1 $asked not $self->{"NextDomain"} @rcpt > 1
Truth table100%5/5
A and B or not(C) and D
A= $i > 1B= $askedC= not $self->{"NextDomain"}D= @rcpt > 1
ABCDresult
0XXX0
100X0
10100
11XX1
10111
135 return Mail::DKIM2::Signature->new(
136 Sequence => $i,
137 Version => $self->{_mi_version} || undef,
138 Timestamp => $self->{_timestamp},
139 Domain => $self->{Domain},
140 ($self->{NextDomain}
141 ? (NextDomain => $self->{NextDomain})
142 : (MailFrom => $self->{MailFrom},
143 (defined $rcpt ? (RcptTo => \@rcpt) : ()))),
144 Signatures => [[ $self->{Selector}, $self->{Algorithm}, '' ]],
145 535 ▶ ($self->{Nonce} ? (Nonce => $self->{Nonce}) : ()),
Branch: 4 branches
BranchTrueFalseTotal
$self->{'NextDomain'} ? : 28 507 535
defined $rcpt ? : 505 2 507
$self->{'Nonce'} ? : 0 535 535
@flags ? : 16 519 535
Condition50%1/2
$self->{'_mi_version'} || undef
l!l
5350
MC/DC0%0/1
$self->{'_mi_version'} || undef
$self->{'_mi_version'}
Truth table50%1/2
A
A= $self->{'_mi_version'}
Aresult
11
00
146 (@flags ? (Flags => \@flags) : ()),
147 );
148 }
149  
150 # For from_pair_error: whether this d= may make a From in parent domain
151 # $dom Authentic ('open') or not ('strict').
152 14 ▶sub _parent_policy {
Subroutine _parent_policy: called CC=5 SCAR=16.1
153 14 my ($self, $dom) = @_;
154 14 ▶ if (my $cb = $self->{ParentFromCallback}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $self->{"ParentFromCallback"}) 8 6 14
155 8 ▶ return $cb->($dom, $self) ? 'open' : 'strict';
Branch: 1 branch
BranchTrueFalseTotal
&$cb($dom, $self) ? : 4 4 8
156 }
157 6 ▶ return 'strict' unless $self->{PolicyCallback} || $self->{Resolver};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"PolicyCallback"} or $self->{"Resolver"} 1 5 6
Condition100%3/3
$self->{"PolicyCallback"} or $self->{"Resolver"}
l!l&&r!l&&!r
321
MC/DC100%2/2
$self->{"PolicyCallback"} or $self->{"Resolver"}
$self->{"PolicyCallback"} $self->{"Resolver"}
Truth table100%3/3
A or B
A= $self->{"PolicyCallback"}B= $self->{"Resolver"}
ABresult
1X1
011
000
158 my ($p) = Mail::DKIM2::Key::policy_from_source($dom,
159 5 ▶ $self->{PolicyCallback}, sub { $self->_resolver }, $self);
Subroutine __ANON__: called CC=1 SCAR=0.0
160 5 return $p;
161 }
162  
163 # fetch_domain_policy($domain): the TXT records at _domainkey.$domain
164 # through the Resolver, as Mail::DKIM2::Verifier's: for a PolicyCallback
165 # to fall back to.
166 0 ▶sub fetch_domain_policy {
Subroutine fetch_domain_policy: not called CC=1 SCAR=6.9
Pod documented
167 0 my ($self, $dom) = @_;
168 0 return Mail::DKIM2::Key::query_domain_policy($self->_resolver, $dom);
169 }
170  
171 # The Resolver for the Domain Policy Record lookup: the caller's, else a
172 # default Net::DNS::Resolver, made only when a lookup is needed.
173 2 ▶sub _resolver {
Subroutine _resolver: called CC=2 SCAR=10.1
174 2 my ($self) = @_;
175 2 ▶ return $self->{Resolver} //= do {
Condition33%1/3
$self->{"Resolver"} //= do { require Net::DNS::Resolver; "Net::DNS::Resolver"->new }
l!l&&r!l&&!r
200
MC/DC0%0/2
$self->{"Resolver"} //= do { require Net::DNS::Resolver; "Net::DNS::Resolver"->new }
$self->{"Resolver"} do { require Net::DNS::Resolver; "Net::DNS::Resolver"->new }
Truth table33%1/3
A //= B
A= $self->{"Resolver"}B= do { require Net::DNS::Resolver; "Net::DNS::Resolver"->new }
ABresult
1X1
011
000
176 0 require Net::DNS::Resolver;
177 0 Net::DNS::Resolver->new;
178 };
179 }
180  
181 # True when this signature answers for the From the message carries: there
182 # is no signature yet (this hop is i=1), or a Message-Instance no existing
183 # signature covers (m= above every signature's m=) has a header Recipe that
184 # names From -- this hop, or a list manager on its host, changed it. One
185 # that does not parse is left to the verifiers (and Mail::DKIM2::Gate),
186 # which refuse it: the Signer checks only what it needs to sign.
187 538 ▶sub _from_must_match {
Subroutine _from_must_match: called CC=3 SCAR=11.0
188 538 my ($self) = @_;
189 538 my $chain = $self->{_chain};
190 538 ▶ return 1 unless $chain->signature_numbers;
Branch: 1 branch
BranchTrueFalseTotal
unless $chain->signature_numbers 321 217 538
191 217 ▶ return $chain->instances_changing('From', $chain->covered_m) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$chain->instances_changing('From', $chain->covered_m) ? : 20 197 217
192 }
193  
194 525 ▶sub finish_body {
Subroutine finish_body: called CC=1 SCAR=0.0
Pod documented
195 525 my $self = shift;
196 525 $self->_compute_signature;
197 525 $self->{result} = 'signed';
198 }
199  
200 # Re-sign the same message for a different envelope recipient, returning the
201 # folded DKIM2-Signature header to prepend for that recipient.
202 #
203 # This is the cheap half of signing. §12.8 signs solely the Message-Instance and
204 # DKIM2-Signature header fields, so everything expensive -- the body hash and
205 # the hash of every other header field -- lives in the Message-Instance and is
206 # identical for every recipient. Only rt= changes, so an additional recipient
207 # costs one signature over a few hundred bytes rather than another pass over
208 # the message.
209 #
210 # Feed the message through PRINT/CLOSE once, then call this per recipient:
211 #
212 # $signer->PRINT($message); $signer->CLOSE; # once
213 # for my $rcpt (@rcpts) {
214 # my $header = $signer->sign_for_recipient($rcpt);
215 # }
216 #
217 # Takes one address or an arrayref of them. Not usable on a signature carrying
218 # nd=, which excludes rt= by §10.7; Signature::rcpt_to croaks in that case.
219 12 ▶sub sign_for_recipient {
Subroutine sign_for_recipient: called CC=6 SCAR=18.0
Pod documented
220 12 my ($self, $rcpt) = @_;
221 croak "sign_for_recipient: no signature: " . ($self->{details} // 'CLOSE has not run')
222 12 ▶ unless $self->{_signature};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_signature"} 1 11 12
Condition50%1/2
$self->{"details"} // "CLOSE has not run"
l!l
10
MC/DC0%0/1
$self->{"details"} // "CLOSE has not run"
$self->{"details"}
Truth table50%1/2
A
A= $self->{"details"}
Aresult
11
00
223 croak "cannot set rt= on a signature carrying nd= (-09 §10.7)"
224 11 ▶ if $self->{NextDomain};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"NextDomain"} 1 10 11
225 10 ▶ my @list = ref $rcpt eq 'ARRAY' ? @$rcpt : ($rcpt);
Branch: 1 branch
BranchTrueFalseTotal
ref $rcpt eq 'ARRAY' ? : 1 9 10
226 10 ▶ croak "rcpt_to requires at least one recipient" unless @list;
Branch: 1 branch
BranchTrueFalseTotal
unless @list 0 10 10
227 10 $self->{_signature} = $self->_new_signature(\@list);
228 10 $self->_compute_signature;
229 10 $self->{result} = 'signed';
230 10 return $self->as_string;
231 }
232  
233 535 ▶sub _compute_signature {
Subroutine _compute_signature: called CC=1 SCAR=0.0
234 535 my $self = shift;
235  
236 535 my $signature = $self->{_signature};
237 535 my $chain = $self->{_chain};
238  
239 # Reset s= to the empty placeholder before building the signing input.
240 # Without this a second call would sign an input containing the FIRST
241 # call's signature bytes, which no verifier can reproduce -- the signing
242 # input must always carry an empty s= value (§12.8).
243 535 $signature->set_tag('s', join(':', $self->{Selector}, $self->{Algorithm}, ''));
244  
245 my $signing_input = build_signing_input(
246 1161 instances => [ map { $chain->instance_field($_) } $chain->instance_numbers ],
247 535 signatures => [ map { $chain->signature_field($_) } $chain->signature_numbers ],
248 signing => $signature->as_folded_string, # as it will be sent
249 );
250  
251 535 my $sig_raw = $self->{_alg}{sign}->($self->{Key}, $signing_input);
252 535 my $signb64 = encode_base64($sig_raw, '');
253  
254 # Update the signature object with the actual signature
255 # Format: sel:alg:sig
256 535 $signature->set_tag('s', join(':', $self->{Selector}, $self->{Algorithm}, $signb64));
257 }
258  
259 10 ▶sub signature {
Subroutine signature: called CC=1 SCAR=0.0
Pod documented
260 10 my $self = shift;
261 10 return $self->{_signature};
262 }
263  
264 # No signature (before CLOSE, or a refused message) croaks: an empty string
265 # prepended to the message by a caller that did not check result() is a
266 # blank line that ends its header block.
267 517 ▶sub as_string {
Subroutine as_string: called CC=3 SCAR=11.1
Pod documented
268 517 my $self = shift;
269 croak 'no signature: ' . ($self->{details} // 'the message has not been signed') . "\n"
270 517 ▶ unless $self->{_signature};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_signature"} 2 515 517
Condition50%1/2
$self->{"details"} // "the message has not been signed"
l!l
20
MC/DC0%0/1
$self->{"details"} // "the message has not been signed"
$self->{"details"}
Truth table50%1/2
A
A= $self->{"details"}
Aresult
11
00
271 515 return $self->{_signature}->as_folded_string();
272 }
273  
274 # undef until CLOSE; then 'signed', or 'fail' with the reason in details().
275 249 ▶sub result {
Subroutine result: called CC=1 SCAR=0.0
Pod documented
276 249 my $self = shift;
277 249 return $self->{result};
278 }
279  
280 18 ▶sub details {
Subroutine details: called CC=1 SCAR=0.0
Pod documented
281 18 my $self = shift;
282 18 return $self->{details};
283 }
284  
285 57 ▶sub result_detail {
Subroutine result_detail: called CC=3 SCAR=11.1
Pod documented
286 57 my $self = shift;
287 57 ▶ my $result = $self->result // return;
Condition50%1/2
$self->result // (return)
l!l
570
MC/DC0%0/1
$self->result // (return)
$self->result
Truth table50%1/2
A
A= $self->result
Aresult
11
00
288 57 ▶ return $self->{details} ? "$result ($self->{details})" : $result;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'details'} ? : 29 28 57
289 }
290  
291 158 1;
292  
293 __END__
« lib/Mail/Milter/Authentication/Handler/DKIM2Verify.pm ↑ Summary lib/Mail/DKIM2/Canon.pm »