DKIM: Ed25519 signatures (GnuTLS 3.6.0 and later)
[users/jgh/exim.git] / src / src / dkim.c
index 0ac4dc32e48f40225fcf5907a77084fbd82d53f2..c7bf641527822a7fecc97fe383ca7c548a5ea2f1 100644 (file)
@@ -2,7 +2,7 @@
 *     Exim - an Internet mail transport agent    *
 *************************************************/
 
-/* Copyright (c) University of Cambridge, 1995 - 2007 */
+/* Copyright (c) University of Cambridge, 1995 - 2017 */
 /* See the file NOTICE for conditions of use and distribution. */
 
 /* Code for DKIM support. Other DKIM relevant code is in
 
 #ifndef DISABLE_DKIM
 
-#include "pdkim/pdkim.h"
+# include "pdkim/pdkim.h"
 
-pdkim_ctx       *dkim_verify_ctx = NULL;
+# ifdef MACRO_PREDEF
+#  include "macro_predef.h"
+
+void
+params_dkim(void)
+{
+builtin_macro_create_var(US"_DKIM_SIGN_HEADERS", US PDKIM_DEFAULT_SIGN_HEADERS);
+}
+# else /*!MACRO_PREDEF*/
+
+
+
+
+int dkim_verify_oldpool;
+pdkim_ctx *dkim_verify_ctx = NULL;
 pdkim_signature *dkim_signatures = NULL;
-pdkim_signature *dkim_cur_sig    = NULL;
+pdkim_signature *dkim_cur_sig = NULL;
+static const uschar * dkim_collect_error = NULL;
 
-int dkim_exim_query_dns_txt(char *name, char *answer) {
-  dns_answer dnsa;
-  dns_scan   dnss;
-  dns_record *rr;
 
-  lookup_dnssec_authenticated = NULL;
-  if (dns_lookup(&dnsa, (uschar *)name, T_TXT, NULL) != DNS_SUCCEED) return PDKIM_FAIL;
 
-  /* Search for TXT record */
-  for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS);
-       rr != NULL;
-       rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT))
-    if (rr->type == T_TXT) break;
+/*XXX the caller only uses the first record if we return multiple.
+*/
 
-  /* Copy record content to the answer buffer */
-  if (rr != NULL) {
+static uschar *
+dkim_exim_query_dns_txt(char * name)
+{
+dns_answer dnsa;
+dns_scan dnss;
+dns_record *rr;
+gstring * g = NULL;
+
+lookup_dnssec_authenticated = NULL;
+if (dns_lookup(&dnsa, US name, T_TXT, NULL) != DNS_SUCCEED)
+  return NULL; /*XXX better error detail?  logging? */
+
+/* Search for TXT record */
+
+for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS);
+     rr;
+     rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT))
+  if (rr->type == T_TXT)
+    {
     int rr_offset = 0;
-    int answer_offset = 0;
-    while (rr_offset < rr->size) {
-      uschar len = (rr->data)[rr_offset++];
-      snprintf(answer+(answer_offset),
-               PDKIM_DNS_TXT_MAX_RECLEN-(answer_offset),
-               "%.*s", (int)len, (char *)((rr->data)+rr_offset));
-      rr_offset+=len;
-      answer_offset+=len;
-      if (answer_offset >= PDKIM_DNS_TXT_MAX_RECLEN) {
-        return PDKIM_FAIL;
+
+    /* Copy record content to the answer buffer */
+
+    while (rr_offset < rr->size)
+      {
+      uschar len = rr->data[rr_offset++];
+
+      g = string_catn(g, US(rr->data + rr_offset), len);
+      if (g->ptr >= PDKIM_DNS_TXT_MAX_RECLEN)
+       goto bad;
+
+      rr_offset += len;
+      }
+
+    /* check if this looks like a DKIM record */
+    if (Ustrncmp(g->s, "v=", 2) != 0 || strncasecmp(CS g->s, "v=dkim", 6) == 0)
+      {
+      store_reset(g->s + g->ptr + 1);
+      return string_from_gstring(g);
       }
+
+    if (g) g->ptr = 0;         /* overwrite previous record */
     }
-  }
-  else return PDKIM_FAIL;
 
-  return PDKIM_OK;
+bad:
+if (g) store_reset(g);
+return NULL;   /*XXX better error detail?  logging? */
 }
 
 
