SYNOPSIS

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

  # create a signer object
  my $dkim = Mail::DKIM::Signer->new(
                  Algorithm => "rsa-sha1",
                  Method => "relaxed",
                  Domain => "example.org",
                  Selector => "selector1",
                  KeyFile => "private.key",
             );

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

  # 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
      $dkim->PRINT("$_\015\012");
  }
  $dkim->CLOSE;

  # what is the signature result?
  my $signature = $dkim->signature;
  print $signature->as_string;

DESCRIPTION

This class is the part of Mail::DKIM responsible for generating signatures for a given message. You create an object of this class, specifying the parameters of the signature you wish to create, or specifying a callback function so that the signature parameters can be determined later. Next, you feed it the entire message using \*(L"\s-1PRINT\s0()\*(R", completing with \*(L"\s-1CLOSE\s0()\*(R". Finally, use the \*(L"signatures()\*(R" method to access the generated signatures.

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

\fInew()\fP

Construct an object-oriented signer.

# create a signer using the default policy my $dkim = Mail::DKIM::Signer->new( Algorithm => "rsa-sha1", Method => "relaxed", Domain => "example.org", Selector => "selector1", KeyFile => "private.key", );

# create a signer using a custom policy my $dkim = Mail::DKIM::Signer->new( Policy => $policyfn, );

The \*(L"default policy\*(R" is to create a \s-1DKIM\s0 signature using the specified parameters, but only if the message's sender matches the domain. The following parameters can be passed to this new() method to influence the resulting signature: Algorithm, Method, Domain, Selector, KeyFile, Identity, Timestamp.

If you want different behavior, you can provide a \*(L"signer policy\*(R" instead. A signer policy is a subroutine or class that determines signature parameters after the message's headers have been parsed. See the section \*(L"\s-1SIGNER\s0 \s-1POLICIES\s0\*(R" below for more information.

See Mail::DKIM::SignerPolicy for more information about policy objects.

In addition to the parameters demonstrated above, the following are recognized:

Key

rather than using \*(C`KeyFile\*(C', use \*(C`Key\*(C' to use an already-loaded Mail::DKIM::PrivateKey object.

METHODS

\s-1\fIPRINT\s0()\fP

Feed part of the message to the signer.

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

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

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

\s-1\fICLOSE\s0()\fP

Call this when finished feeding in the message.

$dkim->CLOSE;

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

\fIadd_signature()\fP

Used by signer policy to create a new signature.

$dkim->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.

\fIalgorithm()\fP

Get or set the selected algorithm.

$alg = $dkim->algorithm;

$dkim->algorithm("rsa-sha1");

\fIdomain()\fP

Get or set the selected domain.

$alg = $dkim->domain;

$dkim->domain("example.org");

\fIload()\fP

Load the entire message from a file handle.

$dkim->load($file_handle);

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

\fIheaders()\fP

Determine which headers to put in signature.

my $headers = $dkim->headers;

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

\fIkey()\fP

Get or set the private key object.

my $key = $dkim->key;

$dkim->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 \*(L"key_file()\*(R".

\fIkey_file()\fP

Get or set the filename containing the private key.

my $filename = $dkim->key_file;

$dkim->key_file("private.key");

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

\fImethod()\fP

Get or set the selected canonicalization method.

$alg = $dkim->method;

$dkim->method("relaxed");

\fImessage_originator()\fP

Access the \*(L"From\*(R" header.

my $address = $dkim->message_originator;

Returns the \*(L"originator address\*(R" 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 = $dkim->message_originator->address;

See also \*(L"message_sender()\*(R".

\fImessage_sender()\fP

Access the \*(L"From\*(R" or \*(L"Sender\*(R" header.

my $address = $dkim->message_sender;

Returns the \*(L"sender\*(R" 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 \*(L"sender\*(R" 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 \*(L"sender\*(R" would be the secretary and the \*(L"originator\*(R" would be the actual author.

\fIselector()\fP

Get or set the current key selector.

$alg = $dkim->selector;

$dkim->selector("alpha");

\fIsignature()\fP

Access the generated signature object.

my $signature = $dkim->signature;

Returns the generated signature. The signature is an object of type Mail::DKIM::Signature. If multiple signatures were generated, this method returns the last one.

The signature (as text) should be prepended to the message to make the resulting message. At the very least, it should precede any headers that were signed.

\fIsignatures()\fP

Access list of generated signature objects.

my @signatures = $dkim->signatures;

Returns all generated signatures, as a list.

SIGNER POLICIES

The new() constructor takes an optional Policy argument. This can be a Perl object or class with an apply() method, or just a simple subroutine reference. The method/subroutine will be called with the signer object as an argument. The policy is responsible for checking the message and specifying signature parameters. The policy must return a nonzero value to create the signature, otherwise no signature will be created. E.g.,

my $policyfn = sub { my $dkim = shift;

# specify signature parameters $dkim->algorithm("rsa-sha1"); $dkim->method("relaxed"); $dkim->domain("example.org"); $dkim->selector("mx1");

# return true value to create the signature return 1; };

Or the policy object can actually create the signature, using the add_signature method within the policy object. If you add a signature, you do not need to return a nonzero value. This mechanism can be utilized to create multiple signatures, or to create the older DomainKey-style signatures.

my $policyfn = sub { my $dkim = shift; $dkim->add_signature( new Mail::DKIM::Signature( Algorithm => "rsa-sha1", Method => "relaxed", Headers => $dkim->headers, Domain => "example.org", Selector => "mx1", )); $dkim->add_signature( new Mail::DKIM::DkSignature( Algorithm => "rsa-sha1", Method => "nofws", Headers => $dkim->headers, Domain => "example.org", Selector => "mx1", )); return; };

If no policy is specified, the default policy is used. The default policy signs every message using the domain, algorithm, method, and selector specified in the new() constructor.

RELATED TO Mail::DKIM::Signer…

Mail::DKIM::SignerPolicy

AUTHOR

Jason Long, <[email protected]>

COPYRIGHT AND LICENSE

Copyright (C) 2006-2007 by Messiah College

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.