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>
41 #include "unpack_dns.h"
45 * Use RFC8601 (Authentication-Results) codes instead of RFC6376 codes,
46 * since they're more expressive.
59 struct header *header;
61 const char *state_reason;
71 /* Make sure padding bits for base64 decoding fit */
72 char bh[EVP_MAX_MD_SIZE + (3 - (EVP_MAX_MD_SIZE % 3))];
76 #define CANON_HEADER_SIMPLE 0
77 #define CANON_HEADER_RELAXED 1
78 #define CANON_HEADER 1
79 #define CANON_BODY_SIMPLE 0
80 #define CANON_BODY_RELAXED 1 << 1
81 #define CANON_BODY 1 << 1
82 #define CANON_DONE 1 << 2
83 char d[HOST_NAME_MAX + 1];
89 char s[HOST_NAME_MAX + 1];
90 time_t t; /* Signature t=/timestamp */
93 int kt; /* Key t=/Flags */
96 struct event_asr *query;
106 struct signature *sig;
109 #define AUTHENTICATION_RESULTS_LINELEN 78
110 #define MIN(a, b) ((a) < (b) ? (a) : (b))
113 struct osmtpd_ctx *ctx;
116 size_t body_whitelines;
118 struct header *header;
125 void dkim_err(struct message *, char *);
126 void dkim_errx(struct message *, char *);
127 void dkim_conf(const char *, const char *);
128 void dkim_dataline(struct osmtpd_ctx *, const char *);
129 void dkim_commit(struct osmtpd_ctx *);
130 void *dkim_message_new(struct osmtpd_ctx *);
131 void dkim_message_free(struct osmtpd_ctx *, void *);
132 void dkim_header_add(struct osmtpd_ctx *, const char *);
133 void dkim_signature_parse(struct header *);
134 void dkim_signature_parse_v(struct signature *, const char *, const char *);
135 void dkim_signature_parse_a(struct signature *, const char *, const char *);
136 void dkim_signature_parse_b(struct signature *, const char *, const char *);
137 void dkim_signature_parse_bh(struct signature *, const char *, const char *);
138 void dkim_signature_parse_c(struct signature *, const char *, const char *);
139 void dkim_signature_parse_d(struct signature *, const char *, const char *);
140 void dkim_signature_parse_h(struct signature *, const char *, const char *);
141 void dkim_signature_parse_i(struct signature *, const char *, const char *);
142 void dkim_signature_parse_l(struct signature *, const char *, const char *);
143 void dkim_signature_parse_q(struct signature *, const char *, const char *);
144 void dkim_signature_parse_s(struct signature *, const char *, const char *);
145 void dkim_signature_parse_t(struct signature *, const char *, const char *);
146 void dkim_signature_parse_x(struct signature *, const char *, const char *);
147 void dkim_signature_parse_z(struct signature *, const char *, const char *);
148 void dkim_signature_verify(struct signature *);
149 void dkim_signature_header(EVP_MD_CTX *, struct signature *, struct header *);
150 void dkim_signature_state(struct signature *, enum state, const char *);
151 const char *dkim_state2str(enum state);
152 void dkim_header_cat(struct osmtpd_ctx *, const char *);
153 void dkim_body_parse(struct message *, const char *);
154 void dkim_body_verify(struct signature *);
155 void dkim_rr_resolve(struct asr_result *, void *);
156 void dkim_message_verify(struct message *);
157 ssize_t dkim_ar_cat(char **ar, size_t *n, size_t aroff, const char *fmt, ...)
158 __attribute__((__format__ (printf, 4, 5)));
159 void dkim_ar_print(struct osmtpd_ctx *, const char *);
160 int dkim_key_text_parse(struct signature *, const char *);
163 EVP_ENCODE_CTX *ectx = NULL;
166 main(int argc, char *argv[])
169 osmtpd_errx(1, "Invalid argument count");
171 OpenSSL_add_all_digests();
173 if (unveil(_PATH_TMP, "rwc") == -1)
174 osmtpd_err(1, "unveil");
175 if (pledge("stdio rpath wpath cpath dns", NULL) == -1)
176 osmtpd_err(1, "pledge");
178 if ((ectx = EVP_ENCODE_CTX_new()) == NULL)
179 osmtpd_err(1, "EVP_ENCODE_CTX_new");
181 osmtpd_register_conf(dkim_conf);
182 osmtpd_register_filter_dataline(dkim_dataline);
183 osmtpd_register_filter_commit(dkim_commit);
184 osmtpd_local_message(dkim_message_new, dkim_message_free);
191 dkim_conf(const char *key, const char *value)
196 if (authservid == NULL)
197 osmtpd_errx(1, "Didn't receive admd config option");
200 if (strcmp(key, "admd") == 0 && authservid == NULL) {
201 if ((authservid = strdup(value)) == NULL)
202 osmtpd_err(1, "malloc");
203 end = osmtpd_ltok_skip_value(authservid, 0);
204 if (authservid + strlen(authservid) != end)
205 osmtpd_errx(1, "Invalid authservid");
210 dkim_dataline(struct osmtpd_ctx *ctx, const char *line)
212 struct message *msg = ctx->local_message;
216 if (line[0] == '.' && line[1] =='\0') {
218 osmtpd_filter_dataline(ctx, ".");
223 if (fprintf(msg->origf, "%s\n", line) < 0) {
224 dkim_err(msg, "Couldn't write to tempfile");
227 if (line[0] == '.') {
229 if (line[0] == '\0') {
231 for (i = 0; i < msg->nheaders; i++) {
232 if (msg->header[i].sig == NULL)
234 dkim_body_verify(msg->header[i].sig);
236 dkim_message_verify(msg);
240 if (msg->parsing_headers) {
241 dkim_header_add(ctx, line);
242 if (line[0] == '\0') {
243 msg->parsing_headers = 0;
244 for (i = 0; i < msg->nheaders; i++) {
245 if (msg->header[i].sig == NULL)
247 if (msg->header[i].sig->query == NULL)
248 dkim_signature_verify(
254 dkim_body_parse(msg, line);
259 dkim_commit(struct osmtpd_ctx *ctx)
261 struct message *msg = ctx->local_message;
264 osmtpd_filter_disconnect(ctx, "Internal server error");
266 osmtpd_filter_proceed(ctx);
270 dkim_message_new(struct osmtpd_ctx *ctx)
274 if ((msg = malloc(sizeof(*msg))) == NULL)
277 if ((msg->origf = tmpfile()) == NULL) {
278 dkim_err(msg, "Can't open tempfile");
282 msg->parsing_headers = 1;
283 msg->body_whitelines = 0;
294 dkim_message_free(struct osmtpd_ctx *ctx, void *data)
296 struct message *msg = data;
300 for (i = 0; i < msg->nheaders; i++) {
301 if (msg->header[i].sig != NULL) {
302 free(msg->header[i].sig->b);
303 EVP_MD_CTX_free(msg->header[i].sig->bhctx);
304 for (j = 0; msg->header[i].sig->h != NULL &&
305 msg->header[i].sig->h[j] != NULL; j++)
306 free(msg->header[i].sig->h[j]);
307 free(msg->header[i].sig->h);
308 EVP_PKEY_free(msg->header[i].sig->p);
310 free(msg->header[i].buf);
311 free(msg->header[i].sig);
318 dkim_header_add(struct osmtpd_ctx *ctx, const char *line)
320 struct message *msg = ctx->local_message;
321 const char *start, *end, *verify;
322 struct header *headers;
325 if (msg->nheaders > 0 &&
326 msg->header[msg->nheaders - 1].readdone == 0) {
327 if (line[0] != ' ' && line[0] != '\t') {
328 msg->header[msg->nheaders - 1].readdone = 1;
329 start = msg->header[msg->nheaders - 1].buf;
330 end = osmtpd_ltok_skip_field_name(start, 0);
331 /* In case someone uses an obs-optional */
333 verify = osmtpd_ltok_skip_wsp(end, 1);
336 start, "DKIM-Signature", end - start) == 0 &&
338 dkim_signature_parse(
339 &msg->header[msg->nheaders - 1]);
343 dkim_header_cat(ctx, line);
347 if (msg->nheaders % 10 == 0) {
348 if ((headers = recallocarray(msg->header, msg->nheaders,
349 msg->nheaders + 10, sizeof(*msg->header))) == NULL) {
350 dkim_err(msg, "malloc");
353 msg->header = headers;
354 for (i = 0; i < msg->nheaders; i++) {
355 if (msg->header[i].sig == NULL)
357 msg->header[i].sig->header = &msg->header[i];
360 msg->header[msg->nheaders].msg = msg;
362 dkim_header_cat(ctx, line);
366 dkim_header_cat(struct osmtpd_ctx *ctx, const char *line)
368 struct message *msg = ctx->local_message;
369 struct header *header = &msg->header[msg->nheaders - 1];
372 size_t needed = header->buflen + strlen(line) + 2;
374 if (needed > (header->buflen / 1024) + 1) {
375 buf = reallocarray(header->buf, (needed / 1024) + 1, 1024);
377 dkim_err(msg, "malloc");
382 header->buflen += snprintf(header->buf + header->buflen,
383 (((needed / 1024) + 1) * 1024) - header->buflen, "%s%s",
384 header->buflen == 0 ? "" : "\r\n", line);
388 dkim_signature_parse(struct header *header)
390 struct signature *sig;
391 struct asr_query *query;
392 const char *buf, *i, *end;
394 char subdomain[HOST_NAME_MAX + 1];
397 /* Format checked by dkim_header_add */
398 buf = osmtpd_ltok_skip_field_name(header->buf, 0);
399 buf = osmtpd_ltok_skip_wsp(buf, 1) + 1;
401 if ((header->sig = calloc(1, sizeof(*header->sig))) == NULL) {
402 dkim_err(header->msg, "malloc");
406 sig->header = header;
411 end = osmtpd_ltok_skip_tag_list(buf, 0);
412 if (end == NULL || end[0] != '\0') {
413 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid tag-list");
417 while (buf[0] != '\0') {
418 buf = osmtpd_ltok_skip_fws(buf, 1);
419 end = osmtpd_ltok_skip_tag_name(buf, 0);
421 /* Unknown tag-name */
422 if ((size_t)(end - buf) >= sizeof(tagname))
425 strlcpy(tagname, buf, (end - buf) + 1);
426 buf = osmtpd_ltok_skip_fws(end, 1);
428 buf = osmtpd_ltok_skip_fws(buf + 1, 1);
429 end = osmtpd_ltok_skip_tag_value(buf, 1);
430 if (strcmp(tagname, "v") == 0)
431 dkim_signature_parse_v(sig, buf, end);
432 else if (strcmp(tagname, "a") == 0)
433 dkim_signature_parse_a(sig, buf, end);
434 else if (strcmp(tagname, "b") == 0)
435 dkim_signature_parse_b(sig, buf, end);
436 else if (strcmp(tagname, "bh") == 0)
437 dkim_signature_parse_bh(sig, buf, end);
438 else if (strcmp(tagname, "c") == 0)
439 dkim_signature_parse_c(sig, buf, end);
440 else if (strcmp(tagname, "d") == 0)
441 dkim_signature_parse_d(sig, buf, end);
442 else if (strcmp(tagname, "h") == 0)
443 dkim_signature_parse_h(sig, buf, end);
444 else if (strcmp(tagname, "i") == 0)
445 dkim_signature_parse_i(sig, buf, end);
446 else if (strcmp(tagname, "l") == 0)
447 dkim_signature_parse_l(sig, buf, end);
448 else if (strcmp(tagname, "q") == 0)
449 dkim_signature_parse_q(sig, buf, end);
450 else if (strcmp(tagname, "s") == 0)
451 dkim_signature_parse_s(sig, buf, end);
452 else if (strcmp(tagname, "t") == 0)
453 dkim_signature_parse_t(sig, buf, end);
454 else if (strcmp(tagname, "x") == 0)
455 dkim_signature_parse_x(sig, buf, end);
456 else if (strcmp(tagname, "z") == 0)
457 dkim_signature_parse_z(sig, buf, end);
459 buf = osmtpd_ltok_skip_fws(end, 1);
462 else if (buf[0] != '\0') {
463 dkim_signature_state(sig, DKIM_PERMERROR,
468 if (sig->state != DKIM_UNKNOWN)
472 dkim_signature_state(sig, DKIM_PERMERROR, "Missing v tag");
473 else if (sig->ah == NULL)
474 dkim_signature_state(sig, DKIM_PERMERROR, "Missing a tag");
475 else if (sig->b == NULL)
476 dkim_signature_state(sig, DKIM_PERMERROR, "Missing b tag");
477 else if (sig->bhsz == 0)
478 dkim_signature_state(sig, DKIM_PERMERROR, "Missing bh tag");
479 else if (sig->d[0] == '\0')
480 dkim_signature_state(sig, DKIM_PERMERROR, "Missing d tag");
481 else if (sig->h == NULL)
482 dkim_signature_state(sig, DKIM_PERMERROR, "Missing h tag");
483 else if (sig->s[0] == '\0')
484 dkim_signature_state(sig, DKIM_PERMERROR, "Missing s tag");
485 if (sig->state != DKIM_UNKNOWN)
488 if (sig->i != NULL) {
489 i = osmtpd_ltok_skip_local_part(sig->i, 1) + 1;
490 ilen = sig->isz - (size_t)(i - sig->i);
491 dlen = strlen(sig->d);
493 dkim_signature_state(sig, DKIM_PERMERROR,
494 "i tag not subdomain of d");
498 if ((i[-1] != '.' && i[-1] != '@') ||
499 strncasecmp(i, sig->d, dlen) != 0) {
500 dkim_signature_state(sig, DKIM_PERMERROR,
501 "i tag not subdomain of d");
505 if (sig->t != -1 && sig->x != -1 && sig->t > sig->x) {
506 dkim_signature_state(sig, DKIM_PERMERROR, "t tag after x tag");
510 if ((size_t)snprintf(subdomain, sizeof(subdomain), "%s._domainkey.%s",
511 sig->s, sig->d) >= sizeof(subdomain)) {
512 dkim_signature_state(sig, DKIM_PERMERROR,
513 "dns/txt query too long");
517 if ((query = res_query_async(subdomain, C_IN, T_TXT, NULL)) == NULL) {
518 dkim_err(header->msg, "res_query_async");
521 if ((sig->query = event_asr_run(query, dkim_rr_resolve, sig)) == NULL) {
522 dkim_err(header->msg, "event_asr_run");
529 dkim_signature_parse_v(struct signature *sig, const char *start, const char *end)
531 if (sig->v != 0) { /* Duplicate tag */
532 dkim_signature_state(sig, DKIM_PERMERROR, "Duplicate v tag");
535 /* Unsupported version */
536 if (start[0] != '1' || start + 1 != end)
537 dkim_signature_state(sig, DKIM_NEUTRAL, "Unsupported v tag");
543 dkim_signature_parse_a(struct signature *sig, const char *start, const char *end)
545 char ah[sizeof("sha256")];
547 if (sig->ah != NULL) {
548 dkim_signature_state(sig, DKIM_PERMERROR, "Duplicate a tag");
552 if (osmtpd_ltok_skip_sig_a_tag_alg(start, 0) != end) {
553 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid a tag");
557 sig->asz = (size_t)(end - start);
558 if (strncmp(start, "rsa-", 4) == 0) {
560 sig->ak = EVP_PKEY_RSA;
563 } else if (strncmp(start, "ed25519-", 8) == 0) {
565 sig->ak = EVP_PKEY_ED25519;
569 dkim_signature_state(sig, DKIM_NEUTRAL, "Unsuppored a tag k");
572 if ((size_t)(end - start) >= sizeof(ah)) {
573 dkim_signature_state(sig, DKIM_NEUTRAL, "Unsuppored a tag h");
576 strlcpy(ah, start, sizeof(ah));
577 ah[end - start] = '\0';
578 if ((sig->ah = EVP_get_digestbyname(ah)) == NULL) {
579 dkim_signature_state(sig, DKIM_NEUTRAL, "Unsuppored a tag h");
582 if ((sig->bhctx = EVP_MD_CTX_new()) == NULL) {
583 dkim_err(sig->header->msg, "EVP_MD_CTX_new");
586 if (EVP_DigestInit_ex(sig->bhctx, sig->ah, NULL) <= 0) {
587 dkim_err(sig->header->msg, "EVP_DigestInit_ex");
593 dkim_signature_parse_b(struct signature *sig, const char *start, const char *end)
597 if (sig->b != NULL) {
598 dkim_signature_state(sig, DKIM_PERMERROR, "Duplicate b tag");
601 sig->bheader = start;
602 if ((sig->b = malloc((((end - start) / 4) + 1) * 3)) == NULL) {
603 dkim_err(sig->header->msg, "malloc");
606 /* EVP_DecodeBlock doesn't handle internal whitespace */
607 EVP_DecodeInit(ectx);
608 if (EVP_DecodeUpdate(ectx, sig->b, &decodesz, start,
609 (int)(end - start)) == -1) {
610 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid b tag");
614 if (EVP_DecodeFinal(ectx, sig->b + sig->bsz,
616 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid b tag");
619 sig->bsz += decodesz;
623 dkim_signature_parse_bh(struct signature *sig, const char *start, const char *end)
629 if (sig->bhsz != 0) {
630 dkim_signature_state(sig, DKIM_PERMERROR, "Duplicate bh tag");
634 * EVP_Decode* expects sig->bh to be large enough,
635 * so count the actual b64 characters.
640 b64 = osmtpd_ltok_skip_fws(b64, 1);
641 if (osmtpd_ltok_skip_alphadigitps(b64, 0) == NULL)
648 b64 = osmtpd_ltok_skip_fws(b64 + 1, 1);
654 /* Invalid tag value */
655 if (b64 != end || n % 4 != 0 || (n / 4) * 3 > sizeof(sig->bh)) {
656 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid bh tag");
659 /* EVP_DecodeBlock doesn't handle internal whitespace */
660 EVP_DecodeInit(ectx);
661 if (EVP_DecodeUpdate(ectx, sig->bh, &decodesz, start,
662 (int)(end - start)) == -1) {
664 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid bh tag");
667 sig->bhsz = decodesz;
668 if (EVP_DecodeFinal(ectx, sig->bh + sig->bhsz, &decodesz) == -1) {
670 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid bh tag");
673 sig->bhsz += decodesz;
677 dkim_signature_parse_c(struct signature *sig, const char *start, const char *end)
680 dkim_signature_state(sig, DKIM_PERMERROR, "Duplicate c tag");
683 if (strncmp(start, "simple", 6) == 0) {
684 sig->c = CANON_HEADER_SIMPLE;
686 } else if (strncmp(start, "relaxed", 7) == 0) {
687 sig->c = CANON_HEADER_RELAXED;
690 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid c tag");
693 if (start[0] == '/') {
695 if (strncmp(start, "simple", 6) == 0) {
696 sig->c |= CANON_BODY_SIMPLE;
698 } else if (strncmp(start, "relaxed", 7) == 0) {
699 sig->c |= CANON_BODY_RELAXED;
702 dkim_signature_state(sig, DKIM_PERMERROR,
709 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid c tag");
712 sig->c |= CANON_DONE;
716 dkim_signature_parse_d(struct signature *sig, const char *start, const char *end)
718 if (sig->d[0] != '\0') {
719 dkim_signature_state(sig, DKIM_PERMERROR, "Duplicate d tag");
722 if (osmtpd_ltok_skip_sig_d_tag_value(start, 0) != end ||
723 (size_t)(end - start) >= sizeof(sig->d)) {
724 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid d tag");
727 strlcpy(sig->d, start, end - start + 1);
731 dkim_signature_parse_h(struct signature *sig, const char *start, const char *end)
736 if (sig->h != NULL) {
737 dkim_signature_state(sig, DKIM_PERMERROR, "Duplicate h tag");
740 if (osmtpd_ltok_skip_sig_h_tag_value(start, 0) < end) {
741 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid h tag");
746 if ((h = osmtpd_ltok_skip_hdr_name(h, 0)) == NULL) {
747 dkim_signature_state(sig, DKIM_PERMERROR,
752 /* ';' is part of hdr-name */
757 h = osmtpd_ltok_skip_fws(h, 1);
760 h = osmtpd_ltok_skip_fws(h + 1, 1);
762 if ((sig->h = calloc(n + 1, sizeof(*sig->h))) == NULL) {
763 dkim_err(sig->header->msg, "malloc");
769 h = osmtpd_ltok_skip_hdr_name(start, 0);
770 /* ';' is part of hdr-name */
772 sig->h[n] = strndup(start, end - start);
775 if ((sig->h[n++] = strndup(start, h - start)) == NULL) {
776 dkim_err(sig->header->msg, "malloc");
779 start = osmtpd_ltok_skip_fws(h, 1);
782 start = osmtpd_ltok_skip_fws(start + 1, 1);
787 dkim_signature_parse_i(struct signature *sig, const char *start, const char *end)
789 if (sig->i != NULL) {
790 dkim_signature_state(sig, DKIM_PERMERROR, "Duplicate i tag");
793 if (osmtpd_ltok_skip_sig_i_tag_value(start, 0) != end) {
794 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid i tag");
798 sig->isz = (size_t)(end - start);
802 dkim_signature_parse_l(struct signature *sig, const char *start, const char *end)
807 if (sig->l != -1) { /* Duplicate tag */
808 dkim_signature_state(sig, DKIM_PERMERROR, "Duplicate l tag");
812 l = strtoll(start, &lend, 10);
813 /* > 76 digits in stroll is an overflow */
814 if (osmtpd_ltok_skip_digit(start, 0) == NULL ||
815 lend != end || errno != 0) {
816 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid l tag");
820 dkim_signature_state(sig, DKIM_PERMERROR, "l tag too large");
827 dkim_signature_parse_q(struct signature *sig, const char *start, const char *end)
832 dkim_signature_state(sig, DKIM_PERMERROR, "Duplicate q tag");
837 start = osmtpd_ltok_skip_fws(start, 1);
838 qend = osmtpd_ltok_skip_sig_q_tag_method(start, 0);
840 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid q tag");
843 if (strncmp(start, "dns/txt", qend - start) == 0)
845 start = osmtpd_ltok_skip_fws(qend, 1);
850 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid q tag");
855 dkim_signature_state(sig, DKIM_NEUTRAL, "No useable q found");
861 dkim_signature_parse_s(struct signature *sig, const char *start, const char *end)
863 if (sig->s[0] != '\0') {
864 dkim_signature_state(sig, DKIM_PERMERROR, "Duplicate s tag");
867 if (osmtpd_ltok_skip_selector(start, 0) != end) {
868 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid s tag");
871 strlcpy(sig->s, start, end - start + 1);
875 dkim_signature_parse_t(struct signature *sig, const char *start, const char *end)
880 dkim_signature_state(sig, DKIM_PERMERROR, "Duplicate t tag");
884 sig->t = strtoll(start, &tend, 10);
885 if (osmtpd_ltok_skip_digit(start, 0) == NULL || tend != end ||
886 tend - start > 12 || errno != 0) {
887 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid t tag");
893 dkim_signature_parse_x(struct signature *sig, const char *start, const char *end)
898 dkim_signature_state(sig, DKIM_PERMERROR, "Duplicate x tag");
902 sig->x = strtoll(start, &xend, 10);
903 if (osmtpd_ltok_skip_digit(start, 0) == NULL || xend != end ||
904 xend - start > 12 || errno != 0) {
905 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid x tag");
911 dkim_signature_parse_z(struct signature *sig, const char *start, const char *end)
914 dkim_signature_state(sig, DKIM_PERMERROR, "Duplicate z tag");
919 if (osmtpd_ltok_skip_sig_z_tag_value(start, 0) != end) {
920 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid z tag");
926 dkim_signature_verify(struct signature *sig)
928 struct message *msg = sig->header->msg;
929 static EVP_MD_CTX *bctx = NULL;
930 char digest[EVP_MAX_MD_SIZE];
931 unsigned int digestsz;
935 if (sig->state != DKIM_UNKNOWN)
939 if ((bctx = EVP_MD_CTX_new()) == NULL) {
940 dkim_errx(msg, "EVP_MD_CTX_new");
944 EVP_MD_CTX_reset(bctx);
946 if (EVP_DigestVerifyInit(bctx, NULL, sig->ah, NULL,
948 dkim_errx(msg, "EVP_DigestVerifyInit");
952 if (EVP_DigestInit_ex(bctx, sig->ah, NULL) != 1) {
953 dkim_errx(msg, "EVP_DigestInit_ex");
958 for (i = 0; i < msg->nheaders; i++)
959 msg->header[i].parsed = 0;
961 for (header = 0; sig->h[header] != NULL; header++) {
962 for (i = msg->nheaders; i > 0; ) {
964 if (msg->header[i].parsed ||
965 strncasecmp(msg->header[i].buf, sig->h[header],
966 strlen(sig->h[header])) != 0 ||
967 msg->header[i].sig == sig)
969 end = osmtpd_ltok_skip_fws(
970 msg->header[i].buf + strlen(sig->h[header]), 1);
973 dkim_signature_header(bctx, sig, &(msg->header[i]));
974 msg->header[i].parsed = 1;
977 dkim_signature_header(bctx, sig, sig->header);
979 if (EVP_DigestVerifyFinal(bctx, sig->b, sig->bsz) != 1)
980 dkim_signature_state(sig, DKIM_FAIL, "b mismatch");
982 if (EVP_DigestFinal_ex(bctx, digest, &digestsz) == 0) {
983 dkim_errx(msg, "EVP_DigestFinal_ex");
986 if (EVP_DigestVerifyInit(bctx, NULL, NULL, NULL, sig->p) != 1) {
987 dkim_errx(msg, "EVP_DigestVerifyInit");
990 switch (EVP_DigestVerify(bctx, sig->b, sig->bsz, digest,
995 dkim_signature_state(sig, DKIM_FAIL, "b mismatch");
998 dkim_errx(msg, "EVP_DigestVerify");
1004 /* EVP_DigestVerifyUpdate is a macro, so we can't alias this on a variable */
1005 #define dkim_b_digest_update(a, b, c) \
1006 (sig->sephash ? EVP_DigestUpdate((a), (b), (c)) :\
1007 EVP_DigestVerifyUpdate((a), (b), (c)))
1010 dkim_signature_header(EVP_MD_CTX *bctx, struct signature *sig,
1011 struct header *header)
1014 const char *ptr = header->buf, *end;
1016 int canon = sig->c & CANON_HEADER;
1018 for (ptr = header->buf; ptr[0] != '\0'; ptr++) {
1020 if (canon == CANON_HEADER_RELAXED) {
1021 ptr = osmtpd_ltok_skip_fws(ptr, 1);
1022 c = tolower(ptr[0]);
1027 if (canon == CANON_HEADER_RELAXED)
1028 ptr = osmtpd_ltok_skip_fws(
1031 if (dkim_b_digest_update(bctx, &c, 1) == 0) {
1032 dkim_errx(sig->header->msg,
1033 "dkim_b_digest_update");
1038 end = osmtpd_ltok_skip_fws(ptr, 1);
1040 if (sig->header == header && ptr == sig->bheader) {
1041 ptr = osmtpd_ltok_skip_tag_value(
1045 if (dkim_b_digest_update(bctx, ptr, 1) == 0) {
1046 dkim_errx(sig->header->msg,
1047 "dkim_b_digest_update");
1051 if (canon == CANON_HEADER_RELAXED) {
1054 if (dkim_b_digest_update(bctx, " ", 1) == 0) {
1055 dkim_errx(sig->header->msg,
1056 "dkim_b_digest_update");
1060 if (dkim_b_digest_update(bctx, ptr,
1062 dkim_errx(sig->header->msg,
1063 "dkim_b_digest_update");
1071 if (sig->header != header) {
1072 if (dkim_b_digest_update(bctx, "\r\n", 2) == 0) {
1073 dkim_errx(sig->header->msg, "dkim_b_digest_update");
1080 dkim_signature_state(struct signature *sig, enum state state,
1083 if (sig->query != NULL) {
1084 event_asr_abort(sig->query);
1087 switch (sig->state) {
1092 osmtpd_errx(1, "Unexpected transition");
1094 if (state == DKIM_PASS)
1098 if (state == DKIM_PASS)
1100 if (state == DKIM_TEMPERROR || state == DKIM_PERMERROR)
1102 osmtpd_errx(1, "Unexpected transition");
1103 case DKIM_TEMPERROR:
1104 if (state == DKIM_PERMERROR)
1107 case DKIM_PERMERROR:
1111 sig->state_reason = reason;
1115 dkim_state2str(enum state state)
1129 case DKIM_TEMPERROR:
1131 case DKIM_PERMERROR:
1137 dkim_rr_resolve(struct asr_result *ar, void *arg)
1139 struct signature *sig = arg;
1140 char key[UINT16_MAX + 1];
1142 size_t keylen, cstrlen;
1144 struct dns_header h;
1150 if (ar->ar_h_errno == TRY_AGAIN || ar->ar_h_errno == NO_RECOVERY) {
1151 dkim_signature_state(sig, DKIM_TEMPERROR,
1152 hstrerror(ar->ar_h_errno));
1155 if (ar->ar_h_errno != NETDB_SUCCESS) {
1156 dkim_signature_state(sig, DKIM_PERMERROR,
1157 hstrerror(ar->ar_h_errno));
1161 unpack_init(&pack, ar->ar_data, ar->ar_datalen);
1162 if (unpack_header(&pack, &h) != 0 ||
1163 unpack_query(&pack, &q) != 0) {
1164 dkim_signature_state(sig, DKIM_PERMERROR, "Invalid dns/txt");
1167 for (; h.ancount > 0; h.ancount--) {
1168 unpack_rr(&pack, &rr);
1169 if (rr.rr_type != T_TXT)
1173 rr_txt = rr.rr.other.rdata;
1174 while (rr.rr.other.rdlen > 0) {
1175 cstrlen = ((const unsigned char *)rr_txt)[0];
1176 if (cstrlen >= rr.rr.other.rdlen ||
1177 keylen + cstrlen >= sizeof(key))
1180 * RFC 6376 Section 3.6.2.2
1181 * Strings in a TXT RR MUST be concatenated together
1182 * before use with no intervening whitespace.
1184 strlcpy(key + keylen, rr_txt + 1, cstrlen + 1);
1185 rr.rr.other.rdlen -= (cstrlen + 1);
1186 rr_txt += (cstrlen + 1);
1189 if (rr.rr.other.rdlen > 0) /* Invalid TXT RDATA */
1192 if (dkim_key_text_parse(sig, key))
1196 if (h.ancount == 0) {
1197 dkim_signature_state(sig, DKIM_PERMERROR,
1198 "No matching key found");
1200 /* Only verify if all headers have been read */
1201 if (!sig->header->msg->parsing_headers)
1202 dkim_signature_verify(sig);
1206 dkim_message_verify(sig->header->msg);
1210 dkim_key_text_parse(struct signature *sig, const char *key)
1212 char tagname, *hashname;
1213 const char *end, *tagvend;
1214 char pkraw[UINT16_MAX] = "", pkimp[UINT16_MAX];
1215 size_t pkrawlen = 0, pkoff, linelen;
1216 int h = 0, k = 0, n = 0, p = 0, s = 0, t = 0, first = 1;
1223 key = osmtpd_ltok_skip_fws(key, 1);
1224 /* Validate syntax early */
1225 if ((end = osmtpd_ltok_skip_tag_list(key, 0)) == NULL)
1228 while (key[0] != '\0') {
1229 key = osmtpd_ltok_skip_fws(key, 1);
1230 if ((end = osmtpd_ltok_skip_tag_name(key, 0)) == NULL)
1233 if ((size_t)(end - key) != 1)
1237 key = osmtpd_ltok_skip_fws(end, 1);
1241 key = osmtpd_ltok_skip_fws(key + 1, 1);
1242 if ((end = osmtpd_ltok_skip_tag_value(key, 0)) == NULL)
1247 * RFC 6376 section 3.6.1, v=:
1248 * RECOMMENDED...This tag MUST be the first tag in the
1252 osmtpd_ltok_skip_key_v_tag_value(key, 0) != end)
1257 if (h != 0) /* Duplicate tag */
1259 /* Invalid tag value */
1260 if (osmtpd_ltok_skip_key_h_tag_value(key, 0) != end)
1263 if ((tagvend = osmtpd_ltok_skip_key_h_tag_alg(
1266 hashname = strndup(key, tagvend - key);
1267 if (hashname == NULL) {
1268 dkim_err(sig->header->msg, "malloc");
1271 if (EVP_get_digestbyname(hashname) == sig->ah) {
1277 key = osmtpd_ltok_skip_fws(tagvend, 1);
1280 key = osmtpd_ltok_skip_fws(key + 1, 1);
1287 if (k != 0) /* Duplicate tag */
1290 if (strncmp(key, "rsa", end - key) == 0) {
1291 if (sig->ak != EVP_PKEY_RSA)
1294 } else if (strncmp(key, "ed25519", end - key) == 0) {
1295 if (sig->ak != EVP_PKEY_ED25519)
1303 if (n != 0) /* Duplicate tag */
1306 /* semicolon is part of safe-char */
1307 if (osmtpd_ltok_skip_key_n_tag_value(key, 0) < end)
1312 if (p != 0) /* Duplicate tag */
1316 key = osmtpd_ltok_skip_fws(key, 1);
1317 if (osmtpd_ltok_skip_alphadigitps(
1320 pkraw[pkrawlen++] = key++[0];
1321 if (pkrawlen >= sizeof(pkraw))
1324 if (key[0] == '=') {
1325 pkraw[pkrawlen++] = '=';
1326 key = osmtpd_ltok_skip_fws(key + 1, 1);
1327 if (pkrawlen >= sizeof(pkraw))
1329 if (key[0] == '=') {
1330 pkraw[pkrawlen++] = '=';
1332 if (pkrawlen >= sizeof(pkraw))
1336 /* Invalid tag value */
1337 if (pkrawlen % 4 != 0 || key != end)
1341 if (s != 0) /* Duplicate tag */
1343 /* Invalid tag value */
1344 if (osmtpd_ltok_skip_key_s_tag_value(key, 0) != end)
1348 osmtpd_ltok_skip_key_s_tag_type(
1351 if (strncmp(key, "*", tagvend - key) == 0 ||
1352 strncmp(key, "email", tagvend - key) == 0) {
1356 key = osmtpd_ltok_skip_fws(tagvend, 1);
1359 key = osmtpd_ltok_skip_fws(key + 1, 1);
1366 if (t != 0) /* Duplicate tag */
1369 if (osmtpd_ltok_skip_key_t_tag_value(key, 0) != end)
1372 tagvend = osmtpd_ltok_skip_key_t_tag_flag(
1374 if (strncmp(key, "y", tagvend - key) == 0)
1376 else if (strncmp(key, "s", tagvend - key) == 0)
1378 key = osmtpd_ltok_skip_fws(tagvend, 1);
1381 key = osmtpd_ltok_skip_fws(key + 1, 1);
1390 key = osmtpd_ltok_skip_fws(key, 1);
1393 else if (key[0] != '\0')
1397 if (!p) /* Missing tag */
1399 if (k == 0 && sig->ak != EVP_PKEY_RSA) /* Default to RSA */
1402 if (pkraw[0] == '\0') {
1403 dkim_signature_state(sig, DKIM_PERMERROR, "Key is revoked");
1409 pkoff = strlcpy(pkimp, "-----BEGIN PUBLIC KEY-----\n",
1412 for (key = pkraw; key[0] != '\0';) {
1413 if (pkoff + 2 >= sizeof(pkimp))
1415 pkimp[pkoff++] = key++[0];
1416 if (++linelen == 64) {
1417 pkimp[pkoff++] = '\n';
1421 /* Leverage pkoff check in loop */
1423 pkimp[pkoff++] = '\n';
1424 /* PEM_read_bio_PUBKEY will catch truncated keys */
1425 pkoff += strlcpy(pkimp + pkoff, "-----END PUBLIC KEY-----\n",
1426 sizeof(pkimp) - pkoff);
1427 if ((bio = BIO_new_mem_buf(pkimp, pkoff)) == NULL) {
1428 dkim_err(sig->header->msg, "BIO_new_mem_buf");
1431 sig->p = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL);
1435 case EVP_PKEY_ED25519:
1436 if ((pkrawlen / 4) * 3 >= sizeof(pkimp))
1438 EVP_DecodeInit(ectx);
1439 if (EVP_DecodeUpdate(ectx, pkimp, &tmp, pkraw, pkrawlen) == -1)
1442 if (EVP_DecodeFinal(ectx, pkimp, &tmp) == -1)
1445 sig->p = EVP_PKEY_new_raw_public_key(sig->ak, NULL, pkimp,
1450 if (sig->p == NULL) {
1452 * XXX No clue how to differentiate between invalid key and
1453 * temporary failure like *alloc.
1454 * Assume invalid key, because it's more likely.
1462 dkim_body_parse(struct message *msg, const char *line)
1464 struct signature *sig;
1465 const char *end = line, *hash, *prev;
1466 size_t hashn, len, i;
1469 if (line[0] == '\0') {
1470 msg->body_whitelines++;
1474 while (msg->body_whitelines-- > 0) {
1475 for (i = 0; i < msg->nheaders; i++) {
1476 if ((sig = msg->header[i].sig) == NULL ||
1477 sig->state != DKIM_UNKNOWN)
1479 hashn = sig->l == -1 ? 2 : MIN(2, sig->l);
1480 sig->l -= sig->l == -1 ? 0 : hashn;
1481 if (EVP_DigestUpdate(sig->bhctx, "\r\n", hashn) == 0) {
1482 dkim_errx(msg, "EVP_DigestUpdate");
1487 msg->body_whitelines = 0;
1490 while (line[0] != '\0') {
1493 if ((end = osmtpd_ltok_skip_wsp(end, 0)) == NULL)
1499 while (osmtpd_ltok_skip_wsp(end, 0) == NULL &&
1503 for (i = 0; i < msg->nheaders; i++) {
1504 sig = msg->header[i].sig;
1505 if (sig == NULL || sig->state != DKIM_UNKNOWN)
1508 (sig->c & CANON_BODY) == CANON_BODY_RELAXED) {
1510 len = end[0] == '\0' ? 0 : 1;
1513 len = (size_t)(end - line);
1515 hashn = sig->l == -1 ? len : MIN(len, (size_t)sig->l);
1516 sig->l -= sig->l == -1 ? 0 : hashn;
1517 ret = EVP_DigestUpdate(sig->bhctx, hash, hashn);
1519 dkim_errx(msg, "EVP_DigestUpdate");
1525 for (i = 0; i < msg->nheaders; i++) {
1526 sig = msg->header[i].sig;
1527 if (sig == NULL || sig->state != DKIM_UNKNOWN)
1529 hashn = sig->l == -1 ? 2 : MIN(2, sig->l);
1530 sig->l -= sig->l == -1 ? 0 : hashn;
1531 ret = EVP_DigestUpdate(sig->bhctx, "\r\n", hashn);
1533 dkim_errx(msg, "EVP_DigestUpdate");
1540 dkim_body_verify(struct signature *sig)
1542 unsigned char digest[EVP_MAX_MD_SIZE];
1543 unsigned int digestsz;
1545 if (sig->state != DKIM_UNKNOWN)
1548 if ((sig->c & CANON_BODY) == CANON_BODY_SIMPLE &&
1549 !sig->header->msg->has_body) {
1550 if (EVP_DigestUpdate(sig->bhctx, "\r\n",
1551 sig->l == -1 ? 2 : MIN(2, sig->l)) <= 0) {
1552 dkim_errx(sig->header->msg,
1553 "Can't update hash context");
1558 dkim_signature_state(sig, DKIM_PERMERROR,
1559 "l tag larger than body");
1563 if (EVP_DigestFinal_ex(sig->bhctx, digest, &digestsz) == 0) {
1564 dkim_errx(sig->header->msg, "EVP_DigestFinal_ex");
1568 if (digestsz != sig->bhsz || memcmp(digest, sig->bh, digestsz) != 0)
1569 dkim_signature_state(sig, DKIM_FAIL, "bh mismatch");
1573 dkim_message_verify(struct message *msg)
1575 struct signature *sig;
1577 ssize_t n, aroff = 0;
1585 for (i = 0; i < msg->nheaders; i++) {
1586 if (msg->header[i].sig == NULL)
1588 if (msg->header[i].sig->query != NULL)
1590 if (msg->header[i].sig->state != DKIM_UNKNOWN)
1592 dkim_signature_state(msg->header[i].sig, DKIM_PASS, NULL);
1595 if ((aroff = dkim_ar_cat(&line, &linelen, aroff,
1596 "Authentication-Results: %s", authservid)) == -1) {
1597 dkim_err(msg, "malloc");
1600 for (i = 0; i < msg->nheaders; i++) {
1601 sig = msg->header[i].sig;
1605 if ((aroff = dkim_ar_cat(&line, &linelen, aroff, "; dkim=%s",
1606 dkim_state2str(sig->state))) == -1) {
1607 dkim_err(msg, "malloc");
1610 if (sig->state_reason != NULL) {
1611 if ((aroff = dkim_ar_cat(&line, &linelen, aroff,
1612 " reason=\"%s\"", sig->state_reason)) == -1) {
1613 dkim_err(msg, "malloc");
1617 if (sig->s[0] != '\0') {
1618 if ((aroff = dkim_ar_cat(&line, &linelen, aroff,
1619 " header.s=%s", sig->s)) == -1) {
1620 dkim_err(msg, "malloc");
1624 if (sig->d[0] != '\0') {
1625 if ((aroff = dkim_ar_cat(&line, &linelen, aroff,
1626 " header.d=%s", sig->d)) == -1) {
1627 dkim_err(msg, "malloc");
1632 * Don't print i-tag, since localpart can be a quoted-string,
1633 * which can contain FWS and CFWS.
1635 if (sig->a != NULL) {
1636 if ((aroff = dkim_ar_cat(&line, &linelen, aroff,
1637 " header.a=%.*s", (int)sig->asz, sig->a)) == -1) {
1638 dkim_err(msg, "malloc");
1644 aroff = dkim_ar_cat(&line, &linelen, aroff, "; dkim=none");
1646 dkim_err(msg, "malloc");
1650 dkim_ar_print(msg->ctx, line);
1653 while ((n = getline(&line, &linelen, msg->origf)) != -1) {
1655 osmtpd_filter_dataline(msg->ctx, "%s", line);
1657 if (ferror(msg->origf))
1658 dkim_err(msg, "getline");
1665 dkim_ar_print(struct osmtpd_ctx *ctx, const char *start)
1667 const char *scan, *checkpoint, *ncheckpoint;
1669 int first = 1, arid = 1;
1672 ncheckpoint = osmtpd_ltok_skip_hdr_name(start, 0) + 1;
1673 for (scan = start; scan[0] != '\0'; scan++) {
1674 if (scan[0] == '\t')
1675 arlen = (arlen + 8) & ~7;
1678 if (arlen >= AUTHENTICATION_RESULTS_LINELEN) {
1679 osmtpd_filter_dataline(ctx, "%s%.*s", first ? "" : "\t",
1680 (int)((checkpoint == start ?
1681 ncheckpoint : checkpoint) - start), start);
1682 start = osmtpd_ltok_skip_cfws(checkpoint, 1);
1687 if (scan == ncheckpoint) {
1688 checkpoint = ncheckpoint;
1689 ncheckpoint = osmtpd_ltok_skip_cfws(ncheckpoint, 1);
1692 ncheckpoint = osmtpd_ltok_skip_value(
1696 } else if (strncmp(ncheckpoint, "dkim",
1697 sizeof("dkim") - 1) == 0) {
1698 ncheckpoint = osmtpd_ltok_skip_keyword(
1699 ncheckpoint + sizeof("dkim"), 0);
1701 } else if (strncmp(ncheckpoint, "reason",
1702 sizeof("reason") - 1) == 0) {
1703 ncheckpoint = osmtpd_ltok_skip_value(
1704 ncheckpoint + sizeof("reason"), 0);
1707 ncheckpoint += sizeof("header.x=") - 1;
1708 ncheckpoint = osmtpd_ltok_skip_ar_pvalue(
1710 if (ncheckpoint[0] == ';')
1715 osmtpd_filter_dataline(ctx, "%s%s", first ? "" : "\t", start);
1719 dkim_ar_cat(char **ar, size_t *n, size_t aroff, const char *fmt, ...)
1727 size = vsnprintf(*ar + aroff, *n - aroff, fmt, ap);
1729 if (size + aroff <= *n)
1730 return (ssize_t)size + aroff;
1731 nn = (((aroff + size) / 256) + 1) * 256;
1732 artmp = realloc(*ar, nn);
1738 size = vsnprintf(*ar + aroff, *n - aroff, fmt, ap);
1740 return (ssize_t)size + aroff;
1744 dkim_err(struct message *msg, char *text)
1747 fprintf(stderr, "%s: %s\n", text, strerror(errno));
1751 dkim_errx(struct message *msg, char *text)
1754 fprintf(stderr, "%s\n", text);
1760 fprintf(stderr, "usage: filter-dkimverify\n");