Class DkimSigningHelper

java.lang.Object
com.mimecast.robin.signing.DkimSigningHelper

public final class DkimSigningHelper extends Object
Applies DKIM signatures to outbound RelaySession envelope files.

Every code path that enqueues an outbound message to the persistent relay queue must invoke applyDkimSignaturesIfEnabled(RelaySession) before enqueueing, so that the signature is baked into the file that the queue processor later delivers. Retries of a queued file therefore transmit the already-signed bytes unchanged.

There are two such call sites today:

  • RelayMessage.deliver() — messages accepted over SMTP with an outbound Session.
  • BotHelper.queueBotResponse() — bot-generated responses that construct an outbound session in-process rather than receiving one over SMTP.
Extracting this into a helper keeps the two paths from diverging (the bot path previously bypassed signing entirely, so bot replies went out unsigned).
  • Field Details

    • log

      private static final org.apache.logging.log4j.Logger log
  • Constructor Details

    • DkimSigningHelper

      private DkimSigningHelper()
  • Method Details

    • applyDkimSignaturesIfEnabled

      public static void applyDkimSignaturesIfEnabled(RelaySession relaySession)
      Signs each envelope file in the session (in place, on disk) with every configured (domain, selector) pair returned by DkimSigningLookup for the envelope's sender domain. Idempotent per call: skips when DKIM signing is not enabled, when no signing key is configured, when the sender domain has no lookup rows, or when the envelope has no file.
      Parameters:
      relaySession - Session whose envelope files should be signed prior to enqueue.
    • readPrivateKey

      private static String readPrivateKey(String keyPath) throws IOException
      Reads a PKCS8 PEM private key file and returns the base64 content without PEM headers.
      Parameters:
      keyPath - Path to the PEM key file.
      Returns:
      Base64 key content (no PEM headers/footers, single line).
      Throws:
      IOException - If the file cannot be read.
    • prependDkimSignatures

      private static void prependDkimSignatures(File emailFile, byte[] original, List<String> signatures) throws IOException
      Prepends DKIM-Signature headers to the email file.
      Parameters:
      emailFile - Email file to modify in place.
      original - Original email content (already read from the file).
      signatures - List of DKIM-Signature header values (already RFC 5322 folded by Rspamd).
      Throws:
      IOException - If the file cannot be written.
    • extractSenderDomain

      private static String extractSenderDomain(String mail)
      Extracts the domain part from an email address.
      Parameters:
      mail - Email address (e.g., "user@example.com").
      Returns:
      Domain, or null if no @ is present.