Blob


1 /*
2 * Copyright (c) 2019 Martijn van Duren <martijn@openbsd.org>
3 *
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.
7 *
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.
15 */
16 #include <openssl/err.h>
17 #include <openssl/evp.h>
18 #include <openssl/pem.h>
19 #include <openssl/sha.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <syslog.h>
29 #include <time.h>
30 #include <unistd.h>
32 #include "openbsd-compat.h"
33 #include "opensmtpd.h"
34 #include "mheader.h"
36 struct dkim_signature {
37 char *signature;
38 size_t size;
39 size_t len;
40 };
42 struct dkim_message {
43 FILE *origf;
44 int parsing_headers;
45 char **headers;
46 int lastheader;
47 size_t body_whitelines;
48 int has_body;
49 struct dkim_signature signature;
50 EVP_MD_CTX *dctx;
51 };
53 /* RFC 6376 section 5.4.1 */
54 static char *dsign_headers[] = {
55 "from",
56 "reply-to",
57 "subject",
58 "date",
59 "to",
60 "cc",
61 "resent-date",
62 "resent-from",
63 "resent-to",
64 "resent-cc",
65 "in-reply-to",
66 "references",
67 "list-id",
68 "list-help",
69 "list-unsubscribe",
70 "list-subscribe",
71 "list-post",
72 "list-owner",
73 "list-archive"
74 };
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
101 void usage(void);
102 void dkim_adddomain(char *);
103 void dkim_headers_set(char *);
104 int dkim_dataline(struct osmtpd_ctx *, const char *);
105 void *dkim_message_new(struct osmtpd_ctx *);
106 void dkim_message_free(struct osmtpd_ctx *, void *);
107 void dkim_parse_header(struct dkim_message *, char *, int);
108 void dkim_parse_body(struct dkim_message *, char *);
109 void dkim_sign(struct osmtpd_ctx *);
110 void dkim_signature_printheader(struct dkim_message *, const char *);
111 void dkim_signature_printf(struct dkim_message *, char *, ...)
112 __attribute__((__format__ (printf, 2, 3)));
113 void dkim_signature_normalize(struct dkim_message *);
114 const char *dkim_domain_select(struct dkim_message *, char *);
115 void dkim_signature_need(struct dkim_message *, size_t);
116 int dkim_sign_init(struct dkim_message *);
118 int
119 main(int argc, char *argv[])
121 int ch;
122 FILE *file;
123 char *line;
124 size_t linesz;
125 ssize_t linelen;
126 const char *errstr;
128 while ((ch = getopt(argc, argv, "a:c:D:d:h:k:s:tx:z")) != -1) {
129 switch (ch) {
130 case 'a':
131 if (strncmp(optarg, "rsa-", 4) == 0) {
132 cryptalg = "rsa";
133 hashalg = optarg + 4;
134 keyid = EVP_PKEY_RSA;
135 sephash = 0;
136 } else if (strncmp(optarg, "ed25519-", 8) == 0) {
137 hashalg = optarg + 8;
138 cryptalg = "ed25519";
139 keyid = EVP_PKEY_ED25519;
140 sephash = 1;
141 } else
142 osmtpd_errx(1, "invalid algorithm");
143 break;
144 case 'c':
145 if (strncmp(optarg, "simple", 6) == 0) {
146 canonheader = CANON_SIMPLE;
147 optarg += 6;
148 } else if (strncmp(optarg, "relaxed", 7) == 0) {
149 canonheader = CANON_RELAXED;
150 optarg += 7;
151 } else
152 osmtpd_errx(1, "Invalid canonicalization");
153 if (optarg[0] == '/') {
154 if (strcmp(optarg + 1, "simple") == 0)
155 canonbody = CANON_SIMPLE;
156 else if (strcmp(optarg + 1, "relaxed") == 0)
157 canonbody = CANON_RELAXED;
158 else
159 osmtpd_errx(1,
160 "Invalid canonicalization");
161 } else if (optarg[0] == '\0')
162 canonbody = CANON_SIMPLE;
163 else
164 osmtpd_errx(1, "Invalid canonicalization");
165 break;
166 case 'D':
167 if ((file = fopen(optarg, "r")) == NULL)
168 osmtpd_err(1, "Can't open domain file (%s)",
169 optarg);
170 do {
171 line = NULL;
172 linesz = 0;
173 linelen = getline(&line, &linesz, file);
174 if (linelen > 0) {
175 if (line[linelen - 1] == '\n')
176 line[linelen - 1] = '\0';
177 if (line[0] == '#')
178 continue;
179 dkim_adddomain(line);
181 } while (linelen != -1);
182 if (ferror(file))
183 osmtpd_err(1, "Error reading domain file (%s)",
184 optarg);
185 fclose(file);
186 break;
187 case 'd':
188 dkim_adddomain(optarg);
189 break;
190 case 'h':
191 dkim_headers_set(optarg);
192 break;
193 case 'k':
194 if ((file = fopen(optarg, "r")) == NULL)
195 osmtpd_err(1, "Can't open key file (%s)",
196 optarg);
197 pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL);
198 if (pkey == NULL)
199 osmtpd_errx(1, "Can't read key file");
200 fclose(file);
201 break;
202 case 's':
203 selector = optarg;
204 break;
205 case 't':
206 addtime = 1;
207 break;
208 case 'x':
209 addexpire = strtonum(optarg, 1, INT64_MAX, &errstr);
210 if (addexpire == 0)
211 osmtpd_errx(1, "Expire offset is %s", errstr);
212 break;
213 case 'z':
214 addheaders++;
215 break;
216 default:
217 usage();
221 OpenSSL_add_all_digests();
223 if (pledge("tmppath stdio", NULL) == -1)
224 osmtpd_err(1, "pledge");
226 if ((hash_md = EVP_get_digestbyname(hashalg)) == NULL)
227 osmtpd_errx(1, "Can't find hash: %s", hashalg);
229 if (domain == NULL || selector == NULL || pkey == NULL)
230 usage();
232 if (EVP_PKEY_id(pkey) != keyid)
233 osmtpd_errx(1, "Key is not of type %s", cryptalg);
235 osmtpd_register_filter_dataline(dkim_dataline);
236 osmtpd_local_message(dkim_message_new, dkim_message_free);
237 osmtpd_run();
239 return 0;
242 void
243 dkim_adddomain(char *d)
245 domain = reallocarray(domain, ndomains + 1, sizeof(*domain));
246 if (domain == NULL)
247 osmtpd_err(1, "reallocarray");
248 domain[ndomains++] = d;
251 int
252 dkim_dataline(struct osmtpd_ctx *ctx, const char *line)
254 struct dkim_message *message = ctx->local_message;
255 char *linedup;
256 size_t linelen;
258 linelen = strlen(line);
259 if (fprintf(message->origf, "%s\n", line) < (int) linelen) {
260 osmtpd_warnx(ctx, "Couldn't write to tempfile");
261 return -1;
264 if (line[0] == '.' && line[1] =='\0') {
265 dkim_sign(ctx);
266 } else if (linelen != 0 && message->parsing_headers) {
267 if (line[0] == '.')
268 line++;
269 if ((linedup = strdup(line)) == NULL)
270 osmtpd_err(1, "strdup");
271 dkim_parse_header(message, linedup, 0);
272 free(linedup);
273 } else if (linelen == 0 && message->parsing_headers) {
274 if (addheaders > 0)
275 dkim_signature_printf(message, "; ");
276 message->parsing_headers = 0;
277 } else {
278 if (line[0] == '.')
279 line++;
280 if ((linedup = strdup(line)) == NULL)
281 osmtpd_err(1, "strdup");
282 dkim_parse_body(message, linedup);
283 free(linedup);
286 return 0;
289 void *
290 dkim_message_new(struct osmtpd_ctx *ctx)
292 struct dkim_message *message;
294 if ((message = calloc(1, sizeof(*message))) == NULL) {
295 osmtpd_warn(ctx, "calloc");
296 return NULL;
299 if ((message->origf = tmpfile()) == NULL) {
300 osmtpd_warn(ctx, "Failed to open tempfile");
301 goto fail;
303 message->parsing_headers = 1;
305 message->body_whitelines = 0;
306 message->headers = calloc(1, sizeof(*(message->headers)));
307 if (message->headers == NULL) {
308 osmtpd_warn(ctx, "calloc");
309 goto fail;
311 message->lastheader = 0;
312 message->signature.signature = NULL;
313 message->signature.size = 0;
314 message->signature.len = 0;
316 dkim_signature_printf(message,
317 "DKIM-Signature: v=%s; a=%s-%s; c=%s/%s; s=%s; ", "1",
318 cryptalg, hashalg,
319 canonheader == CANON_SIMPLE ? "simple" : "relaxed",
320 canonbody == CANON_SIMPLE ? "simple" : "relaxed", selector);
321 if (addheaders > 0)
322 dkim_signature_printf(message, "z=");
324 if ((message->dctx = EVP_MD_CTX_new()) == NULL) {
325 osmtpd_warnx(ctx, "EVP_MD_CTX_new");
326 goto fail;
328 if (EVP_DigestInit_ex(message->dctx, hash_md, NULL) <= 0) {
329 osmtpd_warnx(ctx, "EVP_DigestInit_ex");
330 goto fail;
333 return message;
334 fail:
335 dkim_message_free(ctx, message);
336 return NULL;
339 void
340 dkim_message_free(struct osmtpd_ctx *ctx, void *data)
342 struct dkim_message *message = data;
343 size_t i;
345 fclose(message->origf);
346 EVP_MD_CTX_free(message->dctx);
347 free(message->signature.signature);
348 for (i = 0; message->headers != NULL &&
349 message->headers[i] != NULL; i++)
350 free(message->headers[i]);
351 free(message->headers);
352 free(message);
355 void
356 dkim_headers_set(char *headers)
358 size_t i;
359 int has_from = 0;
361 nsign_headers = 1;
363 for (i = 0; headers[i] != '\0'; i++) {
364 /* RFC 5322 field-name */
365 if (!(headers[i] >= 33 && headers[i] <= 126))
366 osmtpd_errx(1, "-h: invalid character");
367 if (headers[i] == ':') {
368 /* Test for empty headers */
369 if (i == 0 || headers[i - 1] == ':')
370 osmtpd_errx(1, "-h: header can't be empty");
371 nsign_headers++;
373 headers[i] = tolower(headers[i]);
375 if (headers[i - 1] == ':')
376 osmtpd_errx(1, "-h: header can't be empty");
378 if ((sign_headers = reallocarray(NULL, nsign_headers + 1,
379 sizeof(*sign_headers))) == NULL)
380 osmtpd_errx(1, "reallocarray");
382 for (i = 0; i < nsign_headers; i++) {
383 sign_headers[i] = headers;
384 if (i != nsign_headers - 1) {
385 headers = strchr(headers, ':');
386 headers++[0] = '\0';
388 if (strcasecmp(sign_headers[i], "from") == 0)
389 has_from = 1;
391 if (!has_from)
392 osmtpd_errx(1, "From header must be included");
395 void
396 dkim_parse_header(struct dkim_message *message, char *line, int force)
398 size_t i;
399 size_t r, w;
400 size_t linelen;
401 size_t lastheader;
402 size_t hlen;
403 int fieldname = 0;
404 char **mtmp;
405 char *htmp;
406 char *tmp;
408 if (addheaders == 2 && !force)
409 dkim_signature_printheader(message, line);
411 if ((line[0] == ' ' || line[0] == '\t') && !message->lastheader)
412 return;
413 if ((line[0] != ' ' && line[0] != '\t')) {
414 message->lastheader = 0;
415 for (i = 0; i < nsign_headers; i++) {
416 hlen = strlen(sign_headers[i]);
417 if (strncasecmp(line, sign_headers[i], hlen) == 0) {
418 while (line[hlen] == ' ' || line[hlen] == '\t')
419 hlen++;
420 if (line[hlen] != ':')
421 continue;
422 break;
425 if (i == nsign_headers && !force)
426 return;
429 if (addheaders == 1 && !force)
430 dkim_signature_printheader(message, line);
432 if (canonheader == CANON_RELAXED) {
433 if (!message->lastheader)
434 fieldname = 1;
435 for (r = w = 0; line[r] != '\0'; r++) {
436 if (line[r] == ':' && fieldname) {
437 if (w > 0 && line[w - 1] == ' ')
438 line[w - 1] = ':';
439 else
440 line[w++] = ':';
441 fieldname = 0;
442 while (line[r + 1] == ' ' ||
443 line[r + 1] == '\t')
444 r++;
445 continue;
447 if (line[r] == ' ' || line[r] == '\t' ||
448 line[r] == '\r' || line[r] == '\n') {
449 if (r != 0 && w != 0 && line[w - 1] == ' ')
450 continue;
451 else
452 line[w++] = ' ';
453 } else if (fieldname) {
454 line[w++] = tolower(line[r]);
455 continue;
456 } else
457 line[w++] = line[r];
459 linelen = (w != 0 && line[w - 1] == ' ') ? w - 1 : w;
460 line[linelen] = '\0';
461 } else
462 linelen = strlen(line);
464 for (lastheader = 0; message->headers[lastheader] != NULL; lastheader++)
465 continue;
466 if (!message->lastheader) {
467 mtmp = recallocarray(message->headers, lastheader + 1,
468 lastheader + 2, sizeof(*mtmp));
469 if (mtmp == NULL)
470 osmtpd_err(1, "reallocarray");
471 message->headers = mtmp;
473 if ((message->headers[lastheader] = strdup(line)) == NULL)
474 osmtpd_err(1, "strdup");
475 message->headers[lastheader + 1 ] = NULL;
476 message->lastheader = 1;
477 } else {
478 lastheader--;
479 linelen += strlen(message->headers[lastheader]);
480 if (canonheader == CANON_SIMPLE)
481 linelen += 2;
482 linelen++;
483 htmp = reallocarray(message->headers[lastheader], linelen,
484 sizeof(*htmp));
485 if (htmp == NULL)
486 osmtpd_err(1, "reallocarray");
487 message->headers[lastheader] = htmp;
488 if (canonheader == CANON_SIMPLE) {
489 (void)strlcat(htmp, "\r\n", linelen);
490 } else if (canonheader == CANON_RELAXED &&
491 (tmp = strchr(message->headers[lastheader], ':')) != NULL &&
492 tmp[1] == '\0')
493 line++;
495 (void)strlcat(htmp, line, linelen);
499 void
500 dkim_parse_body(struct dkim_message *message, char *line)
502 size_t r, w;
503 size_t linelen;
505 if (canonbody == CANON_RELAXED) {
506 for (r = w = 0; line[r] != '\0'; r++) {
507 if (line[r] == ' ' || line[r] == '\t') {
508 if (r != 0 && line[w - 1] == ' ')
509 continue;
510 else
511 line[w++] = ' ';
512 } else
513 line[w++] = line[r];
515 linelen = (w != 0 && line[w - 1] == ' ') ? w - 1 : w;
516 line[linelen] = '\0';
517 } else
518 linelen = strlen(line);
520 if (line[0] == '\0') {
521 message->body_whitelines++;
522 return;
525 while (message->body_whitelines--) {
526 if (EVP_DigestUpdate(message->dctx, "\r\n", 2) == 0)
527 osmtpd_errx(1, "EVP_DigestUpdate");
529 message->body_whitelines = 0;
530 message->has_body = 1;
532 if (EVP_DigestUpdate(message->dctx, line, linelen) == 0 ||
533 EVP_DigestUpdate(message->dctx, "\r\n", 2) == 0)
534 osmtpd_errx(1, "EVP_DigestUpdate");
537 void
538 dkim_sign(struct osmtpd_ctx *ctx)
540 struct dkim_message *message = ctx->local_message;
541 /* Use largest hash size here */
542 unsigned char bdigest[EVP_MAX_MD_SIZE];
543 unsigned char digest[(((sizeof(bdigest) + 2) / 3) * 4) + 1];
544 unsigned char *b;
545 const char *sdomain = domain[0], *tsdomain;
546 time_t now;
547 ssize_t i;
548 size_t linelen = 0;
549 char *tmp, *tmp2;
550 unsigned int digestsz;
552 if (addtime || addexpire)
553 now = time(NULL);
554 if (addtime)
555 dkim_signature_printf(message, "t=%lld; ", (long long)now);
556 if (addexpire != 0)
557 dkim_signature_printf(message, "x=%lld; ",
558 now + addexpire < now ? LLONG_MAX : now + addexpire);
560 if (canonbody == CANON_SIMPLE && !message->has_body) {
561 if (EVP_DigestUpdate(message->dctx, "\r\n", 2) <= 0)
562 osmtpd_errx(1, "EVP_DigestUpdate");
564 if (EVP_DigestFinal_ex(message->dctx, bdigest, &digestsz) == 0)
565 osmtpd_errx(1, "EVP_DigestFinal_ex");
566 EVP_EncodeBlock(digest, bdigest, digestsz);
567 dkim_signature_printf(message, "bh=%s; h=", digest);
568 /* Reverse order for ease of use of RFC6367 section 5.4.2 */
569 for (i = 0; message->headers[i] != NULL; i++)
570 continue;
571 EVP_MD_CTX_reset(message->dctx);
572 if (!sephash) {
573 if (EVP_DigestSignInit(message->dctx, NULL, hash_md, NULL,
574 pkey) != 1)
575 osmtpd_errx(1, "EVP_DigestSignInit");
576 } else {
577 if (EVP_DigestInit_ex(message->dctx, hash_md, NULL) != 1)
578 osmtpd_errx(1, "EVP_DigestInit_ex");
580 for (i--; i >= 0; i--) {
581 if (!sephash) {
582 if (EVP_DigestSignUpdate(message->dctx,
583 message->headers[i],
584 strlen(message->headers[i])) != 1 ||
585 EVP_DigestSignUpdate(message->dctx, "\r\n",
586 2) <= 0)
587 osmtpd_errx(1, "EVP_DigestSignUpdate");
588 } else {
589 if (EVP_DigestUpdate(message->dctx, message->headers[i],
590 strlen(message->headers[i])) != 1 ||
591 EVP_DigestUpdate(message->dctx, "\r\n", 2) <= 0)
592 osmtpd_errx(1, "EVP_DigestSignUpdate");
594 if ((tsdomain = dkim_domain_select(message, message->headers[i])) != NULL)
595 sdomain = tsdomain;
596 /* We're done with the cached header after hashing */
597 for (tmp = message->headers[i]; tmp[0] != ':'; tmp++) {
598 if (tmp[0] == ' ' || tmp[0] == '\t')
599 break;
600 tmp[0] = tolower(tmp[0]);
602 tmp[0] = '\0';
603 dkim_signature_printf(message, "%s%s",
604 message->headers[i + 1] == NULL ? "" : ":",
605 message->headers[i]);
607 dkim_signature_printf(message, "; d=%s; b=", sdomain);
608 dkim_signature_normalize(message);
609 if ((tmp = strdup(message->signature.signature)) == NULL)
610 osmtpd_err(1, "strdup");
611 dkim_parse_header(message, tmp, 1);
612 if (!sephash) {
613 if (EVP_DigestSignUpdate(message->dctx, tmp,
614 strlen(tmp)) != 1)
615 osmtpd_errx(1, "EVP_DigestSignUpdate");
616 } else {
617 if (EVP_DigestUpdate(message->dctx, tmp, strlen(tmp)) != 1)
618 osmtpd_errx(1, "EVP_DigestUpdate");
620 free(tmp);
621 if (!sephash) {
622 if (EVP_DigestSignFinal(message->dctx, NULL, &linelen) != 1)
623 osmtpd_errx(1, "EVP_DigestSignFinal");
624 } else {
625 if (EVP_DigestFinal_ex(message->dctx, bdigest,
626 &digestsz) != 1)
627 osmtpd_errx(1, "EVP_DigestFinal_ex");
628 EVP_MD_CTX_reset(message->dctx);
629 if (EVP_DigestSignInit(message->dctx, NULL, NULL, NULL,
630 pkey) != 1)
631 osmtpd_errx(1, "EVP_DigestSignInit");
632 if (EVP_DigestSign(message->dctx, NULL, &linelen, bdigest,
633 digestsz) != 1)
634 osmtpd_errx(1, "EVP_DigestSign");
636 if ((tmp = malloc(linelen)) == NULL)
637 osmtpd_err(1, "malloc");
638 if (!sephash) {
639 if (EVP_DigestSignFinal(message->dctx, tmp, &linelen) != 1)
640 osmtpd_errx(1, "EVP_DigestSignFinal");
641 } else {
642 if (EVP_DigestSign(message->dctx, tmp, &linelen, bdigest,
643 digestsz) != 1)
644 osmtpd_errx(1, "EVP_DigestSign");
646 if ((b = malloc((((linelen + 2) / 3) * 4) + 1)) == NULL)
647 osmtpd_err(1, "malloc");
648 EVP_EncodeBlock(b, tmp, linelen);
649 free(tmp);
650 dkim_signature_printf(message, "%s\r\n", b);
651 free(b);
652 dkim_signature_normalize(message);
653 tmp = message->signature.signature;
654 while ((tmp2 = strchr(tmp, '\r')) != NULL) {
655 tmp2[0] = '\0';
656 osmtpd_filter_dataline(ctx, "%s", tmp);
657 tmp = tmp2 + 2;
659 tmp = NULL;
660 linelen = 0;
661 rewind(message->origf);
662 while ((i = getline(&tmp, &linelen, message->origf)) != -1) {
663 tmp[i - 1] = '\0';
664 osmtpd_filter_dataline(ctx, "%s", tmp);
666 free(tmp);
667 return;
670 void
671 dkim_signature_normalize(struct dkim_message *message)
673 size_t i;
674 size_t linelen;
675 size_t checkpoint;
676 size_t skip;
677 size_t *headerlen = &(message->signature.len);
678 int headername = 1;
679 char tag = '\0';
680 char *sig = message->signature.signature;
682 for (linelen = i = 0; sig[i] != '\0'; i++) {
683 if (sig[i] == '\r' && sig[i + 1] == '\n') {
684 i++;
685 checkpoint = 0;
686 linelen = 0;
687 continue;
689 if (sig[i] == '\t')
690 linelen = (linelen + 8) & ~7;
691 else
692 linelen++;
693 if (headername) {
694 if (sig[i] == ':') {
695 headername = 0;
696 checkpoint = i;
698 continue;
700 if (linelen > DKIM_SIGNATURE_LINELEN && checkpoint != 0) {
701 for (skip = checkpoint + 1;
702 sig[skip] == ' ' || sig[skip] == '\t';
703 skip++)
704 continue;
705 skip -= checkpoint + 1;
706 dkim_signature_need(message,
707 skip > 3 ? 0 : 3 - skip + 1);
708 sig = message->signature.signature;
710 memmove(sig + checkpoint + 3,
711 sig + checkpoint + skip,
712 *headerlen - skip - checkpoint + 1);
713 sig[checkpoint + 1] = '\r';
714 sig[checkpoint + 2] = '\n';
715 sig[checkpoint + 3] = '\t';
716 linelen = 8;
717 *headerlen = *headerlen + 3 - skip;
718 i = checkpoint + 3;
719 checkpoint = 0;
721 if (sig[i] == ';') {
722 checkpoint = i;
723 tag = '\0';
724 continue;
726 switch (tag) {
727 case 'B':
728 case 'b':
729 case 'z':
730 checkpoint = i;
731 break;
732 case 'h':
733 if (sig[i] == ':')
734 checkpoint = i;
735 break;
737 if (tag == '\0' && sig[i] != ' ' && sig[i] != '\t') {
738 if ((tag = sig[i]) == 'b' && sig[i + 1] == 'h' &&
739 sig[i + 2] == '=') {
740 tag = 'B';
741 linelen += 2;
742 i += 2;
743 } else
744 tag = sig[i];
749 void
750 dkim_signature_printheader(struct dkim_message *message, const char *line)
752 size_t i;
753 int first;
755 first = message->signature.signature[message->signature.len - 1] == '=';
756 for (i = 0; line[i] != '\0'; i++) {
757 if (i == 0 && line[i] != ' ' && line[i] != '\t' && !first)
758 dkim_signature_printf(message, "|");
759 if ((line[i] >= 0x21 && line[i] <= 0x3A) ||
760 (line[i] == 0x3C) ||
761 (line[i] >= 0x3E && line[i] <= 0x7B) ||
762 (line[i] >= 0x7D && line[i] <= 0x7E)) {
763 dkim_signature_printf(message, "%c", line[i]);
764 } else
765 dkim_signature_printf(message, "=%02hhX", line[i]);
767 dkim_signature_printf(message, "=%02hhX=%02hhX", '\r', '\n');
770 void
771 dkim_signature_printf(struct dkim_message *message, char *fmt, ...)
773 struct dkim_signature *sig = &(message->signature);
774 va_list ap;
775 size_t len;
777 va_start(ap, fmt);
778 if ((len = vsnprintf(sig->signature + sig->len, sig->size - sig->len,
779 fmt, ap)) >= sig->size - sig->len) {
780 va_end(ap);
781 dkim_signature_need(message, len + 1);
782 va_start(ap, fmt);
783 if ((len = vsnprintf(sig->signature + sig->len,
784 sig->size - sig->len, fmt, ap)) >= sig->size - sig->len)
785 osmtpd_errx(1, "Miscalculated header size");
787 sig->len += len;
788 va_end(ap);
791 const char *
792 dkim_domain_select(struct dkim_message *message, char *from)
794 char *mdomain0, *mdomain;
795 size_t i;
797 if ((mdomain = mdomain0 = osmtpd_mheader_from_domain(from)) == NULL)
798 return NULL;
800 while (mdomain != NULL && mdomain[0] != '\0') {
801 for (i = 0; i < ndomains; i++) {
802 if (strcasecmp(mdomain, domain[i]) == 0) {
803 free(mdomain0);
804 return domain[i];
807 if ((mdomain = strchr(mdomain, '.')) != NULL)
808 mdomain++;
810 free(mdomain0);
811 return NULL;
814 void
815 dkim_signature_need(struct dkim_message *message, size_t len)
817 struct dkim_signature *sig = &(message->signature);
818 char *tmp;
820 if (sig->len + len < sig->size)
821 return;
822 sig->size = (((len + sig->len) / 512) + 1) * 512;
823 if ((tmp = realloc(sig->signature, sig->size)) == NULL)
824 osmtpd_err(1, "malloc");
825 sig->signature = tmp;
828 __dead void
829 usage(void)
831 fprintf(stderr, "usage: filter-dkimsign [-tz] [-a signalg] "
832 "[-c canonicalization] \n [-h headerfields]"
833 "[-x seconds] -D file -d domain -k keyfile -s selector\n");
834 exit(1);