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 9b92b3ea 2025-05-19 martijn #include <limits.h>
25 48c4bdc1 2019-04-04 martijn #include <stdio.h>
26 48c4bdc1 2019-04-04 martijn #include <stdlib.h>
27 48c4bdc1 2019-04-04 martijn #include <string.h>
28 48c4bdc1 2019-04-04 martijn #include <syslog.h>
29 48c4bdc1 2019-04-04 martijn #include <time.h>
30 48c4bdc1 2019-04-04 martijn #include <unistd.h>
32 babf5d5a 2021-05-11 martijn #include "openbsd-compat.h"
33 6840f1a1 2019-08-22 martijn #include "opensmtpd.h"
34 40cd76f4 2020-08-30 martijn #include "mheader.h"
36 06f6b49b 2019-04-06 martijn struct dkim_signature {
37 06f6b49b 2019-04-06 martijn char *signature;
38 06f6b49b 2019-04-06 martijn size_t size;
39 06f6b49b 2019-04-06 martijn size_t len;
42 6840f1a1 2019-08-22 martijn struct dkim_message {
43 48c4bdc1 2019-04-04 martijn FILE *origf;
44 48c4bdc1 2019-04-04 martijn int parsing_headers;
45 48c4bdc1 2019-04-04 martijn char **headers;
46 48c4bdc1 2019-04-04 martijn int lastheader;
47 48c4bdc1 2019-04-04 martijn size_t body_whitelines;
48 48c4bdc1 2019-04-04 martijn int has_body;
49 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 5fdffb49 2022-01-27 martijn void dkim_adddomain(char *);
103 48c4bdc1 2019-04-04 martijn void dkim_headers_set(char *);
104 f388b764 2025-05-19 kirill int dkim_dataline(struct osmtpd_ctx *, const char *);
105 6840f1a1 2019-08-22 martijn void *dkim_message_new(struct osmtpd_ctx *);
106 6840f1a1 2019-08-22 martijn void dkim_message_free(struct osmtpd_ctx *, void *);
107 6840f1a1 2019-08-22 martijn void dkim_parse_header(struct dkim_message *, char *, int);
108 6840f1a1 2019-08-22 martijn void dkim_parse_body(struct dkim_message *, char *);
109 6840f1a1 2019-08-22 martijn void dkim_sign(struct osmtpd_ctx *);
110 540788da 2025-02-08 martijn void dkim_signature_printheader(struct dkim_message *, const char *);
111 540788da 2025-02-08 martijn void dkim_signature_printf(struct dkim_message *, char *, ...)
112 06f6b49b 2019-04-06 martijn __attribute__((__format__ (printf, 2, 3)));
113 540788da 2025-02-08 martijn void dkim_signature_normalize(struct dkim_message *);
114 40cd76f4 2020-08-30 martijn const char *dkim_domain_select(struct dkim_message *, char *);
115 540788da 2025-02-08 martijn void dkim_signature_need(struct dkim_message *, size_t);
116 6840f1a1 2019-08-22 martijn int dkim_sign_init(struct dkim_message *);
119 48c4bdc1 2019-04-04 martijn main(int argc, char *argv[])
122 5fdffb49 2022-01-27 martijn FILE *file;
123 5fdffb49 2022-01-27 martijn char *line;
124 5fdffb49 2022-01-27 martijn size_t linesz;
125 5fdffb49 2022-01-27 martijn ssize_t linelen;
126 e974922c 2019-05-02 martijn const char *errstr;
128 5fdffb49 2022-01-27 martijn while ((ch = getopt(argc, argv, "a:c:D:d:h:k:s:tx:z")) != -1) {
129 48c4bdc1 2019-04-04 martijn switch (ch) {
130 48c4bdc1 2019-04-04 martijn case 'a':
131 6bcbc798 2021-05-16 martijn if (strncmp(optarg, "rsa-", 4) == 0) {
132 6bcbc798 2021-05-16 martijn cryptalg = "rsa";
133 6bcbc798 2021-05-16 martijn hashalg = optarg + 4;
134 6bcbc798 2021-05-16 martijn keyid = EVP_PKEY_RSA;
135 6bcbc798 2021-05-16 martijn sephash = 0;
136 33a7c297 2022-12-16 martijn #ifdef HAVE_ED25519
137 6bcbc798 2021-05-16 martijn } else if (strncmp(optarg, "ed25519-", 8) == 0) {
138 6bcbc798 2021-05-16 martijn hashalg = optarg + 8;
139 6bcbc798 2021-05-16 martijn cryptalg = "ed25519";
140 6bcbc798 2021-05-16 martijn keyid = EVP_PKEY_ED25519;
141 6bcbc798 2021-05-16 martijn sephash = 1;
144 6bcbc798 2021-05-16 martijn osmtpd_errx(1, "invalid algorithm");
146 48c4bdc1 2019-04-04 martijn case 'c':
147 48c4bdc1 2019-04-04 martijn if (strncmp(optarg, "simple", 6) == 0) {
148 48c4bdc1 2019-04-04 martijn canonheader = CANON_SIMPLE;
149 48c4bdc1 2019-04-04 martijn optarg += 6;
150 48c4bdc1 2019-04-04 martijn } else if (strncmp(optarg, "relaxed", 7) == 0) {
151 48c4bdc1 2019-04-04 martijn canonheader = CANON_RELAXED;
152 48c4bdc1 2019-04-04 martijn optarg += 7;
154 d581ea1c 2025-01-29 kirill osmtpd_errx(1, "Invalid canonicalization");
155 48c4bdc1 2019-04-04 martijn if (optarg[0] == '/') {
156 48c4bdc1 2019-04-04 martijn if (strcmp(optarg + 1, "simple") == 0)
157 48c4bdc1 2019-04-04 martijn canonbody = CANON_SIMPLE;
158 48c4bdc1 2019-04-04 martijn else if (strcmp(optarg + 1, "relaxed") == 0)
159 48c4bdc1 2019-04-04 martijn canonbody = CANON_RELAXED;
161 d581ea1c 2025-01-29 kirill osmtpd_errx(1,
162 6840f1a1 2019-08-22 martijn "Invalid canonicalization");
163 48c4bdc1 2019-04-04 martijn } else if (optarg[0] == '\0')
164 48c4bdc1 2019-04-04 martijn canonbody = CANON_SIMPLE;
166 d581ea1c 2025-01-29 kirill osmtpd_errx(1, "Invalid canonicalization");
168 5fdffb49 2022-01-27 martijn case 'D':
169 5fdffb49 2022-01-27 martijn if ((file = fopen(optarg, "r")) == NULL)
170 5fdffb49 2022-01-27 martijn osmtpd_err(1, "Can't open domain file (%s)",
171 5fdffb49 2022-01-27 martijn optarg);
173 5fdffb49 2022-01-27 martijn line = NULL;
174 5fdffb49 2022-01-27 martijn linesz = 0;
175 5fdffb49 2022-01-27 martijn linelen = getline(&line, &linesz, file);
176 5fdffb49 2022-01-27 martijn if (linelen > 0) {
177 5fdffb49 2022-01-27 martijn if (line[linelen - 1] == '\n')
178 5fdffb49 2022-01-27 martijn line[linelen - 1] = '\0';
179 3c3725bd 2025-01-27 kirill if (line[0] == '#')
180 3c3725bd 2025-01-27 kirill continue;
181 5fdffb49 2022-01-27 martijn dkim_adddomain(line);
183 5fdffb49 2022-01-27 martijn } while (linelen != -1);
184 5fdffb49 2022-01-27 martijn if (ferror(file))
185 5fdffb49 2022-01-27 martijn osmtpd_err(1, "Error reading domain file (%s)",
186 5fdffb49 2022-01-27 martijn optarg);
187 5fdffb49 2022-01-27 martijn fclose(file);
189 48c4bdc1 2019-04-04 martijn case 'd':
190 5fdffb49 2022-01-27 martijn dkim_adddomain(optarg);
192 06f6b49b 2019-04-06 martijn case 'h':
193 06f6b49b 2019-04-06 martijn dkim_headers_set(optarg);
195 06f6b49b 2019-04-06 martijn case 'k':
196 5fdffb49 2022-01-27 martijn if ((file = fopen(optarg, "r")) == NULL)
197 6840f1a1 2019-08-22 martijn osmtpd_err(1, "Can't open key file (%s)",
198 6840f1a1 2019-08-22 martijn optarg);
199 5fdffb49 2022-01-27 martijn pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL);
200 06f6b49b 2019-04-06 martijn if (pkey == NULL)
201 6840f1a1 2019-08-22 martijn osmtpd_errx(1, "Can't read key file");
202 5fdffb49 2022-01-27 martijn fclose(file);
204 06f6b49b 2019-04-06 martijn case 's':
205 06f6b49b 2019-04-06 martijn selector = optarg;
207 e7076cef 2019-05-02 martijn case 't':
208 e7076cef 2019-05-02 martijn addtime = 1;
210 e974922c 2019-05-02 martijn case 'x':
211 e974922c 2019-05-02 martijn addexpire = strtonum(optarg, 1, INT64_MAX, &errstr);
212 e974922c 2019-05-02 martijn if (addexpire == 0)
213 6840f1a1 2019-08-22 martijn osmtpd_errx(1, "Expire offset is %s", errstr);
215 c826412d 2019-05-02 martijn case 'z':
216 6840f1a1 2019-08-22 martijn addheaders++;
218 48c4bdc1 2019-04-04 martijn default:
219 48c4bdc1 2019-04-04 martijn usage();
223 fddd7e1e 2019-04-06 martijn OpenSSL_add_all_digests();
225 6840f1a1 2019-08-22 martijn if (pledge("tmppath stdio", NULL) == -1)
226 6840f1a1 2019-08-22 martijn osmtpd_err(1, "pledge");
228 6bcbc798 2021-05-16 martijn if ((hash_md = EVP_get_digestbyname(hashalg)) == NULL)
229 6bcbc798 2021-05-16 martijn osmtpd_errx(1, "Can't find hash: %s", hashalg);
231 06f6b49b 2019-04-06 martijn if (domain == NULL || selector == NULL || pkey == NULL)
232 48c4bdc1 2019-04-04 martijn usage();
234 6bcbc798 2021-05-16 martijn if (EVP_PKEY_id(pkey) != keyid)
235 6bcbc798 2021-05-16 martijn osmtpd_errx(1, "Key is not of type %s", cryptalg);
237 6840f1a1 2019-08-22 martijn osmtpd_register_filter_dataline(dkim_dataline);
238 6840f1a1 2019-08-22 martijn osmtpd_local_message(dkim_message_new, dkim_message_free);
239 6840f1a1 2019-08-22 martijn osmtpd_run();
241 48c4bdc1 2019-04-04 martijn return 0;
245 5fdffb49 2022-01-27 martijn dkim_adddomain(char *d)
247 5fdffb49 2022-01-27 martijn domain = reallocarray(domain, ndomains + 1, sizeof(*domain));
248 5fdffb49 2022-01-27 martijn if (domain == NULL)
249 e1300062 2025-04-08 kirill osmtpd_err(1, "reallocarray");
250 5fdffb49 2022-01-27 martijn domain[ndomains++] = d;
254 6840f1a1 2019-08-22 martijn dkim_dataline(struct osmtpd_ctx *ctx, const char *line)
256 6840f1a1 2019-08-22 martijn struct dkim_message *message = ctx->local_message;
257 6840f1a1 2019-08-22 martijn char *linedup;
258 48c4bdc1 2019-04-04 martijn size_t linelen;
260 48c4bdc1 2019-04-04 martijn linelen = strlen(line);
261 f388b764 2025-05-19 kirill if (fprintf(message->origf, "%s\n", line) < (int) linelen) {
262 9b92b3ea 2025-05-19 martijn osmtpd_warnx(ctx, "Couldn't write to tempfile");
263 f388b764 2025-05-19 kirill return -1;
266 7ecf4491 2019-05-01 martijn if (line[0] == '.' && line[1] =='\0') {
267 6840f1a1 2019-08-22 martijn dkim_sign(ctx);
268 6840f1a1 2019-08-22 martijn } else if (linelen != 0 && message->parsing_headers) {
269 f5286ea3 2019-04-08 martijn if (line[0] == '.')
271 6840f1a1 2019-08-22 martijn if ((linedup = strdup(line)) == NULL)
272 e1300062 2025-04-08 kirill osmtpd_err(1, "strdup");
273 6840f1a1 2019-08-22 martijn dkim_parse_header(message, linedup, 0);
274 6840f1a1 2019-08-22 martijn free(linedup);
275 6840f1a1 2019-08-22 martijn } else if (linelen == 0 && message->parsing_headers) {
276 540788da 2025-02-08 martijn if (addheaders > 0)
277 540788da 2025-02-08 martijn dkim_signature_printf(message, "; ");
278 6840f1a1 2019-08-22 martijn message->parsing_headers = 0;
279 f5286ea3 2019-04-08 martijn } else {
280 f5286ea3 2019-04-08 martijn if (line[0] == '.')
282 6840f1a1 2019-08-22 martijn if ((linedup = strdup(line)) == NULL)
283 e1300062 2025-04-08 kirill osmtpd_err(1, "strdup");
284 6840f1a1 2019-08-22 martijn dkim_parse_body(message, linedup);
285 6840f1a1 2019-08-22 martijn free(linedup);
288 f388b764 2025-05-19 kirill return 0;
292 6840f1a1 2019-08-22 martijn dkim_message_new(struct osmtpd_ctx *ctx)
294 6840f1a1 2019-08-22 martijn struct dkim_message *message;
296 41815bcc 2021-05-14 martijn if ((message = calloc(1, sizeof(*message))) == NULL) {
297 9b92b3ea 2025-05-19 martijn osmtpd_warn(ctx, "calloc");
298 41815bcc 2021-05-14 martijn return NULL;
301 6840f1a1 2019-08-22 martijn if ((message->origf = tmpfile()) == NULL) {
302 9b92b3ea 2025-05-19 martijn osmtpd_warn(ctx, "Failed to open tempfile");
303 41815bcc 2021-05-14 martijn goto fail;
305 6840f1a1 2019-08-22 martijn message->parsing_headers = 1;
307 6840f1a1 2019-08-22 martijn message->body_whitelines = 0;
308 6840f1a1 2019-08-22 martijn message->headers = calloc(1, sizeof(*(message->headers)));
309 9b92b3ea 2025-05-19 martijn if (message->headers == NULL) {
310 9b92b3ea 2025-05-19 martijn osmtpd_warn(ctx, "calloc");
311 9b92b3ea 2025-05-19 martijn goto fail;
313 6840f1a1 2019-08-22 martijn message->lastheader = 0;
314 6840f1a1 2019-08-22 martijn message->signature.signature = NULL;
315 6840f1a1 2019-08-22 martijn message->signature.size = 0;
316 6840f1a1 2019-08-22 martijn message->signature.len = 0;
318 540788da 2025-02-08 martijn dkim_signature_printf(message,
319 40cd76f4 2020-08-30 martijn "DKIM-Signature: v=%s; a=%s-%s; c=%s/%s; s=%s; ", "1",
320 fa10b9e8 2019-04-06 martijn cryptalg, hashalg,
321 06f6b49b 2019-04-06 martijn canonheader == CANON_SIMPLE ? "simple" : "relaxed",
322 540788da 2025-02-08 martijn canonbody == CANON_SIMPLE ? "simple" : "relaxed", selector);
323 540788da 2025-02-08 martijn if (addheaders > 0)
324 540788da 2025-02-08 martijn dkim_signature_printf(message, "z=");
326 9b92b3ea 2025-05-19 martijn if ((message->dctx = EVP_MD_CTX_new()) == NULL) {
327 9b92b3ea 2025-05-19 martijn osmtpd_warnx(ctx, "EVP_MD_CTX_new");
328 9b92b3ea 2025-05-19 martijn goto fail;
330 9b92b3ea 2025-05-19 martijn if (EVP_DigestInit_ex(message->dctx, hash_md, NULL) <= 0) {
331 9b92b3ea 2025-05-19 martijn osmtpd_warnx(ctx, "EVP_DigestInit_ex");
332 9b92b3ea 2025-05-19 martijn goto fail;
335 6840f1a1 2019-08-22 martijn return message;
337 500ea6d4 2022-01-27 martijn dkim_message_free(ctx, message);
338 41815bcc 2021-05-14 martijn return NULL;
342 6840f1a1 2019-08-22 martijn dkim_message_free(struct osmtpd_ctx *ctx, void *data)
344 6840f1a1 2019-08-22 martijn struct dkim_message *message = data;
345 48c4bdc1 2019-04-04 martijn size_t i;
347 6840f1a1 2019-08-22 martijn fclose(message->origf);
348 6bcbc798 2021-05-16 martijn EVP_MD_CTX_free(message->dctx);
349 6840f1a1 2019-08-22 martijn free(message->signature.signature);
350 500ea6d4 2022-01-27 martijn for (i = 0; message->headers != NULL &&
351 500ea6d4 2022-01-27 martijn message->headers[i] != NULL; i++)
352 6840f1a1 2019-08-22 martijn free(message->headers[i]);
353 6840f1a1 2019-08-22 martijn free(message->headers);
354 6840f1a1 2019-08-22 martijn free(message);
358 48c4bdc1 2019-04-04 martijn dkim_headers_set(char *headers)
360 48c4bdc1 2019-04-04 martijn size_t i;
361 48c4bdc1 2019-04-04 martijn int has_from = 0;
363 48c4bdc1 2019-04-04 martijn nsign_headers = 1;
365 48c4bdc1 2019-04-04 martijn for (i = 0; headers[i] != '\0'; i++) {
366 48c4bdc1 2019-04-04 martijn /* RFC 5322 field-name */
367 48c4bdc1 2019-04-04 martijn if (!(headers[i] >= 33 && headers[i] <= 126))
368 6840f1a1 2019-08-22 martijn osmtpd_errx(1, "-h: invalid character");
369 48c4bdc1 2019-04-04 martijn if (headers[i] == ':') {
370 48c4bdc1 2019-04-04 martijn /* Test for empty headers */
371 48c4bdc1 2019-04-04 martijn if (i == 0 || headers[i - 1] == ':')
372 6840f1a1 2019-08-22 martijn osmtpd_errx(1, "-h: header can't be empty");
373 48c4bdc1 2019-04-04 martijn nsign_headers++;
375 48c4bdc1 2019-04-04 martijn headers[i] = tolower(headers[i]);
377 48c4bdc1 2019-04-04 martijn if (headers[i - 1] == ':')
378 6840f1a1 2019-08-22 martijn osmtpd_errx(1, "-h: header can't be empty");
380 6840f1a1 2019-08-22 martijn if ((sign_headers = reallocarray(NULL, nsign_headers + 1,
381 6840f1a1 2019-08-22 martijn sizeof(*sign_headers))) == NULL)
382 e1300062 2025-04-08 kirill osmtpd_errx(1, "reallocarray");
384 48c4bdc1 2019-04-04 martijn for (i = 0; i < nsign_headers; i++) {
385 48c4bdc1 2019-04-04 martijn sign_headers[i] = headers;
386 48c4bdc1 2019-04-04 martijn if (i != nsign_headers - 1) {
387 48c4bdc1 2019-04-04 martijn headers = strchr(headers, ':');
388 48c4bdc1 2019-04-04 martijn headers++[0] = '\0';
390 48c4bdc1 2019-04-04 martijn if (strcasecmp(sign_headers[i], "from") == 0)
391 48c4bdc1 2019-04-04 martijn has_from = 1;
393 48c4bdc1 2019-04-04 martijn if (!has_from)
394 6840f1a1 2019-08-22 martijn osmtpd_errx(1, "From header must be included");
398 6840f1a1 2019-08-22 martijn dkim_parse_header(struct dkim_message *message, char *line, int force)
400 48c4bdc1 2019-04-04 martijn size_t i;
401 48c4bdc1 2019-04-04 martijn size_t r, w;
402 48c4bdc1 2019-04-04 martijn size_t linelen;
403 48c4bdc1 2019-04-04 martijn size_t lastheader;
404 06f6b49b 2019-04-06 martijn size_t hlen;
405 b6e37a5e 2019-04-08 martijn int fieldname = 0;
406 48c4bdc1 2019-04-04 martijn char **mtmp;
407 48c4bdc1 2019-04-04 martijn char *htmp;
408 63ac36ce 2019-04-08 martijn char *tmp;
410 540788da 2025-02-08 martijn if (addheaders == 2 && !force)
411 540788da 2025-02-08 martijn dkim_signature_printheader(message, line);
413 6840f1a1 2019-08-22 martijn if ((line[0] == ' ' || line[0] == '\t') && !message->lastheader)
415 48c4bdc1 2019-04-04 martijn if ((line[0] != ' ' && line[0] != '\t')) {
416 6840f1a1 2019-08-22 martijn message->lastheader = 0;
417 48c4bdc1 2019-04-04 martijn for (i = 0; i < nsign_headers; i++) {
418 06f6b49b 2019-04-06 martijn hlen = strlen(sign_headers[i]);
419 06f6b49b 2019-04-06 martijn if (strncasecmp(line, sign_headers[i], hlen) == 0) {
420 06f6b49b 2019-04-06 martijn while (line[hlen] == ' ' || line[hlen] == '\t')
422 06f6b49b 2019-04-06 martijn if (line[hlen] != ':')
423 06f6b49b 2019-04-06 martijn continue;
427 06f6b49b 2019-04-06 martijn if (i == nsign_headers && !force)
431 540788da 2025-02-08 martijn if (addheaders == 1 && !force)
432 540788da 2025-02-08 martijn dkim_signature_printheader(message, line);
434 48c4bdc1 2019-04-04 martijn if (canonheader == CANON_RELAXED) {
435 6840f1a1 2019-08-22 martijn if (!message->lastheader)
436 b6e37a5e 2019-04-08 martijn fieldname = 1;
437 48c4bdc1 2019-04-04 martijn for (r = w = 0; line[r] != '\0'; r++) {
438 5d06ccad 2019-04-08 martijn if (line[r] == ':' && fieldname) {
439 cd6f9b03 2019-10-23 martijn if (w > 0 && line[w - 1] == ' ')
440 48c4bdc1 2019-04-04 martijn line[w - 1] = ':';
442 48c4bdc1 2019-04-04 martijn line[w++] = ':';
443 48c4bdc1 2019-04-04 martijn fieldname = 0;
444 48c4bdc1 2019-04-04 martijn while (line[r + 1] == ' ' ||
445 48c4bdc1 2019-04-04 martijn line[r + 1] == '\t')
447 48c4bdc1 2019-04-04 martijn continue;
449 06f6b49b 2019-04-06 martijn if (line[r] == ' ' || line[r] == '\t' ||
450 06f6b49b 2019-04-06 martijn line[r] == '\r' || line[r] == '\n') {
451 cd6f9b03 2019-10-23 martijn if (r != 0 && w != 0 && line[w - 1] == ' ')
452 48c4bdc1 2019-04-04 martijn continue;
454 48c4bdc1 2019-04-04 martijn line[w++] = ' ';
455 48c4bdc1 2019-04-04 martijn } else if (fieldname) {
456 48c4bdc1 2019-04-04 martijn line[w++] = tolower(line[r]);
457 48c4bdc1 2019-04-04 martijn continue;
459 48c4bdc1 2019-04-04 martijn line[w++] = line[r];
461 cd6f9b03 2019-10-23 martijn linelen = (w != 0 && line[w - 1] == ' ') ? w - 1 : w;
462 48c4bdc1 2019-04-04 martijn line[linelen] = '\0';
464 48c4bdc1 2019-04-04 martijn linelen = strlen(line);
466 6840f1a1 2019-08-22 martijn for (lastheader = 0; message->headers[lastheader] != NULL; lastheader++)
467 48c4bdc1 2019-04-04 martijn continue;
468 6840f1a1 2019-08-22 martijn if (!message->lastheader) {
469 6840f1a1 2019-08-22 martijn mtmp = recallocarray(message->headers, lastheader + 1,
470 a53d74c4 2019-04-08 martijn lastheader + 2, sizeof(*mtmp));
471 d581ea1c 2025-01-29 kirill if (mtmp == NULL)
472 e1300062 2025-04-08 kirill osmtpd_err(1, "reallocarray");
473 6840f1a1 2019-08-22 martijn message->headers = mtmp;
475 d581ea1c 2025-01-29 kirill if ((message->headers[lastheader] = strdup(line)) == NULL)
476 e1300062 2025-04-08 kirill osmtpd_err(1, "strdup");
477 6840f1a1 2019-08-22 martijn message->headers[lastheader + 1 ] = NULL;
478 6840f1a1 2019-08-22 martijn message->lastheader = 1;
479 48c4bdc1 2019-04-04 martijn } else {
480 48c4bdc1 2019-04-04 martijn lastheader--;
481 6840f1a1 2019-08-22 martijn linelen += strlen(message->headers[lastheader]);
482 48c4bdc1 2019-04-04 martijn if (canonheader == CANON_SIMPLE)
483 48c4bdc1 2019-04-04 martijn linelen += 2;
484 48c4bdc1 2019-04-04 martijn linelen++;
485 6840f1a1 2019-08-22 martijn htmp = reallocarray(message->headers[lastheader], linelen,
486 48c4bdc1 2019-04-04 martijn sizeof(*htmp));
487 d581ea1c 2025-01-29 kirill if (htmp == NULL)
488 e1300062 2025-04-08 kirill osmtpd_err(1, "reallocarray");
489 6840f1a1 2019-08-22 martijn message->headers[lastheader] = htmp;
490 48c4bdc1 2019-04-04 martijn if (canonheader == CANON_SIMPLE) {
491 9b92b3ea 2025-05-19 martijn (void)strlcat(htmp, "\r\n", linelen);
492 63ac36ce 2019-04-08 martijn } else if (canonheader == CANON_RELAXED &&
493 6840f1a1 2019-08-22 martijn (tmp = strchr(message->headers[lastheader], ':')) != NULL &&
494 63ac36ce 2019-04-08 martijn tmp[1] == '\0')
497 9b92b3ea 2025-05-19 martijn (void)strlcat(htmp, line, linelen);
502 6840f1a1 2019-08-22 martijn dkim_parse_body(struct dkim_message *message, char *line)
504 48c4bdc1 2019-04-04 martijn size_t r, w;
505 48c4bdc1 2019-04-04 martijn size_t linelen;
507 1308710c 2019-04-09 martijn if (canonbody == CANON_RELAXED) {
508 1308710c 2019-04-09 martijn for (r = w = 0; line[r] != '\0'; r++) {
509 1308710c 2019-04-09 martijn if (line[r] == ' ' || line[r] == '\t') {
510 1308710c 2019-04-09 martijn if (r != 0 && line[w - 1] == ' ')
511 1308710c 2019-04-09 martijn continue;
513 1308710c 2019-04-09 martijn line[w++] = ' ';
515 1308710c 2019-04-09 martijn line[w++] = line[r];
517 cd6f9b03 2019-10-23 martijn linelen = (w != 0 && line[w - 1] == ' ') ? w - 1 : w;
518 1308710c 2019-04-09 martijn line[linelen] = '\0';
520 1308710c 2019-04-09 martijn linelen = strlen(line);
522 48c4bdc1 2019-04-04 martijn if (line[0] == '\0') {
523 6840f1a1 2019-08-22 martijn message->body_whitelines++;
527 6840f1a1 2019-08-22 martijn while (message->body_whitelines--) {
528 d581ea1c 2025-01-29 kirill if (EVP_DigestUpdate(message->dctx, "\r\n", 2) == 0)
529 d581ea1c 2025-01-29 kirill osmtpd_errx(1, "EVP_DigestUpdate");
531 6840f1a1 2019-08-22 martijn message->body_whitelines = 0;
532 6840f1a1 2019-08-22 martijn message->has_body = 1;
534 6bcbc798 2021-05-16 martijn if (EVP_DigestUpdate(message->dctx, line, linelen) == 0 ||
535 d581ea1c 2025-01-29 kirill EVP_DigestUpdate(message->dctx, "\r\n", 2) == 0)
536 d581ea1c 2025-01-29 kirill osmtpd_errx(1, "EVP_DigestUpdate");
540 6840f1a1 2019-08-22 martijn dkim_sign(struct osmtpd_ctx *ctx)
542 6840f1a1 2019-08-22 martijn struct dkim_message *message = ctx->local_message;
543 365862b1 2019-05-02 martijn /* Use largest hash size here */
544 8e76d341 2021-06-02 martijn unsigned char bdigest[EVP_MAX_MD_SIZE];
545 8e76d341 2021-06-02 martijn unsigned char digest[(((sizeof(bdigest) + 2) / 3) * 4) + 1];
546 8e76d341 2021-06-02 martijn unsigned char *b;
547 40cd76f4 2020-08-30 martijn const char *sdomain = domain[0], *tsdomain;
548 e974922c 2019-05-02 martijn time_t now;
549 6840f1a1 2019-08-22 martijn ssize_t i;
550 6bcbc798 2021-05-16 martijn size_t linelen = 0;
551 365862b1 2019-05-02 martijn char *tmp, *tmp2;
552 8e76d341 2021-06-02 martijn unsigned int digestsz;
554 e974922c 2019-05-02 martijn if (addtime || addexpire)
555 e974922c 2019-05-02 martijn now = time(NULL);
556 540788da 2025-02-08 martijn if (addtime)
557 540788da 2025-02-08 martijn dkim_signature_printf(message, "t=%lld; ", (long long)now);
558 540788da 2025-02-08 martijn if (addexpire != 0)
559 540788da 2025-02-08 martijn dkim_signature_printf(message, "x=%lld; ",
560 9b92b3ea 2025-05-19 martijn now + addexpire < now ? LLONG_MAX : now + addexpire);
562 6840f1a1 2019-08-22 martijn if (canonbody == CANON_SIMPLE && !message->has_body) {
563 d581ea1c 2025-01-29 kirill if (EVP_DigestUpdate(message->dctx, "\r\n", 2) <= 0)
564 d581ea1c 2025-01-29 kirill osmtpd_errx(1, "EVP_DigestUpdate");
566 d581ea1c 2025-01-29 kirill if (EVP_DigestFinal_ex(message->dctx, bdigest, &digestsz) == 0)
567 d581ea1c 2025-01-29 kirill osmtpd_errx(1, "EVP_DigestFinal_ex");
568 6bcbc798 2021-05-16 martijn EVP_EncodeBlock(digest, bdigest, digestsz);
569 540788da 2025-02-08 martijn dkim_signature_printf(message, "bh=%s; h=", digest);
570 365862b1 2019-05-02 martijn /* Reverse order for ease of use of RFC6367 section 5.4.2 */
571 6840f1a1 2019-08-22 martijn for (i = 0; message->headers[i] != NULL; i++)
572 365862b1 2019-05-02 martijn continue;
573 6bcbc798 2021-05-16 martijn EVP_MD_CTX_reset(message->dctx);
574 6bcbc798 2021-05-16 martijn if (!sephash) {
575 6bcbc798 2021-05-16 martijn if (EVP_DigestSignInit(message->dctx, NULL, hash_md, NULL,
576 d581ea1c 2025-01-29 kirill pkey) != 1)
577 d581ea1c 2025-01-29 kirill osmtpd_errx(1, "EVP_DigestSignInit");
578 6bcbc798 2021-05-16 martijn } else {
579 d581ea1c 2025-01-29 kirill if (EVP_DigestInit_ex(message->dctx, hash_md, NULL) != 1)
580 d581ea1c 2025-01-29 kirill osmtpd_errx(1, "EVP_DigestInit_ex");
582 365862b1 2019-05-02 martijn for (i--; i >= 0; i--) {
583 6bcbc798 2021-05-16 martijn if (!sephash) {
584 6bcbc798 2021-05-16 martijn if (EVP_DigestSignUpdate(message->dctx,
585 6bcbc798 2021-05-16 martijn message->headers[i],
586 6bcbc798 2021-05-16 martijn strlen(message->headers[i])) != 1 ||
587 6bcbc798 2021-05-16 martijn EVP_DigestSignUpdate(message->dctx, "\r\n",
589 d581ea1c 2025-01-29 kirill osmtpd_errx(1, "EVP_DigestSignUpdate");
590 6bcbc798 2021-05-16 martijn } else {
591 6bcbc798 2021-05-16 martijn if (EVP_DigestUpdate(message->dctx, message->headers[i],
592 6bcbc798 2021-05-16 martijn strlen(message->headers[i])) != 1 ||
593 d581ea1c 2025-01-29 kirill EVP_DigestUpdate(message->dctx, "\r\n", 2) <= 0)
594 d581ea1c 2025-01-29 kirill osmtpd_errx(1, "EVP_DigestSignUpdate");
596 40cd76f4 2020-08-30 martijn if ((tsdomain = dkim_domain_select(message, message->headers[i])) != NULL)
597 40cd76f4 2020-08-30 martijn sdomain = tsdomain;
598 365862b1 2019-05-02 martijn /* We're done with the cached header after hashing */
599 6840f1a1 2019-08-22 martijn for (tmp = message->headers[i]; tmp[0] != ':'; tmp++) {
600 365862b1 2019-05-02 martijn if (tmp[0] == ' ' || tmp[0] == '\t')
602 365862b1 2019-05-02 martijn tmp[0] = tolower(tmp[0]);
604 365862b1 2019-05-02 martijn tmp[0] = '\0';
605 540788da 2025-02-08 martijn dkim_signature_printf(message, "%s%s",
606 6840f1a1 2019-08-22 martijn message->headers[i + 1] == NULL ? "" : ":",
607 540788da 2025-02-08 martijn message->headers[i]);
609 40cd76f4 2020-08-30 martijn dkim_signature_printf(message, "; d=%s; b=", sdomain);
610 540788da 2025-02-08 martijn dkim_signature_normalize(message);
611 d581ea1c 2025-01-29 kirill if ((tmp = strdup(message->signature.signature)) == NULL)
612 e1300062 2025-04-08 kirill osmtpd_err(1, "strdup");
613 6840f1a1 2019-08-22 martijn dkim_parse_header(message, tmp, 1);
614 6bcbc798 2021-05-16 martijn if (!sephash) {
615 6bcbc798 2021-05-16 martijn if (EVP_DigestSignUpdate(message->dctx, tmp,
616 d581ea1c 2025-01-29 kirill strlen(tmp)) != 1)
617 d581ea1c 2025-01-29 kirill osmtpd_errx(1, "EVP_DigestSignUpdate");
618 6bcbc798 2021-05-16 martijn } else {
619 d581ea1c 2025-01-29 kirill if (EVP_DigestUpdate(message->dctx, tmp, strlen(tmp)) != 1)
620 d581ea1c 2025-01-29 kirill osmtpd_errx(1, "EVP_DigestUpdate");
622 365862b1 2019-05-02 martijn free(tmp);
623 6bcbc798 2021-05-16 martijn if (!sephash) {
624 d581ea1c 2025-01-29 kirill if (EVP_DigestSignFinal(message->dctx, NULL, &linelen) != 1)
625 d581ea1c 2025-01-29 kirill osmtpd_errx(1, "EVP_DigestSignFinal");
626 33a7c297 2022-12-16 martijn #ifdef HAVE_ED25519
627 6bcbc798 2021-05-16 martijn } else {
628 6bcbc798 2021-05-16 martijn if (EVP_DigestFinal_ex(message->dctx, bdigest,
629 d581ea1c 2025-01-29 kirill &digestsz) != 1)
630 d581ea1c 2025-01-29 kirill osmtpd_errx(1, "EVP_DigestFinal_ex");
631 6bcbc798 2021-05-16 martijn EVP_MD_CTX_reset(message->dctx);
632 6bcbc798 2021-05-16 martijn if (EVP_DigestSignInit(message->dctx, NULL, NULL, NULL,
633 d581ea1c 2025-01-29 kirill pkey) != 1)
634 d581ea1c 2025-01-29 kirill osmtpd_errx(1, "EVP_DigestSignInit");
635 6bcbc798 2021-05-16 martijn if (EVP_DigestSign(message->dctx, NULL, &linelen, bdigest,
636 d581ea1c 2025-01-29 kirill digestsz) != 1)
637 d581ea1c 2025-01-29 kirill osmtpd_errx(1, "EVP_DigestSign");
640 d581ea1c 2025-01-29 kirill if ((tmp = malloc(linelen)) == NULL)
641 e1300062 2025-04-08 kirill osmtpd_err(1, "malloc");
642 6bcbc798 2021-05-16 martijn if (!sephash) {
643 d581ea1c 2025-01-29 kirill if (EVP_DigestSignFinal(message->dctx, tmp, &linelen) != 1)
644 d581ea1c 2025-01-29 kirill osmtpd_errx(1, "EVP_DigestSignFinal");
645 33a7c297 2022-12-16 martijn #ifdef HAVE_ED25519
646 6bcbc798 2021-05-16 martijn } else {
647 6bcbc798 2021-05-16 martijn if (EVP_DigestSign(message->dctx, tmp, &linelen, bdigest,
648 d581ea1c 2025-01-29 kirill digestsz) != 1)
649 d581ea1c 2025-01-29 kirill osmtpd_errx(1, "EVP_DigestSign");
652 d581ea1c 2025-01-29 kirill if ((b = malloc((((linelen + 2) / 3) * 4) + 1)) == NULL)
653 e1300062 2025-04-08 kirill osmtpd_err(1, "malloc");
654 365862b1 2019-05-02 martijn EVP_EncodeBlock(b, tmp, linelen);
655 365862b1 2019-05-02 martijn free(tmp);
656 6840f1a1 2019-08-22 martijn dkim_signature_printf(message, "%s\r\n", b);
657 365862b1 2019-05-02 martijn free(b);
658 6840f1a1 2019-08-22 martijn dkim_signature_normalize(message);
659 6840f1a1 2019-08-22 martijn tmp = message->signature.signature;
660 365862b1 2019-05-02 martijn while ((tmp2 = strchr(tmp, '\r')) != NULL) {
661 365862b1 2019-05-02 martijn tmp2[0] = '\0';
662 6840f1a1 2019-08-22 martijn osmtpd_filter_dataline(ctx, "%s", tmp);
663 365862b1 2019-05-02 martijn tmp = tmp2 + 2;
665 365862b1 2019-05-02 martijn tmp = NULL;
666 365862b1 2019-05-02 martijn linelen = 0;
667 6840f1a1 2019-08-22 martijn rewind(message->origf);
668 6840f1a1 2019-08-22 martijn while ((i = getline(&tmp, &linelen, message->origf)) != -1) {
669 365862b1 2019-05-02 martijn tmp[i - 1] = '\0';
670 6840f1a1 2019-08-22 martijn osmtpd_filter_dataline(ctx, "%s", tmp);
672 be6d950d 2021-03-21 martijn free(tmp);
677 6840f1a1 2019-08-22 martijn dkim_signature_normalize(struct dkim_message *message)
679 06f6b49b 2019-04-06 martijn size_t i;
680 06f6b49b 2019-04-06 martijn size_t linelen;
681 06f6b49b 2019-04-06 martijn size_t checkpoint;
682 06f6b49b 2019-04-06 martijn size_t skip;
683 6840f1a1 2019-08-22 martijn size_t *headerlen = &(message->signature.len);
684 06f6b49b 2019-04-06 martijn int headername = 1;
685 06f6b49b 2019-04-06 martijn char tag = '\0';
686 6840f1a1 2019-08-22 martijn char *sig = message->signature.signature;
688 06f6b49b 2019-04-06 martijn for (linelen = i = 0; sig[i] != '\0'; i++) {
689 ffa37923 2019-04-09 martijn if (sig[i] == '\r' && sig[i + 1] == '\n') {
691 06f6b49b 2019-04-06 martijn checkpoint = 0;
692 06f6b49b 2019-04-06 martijn linelen = 0;
693 06f6b49b 2019-04-06 martijn continue;
695 06f6b49b 2019-04-06 martijn if (sig[i] == '\t')
696 06f6b49b 2019-04-06 martijn linelen = (linelen + 8) & ~7;
698 06f6b49b 2019-04-06 martijn linelen++;
699 06f6b49b 2019-04-06 martijn if (headername) {
700 06f6b49b 2019-04-06 martijn if (sig[i] == ':') {
701 06f6b49b 2019-04-06 martijn headername = 0;
702 06f6b49b 2019-04-06 martijn checkpoint = i;
704 06f6b49b 2019-04-06 martijn continue;
706 ffa37923 2019-04-09 martijn if (linelen > DKIM_SIGNATURE_LINELEN && checkpoint != 0) {
707 06f6b49b 2019-04-06 martijn for (skip = checkpoint + 1;
708 06f6b49b 2019-04-06 martijn sig[skip] == ' ' || sig[skip] == '\t';
710 06f6b49b 2019-04-06 martijn continue;
711 06f6b49b 2019-04-06 martijn skip -= checkpoint + 1;
712 540788da 2025-02-08 martijn dkim_signature_need(message,
713 540788da 2025-02-08 martijn skip > 3 ? 0 : 3 - skip + 1);
714 6840f1a1 2019-08-22 martijn sig = message->signature.signature;
716 06f6b49b 2019-04-06 martijn memmove(sig + checkpoint + 3,
717 06f6b49b 2019-04-06 martijn sig + checkpoint + skip,
718 540f552a 2019-04-06 martijn *headerlen - skip - checkpoint + 1);
719 06f6b49b 2019-04-06 martijn sig[checkpoint + 1] = '\r';
720 06f6b49b 2019-04-06 martijn sig[checkpoint + 2] = '\n';
721 06f6b49b 2019-04-06 martijn sig[checkpoint + 3] = '\t';
722 06f6b49b 2019-04-06 martijn linelen = 8;
723 06f6b49b 2019-04-06 martijn *headerlen = *headerlen + 3 - skip;
724 06f6b49b 2019-04-06 martijn i = checkpoint + 3;
725 06f6b49b 2019-04-06 martijn checkpoint = 0;
727 06f6b49b 2019-04-06 martijn if (sig[i] == ';') {
728 06f6b49b 2019-04-06 martijn checkpoint = i;
729 06f6b49b 2019-04-06 martijn tag = '\0';
730 06f6b49b 2019-04-06 martijn continue;
732 06f6b49b 2019-04-06 martijn switch (tag) {
733 06f6b49b 2019-04-06 martijn case 'B':
734 06f6b49b 2019-04-06 martijn case 'b':
735 c826412d 2019-05-02 martijn case 'z':
736 06f6b49b 2019-04-06 martijn checkpoint = i;
738 06f6b49b 2019-04-06 martijn case 'h':
739 06f6b49b 2019-04-06 martijn if (sig[i] == ':')
740 06f6b49b 2019-04-06 martijn checkpoint = i;
743 06f6b49b 2019-04-06 martijn if (tag == '\0' && sig[i] != ' ' && sig[i] != '\t') {
744 06f6b49b 2019-04-06 martijn if ((tag = sig[i]) == 'b' && sig[i + 1] == 'h' &&
745 06f6b49b 2019-04-06 martijn sig[i + 2] == '=') {
746 06f6b49b 2019-04-06 martijn tag = 'B';
747 06f6b49b 2019-04-06 martijn linelen += 2;
750 06f6b49b 2019-04-06 martijn tag = sig[i];
756 c08ad6cc 2025-02-08 martijn dkim_signature_printheader(struct dkim_message *message, const char *line)
758 82ec5c6f 2025-02-08 martijn size_t i;
759 c826412d 2019-05-02 martijn int first;
761 6840f1a1 2019-08-22 martijn first = message->signature.signature[message->signature.len - 1] == '=';
762 82ec5c6f 2025-02-08 martijn for (i = 0; line[i] != '\0'; i++) {
763 c08ad6cc 2025-02-08 martijn if (i == 0 && line[i] != ' ' && line[i] != '\t' && !first)
764 540788da 2025-02-08 martijn dkim_signature_printf(message, "|");
765 c08ad6cc 2025-02-08 martijn if ((line[i] >= 0x21 && line[i] <= 0x3A) ||
766 c08ad6cc 2025-02-08 martijn (line[i] == 0x3C) ||
767 c08ad6cc 2025-02-08 martijn (line[i] >= 0x3E && line[i] <= 0x7B) ||
768 82ec5c6f 2025-02-08 martijn (line[i] >= 0x7D && line[i] <= 0x7E)) {
769 540788da 2025-02-08 martijn dkim_signature_printf(message, "%c", line[i]);
771 540788da 2025-02-08 martijn dkim_signature_printf(message, "=%02hhX", line[i]);
773 540788da 2025-02-08 martijn dkim_signature_printf(message, "=%02hhX=%02hhX", '\r', '\n');
777 6840f1a1 2019-08-22 martijn dkim_signature_printf(struct dkim_message *message, char *fmt, ...)
779 6840f1a1 2019-08-22 martijn struct dkim_signature *sig = &(message->signature);
780 06f6b49b 2019-04-06 martijn va_list ap;
781 06f6b49b 2019-04-06 martijn size_t len;
783 06f6b49b 2019-04-06 martijn va_start(ap, fmt);
784 06f6b49b 2019-04-06 martijn if ((len = vsnprintf(sig->signature + sig->len, sig->size - sig->len,
785 06f6b49b 2019-04-06 martijn fmt, ap)) >= sig->size - sig->len) {
786 06f6b49b 2019-04-06 martijn va_end(ap);
787 540788da 2025-02-08 martijn dkim_signature_need(message, len + 1);
788 06f6b49b 2019-04-06 martijn va_start(ap, fmt);
789 6840f1a1 2019-08-22 martijn if ((len = vsnprintf(sig->signature + sig->len,
790 6840f1a1 2019-08-22 martijn sig->size - sig->len, fmt, ap)) >= sig->size - sig->len)
791 6840f1a1 2019-08-22 martijn osmtpd_errx(1, "Miscalculated header size");
793 06f6b49b 2019-04-06 martijn sig->len += len;
794 06f6b49b 2019-04-06 martijn va_end(ap);
797 40cd76f4 2020-08-30 martijn const char *
798 40cd76f4 2020-08-30 martijn dkim_domain_select(struct dkim_message *message, char *from)
800 40cd76f4 2020-08-30 martijn char *mdomain0, *mdomain;
801 40cd76f4 2020-08-30 martijn size_t i;
803 d581ea1c 2025-01-29 kirill if ((mdomain = mdomain0 = osmtpd_mheader_from_domain(from)) == NULL)
804 40cd76f4 2020-08-30 martijn return NULL;
806 40cd76f4 2020-08-30 martijn while (mdomain != NULL && mdomain[0] != '\0') {
807 40cd76f4 2020-08-30 martijn for (i = 0; i < ndomains; i++) {
808 40cd76f4 2020-08-30 martijn if (strcasecmp(mdomain, domain[i]) == 0) {
809 40cd76f4 2020-08-30 martijn free(mdomain0);
810 40cd76f4 2020-08-30 martijn return domain[i];
813 40cd76f4 2020-08-30 martijn if ((mdomain = strchr(mdomain, '.')) != NULL)
814 40cd76f4 2020-08-30 martijn mdomain++;
816 40cd76f4 2020-08-30 martijn free(mdomain0);
817 40cd76f4 2020-08-30 martijn return NULL;
821 6840f1a1 2019-08-22 martijn dkim_signature_need(struct dkim_message *message, size_t len)
823 6840f1a1 2019-08-22 martijn struct dkim_signature *sig = &(message->signature);
824 06f6b49b 2019-04-06 martijn char *tmp;
826 a27b5ff7 2019-04-08 martijn if (sig->len + len < sig->size)
828 503597cc 2019-04-08 martijn sig->size = (((len + sig->len) / 512) + 1) * 512;
829 d581ea1c 2025-01-29 kirill if ((tmp = realloc(sig->signature, sig->size)) == NULL)
830 e1300062 2025-04-08 kirill osmtpd_err(1, "malloc");
831 06f6b49b 2019-04-06 martijn sig->signature = tmp;
834 48c4bdc1 2019-04-04 martijn __dead void
835 48c4bdc1 2019-04-04 martijn usage(void)
837 fe371fc9 2019-09-05 martijn fprintf(stderr, "usage: filter-dkimsign [-tz] [-a signalg] "
838 fe371fc9 2019-09-05 martijn "[-c canonicalization] \n [-h headerfields]"
839 5fdffb49 2022-01-27 martijn "[-x seconds] -D file -d domain -k keyfile -s selector\n");
840 48c4bdc1 2019-04-04 martijn exit(1);