badpenguin.dkim
Class Signer

java.lang.Object
  extended by badpenguin.dkim.Signer

public class Signer
extends java.lang.Object

The Signer class implements the necessary methods to create a DKIM or DomainKey signature header for a given message. It tries to be compliant with version 1 of the DKIM specification. See: http://www.ietf.org/rfc/rfc4871.txt

Author:
Mark Boddington <dk_NO_im@_SP_bad_AM_penguin.co.uk>
http://www.badpenguin.co.uk;

Constructor Summary
Signer(DkimSignature DKIMSig, java.security.PrivateKey key)
          Create a Signer object using the specified DkimSignature object, and the PrivateKey.
Signer(java.lang.String selector, java.lang.String domain, java.lang.String alg, java.security.PrivateKey key)
          Creates a Signer object using the specified Domain, Selector and PrivateKey.
Signer(java.lang.String selector, java.lang.String domain, java.lang.String headers, java.lang.String alg, java.security.PrivateKey key)
          Creates a Signer object using the specified Domain, Selector, Algorithm and PrivateKey.
 
Method Summary
 java.lang.String[] getAdditionalHeaders()
          Get the current additional headers, which will appended to the H tag, and included in the signature.
 boolean getAutoHeaders()
          Get the current autoHeaders setting for this Signer.
 boolean getAutoTimeStamp()
          Get the current setting for autoTimeStamp.
 DkimSignature getDkimSignature()
          Get the DkimSignature object in use by this Signer.
 int getExpireSeconds()
          Get the current value of the Expire Seconds.
 void setAdditionalHeaders(java.lang.String[] headers)
          This method allows you to append extra headers to the ones generated by autoheaders, and should be used in place of the DkimSignature equivalents when the Signer has autoHeaders switched on.
 void setAutoExpire(boolean autoExpire)
          Set whether the signature should automatically calculate an expiration tag.
 void setAutoHeaders(boolean autoHeaders)
          If autoHeaders is on (true), then we will ask the canoncialiser to provide us with the list of headers present in the message, and recommended by the RFC.
 void setAutoTimeStamp(boolean autoTimeStamp)
          Switch time stamp generation on or off.
 void setExpireSeconds(int seconds)
          Set the number of seconds the DkimSignature will be valid for.
 java.lang.String signMail(java.io.InputStream msg)
          Generate a DKIM-Signature header for the provided mail message.
 void signMail(java.io.InputStream msg, java.io.OutputStream out)
          Process the incoming email from the InputStream msg and return a DKIM-Signed copy of the email in the OutputStream out.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Signer

public Signer(java.lang.String selector,
              java.lang.String domain,
              java.lang.String headers,
              java.lang.String alg,
              java.security.PrivateKey key)
       throws DkimException
Creates a Signer object using the specified Domain, Selector, Algorithm and PrivateKey. This constructor also reads in the colon separated list of headers which you wish to be signed. We will use defaults for all other DKIM options, unless you call one of the methods to modify a setting.

Parameters:
selector - - The selector for this DKIM header
domain - - The domain name for this DKIM header
headers - - A colon separated list of headers to sign
alg - - The hashing Algorithm to be used, rsa-sha256 or rsa-sha1
key - - The PrivateKey (which matches the public one found in DNS).
Throws:
java.security.NoSuchAlgorithmException
java.security.InvalidKeyException
DkimException

Signer

public Signer(java.lang.String selector,
              java.lang.String domain,
              java.lang.String alg,
              java.security.PrivateKey key)
       throws DkimException
Creates a Signer object using the specified Domain, Selector and PrivateKey. We will use defaults for all other DKIM options, unless you call one of the methods to modify their settings.

Parameters:
selector - - The selector for this DKIM header
domain - - The domain name for this DKIM header
alg - - The hashing Algorithm to be used, rsa-sha256 or rsa-sha1
key - - The PrivateKey (which matches the public one found in DNS).
Throws:
java.security.NoSuchAlgorithmException
java.security.InvalidKeyException
DkimException

Signer

public Signer(DkimSignature DKIMSig,
              java.security.PrivateKey key)
       throws DkimException
Create a Signer object using the specified DkimSignature object, and the PrivateKey. This version of the constructor allows you to control all of the settings of the DKIM signature..... Because you create it yourself ;-)

Parameters:
DKIMSig -
key -
Throws:
java.security.NoSuchAlgorithmException
java.security.InvalidKeyException
DkimException
Method Detail

setAutoHeaders

public void setAutoHeaders(boolean autoHeaders)
If autoHeaders is on (true), then we will ask the canoncialiser to provide us with the list of headers present in the message, and recommended by the RFC. We will then sign those headers, this will override any headers which were manually set on the DkimSignature object.

Parameters:
autoHeaders -

getAutoHeaders

public boolean getAutoHeaders()
Get the current autoHeaders setting for this Signer.

Returns:
autoHeaders true/false

setAdditionalHeaders

public void setAdditionalHeaders(java.lang.String[] headers)
This method allows you to append extra headers to the ones generated by autoheaders, and should be used in place of the DkimSignature equivalents when the Signer has autoHeaders switched on.

Parameters:
headers -

getAdditionalHeaders

public java.lang.String[] getAdditionalHeaders()
Get the current additional headers, which will appended to the H tag, and included in the signature. These are appended after the Headers which are detected by autoHeaders, and are only applied if autoHeaders is in use.

Returns:
An array containing the headers

setExpireSeconds

public void setExpireSeconds(int seconds)
Set the number of seconds the DkimSignature will be valid for. When the Signature is updated, this value will be added to the current time and used to generate the e(X)pires tag in the DKIM Signature.

Note: This is only used if autoExpire is on, See: setAutoExpire()

Parameters:
seconds -

getExpireSeconds

public int getExpireSeconds()
Get the current value of the Expire Seconds. This is the Validity period of a newly signed message.

Returns:
expireSeconds

setAutoTimeStamp

public void setAutoTimeStamp(boolean autoTimeStamp)
Switch time stamp generation on or off.

Parameters:
autoTimeStamp -

getAutoTimeStamp

public boolean getAutoTimeStamp()
Get the current setting for autoTimeStamp.

Returns:
TimeStamp on/off

setAutoExpire

public void setAutoExpire(boolean autoExpire)
Set whether the signature should automatically calculate an expiration tag. If set then the value of expireSeconds will be used to calculate the value for the DKIM e(X)pire tag. See the setExpireSeconds() method.

Parameters:
autoExpire - on/off

getDkimSignature

public DkimSignature getDkimSignature()
Get the DkimSignature object in use by this Signer.

Returns:
DkimSignature

signMail

public java.lang.String signMail(java.io.InputStream msg)
                          throws DkimException
Generate a DKIM-Signature header for the provided mail message. This function expects to receive a BufferedInputStream containing the raw email message. That means both the headers and the body as transfered in SMTP data, excluding the ending <CRLF>.<CRLF>

Parameters:
msg - - The raw email message (headers + body)
Returns:
DKIM-Signature
Throws:
DkimException

signMail

public void signMail(java.io.InputStream msg,
                     java.io.OutputStream out)
              throws DkimException
Process the incoming email from the InputStream msg and return a DKIM-Signed copy of the email in the OutputStream out. If anything goes wrong, then throw a DkimException

Parameters:
msg - - The incoming message
out - - An output stream to write the signed message to
Throws:
DkimException


© Copyright 2009 Mark Boddington (www.badpenguin.co.uk)