Mail-DKIM2

 view release on metacpan or  search on metacpan

t/lib/Spec07Fixture.pm  view on Meta::CPAN

package Spec07Fixture;
# Messages for the draft -08 shared behaviour tests (the U1..D1 table every
# implementation in the interop repository writes). The table fixes the
# domains (example.com, example.org, lists.example.org, fwd.example.net, ...),
# so instead of DNS entries for each, every signature here is made with
# test1.dkim2.com's sel1 RSA key whatever its d= says, and pubkey_cb() answers
# that key for every domain and selector.
#
# add_sig() builds the DKIM2-Signature by hand, so a test can make any chain
# it likes -- a broken Chain of Custody, an mf= in odd case, a From that is
# not Authentic -- which Mail::DKIM2::Signer would refuse or normalise.
use strict;
use warnings;
use MIME::Base64 qw(encode_base64);
use Crypt::PK::RSA;
use Mail::DKIM2::Common qw(parse_mime build_signing_input extract_mi_version);
use Mail::DKIM2::MessageInstance;
use Mail::DKIM2::Signature;
use Mail::DKIM2::Verifier;
use DKIM2TestKeys;

sub key { return DKIM2TestKeys::private_key('test1.dkim2.com', 'sel1') }

# A PubkeyCallback answering the one test key for any domain.
sub pubkey_cb {
    my $der = key()->export_key_der('public');
    my $pub = Crypt::PK::RSA->new(\$der);
    return sub { return $pub };
}

# message(%o): a plain message, CRLF throughout. Options from, to, subject,
# extra (more header fields, each ending in CRLF) and body.
sub message {
    my (%o) = @_;
    return 'From: ' . ($o{from} // 'A <a@example.com>') . "\r\n"
         . 'To: ' . ($o{to} // 'list@lists.example.org') . "\r\n"
         . 'Subject: ' . ($o{subject} // 'test') . "\r\n"
         . ($o{extra} // '')
         . "\r\n" . ($o{body} // "body\r\n");
}

# add_mi($cur[, $prev]): $cur with a new top Message-Instance -- m=1 over
# $cur, or, given $prev ($cur before this hop's change, carrying the same
# instances and signatures), the next m= with the Recipes back to $prev.
sub add_mi {
    my ($cur, $prev) = @_;
    my $mi = defined $prev
        ? Mail::DKIM2::MessageInstance->calculate(parse_mime($cur), parse_mime($prev))
        : Mail::DKIM2::MessageInstance->calculate(parse_mime($cur));
    return 'Message-Instance: ' . $mi->as_string . "\r\n" . $cur;
}

# mi_with_recipe($text, $json): $text with a hand-made next Message-Instance
# whose hashes match $text and whose r= is base64($json), verbatim.
sub mi_with_recipe {
    my ($text, $json) = @_;
    my $msg = parse_mime($text);
    my @v = map { extract_mi_version($_) } $msg->header_raw('Message-Instance');
    my ($top) = sort { $b <=> $a } @v;
    my $m = ($top // 0) + 1;
    my $h = Mail::DKIM2::MessageInstance::h_digest($msg, 'sha256');
    my $b = Mail::DKIM2::MessageInstance::b_digest($msg, 'sha256');
    return "Message-Instance: m=$m; h=sha256:$h:$b; r=" . encode_base64($json, '')
         . ";\r\n" . $text;
}

# The decoded r= JSON of the top Message-Instance of $text.
sub top_recipe_json {
    my ($text) = @_;
    my @mi = parse_mime($text)->header_raw('Message-Instance');
    my ($top) = sort { extract_mi_version($b) <=> extract_mi_version($a) } @mi;
    my ($r) = $top =~ /\br=([^;]*)/ or return;
    $r =~ s/\s//g;
    return MIME::Base64::decode_base64($r);
}

# add_sig($text, %o): $text with a new DKIM2-Signature on top, i= the next
# one. Options: d (required), mf (a raw path, e.g. '<a@example.com>'), rt
# (arrayref of raw paths), nd, t (default now), f (arrayref of flags), m
# (default the top Message-Instance's m=). mf and rt go in exactly as given.
sub add_sig {
    my ($text, %o) = @_;



( run in 0.563 second using v1.01-cache-2.11-cpan-8379b9b8ec2 )