NAME

Mail::DKIM::ARC::Signer - generates a DKIM signature for a message

VERSION

version 1.20240923

SYNOPSIS

use Mail::DKIM::ARC::Signer;
use Mail::DKIM::TextWrap;  #recommended

# create a signer object
my $signer = Mail::DKIM::ARC::Signer->new(
                Algorithm => 'rsa-sha256',
                Chain => 'none',    # or pass|fail|ar
                Domain => 'example.org',
                SrvId => 'example.org',
                Selector => 'selector1',
                KeyFile => 'private.key',
                Headers => 'x-header:x-header2',
           );

# read an email from a file handle
$signer->load(*STDIN);

# NOTE: any email being ARC signed must have an Authentication-Results
# header so that the ARC seal can cover those results copied into
# an ARC-Authentication-Results header.

# or read an email and pass it into the signer, one line at a time
while (<STDIN>)
{
    # remove local line terminators
    chomp;
    s/\015$//;

    # use SMTP line terminators
    $signer->PRINT("$_\015\012");
}
$signer->CLOSE;

die 'Failed' $signer->result_detail() unless $signer->result() eq 'sealed';

# Get all the signature headers to prepend to the message
# ARC-Seal, ARC-Message-Signature and ARC-Authentication-Results
# in that order.
print $signer->as_string;

DESCRIPTION

This class is the part of Mail::DKIM responsible for generating ARC Seals for a given message. You create an object of this class, specifying the parameters for the ARC-Message-Signature you wish to create.

You also need to pass the 'Chain' value (pass or fail) from validation of the previous ARC-Seals on the message.

Next, you feed it the entire message using "PRINT()", completing with "CLOSE()".

Finally, use the "as_string()" method to get the new ARC headers.

Note: you can only seal a message which has already had an Authentication-Results header added, either by using "PRINT()" to pre-feed it into this module, or by adding a message which has already been authenticated by your inbound scanning mechanisms.

It is not necessary to ARC-Seal a message which already has DKIM signatures if you are not modifying the message and hence breaking the existing DKIM-Signature or top ARC-Message-Signature on the email.

Pretty Signatures

