2 * Copyright (c) 2022 Martijn van Duren <martijn@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 #include <sys/types.h>
17 #include <sys/socket.h>
19 #include <openssl/evp.h>
20 #include <openssl/pem.h>
21 #include <openssl/sha.h>
22 #include <openssl/err.h>
24 #include <arpa/nameser.h>
31 #include <opensmtpd.h>
40 #include "unpack_dns.h"
44 * Use RFC8601 (Authentication-Results) codes instead of RFC6376 codes,
45 * since they're more expressive.
58 struct header *header;
60 const char *state_reason;
70 /* Make sure padding bits for base64 decoding fit */
71 char bh[EVP_MAX_MD_SIZE + (3 - (EVP_MAX_MD_SIZE % 3))];
75 #define CANON_HEADER_SIMPLE 0
76 #define CANON_HEADER_RELAXED 1
77 #define CANON_HEADER 1
78 #define CANON_BODY_SIMPLE 0
79 #define CANON_BODY_RELAXED 1 << 1
80 #define CANON_BODY 1 << 1
81 #define CANON_DONE 1 << 2
82 char d[HOST_NAME_MAX + 1];
88 char s[HOST_NAME_MAX + 1];
89 time_t t; /* Signature t=/timestamp */
92 int kt; /* Key t=/Flags */
95 struct event_asr *query;
105 struct signature *sig;
108 #define AUTHENTICATION_RESULTS_LINELEN 78
109 #define MIN(a, b) ((a) < (b) ? (a) : (b))
112 struct osmtpd_ctx *ctx;
115 size_t body_whitelines;
117 struct header *header;
124 void dkim_err(struct message *, char *);
125 void dkim_errx(struct message *, char *);
126 void dkim_conf(const char *, const char *);
127 void dkim_dataline(struct osmtpd_ctx *, const char *);
128 void dkim_commit(struct osmtpd_ctx *);
129 void *dkim_message_new(struct osmtpd_ctx *);
130 void dkim_message_free(struct osmtpd_ctx *, void *);
131 void dkim_header_add(struct osmtpd_ctx *, const char *);
132 void dkim_signature_parse(struct header *);
133 void dkim_signature_parse_v(struct signature *, const char *, const char *);
134 void dkim_signature_parse_a(struct signature *, const char *, const char *);
135 void dkim_signature_parse_b(struct signature *, const char *, const char *);
136 void dkim_signature_parse_bh(struct signature *, const char *, const char *);
137 void dkim_signature_parse_c(struct signature *, const char *, const char *);
138 void dkim_signature_parse_d(struct signature *, const char *, const char *);
139 void dkim_signature_parse_h(struct signature *, const char *, const char *);
140 void dkim_signature_parse_i(struct signature *, const char *, const char *);
141 void dkim_signature_parse_l(struct signature *, const char *, const char *);
142 void dkim_signature_parse_q(struct signature *, const char *, const char *);
143 void dkim_signature_parse_s(struct signature *, const char *, const char *);
144 void dkim_signature_parse_t(struct signature *, const char *, const char *);
145 void dkim_signature_parse_x(struct signature *, const char *, const char *);
146 void dkim_signature_parse_z(struct signature *, const char *, const char *);
147 void dkim_signature_verify(struct signature *);
148 void dkim_signature_header(EVP_MD_CTX *, struct signature *, struct header *);
149 void dkim_signature_state(struct signature *, enum state, const char *);
150 const char *dkim_state2str(enum state);
151 void dkim_header_cat(struct osmtpd_ctx *, const char *);
152 void dkim_body_parse(struct message *, const char *);
153 void dkim_body_verify(struct signature *);
154 void dkim_rr_resolve(struct asr_result *, void *);
155 void dkim_message_verify(struct message *);
156 ssize_t dkim_ar_cat(char **ar, size_t *n, size_t aroff, const char *fmt, ...)
157 __attribute__((__format__ (printf, 4, 5)));
158 void dkim_ar_print(struct osmtpd_ctx *, const char *);
159 int dkim_key_text_parse(struct signature *, const char *);
162 EVP_ENCODE_CTX *ectx = NULL;
165 main(int argc, char *argv[])
168 osmtpd_errx(1, "Invalid argument count");
170 OpenSSL_add_all_digests();
172 if (pledge("tmppath stdio dns", NULL) == -1)
173 osmtpd_err(1, "pledge");
175 if ((ectx = EVP_ENCODE_CTX_new()) == NULL)
176 osmtpd_err(1, "EVP_ENCODE_CTX_new");
178 osmtpd_register_conf(dkim_conf);
179 osmtpd_register_filter_dataline(dkim_dataline);
180 osmtpd_register_filter_commit(dkim_commit);
181 osmtpd_local_message(dkim_message_new, dkim_message_free);
188 dkim_conf(const char *key, const char *value)
193 if (authservid == NULL)
194 osmtpd_errx(1, "Didn't receive admd config option");
197 if (strcmp(key, "admd") == 0 && authservid == NULL) {
198 if ((authservid = strdup(value)) == NULL)
199 osmtpd_err(1, "malloc");
200 end = osmtpd_ltok_skip_value(authservid, 0);
201 if (authservid + strlen(authservid) != end)
202 osmtpd_errx(1, "Invalid authservid");
207 dkim_dataline(struct osmtpd_ctx *ctx, const char *line)
209 struct message *msg = ctx->local_message;
213 if (line[0] == '.' && line[1] =='\0') {
215 osmtpd_filter_dataline(ctx, ".");
220 if (fprintf(msg->origf, "%s\n", line) < 0) {
221 dkim_err(msg, "Couldn't write to tempfile");
224 if (line[0] == '.') {
226 if (line[0] == '\0') {
228 for (i = 0; i < msg->nheaders; i++) {
229 if (msg->header[i].sig == NULL)
231 dkim_body_verify(msg->header[i].sig);
233 dkim_message_verify(msg);
237 if (msg->parsing_headers) {
238 dkim_header_add(ctx, line);
239 if (line[0] == '\0') {
240 msg->parsing_headers = 0;
241 for (i = 0; i < msg->nheaders; i++) {
242 if (msg->header[i].sig == NULL)
244 if (msg->header[i].sig->query == NULL)
245 dkim_signature_verify(
251 dkim_body_parse(msg, line);
256 dkim_commit(struct osmtpd_ctx *ctx)
258 struct message *msg = ctx->local_message;
261 osmtpd_filter_disconnect(ctx, "Internal server error");
263 osmtpd_filter_proceed(ctx);
267 dkim_message_new(struct osmtpd_ctx *ctx)
271 if ((msg = malloc(sizeof(*msg))) == NULL)
274 if ((msg->origf = tmpfile()) == NULL) {
275 dkim_err(msg, "Can't open tempfile");
279 msg->parsing_headers = 1;
280 msg->body_whitelines = 0;
291 dkim_message_free(struct osmtpd_ctx *ctx, void *data)
293 struct message *msg = data;
297 for (i = 0; i < msg->nheaders; i++) {
298 if (msg->header[i].sig != NULL) {
299 free(msg->header[i].sig->b);
300 EVP_MD_CTX_free(msg->header[i].sig->bhctx);
301 for (j = 0; msg->header[i].sig->h != NULL &&
302 msg->header[i].sig->h[j] != NULL; j++)
303 free(msg->header[i].sig->h[j]);
304 free(msg->header[i].sig->h);
305 EVP_PKEY_free(msg->header[i].sig->p);
307 free(msg->header[i].buf);
308 free(msg->header[i].sig);
315 dkim_header_add(struct osmtpd_ctx *ctx, const char *line)
317 struct message *msg = ctx->local_message;
318 const char *start, *end, *verify;
319 struct header *headers;
322 if (msg->nheaders > 0 &&
323 msg->header[msg->nheaders - 1].readdone == 0) {
324 if (line[0] != ' ' && line[0] != '\t') {
325 msg->header[msg->nheaders - 1].readdone = 1;
326 start = msg->header[msg->nheaders - 1].buf;
327 end = osmtpd_ltok_skip_field_name(start, 0);
328 /* In case someone uses an obs-optional */
330 verify = osmtpd_ltok_skip_wsp(end, 1);
333 start, "DKIM-Signature", end - start) == 0 &&
335 dkim_signature_parse(
336 &msg->header[msg->nheaders - 1]);
340 dkim_header_cat(ctx, line);
344 if (msg->nheaders % 10 == 0) {
345 if ((headers = recallocarray(msg->header, msg->nheaders,
346 msg->nheaders + 10, sizeof(*msg->header))) == NULL) {
347 dkim_err(msg, "malloc");
350 msg->header = headers;
351 for (i = 0; i < msg->nheaders; i++) {
352 if (msg->header[i].sig == NULL)
354 msg->header[i].sig->header = &msg->header[i];
357 msg->header[msg->nheaders].msg = msg;
359 dkim_header_cat(ctx, line);
363 dkim_header_cat(struct osmtpd_ctx *ctx, const char *line)
365 struct message *msg = ctx->local_message;
366 struct header *header = &msg->header[msg->nheaders - 1];
369 size_t needed = header->buflen + strlen(line) + 2;
371 if (needed > (header->buflen / 1024) + 1) {
372 buf = reallocarray(header->buf, (needed / 1024) + 1, 1024);
374 dkim_err(msg, "malloc");
379 header->buflen += snprintf(header->buf + header->buflen,
380 (((needed / 1024) + 1) * 1024) - header->buflen, "%s%s",
381 header->buflen == 0 ? "" : "\r\n", line);
385 dkim_signature_parse(struct header *header)
387 struct signature *sig;
388 struct asr_query *query;
389 const char *buf, *i, *end;
391 char subdomain[HOST_NAME_MAX + 1];
394 /* Format checked by dkim_header_add */
395 buf = osmtpd_ltok_skip_field_name(header->buf, 0);
396 buf = osmtpd_ltok_skip_wsp(buf, 1) + 1;
398 if ((header->sig = calloc(1, sizeof(*header->sig))) == NULL) {
399 dkim_err(header->msg, "malloc");
403 sig->header = header;
408 end = osmtpd_ltok_skip_tag_list(buf, 0);
409 if (end == NULL || end[0] != '\0') {
410 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid tag-list");
414 while (buf[0] != '\0') {
415 buf = osmtpd_ltok_skip_fws(buf, 1);
416 end = osmtpd_ltok_skip_tag_name(buf, 0);
418 /* Unknown tag-name */
419 if ((size_t)(end - buf) >= sizeof(tagname))
422 strlcpy(tagname, buf, (end - buf) + 1);
423 buf = osmtpd_ltok_skip_fws(end, 1);
425 buf = osmtpd_ltok_skip_fws(buf + 1, 1);
426 end = osmtpd_ltok_skip_tag_value(buf, 1);
427 if (strcmp(tagname, "v") == 0)
428 dkim_signature_parse_v(sig, buf, end);
429 else if (strcmp(tagname, "a") == 0)
430 dkim_signature_parse_a(sig, buf, end);
431 else if (strcmp(tagname, "b") == 0)
432 dkim_signature_parse_b(sig, buf, end);
433 else if (strcmp(tagname, "bh") == 0)
434 dkim_signature_parse_bh(sig, buf, end);
435 else if (strcmp(tagname, "c") == 0)
436 dkim_signature_parse_c(sig, buf, end);
437 else if (strcmp(tagname, "d") == 0)
438 dkim_signature_parse_d(sig, buf, end);
439 else if (strcmp(tagname, "h") == 0)
440 dkim_signature_parse_h(sig, buf, end);
441 else if (strcmp(tagname, "i") == 0)
442 dkim_signature_parse_i(sig, buf, end);
443 else if (strcmp(tagname, "l") == 0)
444 dkim_signature_parse_l(sig, buf, end);
445 else if (strcmp(tagname, "q") == 0)
446 dkim_signature_parse_q(sig, buf, end);
447 else if (strcmp(tagname, "s") == 0)
448 dkim_signature_parse_s(sig, buf, end);
449 else if (strcmp(tagname, "t") == 0)
450 dkim_signature_parse_t(sig, buf, end);
451 else if (strcmp(tagname, "x") == 0)
452 dkim_signature_parse_x(sig, buf, end);
453 else if (strcmp(tagname, "z") == 0)
454 dkim_signature_parse_z(sig, buf, end);
456 buf = osmtpd_ltok_skip_fws(end, 1);
459 else if (buf[0] != '\0') {
460 dkim_signature_state(sig, DKIM_PERMERROR,
465 if (sig->state != DKIM_UNKNOWN)
469 dkim_signature_state(sig, DKIM_PERMERROR, "Missing v tag");
470 else if (sig->ah == NULL)
471 dkim_signature_state(sig, DKIM_PERMERROR, "Missing a tag");
472 else if (sig->b == NULL)
473 dkim_signature_state(sig, DKIM_PERMERROR, "Missing b tag");
474 else if (sig->bhsz == 0)
475 dkim_signature_state(sig, DKIM_PERMERROR, "Missing bh tag");
476 else if (sig->d[0] == '\0')
477 dkim_signature_state(sig, DKIM_PERMERROR, "Missing d tag");
478 else if (sig->h == NULL)
479 dkim_signature_state(sig, DKIM_PERMERROR, "Missing h tag");
480 else if (sig->s[0] == '\0')
481 dkim_signature_state(sig, DKIM_PERMERROR, "Missing s tag");
482 if (sig->state != DKIM_UNKNOWN)
485 if (sig->i != NULL) {
486 i = osmtpd_ltok_skip_local_part(sig->i, 1) + 1;
487 ilen = sig->isz - (size_t)(i - sig->i);
488 dlen = strlen(sig->d);
490 dkim_signature_state(sig, DKIM_PERMERROR,
491 "i tag not subdomain of d");
495 if ((i[-1] != '.' && i[-1] != '@') ||
496 strncasecmp(i, sig->d, dlen) != 0) {
497 dkim_signature_state(sig, DKIM_PERMERROR,
498 "i tag not subdomain of d");
502 if (sig->t != -1 && sig->x != -1 && sig->t > sig->x) {
503 dkim_signature_state(sig, DKIM_PERMERROR, "t tag after x tag");
507 if ((size_t)snprintf(subdomain, sizeof(subdomain), "%s._domainkey.%s",
508 sig->s, sig->d) >= sizeof(subdomain)) {
509 dkim_signature_state(sig, DKIM_PERMERROR,
510 "dns/txt query too long");
514 if ((query = res_query_async(subdomain, C_IN, T_TXT, NULL)) == NULL) {
515 dkim_err(header->msg, "res_query_async");
518 if ((sig->query = event_asr_run(query, dkim_rr_resolve, sig)) == NULL) {
519 dkim_err(header->msg, "event_asr_run");
526 dkim_signature_parse_v(struct signature *sig, const char *start, const char *end)
528 if (sig->v != 0) { /* Duplicate tag */
529 dkim_signature_state(sig, DKIM_PERMERROR, "Duplicate v tag");
532 /* Unsupported version */
533 if (start[0] != '1' || start + 1 != end)
534 dkim_signature_state(sig, DKIM_NEUTRAL, "Unsupported v tag");
540 dkim_signature_parse_a(struct signature *sig, const char *start, const char *end)
542 char ah[sizeof("sha256")];
544 if (sig->ah != NULL) {
545 dkim_signature_state(sig, DKIM_PERMERROR, "Duplicate a tag");
549 if (osmtpd_ltok_skip_sig_a_tag_alg(start, 0) != end) {
550 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid a tag");
554 sig->asz = (size_t)(end - start);
555 if (strncmp(start, "rsa-", 4) == 0) {
557 sig->ak = EVP_PKEY_RSA;
560 } else if (strncmp(start, "ed25519-", 8) == 0) {
562 sig->ak = EVP_PKEY_ED25519;
566 dkim_signature_state(sig, DKIM_NEUTRAL, "Unsuppored a tag k");
569 if ((size_t)(end - start) >= sizeof(ah)) {
570 dkim_signature_state(sig, DKIM_NEUTRAL, "Unsuppored a tag h");
573 strlcpy(ah, start, sizeof(ah));
574 ah[end - start] = '\0';
575 if ((sig->ah = EVP_get_digestbyname(ah)) == NULL) {
576 dkim_signature_state(sig, DKIM_NEUTRAL, "Unsuppored a tag h");
579 if ((sig->bhctx = EVP_MD_CTX_new()) == NULL) {
580 dkim_err(sig->header->msg, "EVP_MD_CTX_new");
583 if (EVP_DigestInit_ex(sig->bhctx, sig->ah, NULL) <= 0) {
584 dkim_err(sig->header->msg, "EVP_DigestInit_ex");
590 dkim_signature_parse_b(struct signature *sig, const char *start, const char *end)
594 if (sig->b != NULL) {
595 dkim_signature_state(sig, DKIM_PERMERROR, "Duplicate b tag");
598 sig->bheader = start;
599 if ((sig->b = malloc((((end - start) / 4) + 1) * 3)) == NULL) {
600 dkim_err(sig->header->msg, "malloc");
603 /* EVP_DecodeBlock doesn't handle internal whitespace */
604 EVP_DecodeInit(ectx);
605 if (EVP_DecodeUpdate(ectx, sig->b, &decodesz, start,
606 (int)(end - start)) == -1) {
607 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid b tag");
611 if (EVP_DecodeFinal(ectx, sig->b + sig->bsz,
613 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid b tag");
616 sig->bsz += decodesz;
620 dkim_signature_parse_bh(struct signature *sig, const char *start, const char *end)
626 if (sig->bhsz != 0) {
627 dkim_signature_state(sig, DKIM_PERMERROR, "Duplicate bh tag");
631 * EVP_Decode* expects sig->bh to be large enough,
632 * so count the actual b64 characters.
637 b64 = osmtpd_ltok_skip_fws(b64, 1);
638 if (osmtpd_ltok_skip_alphadigitps(b64, 0) == NULL)
645 b64 = osmtpd_ltok_skip_fws(b64 + 1, 1);
651 /* Invalid tag value */
652 if (b64 != end || n % 4 != 0 || (n / 4) * 3 > sizeof(sig->bh)) {
653 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid bh tag");
656 /* EVP_DecodeBlock doesn't handle internal whitespace */
657 EVP_DecodeInit(ectx);
658 if (EVP_DecodeUpdate(ectx, sig->bh, &decodesz, start,
659 (int)(end - start)) == -1) {
661 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid bh tag");
664 sig->bhsz = decodesz;
665 if (EVP_DecodeFinal(ectx, sig->bh + sig->bhsz, &decodesz) == -1) {
667 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid bh tag");
670 sig->bhsz += decodesz;
674 dkim_signature_parse_c(struct signature *sig, const char *start, const char *end)
677 dkim_signature_state(sig, DKIM_PERMERROR, "Duplicate c tag");
680 if (strncmp(start, "simple", 6) == 0) {
681 sig->c = CANON_HEADER_SIMPLE;
683 } else if (strncmp(start, "relaxed", 7) == 0) {
684 sig->c = CANON_HEADER_RELAXED;
687 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid c tag");
690 if (start[0] == '/') {
692 if (strncmp(start, "simple", 6) == 0) {
693 sig->c |= CANON_BODY_SIMPLE;
695 } else if (strncmp(start, "relaxed", 7) == 0) {
696 sig->c |= CANON_BODY_RELAXED;
699 dkim_signature_state(sig, DKIM_PERMERROR,
706 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid c tag");
709 sig->c |= CANON_DONE;
713 dkim_signature_parse_d(struct signature *sig, const char *start, const char *end)
715 if (sig->d[0] != '\0') {
716 dkim_signature_state(sig, DKIM_PERMERROR, "Duplicate d tag");
719 if (osmtpd_ltok_skip_sig_d_tag_value(start, 0) != end ||
720 (size_t)(end - start) >= sizeof(sig->d)) {
721 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid d tag");
724 strlcpy(sig->d, start, end - start + 1);
728 dkim_signature_parse_h(struct signature *sig, const char *start, const char *end)
733 if (sig->h != NULL) {
734 dkim_signature_state(sig, DKIM_PERMERROR, "Duplicate h tag");
737 if (osmtpd_ltok_skip_sig_h_tag_value(start, 0) < end) {
738 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid h tag");
743 if ((h = osmtpd_ltok_skip_hdr_name(h, 0)) == NULL) {
744 dkim_signature_state(sig, DKIM_PERMERROR,
749 /* ';' is part of hdr-name */
754 h = osmtpd_ltok_skip_fws(h, 1);
757 h = osmtpd_ltok_skip_fws(h + 1, 1);
759 if ((sig->h = calloc(n + 1, sizeof(*sig->h))) == NULL) {
760 dkim_err(sig->header->msg, "malloc");
766 h = osmtpd_ltok_skip_hdr_name(start, 0);
767 /* ';' is part of hdr-name */
769 sig->h[n] = strndup(start, end - start);
772 if ((sig->h[n++] = strndup(start, h - start)) == NULL) {
773 dkim_err(sig->header->msg, "malloc");
776 start = osmtpd_ltok_skip_fws(h, 1);
779 start = osmtpd_ltok_skip_fws(start + 1, 1);
784 dkim_signature_parse_i(struct signature *sig, const char *start, const char *end)
786 if (sig->i != NULL) {
787 dkim_signature_state(sig, DKIM_PERMERROR, "Duplicate i tag");
790 if (osmtpd_ltok_skip_sig_i_tag_value(start, 0) != end) {
791 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid i tag");
795 sig->isz = (size_t)(end - start);
799 dkim_signature_parse_l(struct signature *sig, const char *start, const char *end)
804 if (sig->l != -1) { /* Duplicate tag */
805 dkim_signature_state(sig, DKIM_PERMERROR, "Duplicate l tag");
809 l = strtoll(start, &lend, 10);
810 /* > 76 digits in stroll is an overflow */
811 if (osmtpd_ltok_skip_digit(start, 0) == NULL ||
812 lend != end || errno != 0) {
813 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid l tag");
817 dkim_signature_state(sig, DKIM_PERMERROR, "l tag too large");
824 dkim_signature_parse_q(struct signature *sig, const char *start, const char *end)
829 dkim_signature_state(sig, DKIM_PERMERROR, "Duplicate q tag");
834 start = osmtpd_ltok_skip_fws(start, 1);
835 qend = osmtpd_ltok_skip_sig_q_tag_method(start, 0);
837 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid q tag");
840 if (strncmp(start, "dns/txt", qend - start) == 0)
842 start = osmtpd_ltok_skip_fws(qend, 1);
847 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid q tag");
852 dkim_signature_state(sig, DKIM_NEUTRAL, "No useable q found");
858 dkim_signature_parse_s(struct signature *sig, const char *start, const char *end)
860 if (sig->s[0] != '\0') {
861 dkim_signature_state(sig, DKIM_PERMERROR, "Duplicate s tag");
864 if (osmtpd_ltok_skip_selector(start, 0) != end) {
865 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid s tag");
868 strlcpy(sig->s, start, end - start + 1);
872 dkim_signature_parse_t(struct signature *sig, const char *start, const char *end)
877 dkim_signature_state(sig, DKIM_PERMERROR, "Duplicate t tag");
881 sig->t = strtoll(start, &tend, 10);
882 if (osmtpd_ltok_skip_digit(start, 0) == NULL || tend != end ||
883 tend - start > 12 || errno != 0) {
884 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid t tag");
890 dkim_signature_parse_x(struct signature *sig, const char *start, const char *end)
895 dkim_signature_state(sig, DKIM_PERMERROR, "Duplicate x tag");
899 sig->x = strtoll(start, &xend, 10);
900 if (osmtpd_ltok_skip_digit(start, 0) == NULL || xend != end ||
901 xend - start > 12 || errno != 0) {
902 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid x tag");
908 dkim_signature_parse_z(struct signature *sig, const char *start, const char *end)
911 dkim_signature_state(sig, DKIM_PERMERROR, "Duplicate z tag");
916 if (osmtpd_ltok_skip_sig_z_tag_value(start, 0) != end) {
917 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid z tag");
923 dkim_signature_verify(struct signature *sig)
925 struct message *msg = sig->header->msg;
926 static EVP_MD_CTX *bctx = NULL;
927 char digest[EVP_MAX_MD_SIZE];
928 unsigned int digestsz;
932 if (sig->state != DKIM_UNKNOWN)
936 if ((bctx = EVP_MD_CTX_new()) == NULL) {
937 dkim_errx(msg, "EVP_MD_CTX_new");
941 EVP_MD_CTX_reset(bctx);
943 if (EVP_DigestVerifyInit(bctx, NULL, sig->ah, NULL,
945 dkim_errx(msg, "EVP_DigestVerifyInit");
949 if (EVP_DigestInit_ex(bctx, sig->ah, NULL) != 1) {
950 dkim_errx(msg, "EVP_DigestInit_ex");
955 for (i = 0; i < msg->nheaders; i++)
956 msg->header[i].parsed = 0;
958 for (header = 0; sig->h[header] != NULL; header++) {
959 for (i = msg->nheaders; i > 0; ) {
961 if (msg->header[i].parsed ||
962 strncasecmp(msg->header[i].buf, sig->h[header],
963 strlen(sig->h[header])) != 0 ||
964 msg->header[i].sig == sig)
966 end = osmtpd_ltok_skip_fws(
967 msg->header[i].buf + strlen(sig->h[header]), 1);
970 dkim_signature_header(bctx, sig, &(msg->header[i]));
971 msg->header[i].parsed = 1;
974 dkim_signature_header(bctx, sig, sig->header);
976 if (EVP_DigestVerifyFinal(bctx, sig->b, sig->bsz) != 1)
977 dkim_signature_state(sig, DKIM_FAIL, "b mismatch");
979 if (EVP_DigestFinal_ex(bctx, digest, &digestsz) == 0) {
980 dkim_errx(msg, "EVP_DigestFinal_ex");
983 if (EVP_DigestVerifyInit(bctx, NULL, NULL, NULL, sig->p) != 1) {
984 dkim_errx(msg, "EVP_DigestVerifyInit");
987 switch (EVP_DigestVerify(bctx, sig->b, sig->bsz, digest,
992 dkim_signature_state(sig, DKIM_FAIL, "b mismatch");
995 dkim_errx(msg, "EVP_DigestVerify");
1001 /* EVP_DigestVerifyUpdate is a macro, so we can't alias this on a variable */
1002 #define dkim_b_digest_update(a, b, c) \
1003 (sig->sephash ? EVP_DigestUpdate((a), (b), (c)) :\
1004 EVP_DigestVerifyUpdate((a), (b), (c)))
1007 dkim_signature_header(EVP_MD_CTX *bctx, struct signature *sig,
1008 struct header *header)
1011 const char *ptr = header->buf, *end;
1013 int canon = sig->c & CANON_HEADER;
1015 for (ptr = header->buf; ptr[0] != '\0'; ptr++) {
1017 if (canon == CANON_HEADER_RELAXED) {
1018 ptr = osmtpd_ltok_skip_fws(ptr, 1);
1019 c = tolower(ptr[0]);
1024 if (canon == CANON_HEADER_RELAXED)
1025 ptr = osmtpd_ltok_skip_fws(
1028 if (dkim_b_digest_update(bctx, &c, 1) == 0) {
1029 dkim_errx(sig->header->msg,
1030 "dkim_b_digest_update");
1035 end = osmtpd_ltok_skip_fws(ptr, 1);
1037 if (sig->header == header && ptr == sig->bheader) {
1038 ptr = osmtpd_ltok_skip_tag_value(
1042 if (dkim_b_digest_update(bctx, ptr, 1) == 0) {
1043 dkim_errx(sig->header->msg,
1044 "dkim_b_digest_update");
1048 if (canon == CANON_HEADER_RELAXED) {
1051 if (dkim_b_digest_update(bctx, " ", 1) == 0) {
1052 dkim_errx(sig->header->msg,
1053 "dkim_b_digest_update");
1057 if (dkim_b_digest_update(bctx, ptr,
1059 dkim_errx(sig->header->msg,
1060 "dkim_b_digest_update");
1068 if (sig->header != header) {
1069 if (dkim_b_digest_update(bctx, "\r\n", 2) == 0) {
1070 dkim_errx(sig->header->msg, "dkim_b_digest_update");
1077 dkim_signature_state(struct signature *sig, enum state state,
1080 if (sig->query != NULL) {
1081 event_asr_abort(sig->query);
1084 switch (sig->state) {
1089 osmtpd_errx(1, "Unexpected transition");
1091 if (state == DKIM_PASS)
1095 if (state == DKIM_PASS)
1097 if (state == DKIM_TEMPERROR || state == DKIM_PERMERROR)
1099 osmtpd_errx(1, "Unexpected transition");
1100 case DKIM_TEMPERROR:
1101 if (state == DKIM_PERMERROR)
1104 case DKIM_PERMERROR:
1108 sig->state_reason = reason;
1112 dkim_state2str(enum state state)
1126 case DKIM_TEMPERROR:
1128 case DKIM_PERMERROR:
1134 dkim_rr_resolve(struct asr_result *ar, void *arg)
1136 struct signature *sig = arg;
1137 char key[UINT16_MAX + 1];
1139 size_t keylen, cstrlen;
1141 struct dns_header h;
1147 if (ar->ar_h_errno == TRY_AGAIN || ar->ar_h_errno == NO_RECOVERY) {
1148 dkim_signature_state(sig, DKIM_TEMPERROR,
1149 hstrerror(ar->ar_h_errno));
1152 if (ar->ar_h_errno != NETDB_SUCCESS) {
1153 dkim_signature_state(sig, DKIM_PERMERROR,
1154 hstrerror(ar->ar_h_errno));
1158 unpack_init(&pack, ar->ar_data, ar->ar_datalen);
1159 if (unpack_header(&pack, &h) != 0 ||
1160 unpack_query(&pack, &q) != 0) {
1161 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid dns/txt");
1164 for (; h.ancount > 0; h.ancount--) {
1165 unpack_rr(&pack, &rr);
1166 if (rr.rr_type != T_TXT)
1170 rr_txt = rr.rr.other.rdata;
1171 while (rr.rr.other.rdlen > 0) {
1172 cstrlen = ((const unsigned char *)rr_txt)[0];
1173 if (cstrlen >= rr.rr.other.rdlen ||
1174 keylen + cstrlen >= sizeof(key))
1177 * RFC 6376 Section 3.6.2.2
1178 * Strings in a TXT RR MUST be concatenated together
1179 * before use with no intervening whitespace.
1181 strlcpy(key + keylen, rr_txt + 1, cstrlen + 1);
1182 rr.rr.other.rdlen -= (cstrlen + 1);
1183 rr_txt += (cstrlen + 1);
1186 if (rr.rr.other.rdlen > 0) /* Invalid TXT RDATA */
1189 if (dkim_key_text_parse(sig, key))
1193 if (h.ancount == 0) {
1194 dkim_signature_state(sig, DKIM_PERMERROR,
1195 "No matching key found");
1197 /* Only verify if all headers have been read */
1198 if (!sig->header->msg->parsing_headers)
1199 dkim_signature_verify(sig);
1203 dkim_message_verify(sig->header->msg);
1207 dkim_key_text_parse(struct signature *sig, const char *key)
1209 char tagname, *hashname;
1210 const char *end, *tagvend;
1211 char pkraw[UINT16_MAX] = "", pkimp[UINT16_MAX];
1212 size_t pkrawlen = 0, pkoff, linelen;
1213 int h = 0, k = 0, n = 0, p = 0, s = 0, t = 0, first = 1;
1220 key = osmtpd_ltok_skip_fws(key, 1);
1221 /* Validate syntax early */
1222 if ((end = osmtpd_ltok_skip_tag_list(key, 0)) == NULL)
1225 while (key[0] != '\0') {
1226 key = osmtpd_ltok_skip_fws(key, 1);
1227 if ((end = osmtpd_ltok_skip_tag_name(key, 0)) == NULL)
1230 if ((size_t)(end - key) != 1)
1234 key = osmtpd_ltok_skip_fws(end, 1);
1238 key = osmtpd_ltok_skip_fws(key + 1, 1);
1239 if ((end = osmtpd_ltok_skip_tag_value(key, 0)) == NULL)
1244 * RFC 6376 section 3.6.1, v=:
1245 * RECOMMENDED...This tag MUST be the first tag in the
1249 osmtpd_ltok_skip_key_v_tag_value(key, 0) != end)
1254 if (h != 0) /* Duplicate tag */
1256 /* Invalid tag value */
1257 if (osmtpd_ltok_skip_key_h_tag_value(key, 0) != end)
1260 if ((tagvend = osmtpd_ltok_skip_key_h_tag_alg(
1263 hashname = strndup(key, tagvend - key);
1264 if (hashname == NULL) {
1265 dkim_err(sig->header->msg, "malloc");
1268 if (EVP_get_digestbyname(hashname) == sig->ah) {
1274 key = osmtpd_ltok_skip_fws(tagvend, 1);
1277 key = osmtpd_ltok_skip_fws(key + 1, 1);
1284 if (k != 0) /* Duplicate tag */
1287 if (strncmp(key, "rsa", end - key) == 0) {
1288 if (sig->ak != EVP_PKEY_RSA)
1291 } else if (strncmp(key, "ed25519", end - key) == 0) {
1292 if (sig->ak != EVP_PKEY_ED25519)
1300 if (n != 0) /* Duplicate tag */
1303 /* semicolon is part of safe-char */
1304 if (osmtpd_ltok_skip_key_n_tag_value(key, 0) < end)
1309 if (p != 0) /* Duplicate tag */
1313 key = osmtpd_ltok_skip_fws(key, 1);
1314 if (osmtpd_ltok_skip_alphadigitps(
1317 pkraw[pkrawlen++] = key++[0];
1318 if (pkrawlen >= sizeof(pkraw))
1321 if (key[0] == '=') {
1322 pkraw[pkrawlen++] = '=';
1323 key = osmtpd_ltok_skip_fws(key + 1, 1);
1324 if (pkrawlen >= sizeof(pkraw))
1326 if (key[0] == '=') {
1327 pkraw[pkrawlen++] = '=';
1329 if (pkrawlen >= sizeof(pkraw))
1333 /* Invalid tag value */
1334 if (pkrawlen % 4 != 0 || key != end)
1338 if (s != 0) /* Duplicate tag */
1340 /* Invalid tag value */
1341 if (osmtpd_ltok_skip_key_s_tag_value(key, 0) != end)
1345 osmtpd_ltok_skip_key_s_tag_type(
1348 if (strncmp(key, "*", tagvend - key) == 0 ||
1349 strncmp(key, "email", tagvend - key) == 0) {
1353 key = osmtpd_ltok_skip_fws(tagvend, 1);
1356 key = osmtpd_ltok_skip_fws(key + 1, 1);
1363 if (t != 0) /* Duplicate tag */
1366 if (osmtpd_ltok_skip_key_t_tag_value(key, 0) != end)
1369 tagvend = osmtpd_ltok_skip_key_t_tag_flag(
1371 if (strncmp(key, "y", tagvend - key) == 0)
1373 else if (strncmp(key, "s", tagvend - key) == 0)
1375 key = osmtpd_ltok_skip_fws(tagvend, 1);
1378 key = osmtpd_ltok_skip_fws(key + 1, 1);
1387 key = osmtpd_ltok_skip_fws(key, 1);
1390 else if (key[0] != '\0')
1394 if (!p) /* Missing tag */
1396 if (k == 0 && sig->ak != EVP_PKEY_RSA) /* Default to RSA */
1399 if (pkraw[0] == '\0') {
1400 dkim_signature_state(sig, DKIM_PERMERROR, "Key is revoked");
1406 pkoff = strlcpy(pkimp, "-----BEGIN PUBLIC KEY-----\n",
1409 for (key = pkraw; key[0] != '\0';) {
1410 if (pkoff + 2 >= sizeof(pkimp))
1412 pkimp[pkoff++] = key++[0];
1413 if (++linelen == 64) {
1414 pkimp[pkoff++] = '\n';
1418 /* Leverage pkoff check in loop */
1420 pkimp[pkoff++] = '\n';
1421 /* PEM_read_bio_PUBKEY will catch truncated keys */
1422 pkoff += strlcpy(pkimp + pkoff, "-----END PUBLIC KEY-----\n",
1423 sizeof(pkimp) - pkoff);
1424 if ((bio = BIO_new_mem_buf(pkimp, pkoff)) == NULL) {
1425 dkim_err(sig->header->msg, "BIO_new_mem_buf");
1428 sig->p = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL);
1432 case EVP_PKEY_ED25519:
1433 if ((pkrawlen / 4) * 3 >= sizeof(pkimp))
1435 EVP_DecodeInit(ectx);
1436 if (EVP_DecodeUpdate(ectx, pkimp, &tmp, pkraw, pkrawlen) == -1)
1439 if (EVP_DecodeFinal(ectx, pkimp, &tmp) == -1)
1442 sig->p = EVP_PKEY_new_raw_public_key(sig->ak, NULL, pkimp,
1447 if (sig->p == NULL) {
1449 * XXX No clue how to differentiate between invalid key and
1450 * temporary failure like *alloc.
1451 * Assume invalid key, because it's more likely.
1459 dkim_body_parse(struct message *msg, const char *line)
1461 struct signature *sig;
1462 const char *end = line, *hash, *prev;
1463 size_t hashn, len, i;
1466 if (line[0] == '\0') {
1467 msg->body_whitelines++;
1471 while (msg->body_whitelines-- > 0) {
1472 for (i = 0; i < msg->nheaders; i++) {
1473 if ((sig = msg->header[i].sig) == NULL ||
1474 sig->state != DKIM_UNKNOWN)
1476 hashn = sig->l == -1 ? 2 : MIN(2, sig->l);
1477 sig->l -= sig->l == -1 ? 0 : hashn;
1478 if (EVP_DigestUpdate(sig->bhctx, "\r\n", hashn) == 0) {
1479 dkim_errx(msg, "EVP_DigestUpdate");
1484 msg->body_whitelines = 0;
1487 while (line[0] != '\0') {
1490 if ((end = osmtpd_ltok_skip_wsp(end, 0)) == NULL)
1496 while (osmtpd_ltok_skip_wsp(end, 0) == NULL &&
1500 for (i = 0; i < msg->nheaders; i++) {
1501 sig = msg->header[i].sig;
1502 if (sig == NULL || sig->state != DKIM_UNKNOWN)
1505 (sig->c & CANON_BODY) == CANON_BODY_RELAXED) {
1507 len = end[0] == '\0' ? 0 : 1;
1510 len = (size_t)(end - line);
1512 hashn = sig->l == -1 ? len : MIN(len, (size_t)sig->l);
1513 sig->l -= sig->l == -1 ? 0 : hashn;
1514 ret = EVP_DigestUpdate(sig->bhctx, hash, hashn);
1516 dkim_errx(msg, "EVP_DigestUpdate");
1522 for (i = 0; i < msg->nheaders; i++) {
1523 sig = msg->header[i].sig;
1524 if (sig == NULL || sig->state != DKIM_UNKNOWN)
1526 hashn = sig->l == -1 ? 2 : MIN(2, sig->l);
1527 sig->l -= sig->l == -1 ? 0 : hashn;
1528 ret = EVP_DigestUpdate(sig->bhctx, "\r\n", hashn);
1530 dkim_errx(msg, "EVP_DigestUpdate");
1537 dkim_body_verify(struct signature *sig)
1539 unsigned char digest[EVP_MAX_MD_SIZE];
1540 unsigned int digestsz;
1542 if (sig->state != DKIM_UNKNOWN)
1545 if ((sig->c & CANON_BODY) == CANON_BODY_SIMPLE &&
1546 !sig->header->msg->has_body) {
1547 if (EVP_DigestUpdate(sig->bhctx, "\r\n",
1548 sig->l == -1 ? 2 : MIN(2, sig->l)) <= 0) {
1549 dkim_errx(sig->header->msg,
1550 "Can't update hash context");
1555 dkim_signature_state(sig, DKIM_PERMERROR,
1556 "l tag larger than body");
1560 if (EVP_DigestFinal_ex(sig->bhctx, digest, &digestsz) == 0) {
1561 dkim_errx(sig->header->msg, "EVP_DigestFinal_ex");
1565 if (digestsz != sig->bhsz || memcmp(digest, sig->bh, digestsz) != 0)
1566 dkim_signature_state(sig, DKIM_FAIL, "bh mismatch");
1570 dkim_message_verify(struct message *msg)
1572 struct signature *sig;
1574 ssize_t n, aroff = 0;
1582 for (i = 0; i < msg->nheaders; i++) {
1583 if (msg->header[i].sig == NULL)
1585 if (msg->header[i].sig->query != NULL)
1587 if (msg->header[i].sig->state != DKIM_UNKNOWN)
1589 dkim_signature_state(msg->header[i].sig, DKIM_PASS, NULL);
1592 if ((aroff = dkim_ar_cat(&line, &linelen, aroff,
1593 "Authentication-Results: %s", authservid)) == -1) {
1594 dkim_err(msg, "malloc");
1597 for (i = 0; i < msg->nheaders; i++) {
1598 sig = msg->header[i].sig;
1602 if ((aroff = dkim_ar_cat(&line, &linelen, aroff, "; dkim=%s",
1603 dkim_state2str(sig->state))) == -1) {
1604 dkim_err(msg, "malloc");
1607 if (sig->state_reason != NULL) {
1608 if ((aroff = dkim_ar_cat(&line, &linelen, aroff,
1609 " reason=\"%s\"", sig->state_reason)) == -1) {
1610 dkim_err(msg, "malloc");
1614 if (sig->s[0] != '\0') {
1615 if ((aroff = dkim_ar_cat(&line, &linelen, aroff,
1616 " header.s=%s", sig->s)) == -1) {
1617 dkim_err(msg, "malloc");
1621 if (sig->d[0] != '\0') {
1622 if ((aroff = dkim_ar_cat(&line, &linelen, aroff,
1623 " header.d=%s", sig->d)) == -1) {
1624 dkim_err(msg, "malloc");
1629 * Don't print i-tag, since localpart can be a quoted-string,
1630 * which can contain FWS and CFWS.
1632 if (sig->a != NULL) {
1633 if ((aroff = dkim_ar_cat(&line, &linelen, aroff,
1634 " header.a=%.*s", (int)sig->asz, sig->a)) == -1) {
1635 dkim_err(msg, "malloc");
1641 aroff = dkim_ar_cat(&line, &linelen, aroff, "; dkim=none");
1643 dkim_err(msg, "malloc");
1647 dkim_ar_print(msg->ctx, line);
1650 while ((n = getline(&line, &linelen, msg->origf)) != -1) {
1652 osmtpd_filter_dataline(msg->ctx, "%s", line);
1654 if (ferror(msg->origf))
1655 dkim_err(msg, "getline");
1662 dkim_ar_print(struct osmtpd_ctx *ctx, const char *start)
1664 const char *scan, *checkpoint, *ncheckpoint;
1666 int first = 1, arid = 1;
1669 ncheckpoint = osmtpd_ltok_skip_hdr_name(start, 0) + 1;
1670 for (scan = start; scan[0] != '\0'; scan++) {
1671 if (scan[0] == '\t')
1672 arlen = (arlen + 8) & ~7;
1675 if (arlen >= AUTHENTICATION_RESULTS_LINELEN) {
1676 osmtpd_filter_dataline(ctx, "%s%.*s", first ? "" : "\t",
1677 (int)((checkpoint == start ?
1678 ncheckpoint : checkpoint) - start), start);
1679 start = osmtpd_ltok_skip_cfws(checkpoint, 1);
1684 if (scan == ncheckpoint) {
1685 checkpoint = ncheckpoint;
1686 ncheckpoint = osmtpd_ltok_skip_cfws(ncheckpoint, 1);
1689 ncheckpoint = osmtpd_ltok_skip_value(
1693 } else if (strncmp(ncheckpoint, "dkim",
1694 sizeof("dkim") - 1) == 0) {
1695 ncheckpoint = osmtpd_ltok_skip_keyword(
1696 ncheckpoint + sizeof("dkim"), 0);
1698 } else if (strncmp(ncheckpoint, "reason",
1699 sizeof("reason") - 1) == 0) {
1700 ncheckpoint = osmtpd_ltok_skip_value(
1701 ncheckpoint + sizeof("reason"), 0);
1704 ncheckpoint += sizeof("header.x=") - 1;
1705 ncheckpoint = osmtpd_ltok_skip_ar_pvalue(
1707 if (ncheckpoint[0] == ';')
1712 osmtpd_filter_dataline(ctx, "%s%s", first ? "" : "\t", start);
1716 dkim_ar_cat(char **ar, size_t *n, size_t aroff, const char *fmt, ...)
1724 size = vsnprintf(*ar + aroff, *n - aroff, fmt, ap);
1726 if (size + aroff <= *n)
1727 return (ssize_t)size + aroff;
1728 nn = (((aroff + size) / 256) + 1) * 256;
1729 artmp = realloc(*ar, nn);
1735 size = vsnprintf(*ar + aroff, *n - aroff, fmt, ap);
1737 return (ssize_t)size + aroff;
1741 dkim_err(struct message *msg, char *text)
1744 fprintf(stderr, "%s: %s\n", text, strerror(errno));
1748 dkim_errx(struct message *msg, char *text)
1751 fprintf(stderr, "%s\n", text);
1757 fprintf(stderr, "usage: filter-dkimverify\n");