Package net.markenwerk.utils.mail.dkim
Class DkimSigner
- java.lang.Object
-
- net.markenwerk.utils.mail.dkim.DkimSigner
-
public class DkimSigner extends Object
Main class providing a signature according to DKIM RFC 4871.- Since:
- 1.0.0
- Author:
- Torsten Krause (tk at markenwerk dot net), Florian Sager
-
-
Constructor Summary
Constructors Constructor Description DkimSigner(String signingDomain, String selector, File derFile)Created a newDkimSignerfor the given signing domain and selector with the given DER encoded RSA private Key.DkimSigner(String signingDomain, String selector, InputStream derStream)Created a newDkimSignerfor the given signing domain and selector with the given DER encoded RSA private Key.DkimSigner(String signingDomain, String selector, RSAPrivateKey privateKey)Created a newDkimSignerfor the given signing domain and selector with the givenRSAPrivateKey.DkimSigner(String signingDomain, String selector, net.i2p.crypto.eddsa.EdDSAPrivateKey privateKey)Created a newDkimSignerfor the given signing domain and selector with the givenRSAPrivateKey.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidaddHeaderToSign(String header)Adds a header to the set of headers that will be included in the signature, if present.CanonicalizationgetBodyCanonicalization()Returns the configuredCanonicalizationto be used for the body.CanonicalizationgetHeaderCanonicalization()Returns the configuredCanonicalizationto be used for the headers.StringgetIdentity()Returns the configured identity parameter.booleangetLengthParam()Returns the configured length parameter.SigningAlgorithmgetSigningAlgorithm()Returns the configuredSigningAlgorithm.booleanisCheckDomainKey()Returns whether the domain key should be retrieved and checked.booleanisCopyHeaderFields()Returns the configured z parameter.booleanisZParam()Deprecated.UseisCopyHeaderFields()instead.voidremoveHeaderToSign(String header)Removes a header from the set of headers that will be included in the signature, unless it is one of the required headers ('From', 'To', 'Subject').voidsetBodyCanonicalization(Canonicalization canonicalization)Sets theCanonicalizationto be used for the body.voidsetCheckDomainKey(boolean checkDomainKey)Sets, whether the domain key should be retrieved and checked.voidsetCopyHeaderFields(boolean copyHeaderFields)Sets the z parameter to be used.voidsetHeaderCanonicalization(Canonicalization canonicalization)Sets theCanonicalizationto be used for the headers.voidsetIdentity(String identity)Sets the identity parameter to be used.voidsetLengthParam(boolean lengthParam)Sets the length parameter to be used.voidsetSigningAlgorithm(SigningAlgorithm signingAlgorithm)Sets theSigningAlgorithmto be used.voidsetZParam(boolean zParam)Deprecated.UsesetCopyHeaderFields(boolean)instead.protected Stringsign(DkimMessage message)Returns the DKIM signature header line.
-
-
-
Constructor Detail
-
DkimSigner
public DkimSigner(String signingDomain, String selector, File derFile) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, DkimException
Created a newDkimSignerfor the given signing domain and selector with the given DER encoded RSA private Key.- Parameters:
signingDomain- The signing domain to be used.selector- The selector to be used.derFile- AFilethat contains the DER encoded RSA private key to be used.- Throws:
IOException- If reading the content of the givenFilefailed.NoSuchAlgorithmException- If the RSA algorithm is not supported.InvalidKeySpecException- If the content of the givenFilecouldn't be interpreted as an RSA private key.DkimException- If the given signing domain is invalid.
-
DkimSigner
public DkimSigner(String signingDomain, String selector, InputStream derStream) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException
Created a newDkimSignerfor the given signing domain and selector with the given DER encoded RSA private Key.- Parameters:
signingDomain- The signing domain to be used.selector- The selector to be used.derStream- AInputStreamthat yields the DER encoded RSA private key to be used. TheInputStreamwill be closed after it has been read.- Throws:
IOException- If reading the content of the givenInputStreamfailed.NoSuchAlgorithmException- If the RSA algorithm is not supported.InvalidKeySpecException- If the content of the givenInputStreamcouldn't be interpreted as an RSA private key.DkimException- If the given signing domain is invalid.
-
DkimSigner
public DkimSigner(String signingDomain, String selector, RSAPrivateKey privateKey) throws DkimException
Created a newDkimSignerfor the given signing domain and selector with the givenRSAPrivateKey.- Parameters:
signingDomain- The signing domain to be used.selector- The selector to be used.privateKey- TheRSAPrivateKeyto be used to signDkimMessages.- Throws:
DkimException- If the given signing domain is invalid.
-
DkimSigner
public DkimSigner(String signingDomain, String selector, net.i2p.crypto.eddsa.EdDSAPrivateKey privateKey) throws DkimException
Created a newDkimSignerfor the given signing domain and selector with the givenRSAPrivateKey.- Parameters:
signingDomain- The signing domain to be used.selector- The selector to be used.privateKey- TheRSAPrivateKeyto be used to signDkimMessages.- Throws:
DkimException- If the given signing domain is invalid.
-
-
Method Detail
-
addHeaderToSign
public void addHeaderToSign(String header)
Adds a header to the set of headers that will be included in the signature, if present.- Parameters:
header- The name of the header.
-
removeHeaderToSign
public void removeHeaderToSign(String header)
Removes a header from the set of headers that will be included in the signature, unless it is one of the required headers ('From', 'To', 'Subject').- Parameters:
header- The name of the header.
-
getSigningAlgorithm
public SigningAlgorithm getSigningAlgorithm()
Returns the configuredSigningAlgorithm.- Returns:
- The configured
SigningAlgorithm.
-
setSigningAlgorithm
public void setSigningAlgorithm(SigningAlgorithm signingAlgorithm) throws DkimException
Sets theSigningAlgorithmto be used.- Parameters:
signingAlgorithm- TheSigningAlgorithmto be used.- Throws:
DkimException- If either the signing algorithm or the hashing algorithm is not supported or theSignaturecouldn't be initialized.
-
getHeaderCanonicalization
public Canonicalization getHeaderCanonicalization()
Returns the configuredCanonicalizationto be used for the headers.- Returns:
- The configured
Canonicalizationto be used for the headers.
-
setHeaderCanonicalization
public void setHeaderCanonicalization(Canonicalization canonicalization)
Sets theCanonicalizationto be used for the headers.- Parameters:
canonicalization- TheCanonicalizationto be used for the headers.
-
getBodyCanonicalization
public Canonicalization getBodyCanonicalization()
Returns the configuredCanonicalizationto be used for the body.- Returns:
- The configured
Canonicalizationto be used for the body.
-
setBodyCanonicalization
public void setBodyCanonicalization(Canonicalization canonicalization)
Sets theCanonicalizationto be used for the body.- Parameters:
canonicalization- TheCanonicalizationto be used for the body.
-
getIdentity
public String getIdentity()
Returns the configured identity parameter.- Returns:
- The configured identity parameter.
-
setIdentity
public void setIdentity(String identity) throws DkimException
Sets the identity parameter to be used.- Parameters:
identity- The identity to be used.- Throws:
DkimException- If the given identity parameter isn't the signing domain of thisDkimSigneror an subdomain thereof.
-
getLengthParam
public boolean getLengthParam()
Returns the configured length parameter.- Returns:
- The configured length parameter.
-
setLengthParam
public void setLengthParam(boolean lengthParam)
Sets the length parameter to be used.- Parameters:
lengthParam- The length parameter to be used.
-
isZParam
@Deprecated public boolean isZParam()
Deprecated.UseisCopyHeaderFields()instead.Returns the configured z parameter.- Returns:
- The configured z parameter.
-
setZParam
@Deprecated public void setZParam(boolean zParam)
Deprecated.UsesetCopyHeaderFields(boolean)instead.Sets the z parameter to be used.- Parameters:
zParam- The z parameter to be used.
-
isCopyHeaderFields
public boolean isCopyHeaderFields()
Returns the configured z parameter.- Returns:
- The configured z parameter.
-
setCopyHeaderFields
public void setCopyHeaderFields(boolean copyHeaderFields)
Sets the z parameter to be used.- Parameters:
zParam- The z parameter to be used.
-
isCheckDomainKey
public boolean isCheckDomainKey()
Returns whether the domain key should be retrieved and checked.- Returns:
- Whether the domain key should be retrieved and checked.
- See Also:
DomainKey#check(String, RSAPrivateKey)
-
setCheckDomainKey
public void setCheckDomainKey(boolean checkDomainKey)
Sets, whether the domain key should be retrieved and checked.- Parameters:
checkDomainKey- Whether the domain key should be retrieved and checked.
-
sign
protected String sign(DkimMessage message) throws MessagingException
Returns the DKIM signature header line.- Parameters:
message- TheDkimMessageto sign.- Returns:
- The DKIM signature header line
- Throws:
DkimSigningException- If theDkimMessagecouldn't be signed.MessagingException
-
-