| 1 | package Mail::DKIM::Canonicalization::DkimCommon; | ||||||||||
| 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: common canonicalization | ||||||||||
| 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 base 'Mail::DKIM::Canonicalization::Base'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 20 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | 1257 | ▶ | sub init { | ||||||||
|
Subroutine
init: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||
| 18 | 1257 | my $self = shift; | |||||||||
| 19 | 1257 | $self->SUPER::init; | |||||||||
| 20 | |||||||||||
| 21 | 1257 | $self->{body_count} = 0; | |||||||||
| 22 | 1257 | $self->{body_truncated} = 0; | |||||||||
| 23 | |||||||||||
| 24 | # these canonicalization methods require signature to use | ||||||||||
| 25 | $self->{Signature} | ||||||||||
| 26 | 1257 | ▶ | or croak 'no signature specified'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | # similar to code in DkCommon.pm | ||||||||||
| 30 | 509 | ▶ | sub add_header { | ||||||||
|
Subroutine
add_header: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 31 | |||||||||||
| 32 | #Note: canonicalization of headers is performed | ||||||||||
| 33 | #in finish_header() | ||||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 321 | ▶ | sub finish_header { | ||||||||
|
Subroutine
finish_header: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||
| 37 | 321 | my $self = shift; | |||||||||
| 38 | 321 | my %args = @_; | |||||||||
| 39 | |||||||||||
| 40 | # Headers are canonicalized in the order specified by the h= tag. | ||||||||||
| 41 | # However, in the case of multiple instances of the same header name, | ||||||||||
| 42 | # the headers will be canonicalized in reverse order (i.e. "from | ||||||||||
| 43 | # the bottom of the header field block to the top"). | ||||||||||
| 44 | # | ||||||||||
| 45 | # This is described in 5.4 of RFC4871. | ||||||||||
| 46 | |||||||||||
| 47 | # Mail::DKIM::Verifier rejects a DKIM signature without a usable h= | ||||||||||
| 48 | # tag before it gets here. An ARC-Message-Signature with an empty h= | ||||||||||
| 49 | # is canonicalized as it is (see Mail::DKIM::ARC::Verifier), and the | ||||||||||
| 50 | # signer sets h= after this has run. | ||||||||||
| 51 | |||||||||||
| 52 | # the message headers grouped by name, bottom-up, so each name in h= | ||||||||||
| 53 | # costs one lookup rather than a scan of the header; the grouping is | ||||||||||
| 54 | # shared through HeaderIndex by every signature on the message | ||||||||||
| 55 | 321 | ▶ | my $heads = $args{HeaderIndex} ? $args{HeaderIndex}{dkim} : undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 321 | ▶ | unless ($heads) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | 233 | $heads = _group_headers( $args{Headers} ); | |||||||||
| 58 | 233 | ▶ | $args{HeaderIndex}{dkim} = $heads if $args{HeaderIndex}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | # iterate through the header field names specified in the signature | ||||||||||
| 62 | 321 | my %used; | |||||||||
| 63 | 321 | my @sig_headers = $self->{Signature}->headerlist; | |||||||||
| 64 | 321 | foreach my $hdr_name (@sig_headers) { | |||||||||
| 65 | |||||||||||
| 66 | # find the specified header in the message, counting how many of | ||||||||||
| 67 | # that name we've used, so if it occurs more than once, we'll get | ||||||||||
| 68 | # the next header in line | ||||||||||
| 69 | 21467 | my $name = lc $hdr_name; | |||||||||
| 70 | 21467 | ▶ | my $list = $heads->{$name} or next; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 1424 | my $hdr = $list->[ $used{$name}++ ]; | |||||||||
| 72 | 1424 | ▶ | next unless defined $hdr; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | |||||||||||
| 74 | 1420 | $hdr =~ s/\015\012\z//s; | |||||||||
| 75 | 1420 | $self->output( $self->canonicalize_header($hdr) . "\015\012" ); | |||||||||
| 76 | } | ||||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | # group the headers by lowercased name; since the bottom-most headers | ||||||||||
| 80 | # are to get precedence, each group is in reverse order (now the first | ||||||||||
| 81 | # header matching a particular name is the header to insert) | ||||||||||
| 82 | 233 | ▶ | sub _group_headers { | ||||||||
|
Subroutine
_group_headers: called CC=3 SCAR=11.0
| |||||||||||
| 83 | 233 | my ($headers) = @_; | |||||||||
| 84 | 233 | my %heads; | |||||||||
| 85 | 233 | foreach my $hdr ( reverse @$headers ) { | |||||||||
| 86 | 4770 | ▶ | next unless $hdr =~ /^([^\s:]+)\s*:/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 4767 | push @{ $heads{ lc $1 } }, $hdr; | |||||||||
| 88 | } | ||||||||||
| 89 | 233 | return \%heads; | |||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | 348 | ▶ | sub add_body { | ||||||||
|
Subroutine
add_body: called CC=4 SCAR=17.3
Pod
documented
| |||||||||||
| 93 | 348 | my $self = shift; | |||||||||
| 94 | 348 | my ($multiline) = @_; | |||||||||
| 95 | |||||||||||
| 96 | 348 | $multiline = $self->canonicalize_body($multiline); | |||||||||
| 97 | 348 | ▶ | if ( $self->{Signature} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | 348 | ▶ | if ( my $limit = $self->{Signature}->body_count ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | 0 | my $remaining = $limit - $self->{body_count}; | |||||||||
| 100 | 0 | ▶ | if ( length($multiline) > $remaining ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | 0 | $self->{body_truncated} += length($multiline) - $remaining; | |||||||||
| 102 | 0 | $multiline = substr( $multiline, 0, $remaining ); | |||||||||
| 103 | } | ||||||||||
| 104 | } | ||||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | 348 | $self->{body_count} += length($multiline); | |||||||||
| 108 | 348 | $self->output($multiline); | |||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | 544 | ▶ | sub finish_body { | ||||||||
|
Subroutine
finish_body: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | 1 | ▶ | sub finish_message { | ||||||||
|
Subroutine
finish_message: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||
| 115 | 1 | my $self = shift; | |||||||||
| 116 | |||||||||||
| 117 | 1 | ▶ | if ( $self->{Signature} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 118 | 1 | $self->output("\015\012"); | |||||||||
| 119 | |||||||||||
| 120 | # append the DKIM-Signature (without data) | ||||||||||
| 121 | 1 | my $line = $self->{Signature}->as_string_without_data; | |||||||||
| 122 | |||||||||||
| 123 | # signature is subject to same canonicalization as headers | ||||||||||
| 124 | 1 | $self->output( $self->canonicalize_header($line) ); | |||||||||
| 125 | } | ||||||||||
| 126 | } | ||||||||||
| 127 | |||||||||||
| 128 | 0 | ▶ | sub body_count { | ||||||||
|
Subroutine
body_count: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 129 | 0 | my $self = shift; | |||||||||
| 130 | 0 | return $self->{body_count}; | |||||||||
| 131 | } | ||||||||||
| 132 | |||||||||||
| 133 | 0 | ▶ | sub body_truncated { | ||||||||
|
Subroutine
body_truncated: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 134 | 0 | my $self = shift; | |||||||||
| 135 | 0 | return $self->{body_truncated}; | |||||||||
| 136 | } | ||||||||||
| 137 | |||||||||||
| 138 | 20 | 1; | |||||||||
| 139 | |||||||||||
| 140 | __END__ | ||||||||||