Mail::DKIM includes a signature-wrapping module (which inserts linebreaks into the generated signature so that it looks nicer in the resulting message. To enable this module, simply call

use Mail::DKIM::TextWrap;

in your program before generating the signature.

CONSTRUCTOR

new()

Construct an object-oriented signer.

# create a signer using the default policy
my $signer = Mail::DKIM::ARC::Signer->new(
                Algorithm => 'rsa-sha256',
                Chain => 'none',    # or pass|fail|ar
                Domain => 'example.org',
                SrvId => 'example.org',
                Selector => 'selector1',
                KeyFile => 'private.key',
                Headers => 'x-header:x-header2',
           );
Key

rather than using KeyFile, use Key to use an already-loaded Mail::DKIM::PrivateKey object.

Chain

The cv= value for the Arc-Seal header. "ar" means to copy it from an Authentication-Results header, or use none if there isn't one.

SrvId

The authserv-id in the Authentication-Results headers, defaults to Domain.

Headers

A colon separated list of headers to sign, this is added to the list of default headers as shown in in the DKIM specification.

For each specified header all headers of that type which are present in the message will be signed, but we will not oversign or sign headers which are not present.

If you require greater control over signed headers please use the extended_headers() method instead.

The list of headers signed by default is as follows

From Sender Reply-To Subject Date
Message-ID To Cc MIME-Version
Content-Type Content-Transfer-Encoding Content-ID Content-Description
Resent-Date Resent-From Resent-Sender Resent-To Resent-cc
Resent-Message-ID
In-Reply-To References
List-Id List-Help List-Unsubscribe List-Subscribe
List-Post List-Owner List-Archive
Tags

An optional hashref of additional tags to be added to the ARC signature generated.

SealTags

An optional hashref of additional tags to be added to the ARC seal generated.

METHODS

PRINT()

Feed part of the message to the signer.

$signer->PRINT("a line of the message\015\012");

Feeds content of the message being signed into the signer. The API is designed this way so that the entire message does NOT need to be read into memory at once.

Please note that although the PRINT() method expects you to use SMTP-style line termination characters, you should NOT use the SMTP-style dot-stuffing technique described in RFC 2821 section 4.5.2. Nor should you use a <CR><LF>.<CR><LF> sequence to terminate the message.

CLOSE()

Call this when finished feeding in the message.

$signer->CLOSE;

This method finishes the canonicalization process, computes a hash, and generates a signature.

extended_headers()

This method overrides the headers to be signed and allows more control than is possible with the Headers property in the constructor.

The method expects a HashRef to be passed in.

The Keys are the headers to sign, and the values are either the number of headers of that type to sign, or the special values '*' and '+'.

* will sign ALL headers of that type present in the message.

+ will sign ALL + 1 headers of that type present in the message to prevent additional headers being added.

You may override any of the default headers by including them in the hashref, and disable them by giving them a 0 value.

Keys are case insensitive with the values being added upto the highest value.

Headers => {
    'X-test'  => '*',
    'x-test'  => '1',
    'Subject' => '+',
    'Sender'  => 0,
},

add_signature()

Used by signer policy to create a new signature.

$signer->add_signature(new Mail::DKIM::Signature(...));

Signer policies can use this method to specify complete parameters for the signature to add, including what type of signature. For more information, see Mail::DKIM::SignerPolicy.

algorithm()

Get or set the selected algorithm.

$alg = $signer->algorithm;

$signer->algorithm('rsa-sha256');

domain()

Get or set the selected domain.

$alg = $signer->domain;

$signer->domain('example.org');

load()

Load the entire message from a file handle.

$signer->load($file_handle);

Reads a complete message from the designated file handle, feeding it into the signer. The message must use <CRLF> line terminators (same as the SMTP protocol).

headers()

Determine which headers to put in signature.

my $headers = $signer->headers;

This is a string containing the names of the header fields that will be signed, separated by colons.

key()

Get or set the private key object.

my $key = $signer->key;

$signer->key(Mail::DKIM::PrivateKey->load(File => 'private.key'));

The key object can be any object that implements the sign_digest() method. (Providing your own object can be useful if your actual keys are stored out-of-process.)

If you use this method to specify a private key, do not use "key_file()".

key_file()

Get or set the filename containing the private key.

my $filename = $signer->key_file;

$signer->key_file('private.key');

If you use this method to specify a private key file, do not use "key()".

message_originator()

Access the "From" header.

my $address = $signer->message_originator;

Returns the "originator address" found in the message, as a Mail::Address object. This is typically the (first) name and email address found in the From: header. If there is no From: header, then an empty Mail::Address object is returned.

To get just the email address part, do:

my $email = $signer->message_originator->address;

See also "message_sender()".

message_sender()

Access the "From" or "Sender" header.

my $address = $dkim->message_sender;

Returns the "sender" found in the message, as a Mail::Address object. This is typically the (first) name and email address found in the Sender: header. If there is no Sender: header, it is the first name and email address in the From: header. If neither header is present, then an empty Mail::Address object is returned.

To get just the email address part, do:

my $email = $dkim->message_sender->address;

The "sender" is the mailbox of the agent responsible for the actual transmission of the message. For example, if a secretary were to send a message for another person, the "sender" would be the secretary and the "originator" would be the actual author.

selector()

Get or set the current key selector.

$alg = $dkim->selector;

$dkim->selector('alpha');

signatures()

Access list of generated signature objects.

my @signatures = $dkim->signatures;

Returns all generated signatures, as a list.

as_string()

Returns the new ARC headers

my $pre_headers = $signer->as_string();

The headers are separated by \015\012 (SMTP line separator) including a trailing separator, so can be directly injected in front of the raw message.

as_strings()

Returns the new ARC headers

my @pre_headers = $signer->as_string();

The headers are returned as a list so you can add whatever line ending your local MTA prefers.

AUTHORS

  • Jason Long <jason@long.name>

  • Marc Bradshaw <marc@marcbradshaw.net>

  • Bron Gondwana <brong@fastmailteam.com> (ARC)

THANKS

Work on ensuring that this module passes the ARC test suite was generously sponsored by Valimail (https://www.valimail.com/)

COPYRIGHT AND LICENSE

  • Copyright (C) 2013 by Messiah College

  • Copyright (C) 2010 by Jason Long

  • Copyright (C) 2017 by Standcore LLC

  • Copyright (C) 2020 by FastMail Pty Ltd

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.6 or, at your option, any later version of Perl 5 you may have available.