-void dkim_exim_verify_init(void) {
+void
+dkim_exim_init(void)
+{
+pdkim_init();
+}
 
-  /* Free previous context if there is one */
-  if (dkim_verify_ctx) pdkim_free_ctx(dkim_verify_ctx);
 
-  /* Create new context */
-  dkim_verify_ctx = pdkim_init_verify(PDKIM_INPUT_SMTP,
-                                      &dkim_exim_query_dns_txt
-                                     );
 
-  if (dkim_verify_ctx != NULL) {
-    dkim_collect_input = TRUE;
-    pdkim_set_debug_stream(dkim_verify_ctx,debug_file);
-  }
-  else dkim_collect_input = FALSE;
+void
+dkim_exim_verify_init(BOOL dot_stuffing)
+{
+/* There is a store-reset between header & body reception
+so cannot use the main pool. Any allocs done by Exim
+memory-handling must use the perm pool. */
 
-}
+dkim_verify_oldpool = store_pool;
+store_pool = POOL_PERM;
 
+/* Free previous context if there is one */
 
-void dkim_exim_verify_feed(uschar *data, int len) {
-  if (dkim_collect_input &&
-      pdkim_feed(dkim_verify_ctx,
-                 (char *)data,
-                 len) != PDKIM_OK) dkim_collect_input = FALSE;
-}
+if (dkim_verify_ctx)
+  pdkim_free_ctx(dkim_verify_ctx);
 
+/* Create new context */
 
-void dkim_exim_verify_finish(void) {
-  pdkim_signature *sig = NULL;
-  int dkim_signers_size = 0;
-  int dkim_signers_ptr = 0;
-  dkim_signers = NULL;
+dkim_verify_ctx = pdkim_init_verify(&dkim_exim_query_dns_txt, dot_stuffing);
+dkim_collect_input = !!dkim_verify_ctx;
+dkim_collect_error = NULL;
 
-  /* Delete eventual previous signature chain */
-  dkim_signatures = NULL;
+/* Start feed up with any cached data */
+receive_get_cache();
 
-  /* If we have arrived here with dkim_collect_input == FALSE, it
-     means there was a processing error somewhere along the way.
-     Log the incident and disable futher verification. */
-  if (!dkim_collect_input) {
-    log_write(0, LOG_MAIN, "DKIM: Error while running this message through validation, disabling signature verification.");
-    dkim_disable_verify = TRUE;
-    return;
-  }
+store_pool = dkim_verify_oldpool;
+}
+
+
+void
+dkim_exim_verify_feed(uschar * data, int len)
+{
+int rc;
+
+store_pool = POOL_PERM;
+if (  dkim_collect_input
+   && (rc = pdkim_feed(dkim_verify_ctx, data, len)) != PDKIM_OK)
+  {
+  dkim_collect_error = pdkim_errstr(rc);
+  log_write(0, LOG_MAIN,
+            "DKIM: validation error: %.100s", dkim_collect_error);
   dkim_collect_input = FALSE;
+  }
+store_pool = dkim_verify_oldpool;
+}
 
-  /* Finish DKIM operation and fetch link to signatures chain */
-  if (pdkim_feed_finish(dkim_verify_ctx,&dkim_signatures) != PDKIM_OK) return;
-
-  sig = dkim_signatures;
-  while (sig != NULL) {
-    int size = 0;
-    int ptr = 0;
-    /* Log a line for each signature */
-    uschar *logmsg = string_append(NULL, &size, &ptr, 5,
-
-      string_sprintf( "d=%s s=%s c=%s/%s a=%s ",
-                      sig->domain,
-                      sig->selector,
-                      (sig->canon_headers == PDKIM_CANON_SIMPLE)?"simple":"relaxed",
-                      (sig->canon_body    == PDKIM_CANON_SIMPLE)?"simple":"relaxed",
-                      (sig->algo          == PDKIM_ALGO_RSA_SHA256)?"rsa-sha256":"rsa-sha1"
-                    ),
-      ((sig->identity != NULL)?
-        string_sprintf("i=%s ", sig->identity)
-        :
-        US""
-      ),
-      ((sig->created > 0)?
-        string_sprintf("t=%lu ", sig->created)
-        :
-        US""
-      ),
-      ((sig->expires > 0)?
-        string_sprintf("x=%lu ", sig->expires)
-        :
-        US""
-      ),
-      ((sig->bodylength > -1)?
-        string_sprintf("l=%lu ", sig->bodylength)
-        :
-        US""
-      )
-    );
-
-    switch(sig->verify_status) {
-      case PDKIM_VERIFY_NONE:
-        logmsg = string_append(logmsg, &size, &ptr, 1, "[not verified]");
+
+/* Log the result for the given signature */
+static void
+dkim_exim_verify_log_sig(pdkim_signature * sig)
+{
+gstring * logmsg;
+uschar * s;
+
+if (!sig) return;
+
+if (  !dkim_verify_overall
+   && dkim_verify_status
+      ? Ustrcmp(dkim_verify_status, US"pass") == 0
+      : sig->verify_status == PDKIM_VERIFY_PASS
+   )
+  dkim_verify_overall = string_copy(sig->domain);
+
+if (!LOGGING(dkim_verbose)) return;
+
+logmsg = string_catn(NULL, US"DKIM: ", 6);
+if (!(s = sig->domain)) s = US"<UNSET>";
+logmsg = string_append(logmsg, 2, "d=", s);
+if (!(s = sig->selector)) s = US"<UNSET>";
+logmsg = string_append(logmsg, 2, " s=", s);
+logmsg = string_append(logmsg, 7,
+" c=", sig->canon_headers == PDKIM_CANON_SIMPLE ? "simple" : "relaxed",
+"/",   sig->canon_body    == PDKIM_CANON_SIMPLE ? "simple" : "relaxed",
+" a=", dkim_sig_to_a_tag(sig),
+string_sprintf(" b=" SIZE_T_FMT,
+               (int)sig->sighash.len > -1 ? sig->sighash.len * 8 : 0));
+if ((s= sig->identity)) logmsg = string_append(logmsg, 2, " i=", s);
+if (sig->created > 0) logmsg = string_cat(logmsg,
+                             string_sprintf(" t=%lu", sig->created));
+if (sig->expires > 0) logmsg = string_cat(logmsg,
+                             string_sprintf(" x=%lu", sig->expires));
+if (sig->bodylength > -1) logmsg = string_cat(logmsg,
+                             string_sprintf(" l=%lu", sig->bodylength));
+
+if (  !dkim_verify_status
+   || (  dkim_verify_status == dkim_exim_expand_query(DKIM_VERIFY_STATUS)
+      && dkim_verify_reason == dkim_exim_expand_query(DKIM_VERIFY_REASON)
+   )  )
+  switch (sig->verify_status)
+    {
+    case PDKIM_VERIFY_NONE:
+      logmsg = string_cat(logmsg, US" [not verified]");
       break;
-      case PDKIM_VERIFY_INVALID:
-        logmsg = string_append(logmsg, &size, &ptr, 1, "[invalid - ");
-        switch (sig->verify_ext_status) {
-          case PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE:
-            logmsg = string_append(logmsg, &size, &ptr, 1, "public key record (currently?) unavailable]");
-          break;
-          case PDKIM_VERIFY_INVALID_BUFFER_SIZE:
-            logmsg = string_append(logmsg, &size, &ptr, 1, "overlong public key record]");
-          break;
-          case PDKIM_VERIFY_INVALID_PUBKEY_PARSING:
-            logmsg = string_append(logmsg, &size, &ptr, 1, "syntax error in public key record]");
-          break;
-          default:
-            logmsg = string_append(logmsg, &size, &ptr, 1, "unspecified problem]");
-        }
+
+    case PDKIM_VERIFY_INVALID:
+      logmsg = string_cat(logmsg, US" [invalid - ");
+      switch (sig->verify_ext_status)
+       {
+       case PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE:
+         logmsg = string_cat(logmsg,
+                       US"public key record (currently?) unavailable]");
+         break;
+
+       case PDKIM_VERIFY_INVALID_BUFFER_SIZE:
+         logmsg = string_cat(logmsg, US"overlong public key record]");
+         break;
+
+       case PDKIM_VERIFY_INVALID_PUBKEY_DNSRECORD:
+       case PDKIM_VERIFY_INVALID_PUBKEY_IMPORT:
+         logmsg = string_cat(logmsg, US"syntax error in public key record]");
+         break;
+
+       case PDKIM_VERIFY_INVALID_SIGNATURE_ERROR:
+         logmsg = string_cat(logmsg, US"signature tag missing or invalid]");
+         break;
+
+       case PDKIM_VERIFY_INVALID_DKIM_VERSION:
+         logmsg = string_cat(logmsg, US"unsupported DKIM version]");
+         break;
+
+       default:
+         logmsg = string_cat(logmsg, US"unspecified problem]");
+       }
       break;
-      case PDKIM_VERIFY_FAIL:
-        logmsg = string_append(logmsg, &size, &ptr, 1, "[verification failed - ");
-        switch (sig->verify_ext_status) {
-          case PDKIM_VERIFY_FAIL_BODY:
-            logmsg = string_append(logmsg, &size, &ptr, 1, "body hash mismatch (body probably modified in transit)]");
-          break;
-          case PDKIM_VERIFY_FAIL_MESSAGE:
-            logmsg = string_append(logmsg, &size, &ptr, 1, "signature did not verify (headers probably modified in transit)]");
-          break;
-          default:
-            logmsg = string_append(logmsg, &size, &ptr, 1, "unspecified reason]");
-        }
+
+    case PDKIM_VERIFY_FAIL:
+      logmsg = string_cat(logmsg, US" [verification failed - ");
+      switch (sig->verify_ext_status)
+       {
+       case PDKIM_VERIFY_FAIL_BODY:
+         logmsg = string_cat(logmsg,
+              US"body hash mismatch (body probably modified in transit)]");
+         break;
+
+       case PDKIM_VERIFY_FAIL_MESSAGE:
+         logmsg = string_cat(logmsg,
+               US"signature did not verify "
+               "(headers probably modified in transit)]");
+       break;
+
+       default:
+         logmsg = string_cat(logmsg, US"unspecified reason]");
+       }
       break;
-      case PDKIM_VERIFY_PASS:
-        logmsg = string_append(logmsg, &size, &ptr, 1, "[verification succeeded]");
+
+    case PDKIM_VERIFY_PASS:
+      logmsg = string_cat(logmsg, US" [verification succeeded]");
       break;
     }
+else
+  logmsg = string_append(logmsg, 5,
+           US" [", dkim_verify_status, US" - ", dkim_verify_reason, US"]");
+
+log_write(0, LOG_MAIN, "%s", string_from_gstring(logmsg));
+return;
+}
+
+
+/* Log a line for each signature */
+void
+dkim_exim_verify_log_all(void)
+{
+pdkim_signature * sig;
+for (sig = dkim_signatures; sig; sig = sig->next) dkim_exim_verify_log_sig(sig);
+}
 
-    logmsg[ptr] = '\0';
-    log_write(0, LOG_MAIN, "DKIM: %s", logmsg);
-
-    /* Build a colon-separated list of signing domains (and identities, if present) in dkim_signers */
-    dkim_signers = string_append(dkim_signers,
-                                 &dkim_signers_size,
-                                 &dkim_signers_ptr,
-                                 2,
-                                 sig->domain,
-                                 ":"
-                                );
-
-    if (sig->identity != NULL) {
-      dkim_signers = string_append(dkim_signers,
-                                   &dkim_signers_size,
-                                   &dkim_signers_ptr,
-                                   2,
-                                   sig->identity,
-                                   ":"
-                                  );
-    }
 
-    /* Process next signature */
-    sig = sig->next;
+void
+dkim_exim_verify_finish(void)
+{
+pdkim_signature * sig;
+int rc;
+gstring * g = NULL;
+const uschar * errstr = NULL;
+
+store_pool = POOL_PERM;
+
+/* Delete eventual previous signature chain */
+
+dkim_signers = NULL;
+dkim_signatures = NULL;
+
+if (dkim_collect_error)
+  {
+  log_write(0, LOG_MAIN,
+      "DKIM: Error during validation, disabling signature verification: %.100s",
+      dkim_collect_error);
+  dkim_disable_verify = TRUE;
+  goto out;
   }
 
-  /* NULL-terminate and chop the last colon from the domain list */
-  if (dkim_signers != NULL) {
-    dkim_signers[dkim_signers_ptr] = '\0';
-    if (Ustrlen(dkim_signers) > 0)
-      dkim_signers[Ustrlen(dkim_signers)-1] = '\0';
+dkim_collect_input = FALSE;
+
+/* Finish DKIM operation and fetch link to signatures chain */
+
+rc = pdkim_feed_finish(dkim_verify_ctx, &dkim_signatures, &errstr);
+if (rc != PDKIM_OK && errstr)
+  log_write(0, LOG_MAIN, "DKIM: validation error: %s", errstr);
+
+/* Build a colon-separated list of signing domains (and identities, if present) in dkim_signers */
+
+for (sig = dkim_signatures; sig; sig = sig->next)
+  {
+  if (sig->domain)   g = string_append_listele(g, ':', sig->domain);
+  if (sig->identity) g = string_append_listele(g, ':', sig->identity);
   }
+
+if (g) dkim_signers = g->s;
+
+out:
+store_pool = dkim_verify_oldpool;
 }
 
 
-void dkim_exim_acl_setup(uschar *id) {
-  pdkim_signature *sig = dkim_signatures;
-  dkim_cur_sig = NULL;
-  dkim_cur_signer = id;
-  if (dkim_disable_verify ||
-      !id || !dkim_verify_ctx) return;
-  /* Find signature to run ACL on */
-  while (sig != NULL) {
-    uschar *cmp_val = NULL;
-    if (Ustrchr(id,'@') != NULL) cmp_val = (uschar *)sig->identity;
-                            else cmp_val = (uschar *)sig->domain;
-    if (cmp_val && (strcmpic(cmp_val,id) == 0)) {
-      dkim_cur_sig = sig;
-      /* The "dkim_domain" and "dkim_selector" expansion variables have
-         related globals, since they are used in the signing code too.
-         Instead of inventing separate names for verification, we set
-         them here. This is easy since a domain and selector is guaranteed
-         to be in a signature. The other dkim_* expansion items are
-         dynamically fetched from dkim_cur_sig at expansion time (see
-         function below). */
-      dkim_signing_domain   = (uschar *)sig->domain;
-      dkim_signing_selector = (uschar *)sig->selector;
-      return;
+
+/* Args as per dkim_exim_acl_run() below */
+static int
+dkim_acl_call(uschar * id, gstring ** res_ptr,
+  uschar ** user_msgptr, uschar ** log_msgptr)
+{
+int rc;
+DEBUG(D_receive)
+  debug_printf("calling acl_smtp_dkim for dkim_cur_signer='%s'\n", id);
+
+rc = acl_check(ACL_WHERE_DKIM, NULL, acl_smtp_dkim, user_msgptr, log_msgptr);
+dkim_exim_verify_log_sig(dkim_cur_sig);
+*res_ptr = string_append_listele(*res_ptr, ':', dkim_verify_status);
+return rc;
+}
+
+
+
+/* For the given identity, run the DKIM ACL once for each matching signature.
+
+Arguments
+ id            Identity to look for in dkim signatures
+ res_ptr       ptr to growable string-list of status results,
+               appended to per ACL run
+ user_msgptr   where to put a user error (for SMTP response)
+ log_msgptr    where to put a logging message (not for SMTP response)
+
+Returns:       OK         access is granted by an ACCEPT verb
+               DISCARD    access is granted by a DISCARD verb
+               FAIL       access is denied
+               FAIL_DROP  access is denied; drop the connection
+               DEFER      can't tell at the moment
+               ERROR      disaster
+*/
+
+int
+dkim_exim_acl_run(uschar * id, gstring ** res_ptr,
+  uschar ** user_msgptr, uschar ** log_msgptr)
+{
+pdkim_signature * sig;
+uschar * cmp_val;
+int rc = -1;
+
+dkim_verify_status = US"none";
+dkim_verify_reason = US"";
+dkim_cur_signer = id;
+
+if (dkim_disable_verify || !id || !dkim_verify_ctx)
+  return OK;
+
+/* Find signatures to run ACL on */
+
+for (sig = dkim_signatures; sig; sig = sig->next)
+  if (  (cmp_val = Ustrchr(id, '@') != NULL ? US sig->identity : US sig->domain)
+     && strcmpic(cmp_val, id) == 0
+     )
+    {
+    /* The "dkim_domain" and "dkim_selector" expansion variables have
+    related globals, since they are used in the signing code too.
+    Instead of inventing separate names for verification, we set
+    them here. This is easy since a domain and selector is guaranteed
+    to be in a signature. The other dkim_* expansion items are
+    dynamically fetched from dkim_cur_sig at expansion time (see
+    function below). */
+
+    dkim_cur_sig = sig;
+    dkim_signing_domain = US sig->domain;
+    dkim_signing_selector = US sig->selector;
+    dkim_key_length = sig->sighash.len * 8;
+
+    /* These two return static strings, so we can compare the addr
+    later to see if the ACL overwrote them.  Check that when logging */
+
+    dkim_verify_status = dkim_exim_expand_query(DKIM_VERIFY_STATUS);
+    dkim_verify_reason = dkim_exim_expand_query(DKIM_VERIFY_REASON);
+
+    if ((rc = dkim_acl_call(id, res_ptr, user_msgptr, log_msgptr)) != OK)
+      return rc;
     }
-    sig = sig->next;
-  }
+
+if (rc != -1)
+  return rc;
+
+/* No matching sig found.  Call ACL once anyway. */
+
+dkim_cur_sig = NULL;
+return dkim_acl_call(id, res_ptr, user_msgptr, log_msgptr);
 }
 
 
-uschar *dkim_exim_expand_query(int what) {
+static uschar *
+dkim_exim_expand_defaults(int what)
+{
+switch (what)
+  {
+  case DKIM_ALGO:              return US"";
+  case DKIM_BODYLENGTH:                return US"9999999999999";
+  case DKIM_CANON_BODY:                return US"";
+  case DKIM_CANON_HEADERS:     return US"";
+  case DKIM_COPIEDHEADERS:     return US"";
+  case DKIM_CREATED:           return US"0";
+  case DKIM_EXPIRES:           return US"9999999999999";
+  case DKIM_HEADERNAMES:       return US"";
+  case DKIM_IDENTITY:          return US"";
+  case DKIM_KEY_GRANULARITY:   return US"*";
+  case DKIM_KEY_SRVTYPE:       return US"*";
+  case DKIM_KEY_NOTES:         return US"";
+  case DKIM_KEY_TESTING:       return US"0";
+  case DKIM_NOSUBDOMAINS:      return US"0";
+  case DKIM_VERIFY_STATUS:     return US"none";
+  case DKIM_VERIFY_REASON:     return US"";
+  default:                     return US"";
+  }
+}
 
-  if (!dkim_verify_ctx ||
-      dkim_disable_verify ||
-      !dkim_cur_sig) return dkim_exim_expand_defaults(what);
 
-  switch(what) {
-    case DKIM_ALGO:
-      switch(dkim_cur_sig->algo) {
-        case PDKIM_ALGO_RSA_SHA1:
-          return US"rsa-sha1";
-        case PDKIM_ALGO_RSA_SHA256:
-        default:
-          return US"rsa-sha256";
-      }
-    case DKIM_BODYLENGTH:
-      return (dkim_cur_sig->bodylength >= 0)?
-              (uschar *)string_sprintf(OFF_T_FMT,(LONGLONG_T)dkim_cur_sig->bodylength)
-              :dkim_exim_expand_defaults(what);
-    case DKIM_CANON_BODY:
-      switch(dkim_cur_sig->canon_body) {
-        case PDKIM_CANON_RELAXED:
-          return US"relaxed";
-        case PDKIM_CANON_SIMPLE:
-        default:
-          return US"simple";
+uschar *
+dkim_exim_expand_query(int what)
+{
+if (!dkim_verify_ctx || dkim_disable_verify || !dkim_cur_sig)
+  return dkim_exim_expand_defaults(what);
+
+switch (what)
+  {
+  case DKIM_ALGO:
+    return dkim_sig_to_a_tag(dkim_cur_sig);
+
+  case DKIM_BODYLENGTH:
+    return dkim_cur_sig->bodylength >= 0
+      ? string_sprintf("%ld", dkim_cur_sig->bodylength)
+      : dkim_exim_expand_defaults(what);
+
+  case DKIM_CANON_BODY:
+    switch (dkim_cur_sig->canon_body)
+      {
+      case PDKIM_CANON_RELAXED:        return US"relaxed";
+      case PDKIM_CANON_SIMPLE:
+      default:                 return US"simple";
       }
-    case DKIM_CANON_HEADERS:
-      switch(dkim_cur_sig->canon_headers) {
-        case PDKIM_CANON_RELAXED:
-          return US"relaxed";
-        case PDKIM_CANON_SIMPLE:
-        default:
-          return US"simple";
+
+  case DKIM_CANON_HEADERS:
+    switch (dkim_cur_sig->canon_headers)
+      {
+      case PDKIM_CANON_RELAXED:        return US"relaxed";
+      case PDKIM_CANON_SIMPLE:
+      default:                 return US"simple";
       }
-    case DKIM_COPIEDHEADERS:
-      return dkim_cur_sig->copiedheaders?
-              (uschar *)(dkim_cur_sig->copiedheaders)
-              :dkim_exim_expand_defaults(what);
-    case DKIM_CREATED:
-      return (dkim_cur_sig->created > 0)?
-              (uschar *)string_sprintf("%llu",dkim_cur_sig->created)
-              :dkim_exim_expand_defaults(what);
-    case DKIM_EXPIRES:
-      return (dkim_cur_sig->expires > 0)?
-              (uschar *)string_sprintf("%llu",dkim_cur_sig->expires)
-              :dkim_exim_expand_defaults(what);
-    case DKIM_HEADERNAMES:
-      return dkim_cur_sig->headernames?
-              (uschar *)(dkim_cur_sig->headernames)
-              :dkim_exim_expand_defaults(what);
-    case DKIM_IDENTITY:
-      return dkim_cur_sig->identity?
-              (uschar *)(dkim_cur_sig->identity)
-              :dkim_exim_expand_defaults(what);
-    case DKIM_KEY_GRANULARITY:
-      return dkim_cur_sig->pubkey?
-              (dkim_cur_sig->pubkey->granularity?
-                (uschar *)(dkim_cur_sig->pubkey->granularity)
-                :dkim_exim_expand_defaults(what)
-              )
-              :dkim_exim_expand_defaults(what);
-    case DKIM_KEY_SRVTYPE:
-      return dkim_cur_sig->pubkey?
-              (dkim_cur_sig->pubkey->srvtype?
-                (uschar *)(dkim_cur_sig->pubkey->srvtype)
-                :dkim_exim_expand_defaults(what)
-              )
-              :dkim_exim_expand_defaults(what);
-    case DKIM_KEY_NOTES:
-      return dkim_cur_sig->pubkey?
-              (dkim_cur_sig->pubkey->notes?
-                (uschar *)(dkim_cur_sig->pubkey->notes)
-                :dkim_exim_expand_defaults(what)
-              )
-              :dkim_exim_expand_defaults(what);
-    case DKIM_KEY_TESTING:
-      return dkim_cur_sig->pubkey?
-              (dkim_cur_sig->pubkey->testing?
-                US"1"
-                :dkim_exim_expand_defaults(what)
-              )
-              :dkim_exim_expand_defaults(what);
-    case DKIM_NOSUBDOMAINS:
-      return dkim_cur_sig->pubkey?
-              (dkim_cur_sig->pubkey->no_subdomaining?
-                US"1"
-                :dkim_exim_expand_defaults(what)
-              )
-              :dkim_exim_expand_defaults(what);
-    case DKIM_VERIFY_STATUS:
-      switch(dkim_cur_sig->verify_status) {
-        case PDKIM_VERIFY_INVALID:
-          return US"invalid";
-        case PDKIM_VERIFY_FAIL:
-          return US"fail";
-        case PDKIM_VERIFY_PASS:
-          return US"pass";
-        case PDKIM_VERIFY_NONE:
-        default:
-          return US"none";
+
+  case DKIM_COPIEDHEADERS:
+    return dkim_cur_sig->copiedheaders
+      ? US dkim_cur_sig->copiedheaders : dkim_exim_expand_defaults(what);
+
+  case DKIM_CREATED:
+    return dkim_cur_sig->created > 0
+      ? string_sprintf("%lu", dkim_cur_sig->created)
+      : dkim_exim_expand_defaults(what);
+
+  case DKIM_EXPIRES:
+    return dkim_cur_sig->expires > 0
+      ? string_sprintf("%lu", dkim_cur_sig->expires)
+      : dkim_exim_expand_defaults(what);
+
+  case DKIM_HEADERNAMES:
+    return dkim_cur_sig->headernames
+      ? dkim_cur_sig->headernames : dkim_exim_expand_defaults(what);
+
+  case DKIM_IDENTITY:
+    return dkim_cur_sig->identity
+      ? US dkim_cur_sig->identity : dkim_exim_expand_defaults(what);
+
+  case DKIM_KEY_GRANULARITY:
+    return dkim_cur_sig->pubkey
+      ? dkim_cur_sig->pubkey->granularity
+      ? US dkim_cur_sig->pubkey->granularity
+      : dkim_exim_expand_defaults(what)
+      : dkim_exim_expand_defaults(what);
+
+  case DKIM_KEY_SRVTYPE:
+    return dkim_cur_sig->pubkey
+      ? dkim_cur_sig->pubkey->srvtype
+      ? US dkim_cur_sig->pubkey->srvtype
+      : dkim_exim_expand_defaults(what)
+      : dkim_exim_expand_defaults(what);
+
+  case DKIM_KEY_NOTES:
+    return dkim_cur_sig->pubkey
+      ? dkim_cur_sig->pubkey->notes
+      ? US dkim_cur_sig->pubkey->notes
+      : dkim_exim_expand_defaults(what)
+      : dkim_exim_expand_defaults(what);
+
+  case DKIM_KEY_TESTING:
+    return dkim_cur_sig->pubkey
+      ? dkim_cur_sig->pubkey->testing
+      ? US"1"
+      : dkim_exim_expand_defaults(what)
+      : dkim_exim_expand_defaults(what);
+
+  case DKIM_NOSUBDOMAINS:
+    return dkim_cur_sig->pubkey
+      ? dkim_cur_sig->pubkey->no_subdomaining
+      ? US"1"
+      : dkim_exim_expand_defaults(what)
+      : dkim_exim_expand_defaults(what);
+
+  case DKIM_VERIFY_STATUS:
+    switch (dkim_cur_sig->verify_status)
+      {
+      case PDKIM_VERIFY_INVALID:       return US"invalid";
+      case PDKIM_VERIFY_FAIL:          return US"fail";
+      case PDKIM_VERIFY_PASS:          return US"pass";
+      case PDKIM_VERIFY_NONE:
+      default:                         return US"none";
       }
-    case DKIM_VERIFY_REASON:
-      switch (dkim_cur_sig->verify_ext_status) {
-        case PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE:
-          return US"pubkey_unavailable";
-        case PDKIM_VERIFY_INVALID_PUBKEY_PARSING:
-          return US"pubkey_syntax";
-        case PDKIM_VERIFY_FAIL_BODY:
-          return US"bodyhash_mismatch";
-        case PDKIM_VERIFY_FAIL_MESSAGE:
-          return US"signature_incorrect";
+
+  case DKIM_VERIFY_REASON:
+    switch (dkim_cur_sig->verify_ext_status)
+      {
+      case PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE:
+                                               return US"pubkey_unavailable";
+      case PDKIM_VERIFY_INVALID_PUBKEY_DNSRECORD:return US"pubkey_dns_syntax";
+      case PDKIM_VERIFY_INVALID_PUBKEY_IMPORT: return US"pubkey_der_syntax";
+      case PDKIM_VERIFY_FAIL_BODY:             return US"bodyhash_mismatch";
+      case PDKIM_VERIFY_FAIL_MESSAGE:          return US"signature_incorrect";
       }
-    default:
-      return US"";
+
+  default:
+    return US"";
   }
 }
 
 
-uschar *dkim_exim_expand_defaults(int what) {
-  switch(what) {
-    case DKIM_ALGO:               return US"";
-    case DKIM_BODYLENGTH:         return US"9999999999999";
-    case DKIM_CANON_BODY:         return US"";
-    case DKIM_CANON_HEADERS:      return US"";
-    case DKIM_COPIEDHEADERS:      return US"";
-    case DKIM_CREATED:            return US"0";
-    case DKIM_EXPIRES:            return US"9999999999999";
-    case DKIM_HEADERNAMES:        return US"";
-    case DKIM_IDENTITY:           return US"";
-    case DKIM_KEY_GRANULARITY:    return US"*";
-    case DKIM_KEY_SRVTYPE:        return US"*";
-    case DKIM_KEY_NOTES:          return US"";
-    case DKIM_KEY_TESTING:        return US"0";
-    case DKIM_NOSUBDOMAINS:       return US"0";
-    case DKIM_VERIFY_STATUS:      return US"none";
-    case DKIM_VERIFY_REASON:      return US"";
-    default:                      return US"";
-  }
-}
+/* Generate signatures for the given file.
+If a prefix is given, prepend it to the file for the calculations.
 
+Return:
+  NULL:                error; error string written
+  string:      signature header(s), or a zero-length string (not an error)
+*/
 
-uschar *
-dkim_exim_sign(int dkim_fd, uschar *dkim_private_key,
-       const uschar *dkim_domain, uschar *dkim_selector,
-       uschar *dkim_canon, uschar *dkim_sign_headers)
+gstring *
+dkim_exim_sign(int fd, off_t off, uschar * prefix,
+  struct ob_dkim * dkim, const uschar ** errstr)
 {
-  int sep = 0;
-  uschar *seen_items = NULL;
-  int seen_items_size = 0;
-  int seen_items_offset = 0;
-  uschar itembuf[256];
-  uschar *dkim_canon_expanded;
-  uschar *dkim_sign_headers_expanded;
-  uschar *dkim_private_key_expanded;
-  pdkim_ctx *ctx = NULL;
-  uschar *rc = NULL;
-  uschar *sigbuf = NULL;
-  int sigsize = 0;
-  int sigptr = 0;
-  pdkim_signature *signature;
-  int pdkim_canon;
-  int pdkim_rc;
-  int sread;
-  char buf[4096];
-  int save_errno = 0;
-  int old_pool = store_pool;
-
-  store_pool = POOL_MAIN;
-
-  dkim_domain = expand_cstring(dkim_domain);
-  if (dkim_domain == NULL) {
-    /* expansion error, do not send message. */
-    log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand "
-          "dkim_domain: %s", expand_string_message);
-    rc = NULL;
-    goto CLEANUP;
-  }
-
-  /* Set $dkim_domain expansion variable to each unique domain in list. */
-  while ((dkim_signing_domain = string_nextinlist(&dkim_domain, &sep,
-                                                  itembuf,
-                                                  sizeof(itembuf))) != NULL) {
-    if (!dkim_signing_domain || (dkim_signing_domain[0] == '\0')) continue;
-    /* Only sign once for each domain, no matter how often it
-       appears in the expanded list. */
-    if (seen_items != NULL) {
-      const uschar *seen_items_list = seen_items;
-      if (match_isinlist(dkim_signing_domain,
-                         &seen_items_list,0,NULL,NULL,MCL_STRING,TRUE,NULL) == OK)
-        continue;
-      seen_items = string_append(seen_items,&seen_items_size,&seen_items_offset,1,":");
-    }
-    seen_items = string_append(seen_items,&seen_items_size,&seen_items_offset,1,dkim_signing_domain);
-    seen_items[seen_items_offset] = '\0';
-
-    /* Set up $dkim_selector expansion variable. */
-    dkim_signing_selector = expand_string(dkim_selector);
-    if (dkim_signing_selector == NULL) {
-      log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand "
-                "dkim_selector: %s", expand_string_message);
-      rc = NULL;
-      goto CLEANUP;
-    }
+const uschar * dkim_domain;
+int sep = 0;
+gstring * seen_doms = NULL;
+pdkim_ctx ctx;
+pdkim_signature * sig;
+gstring * sigbuf;
+int pdkim_rc;
+int sread;
+uschar buf[4096];
+int save_errno = 0;
+int old_pool = store_pool;
+uschar * errwhen;
+
+store_pool = POOL_MAIN;
+
+pdkim_init_context(&ctx, dkim->dot_stuffed, &dkim_exim_query_dns_txt);
+
+if (!(dkim_domain = expand_cstring(dkim->dkim_domain)))
+  /* expansion error, do not send message. */
+  { errwhen = US"dkim_domain"; goto expand_bad; }
+
+/* Set $dkim_domain expansion variable to each unique domain in list. */
+
+while ((dkim_signing_domain = string_nextinlist(&dkim_domain, &sep, NULL, 0)))
+  {
+  const uschar * dkim_sel;
+  int sel_sep = 0;
+
+  if (dkim_signing_domain[0] == '\0')
+    continue;
+
+  /* Only sign once for each domain, no matter how often it
+  appears in the expanded list. */
+
+  if (match_isinlist(dkim_signing_domain, CUSS &seen_doms,
+      0, NULL, NULL, MCL_STRING, TRUE, NULL) == OK)
+    continue;
+
+  seen_doms = string_append_listele(seen_doms, ':', dkim_signing_domain);
+
+  /* Set $dkim_selector expansion variable to each selector in list,
+  for this domain. */
+
+  if (!(dkim_sel = expand_string(dkim->dkim_selector)))
+  if (!(dkim_signing_selector = expand_string(dkim->dkim_selector)))
+    { errwhen = US"dkim_selector"; goto expand_bad; }
+
+  while ((dkim_signing_selector = string_nextinlist(&dkim_sel, &sel_sep,
+         NULL, 0)))
+    {
+    uschar * dkim_canon_expanded;
+    int pdkim_canon;
+    uschar * dkim_sign_headers_expanded = NULL;
+    uschar * dkim_private_key_expanded;
+    uschar * dkim_hash_expanded;
+    uschar * dkim_identity_expanded = NULL;
 
     /* Get canonicalization to use */
-    dkim_canon_expanded = expand_string(dkim_canon?dkim_canon:US"relaxed");
-    if (dkim_canon_expanded == NULL) {
-      /* expansion error, do not send message. */
-      log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand "
-                "dkim_canon: %s", expand_string_message);
-      rc = NULL;
-      goto CLEANUP;
-    }
+
+    dkim_canon_expanded = dkim->dkim_canon
+      ? expand_string(dkim->dkim_canon) : US"relaxed";
+    if (!dkim_canon_expanded)  /* expansion error, do not send message. */
+      { errwhen = US"dkim_canon"; goto expand_bad; }
+
     if (Ustrcmp(dkim_canon_expanded, "relaxed") == 0)
       pdkim_canon = PDKIM_CANON_RELAXED;
     else if (Ustrcmp(dkim_canon_expanded, "simple") == 0)
       pdkim_canon = PDKIM_CANON_SIMPLE;
-    else {
-      log_write(0, LOG_MAIN, "DKIM: unknown canonicalization method '%s', defaulting to 'relaxed'.\n",dkim_canon_expanded);
+    else
+      {
+      log_write(0, LOG_MAIN,
+                "DKIM: unknown canonicalization method '%s', defaulting to 'relaxed'.\n",
+                dkim_canon_expanded);
       pdkim_canon = PDKIM_CANON_RELAXED;
-    }
-
-    if (dkim_sign_headers) {
-      dkim_sign_headers_expanded = expand_string(dkim_sign_headers);
-      if (dkim_sign_headers_expanded == NULL) {
-        log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand "
-                  "dkim_sign_headers: %s", expand_string_message);
-        rc = NULL;
-        goto CLEANUP;
       }
-    }
-    else {
-      /* pass NULL, which means default header list */
-      dkim_sign_headers_expanded = NULL;
-    }
+
+    if (  dkim->dkim_sign_headers
+       && !(dkim_sign_headers_expanded = expand_string(dkim->dkim_sign_headers)))
+      { errwhen = US"dkim_sign_header"; goto expand_bad; }
+    /* else pass NULL, which means default header list */
 
     /* Get private key to use. */
-    dkim_private_key_expanded = expand_string(dkim_private_key);
-    if (dkim_private_key_expanded == NULL) {
-      log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand "
-                "dkim_private_key: %s", expand_string_message);
-      rc = NULL;
-      goto CLEANUP;
-    }
-    if ( (Ustrlen(dkim_private_key_expanded) == 0) ||
-         (Ustrcmp(dkim_private_key_expanded,"0") == 0) ||
-         (Ustrcmp(dkim_private_key_expanded,"false") == 0) ) {
-      /* don't sign, but no error */
-      continue;
-    }
 
-    if (dkim_private_key_expanded[0] == '/') {
-      int privkey_fd = 0;
+    if (!(dkim_private_key_expanded = expand_string(dkim->dkim_private_key)))
+      { errwhen = US"dkim_private_key"; goto expand_bad; }
+
+    if (  Ustrlen(dkim_private_key_expanded) == 0
+       || Ustrcmp(dkim_private_key_expanded, "0") == 0
+       || Ustrcmp(dkim_private_key_expanded, "false") == 0
+       )
+      continue;                /* don't sign, but no error */
+
+    if (dkim_private_key_expanded[0] == '/')
+      {
+      int privkey_fd, off = 0, len;
+
       /* Looks like a filename, load the private key. */
-      memset(big_buffer,0,big_buffer_size);
-      privkey_fd = open(CS dkim_private_key_expanded,O_RDONLY);
-      if (privkey_fd < 0) {
-        log_write(0, LOG_MAIN|LOG_PANIC, "unable to open "
-                  "private key file for reading: %s", dkim_private_key_expanded);
-        rc = NULL;
-        goto CLEANUP;
-      }
-      if (read(privkey_fd,big_buffer,(big_buffer_size-2)) < 0) {
-        log_write(0, LOG_MAIN|LOG_PANIC, "unable to read private key file: %s",
-         dkim_private_key_expanded);
-        rc = NULL;
-        goto CLEANUP;
-      }
-      (void)close(privkey_fd);
+
+      memset(big_buffer, 0, big_buffer_size);
+
+      if ((privkey_fd = open(CS dkim_private_key_expanded, O_RDONLY)) < 0)
+       {
+       log_write(0, LOG_MAIN | LOG_PANIC, "unable to open "
+                  "private key file for reading: %s",
+                  dkim_private_key_expanded);
+       goto bad;
+       }
+
+      do
+       {
+       if ((len = read(privkey_fd, big_buffer + off, big_buffer_size - 2 - off)) < 0)
+         {
+         (void) close(privkey_fd);
+         log_write(0, LOG_MAIN|LOG_PANIC, "unable to read private key file: %s",
+                    dkim_private_key_expanded);
+         goto bad;
+         }
+       off += len;
+       }
+      while (len > 0);
+
+      (void) close(privkey_fd);
+      big_buffer[off] = '\0';
       dkim_private_key_expanded = big_buffer;
-    }
+      }
 
-    ctx = pdkim_init_sign(PDKIM_INPUT_SMTP,
-                          (char *)dkim_signing_domain,
-                          (char *)dkim_signing_selector,
-                          (char *)dkim_private_key_expanded
-                         );
-
-    pdkim_set_debug_stream(ctx,debug_file);
-
-    pdkim_set_optional(ctx,
-                       (char *)dkim_sign_headers_expanded,
-                       NULL,
-                       pdkim_canon,
-                       pdkim_canon,
-                       -1,
-                       PDKIM_ALGO_RSA_SHA256,
-                       0,
-                       0);
-
-    lseek(dkim_fd, 0, SEEK_SET);
-    while((sread = read(dkim_fd,&buf,4096)) > 0) {
-      if (pdkim_feed(ctx,buf,sread) != PDKIM_OK) {
-        rc = NULL;
-        goto CLEANUP;
+    if (!(dkim_hash_expanded = expand_string(dkim->dkim_hash)))
+      { errwhen = US"dkim_hash"; goto expand_bad; }
+
+    if (dkim->dkim_identity)
+      if (!(dkim_identity_expanded = expand_string(dkim->dkim_identity)))
+       { errwhen = US"dkim_identity"; goto expand_bad; }
+      else if (!*dkim_identity_expanded)
+       dkim_identity_expanded = NULL;
+
+  /*XXX so we currently nail signing to RSA + this hash.
+  Need to extract algo from privkey and check for disallowed combos. */
+
+    if (!(sig = pdkim_init_sign(&ctx, dkim_signing_domain,
+                         dkim_signing_selector,
+                         dkim_private_key_expanded,
+                         dkim_hash_expanded,
+                         errstr
+                         )))
+      goto bad;
+    dkim_private_key_expanded[0] = '\0';
+
+    pdkim_set_optional(sig,
+                       CS dkim_sign_headers_expanded,
+                       CS dkim_identity_expanded,
+                       pdkim_canon,
+                       pdkim_canon, -1, 0, 0);
+
+    if (!pdkim_set_bodyhash(&ctx, sig))
+      goto bad;
+
+    if (!ctx.sig)              /* link sig to context chain */
+      ctx.sig = sig;
+    else
+      {
+      pdkim_signature * n = ctx.sig;
+      while (n->next) n = n->next;
+      n->next = sig;
       }
     }
-    /* Handle failed read above. */
-    if (sread == -1) {
-      debug_printf("DKIM: Error reading -K file.\n");
-      save_errno = errno;
-      rc = NULL;
-      goto CLEANUP;
-    }
+  }
+if (!ctx.sig)
+  {
+  DEBUG(D_transport) debug_printf("DKIM: no viable signatures to use\n");
+  sigbuf = string_get(1);      /* return a zero-len string */
+  goto CLEANUP;
+  }
 
-    pdkim_rc = pdkim_feed_finish(ctx,&signature);
-    if (pdkim_rc != PDKIM_OK) {
-      log_write(0, LOG_MAIN|LOG_PANIC, "DKIM: signing failed (RC %d)", pdkim_rc);
-      rc = NULL;
-      goto CLEANUP;
-    }
+if (prefix && (pdkim_rc = pdkim_feed(&ctx, prefix, Ustrlen(prefix))) != PDKIM_OK)
+  goto pk_bad;
+
+if (lseek(fd, off, SEEK_SET) < 0)
+  sread = -1;
+else
+  while ((sread = read(fd, &buf, sizeof(buf))) > 0)
+    if ((pdkim_rc = pdkim_feed(&ctx, buf, sread)) != PDKIM_OK)
+      goto pk_bad;
+
+/* Handle failed read above. */
+if (sread == -1)
+  {
+  debug_printf("DKIM: Error reading -K file.\n");
+  save_errno = errno;
+  goto bad;
+  }
 
-    sigbuf = string_append(sigbuf, &sigsize, &sigptr, 2,
-                           US signature->signature_header,
-                           US"\r\n");
+/* Build string of headers, one per signature */
 
-    pdkim_free_ctx(ctx);
-    ctx = NULL;
-  }
+if ((pdkim_rc = pdkim_feed_finish(&ctx, &sig, errstr)) != PDKIM_OK)
+  goto pk_bad;
 
-  if (sigbuf != NULL) {
-    sigbuf[sigptr] = '\0';
-    rc = sigbuf;
-  } else
-    rc = US"";
+for (sigbuf = NULL; sig; sig = sig->next)
+  sigbuf = string_append(sigbuf, 2, US sig->signature_header, US"\r\n");
 
-  CLEANUP:
-  if (ctx != NULL)
-    pdkim_free_ctx(ctx);
+CLEANUP:
+  (void) string_from_gstring(sigbuf);
   store_pool = old_pool;
   errno = save_errno;
-  return rc;
+  return sigbuf;
+
+pk_bad:
+  log_write(0, LOG_MAIN|LOG_PANIC,
+               "DKIM: signing failed: %.100s", pdkim_errstr(pdkim_rc));
+bad:
+  sigbuf = NULL;
+  goto CLEANUP;
+
+expand_bad:
+  log_write(0, LOG_MAIN | LOG_PANIC, "failed to expand %s: %s",
+             errwhen, expand_string_message);
+  goto bad;
 }
 
-#endif
+# endif        /*!MACRO_PREDEF*/
+#endif /*!DISABLE_DKIM*/