Class DkimSigner

java.lang.Object
org.exoplatform.services.mail.impl.DkimSigner

public final class DkimSigner extends Object
Signs a serialized RFC 5322 message with a DKIM signature (RFC 6376), rsa-sha256 with relaxed/relaxed canonicalization. It needs only the JDK. The signature covers the bytes as they are written on the wire, so the message must be serialized once, signed, and sent as is.
  • Field Details

    • DEFAULT_SIGNED_HEADERS

      public static final List<String> DEFAULT_SIGNED_HEADERS
      Headers signed by default, when they are present in the message.
  • Constructor Details

    • DkimSigner

      public DkimSigner(String domain, String selector, PrivateKey key, List<String> headers)
      Creates a signer for a domain and a selector.
      Parameters:
      domain - the signing domain, the d= tag, as dot separated labels of letters, digits and hyphens (an IDN in its A-label form)
      selector - the selector of the DNS record that holds the public key, written like a domain name
      key - an RSA private key of at least 1024 bits, the floor of RFC 8301 section 3.2 that no mode relaxes
      headers - the names of the headers to sign when present, or null for DEFAULT_SIGNED_HEADERS; From is always signed, and Bcc cannot be signed because the transport removes it before sending
      Throws:
      IllegalArgumentException - when the domain, the selector, a header name or the key is not usable
  • Method Details

    • loadPrivateKey

      public static PrivateKey loadPrivateKey(Path file)
      Reads an unencrypted RSA private key in PKCS#8 PEM format (-----BEGIN PRIVATE KEY-----).
      Parameters:
      file - the PEM file
      Returns:
      the key
      Throws:
      IllegalArgumentException - when the file is not a PKCS#8 RSA key
    • isReadableByOthers

      public static boolean isReadableByOthers(Path file)
      Tells whether the group or the other users can read or write the file.
      Parameters:
      file - the private key file
      Returns:
      true when the group or the others can read or write the file; false when only its owner can, or when the file system has no POSIX permissions to tell (see canCheckPermissions(Path))
    • canCheckPermissions

      public static boolean canCheckPermissions(Path file)
      Tells whether isReadableByOthers(Path) can tell anything about the file.
      Parameters:
      file - the private key file
      Returns:
      false when the file system has no POSIX permissions, so that the permission check is skipped; true otherwise, also when the file cannot be read, which the loading of the key reports
    • parsePrivateKey

      public static PrivateKey parsePrivateKey(String pem)
      Parses an unencrypted RSA private key in PKCS#8 PEM format.
      Parameters:
      pem - the content of the PEM file
      Returns:
      the key
      Throws:
      IllegalArgumentException - when it is not a PKCS#8 RSA key
    • getKeyBits

      public int getKeyBits()
      Gives the size of the signing key.
      Returns:
      the size of the RSA modulus, in bits
    • isWeakKey

      public boolean isWeakKey()
      Tells whether the signing key is weaker than recommended.
      Returns:
      true when the key is accepted but shorter than RECOMMENDED_RSA_BITS
    • getDomain

      public String getDomain()
      Returns:
      the signing domain, the d= tag
    • getSelector

      public String getSelector()
      Returns:
      the selector, the s= tag
    • isAligned

      public boolean isAligned(byte[] message)
      Tells whether the From address of a message is in the signing domain.
      Parameters:
      message - a serialized message, with CRLF line ends
      Returns:
      true when the From address belongs to the signing domain or one of its subdomains, so that the signature is aligned
    • toCrlf

      public static byte[] toCrlf(byte[] message)
      Turns every lone CR and lone LF into CRLF, as the SMTP transport does when it writes the message, so that a signature covers the bytes a receiver gets.
      Parameters:
      message - a serialized message
      Returns:
      the message with CRLF line ends only
    • sign

      public byte[] sign(byte[] message)
      Signs a message.
      Parameters:
      message - a serialized message, with CRLF line ends (see toCrlf(byte[]))
      Returns:
      the same message with a DKIM-Signature header in front of it