Mail-DKIM2
view release on metacpan or search on metacpan
lib/Mail/DKIM2/Common.pm view on Meta::CPAN
package Mail::DKIM2::Common;
use 5.20.0;
use strict;
use warnings;
our $VERSION = '0.36';
use Email::MIME;
use Email::MIME::ContentType ();
# Everything that lived here before 0.31 and now has a module of its own is
# imported, so Mail::DKIM2::Common::fold_header(...) and
# "use Mail::DKIM2::Common qw(fold_header)" go on working.
use Mail::DKIM2::Address qw(
extract_domain
to_rfc5321_path
domain_of
to_alabel
from_domain
from_mailbox
from_authentic
from_refusal_note
relaxed_domain_match
parent_from_matcher
from_pair_error
);
use Mail::DKIM2::Canon qw(
check_ignore_prefixes
should_skip
field_name
header_values
dkim2_canonicalize_header
dkim2_canonicalize_sig_header
build_signing_input
);
use Mail::DKIM2::Fold qw(
fold_header
);
use Mail::DKIM2::Key qw(
signature_algorithm
signature_algorithm_for_key
load_private_key
load_private_key_data
parse_dkim_pubkey
parse_dkim_key_record
key_records
domain_policy
query_domain_policy
policy_from_source
);
use Mail::DKIM2::RecipeJSON qw(
encode_tag_json
decode_tag_json
);
# Every eval in the library rethrows a reference: the library only ever dies
# with strings, so an object is the host's, typically a milter or MTA
# signalling a timeout, and swallowing it would let the caller run on past
# its deadline (Mail::DKIM2's CONVENTIONS).
use Exporter 'import';
our @EXPORT_OK = qw(
should_skip
split_any_all
combine_sets
parse_mime
check_ignore_prefixes
dkim2_canonicalize_header
dkim2_canonicalize_sig_header
field_name
header_values
encode_tag_json
decode_tag_json
fold_header
build_signing_input
extract_mi_version
mi_version_tag
strip_mi_versions
extract_domain
domain_of
to_alabel
from_domain
from_mailbox
from_authentic
from_refusal_note
to_rfc5321_path
relaxed_domain_match
parse_dkim_pubkey
parse_dkim_key_record
key_records
domain_policy
load_private_key
load_private_key_data
signature_algorithm
signature_algorithm_for_key
DKIM2_DRAFT
DKIM2_REPO
DKIM2_DATE
MAX_CHAIN_LENGTH
MAX_CHAIN_NUMBER
MAX_SIGNATURE_ITEMS
FUTURE_LIMIT
EXPIRY
chain_length_error
valid_sequence
UNKEYABLE_SIGNATURE_ERROR
MI_MISSING_M_ERROR
unkeyable_signature_error
chain_number_error
duplicate_number_error
parent_from_matcher
from_pair_error
query_domain_policy
policy_from_source
);
# Provenance emitted in X-DKIM2-Info headers by the milter, the reflector, and
# the mailman/sympa handlers. This is the single source of truth for the Perl
# implementation. DKIM2_DRAFT is the spec revision implemented; DKIM2_DATE is
# the date this implementation's DKIM2 behaviour last changed (a software
# version stamp, not the draft's date) -- bump it on any change to what we
# emit, not only on a spec bump. See ../spec/draft-gondwana-dkim2-debug-header.
use constant DKIM2_DRAFT => 'ietf-dkim-dkim2-spec-09';
use constant DKIM2_REPO => 'github.com/dkim2wg/interop';
use constant DKIM2_DATE => '2026-11-03';
# -09 section 14.3: a DKIM2-Signature whose t= is more than a day after
# delivery is "from the future", and one made more than 14 days before it
# has "expired". Seconds.
use constant FUTURE_LIMIT => 86400;
use constant EXPIRY => 14 * 86400;
# Local policy, not the draft's: a message carrying more Message-Instance or
# DKIM2-Signature fields than this is a PERMERROR, found before any key is
# fetched, signature checked or recipe applied. Every hop costs a verifier a
# DNS lookup, a signature check and an undo of the whole message, and the
# sender chooses how many hops there are.
use constant MAX_CHAIN_LENGTH => 32;
# The largest number a DKIM2-Signature i= or m=, or a Message-Instance m=, may
# be written as: at most two ASCII digits naming 1..99. Anything else is
# malformed before it is ever compared with MAX_CHAIN_LENGTH, and is never
# converted to a number big enough to be a loop bound.
use constant MAX_CHAIN_NUMBER => 99;
# Local policy, not the draft's: the most items one DKIM2-Signature's s= may
# carry. The draft sets no limit (-08 dropped it), and every item may cost a key lookup and a
# signature check, so a crafted s= of thousands of selectors would cost
# thousands of each (the -08 contract, D3). The same limit as the key
# prefetch's; refused before any key is fetched.
use constant MAX_SIGNATURE_ITEMS => 8;
# The PERMERROR for a message over MAX_CHAIN_LENGTH, or undef. Takes an
lib/Mail/DKIM2/Common.pm view on Meta::CPAN
# Strip Message-Instance headers with the given version numbers from a raw message string.
# The message must use CRLF line endings. Returns the modified message string.
sub strip_mi_versions {
my ($message, @versions) = @_;
return $message unless @versions;
my %to_strip = map { $_ => 1 } @versions;
my $EOL = "\015\012";
my $sep_pos = index($message, $EOL . $EOL);
return $message if $sep_pos < 0;
my $hdrs_raw = substr($message, 0, $sep_pos);
my $rest = substr($message, $sep_pos); # includes blank line + body
# Parse folded headers into whole-header strings
my @entries;
my $cur = '';
for my $line (split /\015\012/, $hdrs_raw, -1) {
if ($line =~ /^[ \t]/ && $cur ne '') {
$cur .= $EOL . $line;
} else {
push @entries, $cur if $cur ne '';
$cur = $line;
}
}
push @entries, $cur if $cur ne '';
# Keep all headers except Message-Instance ones with versions in %to_strip
my @kept;
for my $entry (@entries) {
if ($entry =~ /^Message-Instance:/i) {
my ($val) = $entry =~ /^[^:]+:\s*(.*)/s;
if (defined $val) {
$val =~ s/\015?\012[ \t]+/ /g; # unfold for version extraction
my $ver = extract_mi_version($val);
next if defined $ver && $to_strip{$ver};
}
}
push @kept, $entry;
}
return join($EOL, @kept) . $rest;
}
1;
__END__
=encoding utf8
=head1 NAME
Mail::DKIM2::Common - Constants, chain numbering and the any/all groups shared by the DKIM2 modules
=head1 SYNOPSIS
use Mail::DKIM2::Common qw(DKIM2_DRAFT MAX_CHAIN_LENGTH chain_number_error);
# Everything that was here before 0.31 can still be imported from here:
use Mail::DKIM2::Common qw(fold_header load_private_key relaxed_domain_match);
=head1 DESCRIPTION
What the other modules share and none of them owns: the protocol's
constants and this implementation's limits, the numbering of
Message-Instance and DKIM2-Signature fields (and the PERMERROR strings for
fields that cannot be numbered), and the "any of these groups, all of each"
combination that C<h=> and C<s=> both use. Nothing is exported by default.
Until 0.31 this module also held the functions that are now in
=over 4
=item L<Mail::DKIM2::Canon>
header canonicalization, the fields that are not hashed, the signing input;
=item L<Mail::DKIM2::Fold>
C<fold_header>;
=item L<Mail::DKIM2::Address>
envelope paths, domains, the relaxed domain match, the Authentic From check;
=item L<Mail::DKIM2::Key>
private keys, key records, Domain Policy Records, signing algorithms;
=item L<Mail::DKIM2::RecipeJSON>
the base64 JSON of an C<r=> tag.
=back
Every one of them is still importable from here, and callable as
C<Mail::DKIM2::Common::NAME>, so existing callers need no change; they are
documented where they now live.
This module implements draft-ietf-dkim-dkim2-spec-09; see L<Mail::DKIM2/STATUS>
for what that means for the wire format and the API, and
L<Mail::DKIM2/CONVENTIONS> for the option, input and error conventions every
module here follows.
=head1 CONSTANTS
=head2 DKIM2_DRAFT, DKIM2_REPO, DKIM2_DATE
The spec revision implemented (C<ietf-dkim-dkim2-spec-09>), where the
code lives, and the date this implementation's DKIM2 behaviour last
changed. Emitted in X-DKIM2-Info debug headers
(draft-gondwana-dkim2-debug-header).
=head2 MAX_CHAIN_LENGTH
32: a message carrying more Message-Instance or DKIM2-Signature fields
than this, or an C<i=> or C<m=> naming a larger number, is a PERMERROR,
found before any key is fetched. Local policy, not spec.
=head2 MAX_CHAIN_NUMBER
( run in 1.104 second using v1.01-cache-2.11-cpan-8379b9b8ec2 )