2 48c4bdc1 2019-04-04 martijn * Copyright (c) 2019 Martijn van Duren <martijn@openbsd.org>
4 48c4bdc1 2019-04-04 martijn * Permission to use, copy, modify, and distribute this software for any
5 48c4bdc1 2019-04-04 martijn * purpose with or without fee is hereby granted, provided that the above
6 48c4bdc1 2019-04-04 martijn * copyright notice and this permission notice appear in all copies.
8 48c4bdc1 2019-04-04 martijn * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 48c4bdc1 2019-04-04 martijn * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 48c4bdc1 2019-04-04 martijn * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 48c4bdc1 2019-04-04 martijn * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 48c4bdc1 2019-04-04 martijn * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 48c4bdc1 2019-04-04 martijn * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 48c4bdc1 2019-04-04 martijn * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 48c4bdc1 2019-04-04 martijn #include <openssl/evp.h>
17 06f6b49b 2019-04-06 martijn #include <openssl/pem.h>
18 48c4bdc1 2019-04-04 martijn #include <openssl/sha.h>
20 48c4bdc1 2019-04-04 martijn #include <ctype.h>
21 c826412d 2019-05-02 martijn #include <errno.h>
22 1308710c 2019-04-09 martijn #include <fcntl.h>
23 48c4bdc1 2019-04-04 martijn #include <stdio.h>
24 48c4bdc1 2019-04-04 martijn #include <stdlib.h>
25 48c4bdc1 2019-04-04 martijn #include <string.h>
26 48c4bdc1 2019-04-04 martijn #include <syslog.h>
27 48c4bdc1 2019-04-04 martijn #include <time.h>
28 48c4bdc1 2019-04-04 martijn #include <unistd.h>
30 6840f1a1 2019-08-22 martijn #include "opensmtpd.h"
32 06f6b49b 2019-04-06 martijn struct dkim_signature {
33 06f6b49b 2019-04-06 martijn char *signature;
34 06f6b49b 2019-04-06 martijn size_t size;
35 06f6b49b 2019-04-06 martijn size_t len;
38 6840f1a1 2019-08-22 martijn struct dkim_message {
39 48c4bdc1 2019-04-04 martijn FILE *origf;
40 48c4bdc1 2019-04-04 martijn int parsing_headers;
41 48c4bdc1 2019-04-04 martijn char **headers;
42 48c4bdc1 2019-04-04 martijn int lastheader;
43 48c4bdc1 2019-04-04 martijn size_t body_whitelines;
44 48c4bdc1 2019-04-04 martijn int has_body;
45 06f6b49b 2019-04-06 martijn struct dkim_signature signature;
47 6b9e2bc6 2019-04-06 martijn EVP_MD_CTX *b;
48 37df18b6 2019-04-06 martijn EVP_MD_CTX *bh;
51 37c6191b 2019-04-06 martijn /* RFC 6376 section 5.4.1 */
52 37c6191b 2019-04-06 martijn static char *dsign_headers[] = {
54 37c6191b 2019-04-06 martijn "reply-to",
55 37c6191b 2019-04-06 martijn "subject",
59 37c6191b 2019-04-06 martijn "resent-date",
60 37c6191b 2019-04-06 martijn "resent-from",
61 37c6191b 2019-04-06 martijn "resent-to",
62 37c6191b 2019-04-06 martijn "resent-cc",
63 37c6191b 2019-04-06 martijn "in-reply-to",
64 37c6191b 2019-04-06 martijn "references",
65 37c6191b 2019-04-06 martijn "list-id",
66 37c6191b 2019-04-06 martijn "list-help",
67 37c6191b 2019-04-06 martijn "list-unsubscribe",
68 37c6191b 2019-04-06 martijn "list-subscribe",
69 37c6191b 2019-04-06 martijn "list-post",
70 37c6191b 2019-04-06 martijn "list-owner",
71 37c6191b 2019-04-06 martijn "list-archive"
73 37c6191b 2019-04-06 martijn static char **sign_headers = dsign_headers;
74 37c6191b 2019-04-06 martijn static size_t nsign_headers = sizeof(dsign_headers) / sizeof(*dsign_headers);
76 fddd7e1e 2019-04-06 martijn static char *hashalg = "sha256";
77 fddd7e1e 2019-04-06 martijn static char *cryptalg = "rsa";
79 48c4bdc1 2019-04-04 martijn #define CANON_SIMPLE 0
80 48c4bdc1 2019-04-04 martijn #define CANON_RELAXED 1
81 48c4bdc1 2019-04-04 martijn static int canonheader = CANON_SIMPLE;
82 48c4bdc1 2019-04-04 martijn static int canonbody = CANON_SIMPLE;
84 e7076cef 2019-05-02 martijn static int addtime = 0;
85 e974922c 2019-05-02 martijn static long long addexpire = 0;
86 c826412d 2019-05-02 martijn static int addheaders = 0;
88 48c4bdc1 2019-04-04 martijn static char *domain = NULL;
89 06f6b49b 2019-04-06 martijn static char *selector = NULL;
91 06f6b49b 2019-04-06 martijn static EVP_PKEY *pkey;
92 06f6b49b 2019-04-06 martijn static const EVP_MD *hash_md;
94 06f6b49b 2019-04-06 martijn #define DKIM_SIGNATURE_LINELEN 78
96 48c4bdc1 2019-04-04 martijn void usage(void);
97 6840f1a1 2019-08-22 martijn void dkim_err(struct dkim_message *, char *);
98 6840f1a1 2019-08-22 martijn void dkim_errx(struct dkim_message *, char *);
99 48c4bdc1 2019-04-04 martijn void dkim_headers_set(char *);
100 6840f1a1 2019-08-22 martijn void dkim_dataline(struct osmtpd_ctx *, const char *);
101 6840f1a1 2019-08-22 martijn void dkim_commit(struct osmtpd_ctx *);
102 6840f1a1 2019-08-22 martijn void *dkim_message_new(struct osmtpd_ctx *);
103 6840f1a1 2019-08-22 martijn void dkim_message_free(struct osmtpd_ctx *, void *);
104 6840f1a1 2019-08-22 martijn void dkim_parse_header(struct dkim_message *, char *, int);
105 6840f1a1 2019-08-22 martijn void dkim_parse_body(struct dkim_message *, char *);
106 6840f1a1 2019-08-22 martijn void dkim_sign(struct osmtpd_ctx *);
107 6840f1a1 2019-08-22 martijn int dkim_signature_printheader(struct dkim_message *, const char *);
108 6840f1a1 2019-08-22 martijn int dkim_signature_printf(struct dkim_message *, char *, ...)
109 06f6b49b 2019-04-06 martijn __attribute__((__format__ (printf, 2, 3)));
110 6840f1a1 2019-08-22 martijn int dkim_signature_normalize(struct dkim_message *);
111 6840f1a1 2019-08-22 martijn int dkim_signature_need(struct dkim_message *, size_t);
112 6840f1a1 2019-08-22 martijn int dkim_sign_init(struct dkim_message *);
115 48c4bdc1 2019-04-04 martijn main(int argc, char *argv[])
118 06f6b49b 2019-04-06 martijn FILE *keyfile;
119 e974922c 2019-05-02 martijn const char *errstr;
121 6840f1a1 2019-08-22 martijn while ((ch = getopt(argc, argv, "a:c:d:h:k:s:tx:z")) != -1) {
122 48c4bdc1 2019-04-04 martijn switch (ch) {
123 48c4bdc1 2019-04-04 martijn case 'a':
124 fddd7e1e 2019-04-06 martijn if (strncmp(optarg, "rsa-", 4) != 0)
125 6840f1a1 2019-08-22 martijn osmtpd_err(1, "invalid algorithm");
126 fddd7e1e 2019-04-06 martijn hashalg = optarg + 4;
128 48c4bdc1 2019-04-04 martijn case 'c':
129 48c4bdc1 2019-04-04 martijn if (strncmp(optarg, "simple", 6) == 0) {
130 48c4bdc1 2019-04-04 martijn canonheader = CANON_SIMPLE;
131 48c4bdc1 2019-04-04 martijn optarg += 6;
132 48c4bdc1 2019-04-04 martijn } else if (strncmp(optarg, "relaxed", 7) == 0) {
133 48c4bdc1 2019-04-04 martijn canonheader = CANON_RELAXED;
134 48c4bdc1 2019-04-04 martijn optarg += 7;
136 6840f1a1 2019-08-22 martijn osmtpd_err(1, "Invalid canonicalization");
137 48c4bdc1 2019-04-04 martijn if (optarg[0] == '/') {
138 48c4bdc1 2019-04-04 martijn if (strcmp(optarg + 1, "simple") == 0)
139 48c4bdc1 2019-04-04 martijn canonbody = CANON_SIMPLE;
140 48c4bdc1 2019-04-04 martijn else if (strcmp(optarg + 1, "relaxed") == 0)
141 48c4bdc1 2019-04-04 martijn canonbody = CANON_RELAXED;
143 6840f1a1 2019-08-22 martijn osmtpd_err(1,
144 6840f1a1 2019-08-22 martijn "Invalid canonicalization");
145 48c4bdc1 2019-04-04 martijn } else if (optarg[0] == '\0')
146 48c4bdc1 2019-04-04 martijn canonbody = CANON_SIMPLE;
148 6840f1a1 2019-08-22 martijn osmtpd_err(1, "Invalid canonicalization");
150 48c4bdc1 2019-04-04 martijn case 'd':
151 48c4bdc1 2019-04-04 martijn domain = optarg;
153 06f6b49b 2019-04-06 martijn case 'h':
154 06f6b49b 2019-04-06 martijn dkim_headers_set(optarg);
156 06f6b49b 2019-04-06 martijn case 'k':
157 06f6b49b 2019-04-06 martijn if ((keyfile = fopen(optarg, "r")) == NULL)
158 6840f1a1 2019-08-22 martijn osmtpd_err(1, "Can't open key file (%s)",
159 6840f1a1 2019-08-22 martijn optarg);
160 06f6b49b 2019-04-06 martijn pkey = PEM_read_PrivateKey(keyfile, NULL, NULL, NULL);
161 06f6b49b 2019-04-06 martijn if (pkey == NULL)
162 6840f1a1 2019-08-22 martijn osmtpd_errx(1, "Can't read key file");
163 06f6b49b 2019-04-06 martijn if (EVP_PKEY_get0_RSA(pkey) == NULL)
164 6840f1a1 2019-08-22 martijn osmtpd_err(1, "Key is not of type rsa");
165 06f6b49b 2019-04-06 martijn fclose(keyfile);
167 06f6b49b 2019-04-06 martijn case 's':
168 06f6b49b 2019-04-06 martijn selector = optarg;
170 e7076cef 2019-05-02 martijn case 't':
171 e7076cef 2019-05-02 martijn addtime = 1;
173 e974922c 2019-05-02 martijn case 'x':
174 e974922c 2019-05-02 martijn addexpire = strtonum(optarg, 1, INT64_MAX, &errstr);
175 e974922c 2019-05-02 martijn if (addexpire == 0)
176 6840f1a1 2019-08-22 martijn osmtpd_errx(1, "Expire offset is %s", errstr);
178 c826412d 2019-05-02 martijn case 'z':
179 6840f1a1 2019-08-22 martijn addheaders++;
181 48c4bdc1 2019-04-04 martijn default:
182 48c4bdc1 2019-04-04 martijn usage();
186 fddd7e1e 2019-04-06 martijn OpenSSL_add_all_digests();
187 fddd7e1e 2019-04-06 martijn if ((hash_md = EVP_get_digestbyname(hashalg)) == NULL)
188 6840f1a1 2019-08-22 martijn osmtpd_errx(1, "Can't find hash: %s", hashalg);
190 6840f1a1 2019-08-22 martijn if (pledge("tmppath stdio", NULL) == -1)
191 6840f1a1 2019-08-22 martijn osmtpd_err(1, "pledge");
193 06f6b49b 2019-04-06 martijn if (domain == NULL || selector == NULL || pkey == NULL)
194 48c4bdc1 2019-04-04 martijn usage();
196 6840f1a1 2019-08-22 martijn osmtpd_register_filter_dataline(dkim_dataline);
197 6840f1a1 2019-08-22 martijn osmtpd_register_filter_commit(dkim_commit);
198 6840f1a1 2019-08-22 martijn osmtpd_local_message(dkim_message_new, dkim_message_free);
199 6840f1a1 2019-08-22 martijn osmtpd_run();
201 48c4bdc1 2019-04-04 martijn return 0;
205 6840f1a1 2019-08-22 martijn dkim_dataline(struct osmtpd_ctx *ctx, const char *line)
207 6840f1a1 2019-08-22 martijn struct dkim_message *message = ctx->local_message;
208 6840f1a1 2019-08-22 martijn char *linedup;
209 48c4bdc1 2019-04-04 martijn size_t linelen;
211 6840f1a1 2019-08-22 martijn if (message->err)
214 48c4bdc1 2019-04-04 martijn linelen = strlen(line);
215 6840f1a1 2019-08-22 martijn if (fprintf(message->origf, "%s\n", line) < (int) linelen)
216 6840f1a1 2019-08-22 martijn dkim_err(message, "Couldn't write to tempfile");
218 7ecf4491 2019-05-01 martijn if (line[0] == '.' && line[1] =='\0') {
219 6840f1a1 2019-08-22 martijn dkim_sign(ctx);
220 6840f1a1 2019-08-22 martijn } else if (linelen != 0 && message->parsing_headers) {
221 f5286ea3 2019-04-08 martijn if (line[0] == '.')
223 6840f1a1 2019-08-22 martijn if ((linedup = strdup(line)) == NULL)
224 6840f1a1 2019-08-22 martijn osmtpd_err(1, "strdup");
225 6840f1a1 2019-08-22 martijn dkim_parse_header(message, linedup, 0);
226 6840f1a1 2019-08-22 martijn free(linedup);
227 6840f1a1 2019-08-22 martijn } else if (linelen == 0 && message->parsing_headers) {
228 6840f1a1 2019-08-22 martijn if (addheaders > 0 && !dkim_signature_printf(message, "; "))
230 6840f1a1 2019-08-22 martijn message->parsing_headers = 0;
231 f5286ea3 2019-04-08 martijn } else {
232 f5286ea3 2019-04-08 martijn if (line[0] == '.')
234 6840f1a1 2019-08-22 martijn if ((linedup = strdup(line)) == NULL)
235 6840f1a1 2019-08-22 martijn osmtpd_err(1, "strdup");
236 6840f1a1 2019-08-22 martijn dkim_parse_body(message, linedup);
237 6840f1a1 2019-08-22 martijn free(linedup);
242 6840f1a1 2019-08-22 martijn dkim_commit(struct osmtpd_ctx *ctx)
244 6840f1a1 2019-08-22 martijn struct dkim_message *message = ctx->local_message;
246 6840f1a1 2019-08-22 martijn if (message->err)
247 6840f1a1 2019-08-22 martijn osmtpd_filter_disconnect(ctx, "Internal server error");
249 6840f1a1 2019-08-22 martijn osmtpd_filter_proceed(ctx);
253 6840f1a1 2019-08-22 martijn dkim_message_new(struct osmtpd_ctx *ctx)
255 6840f1a1 2019-08-22 martijn struct dkim_message *message;
257 6840f1a1 2019-08-22 martijn if ((message = calloc(1, sizeof(*message))) == NULL)
258 6840f1a1 2019-08-22 martijn osmtpd_err(1, NULL);
260 6840f1a1 2019-08-22 martijn if ((message->origf = tmpfile()) == NULL) {
261 6840f1a1 2019-08-22 martijn dkim_err(message, "Can't open tempfile");
262 48c4bdc1 2019-04-04 martijn return NULL;
264 6840f1a1 2019-08-22 martijn message->parsing_headers = 1;
266 6840f1a1 2019-08-22 martijn message->body_whitelines = 0;
267 6840f1a1 2019-08-22 martijn message->headers = calloc(1, sizeof(*(message->headers)));
268 6840f1a1 2019-08-22 martijn if (message->headers == NULL) {
269 6840f1a1 2019-08-22 martijn dkim_err(message, "Can't save headers");
270 48c4bdc1 2019-04-04 martijn return NULL;
272 6840f1a1 2019-08-22 martijn message->lastheader = 0;
273 6840f1a1 2019-08-22 martijn message->signature.signature = NULL;
274 6840f1a1 2019-08-22 martijn message->signature.size = 0;
275 6840f1a1 2019-08-22 martijn message->signature.len = 0;
276 6840f1a1 2019-08-22 martijn message->err = 0;
278 6840f1a1 2019-08-22 martijn if (!dkim_signature_printf(message,
279 3d3c8040 2019-09-17 martijn "DKIM-Signature: v=%s; a=%s-%s; c=%s/%s; d=%s; s=%s; ", "1",
280 fa10b9e8 2019-04-06 martijn cryptalg, hashalg,
281 06f6b49b 2019-04-06 martijn canonheader == CANON_SIMPLE ? "simple" : "relaxed",
282 06f6b49b 2019-04-06 martijn canonbody == CANON_SIMPLE ? "simple" : "relaxed",
283 06f6b49b 2019-04-06 martijn domain, selector))
284 06f6b49b 2019-04-06 martijn return NULL;
285 6840f1a1 2019-08-22 martijn if (addheaders > 0 && !dkim_signature_printf(message, "z="))
286 c826412d 2019-05-02 martijn return NULL;
288 6840f1a1 2019-08-22 martijn if ((message->b = EVP_MD_CTX_new()) == NULL ||
289 6840f1a1 2019-08-22 martijn (message->bh = EVP_MD_CTX_new()) == NULL) {
290 6840f1a1 2019-08-22 martijn dkim_errx(message, "Can't create hash context");
291 06f6b49b 2019-04-06 martijn return NULL;
293 6840f1a1 2019-08-22 martijn if (EVP_DigestSignInit(message->b, NULL, hash_md, NULL, pkey) <= 0 ||
294 6840f1a1 2019-08-22 martijn EVP_DigestInit_ex(message->bh, hash_md, NULL) == 0) {
295 6840f1a1 2019-08-22 martijn dkim_errx(message, "Failed to initialize hash context");
296 37df18b6 2019-04-06 martijn return NULL;
298 6840f1a1 2019-08-22 martijn return message;
302 6840f1a1 2019-08-22 martijn dkim_message_free(struct osmtpd_ctx *ctx, void *data)
304 6840f1a1 2019-08-22 martijn struct dkim_message *message = data;
305 48c4bdc1 2019-04-04 martijn size_t i;
307 6840f1a1 2019-08-22 martijn fclose(message->origf);
308 6840f1a1 2019-08-22 martijn EVP_MD_CTX_free(message->b);
309 6840f1a1 2019-08-22 martijn EVP_MD_CTX_free(message->bh);
310 6840f1a1 2019-08-22 martijn free(message->signature.signature);
311 6840f1a1 2019-08-22 martijn for (i = 0; message->headers[i] != NULL; i++)
312 6840f1a1 2019-08-22 martijn free(message->headers[i]);
313 6840f1a1 2019-08-22 martijn free(message->headers);
314 6840f1a1 2019-08-22 martijn free(message);
318 48c4bdc1 2019-04-04 martijn dkim_headers_set(char *headers)
320 48c4bdc1 2019-04-04 martijn size_t i;
321 48c4bdc1 2019-04-04 martijn int has_from = 0;
323 48c4bdc1 2019-04-04 martijn nsign_headers = 1;
325 48c4bdc1 2019-04-04 martijn for (i = 0; headers[i] != '\0'; i++) {
326 48c4bdc1 2019-04-04 martijn /* RFC 5322 field-name */
327 48c4bdc1 2019-04-04 martijn if (!(headers[i] >= 33 && headers[i] <= 126))
328 6840f1a1 2019-08-22 martijn osmtpd_errx(1, "-h: invalid character");
329 48c4bdc1 2019-04-04 martijn if (headers[i] == ':') {
330 48c4bdc1 2019-04-04 martijn /* Test for empty headers */
331 48c4bdc1 2019-04-04 martijn if (i == 0 || headers[i - 1] == ':')
332 6840f1a1 2019-08-22 martijn osmtpd_errx(1, "-h: header can't be empty");
333 48c4bdc1 2019-04-04 martijn nsign_headers++;
335 48c4bdc1 2019-04-04 martijn headers[i] = tolower(headers[i]);
337 48c4bdc1 2019-04-04 martijn if (headers[i - 1] == ':')
338 6840f1a1 2019-08-22 martijn osmtpd_errx(1, "-h: header can't be empty");
340 6840f1a1 2019-08-22 martijn if ((sign_headers = reallocarray(NULL, nsign_headers + 1,
341 6840f1a1 2019-08-22 martijn sizeof(*sign_headers))) == NULL)
342 6840f1a1 2019-08-22 martijn osmtpd_errx(1, NULL);
344 48c4bdc1 2019-04-04 martijn for (i = 0; i < nsign_headers; i++) {
345 48c4bdc1 2019-04-04 martijn sign_headers[i] = headers;
346 48c4bdc1 2019-04-04 martijn if (i != nsign_headers - 1) {
347 48c4bdc1 2019-04-04 martijn headers = strchr(headers, ':');
348 48c4bdc1 2019-04-04 martijn headers++[0] = '\0';
350 48c4bdc1 2019-04-04 martijn if (strcasecmp(sign_headers[i], "from") == 0)
351 48c4bdc1 2019-04-04 martijn has_from = 1;
353 48c4bdc1 2019-04-04 martijn if (!has_from)
354 6840f1a1 2019-08-22 martijn osmtpd_errx(1, "From header must be included");
358 6840f1a1 2019-08-22 martijn dkim_err(struct dkim_message *message, char *msg)
360 6840f1a1 2019-08-22 martijn message->err = 1;
361 6840f1a1 2019-08-22 martijn fprintf(stderr, "%s\n", msg);
365 6840f1a1 2019-08-22 martijn dkim_errx(struct dkim_message *message, char *msg)
367 6840f1a1 2019-08-22 martijn message->err = 1;
368 6840f1a1 2019-08-22 martijn fprintf(stderr, "%s\n", msg);
372 6840f1a1 2019-08-22 martijn dkim_parse_header(struct dkim_message *message, char *line, int force)
374 48c4bdc1 2019-04-04 martijn size_t i;
375 48c4bdc1 2019-04-04 martijn size_t r, w;
376 48c4bdc1 2019-04-04 martijn size_t linelen;
377 48c4bdc1 2019-04-04 martijn size_t lastheader;
378 06f6b49b 2019-04-06 martijn size_t hlen;
379 b6e37a5e 2019-04-08 martijn int fieldname = 0;
380 48c4bdc1 2019-04-04 martijn char **mtmp;
381 48c4bdc1 2019-04-04 martijn char *htmp;
382 63ac36ce 2019-04-08 martijn char *tmp;
384 c826412d 2019-05-02 martijn if (addheaders == 2 && !force &&
385 6840f1a1 2019-08-22 martijn !dkim_signature_printheader(message, line))
388 6840f1a1 2019-08-22 martijn if ((line[0] == ' ' || line[0] == '\t') && !message->lastheader)
390 48c4bdc1 2019-04-04 martijn if ((line[0] != ' ' && line[0] != '\t')) {
391 6840f1a1 2019-08-22 martijn message->lastheader = 0;
392 48c4bdc1 2019-04-04 martijn for (i = 0; i < nsign_headers; i++) {
393 06f6b49b 2019-04-06 martijn hlen = strlen(sign_headers[i]);
394 06f6b49b 2019-04-06 martijn if (strncasecmp(line, sign_headers[i], hlen) == 0) {
395 06f6b49b 2019-04-06 martijn while (line[hlen] == ' ' || line[hlen] == '\t')
397 06f6b49b 2019-04-06 martijn if (line[hlen] != ':')
398 06f6b49b 2019-04-06 martijn continue;
402 06f6b49b 2019-04-06 martijn if (i == nsign_headers && !force)
406 c826412d 2019-05-02 martijn if (addheaders == 1 && !force &&
407 6840f1a1 2019-08-22 martijn !dkim_signature_printheader(message, line))
410 48c4bdc1 2019-04-04 martijn if (canonheader == CANON_RELAXED) {
411 6840f1a1 2019-08-22 martijn if (!message->lastheader)
412 b6e37a5e 2019-04-08 martijn fieldname = 1;
413 48c4bdc1 2019-04-04 martijn for (r = w = 0; line[r] != '\0'; r++) {
414 5d06ccad 2019-04-08 martijn if (line[r] == ':' && fieldname) {
415 cd6f9b03 2019-10-23 martijn if (w > 0 && line[w - 1] == ' ')
416 48c4bdc1 2019-04-04 martijn line[w - 1] = ':';
418 48c4bdc1 2019-04-04 martijn line[w++] = ':';
419 48c4bdc1 2019-04-04 martijn fieldname = 0;
420 48c4bdc1 2019-04-04 martijn while (line[r + 1] == ' ' ||
421 48c4bdc1 2019-04-04 martijn line[r + 1] == '\t')
423 48c4bdc1 2019-04-04 martijn continue;
425 06f6b49b 2019-04-06 martijn if (line[r] == ' ' || line[r] == '\t' ||
426 06f6b49b 2019-04-06 martijn line[r] == '\r' || line[r] == '\n') {
427 cd6f9b03 2019-10-23 martijn if (r != 0 && w != 0 && line[w - 1] == ' ')
428 48c4bdc1 2019-04-04 martijn continue;
430 48c4bdc1 2019-04-04 martijn line[w++] = ' ';
431 48c4bdc1 2019-04-04 martijn } else if (fieldname) {
432 48c4bdc1 2019-04-04 martijn line[w++] = tolower(line[r]);
433 48c4bdc1 2019-04-04 martijn continue;
435 48c4bdc1 2019-04-04 martijn line[w++] = line[r];
437 cd6f9b03 2019-10-23 martijn linelen = (w != 0 && line[w - 1] == ' ') ? w - 1 : w;
438 48c4bdc1 2019-04-04 martijn line[linelen] = '\0';
440 48c4bdc1 2019-04-04 martijn linelen = strlen(line);
442 6840f1a1 2019-08-22 martijn for (lastheader = 0; message->headers[lastheader] != NULL; lastheader++)
443 48c4bdc1 2019-04-04 martijn continue;
444 6840f1a1 2019-08-22 martijn if (!message->lastheader) {
445 6840f1a1 2019-08-22 martijn mtmp = recallocarray(message->headers, lastheader + 1,
446 a53d74c4 2019-04-08 martijn lastheader + 2, sizeof(*mtmp));
447 48c4bdc1 2019-04-04 martijn if (mtmp == NULL) {
448 6840f1a1 2019-08-22 martijn dkim_err(message, "Can't store header");
451 6840f1a1 2019-08-22 martijn message->headers = mtmp;
453 6840f1a1 2019-08-22 martijn message->headers[lastheader] = strdup(line);
454 6840f1a1 2019-08-22 martijn message->headers[lastheader + 1 ] = NULL;
455 6840f1a1 2019-08-22 martijn message->lastheader = 1;
456 48c4bdc1 2019-04-04 martijn } else {
457 48c4bdc1 2019-04-04 martijn lastheader--;
458 6840f1a1 2019-08-22 martijn linelen += strlen(message->headers[lastheader]);
459 48c4bdc1 2019-04-04 martijn if (canonheader == CANON_SIMPLE)
460 48c4bdc1 2019-04-04 martijn linelen += 2;
461 48c4bdc1 2019-04-04 martijn linelen++;
462 6840f1a1 2019-08-22 martijn htmp = reallocarray(message->headers[lastheader], linelen,
463 48c4bdc1 2019-04-04 martijn sizeof(*htmp));
464 48c4bdc1 2019-04-04 martijn if (htmp == NULL) {
465 6840f1a1 2019-08-22 martijn dkim_err(message, "Can't store header");
468 6840f1a1 2019-08-22 martijn message->headers[lastheader] = htmp;
469 48c4bdc1 2019-04-04 martijn if (canonheader == CANON_SIMPLE) {
470 48c4bdc1 2019-04-04 martijn if (strlcat(htmp, "\r\n", linelen) >= linelen)
471 6840f1a1 2019-08-22 martijn osmtpd_errx(1, "Missized header");
472 63ac36ce 2019-04-08 martijn } else if (canonheader == CANON_RELAXED &&
473 6840f1a1 2019-08-22 martijn (tmp = strchr(message->headers[lastheader], ':')) != NULL &&
474 63ac36ce 2019-04-08 martijn tmp[1] == '\0')
477 48c4bdc1 2019-04-04 martijn if (strlcat(htmp, line, linelen) >= linelen)
478 6840f1a1 2019-08-22 martijn osmtpd_errx(1, "Missized header");
483 6840f1a1 2019-08-22 martijn dkim_parse_body(struct dkim_message *message, char *line)
485 48c4bdc1 2019-04-04 martijn size_t r, w;
486 48c4bdc1 2019-04-04 martijn size_t linelen;
488 1308710c 2019-04-09 martijn if (canonbody == CANON_RELAXED) {
489 1308710c 2019-04-09 martijn for (r = w = 0; line[r] != '\0'; r++) {
490 1308710c 2019-04-09 martijn if (line[r] == ' ' || line[r] == '\t') {
491 1308710c 2019-04-09 martijn if (r != 0 && line[w - 1] == ' ')
492 1308710c 2019-04-09 martijn continue;
494 1308710c 2019-04-09 martijn line[w++] = ' ';
496 1308710c 2019-04-09 martijn line[w++] = line[r];
498 cd6f9b03 2019-10-23 martijn linelen = (w != 0 && line[w - 1] == ' ') ? w - 1 : w;
499 1308710c 2019-04-09 martijn line[linelen] = '\0';
501 1308710c 2019-04-09 martijn linelen = strlen(line);
503 48c4bdc1 2019-04-04 martijn if (line[0] == '\0') {
504 6840f1a1 2019-08-22 martijn message->body_whitelines++;
508 6840f1a1 2019-08-22 martijn while (message->body_whitelines--) {
509 6840f1a1 2019-08-22 martijn if (EVP_DigestUpdate(message->bh, "\r\n", 2) == 0) {
510 6840f1a1 2019-08-22 martijn dkim_err(message, "Can't update hash context");
514 6840f1a1 2019-08-22 martijn message->body_whitelines = 0;
515 6840f1a1 2019-08-22 martijn message->has_body = 1;
517 6840f1a1 2019-08-22 martijn if (EVP_DigestUpdate(message->bh, line, linelen) == 0 ||
518 6840f1a1 2019-08-22 martijn EVP_DigestUpdate(message->bh, "\r\n", 2) == 0) {
519 6840f1a1 2019-08-22 martijn dkim_err(message, "Can't update hash context");
525 6840f1a1 2019-08-22 martijn dkim_sign(struct osmtpd_ctx *ctx)
527 6840f1a1 2019-08-22 martijn struct dkim_message *message = ctx->local_message;
528 365862b1 2019-05-02 martijn /* Use largest hash size here */
529 365862b1 2019-05-02 martijn char bbh[EVP_MAX_MD_SIZE];
530 365862b1 2019-05-02 martijn char bh[(((sizeof(bbh) + 2) / 3) * 4) + 1];
531 365862b1 2019-05-02 martijn char *b;
532 e974922c 2019-05-02 martijn time_t now;
533 6840f1a1 2019-08-22 martijn ssize_t i;
534 365862b1 2019-05-02 martijn size_t linelen;
535 365862b1 2019-05-02 martijn char *tmp, *tmp2;
537 e974922c 2019-05-02 martijn if (addtime || addexpire)
538 e974922c 2019-05-02 martijn now = time(NULL);
539 6840f1a1 2019-08-22 martijn if (addtime && !dkim_signature_printf(message, "t=%lld; ", now))
541 6840f1a1 2019-08-22 martijn if (addexpire != 0 && !dkim_signature_printf(message, "x=%lld; ",
542 e974922c 2019-05-02 martijn now + addexpire < now ? INT64_MAX : now + addexpire))
545 6840f1a1 2019-08-22 martijn if (canonbody == CANON_SIMPLE && !message->has_body) {
546 6840f1a1 2019-08-22 martijn if (EVP_DigestUpdate(message->bh, "\r\n", 2) <= 0) {
547 6840f1a1 2019-08-22 martijn dkim_err(message, "Can't update hash context");
551 6840f1a1 2019-08-22 martijn if (EVP_DigestFinal_ex(message->bh, bbh, NULL) == 0) {
552 6840f1a1 2019-08-22 martijn dkim_err(message, "Can't finalize hash context");
555 6840f1a1 2019-08-22 martijn EVP_EncodeBlock(bh, bbh, EVP_MD_CTX_size(message->bh));
556 6840f1a1 2019-08-22 martijn if (!dkim_signature_printf(message, "bh=%s; h=", bh))
558 365862b1 2019-05-02 martijn /* Reverse order for ease of use of RFC6367 section 5.4.2 */
559 6840f1a1 2019-08-22 martijn for (i = 0; message->headers[i] != NULL; i++)
560 365862b1 2019-05-02 martijn continue;
561 365862b1 2019-05-02 martijn for (i--; i >= 0; i--) {
562 6840f1a1 2019-08-22 martijn if (EVP_DigestSignUpdate(message->b,
563 6840f1a1 2019-08-22 martijn message->headers[i],
564 6840f1a1 2019-08-22 martijn strlen(message->headers[i])) <= 0 ||
565 6840f1a1 2019-08-22 martijn EVP_DigestSignUpdate(message->b, "\r\n", 2) <= 0) {
566 6840f1a1 2019-08-22 martijn dkim_errx(message, "Failed to update digest context");
569 365862b1 2019-05-02 martijn /* We're done with the cached header after hashing */
570 6840f1a1 2019-08-22 martijn for (tmp = message->headers[i]; tmp[0] != ':'; tmp++) {
571 365862b1 2019-05-02 martijn if (tmp[0] == ' ' || tmp[0] == '\t')
573 365862b1 2019-05-02 martijn tmp[0] = tolower(tmp[0]);
575 365862b1 2019-05-02 martijn tmp[0] = '\0';
576 6840f1a1 2019-08-22 martijn if (!dkim_signature_printf(message, "%s%s",
577 6840f1a1 2019-08-22 martijn message->headers[i + 1] == NULL ? "" : ":",
578 6840f1a1 2019-08-22 martijn message->headers[i]))
581 6840f1a1 2019-08-22 martijn dkim_signature_printf(message, "; b=");
582 6840f1a1 2019-08-22 martijn if (!dkim_signature_normalize(message))
584 6840f1a1 2019-08-22 martijn if ((tmp = strdup(message->signature.signature)) == NULL) {
585 6840f1a1 2019-08-22 martijn dkim_err(message, "Can't create DKIM signature");
588 6840f1a1 2019-08-22 martijn dkim_parse_header(message, tmp, 1);
589 6840f1a1 2019-08-22 martijn if (EVP_DigestSignUpdate(message->b, tmp, strlen(tmp)) <= 0) {
590 6840f1a1 2019-08-22 martijn dkim_err(message, "Failed to update digest context");
593 365862b1 2019-05-02 martijn free(tmp);
594 6840f1a1 2019-08-22 martijn if (EVP_DigestSignFinal(message->b, NULL, &linelen) <= 0) {
595 6840f1a1 2019-08-22 martijn dkim_err(message, "Failed to finalize digest");
598 365862b1 2019-05-02 martijn if ((tmp = malloc(linelen)) == NULL) {
599 6840f1a1 2019-08-22 martijn dkim_err(message, "Can't allocate space for digest");
602 6840f1a1 2019-08-22 martijn if (EVP_DigestSignFinal(message->b, tmp, &linelen) <= 0) {
603 6840f1a1 2019-08-22 martijn dkim_err(message, "Failed to finalize digest");
606 365862b1 2019-05-02 martijn if ((b = malloc((((linelen + 2) / 3) * 4) + 1)) == NULL) {
607 6840f1a1 2019-08-22 martijn dkim_err(message, "Can't create DKIM signature");
610 365862b1 2019-05-02 martijn EVP_EncodeBlock(b, tmp, linelen);
611 365862b1 2019-05-02 martijn free(tmp);
612 6840f1a1 2019-08-22 martijn dkim_signature_printf(message, "%s\r\n", b);
613 365862b1 2019-05-02 martijn free(b);
614 6840f1a1 2019-08-22 martijn dkim_signature_normalize(message);
615 6840f1a1 2019-08-22 martijn tmp = message->signature.signature;
616 365862b1 2019-05-02 martijn while ((tmp2 = strchr(tmp, '\r')) != NULL) {
617 365862b1 2019-05-02 martijn tmp2[0] = '\0';
618 6840f1a1 2019-08-22 martijn osmtpd_filter_dataline(ctx, "%s", tmp);
619 365862b1 2019-05-02 martijn tmp = tmp2 + 2;
621 365862b1 2019-05-02 martijn tmp = NULL;
622 365862b1 2019-05-02 martijn linelen = 0;
623 6840f1a1 2019-08-22 martijn rewind(message->origf);
624 6840f1a1 2019-08-22 martijn while ((i = getline(&tmp, &linelen, message->origf)) != -1) {
625 365862b1 2019-05-02 martijn tmp[i - 1] = '\0';
626 6840f1a1 2019-08-22 martijn osmtpd_filter_dataline(ctx, "%s", tmp);
631 6840f1a1 2019-08-22 martijn dkim_signature_normalize(struct dkim_message *message)
633 06f6b49b 2019-04-06 martijn size_t i;
634 06f6b49b 2019-04-06 martijn size_t linelen;
635 06f6b49b 2019-04-06 martijn size_t checkpoint;
636 06f6b49b 2019-04-06 martijn size_t skip;
637 6840f1a1 2019-08-22 martijn size_t *headerlen = &(message->signature.len);
638 06f6b49b 2019-04-06 martijn int headername = 1;
639 06f6b49b 2019-04-06 martijn char tag = '\0';
640 6840f1a1 2019-08-22 martijn char *sig = message->signature.signature;
642 06f6b49b 2019-04-06 martijn for (linelen = i = 0; sig[i] != '\0'; i++) {
643 ffa37923 2019-04-09 martijn if (sig[i] == '\r' && sig[i + 1] == '\n') {
645 06f6b49b 2019-04-06 martijn checkpoint = 0;
646 06f6b49b 2019-04-06 martijn linelen = 0;
647 06f6b49b 2019-04-06 martijn continue;
649 06f6b49b 2019-04-06 martijn if (sig[i] == '\t')
650 06f6b49b 2019-04-06 martijn linelen = (linelen + 8) & ~7;
652 06f6b49b 2019-04-06 martijn linelen++;
653 06f6b49b 2019-04-06 martijn if (headername) {
654 06f6b49b 2019-04-06 martijn if (sig[i] == ':') {
655 06f6b49b 2019-04-06 martijn headername = 0;
656 06f6b49b 2019-04-06 martijn checkpoint = i;
658 06f6b49b 2019-04-06 martijn continue;
660 ffa37923 2019-04-09 martijn if (linelen > DKIM_SIGNATURE_LINELEN && checkpoint != 0) {
661 06f6b49b 2019-04-06 martijn for (skip = checkpoint + 1;
662 06f6b49b 2019-04-06 martijn sig[skip] == ' ' || sig[skip] == '\t';
664 06f6b49b 2019-04-06 martijn continue;
665 06f6b49b 2019-04-06 martijn skip -= checkpoint + 1;
666 6840f1a1 2019-08-22 martijn if (!dkim_signature_need(message,
667 226e5da8 2019-04-08 martijn skip > 3 ? 0 : 3 - skip + 1))
668 06f6b49b 2019-04-06 martijn return 0;
669 6840f1a1 2019-08-22 martijn sig = message->signature.signature;
671 06f6b49b 2019-04-06 martijn memmove(sig + checkpoint + 3,
672 06f6b49b 2019-04-06 martijn sig + checkpoint + skip,
673 540f552a 2019-04-06 martijn *headerlen - skip - checkpoint + 1);
674 06f6b49b 2019-04-06 martijn sig[checkpoint + 1] = '\r';
675 06f6b49b 2019-04-06 martijn sig[checkpoint + 2] = '\n';
676 06f6b49b 2019-04-06 martijn sig[checkpoint + 3] = '\t';
677 06f6b49b 2019-04-06 martijn linelen = 8;
678 06f6b49b 2019-04-06 martijn *headerlen = *headerlen + 3 - skip;
679 06f6b49b 2019-04-06 martijn i = checkpoint + 3;
680 06f6b49b 2019-04-06 martijn checkpoint = 0;
682 06f6b49b 2019-04-06 martijn if (sig[i] == ';') {
683 06f6b49b 2019-04-06 martijn checkpoint = i;
684 06f6b49b 2019-04-06 martijn tag = '\0';
685 06f6b49b 2019-04-06 martijn continue;
687 06f6b49b 2019-04-06 martijn switch (tag) {
688 06f6b49b 2019-04-06 martijn case 'B':
689 06f6b49b 2019-04-06 martijn case 'b':
690 c826412d 2019-05-02 martijn case 'z':
691 06f6b49b 2019-04-06 martijn checkpoint = i;
693 06f6b49b 2019-04-06 martijn case 'h':
694 06f6b49b 2019-04-06 martijn if (sig[i] == ':')
695 06f6b49b 2019-04-06 martijn checkpoint = i;
698 06f6b49b 2019-04-06 martijn if (tag == '\0' && sig[i] != ' ' && sig[i] != '\t') {
699 06f6b49b 2019-04-06 martijn if ((tag = sig[i]) == 'b' && sig[i + 1] == 'h' &&
700 06f6b49b 2019-04-06 martijn sig[i + 2] == '=') {
701 06f6b49b 2019-04-06 martijn tag = 'B';
702 06f6b49b 2019-04-06 martijn linelen += 2;
705 06f6b49b 2019-04-06 martijn tag = sig[i];
708 06f6b49b 2019-04-06 martijn return 1;
712 6840f1a1 2019-08-22 martijn dkim_signature_printheader(struct dkim_message *message, const char *header)
714 c826412d 2019-05-02 martijn size_t i, j, len;
715 c826412d 2019-05-02 martijn static char *fmtheader = NULL;
716 c826412d 2019-05-02 martijn char *tmp;
717 c826412d 2019-05-02 martijn static size_t size = 0;
718 c826412d 2019-05-02 martijn int first;
720 c826412d 2019-05-02 martijn len = strlen(header);
721 c826412d 2019-05-02 martijn if ((len + 3) * 3 < len) {
722 c826412d 2019-05-02 martijn errno = EOVERFLOW;
723 6840f1a1 2019-08-22 martijn dkim_err(message, "Can't add z-component to header");
724 c826412d 2019-05-02 martijn return 0;
726 c826412d 2019-05-02 martijn if ((len + 3) * 3 > size) {
727 c826412d 2019-05-02 martijn if ((tmp = reallocarray(fmtheader, 3, len + 3)) == NULL) {
728 6840f1a1 2019-08-22 martijn dkim_err(message, "Can't add z-component to header");
729 c826412d 2019-05-02 martijn return 0;
731 c826412d 2019-05-02 martijn fmtheader = tmp;
732 c826412d 2019-05-02 martijn size = (len + 1) * 3;
735 6840f1a1 2019-08-22 martijn first = message->signature.signature[message->signature.len - 1] == '=';
736 c826412d 2019-05-02 martijn for (j = i = 0; header[i] != '\0'; i++, j++) {
737 c826412d 2019-05-02 martijn if (i == 0 && header[i] != ' ' && header[i] != '\t' && !first)
738 c826412d 2019-05-02 martijn fmtheader[j++] = '|';
739 c826412d 2019-05-02 martijn if ((header[i] >= 0x21 && header[i] <= 0x3A) ||
740 6840f1a1 2019-08-22 martijn (header[i] == 0x3C) ||
741 c826412d 2019-05-02 martijn (header[i] >= 0x3E && header[i] <= 0x7B) ||
742 c826412d 2019-05-02 martijn (header[i] >= 0x7D && header[i] <= 0x7E))
743 c826412d 2019-05-02 martijn fmtheader[j] = header[i];
745 c826412d 2019-05-02 martijn fmtheader[j++] = '=';
746 c826412d 2019-05-02 martijn (void) sprintf(fmtheader + j, "%02hhX", header[i]);
750 c826412d 2019-05-02 martijn (void) sprintf(fmtheader + j, "=%02hhX=%02hhX", (unsigned char) '\r',
751 c826412d 2019-05-02 martijn (unsigned char) '\n');
753 6840f1a1 2019-08-22 martijn return dkim_signature_printf(message, "%s", fmtheader);
757 6840f1a1 2019-08-22 martijn dkim_signature_printf(struct dkim_message *message, char *fmt, ...)
759 6840f1a1 2019-08-22 martijn struct dkim_signature *sig = &(message->signature);
760 06f6b49b 2019-04-06 martijn va_list ap;
761 06f6b49b 2019-04-06 martijn size_t len;
763 06f6b49b 2019-04-06 martijn va_start(ap, fmt);
764 06f6b49b 2019-04-06 martijn if ((len = vsnprintf(sig->signature + sig->len, sig->size - sig->len,
765 06f6b49b 2019-04-06 martijn fmt, ap)) >= sig->size - sig->len) {
766 06f6b49b 2019-04-06 martijn va_end(ap);
767 6840f1a1 2019-08-22 martijn if (!dkim_signature_need(message, len + 1))
768 06f6b49b 2019-04-06 martijn return 0;
769 06f6b49b 2019-04-06 martijn va_start(ap, fmt);
770 6840f1a1 2019-08-22 martijn if ((len = vsnprintf(sig->signature + sig->len,
771 6840f1a1 2019-08-22 martijn sig->size - sig->len, fmt, ap)) >= sig->size - sig->len)
772 6840f1a1 2019-08-22 martijn osmtpd_errx(1, "Miscalculated header size");
774 06f6b49b 2019-04-06 martijn sig->len += len;
775 06f6b49b 2019-04-06 martijn va_end(ap);
776 06f6b49b 2019-04-06 martijn return 1;
780 6840f1a1 2019-08-22 martijn dkim_signature_need(struct dkim_message *message, size_t len)
782 6840f1a1 2019-08-22 martijn struct dkim_signature *sig = &(message->signature);
783 06f6b49b 2019-04-06 martijn char *tmp;
785 a27b5ff7 2019-04-08 martijn if (sig->len + len < sig->size)
786 06f6b49b 2019-04-06 martijn return 1;
787 503597cc 2019-04-08 martijn sig->size = (((len + sig->len) / 512) + 1) * 512;
788 06f6b49b 2019-04-06 martijn if ((tmp = realloc(sig->signature, sig->size)) == NULL) {
789 6840f1a1 2019-08-22 martijn dkim_err(message, "No room for signature");
790 06f6b49b 2019-04-06 martijn return 0;
792 06f6b49b 2019-04-06 martijn sig->signature = tmp;
793 06f6b49b 2019-04-06 martijn return 1;
796 48c4bdc1 2019-04-04 martijn __dead void
797 48c4bdc1 2019-04-04 martijn usage(void)
799 fe371fc9 2019-09-05 martijn fprintf(stderr, "usage: filter-dkimsign [-tz] [-a signalg] "
800 fe371fc9 2019-09-05 martijn "[-c canonicalization] \n [-h headerfields]"
801 6840f1a1 2019-08-22 martijn "[-x seconds] -d domain -k keyfile -s selector\n");
802 48c4bdc1 2019-04-04 martijn exit(1);