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 newDkimSigner
for the given signing domain and selector with the given DER encoded RSA private Key.DkimSigner(String signingDomain, String selector, InputStream derStream)
Created a newDkimSigner
for the given signing domain and selector with the given DER encoded RSA private Key.DkimSigner(String signingDomain, String selector, RSAPrivateKey privateKey)
Created a newDkimSigner
for the given signing domain and selector with the givenRSAPrivateKey
.DkimSigner(String signingDomain, String selector, net.i2p.crypto.eddsa.EdDSAPrivateKey privateKey)
Created a newDkimSigner
for the given signing domain and selector with the givenRSAPrivateKey
.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
addHeaderToSign(String header)
Adds a header to the set of headers that will be included in the signature, if present.Canonicalization
getBodyCanonicalization()
Returns the configuredCanonicalization
to be used for the body.Canonicalization
getHeaderCanonicalization()
Returns the configuredCanonicalization
to be used for the headers.String
getIdentity()
Returns the configured identity parameter.boolean
getLengthParam()
Returns the configured length parameter.SigningAlgorithm
getSigningAlgorithm()
Returns the configuredSigningAlgorithm
.boolean
isCheckDomainKey()
Returns whether the domain key should be retrieved and checked.boolean
isCopyHeaderFields()
Returns the configured z parameter.boolean
isZParam()
Deprecated.UseisCopyHeaderFields()
instead.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').void
setBodyCanonicalization(Canonicalization canonicalization)
Sets theCanonicalization
to be used for the body.void
setCheckDomainKey(boolean checkDomainKey)
Sets, whether the domain key should be retrieved and checked.void
setCopyHeaderFields(boolean copyHeaderFields)
Sets the z parameter to be used.void
setHeaderCanonicalization(Canonicalization canonicalization)
Sets theCanonicalization
to be used for the headers.void
setIdentity(String identity)
Sets the identity parameter to be used.void
setLengthParam(boolean lengthParam)
Sets the length parameter to be used.void
setSigningAlgorithm(SigningAlgorithm signingAlgorithm)
Sets theSigningAlgorithm
to be used.void
setZParam(boolean zParam)
Deprecated.UsesetCopyHeaderFields(boolean)
instead.protected String
sign(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 newDkimSigner
for 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
- AFile
that contains the DER encoded RSA private key to be used.- Throws:
IOException
- If reading the content of the givenFile
failed.NoSuchAlgorithmException
- If the RSA algorithm is not supported.InvalidKeySpecException
- If the content of the givenFile
couldn'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 newDkimSigner
for 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
- AInputStream
that yields the DER encoded RSA private key to be used. TheInputStream
will be closed after it has been read.- Throws:
IOException
- If reading the content of the givenInputStream
failed.NoSuchAlgorithmException
- If the RSA algorithm is not supported.InvalidKeySpecException
- If the content of the givenInputStream
couldn'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 newDkimSigner
for the given signing domain and selector with the givenRSAPrivateKey
.- Parameters:
signingDomain
- The signing domain to be used.selector
- The selector to be used.privateKey
- TheRSAPrivateKey
to be used to signDkimMessage
s.- 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 newDkimSigner
for the given signing domain and selector with the givenRSAPrivateKey
.- Parameters:
signingDomain
- The signing domain to be used.selector
- The selector to be used.privateKey
- TheRSAPrivateKey
to be used to signDkimMessage
s.- 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 theSigningAlgorithm
to be used.- Parameters:
signingAlgorithm
- TheSigningAlgorithm
to be used.- Throws:
DkimException
- If either the signing algorithm or the hashing algorithm is not supported or theSignature
couldn't be initialized.
-
getHeaderCanonicalization
public Canonicalization getHeaderCanonicalization()
Returns the configuredCanonicalization
to be used for the headers.- Returns:
- The configured
Canonicalization
to be used for the headers.
-
setHeaderCanonicalization
public void setHeaderCanonicalization(Canonicalization canonicalization)
Sets theCanonicalization
to be used for the headers.- Parameters:
canonicalization
- TheCanonicalization
to be used for the headers.
-
getBodyCanonicalization
public Canonicalization getBodyCanonicalization()
Returns the configuredCanonicalization
to be used for the body.- Returns:
- The configured
Canonicalization
to be used for the body.
-
setBodyCanonicalization
public void setBodyCanonicalization(Canonicalization canonicalization)
Sets theCanonicalization
to be used for the body.- Parameters:
canonicalization
- TheCanonicalization
to 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 thisDkimSigner
or 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
- TheDkimMessage
to sign.- Returns:
- The DKIM signature header line
- Throws:
DkimSigningException
- If theDkimMessage
couldn't be signed.MessagingException
-
-