X-Git-Url: https://git.exim.org/exim.git/blobdiff_plain/aaaa94ea8e4e7f53aec90ba28b3f7f26f28b317f..d9acfc1ce677f0bfd16f1555f3762af13b8e5a7b:/src/src/dkim.c diff --git a/src/src/dkim.c b/src/src/dkim.c index 6bc711ac1..96d7eba81 100644 --- a/src/src/dkim.c +++ b/src/src/dkim.c @@ -2,7 +2,7 @@ * Exim - an Internet mail transport agent * *************************************************/ -/* Copyright (c) University of Cambridge, 1995 - 2017 */ +/* Copyright (c) University of Cambridge, 1995 - 2018 */ /* See the file NOTICE for conditions of use and distribution. */ /* Code for DKIM support. Other DKIM relevant code is in @@ -26,57 +26,66 @@ builtin_macro_create_var(US"_DKIM_SIGN_HEADERS", US PDKIM_DEFAULT_SIGN_HEADERS); +pdkim_ctx dkim_sign_ctx; int dkim_verify_oldpool; pdkim_ctx *dkim_verify_ctx = NULL; -pdkim_signature *dkim_signatures = NULL; pdkim_signature *dkim_cur_sig = NULL; static const uschar * dkim_collect_error = NULL; +#define DKIM_MAX_SIGNATURES 20 + /*XXX the caller only uses the first record if we return multiple. -Could we hand back an allocated string? */ -static int -dkim_exim_query_dns_txt(char *name, char *answer) +uschar * +dkim_exim_query_dns_txt(uschar * 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 PDKIM_FAIL; /*XXX better error detail? logging? */ +if (dns_lookup(&dnsa, 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); +for (dns_record * 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; /* Copy record content to the answer buffer */ 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, CS (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; - answer_offset += len; - if (answer_offset >= PDKIM_DNS_TXT_MAX_RECLEN) - return PDKIM_FAIL; /*XXX better error detail? logging? */ } - return PDKIM_OK; + + /* check if this looks like a DKIM record */ + if (Ustrncmp(g->s, "v=", 2) != 0 || strncasecmp(CS g->s, "v=dkim", 6) == 0) + { + gstring_reset_unused(g); + return string_from_gstring(g); + } + + if (g) g->ptr = 0; /* overwrite previous record */ } -return PDKIM_FAIL; /*XXX better error detail? logging? */ +bad: +if (g) store_reset(g); +return NULL; /*XXX better error detail? logging? */ } @@ -106,7 +115,7 @@ if (dkim_verify_ctx) /* Create new context */ dkim_verify_ctx = pdkim_init_verify(&dkim_exim_query_dns_txt, dot_stuffing); -dkim_collect_input = !!dkim_verify_ctx; +dkim_collect_input = dkim_verify_ctx ? DKIM_MAX_SIGNATURES : 0; dkim_collect_error = NULL; /* Start feed up with any cached data */ @@ -128,7 +137,7 @@ if ( dkim_collect_input dkim_collect_error = pdkim_errstr(rc); log_write(0, LOG_MAIN, "DKIM: validation error: %.100s", dkim_collect_error); - dkim_collect_input = FALSE; + dkim_collect_input = 0; } store_pool = dkim_verify_oldpool; } @@ -143,29 +152,62 @@ uschar * s; if (!sig) return; +/* Remember the domain for the first pass result */ + +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); + +/* Rewrite the sig result if the ACL overrode it. This is only +needed because the DMARC code (sigh) peeks at the dkim sigs. +Mark the sig for this having been done. */ + +if ( dkim_verify_status + && ( dkim_verify_status != dkim_exim_expand_query(DKIM_VERIFY_STATUS) + || dkim_verify_reason != dkim_exim_expand_query(DKIM_VERIFY_REASON) + ) ) + { /* overridden by ACL */ + sig->verify_ext_status = -1; + if (Ustrcmp(dkim_verify_status, US"fail") == 0) + sig->verify_status = PDKIM_VERIFY_POLICY | PDKIM_VERIFY_FAIL; + else if (Ustrcmp(dkim_verify_status, US"invalid") == 0) + sig->verify_status = PDKIM_VERIFY_POLICY | PDKIM_VERIFY_INVALID; + else if (Ustrcmp(dkim_verify_status, US"none") == 0) + sig->verify_status = PDKIM_VERIFY_POLICY | PDKIM_VERIFY_NONE; + else if (Ustrcmp(dkim_verify_status, US"pass") == 0) + sig->verify_status = PDKIM_VERIFY_POLICY | PDKIM_VERIFY_PASS; + else + sig->verify_status = -1; + } + +if (!LOGGING(dkim_verbose)) return; + + logmsg = string_catn(NULL, US"DKIM: ", 6); if (!(s = sig->domain)) s = US""; logmsg = string_append(logmsg, 2, "d=", s); if (!(s = sig->selector)) s = US""; 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)); +logmsg = string_fmt_append(logmsg, " c=%s/%s a=%s b=" SIZE_T_FMT, + sig->canon_headers == PDKIM_CANON_SIMPLE ? "simple" : "relaxed", + sig->canon_body == PDKIM_CANON_SIMPLE ? "simple" : "relaxed", + dkim_sig_to_a_tag(sig), + (int)sig->sighash.len > -1 ? sig->sighash.len * 8 : (size_t)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) - ) ) +if (sig->created > 0) logmsg = string_fmt_append(logmsg, " t=%lu", + sig->created); +if (sig->expires > 0) logmsg = string_fmt_append(logmsg, " x=%lu", + sig->expires); +if (sig->bodylength > -1) logmsg = string_fmt_append(logmsg, " l=%lu", + sig->bodylength); + +if (sig->verify_status & PDKIM_VERIFY_POLICY) + logmsg = string_append(logmsg, 5, + US" [", dkim_verify_status, US" - ", dkim_verify_reason, US"]"); +else switch (sig->verify_status) { case PDKIM_VERIFY_NONE: @@ -216,7 +258,7 @@ if ( !dkim_verify_status logmsg = string_cat(logmsg, US"signature did not verify " "(headers probably modified in transit)]"); - break; + break; default: logmsg = string_cat(logmsg, US"unspecified reason]"); @@ -227,9 +269,6 @@ if ( !dkim_verify_status 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; @@ -240,18 +279,17 @@ return; void dkim_exim_verify_log_all(void) { -pdkim_signature * sig; -for (sig = dkim_signatures; sig; sig = sig->next) dkim_exim_verify_log_sig(sig); +for (pdkim_signature * sig = dkim_signatures; sig; sig = sig->next) + dkim_exim_verify_log_sig(sig); } void dkim_exim_verify_finish(void) { -pdkim_signature * sig; int rc; gstring * g = NULL; -const uschar * errstr; +const uschar * errstr = NULL; store_pool = POOL_PERM; @@ -265,25 +303,22 @@ if (dkim_collect_error) log_write(0, LOG_MAIN, "DKIM: Error during validation, disabling signature verification: %.100s", dkim_collect_error); - dkim_disable_verify = TRUE; + f.dkim_disable_verify = TRUE; goto out; } -dkim_collect_input = FALSE; +dkim_collect_input = 0; /* Finish DKIM operation and fetch link to signatures chain */ -rc = pdkim_feed_finish(dkim_verify_ctx, &dkim_signatures, &errstr); -if (rc != PDKIM_OK) - { - log_write(0, LOG_MAIN, "DKIM: validation error: %.100s%s%s", pdkim_errstr(rc), - errstr ? ": " : "", errstr ? errstr : US""); - goto out; - } +rc = pdkim_feed_finish(dkim_verify_ctx, (pdkim_signature **)&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) +for (pdkim_signature * 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); @@ -335,7 +370,6 @@ 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; @@ -343,12 +377,12 @@ dkim_verify_status = US"none"; dkim_verify_reason = US""; dkim_cur_signer = id; -if (dkim_disable_verify || !id || !dkim_verify_ctx) +if (f.dkim_disable_verify || !id || !dkim_verify_ctx) return OK; /* Find signatures to run ACL on */ -for (sig = dkim_signatures; sig; sig = sig->next) +for (pdkim_signature * sig = dkim_signatures; sig; sig = sig->next) if ( (cmp_val = Ustrchr(id, '@') != NULL ? US sig->identity : US sig->domain) && strcmpic(cmp_val, id) == 0 ) @@ -359,7 +393,7 @@ for (sig = dkim_signatures; sig; sig = sig->next) 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_exim_expand_query() below). */ dkim_cur_sig = sig; dkim_signing_domain = US sig->domain; @@ -371,7 +405,7 @@ for (sig = dkim_signatures; sig; sig = sig->next) 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; } @@ -415,7 +449,7 @@ switch (what) uschar * dkim_exim_expand_query(int what) { -if (!dkim_verify_ctx || dkim_disable_verify || !dkim_cur_sig) +if (!dkim_verify_ctx || f.dkim_disable_verify || !dkim_cur_sig) return dkim_exim_expand_defaults(what); switch (what) @@ -528,18 +562,31 @@ switch (what) } -/* Generate signatures for the given file, returning a string. +void +dkim_exim_sign_init(void) +{ +int old_pool = store_pool; +store_pool = POOL_MAIN; +pdkim_init_context(&dkim_sign_ctx, FALSE, &dkim_exim_query_dns_txt); +store_pool = old_pool; +} + + +/* 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) */ gstring * dkim_exim_sign(int fd, off_t off, uschar * prefix, struct ob_dkim * dkim, const uschar ** errstr) { -const uschar * dkim_domain; +const uschar * dkim_domain = NULL; int sep = 0; gstring * seen_doms = NULL; -pdkim_ctx ctx; pdkim_signature * sig; gstring * sigbuf; int pdkim_rc; @@ -548,18 +595,21 @@ uschar buf[4096]; int save_errno = 0; int old_pool = store_pool; uschar * errwhen; +const uschar * s; -store_pool = POOL_MAIN; +if (dkim->dot_stuffed) + dkim_sign_ctx.flags |= PDKIM_DOT_TERM; -pdkim_init_context(&ctx, dkim->dot_stuffed, &dkim_exim_query_dns_txt); +store_pool = POOL_MAIN; -if (!(dkim_domain = expand_cstring(dkim->dkim_domain))) +if ((s = dkim->dkim_domain) && !(dkim_domain = expand_cstring(s))) /* 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))) +if (dkim_domain) + while ((dkim_signing_domain = string_nextinlist(&dkim_domain, &sep, NULL, 0))) { const uschar * dkim_sel; int sel_sep = 0; @@ -570,6 +620,7 @@ while ((dkim_signing_domain = string_nextinlist(&dkim_domain, &sep, NULL, 0))) /* Only sign once for each domain, no matter how often it appears in the expanded list. */ + dkim_signing_domain = string_copylc(dkim_signing_domain); if (match_isinlist(dkim_signing_domain, CUSS &seen_doms, 0, NULL, NULL, MCL_STRING, TRUE, NULL) == OK) continue; @@ -580,7 +631,6 @@ while ((dkim_signing_domain = string_nextinlist(&dkim_domain, &sep, NULL, 0))) 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, @@ -592,6 +642,8 @@ while ((dkim_signing_domain = string_nextinlist(&dkim_domain, &sep, NULL, 0))) uschar * dkim_private_key_expanded; uschar * dkim_hash_expanded; uschar * dkim_identity_expanded = NULL; + uschar * dkim_timestamps_expanded = NULL; + unsigned long tval = 0, xval = 0; /* Get canonicalization to use */ @@ -628,39 +680,10 @@ while ((dkim_signing_domain = string_nextinlist(&dkim_domain, &sep, NULL, 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); - - 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; - } + if ( dkim_private_key_expanded[0] == '/' + && !(dkim_private_key_expanded = + expand_file_big_buffer(dkim_private_key_expanded))) + goto bad; if (!(dkim_hash_expanded = expand_string(dkim->dkim_hash))) { errwhen = US"dkim_hash"; goto expand_bad; } @@ -671,10 +694,14 @@ while ((dkim_signing_domain = string_nextinlist(&dkim_domain, &sep, NULL, 0))) 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 (dkim->dkim_timestamps) + if (!(dkim_timestamps_expanded = expand_string(dkim->dkim_timestamps))) + { errwhen = US"dkim_timestamps"; goto expand_bad; } + else + xval = (tval = (unsigned long) time(NULL)) + + strtoul(CCS dkim_timestamps_expanded, NULL, 10); - if (!(sig = pdkim_init_sign(&ctx, dkim_signing_domain, + if (!(sig = pdkim_init_sign(&dkim_sign_ctx, dkim_signing_domain, dkim_signing_selector, dkim_private_key_expanded, dkim_hash_expanded, @@ -685,50 +712,68 @@ while ((dkim_signing_domain = string_nextinlist(&dkim_domain, &sep, NULL, 0))) pdkim_set_optional(sig, CS dkim_sign_headers_expanded, - dkim_identity_expanded, + CS dkim_identity_expanded, pdkim_canon, - pdkim_canon, -1, 0, 0); + pdkim_canon, -1, tval, xval); - if (!ctx.sig) /* link sig to context chain */ - ctx.sig = sig; + if (!pdkim_set_sig_bodyhash(&dkim_sign_ctx, sig)) + goto bad; + + if (!dkim_sign_ctx.sig) /* link sig to context chain */ + dkim_sign_ctx.sig = sig; else { - pdkim_signature * n = ctx.sig; + pdkim_signature * n = dkim_sign_ctx.sig; while (n->next) n = n->next; n->next = sig; } } } -if (prefix) - pdkim_feed(&ctx, prefix, Ustrlen(prefix)); - -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; +/* We may need to carry on with the data-feed even if there are no DKIM sigs to +produce, if some other package (eg. ARC) is signing. */ -/* Handle failed read above. */ -if (sread == -1) +if (!dkim_sign_ctx.sig && !dkim->force_bodyhash) { - debug_printf("DKIM: Error reading -K file.\n"); - save_errno = errno; - goto bad; + DEBUG(D_transport) debug_printf("DKIM: no viable signatures to use\n"); + sigbuf = string_get(1); /* return a zero-len string */ } +else + { + if (prefix && (pdkim_rc = pdkim_feed(&dkim_sign_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(&dkim_sign_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; + } -/* Build string of headers, one per signature */ - -if ((pdkim_rc = pdkim_feed_finish(&ctx, &sig, errstr)) != PDKIM_OK) - goto pk_bad; + /* Build string of headers, one per signature */ -for (sigbuf = NULL; sig; sig = sig->next) - sigbuf = string_append(sigbuf, 2, US sig->signature_header, US"\r\n"); + if ((pdkim_rc = pdkim_feed_finish(&dkim_sign_ctx, &sig, errstr)) != PDKIM_OK) + goto pk_bad; -(void) string_from_gstring(sigbuf); + if (!sig) + { + DEBUG(D_transport) debug_printf("DKIM: no signatures to use\n"); + sigbuf = string_get(1); /* return a zero-len string */ + } + else for (sigbuf = NULL; sig; sig = sig->next) + sigbuf = string_append(sigbuf, 2, US sig->signature_header, US"\r\n"); + } CLEANUP: + (void) string_from_gstring(sigbuf); store_pool = old_pool; errno = save_errno; return sigbuf; @@ -746,5 +791,80 @@ expand_bad: goto bad; } + + + +gstring * +authres_dkim(gstring * g) +{ +int start = 0; /* compiler quietening */ + +DEBUG(D_acl) start = g->ptr; + +for (pdkim_signature * sig = dkim_signatures; sig; sig = sig->next) + { + g = string_catn(g, US";\n\tdkim=", 8); + + if (sig->verify_status & PDKIM_VERIFY_POLICY) + g = string_append(g, 5, + US"policy (", dkim_verify_status, US" - ", dkim_verify_reason, US")"); + else switch(sig->verify_status) + { + case PDKIM_VERIFY_NONE: g = string_cat(g, US"none"); break; + case PDKIM_VERIFY_INVALID: + switch (sig->verify_ext_status) + { + case PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE: + g = string_cat(g, US"tmperror (pubkey unavailable)\n\t\t"); break; + case PDKIM_VERIFY_INVALID_BUFFER_SIZE: + g = string_cat(g, US"permerror (overlong public key record)\n\t\t"); break; + case PDKIM_VERIFY_INVALID_PUBKEY_DNSRECORD: + case PDKIM_VERIFY_INVALID_PUBKEY_IMPORT: + g = string_cat(g, US"neutral (public key record import problem)\n\t\t"); + break; + case PDKIM_VERIFY_INVALID_SIGNATURE_ERROR: + g = string_cat(g, US"neutral (signature tag missing or invalid)\n\t\t"); + break; + case PDKIM_VERIFY_INVALID_DKIM_VERSION: + g = string_cat(g, US"neutral (unsupported DKIM version)\n\t\t"); + break; + default: + g = string_cat(g, US"permerror (unspecified problem)\n\t\t"); break; + } + break; + case PDKIM_VERIFY_FAIL: + switch (sig->verify_ext_status) + { + case PDKIM_VERIFY_FAIL_BODY: + g = string_cat(g, + US"fail (body hash mismatch; body probably modified in transit)\n\t\t"); + break; + case PDKIM_VERIFY_FAIL_MESSAGE: + g = string_cat(g, + US"fail (signature did not verify; headers probably modified in transit)\n\t\t"); + break; + default: + g = string_cat(g, US"fail (unspecified reason)\n\t\t"); + break; + } + break; + case PDKIM_VERIFY_PASS: g = string_cat(g, US"pass"); break; + default: g = string_cat(g, US"permerror"); break; + } + if (sig->domain) g = string_append(g, 2, US" header.d=", sig->domain); + if (sig->identity) g = string_append(g, 2, US" header.i=", sig->identity); + if (sig->selector) g = string_append(g, 2, US" header.s=", sig->selector); + g = string_append(g, 2, US" header.a=", dkim_sig_to_a_tag(sig)); + } + +DEBUG(D_acl) + if (g->ptr == start) + debug_printf("DKIM: no authres\n"); + else + debug_printf("DKIM: authres '%.*s'\n", g->ptr - start - 3, g->s + start + 3); +return g; +} + + # endif /*!MACRO_PREDEF*/ #endif /*!DISABLE_DKIM*/