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 6bcbc798 2021-05-16 martijn #include <openssl/err.h>
17 48c4bdc1 2019-04-04 martijn #include <openssl/evp.h>
18 06f6b49b 2019-04-06 martijn #include <openssl/pem.h>
19 48c4bdc1 2019-04-04 martijn #include <openssl/sha.h>
21 48c4bdc1 2019-04-04 martijn #include <ctype.h>
22 c826412d 2019-05-02 martijn #include <errno.h>
23 1308710c 2019-04-09 martijn #include <fcntl.h>
24 48c4bdc1 2019-04-04 martijn #include <stdio.h>
25 48c4bdc1 2019-04-04 martijn #include <stdlib.h>
26 48c4bdc1 2019-04-04 martijn #include <string.h>
27 48c4bdc1 2019-04-04 martijn #include <syslog.h>
28 48c4bdc1 2019-04-04 martijn #include <time.h>
29 48c4bdc1 2019-04-04 martijn #include <unistd.h>
31 babf5d5a 2021-05-11 martijn #include "openbsd-compat.h"
32 6840f1a1 2019-08-22 martijn #include "opensmtpd.h"
33 40cd76f4 2020-08-30 martijn #include "mheader.h"
35 06f6b49b 2019-04-06 martijn struct dkim_signature {
36 06f6b49b 2019-04-06 martijn char *signature;
37 06f6b49b 2019-04-06 martijn size_t size;
38 06f6b49b 2019-04-06 martijn size_t len;
41 6840f1a1 2019-08-22 martijn struct dkim_message {
42 48c4bdc1 2019-04-04 martijn FILE *origf;
43 48c4bdc1 2019-04-04 martijn int parsing_headers;
44 48c4bdc1 2019-04-04 martijn char **headers;
45 48c4bdc1 2019-04-04 martijn int lastheader;
46 48c4bdc1 2019-04-04 martijn size_t body_whitelines;
47 48c4bdc1 2019-04-04 martijn int has_body;
48 06f6b49b 2019-04-06 martijn struct dkim_signature signature;
50 6bcbc798 2021-05-16 martijn EVP_MD_CTX *dctx;
53 37c6191b 2019-04-06 martijn /* RFC 6376 section 5.4.1 */
54 37c6191b 2019-04-06 martijn static char *dsign_headers[] = {
56 37c6191b 2019-04-06 martijn "reply-to",
57 37c6191b 2019-04-06 martijn "subject",
61 37c6191b 2019-04-06 martijn "resent-date",
62 37c6191b 2019-04-06 martijn "resent-from",
63 37c6191b 2019-04-06 martijn "resent-to",
64 37c6191b 2019-04-06 martijn "resent-cc",
65 37c6191b 2019-04-06 martijn "in-reply-to",
66 37c6191b 2019-04-06 martijn "references",
67 37c6191b 2019-04-06 martijn "list-id",
68 37c6191b 2019-04-06 martijn "list-help",
69 37c6191b 2019-04-06 martijn "list-unsubscribe",
70 37c6191b 2019-04-06 martijn "list-subscribe",
71 37c6191b 2019-04-06 martijn "list-post",
72 37c6191b 2019-04-06 martijn "list-owner",
73 37c6191b 2019-04-06 martijn "list-archive"
75 37c6191b 2019-04-06 martijn static char **sign_headers = dsign_headers;
76 37c6191b 2019-04-06 martijn static size_t nsign_headers = sizeof(dsign_headers) / sizeof(*dsign_headers);
78 fddd7e1e 2019-04-06 martijn static char *hashalg = "sha256";
79 fddd7e1e 2019-04-06 martijn static char *cryptalg = "rsa";
81 48c4bdc1 2019-04-04 martijn #define CANON_SIMPLE 0
82 48c4bdc1 2019-04-04 martijn #define CANON_RELAXED 1
83 48c4bdc1 2019-04-04 martijn static int canonheader = CANON_SIMPLE;
84 48c4bdc1 2019-04-04 martijn static int canonbody = CANON_SIMPLE;
86 e7076cef 2019-05-02 martijn static int addtime = 0;
87 e974922c 2019-05-02 martijn static long long addexpire = 0;
88 c826412d 2019-05-02 martijn static int addheaders = 0;
90 40cd76f4 2020-08-30 martijn static char **domain = NULL;
91 40cd76f4 2020-08-30 martijn static size_t ndomains = 0;
92 06f6b49b 2019-04-06 martijn static char *selector = NULL;
94 06f6b49b 2019-04-06 martijn static EVP_PKEY *pkey;
95 06f6b49b 2019-04-06 martijn static const EVP_MD *hash_md;
96 6bcbc798 2021-05-16 martijn static int keyid = EVP_PKEY_RSA;
97 6bcbc798 2021-05-16 martijn static int sephash = 0;
99 06f6b49b 2019-04-06 martijn #define DKIM_SIGNATURE_LINELEN 78
101 48c4bdc1 2019-04-04 martijn void usage(void);
102 6840f1a1 2019-08-22 martijn void dkim_err(struct dkim_message *, char *);
103 6840f1a1 2019-08-22 martijn void dkim_errx(struct dkim_message *, char *);
104 48c4bdc1 2019-04-04 martijn void dkim_headers_set(char *);
105 6840f1a1 2019-08-22 martijn void dkim_dataline(struct osmtpd_ctx *, const char *);
106 6840f1a1 2019-08-22 martijn void dkim_commit(struct osmtpd_ctx *);
107 6840f1a1 2019-08-22 martijn void *dkim_message_new(struct osmtpd_ctx *);
108 6840f1a1 2019-08-22 martijn void dkim_message_free(struct osmtpd_ctx *, void *);
109 6840f1a1 2019-08-22 martijn void dkim_parse_header(struct dkim_message *, char *, int);
110 6840f1a1 2019-08-22 martijn void dkim_parse_body(struct dkim_message *, char *);
111 6840f1a1 2019-08-22 martijn void dkim_sign(struct osmtpd_ctx *);
112 6840f1a1 2019-08-22 martijn int dkim_signature_printheader(struct dkim_message *, const char *);
113 6840f1a1 2019-08-22 martijn int dkim_signature_printf(struct dkim_message *, char *, ...)
114 06f6b49b 2019-04-06 martijn __attribute__((__format__ (printf, 2, 3)));
115 6840f1a1 2019-08-22 martijn int dkim_signature_normalize(struct dkim_message *);
116 40cd76f4 2020-08-30 martijn const char *dkim_domain_select(struct dkim_message *, char *);
117 6840f1a1 2019-08-22 martijn int dkim_signature_need(struct dkim_message *, size_t);
118 6840f1a1 2019-08-22 martijn int dkim_sign_init(struct dkim_message *);
121 48c4bdc1 2019-04-04 martijn main(int argc, char *argv[])
124 06f6b49b 2019-04-06 martijn FILE *keyfile;
125 e974922c 2019-05-02 martijn const char *errstr;
127 6840f1a1 2019-08-22 martijn while ((ch = getopt(argc, argv, "a:c:d:h:k:s:tx:z")) != -1) {
128 48c4bdc1 2019-04-04 martijn switch (ch) {
129 48c4bdc1 2019-04-04 martijn case 'a':
130 6bcbc798 2021-05-16 martijn if (strncmp(optarg, "rsa-", 4) == 0) {
131 6bcbc798 2021-05-16 martijn cryptalg = "rsa";
132 6bcbc798 2021-05-16 martijn hashalg = optarg + 4;
133 6bcbc798 2021-05-16 martijn keyid = EVP_PKEY_RSA;
134 6bcbc798 2021-05-16 martijn sephash = 0;
135 6bcbc798 2021-05-16 martijn #ifdef HAVE_ED25519
136 6bcbc798 2021-05-16 martijn } else if (strncmp(optarg, "ed25519-", 8) == 0) {
137 6bcbc798 2021-05-16 martijn hashalg = optarg + 8;
138 6bcbc798 2021-05-16 martijn cryptalg = "ed25519";
139 6bcbc798 2021-05-16 martijn keyid = EVP_PKEY_ED25519;
140 6bcbc798 2021-05-16 martijn sephash = 1;
143 6bcbc798 2021-05-16 martijn osmtpd_errx(1, "invalid algorithm");
145 48c4bdc1 2019-04-04 martijn case 'c':
146 48c4bdc1 2019-04-04 martijn if (strncmp(optarg, "simple", 6) == 0) {
147 48c4bdc1 2019-04-04 martijn canonheader = CANON_SIMPLE;
148 48c4bdc1 2019-04-04 martijn optarg += 6;
149 48c4bdc1 2019-04-04 martijn } else if (strncmp(optarg, "relaxed", 7) == 0) {
150 48c4bdc1 2019-04-04 martijn canonheader = CANON_RELAXED;
151 48c4bdc1 2019-04-04 martijn optarg += 7;
153 6840f1a1 2019-08-22 martijn osmtpd_err(1, "Invalid canonicalization");
154 48c4bdc1 2019-04-04 martijn if (optarg[0] == '/') {
155 48c4bdc1 2019-04-04 martijn if (strcmp(optarg + 1, "simple") == 0)
156 48c4bdc1 2019-04-04 martijn canonbody = CANON_SIMPLE;
157 48c4bdc1 2019-04-04 martijn else if (strcmp(optarg + 1, "relaxed") == 0)
158 48c4bdc1 2019-04-04 martijn canonbody = CANON_RELAXED;
160 6840f1a1 2019-08-22 martijn osmtpd_err(1,
161 6840f1a1 2019-08-22 martijn "Invalid canonicalization");
162 48c4bdc1 2019-04-04 martijn } else if (optarg[0] == '\0')
163 48c4bdc1 2019-04-04 martijn canonbody = CANON_SIMPLE;
165 6840f1a1 2019-08-22 martijn osmtpd_err(1, "Invalid canonicalization");
167 48c4bdc1 2019-04-04 martijn case 'd':
168 40cd76f4 2020-08-30 martijn if ((domain = reallocarray(domain, ndomains + 1,
169 40cd76f4 2020-08-30 martijn sizeof(*domain))) == NULL)
170 40cd76f4 2020-08-30 martijn osmtpd_err(1, "malloc");
171 40cd76f4 2020-08-30 martijn domain[ndomains++] = optarg;
173 06f6b49b 2019-04-06 martijn case 'h':
174 06f6b49b 2019-04-06 martijn dkim_headers_set(optarg);
176 06f6b49b 2019-04-06 martijn case 'k':
177 06f6b49b 2019-04-06 martijn if ((keyfile = fopen(optarg, "r")) == NULL)
178 6840f1a1 2019-08-22 martijn osmtpd_err(1, "Can't open key file (%s)",
179 6840f1a1 2019-08-22 martijn optarg);
180 06f6b49b 2019-04-06 martijn pkey = PEM_read_PrivateKey(keyfile, NULL, NULL, NULL);
181 06f6b49b 2019-04-06 martijn if (pkey == NULL)
182 6840f1a1 2019-08-22 martijn osmtpd_errx(1, "Can't read key file");
183 06f6b49b 2019-04-06 martijn fclose(keyfile);
185 06f6b49b 2019-04-06 martijn case 's':
186 06f6b49b 2019-04-06 martijn selector = optarg;
188 e7076cef 2019-05-02 martijn case 't':
189 e7076cef 2019-05-02 martijn addtime = 1;
191 e974922c 2019-05-02 martijn case 'x':
192 e974922c 2019-05-02 martijn addexpire = strtonum(optarg, 1, INT64_MAX, &errstr);
193 e974922c 2019-05-02 martijn if (addexpire == 0)
194 6840f1a1 2019-08-22 martijn osmtpd_errx(1, "Expire offset is %s", errstr);
196 c826412d 2019-05-02 martijn case 'z':
197 6840f1a1 2019-08-22 martijn addheaders++;
199 48c4bdc1 2019-04-04 martijn default:
200 48c4bdc1 2019-04-04 martijn usage();
204 fddd7e1e 2019-04-06 martijn OpenSSL_add_all_digests();
206 6840f1a1 2019-08-22 martijn if (pledge("tmppath stdio", NULL) == -1)
207 6840f1a1 2019-08-22 martijn osmtpd_err(1, "pledge");
209 6bcbc798 2021-05-16 martijn if ((hash_md = EVP_get_digestbyname(hashalg)) == NULL)
210 6bcbc798 2021-05-16 martijn osmtpd_errx(1, "Can't find hash: %s", hashalg);
212 06f6b49b 2019-04-06 martijn if (domain == NULL || selector == NULL || pkey == NULL)
213 48c4bdc1 2019-04-04 martijn usage();
215 6bcbc798 2021-05-16 martijn if (EVP_PKEY_id(pkey) != keyid)
216 6bcbc798 2021-05-16 martijn osmtpd_errx(1, "Key is not of type %s", cryptalg);
218 6840f1a1 2019-08-22 martijn osmtpd_register_filter_dataline(dkim_dataline);
219 6840f1a1 2019-08-22 martijn osmtpd_register_filter_commit(dkim_commit);
220 6840f1a1 2019-08-22 martijn osmtpd_local_message(dkim_message_new, dkim_message_free);
221 6840f1a1 2019-08-22 martijn osmtpd_run();
223 48c4bdc1 2019-04-04 martijn return 0;
227 6840f1a1 2019-08-22 martijn dkim_dataline(struct osmtpd_ctx *ctx, const char *line)
229 6840f1a1 2019-08-22 martijn struct dkim_message *message = ctx->local_message;
230 6840f1a1 2019-08-22 martijn char *linedup;
231 48c4bdc1 2019-04-04 martijn size_t linelen;
233 d52cf19a 2020-07-25 martijn if (message->err) {
234 d52cf19a 2020-07-25 martijn if (line[0] == '.' && line[1] =='\0')
235 d52cf19a 2020-07-25 martijn osmtpd_filter_dataline(ctx, ".");
239 48c4bdc1 2019-04-04 martijn linelen = strlen(line);
240 6840f1a1 2019-08-22 martijn if (fprintf(message->origf, "%s\n", line) < (int) linelen)
241 71b92702 2021-05-14 martijn dkim_errx(message, "Couldn't write to tempfile");
243 7ecf4491 2019-05-01 martijn if (line[0] == '.' && line[1] =='\0') {
244 6840f1a1 2019-08-22 martijn dkim_sign(ctx);
245 6840f1a1 2019-08-22 martijn } else if (linelen != 0 && message->parsing_headers) {
246 f5286ea3 2019-04-08 martijn if (line[0] == '.')
248 6840f1a1 2019-08-22 martijn if ((linedup = strdup(line)) == NULL)
249 6840f1a1 2019-08-22 martijn osmtpd_err(1, "strdup");
250 6840f1a1 2019-08-22 martijn dkim_parse_header(message, linedup, 0);
251 6840f1a1 2019-08-22 martijn free(linedup);
252 6840f1a1 2019-08-22 martijn } else if (linelen == 0 && message->parsing_headers) {
253 6840f1a1 2019-08-22 martijn if (addheaders > 0 && !dkim_signature_printf(message, "; "))
255 6840f1a1 2019-08-22 martijn message->parsing_headers = 0;
256 f5286ea3 2019-04-08 martijn } else {
257 f5286ea3 2019-04-08 martijn if (line[0] == '.')
259 6840f1a1 2019-08-22 martijn if ((linedup = strdup(line)) == NULL)
260 6840f1a1 2019-08-22 martijn osmtpd_err(1, "strdup");
261 6840f1a1 2019-08-22 martijn dkim_parse_body(message, linedup);
262 6840f1a1 2019-08-22 martijn free(linedup);
267 6840f1a1 2019-08-22 martijn dkim_commit(struct osmtpd_ctx *ctx)
269 6840f1a1 2019-08-22 martijn struct dkim_message *message = ctx->local_message;
271 6840f1a1 2019-08-22 martijn if (message->err)
272 6840f1a1 2019-08-22 martijn osmtpd_filter_disconnect(ctx, "Internal server error");
274 6840f1a1 2019-08-22 martijn osmtpd_filter_proceed(ctx);
278 6840f1a1 2019-08-22 martijn dkim_message_new(struct osmtpd_ctx *ctx)
280 6840f1a1 2019-08-22 martijn struct dkim_message *message;
282 41815bcc 2021-05-14 martijn if ((message = calloc(1, sizeof(*message))) == NULL) {
283 41815bcc 2021-05-14 martijn dkim_err(message, "Failed to create message context");
284 41815bcc 2021-05-14 martijn return NULL;
287 6840f1a1 2019-08-22 martijn if ((message->origf = tmpfile()) == NULL) {
288 41815bcc 2021-05-14 martijn dkim_err(message, "Failed to open tempfile");
289 41815bcc 2021-05-14 martijn goto fail;
291 6840f1a1 2019-08-22 martijn message->parsing_headers = 1;
293 6840f1a1 2019-08-22 martijn message->body_whitelines = 0;
294 6840f1a1 2019-08-22 martijn message->headers = calloc(1, sizeof(*(message->headers)));
295 6840f1a1 2019-08-22 martijn if (message->headers == NULL) {
296 6840f1a1 2019-08-22 martijn dkim_err(message, "Can't save headers");
297 41815bcc 2021-05-14 martijn goto fail;
299 6840f1a1 2019-08-22 martijn message->lastheader = 0;
300 6840f1a1 2019-08-22 martijn message->signature.signature = NULL;
301 6840f1a1 2019-08-22 martijn message->signature.size = 0;
302 6840f1a1 2019-08-22 martijn message->signature.len = 0;
303 6840f1a1 2019-08-22 martijn message->err = 0;
305 6840f1a1 2019-08-22 martijn if (!dkim_signature_printf(message,
306 40cd76f4 2020-08-30 martijn "DKIM-Signature: v=%s; a=%s-%s; c=%s/%s; s=%s; ", "1",
307 fa10b9e8 2019-04-06 martijn cryptalg, hashalg,
308 06f6b49b 2019-04-06 martijn canonheader == CANON_SIMPLE ? "simple" : "relaxed",
309 40cd76f4 2020-08-30 martijn canonbody == CANON_SIMPLE ? "simple" : "relaxed", selector))
310 41815bcc 2021-05-14 martijn goto fail;
311 6840f1a1 2019-08-22 martijn if (addheaders > 0 && !dkim_signature_printf(message, "z="))
312 41815bcc 2021-05-14 martijn goto fail;
314 6bcbc798 2021-05-16 martijn if ((message->dctx = EVP_MD_CTX_new()) == NULL) {
315 41815bcc 2021-05-14 martijn dkim_errx(message, "Failed to create hash context");
316 41815bcc 2021-05-14 martijn goto fail;
318 6bcbc798 2021-05-16 martijn if (EVP_DigestInit_ex(message->dctx, hash_md, NULL) <= 0) {
319 6840f1a1 2019-08-22 martijn dkim_errx(message, "Failed to initialize hash context");
320 41815bcc 2021-05-14 martijn goto fail;
322 6840f1a1 2019-08-22 martijn return message;
324 500ea6d4 2022-01-27 martijn dkim_message_free(ctx, message);
325 41815bcc 2021-05-14 martijn return NULL;
329 6840f1a1 2019-08-22 martijn dkim_message_free(struct osmtpd_ctx *ctx, void *data)
331 6840f1a1 2019-08-22 martijn struct dkim_message *message = data;
332 48c4bdc1 2019-04-04 martijn size_t i;
334 6840f1a1 2019-08-22 martijn fclose(message->origf);
335 6bcbc798 2021-05-16 martijn EVP_MD_CTX_free(message->dctx);
336 6840f1a1 2019-08-22 martijn free(message->signature.signature);
337 500ea6d4 2022-01-27 martijn for (i = 0; message->headers != NULL &&
338 500ea6d4 2022-01-27 martijn message->headers[i] != NULL; i++)
339 6840f1a1 2019-08-22 martijn free(message->headers[i]);
340 6840f1a1 2019-08-22 martijn free(message->headers);
341 6840f1a1 2019-08-22 martijn free(message);
345 48c4bdc1 2019-04-04 martijn dkim_headers_set(char *headers)
347 48c4bdc1 2019-04-04 martijn size_t i;
348 48c4bdc1 2019-04-04 martijn int has_from = 0;
350 48c4bdc1 2019-04-04 martijn nsign_headers = 1;
352 48c4bdc1 2019-04-04 martijn for (i = 0; headers[i] != '\0'; i++) {
353 48c4bdc1 2019-04-04 martijn /* RFC 5322 field-name */
354 48c4bdc1 2019-04-04 martijn if (!(headers[i] >= 33 && headers[i] <= 126))
355 6840f1a1 2019-08-22 martijn osmtpd_errx(1, "-h: invalid character");
356 48c4bdc1 2019-04-04 martijn if (headers[i] == ':') {
357 48c4bdc1 2019-04-04 martijn /* Test for empty headers */
358 48c4bdc1 2019-04-04 martijn if (i == 0 || headers[i - 1] == ':')
359 6840f1a1 2019-08-22 martijn osmtpd_errx(1, "-h: header can't be empty");
360 48c4bdc1 2019-04-04 martijn nsign_headers++;
362 48c4bdc1 2019-04-04 martijn headers[i] = tolower(headers[i]);
364 48c4bdc1 2019-04-04 martijn if (headers[i - 1] == ':')
365 6840f1a1 2019-08-22 martijn osmtpd_errx(1, "-h: header can't be empty");
367 6840f1a1 2019-08-22 martijn if ((sign_headers = reallocarray(NULL, nsign_headers + 1,
368 6840f1a1 2019-08-22 martijn sizeof(*sign_headers))) == NULL)
369 6840f1a1 2019-08-22 martijn osmtpd_errx(1, NULL);
371 48c4bdc1 2019-04-04 martijn for (i = 0; i < nsign_headers; i++) {
372 48c4bdc1 2019-04-04 martijn sign_headers[i] = headers;
373 48c4bdc1 2019-04-04 martijn if (i != nsign_headers - 1) {
374 48c4bdc1 2019-04-04 martijn headers = strchr(headers, ':');
375 48c4bdc1 2019-04-04 martijn headers++[0] = '\0';
377 48c4bdc1 2019-04-04 martijn if (strcasecmp(sign_headers[i], "from") == 0)
378 48c4bdc1 2019-04-04 martijn has_from = 1;
380 48c4bdc1 2019-04-04 martijn if (!has_from)
381 6840f1a1 2019-08-22 martijn osmtpd_errx(1, "From header must be included");
385 6840f1a1 2019-08-22 martijn dkim_err(struct dkim_message *message, char *msg)
387 6840f1a1 2019-08-22 martijn message->err = 1;
388 803cdd74 2020-07-25 martijn fprintf(stderr, "%s: %s\n", msg, strerror(errno));
392 6840f1a1 2019-08-22 martijn dkim_errx(struct dkim_message *message, char *msg)
394 6840f1a1 2019-08-22 martijn message->err = 1;
395 6840f1a1 2019-08-22 martijn fprintf(stderr, "%s\n", msg);
399 6840f1a1 2019-08-22 martijn dkim_parse_header(struct dkim_message *message, char *line, int force)
401 48c4bdc1 2019-04-04 martijn size_t i;
402 48c4bdc1 2019-04-04 martijn size_t r, w;
403 48c4bdc1 2019-04-04 martijn size_t linelen;
404 48c4bdc1 2019-04-04 martijn size_t lastheader;
405 06f6b49b 2019-04-06 martijn size_t hlen;
406 b6e37a5e 2019-04-08 martijn int fieldname = 0;
407 48c4bdc1 2019-04-04 martijn char **mtmp;
408 48c4bdc1 2019-04-04 martijn char *htmp;
409 63ac36ce 2019-04-08 martijn char *tmp;
411 c826412d 2019-05-02 martijn if (addheaders == 2 && !force &&
412 6840f1a1 2019-08-22 martijn !dkim_signature_printheader(message, line))
415 6840f1a1 2019-08-22 martijn if ((line[0] == ' ' || line[0] == '\t') && !message->lastheader)
417 48c4bdc1 2019-04-04 martijn if ((line[0] != ' ' && line[0] != '\t')) {
418 6840f1a1 2019-08-22 martijn message->lastheader = 0;
419 48c4bdc1 2019-04-04 martijn for (i = 0; i < nsign_headers; i++) {
420 06f6b49b 2019-04-06 martijn hlen = strlen(sign_headers[i]);
421 06f6b49b 2019-04-06 martijn if (strncasecmp(line, sign_headers[i], hlen) == 0) {
422 06f6b49b 2019-04-06 martijn while (line[hlen] == ' ' || line[hlen] == '\t')
424 06f6b49b 2019-04-06 martijn if (line[hlen] != ':')
425 06f6b49b 2019-04-06 martijn continue;
429 06f6b49b 2019-04-06 martijn if (i == nsign_headers && !force)
433 c826412d 2019-05-02 martijn if (addheaders == 1 && !force &&
434 6840f1a1 2019-08-22 martijn !dkim_signature_printheader(message, line))
437 48c4bdc1 2019-04-04 martijn if (canonheader == CANON_RELAXED) {
438 6840f1a1 2019-08-22 martijn if (!message->lastheader)
439 b6e37a5e 2019-04-08 martijn fieldname = 1;
440 48c4bdc1 2019-04-04 martijn for (r = w = 0; line[r] != '\0'; r++) {
441 5d06ccad 2019-04-08 martijn if (line[r] == ':' && fieldname) {
442 cd6f9b03 2019-10-23 martijn if (w > 0 && line[w - 1] == ' ')
443 48c4bdc1 2019-04-04 martijn line[w - 1] = ':';
445 48c4bdc1 2019-04-04 martijn line[w++] = ':';
446 48c4bdc1 2019-04-04 martijn fieldname = 0;
447 48c4bdc1 2019-04-04 martijn while (line[r + 1] == ' ' ||
448 48c4bdc1 2019-04-04 martijn line[r + 1] == '\t')
450 48c4bdc1 2019-04-04 martijn continue;
452 06f6b49b 2019-04-06 martijn if (line[r] == ' ' || line[r] == '\t' ||
453 06f6b49b 2019-04-06 martijn line[r] == '\r' || line[r] == '\n') {
454 cd6f9b03 2019-10-23 martijn if (r != 0 && w != 0 && line[w - 1] == ' ')
455 48c4bdc1 2019-04-04 martijn continue;
457 48c4bdc1 2019-04-04 martijn line[w++] = ' ';
458 48c4bdc1 2019-04-04 martijn } else if (fieldname) {
459 48c4bdc1 2019-04-04 martijn line[w++] = tolower(line[r]);
460 48c4bdc1 2019-04-04 martijn continue;
462 48c4bdc1 2019-04-04 martijn line[w++] = line[r];
464 cd6f9b03 2019-10-23 martijn linelen = (w != 0 && line[w - 1] == ' ') ? w - 1 : w;
465 48c4bdc1 2019-04-04 martijn line[linelen] = '\0';
467 48c4bdc1 2019-04-04 martijn linelen = strlen(line);
469 6840f1a1 2019-08-22 martijn for (lastheader = 0; message->headers[lastheader] != NULL; lastheader++)
470 48c4bdc1 2019-04-04 martijn continue;
471 6840f1a1 2019-08-22 martijn if (!message->lastheader) {
472 6840f1a1 2019-08-22 martijn mtmp = recallocarray(message->headers, lastheader + 1,
473 a53d74c4 2019-04-08 martijn lastheader + 2, sizeof(*mtmp));
474 48c4bdc1 2019-04-04 martijn if (mtmp == NULL) {
475 6840f1a1 2019-08-22 martijn dkim_err(message, "Can't store header");
478 6840f1a1 2019-08-22 martijn message->headers = mtmp;
480 500ea6d4 2022-01-27 martijn if ((message->headers[lastheader] = strdup(line)) == NULL) {
481 500ea6d4 2022-01-27 martijn dkim_err(message, "Can't store header");
484 6840f1a1 2019-08-22 martijn message->headers[lastheader + 1 ] = NULL;
485 6840f1a1 2019-08-22 martijn message->lastheader = 1;
486 48c4bdc1 2019-04-04 martijn } else {
487 48c4bdc1 2019-04-04 martijn lastheader--;
488 6840f1a1 2019-08-22 martijn linelen += strlen(message->headers[lastheader]);
489 48c4bdc1 2019-04-04 martijn if (canonheader == CANON_SIMPLE)
490 48c4bdc1 2019-04-04 martijn linelen += 2;
491 48c4bdc1 2019-04-04 martijn linelen++;
492 6840f1a1 2019-08-22 martijn htmp = reallocarray(message->headers[lastheader], linelen,
493 48c4bdc1 2019-04-04 martijn sizeof(*htmp));
494 48c4bdc1 2019-04-04 martijn if (htmp == NULL) {
495 6840f1a1 2019-08-22 martijn dkim_err(message, "Can't store header");
498 6840f1a1 2019-08-22 martijn message->headers[lastheader] = htmp;
499 48c4bdc1 2019-04-04 martijn if (canonheader == CANON_SIMPLE) {
500 48c4bdc1 2019-04-04 martijn if (strlcat(htmp, "\r\n", linelen) >= linelen)
501 6840f1a1 2019-08-22 martijn osmtpd_errx(1, "Missized header");
502 63ac36ce 2019-04-08 martijn } else if (canonheader == CANON_RELAXED &&
503 6840f1a1 2019-08-22 martijn (tmp = strchr(message->headers[lastheader], ':')) != NULL &&
504 63ac36ce 2019-04-08 martijn tmp[1] == '\0')
507 48c4bdc1 2019-04-04 martijn if (strlcat(htmp, line, linelen) >= linelen)
508 6840f1a1 2019-08-22 martijn osmtpd_errx(1, "Missized header");
513 6840f1a1 2019-08-22 martijn dkim_parse_body(struct dkim_message *message, char *line)
515 48c4bdc1 2019-04-04 martijn size_t r, w;
516 48c4bdc1 2019-04-04 martijn size_t linelen;
518 1308710c 2019-04-09 martijn if (canonbody == CANON_RELAXED) {
519 1308710c 2019-04-09 martijn for (r = w = 0; line[r] != '\0'; r++) {
520 1308710c 2019-04-09 martijn if (line[r] == ' ' || line[r] == '\t') {
521 1308710c 2019-04-09 martijn if (r != 0 && line[w - 1] == ' ')
522 1308710c 2019-04-09 martijn continue;
524 1308710c 2019-04-09 martijn line[w++] = ' ';
526 1308710c 2019-04-09 martijn line[w++] = line[r];
528 cd6f9b03 2019-10-23 martijn linelen = (w != 0 && line[w - 1] == ' ') ? w - 1 : w;
529 1308710c 2019-04-09 martijn line[linelen] = '\0';
531 1308710c 2019-04-09 martijn linelen = strlen(line);
533 48c4bdc1 2019-04-04 martijn if (line[0] == '\0') {
534 6840f1a1 2019-08-22 martijn message->body_whitelines++;
538 6840f1a1 2019-08-22 martijn while (message->body_whitelines--) {
539 6bcbc798 2021-05-16 martijn if (EVP_DigestUpdate(message->dctx, "\r\n", 2) == 0) {
540 71b92702 2021-05-14 martijn dkim_errx(message, "Can't update hash context");
544 6840f1a1 2019-08-22 martijn message->body_whitelines = 0;
545 6840f1a1 2019-08-22 martijn message->has_body = 1;
547 6bcbc798 2021-05-16 martijn if (EVP_DigestUpdate(message->dctx, line, linelen) == 0 ||
548 6bcbc798 2021-05-16 martijn EVP_DigestUpdate(message->dctx, "\r\n", 2) == 0) {
549 71b92702 2021-05-14 martijn dkim_errx(message, "Can't update hash context");
555 6840f1a1 2019-08-22 martijn dkim_sign(struct osmtpd_ctx *ctx)
557 6840f1a1 2019-08-22 martijn struct dkim_message *message = ctx->local_message;
558 365862b1 2019-05-02 martijn /* Use largest hash size here */
559 8e76d341 2021-06-02 martijn unsigned char bdigest[EVP_MAX_MD_SIZE];
560 8e76d341 2021-06-02 martijn unsigned char digest[(((sizeof(bdigest) + 2) / 3) * 4) + 1];
561 8e76d341 2021-06-02 martijn unsigned char *b;
562 40cd76f4 2020-08-30 martijn const char *sdomain = domain[0], *tsdomain;
563 e974922c 2019-05-02 martijn time_t now;
564 6840f1a1 2019-08-22 martijn ssize_t i;
565 6bcbc798 2021-05-16 martijn size_t linelen = 0;
566 365862b1 2019-05-02 martijn char *tmp, *tmp2;
567 8e76d341 2021-06-02 martijn unsigned int digestsz;
569 e974922c 2019-05-02 martijn if (addtime || addexpire)
570 e974922c 2019-05-02 martijn now = time(NULL);
571 a70fd4b2 2021-05-11 martijn if (addtime && !dkim_signature_printf(message, "t=%lld; ",
572 a70fd4b2 2021-05-11 martijn (long long)now))
573 2c88ced1 2021-05-16 martijn goto fail;
574 6840f1a1 2019-08-22 martijn if (addexpire != 0 && !dkim_signature_printf(message, "x=%lld; ",
575 e974922c 2019-05-02 martijn now + addexpire < now ? INT64_MAX : now + addexpire))
576 2c88ced1 2021-05-16 martijn goto fail;
578 6840f1a1 2019-08-22 martijn if (canonbody == CANON_SIMPLE && !message->has_body) {
579 6bcbc798 2021-05-16 martijn if (EVP_DigestUpdate(message->dctx, "\r\n", 2) <= 0) {
580 71b92702 2021-05-14 martijn dkim_errx(message, "Can't update hash context");
581 2c88ced1 2021-05-16 martijn goto fail;
584 6bcbc798 2021-05-16 martijn if (EVP_DigestFinal_ex(message->dctx, bdigest, &digestsz) == 0) {
585 71b92702 2021-05-14 martijn dkim_errx(message, "Can't finalize hash context");
586 2c88ced1 2021-05-16 martijn goto fail;
588 6bcbc798 2021-05-16 martijn EVP_EncodeBlock(digest, bdigest, digestsz);
589 6bcbc798 2021-05-16 martijn if (!dkim_signature_printf(message, "bh=%s; h=", digest))
590 2c88ced1 2021-05-16 martijn goto fail;
591 365862b1 2019-05-02 martijn /* Reverse order for ease of use of RFC6367 section 5.4.2 */
592 6840f1a1 2019-08-22 martijn for (i = 0; message->headers[i] != NULL; i++)
593 365862b1 2019-05-02 martijn continue;
594 6bcbc798 2021-05-16 martijn EVP_MD_CTX_reset(message->dctx);
595 6bcbc798 2021-05-16 martijn if (!sephash) {
596 6bcbc798 2021-05-16 martijn if (EVP_DigestSignInit(message->dctx, NULL, hash_md, NULL,
597 6bcbc798 2021-05-16 martijn pkey) != 1) {
598 6bcbc798 2021-05-16 martijn dkim_errx(message, "Failed to initialize signature "
599 6bcbc798 2021-05-16 martijn "context");
600 2c88ced1 2021-05-16 martijn goto fail;
602 6bcbc798 2021-05-16 martijn } else {
603 6bcbc798 2021-05-16 martijn if (EVP_DigestInit_ex(message->dctx, hash_md, NULL) != 1) {
604 6bcbc798 2021-05-16 martijn dkim_errx(message, "Failed to initialize hash context");
605 2c88ced1 2021-05-16 martijn goto fail;
608 365862b1 2019-05-02 martijn for (i--; i >= 0; i--) {
609 6bcbc798 2021-05-16 martijn if (!sephash) {
610 6bcbc798 2021-05-16 martijn if (EVP_DigestSignUpdate(message->dctx,
611 6bcbc798 2021-05-16 martijn message->headers[i],
612 6bcbc798 2021-05-16 martijn strlen(message->headers[i])) != 1 ||
613 6bcbc798 2021-05-16 martijn EVP_DigestSignUpdate(message->dctx, "\r\n",
614 6bcbc798 2021-05-16 martijn 2) <= 0) {
615 6bcbc798 2021-05-16 martijn dkim_errx(message, "Failed to update signature "
616 6bcbc798 2021-05-16 martijn "context");
617 2c88ced1 2021-05-16 martijn goto fail;
619 6bcbc798 2021-05-16 martijn } else {
620 6bcbc798 2021-05-16 martijn if (EVP_DigestUpdate(message->dctx, message->headers[i],
621 6bcbc798 2021-05-16 martijn strlen(message->headers[i])) != 1 ||
622 6bcbc798 2021-05-16 martijn EVP_DigestUpdate(message->dctx, "\r\n", 2) <= 0) {
623 6bcbc798 2021-05-16 martijn dkim_errx(message, "Failed to update digest "
624 6bcbc798 2021-05-16 martijn "context");
625 2c88ced1 2021-05-16 martijn goto fail;
628 40cd76f4 2020-08-30 martijn if ((tsdomain = dkim_domain_select(message, message->headers[i])) != NULL)
629 40cd76f4 2020-08-30 martijn sdomain = tsdomain;
630 365862b1 2019-05-02 martijn /* We're done with the cached header after hashing */
631 6840f1a1 2019-08-22 martijn for (tmp = message->headers[i]; tmp[0] != ':'; tmp++) {
632 365862b1 2019-05-02 martijn if (tmp[0] == ' ' || tmp[0] == '\t')
634 365862b1 2019-05-02 martijn tmp[0] = tolower(tmp[0]);
636 365862b1 2019-05-02 martijn tmp[0] = '\0';
637 6840f1a1 2019-08-22 martijn if (!dkim_signature_printf(message, "%s%s",
638 6840f1a1 2019-08-22 martijn message->headers[i + 1] == NULL ? "" : ":",
639 6840f1a1 2019-08-22 martijn message->headers[i]))
640 2c88ced1 2021-05-16 martijn goto fail;
642 40cd76f4 2020-08-30 martijn dkim_signature_printf(message, "; d=%s; b=", sdomain);
643 6840f1a1 2019-08-22 martijn if (!dkim_signature_normalize(message))
644 2c88ced1 2021-05-16 martijn goto fail;
645 6840f1a1 2019-08-22 martijn if ((tmp = strdup(message->signature.signature)) == NULL) {
646 6840f1a1 2019-08-22 martijn dkim_err(message, "Can't create DKIM signature");
647 2c88ced1 2021-05-16 martijn goto fail;
649 6840f1a1 2019-08-22 martijn dkim_parse_header(message, tmp, 1);
650 6bcbc798 2021-05-16 martijn if (!sephash) {
651 6bcbc798 2021-05-16 martijn if (EVP_DigestSignUpdate(message->dctx, tmp,
652 6bcbc798 2021-05-16 martijn strlen(tmp)) != 1) {
653 6bcbc798 2021-05-16 martijn dkim_errx(message, "Failed to update signature "
654 6bcbc798 2021-05-16 martijn "context");
655 2c88ced1 2021-05-16 martijn goto fail;
657 6bcbc798 2021-05-16 martijn } else {
658 6bcbc798 2021-05-16 martijn if (EVP_DigestUpdate(message->dctx, tmp, strlen(tmp)) != 1) {
659 6bcbc798 2021-05-16 martijn dkim_errx(message, "Failed to update digest context");
660 2c88ced1 2021-05-16 martijn goto fail;
663 365862b1 2019-05-02 martijn free(tmp);
664 6bcbc798 2021-05-16 martijn if (!sephash) {
665 6bcbc798 2021-05-16 martijn if (EVP_DigestSignFinal(message->dctx, NULL, &linelen) != 1) {
666 6bcbc798 2021-05-16 martijn dkim_errx(message, "Can't finalize signature context");
667 2c88ced1 2021-05-16 martijn goto fail;
669 6bcbc798 2021-05-16 martijn #ifdef HAVE_ED25519
670 6bcbc798 2021-05-16 martijn } else {
671 6bcbc798 2021-05-16 martijn if (EVP_DigestFinal_ex(message->dctx, bdigest,
672 6bcbc798 2021-05-16 martijn &digestsz) != 1) {
673 6bcbc798 2021-05-16 martijn dkim_errx(message, "Can't finalize hash context");
674 2c88ced1 2021-05-16 martijn goto fail;
676 6bcbc798 2021-05-16 martijn EVP_MD_CTX_reset(message->dctx);
677 6bcbc798 2021-05-16 martijn if (EVP_DigestSignInit(message->dctx, NULL, NULL, NULL,
678 6bcbc798 2021-05-16 martijn pkey) != 1) {
679 6bcbc798 2021-05-16 martijn dkim_errx(message, "Failed to initialize signature "
680 6bcbc798 2021-05-16 martijn "context");
681 2c88ced1 2021-05-16 martijn goto fail;
683 6bcbc798 2021-05-16 martijn if (EVP_DigestSign(message->dctx, NULL, &linelen, bdigest,
684 6bcbc798 2021-05-16 martijn digestsz) != 1) {
685 6bcbc798 2021-05-16 martijn dkim_errx(message, "Failed to finalize signature");
686 2c88ced1 2021-05-16 martijn goto fail;
690 365862b1 2019-05-02 martijn if ((tmp = malloc(linelen)) == NULL) {
691 6bcbc798 2021-05-16 martijn dkim_err(message, "Can't allocate space for signature");
692 2c88ced1 2021-05-16 martijn goto fail;
694 6bcbc798 2021-05-16 martijn if (!sephash) {
695 6bcbc798 2021-05-16 martijn if (EVP_DigestSignFinal(message->dctx, tmp, &linelen) != 1) {
696 6bcbc798 2021-05-16 martijn dkim_errx(message, "Failed to finalize signature");
697 2c88ced1 2021-05-16 martijn goto fail;
699 6bcbc798 2021-05-16 martijn #ifdef HAVE_ED25519
700 6bcbc798 2021-05-16 martijn } else {
701 6bcbc798 2021-05-16 martijn if (EVP_DigestSign(message->dctx, tmp, &linelen, bdigest,
702 6bcbc798 2021-05-16 martijn digestsz) != 1) {
703 6bcbc798 2021-05-16 martijn dkim_errx(message, "Failed to finalize signature");
704 2c88ced1 2021-05-16 martijn goto fail;
708 365862b1 2019-05-02 martijn if ((b = malloc((((linelen + 2) / 3) * 4) + 1)) == NULL) {
709 6840f1a1 2019-08-22 martijn dkim_err(message, "Can't create DKIM signature");
710 2c88ced1 2021-05-16 martijn goto fail;
712 365862b1 2019-05-02 martijn EVP_EncodeBlock(b, tmp, linelen);
713 365862b1 2019-05-02 martijn free(tmp);
714 6840f1a1 2019-08-22 martijn dkim_signature_printf(message, "%s\r\n", b);
715 365862b1 2019-05-02 martijn free(b);
716 6840f1a1 2019-08-22 martijn dkim_signature_normalize(message);
717 6840f1a1 2019-08-22 martijn tmp = message->signature.signature;
718 365862b1 2019-05-02 martijn while ((tmp2 = strchr(tmp, '\r')) != NULL) {
719 365862b1 2019-05-02 martijn tmp2[0] = '\0';
720 6840f1a1 2019-08-22 martijn osmtpd_filter_dataline(ctx, "%s", tmp);
721 365862b1 2019-05-02 martijn tmp = tmp2 + 2;
723 365862b1 2019-05-02 martijn tmp = NULL;
724 365862b1 2019-05-02 martijn linelen = 0;
725 6840f1a1 2019-08-22 martijn rewind(message->origf);
726 6840f1a1 2019-08-22 martijn while ((i = getline(&tmp, &linelen, message->origf)) != -1) {
727 365862b1 2019-05-02 martijn tmp[i - 1] = '\0';
728 6840f1a1 2019-08-22 martijn osmtpd_filter_dataline(ctx, "%s", tmp);
730 be6d950d 2021-03-21 martijn free(tmp);
733 2c88ced1 2021-05-16 martijn osmtpd_filter_dataline(ctx, ".");
737 6840f1a1 2019-08-22 martijn dkim_signature_normalize(struct dkim_message *message)
739 06f6b49b 2019-04-06 martijn size_t i;
740 06f6b49b 2019-04-06 martijn size_t linelen;
741 06f6b49b 2019-04-06 martijn size_t checkpoint;
742 06f6b49b 2019-04-06 martijn size_t skip;
743 6840f1a1 2019-08-22 martijn size_t *headerlen = &(message->signature.len);
744 06f6b49b 2019-04-06 martijn int headername = 1;
745 06f6b49b 2019-04-06 martijn char tag = '\0';
746 6840f1a1 2019-08-22 martijn char *sig = message->signature.signature;
748 06f6b49b 2019-04-06 martijn for (linelen = i = 0; sig[i] != '\0'; i++) {
749 ffa37923 2019-04-09 martijn if (sig[i] == '\r' && sig[i + 1] == '\n') {
751 06f6b49b 2019-04-06 martijn checkpoint = 0;
752 06f6b49b 2019-04-06 martijn linelen = 0;
753 06f6b49b 2019-04-06 martijn continue;
755 06f6b49b 2019-04-06 martijn if (sig[i] == '\t')
756 06f6b49b 2019-04-06 martijn linelen = (linelen + 8) & ~7;
758 06f6b49b 2019-04-06 martijn linelen++;
759 06f6b49b 2019-04-06 martijn if (headername) {
760 06f6b49b 2019-04-06 martijn if (sig[i] == ':') {
761 06f6b49b 2019-04-06 martijn headername = 0;
762 06f6b49b 2019-04-06 martijn checkpoint = i;
764 06f6b49b 2019-04-06 martijn continue;
766 ffa37923 2019-04-09 martijn if (linelen > DKIM_SIGNATURE_LINELEN && checkpoint != 0) {
767 06f6b49b 2019-04-06 martijn for (skip = checkpoint + 1;
768 06f6b49b 2019-04-06 martijn sig[skip] == ' ' || sig[skip] == '\t';
770 06f6b49b 2019-04-06 martijn continue;
771 06f6b49b 2019-04-06 martijn skip -= checkpoint + 1;
772 6840f1a1 2019-08-22 martijn if (!dkim_signature_need(message,
773 226e5da8 2019-04-08 martijn skip > 3 ? 0 : 3 - skip + 1))
774 06f6b49b 2019-04-06 martijn return 0;
775 6840f1a1 2019-08-22 martijn sig = message->signature.signature;
777 06f6b49b 2019-04-06 martijn memmove(sig + checkpoint + 3,
778 06f6b49b 2019-04-06 martijn sig + checkpoint + skip,
779 540f552a 2019-04-06 martijn *headerlen - skip - checkpoint + 1);
780 06f6b49b 2019-04-06 martijn sig[checkpoint + 1] = '\r';
781 06f6b49b 2019-04-06 martijn sig[checkpoint + 2] = '\n';
782 06f6b49b 2019-04-06 martijn sig[checkpoint + 3] = '\t';
783 06f6b49b 2019-04-06 martijn linelen = 8;
784 06f6b49b 2019-04-06 martijn *headerlen = *headerlen + 3 - skip;
785 06f6b49b 2019-04-06 martijn i = checkpoint + 3;
786 06f6b49b 2019-04-06 martijn checkpoint = 0;
788 06f6b49b 2019-04-06 martijn if (sig[i] == ';') {
789 06f6b49b 2019-04-06 martijn checkpoint = i;
790 06f6b49b 2019-04-06 martijn tag = '\0';
791 06f6b49b 2019-04-06 martijn continue;
793 06f6b49b 2019-04-06 martijn switch (tag) {
794 06f6b49b 2019-04-06 martijn case 'B':
795 06f6b49b 2019-04-06 martijn case 'b':
796 c826412d 2019-05-02 martijn case 'z':
797 06f6b49b 2019-04-06 martijn checkpoint = i;
799 06f6b49b 2019-04-06 martijn case 'h':
800 06f6b49b 2019-04-06 martijn if (sig[i] == ':')
801 06f6b49b 2019-04-06 martijn checkpoint = i;
804 06f6b49b 2019-04-06 martijn if (tag == '\0' && sig[i] != ' ' && sig[i] != '\t') {
805 06f6b49b 2019-04-06 martijn if ((tag = sig[i]) == 'b' && sig[i + 1] == 'h' &&
806 06f6b49b 2019-04-06 martijn sig[i + 2] == '=') {
807 06f6b49b 2019-04-06 martijn tag = 'B';
808 06f6b49b 2019-04-06 martijn linelen += 2;
811 06f6b49b 2019-04-06 martijn tag = sig[i];
814 06f6b49b 2019-04-06 martijn return 1;
818 6840f1a1 2019-08-22 martijn dkim_signature_printheader(struct dkim_message *message, const char *header)
820 c826412d 2019-05-02 martijn size_t i, j, len;
821 c826412d 2019-05-02 martijn static char *fmtheader = NULL;
822 c826412d 2019-05-02 martijn char *tmp;
823 c826412d 2019-05-02 martijn static size_t size = 0;
824 c826412d 2019-05-02 martijn int first;
826 c826412d 2019-05-02 martijn len = strlen(header);
827 c826412d 2019-05-02 martijn if ((len + 3) * 3 < len) {
828 c826412d 2019-05-02 martijn errno = EOVERFLOW;
829 6840f1a1 2019-08-22 martijn dkim_err(message, "Can't add z-component to header");
830 c826412d 2019-05-02 martijn return 0;
832 c826412d 2019-05-02 martijn if ((len + 3) * 3 > size) {
833 c826412d 2019-05-02 martijn if ((tmp = reallocarray(fmtheader, 3, len + 3)) == NULL) {
834 6840f1a1 2019-08-22 martijn dkim_err(message, "Can't add z-component to header");
835 c826412d 2019-05-02 martijn return 0;
837 c826412d 2019-05-02 martijn fmtheader = tmp;
838 c826412d 2019-05-02 martijn size = (len + 1) * 3;
841 6840f1a1 2019-08-22 martijn first = message->signature.signature[message->signature.len - 1] == '=';
842 c826412d 2019-05-02 martijn for (j = i = 0; header[i] != '\0'; i++, j++) {
843 c826412d 2019-05-02 martijn if (i == 0 && header[i] != ' ' && header[i] != '\t' && !first)
844 c826412d 2019-05-02 martijn fmtheader[j++] = '|';
845 c826412d 2019-05-02 martijn if ((header[i] >= 0x21 && header[i] <= 0x3A) ||
846 6840f1a1 2019-08-22 martijn (header[i] == 0x3C) ||
847 c826412d 2019-05-02 martijn (header[i] >= 0x3E && header[i] <= 0x7B) ||
848 c826412d 2019-05-02 martijn (header[i] >= 0x7D && header[i] <= 0x7E))
849 c826412d 2019-05-02 martijn fmtheader[j] = header[i];
851 c826412d 2019-05-02 martijn fmtheader[j++] = '=';
852 c826412d 2019-05-02 martijn (void) sprintf(fmtheader + j, "%02hhX", header[i]);
856 c826412d 2019-05-02 martijn (void) sprintf(fmtheader + j, "=%02hhX=%02hhX", (unsigned char) '\r',
857 c826412d 2019-05-02 martijn (unsigned char) '\n');
859 6840f1a1 2019-08-22 martijn return dkim_signature_printf(message, "%s", fmtheader);
863 6840f1a1 2019-08-22 martijn dkim_signature_printf(struct dkim_message *message, char *fmt, ...)
865 6840f1a1 2019-08-22 martijn struct dkim_signature *sig = &(message->signature);
866 06f6b49b 2019-04-06 martijn va_list ap;
867 06f6b49b 2019-04-06 martijn size_t len;
869 06f6b49b 2019-04-06 martijn va_start(ap, fmt);
870 06f6b49b 2019-04-06 martijn if ((len = vsnprintf(sig->signature + sig->len, sig->size - sig->len,
871 06f6b49b 2019-04-06 martijn fmt, ap)) >= sig->size - sig->len) {
872 06f6b49b 2019-04-06 martijn va_end(ap);
873 6840f1a1 2019-08-22 martijn if (!dkim_signature_need(message, len + 1))
874 06f6b49b 2019-04-06 martijn return 0;
875 06f6b49b 2019-04-06 martijn va_start(ap, fmt);
876 6840f1a1 2019-08-22 martijn if ((len = vsnprintf(sig->signature + sig->len,
877 6840f1a1 2019-08-22 martijn sig->size - sig->len, fmt, ap)) >= sig->size - sig->len)
878 6840f1a1 2019-08-22 martijn osmtpd_errx(1, "Miscalculated header size");
880 06f6b49b 2019-04-06 martijn sig->len += len;
881 06f6b49b 2019-04-06 martijn va_end(ap);
882 06f6b49b 2019-04-06 martijn return 1;
885 40cd76f4 2020-08-30 martijn const char *
886 40cd76f4 2020-08-30 martijn dkim_domain_select(struct dkim_message *message, char *from)
888 40cd76f4 2020-08-30 martijn char *mdomain0, *mdomain;
889 40cd76f4 2020-08-30 martijn size_t i;
891 40cd76f4 2020-08-30 martijn if ((mdomain = mdomain0 = osmtpd_mheader_from_domain(from)) == NULL) {
892 40cd76f4 2020-08-30 martijn if (errno != EINVAL) {
893 71b92702 2021-05-14 martijn dkim_errx(message, "Couldn't parse from header");
894 40cd76f4 2020-08-30 martijn return NULL;
896 40cd76f4 2020-08-30 martijn return NULL;
899 40cd76f4 2020-08-30 martijn while (mdomain != NULL && mdomain[0] != '\0') {
900 40cd76f4 2020-08-30 martijn for (i = 0; i < ndomains; i++) {
901 40cd76f4 2020-08-30 martijn if (strcasecmp(mdomain, domain[i]) == 0) {
902 40cd76f4 2020-08-30 martijn free(mdomain0);
903 40cd76f4 2020-08-30 martijn return domain[i];
906 40cd76f4 2020-08-30 martijn if ((mdomain = strchr(mdomain, '.')) != NULL)
907 40cd76f4 2020-08-30 martijn mdomain++;
909 40cd76f4 2020-08-30 martijn free(mdomain0);
910 40cd76f4 2020-08-30 martijn return NULL;
914 6840f1a1 2019-08-22 martijn dkim_signature_need(struct dkim_message *message, size_t len)
916 6840f1a1 2019-08-22 martijn struct dkim_signature *sig = &(message->signature);
917 06f6b49b 2019-04-06 martijn char *tmp;
919 a27b5ff7 2019-04-08 martijn if (sig->len + len < sig->size)
920 06f6b49b 2019-04-06 martijn return 1;
921 503597cc 2019-04-08 martijn sig->size = (((len + sig->len) / 512) + 1) * 512;
922 06f6b49b 2019-04-06 martijn if ((tmp = realloc(sig->signature, sig->size)) == NULL) {
923 6840f1a1 2019-08-22 martijn dkim_err(message, "No room for signature");
924 06f6b49b 2019-04-06 martijn return 0;
926 06f6b49b 2019-04-06 martijn sig->signature = tmp;
927 06f6b49b 2019-04-06 martijn return 1;
930 48c4bdc1 2019-04-04 martijn __dead void
931 48c4bdc1 2019-04-04 martijn usage(void)
933 fe371fc9 2019-09-05 martijn fprintf(stderr, "usage: filter-dkimsign [-tz] [-a signalg] "
934 fe371fc9 2019-09-05 martijn "[-c canonicalization] \n [-h headerfields]"
935 6840f1a1 2019-08-22 martijn "[-x seconds] -d domain -k keyfile -s selector\n");
936 48c4bdc1 2019-04-04 martijn exit(1);