| 1 | package Mail::DKIM::Canonicalization::DkCommon; | ||||||||||||
| 2 | 11 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 11 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 11 | our $VERSION = '1.20261116'; # VERSION | |||||||||||
| 5 | # ABSTRACT: dk common canonicalization | ||||||||||||
| 6 | |||||||||||||
| 7 | # Copyright 2005-2006 Messiah College. All rights reserved. | ||||||||||||
| 8 | # Jason Long <jlong@messiah.edu> | ||||||||||||
| 9 | |||||||||||||
| 10 | # This program is free software; you can redistribute it and/or | ||||||||||||
| 11 | # modify it under the same terms as Perl itself. | ||||||||||||
| 12 | |||||||||||||
| 13 | 11 | ▶ | use base 'Mail::DKIM::Canonicalization::Base'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 11 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | |||||||||||||
| 16 | 21 | ▶ | sub init { | ||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 17 | 21 | my $self = shift; | |||||||||||
| 18 | 21 | $self->SUPER::init; | |||||||||||
| 19 | |||||||||||||
| 20 | 21 | $self->{header_count} = 0; | |||||||||||
| 21 | } | ||||||||||||
| 22 | |||||||||||||
| 23 | # similar to code in DkimCommon.pm | ||||||||||||
| 24 | 140 | ▶ | sub add_header { | ||||||||||
|
Subroutine
add_header: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 25 | |||||||||||||
| 26 | #Note: canonicalization of headers is performed | ||||||||||||
| 27 | #in finish_header() | ||||||||||||
| 28 | |||||||||||||
| 29 | 140 | my $self = shift; | |||||||||||
| 30 | 140 | $self->{header_count}++; | |||||||||||
| 31 | } | ||||||||||||
| 32 | |||||||||||||
| 33 | 21 | ▶ | sub finish_header { | ||||||||||
|
Subroutine
finish_header: called CC=12 SCAR=25.1
Pod
documented
| |||||||||||||
| 34 | 21 | my $self = shift; | |||||||||||
| 35 | 21 | my %args = @_; | |||||||||||
| 36 | |||||||||||||
| 37 | # RFC4870, 3.3: | ||||||||||||
| 38 | # h = A colon-separated list of header field names that identify the | ||||||||||||
| 39 | # headers presented to the signing algorithm. If present, the | ||||||||||||
| 40 | # value MUST contain the complete list of headers in the order | ||||||||||||
| 41 | # presented to the signing algorithm. | ||||||||||||
| 42 | # | ||||||||||||
| 43 | # In the presence of duplicate headers, a signer may include | ||||||||||||
| 44 | # duplicate entries in the list of headers in this tag. If a | ||||||||||||
| 45 | # header is included in this list, a verifier must include all | ||||||||||||
| 46 | # occurrences of that header, subsequent to the "DomainKey- | ||||||||||||
| 47 | # Signature:" header in the verification. | ||||||||||||
| 48 | # | ||||||||||||
| 49 | # RFC4870, 3.4.2.1: | ||||||||||||
| 50 | # * Each line of the email is presented to the signing algorithm in | ||||||||||||
| 51 | # the order it occurs in the complete email, from the first line of | ||||||||||||
| 52 | # the headers to the last line of the body. | ||||||||||||
| 53 | # * If the "h" tag is used, only those header lines (and their | ||||||||||||
| 54 | # continuation lines if any) added to the "h" tag list are included. | ||||||||||||
| 55 | |||||||||||||
| 56 | # only consider headers AFTER my signature | ||||||||||||
| 57 | 21 | my @sig_headers; | |||||||||||
| 58 | { | ||||||||||||
| 59 | 21 | my $s0 = @{ $args{Headers} } - $self->{header_count}; | |||||||||||
| 60 | 21 | my $s1 = @{ $args{Headers} } - 1; | |||||||||||
| 61 | 21 | @sig_headers = ( @{ $args{Headers} } )[ $s0 .. $s1 ]; | |||||||||||
| 62 | } | ||||||||||||
| 63 | |||||||||||||
| 64 | # check if signature specifies a list of headers | ||||||||||||
| 65 | 21 | my @sig_header_names = $self->{Signature}->headerlist; | |||||||||||
| 66 | 21 | ▶ | if (@sig_header_names) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 67 | |||||||||||||
| 68 | # - first, group all header fields with the same name together | ||||||||||||
| 69 | # (using a hash of arrays) | ||||||||||||
| 70 | 17 | my %heads; | |||||||||||
| 71 | 17 | foreach my $line (@sig_headers) { | |||||||||||
| 72 | 123 | ▶ | next unless $line =~ /^([^\s:]+)\s*:/; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 73 | 123 | my $field_name = lc $1; | |||||||||||
| 74 | |||||||||||||
| 75 | 123 | ▶ | $heads{$field_name} ||= []; | ||||||||||
|
Condition100%2/2
$heads{$field_name} ||= []
MC/DC100%1/1
$heads{$field_name} ||= []
$heads{$field_name}
Truth table100%2/2
A
A= $heads{$field_name}
| |||||||||||||
| 76 | 123 | push @{ $heads{$field_name} }, $line; | |||||||||||
| 77 | } | ||||||||||||
| 78 | |||||||||||||
| 79 | # - second, count how many times each header field name appears | ||||||||||||
| 80 | # in the h= tag | ||||||||||||
| 81 | 17 | my %counts; | |||||||||||
| 82 | 17 | foreach my $field_name (@sig_header_names) { | |||||||||||
| 83 | 106 | ▶ | $heads{ lc $field_name } ||= []; | ||||||||||
|
Condition100%2/2
$heads{lc $field_name} ||= []
MC/DC100%1/1
$heads{lc $field_name} ||= []
$heads{lc $field_name}
Truth table100%2/2
A
A= $heads{lc $field_name}
| |||||||||||||
| 84 | 106 | $counts{ lc $field_name }++; | |||||||||||
| 85 | } | ||||||||||||
| 86 | |||||||||||||
| 87 | # - finally, working backwards through the h= tag, | ||||||||||||
| 88 | # collect the headers we will be signing (last to first). | ||||||||||||
| 89 | # Normally, one occurrence of a name in the h= tag | ||||||||||||
| 90 | # correlates to one occurrence of that header being presented | ||||||||||||
| 91 | # to canonicalization, but if (working backwards) we are | ||||||||||||
| 92 | # at the first occurrence of that name, and there are | ||||||||||||
| 93 | # multiple headers of that name, then put them all in. | ||||||||||||
| 94 | # | ||||||||||||
| 95 | 17 | @sig_headers = (); | |||||||||||
| 96 | 17 | ▶ | while ( my $field_name = pop @sig_header_names ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 97 | 106 | $counts{ lc $field_name }--; | |||||||||||
| 98 | 106 | ▶ | if ( $counts{ lc $field_name } > 0 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 99 | |||||||||||||
| 100 | # this field is named more than once in the h= tag, | ||||||||||||
| 101 | # so only take the last occuring of that header | ||||||||||||
| 102 | 0 | my $line = pop @{ $heads{ lc $field_name } }; | |||||||||||
| 103 | 0 | ▶ | unshift @sig_headers, $line if defined $line; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 104 | } | ||||||||||||
| 105 | else { | ||||||||||||
| 106 | 106 | unshift @sig_headers, @{ $heads{ lc $field_name } }; | |||||||||||
| 107 | 106 | $heads{ lc $field_name } = []; | |||||||||||
| 108 | } | ||||||||||||
| 109 | } | ||||||||||||
| 110 | } | ||||||||||||
| 111 | |||||||||||||
| 112 | # iterate through each header, in the order determined above | ||||||||||||
| 113 | 21 | foreach my $line (@sig_headers) { | |||||||||||
| 114 | 123 | ▶ | if ( $line =~ /^(from|sender)\s*:(.*)$/i ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 115 | 25 | my $field = $1; | |||||||||||
| 116 | 25 | my $content = $2; | |||||||||||
| 117 | 25 | $self->{interesting_header}->{ lc $field } = $content; | |||||||||||
| 118 | } | ||||||||||||
| 119 | 123 | $line =~ s/\015\012\z//s; | |||||||||||
| 120 | 123 | $self->output( $self->canonicalize_header( $line . "\015\012" ) ); | |||||||||||
| 121 | } | ||||||||||||
| 122 | |||||||||||||
| 123 | 21 | $self->output( $self->canonicalize_body("\015\012") ); | |||||||||||
| 124 | } | ||||||||||||
| 125 | |||||||||||||
| 126 | 21 | ▶ | sub add_body { | ||||||||||
|
Subroutine
add_body: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 127 | 21 | my $self = shift; | |||||||||||
| 128 | 21 | my ($multiline) = @_; | |||||||||||
| 129 | |||||||||||||
| 130 | 21 | $self->output( $self->canonicalize_body($multiline) ); | |||||||||||
| 131 | } | ||||||||||||
| 132 | |||||||||||||
| 133 | 21 | ▶ | sub finish_body { | ||||||||||
|
Subroutine
finish_body: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 134 | } | ||||||||||||
| 135 | |||||||||||||
| 136 | 0 | ▶ | sub finish_message { | ||||||||||
|
Subroutine
finish_message: not called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 137 | } | ||||||||||||
| 138 | |||||||||||||
| 139 | 11 | 1; | |||||||||||
| 140 | |||||||||||||
| 141 | __END__ | ||||||||||||