| 1 | package Mail::DKIM::Canonicalization::seal; | ||||||||||||||||||||||||||||
| 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: arc seal canonicalization | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | # Copyright 2017 FastMail Pty Ltd. All Rights Reserved. | ||||||||||||||||||||||||||||
| 8 | # Bron Gondwana <brong@fastmailteam.com> | ||||||||||||||||||||||||||||
| 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 | # This canonicalization is for the ARC-Seal header from | ||||||||||||||||||||||||||||
| 14 | # https://tools.ietf.org/html/draft-ietf-dmarc-arc-protocol-06 | ||||||||||||||||||||||||||||
| 15 | # Rather than having a 'h' property, it processes the headers in | ||||||||||||||||||||||||||||
| 16 | # a pre-defined way. | ||||||||||||||||||||||||||||
| 17 | # | ||||||||||||||||||||||||||||
| 18 | # 5.1.1.3. Deterministic (Implicit) 'h' Tag Value for ARC-Seal | ||||||||||||||||||||||||||||
| 19 | # | ||||||||||||||||||||||||||||
| 20 | # In this section, the term "scope" is used to indicate those header | ||||||||||||||||||||||||||||
| 21 | # fields signed by an ARC-Seal header field. A number in parentheses | ||||||||||||||||||||||||||||
| 22 | # indicates the instance of that field, starting at 1. The suffix "- | ||||||||||||||||||||||||||||
| 23 | # no-b" is used with an ARC-Seal field to indicate that its "b" field | ||||||||||||||||||||||||||||
| 24 | # is empty at the time the signature is computed, as described in | ||||||||||||||||||||||||||||
| 25 | # Section 3.5 of [RFC6376]. "AAR" refers to ARC-Authentication- | ||||||||||||||||||||||||||||
| 26 | # Results, "AMS" to ARC-Message-Signature, "AS" to ARC-Seal, and "ASB" | ||||||||||||||||||||||||||||
| 27 | # to an ARC-Seal with an empty "b" tag. | ||||||||||||||||||||||||||||
| 28 | # | ||||||||||||||||||||||||||||
| 29 | # Generally, the scope of an ARC set for a message containing "n" ARC | ||||||||||||||||||||||||||||
| 30 | # sets is the concatenation of the following, for x (instance number) | ||||||||||||||||||||||||||||
| 31 | # from 1 to n: | ||||||||||||||||||||||||||||
| 32 | # | ||||||||||||||||||||||||||||
| 33 | # o AAR(x); | ||||||||||||||||||||||||||||
| 34 | # | ||||||||||||||||||||||||||||
| 35 | # o AMS(x); | ||||||||||||||||||||||||||||
| 36 | # | ||||||||||||||||||||||||||||
| 37 | # o ASB(x) if x = n, else AS(x) | ||||||||||||||||||||||||||||
| 38 | # | ||||||||||||||||||||||||||||
| 39 | # Thus for a message with no seals (i.e., upon injection), the scope of | ||||||||||||||||||||||||||||
| 40 | # the first ARC set is AAR(1):AMS(1):ASB(1). The ARC set thus | ||||||||||||||||||||||||||||
| 41 | # generated would produce a first ARC-Seal with a "b" value. The next | ||||||||||||||||||||||||||||
| 42 | # ARC set would include in its signed content the prior scope, so it | ||||||||||||||||||||||||||||
| 43 | # would have a scope of AAR(1):AMS(1):AS(1):AAR(2):AMS(2):ASB(2). | ||||||||||||||||||||||||||||
| 44 | # | ||||||||||||||||||||||||||||
| 45 | # Note: Typically header field sets appear within the header in | ||||||||||||||||||||||||||||
| 46 | # descending instance order. | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 20 | ▶ | use base 'Mail::DKIM::Canonicalization::relaxed'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 49 | 20 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 545 | ▶ | sub init { | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 52 | 545 | my $self = shift; | |||||||||||||||||||||||||||
| 53 | 545 | $self->SUPER::init; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # ARC limits instance numbers to 1..50 (RFC 8617 section 4.2.1) | ||||||||||||||||||||||||||||
| 57 | 20 | our $MAX_INSTANCE = 50; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # how to find the instance of each ARC header | ||||||||||||||||||||||||||||
| 60 | 20 | my %ARC_HEADERS = ( | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # i= comes first, and the rest is free text which may itself | ||||||||||||||||||||||||||||
| 63 | # contain "; i=" (RFC 8617 section 4.1.1) | ||||||||||||||||||||||||||||
| 64 | 'arc-authentication-results' => | ||||||||||||||||||||||||||||
| 65 | qr/^ARC-Authentication-Results:\s*i=(\d+)\s*;/i, | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # these are tag lists, so i= may be anywhere; this ugly pattern | ||||||||||||||||||||||||||||
| 68 | # matches header: field; field; ... i=N | ||||||||||||||||||||||||||||
| 69 | # (not (?:[^;]*;\s*)*, where whitespace after a ; can be taken by | ||||||||||||||||||||||||||||
| 70 | # either part, which is quadratic in a whitespace run) | ||||||||||||||||||||||||||||
| 71 | 'arc-message-signature' => | ||||||||||||||||||||||||||||
| 72 | qr/^ARC-Message-Signature:(?:[^;]*;)*\s*i=(\d+)/i, | ||||||||||||||||||||||||||||
| 73 | 'arc-seal' => qr/^ARC-Seal:(?:[^;]*;)*\s*i=(\d+)/i, | ||||||||||||||||||||||||||||
| 74 | ); | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # the instance of an ARC-Authentication-Results header field as this | ||||||||||||||||||||||||||||
| 77 | # canonicalization finds it, without leading zeros, or undef if it finds | ||||||||||||||||||||||||||||
| 78 | # none; the verifier and signer use this so that every field they accept | ||||||||||||||||||||||||||||
| 79 | # as an AAR is one the seals cover | ||||||||||||||||||||||||||||
| 80 | 300 | ▶ | sub _aar_instance { | ||||||||||||||||||||||||||
|
Subroutine
_aar_instance: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 81 | 300 | my ($header) = @_; | |||||||||||||||||||||||||||
| 82 | 300 | ▶ | my ($i) = $header =~ $ARC_HEADERS{'arc-authentication-results'} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | or return; | ||||||||||||||||||||||||||||
| 84 | 285 | $i =~ s{^0+(?=\d)}{}; | |||||||||||||||||||||||||||
| 85 | 285 | return $i; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # index the ARC headers by lowercased name and instance, keeping the | ||||||||||||||||||||||||||||
| 89 | # first of each, so each lookup doesn't have to scan the whole header | ||||||||||||||||||||||||||||
| 90 | 155 | ▶ | sub _index_headers { | ||||||||||||||||||||||||||
|
Subroutine
_index_headers: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 91 | 155 | my ( $self, $headers ) = @_; | |||||||||||||||||||||||||||
| 92 | 155 | my %index; | |||||||||||||||||||||||||||
| 93 | 155 | foreach my $hdr (@$headers) { | |||||||||||||||||||||||||||
| 94 | 2228 | ▶ | next unless $hdr =~ /^ARC-/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 683 | foreach my $h ( keys %ARC_HEADERS ) { | |||||||||||||||||||||||||||
| 96 | 2049 | ▶ | next unless $hdr =~ $ARC_HEADERS{$h}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 678 | ( my $i = $1 ) =~ s{^0+(?=\d)}{}; | |||||||||||||||||||||||||||
| 98 | 678 | ▶ | $index{$h}{$i} = $hdr unless exists $index{$h}{$i}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | 155 | return \%index; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 4246 | ▶ | sub _output_indexed_header { | ||||||||||||||||||||||||||
|
Subroutine
_output_indexed_header: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 105 | 4246 | my ( $self, $index, $h, $i ) = @_; | |||||||||||||||||||||||||||
| 106 | 4246 | my $hdr = $index->{ lc $h }{$i}; | |||||||||||||||||||||||||||
| 107 | 4246 | ▶ | return unless defined $hdr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 4234 | $hdr =~ s/\015\012\z//s; | |||||||||||||||||||||||||||
| 109 | 4234 | $self->output( $self->canonicalize_header($hdr) . "\015\012" ); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 227 | ▶ | sub finish_header { | ||||||||||||||||||||||||||
|
Subroutine
finish_header: called CC=10 SCAR=23.2
Pod
documented
| |||||||||||||||||||||||||||||
| 113 | 227 | my $self = shift; | |||||||||||||||||||||||||||
| 114 | 227 | my %args = @_; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 227 | my $i = $self->{Signature}->identity(); | |||||||||||||||||||||||||||
| 117 | 227 | ▶ | return unless $i =~ m{^\d+$}; # don't waste time if i= is bogus | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 227 | $i =~ s{^0+(?=\d)}{}; | |||||||||||||||||||||||||||
| 119 | 227 | ▶ | return if $i < 1 or $i > $MAX_INSTANCE; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$i < 1 or $i > $Mail::DKIM::Canonicalization::seal::MAX_INSTANCE
MC/DC0%0/2
$i < 1 or $i > $Mail::DKIM::Canonicalization::seal::MAX_INSTANCE
$i < 1
$i > $Mail::DKIM::Canonicalization::seal::MAX_INSTANCE
Truth table33%1/3
A or B
A= $i < 1B= $i > $Mail::DKIM::Canonicalization::seal::MAX_INSTANCE
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # the index is shared through HeaderIndex by every seal on the message | ||||||||||||||||||||||||||||
| 122 | 227 | ▶ | my $index = $args{HeaderIndex} ? $args{HeaderIndex}{seal} : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 227 | ▶ | unless ($index) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 154 | $index = $self->_index_headers( $args{Headers} ); | |||||||||||||||||||||||||||
| 125 | 154 | ▶ | $args{HeaderIndex}{seal} = $index if $args{HeaderIndex}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 227 | my $chain = $args{Chain}; | |||||||||||||||||||||||||||
| 129 | 227 | ▶ | $chain = $self->{Signature}->chain() if ! defined $chain; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | # we include the seal for everything else | ||||||||||||||||||||||||||||
| 132 | # if the previous status was pass | ||||||||||||||||||||||||||||
| 133 | 227 | ▶ | if ( $chain eq 'pass' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 209 | foreach my $n ( 1 .. ( $i - 1 ) ) { | |||||||||||||||||||||||||||
| 135 | 1264 | $self->_output_indexed_header( $index, 'ARC-Authentication-Results', $n ); | |||||||||||||||||||||||||||
| 136 | 1264 | $self->_output_indexed_header( $index, 'ARC-Message-Signature', $n ); | |||||||||||||||||||||||||||
| 137 | 1264 | $self->_output_indexed_header( $index, 'ARC-Seal', $n ); | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | # always include this header set | ||||||||||||||||||||||||||||
| 142 | 227 | $self->_output_indexed_header( $index, 'ARC-Authentication-Results', $i ); | |||||||||||||||||||||||||||
| 143 | 227 | $self->_output_indexed_header( $index, 'ARC-Message-Signature', $i ); | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | # we don't add ARC-Seal at our index, because that is this signature, and it's | ||||||||||||||||||||||||||||
| 146 | # formed with standard DKIM style, so automatically appeneded | ||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 238 | ▶ | sub add_body { | ||||||||||||||||||||||||||
|
Subroutine
add_body: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | # no body add | ||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 20 | 1; | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | __END__ | ||||||||||||||||||||||||||||