2 * Copyright (c) 2019 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 <openssl/err.h>
17 #include <openssl/evp.h>
18 #include <openssl/pem.h>
19 #include <openssl/sha.h>
31 #include "openbsd-compat.h"
32 #include "opensmtpd.h"
35 struct dkim_signature {
46 size_t body_whitelines;
48 struct dkim_signature signature;
53 /* RFC 6376 section 5.4.1 */
54 static char *dsign_headers[] = {
75 static char **sign_headers = dsign_headers;
76 static size_t nsign_headers = sizeof(dsign_headers) / sizeof(*dsign_headers);
78 static char *hashalg = "sha256";
79 static char *cryptalg = "rsa";
81 #define CANON_SIMPLE 0
82 #define CANON_RELAXED 1
83 static int canonheader = CANON_SIMPLE;
84 static int canonbody = CANON_SIMPLE;
86 static int addtime = 0;
87 static long long addexpire = 0;
88 static int addheaders = 0;
90 static char **domain = NULL;
91 static size_t ndomains = 0;
92 static char *selector = NULL;
94 static EVP_PKEY *pkey;
95 static const EVP_MD *hash_md;
96 static int keyid = EVP_PKEY_RSA;
97 static int sephash = 0;
99 #define DKIM_SIGNATURE_LINELEN 78
102 void dkim_adddomain(char *);
103 void dkim_err(struct dkim_message *, char *);
104 void dkim_errx(struct dkim_message *, char *);
105 void dkim_headers_set(char *);
106 void dkim_dataline(struct osmtpd_ctx *, const char *);
107 void dkim_commit(struct osmtpd_ctx *);
108 void *dkim_message_new(struct osmtpd_ctx *);
109 void dkim_message_free(struct osmtpd_ctx *, void *);
110 void dkim_parse_header(struct dkim_message *, char *, int);
111 void dkim_parse_body(struct dkim_message *, char *);
112 void dkim_sign(struct osmtpd_ctx *);
113 int dkim_signature_printheader(struct dkim_message *, const char *);
114 int dkim_signature_printf(struct dkim_message *, char *, ...)
115 __attribute__((__format__ (printf, 2, 3)));
116 int dkim_signature_normalize(struct dkim_message *);
117 const char *dkim_domain_select(struct dkim_message *, char *);
118 int dkim_signature_need(struct dkim_message *, size_t);
119 int dkim_sign_init(struct dkim_message *);
122 main(int argc, char *argv[])
131 while ((ch = getopt(argc, argv, "a:c:D:d:h:k:s:tx:z")) != -1) {
134 if (strncmp(optarg, "rsa-", 4) == 0) {
136 hashalg = optarg + 4;
137 keyid = EVP_PKEY_RSA;
140 } else if (strncmp(optarg, "ed25519-", 8) == 0) {
141 hashalg = optarg + 8;
142 cryptalg = "ed25519";
143 keyid = EVP_PKEY_ED25519;
147 osmtpd_errx(1, "invalid algorithm");
150 if (strncmp(optarg, "simple", 6) == 0) {
151 canonheader = CANON_SIMPLE;
153 } else if (strncmp(optarg, "relaxed", 7) == 0) {
154 canonheader = CANON_RELAXED;
157 osmtpd_err(1, "Invalid canonicalization");
158 if (optarg[0] == '/') {
159 if (strcmp(optarg + 1, "simple") == 0)
160 canonbody = CANON_SIMPLE;
161 else if (strcmp(optarg + 1, "relaxed") == 0)
162 canonbody = CANON_RELAXED;
165 "Invalid canonicalization");
166 } else if (optarg[0] == '\0')
167 canonbody = CANON_SIMPLE;
169 osmtpd_err(1, "Invalid canonicalization");
172 if ((file = fopen(optarg, "r")) == NULL)
173 osmtpd_err(1, "Can't open domain file (%s)",
178 linelen = getline(&line, &linesz, file);
180 if (line[linelen - 1] == '\n')
181 line[linelen - 1] = '\0';
184 dkim_adddomain(line);
186 } while (linelen != -1);
188 osmtpd_err(1, "Error reading domain file (%s)",
193 dkim_adddomain(optarg);
196 dkim_headers_set(optarg);
199 if ((file = fopen(optarg, "r")) == NULL)
200 osmtpd_err(1, "Can't open key file (%s)",
202 pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL);
204 osmtpd_errx(1, "Can't read key file");
214 addexpire = strtonum(optarg, 1, INT64_MAX, &errstr);
216 osmtpd_errx(1, "Expire offset is %s", errstr);
226 OpenSSL_add_all_digests();
228 if (pledge("tmppath stdio", NULL) == -1)
229 osmtpd_err(1, "pledge");
231 if ((hash_md = EVP_get_digestbyname(hashalg)) == NULL)
232 osmtpd_errx(1, "Can't find hash: %s", hashalg);
234 if (domain == NULL || selector == NULL || pkey == NULL)
237 if (EVP_PKEY_id(pkey) != keyid)
238 osmtpd_errx(1, "Key is not of type %s", cryptalg);
240 osmtpd_register_filter_dataline(dkim_dataline);
241 osmtpd_register_filter_commit(dkim_commit);
242 osmtpd_local_message(dkim_message_new, dkim_message_free);
249 dkim_adddomain(char *d)
251 domain = reallocarray(domain, ndomains + 1, sizeof(*domain));
253 osmtpd_err(1, "malloc");
254 domain[ndomains++] = d;
259 dkim_dataline(struct osmtpd_ctx *ctx, const char *line)
261 struct dkim_message *message = ctx->local_message;
266 if (line[0] == '.' && line[1] =='\0')
267 osmtpd_filter_dataline(ctx, ".");
271 linelen = strlen(line);
272 if (fprintf(message->origf, "%s\n", line) < (int) linelen)
273 dkim_errx(message, "Couldn't write to tempfile");
275 if (line[0] == '.' && line[1] =='\0') {
277 } else if (linelen != 0 && message->parsing_headers) {
280 if ((linedup = strdup(line)) == NULL)
281 osmtpd_err(1, "strdup");
282 dkim_parse_header(message, linedup, 0);
284 } else if (linelen == 0 && message->parsing_headers) {
285 if (addheaders > 0 && !dkim_signature_printf(message, "; "))
287 message->parsing_headers = 0;
291 if ((linedup = strdup(line)) == NULL)
292 osmtpd_err(1, "strdup");
293 dkim_parse_body(message, linedup);
299 dkim_commit(struct osmtpd_ctx *ctx)
301 struct dkim_message *message = ctx->local_message;
304 osmtpd_filter_disconnect(ctx, "Internal server error");
306 osmtpd_filter_proceed(ctx);
310 dkim_message_new(struct osmtpd_ctx *ctx)
312 struct dkim_message *message;
314 if ((message = calloc(1, sizeof(*message))) == NULL) {
315 dkim_err(message, "Failed to create message context");
319 if ((message->origf = tmpfile()) == NULL) {
320 dkim_err(message, "Failed to open tempfile");
323 message->parsing_headers = 1;
325 message->body_whitelines = 0;
326 message->headers = calloc(1, sizeof(*(message->headers)));
327 if (message->headers == NULL) {
328 dkim_err(message, "Can't save headers");
331 message->lastheader = 0;
332 message->signature.signature = NULL;
333 message->signature.size = 0;
334 message->signature.len = 0;
337 if (!dkim_signature_printf(message,
338 "DKIM-Signature: v=%s; a=%s-%s; c=%s/%s; s=%s; ", "1",
340 canonheader == CANON_SIMPLE ? "simple" : "relaxed",
341 canonbody == CANON_SIMPLE ? "simple" : "relaxed", selector))
343 if (addheaders > 0 && !dkim_signature_printf(message, "z="))
346 if ((message->dctx = EVP_MD_CTX_new()) == NULL) {
347 dkim_errx(message, "Failed to create hash context");
350 if (EVP_DigestInit_ex(message->dctx, hash_md, NULL) <= 0) {
351 dkim_errx(message, "Failed to initialize hash context");
356 dkim_message_free(ctx, message);
361 dkim_message_free(struct osmtpd_ctx *ctx, void *data)
363 struct dkim_message *message = data;
366 fclose(message->origf);
367 EVP_MD_CTX_free(message->dctx);
368 free(message->signature.signature);
369 for (i = 0; message->headers != NULL &&
370 message->headers[i] != NULL; i++)
371 free(message->headers[i]);
372 free(message->headers);
377 dkim_headers_set(char *headers)
384 for (i = 0; headers[i] != '\0'; i++) {
385 /* RFC 5322 field-name */
386 if (!(headers[i] >= 33 && headers[i] <= 126))
387 osmtpd_errx(1, "-h: invalid character");
388 if (headers[i] == ':') {
389 /* Test for empty headers */
390 if (i == 0 || headers[i - 1] == ':')
391 osmtpd_errx(1, "-h: header can't be empty");
394 headers[i] = tolower(headers[i]);
396 if (headers[i - 1] == ':')
397 osmtpd_errx(1, "-h: header can't be empty");
399 if ((sign_headers = reallocarray(NULL, nsign_headers + 1,
400 sizeof(*sign_headers))) == NULL)
401 osmtpd_errx(1, NULL);
403 for (i = 0; i < nsign_headers; i++) {
404 sign_headers[i] = headers;
405 if (i != nsign_headers - 1) {
406 headers = strchr(headers, ':');
409 if (strcasecmp(sign_headers[i], "from") == 0)
413 osmtpd_errx(1, "From header must be included");
417 dkim_err(struct dkim_message *message, char *msg)
420 fprintf(stderr, "%s: %s\n", msg, strerror(errno));
424 dkim_errx(struct dkim_message *message, char *msg)
427 fprintf(stderr, "%s\n", msg);
431 dkim_parse_header(struct dkim_message *message, char *line, int force)
443 if (addheaders == 2 && !force &&
444 !dkim_signature_printheader(message, line))
447 if ((line[0] == ' ' || line[0] == '\t') && !message->lastheader)
449 if ((line[0] != ' ' && line[0] != '\t')) {
450 message->lastheader = 0;
451 for (i = 0; i < nsign_headers; i++) {
452 hlen = strlen(sign_headers[i]);
453 if (strncasecmp(line, sign_headers[i], hlen) == 0) {
454 while (line[hlen] == ' ' || line[hlen] == '\t')
456 if (line[hlen] != ':')
461 if (i == nsign_headers && !force)
465 if (addheaders == 1 && !force &&
466 !dkim_signature_printheader(message, line))
469 if (canonheader == CANON_RELAXED) {
470 if (!message->lastheader)
472 for (r = w = 0; line[r] != '\0'; r++) {
473 if (line[r] == ':' && fieldname) {
474 if (w > 0 && line[w - 1] == ' ')
479 while (line[r + 1] == ' ' ||
484 if (line[r] == ' ' || line[r] == '\t' ||
485 line[r] == '\r' || line[r] == '\n') {
486 if (r != 0 && w != 0 && line[w - 1] == ' ')
490 } else if (fieldname) {
491 line[w++] = tolower(line[r]);
496 linelen = (w != 0 && line[w - 1] == ' ') ? w - 1 : w;
497 line[linelen] = '\0';
499 linelen = strlen(line);
501 for (lastheader = 0; message->headers[lastheader] != NULL; lastheader++)
503 if (!message->lastheader) {
504 mtmp = recallocarray(message->headers, lastheader + 1,
505 lastheader + 2, sizeof(*mtmp));
507 dkim_err(message, "Can't store header");
510 message->headers = mtmp;
512 if ((message->headers[lastheader] = strdup(line)) == NULL) {
513 dkim_err(message, "Can't store header");
516 message->headers[lastheader + 1 ] = NULL;
517 message->lastheader = 1;
520 linelen += strlen(message->headers[lastheader]);
521 if (canonheader == CANON_SIMPLE)
524 htmp = reallocarray(message->headers[lastheader], linelen,
527 dkim_err(message, "Can't store header");
530 message->headers[lastheader] = htmp;
531 if (canonheader == CANON_SIMPLE) {
532 if (strlcat(htmp, "\r\n", linelen) >= linelen)
533 osmtpd_errx(1, "Missized header");
534 } else if (canonheader == CANON_RELAXED &&
535 (tmp = strchr(message->headers[lastheader], ':')) != NULL &&
539 if (strlcat(htmp, line, linelen) >= linelen)
540 osmtpd_errx(1, "Missized header");
545 dkim_parse_body(struct dkim_message *message, char *line)
550 if (canonbody == CANON_RELAXED) {
551 for (r = w = 0; line[r] != '\0'; r++) {
552 if (line[r] == ' ' || line[r] == '\t') {
553 if (r != 0 && line[w - 1] == ' ')
560 linelen = (w != 0 && line[w - 1] == ' ') ? w - 1 : w;
561 line[linelen] = '\0';
563 linelen = strlen(line);
565 if (line[0] == '\0') {
566 message->body_whitelines++;
570 while (message->body_whitelines--) {
571 if (EVP_DigestUpdate(message->dctx, "\r\n", 2) == 0) {
572 dkim_errx(message, "Can't update hash context");
576 message->body_whitelines = 0;
577 message->has_body = 1;
579 if (EVP_DigestUpdate(message->dctx, line, linelen) == 0 ||
580 EVP_DigestUpdate(message->dctx, "\r\n", 2) == 0) {
581 dkim_errx(message, "Can't update hash context");
587 dkim_sign(struct osmtpd_ctx *ctx)
589 struct dkim_message *message = ctx->local_message;
590 /* Use largest hash size here */
591 unsigned char bdigest[EVP_MAX_MD_SIZE];
592 unsigned char digest[(((sizeof(bdigest) + 2) / 3) * 4) + 1];
594 const char *sdomain = domain[0], *tsdomain;
599 unsigned int digestsz;
601 if (addtime || addexpire)
603 if (addtime && !dkim_signature_printf(message, "t=%lld; ",
606 if (addexpire != 0 && !dkim_signature_printf(message, "x=%lld; ",
607 now + addexpire < now ? INT64_MAX : now + addexpire))
610 if (canonbody == CANON_SIMPLE && !message->has_body) {
611 if (EVP_DigestUpdate(message->dctx, "\r\n", 2) <= 0) {
612 dkim_errx(message, "Can't update hash context");
616 if (EVP_DigestFinal_ex(message->dctx, bdigest, &digestsz) == 0) {
617 dkim_errx(message, "Can't finalize hash context");
620 EVP_EncodeBlock(digest, bdigest, digestsz);
621 if (!dkim_signature_printf(message, "bh=%s; h=", digest))
623 /* Reverse order for ease of use of RFC6367 section 5.4.2 */
624 for (i = 0; message->headers[i] != NULL; i++)
626 EVP_MD_CTX_reset(message->dctx);
628 if (EVP_DigestSignInit(message->dctx, NULL, hash_md, NULL,
630 dkim_errx(message, "Failed to initialize signature "
635 if (EVP_DigestInit_ex(message->dctx, hash_md, NULL) != 1) {
636 dkim_errx(message, "Failed to initialize hash context");
640 for (i--; i >= 0; i--) {
642 if (EVP_DigestSignUpdate(message->dctx,
644 strlen(message->headers[i])) != 1 ||
645 EVP_DigestSignUpdate(message->dctx, "\r\n",
647 dkim_errx(message, "Failed to update signature "
652 if (EVP_DigestUpdate(message->dctx, message->headers[i],
653 strlen(message->headers[i])) != 1 ||
654 EVP_DigestUpdate(message->dctx, "\r\n", 2) <= 0) {
655 dkim_errx(message, "Failed to update digest "
660 if ((tsdomain = dkim_domain_select(message, message->headers[i])) != NULL)
662 /* We're done with the cached header after hashing */
663 for (tmp = message->headers[i]; tmp[0] != ':'; tmp++) {
664 if (tmp[0] == ' ' || tmp[0] == '\t')
666 tmp[0] = tolower(tmp[0]);
669 if (!dkim_signature_printf(message, "%s%s",
670 message->headers[i + 1] == NULL ? "" : ":",
671 message->headers[i]))
674 dkim_signature_printf(message, "; d=%s; b=", sdomain);
675 if (!dkim_signature_normalize(message))
677 if ((tmp = strdup(message->signature.signature)) == NULL) {
678 dkim_err(message, "Can't create DKIM signature");
681 dkim_parse_header(message, tmp, 1);
683 if (EVP_DigestSignUpdate(message->dctx, tmp,
685 dkim_errx(message, "Failed to update signature "
690 if (EVP_DigestUpdate(message->dctx, tmp, strlen(tmp)) != 1) {
691 dkim_errx(message, "Failed to update digest context");
697 if (EVP_DigestSignFinal(message->dctx, NULL, &linelen) != 1) {
698 dkim_errx(message, "Can't finalize signature context");
703 if (EVP_DigestFinal_ex(message->dctx, bdigest,
705 dkim_errx(message, "Can't finalize hash context");
708 EVP_MD_CTX_reset(message->dctx);
709 if (EVP_DigestSignInit(message->dctx, NULL, NULL, NULL,
711 dkim_errx(message, "Failed to initialize signature "
715 if (EVP_DigestSign(message->dctx, NULL, &linelen, bdigest,
717 dkim_errx(message, "Failed to finalize signature");
722 if ((tmp = malloc(linelen)) == NULL) {
723 dkim_err(message, "Can't allocate space for signature");
727 if (EVP_DigestSignFinal(message->dctx, tmp, &linelen) != 1) {
728 dkim_errx(message, "Failed to finalize signature");
733 if (EVP_DigestSign(message->dctx, tmp, &linelen, bdigest,
735 dkim_errx(message, "Failed to finalize signature");
740 if ((b = malloc((((linelen + 2) / 3) * 4) + 1)) == NULL) {
741 dkim_err(message, "Can't create DKIM signature");
744 EVP_EncodeBlock(b, tmp, linelen);
746 dkim_signature_printf(message, "%s\r\n", b);
748 dkim_signature_normalize(message);
749 tmp = message->signature.signature;
750 while ((tmp2 = strchr(tmp, '\r')) != NULL) {
752 osmtpd_filter_dataline(ctx, "%s", tmp);
757 rewind(message->origf);
758 while ((i = getline(&tmp, &linelen, message->origf)) != -1) {
760 osmtpd_filter_dataline(ctx, "%s", tmp);
765 osmtpd_filter_dataline(ctx, ".");
769 dkim_signature_normalize(struct dkim_message *message)
775 size_t *headerlen = &(message->signature.len);
778 char *sig = message->signature.signature;
780 for (linelen = i = 0; sig[i] != '\0'; i++) {
781 if (sig[i] == '\r' && sig[i + 1] == '\n') {
788 linelen = (linelen + 8) & ~7;
798 if (linelen > DKIM_SIGNATURE_LINELEN && checkpoint != 0) {
799 for (skip = checkpoint + 1;
800 sig[skip] == ' ' || sig[skip] == '\t';
803 skip -= checkpoint + 1;
804 if (!dkim_signature_need(message,
805 skip > 3 ? 0 : 3 - skip + 1))
807 sig = message->signature.signature;
809 memmove(sig + checkpoint + 3,
810 sig + checkpoint + skip,
811 *headerlen - skip - checkpoint + 1);
812 sig[checkpoint + 1] = '\r';
813 sig[checkpoint + 2] = '\n';
814 sig[checkpoint + 3] = '\t';
816 *headerlen = *headerlen + 3 - skip;
836 if (tag == '\0' && sig[i] != ' ' && sig[i] != '\t') {
837 if ((tag = sig[i]) == 'b' && sig[i + 1] == 'h' &&
850 dkim_signature_printheader(struct dkim_message *message, const char *header)
853 static char *fmtheader = NULL;
855 static size_t size = 0;
858 len = strlen(header);
859 if ((len + 3) * 3 < len) {
861 dkim_err(message, "Can't add z-component to header");
864 if ((len + 3) * 3 > size) {
865 if ((tmp = reallocarray(fmtheader, 3, len + 3)) == NULL) {
866 dkim_err(message, "Can't add z-component to header");
870 size = (len + 1) * 3;
873 first = message->signature.signature[message->signature.len - 1] == '=';
874 for (j = i = 0; header[i] != '\0'; i++, j++) {
875 if (i == 0 && header[i] != ' ' && header[i] != '\t' && !first)
876 fmtheader[j++] = '|';
877 if ((header[i] >= 0x21 && header[i] <= 0x3A) ||
878 (header[i] == 0x3C) ||
879 (header[i] >= 0x3E && header[i] <= 0x7B) ||
880 (header[i] >= 0x7D && header[i] <= 0x7E))
881 fmtheader[j] = header[i];
883 fmtheader[j++] = '=';
884 (void) sprintf(fmtheader + j, "%02hhX", header[i]);
888 (void) sprintf(fmtheader + j, "=%02hhX=%02hhX", (unsigned char) '\r',
889 (unsigned char) '\n');
891 return dkim_signature_printf(message, "%s", fmtheader);
895 dkim_signature_printf(struct dkim_message *message, char *fmt, ...)
897 struct dkim_signature *sig = &(message->signature);
902 if ((len = vsnprintf(sig->signature + sig->len, sig->size - sig->len,
903 fmt, ap)) >= sig->size - sig->len) {
905 if (!dkim_signature_need(message, len + 1))
908 if ((len = vsnprintf(sig->signature + sig->len,
909 sig->size - sig->len, fmt, ap)) >= sig->size - sig->len)
910 osmtpd_errx(1, "Miscalculated header size");
918 dkim_domain_select(struct dkim_message *message, char *from)
920 char *mdomain0, *mdomain;
923 if ((mdomain = mdomain0 = osmtpd_mheader_from_domain(from)) == NULL) {
924 if (errno != EINVAL) {
925 dkim_errx(message, "Couldn't parse from header");
931 while (mdomain != NULL && mdomain[0] != '\0') {
932 for (i = 0; i < ndomains; i++) {
933 if (strcasecmp(mdomain, domain[i]) == 0) {
938 if ((mdomain = strchr(mdomain, '.')) != NULL)
946 dkim_signature_need(struct dkim_message *message, size_t len)
948 struct dkim_signature *sig = &(message->signature);
951 if (sig->len + len < sig->size)
953 sig->size = (((len + sig->len) / 512) + 1) * 512;
954 if ((tmp = realloc(sig->signature, sig->size)) == NULL) {
955 dkim_err(message, "No room for signature");
958 sig->signature = tmp;
965 fprintf(stderr, "usage: filter-dkimsign [-tz] [-a signalg] "
966 "[-c canonicalization] \n [-h headerfields]"
967 "[-x seconds] -D file -d domain -k keyfile -s selector\n");