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 41815bcc 2021-05-14 martijn free(message->headers);
325 6bcbc798 2021-05-16 martijn EVP_MD_CTX_free(message->dctx);
326 41815bcc 2021-05-14 martijn free(message);
327 41815bcc 2021-05-14 martijn return NULL;
331 6840f1a1 2019-08-22 martijn dkim_message_free(struct osmtpd_ctx *ctx, void *data)
333 6840f1a1 2019-08-22 martijn struct dkim_message *message = data;
334 48c4bdc1 2019-04-04 martijn size_t i;
336 6840f1a1 2019-08-22 martijn fclose(message->origf);
337 6bcbc798 2021-05-16 martijn EVP_MD_CTX_free(message->dctx);
338 6840f1a1 2019-08-22 martijn free(message->signature.signature);
339 6840f1a1 2019-08-22 martijn for (i = 0; message->headers[i] != NULL; i++)
340 6840f1a1 2019-08-22 martijn free(message->headers[i]);
341 6840f1a1 2019-08-22 martijn free(message->headers);
342 6840f1a1 2019-08-22 martijn free(message);
346 48c4bdc1 2019-04-04 martijn dkim_headers_set(char *headers)
348 48c4bdc1 2019-04-04 martijn size_t i;
349 48c4bdc1 2019-04-04 martijn int has_from = 0;
351 48c4bdc1 2019-04-04 martijn nsign_headers = 1;
353 48c4bdc1 2019-04-04 martijn for (i = 0; headers[i] != '\0'; i++) {
354 48c4bdc1 2019-04-04 martijn /* RFC 5322 field-name */
355 48c4bdc1 2019-04-04 martijn if (!(headers[i] >= 33 && headers[i] <= 126))
356 6840f1a1 2019-08-22 martijn osmtpd_errx(1, "-h: invalid character");
357 48c4bdc1 2019-04-04 martijn if (headers[i] == ':') {
358 48c4bdc1 2019-04-04 martijn /* Test for empty headers */
359 48c4bdc1 2019-04-04 martijn if (i == 0 || headers[i - 1] == ':')
360 6840f1a1 2019-08-22 martijn osmtpd_errx(1, "-h: header can't be empty");
361 48c4bdc1 2019-04-04 martijn nsign_headers++;
363 48c4bdc1 2019-04-04 martijn headers[i] = tolower(headers[i]);
365 48c4bdc1 2019-04-04 martijn if (headers[i - 1] == ':')
366 6840f1a1 2019-08-22 martijn osmtpd_errx(1, "-h: header can't be empty");
368 6840f1a1 2019-08-22 martijn if ((sign_headers = reallocarray(NULL, nsign_headers + 1,
369 6840f1a1 2019-08-22 martijn sizeof(*sign_headers))) == NULL)
370 6840f1a1 2019-08-22 martijn osmtpd_errx(1, NULL);
372 48c4bdc1 2019-04-04 martijn for (i = 0; i < nsign_headers; i++) {
373 48c4bdc1 2019-04-04 martijn sign_headers[i] = headers;
374 48c4bdc1 2019-04-04 martijn if (i != nsign_headers - 1) {
375 48c4bdc1 2019-04-04 martijn headers = strchr(headers, ':');
376 48c4bdc1 2019-04-04 martijn headers++[0] = '\0';
378 48c4bdc1 2019-04-04 martijn if (strcasecmp(sign_headers[i], "from") == 0)
379 48c4bdc1 2019-04-04 martijn has_from = 1;
381 48c4bdc1 2019-04-04 martijn if (!has_from)
382 6840f1a1 2019-08-22 martijn osmtpd_errx(1, "From header must be included");
386 6840f1a1 2019-08-22 martijn dkim_err(struct dkim_message *message, char *msg)
388 6840f1a1 2019-08-22 martijn message->err = 1;
389 803cdd74 2020-07-25 martijn fprintf(stderr, "%s: %s\n", msg, strerror(errno));
393 6840f1a1 2019-08-22 martijn dkim_errx(struct dkim_message *message, char *msg)
395 6840f1a1 2019-08-22 martijn message->err = 1;
396 6840f1a1 2019-08-22 martijn fprintf(stderr, "%s\n", msg);
400 6840f1a1 2019-08-22 martijn dkim_parse_header(struct dkim_message *message, char *line, int force)
402 48c4bdc1 2019-04-04 martijn size_t i;
403 48c4bdc1 2019-04-04 martijn size_t r, w;
404 48c4bdc1 2019-04-04 martijn size_t linelen;
405 48c4bdc1 2019-04-04 martijn size_t lastheader;
406 06f6b49b 2019-04-06 martijn size_t hlen;
407 b6e37a5e 2019-04-08 martijn int fieldname = 0;
408 48c4bdc1 2019-04-04 martijn char **mtmp;
409 48c4bdc1 2019-04-04 martijn char *htmp;
410 63ac36ce 2019-04-08 martijn char *tmp;
412 c826412d 2019-05-02 martijn if (addheaders == 2 && !force &&
413 6840f1a1 2019-08-22 martijn !dkim_signature_printheader(message, line))
416 6840f1a1 2019-08-22 martijn if ((line[0] == ' ' || line[0] == '\t') && !message->lastheader)
418 48c4bdc1 2019-04-04 martijn if ((line[0] != ' ' && line[0] != '\t')) {
419 6840f1a1 2019-08-22 martijn message->lastheader = 0;
420 48c4bdc1 2019-04-04 martijn for (i = 0; i < nsign_headers; i++) {
421 06f6b49b 2019-04-06 martijn hlen = strlen(sign_headers[i]);
422 06f6b49b 2019-04-06 martijn if (strncasecmp(line, sign_headers[i], hlen) == 0) {
423 06f6b49b 2019-04-06 martijn while (line[hlen] == ' ' || line[hlen] == '\t')
425 06f6b49b 2019-04-06 martijn if (line[hlen] != ':')
426 06f6b49b 2019-04-06 martijn continue;
430 06f6b49b 2019-04-06 martijn if (i == nsign_headers && !force)
434 c826412d 2019-05-02 martijn if (addheaders == 1 && !force &&
435 6840f1a1 2019-08-22 martijn !dkim_signature_printheader(message, line))
438 48c4bdc1 2019-04-04 martijn if (canonheader == CANON_RELAXED) {
439 6840f1a1 2019-08-22 martijn if (!message->lastheader)
440 b6e37a5e 2019-04-08 martijn fieldname = 1;
441 48c4bdc1 2019-04-04 martijn for (r = w = 0; line[r] != '\0'; r++) {
442 5d06ccad 2019-04-08 martijn if (line[r] == ':' && fieldname) {
443 cd6f9b03 2019-10-23 martijn if (w > 0 && line[w - 1] == ' ')
444 48c4bdc1 2019-04-04 martijn line[w - 1] = ':';
446 48c4bdc1 2019-04-04 martijn line[w++] = ':';
447 48c4bdc1 2019-04-04 martijn fieldname = 0;
448 48c4bdc1 2019-04-04 martijn while (line[r + 1] == ' ' ||
449 48c4bdc1 2019-04-04 martijn line[r + 1] == '\t')
451 48c4bdc1 2019-04-04 martijn continue;
453 06f6b49b 2019-04-06 martijn if (line[r] == ' ' || line[r] == '\t' ||
454 06f6b49b 2019-04-06 martijn line[r] == '\r' || line[r] == '\n') {
455 cd6f9b03 2019-10-23 martijn if (r != 0 && w != 0 && line[w - 1] == ' ')
456 48c4bdc1 2019-04-04 martijn continue;
458 48c4bdc1 2019-04-04 martijn line[w++] = ' ';
459 48c4bdc1 2019-04-04 martijn } else if (fieldname) {
460 48c4bdc1 2019-04-04 martijn line[w++] = tolower(line[r]);
461 48c4bdc1 2019-04-04 martijn continue;
463 48c4bdc1 2019-04-04 martijn line[w++] = line[r];
465 cd6f9b03 2019-10-23 martijn linelen = (w != 0 && line[w - 1] == ' ') ? w - 1 : w;
466 48c4bdc1 2019-04-04 martijn line[linelen] = '\0';
468 48c4bdc1 2019-04-04 martijn linelen = strlen(line);
470 6840f1a1 2019-08-22 martijn for (lastheader = 0; message->headers[lastheader] != NULL; lastheader++)
471 48c4bdc1 2019-04-04 martijn continue;
472 6840f1a1 2019-08-22 martijn if (!message->lastheader) {
473 6840f1a1 2019-08-22 martijn mtmp = recallocarray(message->headers, lastheader + 1,
474 a53d74c4 2019-04-08 martijn lastheader + 2, sizeof(*mtmp));
475 48c4bdc1 2019-04-04 martijn if (mtmp == NULL) {
476 6840f1a1 2019-08-22 martijn dkim_err(message, "Can't store header");
479 6840f1a1 2019-08-22 martijn message->headers = mtmp;
481 6840f1a1 2019-08-22 martijn message->headers[lastheader] = strdup(line);
482 6840f1a1 2019-08-22 martijn message->headers[lastheader + 1 ] = NULL;
483 6840f1a1 2019-08-22 martijn message->lastheader = 1;
484 48c4bdc1 2019-04-04 martijn } else {
485 48c4bdc1 2019-04-04 martijn lastheader--;
486 6840f1a1 2019-08-22 martijn linelen += strlen(message->headers[lastheader]);
487 48c4bdc1 2019-04-04 martijn if (canonheader == CANON_SIMPLE)
488 48c4bdc1 2019-04-04 martijn linelen += 2;
489 48c4bdc1 2019-04-04 martijn linelen++;
490 6840f1a1 2019-08-22 martijn htmp = reallocarray(message->headers[lastheader], linelen,
491 48c4bdc1 2019-04-04 martijn sizeof(*htmp));
492 48c4bdc1 2019-04-04 martijn if (htmp == NULL) {
493 6840f1a1 2019-08-22 martijn dkim_err(message, "Can't store header");
496 6840f1a1 2019-08-22 martijn message->headers[lastheader] = htmp;
497 48c4bdc1 2019-04-04 martijn if (canonheader == CANON_SIMPLE) {
498 48c4bdc1 2019-04-04 martijn if (strlcat(htmp, "\r\n", linelen) >= linelen)
499 6840f1a1 2019-08-22 martijn osmtpd_errx(1, "Missized header");
500 63ac36ce 2019-04-08 martijn } else if (canonheader == CANON_RELAXED &&
501 6840f1a1 2019-08-22 martijn (tmp = strchr(message->headers[lastheader], ':')) != NULL &&
502 63ac36ce 2019-04-08 martijn tmp[1] == '\0')
505 48c4bdc1 2019-04-04 martijn if (strlcat(htmp, line, linelen) >= linelen)
506 6840f1a1 2019-08-22 martijn osmtpd_errx(1, "Missized header");
511 6840f1a1 2019-08-22 martijn dkim_parse_body(struct dkim_message *message, char *line)
513 48c4bdc1 2019-04-04 martijn size_t r, w;
514 48c4bdc1 2019-04-04 martijn size_t linelen;
516 1308710c 2019-04-09 martijn if (canonbody == CANON_RELAXED) {
517 1308710c 2019-04-09 martijn for (r = w = 0; line[r] != '\0'; r++) {
518 1308710c 2019-04-09 martijn if (line[r] == ' ' || line[r] == '\t') {
519 1308710c 2019-04-09 martijn if (r != 0 && line[w - 1] == ' ')
520 1308710c 2019-04-09 martijn continue;
522 1308710c 2019-04-09 martijn line[w++] = ' ';
524 1308710c 2019-04-09 martijn line[w++] = line[r];
526 cd6f9b03 2019-10-23 martijn linelen = (w != 0 && line[w - 1] == ' ') ? w - 1 : w;
527 1308710c 2019-04-09 martijn line[linelen] = '\0';
529 1308710c 2019-04-09 martijn linelen = strlen(line);
531 48c4bdc1 2019-04-04 martijn if (line[0] == '\0') {
532 6840f1a1 2019-08-22 martijn message->body_whitelines++;
536 6840f1a1 2019-08-22 martijn while (message->body_whitelines--) {
537 6bcbc798 2021-05-16 martijn if (EVP_DigestUpdate(message->dctx, "\r\n", 2) == 0) {
538 71b92702 2021-05-14 martijn dkim_errx(message, "Can't update hash context");
542 6840f1a1 2019-08-22 martijn message->body_whitelines = 0;
543 6840f1a1 2019-08-22 martijn message->has_body = 1;
545 6bcbc798 2021-05-16 martijn if (EVP_DigestUpdate(message->dctx, line, linelen) == 0 ||
546 6bcbc798 2021-05-16 martijn EVP_DigestUpdate(message->dctx, "\r\n", 2) == 0) {
547 71b92702 2021-05-14 martijn dkim_errx(message, "Can't update hash context");
553 6840f1a1 2019-08-22 martijn dkim_sign(struct osmtpd_ctx *ctx)
555 6840f1a1 2019-08-22 martijn struct dkim_message *message = ctx->local_message;
556 365862b1 2019-05-02 martijn /* Use largest hash size here */
557 6bcbc798 2021-05-16 martijn char bdigest[EVP_MAX_MD_SIZE];
558 6bcbc798 2021-05-16 martijn char digest[(((sizeof(bdigest) + 2) / 3) * 4) + 1];
559 365862b1 2019-05-02 martijn char *b;
560 40cd76f4 2020-08-30 martijn const char *sdomain = domain[0], *tsdomain;
561 e974922c 2019-05-02 martijn time_t now;
562 6840f1a1 2019-08-22 martijn ssize_t i;
563 6bcbc798 2021-05-16 martijn size_t linelen = 0;
564 365862b1 2019-05-02 martijn char *tmp, *tmp2;
565 6bcbc798 2021-05-16 martijn int digestsz;
567 e974922c 2019-05-02 martijn if (addtime || addexpire)
568 e974922c 2019-05-02 martijn now = time(NULL);
569 a70fd4b2 2021-05-11 martijn if (addtime && !dkim_signature_printf(message, "t=%lld; ",
570 a70fd4b2 2021-05-11 martijn (long long)now))
571 2c88ced1 2021-05-16 martijn goto fail;
572 6840f1a1 2019-08-22 martijn if (addexpire != 0 && !dkim_signature_printf(message, "x=%lld; ",
573 e974922c 2019-05-02 martijn now + addexpire < now ? INT64_MAX : now + addexpire))
574 2c88ced1 2021-05-16 martijn goto fail;
576 6840f1a1 2019-08-22 martijn if (canonbody == CANON_SIMPLE && !message->has_body) {
577 6bcbc798 2021-05-16 martijn if (EVP_DigestUpdate(message->dctx, "\r\n", 2) <= 0) {
578 71b92702 2021-05-14 martijn dkim_errx(message, "Can't update hash context");
579 2c88ced1 2021-05-16 martijn goto fail;
582 6bcbc798 2021-05-16 martijn if (EVP_DigestFinal_ex(message->dctx, bdigest, &digestsz) == 0) {
583 71b92702 2021-05-14 martijn dkim_errx(message, "Can't finalize hash context");
584 2c88ced1 2021-05-16 martijn goto fail;
586 6bcbc798 2021-05-16 martijn EVP_EncodeBlock(digest, bdigest, digestsz);
587 6bcbc798 2021-05-16 martijn if (!dkim_signature_printf(message, "bh=%s; h=", digest))
588 2c88ced1 2021-05-16 martijn goto fail;
589 365862b1 2019-05-02 martijn /* Reverse order for ease of use of RFC6367 section 5.4.2 */
590 6840f1a1 2019-08-22 martijn for (i = 0; message->headers[i] != NULL; i++)
591 365862b1 2019-05-02 martijn continue;
592 6bcbc798 2021-05-16 martijn EVP_MD_CTX_reset(message->dctx);
593 6bcbc798 2021-05-16 martijn if (!sephash) {
594 6bcbc798 2021-05-16 martijn if (EVP_DigestSignInit(message->dctx, NULL, hash_md, NULL,
595 6bcbc798 2021-05-16 martijn pkey) != 1) {
596 6bcbc798 2021-05-16 martijn dkim_errx(message, "Failed to initialize signature "
597 6bcbc798 2021-05-16 martijn "context");
598 2c88ced1 2021-05-16 martijn goto fail;
600 6bcbc798 2021-05-16 martijn } else {
601 6bcbc798 2021-05-16 martijn if (EVP_DigestInit_ex(message->dctx, hash_md, NULL) != 1) {
602 6bcbc798 2021-05-16 martijn dkim_errx(message, "Failed to initialize hash context");
603 2c88ced1 2021-05-16 martijn goto fail;
606 365862b1 2019-05-02 martijn for (i--; i >= 0; i--) {
607 6bcbc798 2021-05-16 martijn if (!sephash) {
608 6bcbc798 2021-05-16 martijn if (EVP_DigestSignUpdate(message->dctx,
609 6bcbc798 2021-05-16 martijn message->headers[i],
610 6bcbc798 2021-05-16 martijn strlen(message->headers[i])) != 1 ||
611 6bcbc798 2021-05-16 martijn EVP_DigestSignUpdate(message->dctx, "\r\n",
612 6bcbc798 2021-05-16 martijn 2) <= 0) {
613 6bcbc798 2021-05-16 martijn dkim_errx(message, "Failed to update signature "
614 6bcbc798 2021-05-16 martijn "context");
615 2c88ced1 2021-05-16 martijn goto fail;
617 6bcbc798 2021-05-16 martijn } else {
618 6bcbc798 2021-05-16 martijn if (EVP_DigestUpdate(message->dctx, message->headers[i],
619 6bcbc798 2021-05-16 martijn strlen(message->headers[i])) != 1 ||
620 6bcbc798 2021-05-16 martijn EVP_DigestUpdate(message->dctx, "\r\n", 2) <= 0) {
621 6bcbc798 2021-05-16 martijn dkim_errx(message, "Failed to update digest "
622 6bcbc798 2021-05-16 martijn "context");
623 2c88ced1 2021-05-16 martijn goto fail;
626 40cd76f4 2020-08-30 martijn if ((tsdomain = dkim_domain_select(message, message->headers[i])) != NULL)
627 40cd76f4 2020-08-30 martijn sdomain = tsdomain;
628 365862b1 2019-05-02 martijn /* We're done with the cached header after hashing */
629 6840f1a1 2019-08-22 martijn for (tmp = message->headers[i]; tmp[0] != ':'; tmp++) {
630 365862b1 2019-05-02 martijn if (tmp[0] == ' ' || tmp[0] == '\t')
632 365862b1 2019-05-02 martijn tmp[0] = tolower(tmp[0]);
634 365862b1 2019-05-02 martijn tmp[0] = '\0';
635 6840f1a1 2019-08-22 martijn if (!dkim_signature_printf(message, "%s%s",
636 6840f1a1 2019-08-22 martijn message->headers[i + 1] == NULL ? "" : ":",
637 6840f1a1 2019-08-22 martijn message->headers[i]))
638 2c88ced1 2021-05-16 martijn goto fail;
640 40cd76f4 2020-08-30 martijn dkim_signature_printf(message, "; d=%s; b=", sdomain);
641 6840f1a1 2019-08-22 martijn if (!dkim_signature_normalize(message))
642 2c88ced1 2021-05-16 martijn goto fail;
643 6840f1a1 2019-08-22 martijn if ((tmp = strdup(message->signature.signature)) == NULL) {
644 6840f1a1 2019-08-22 martijn dkim_err(message, "Can't create DKIM signature");
645 2c88ced1 2021-05-16 martijn goto fail;
647 6840f1a1 2019-08-22 martijn dkim_parse_header(message, tmp, 1);
648 6bcbc798 2021-05-16 martijn if (!sephash) {
649 6bcbc798 2021-05-16 martijn if (EVP_DigestSignUpdate(message->dctx, tmp,
650 6bcbc798 2021-05-16 martijn strlen(tmp)) != 1) {
651 6bcbc798 2021-05-16 martijn dkim_errx(message, "Failed to update signature "
652 6bcbc798 2021-05-16 martijn "context");
653 2c88ced1 2021-05-16 martijn goto fail;
655 6bcbc798 2021-05-16 martijn } else {
656 6bcbc798 2021-05-16 martijn if (EVP_DigestUpdate(message->dctx, tmp, strlen(tmp)) != 1) {
657 6bcbc798 2021-05-16 martijn dkim_errx(message, "Failed to update digest context");
658 2c88ced1 2021-05-16 martijn goto fail;
661 365862b1 2019-05-02 martijn free(tmp);
662 6bcbc798 2021-05-16 martijn if (!sephash) {
663 6bcbc798 2021-05-16 martijn if (EVP_DigestSignFinal(message->dctx, NULL, &linelen) != 1) {
664 6bcbc798 2021-05-16 martijn dkim_errx(message, "Can't finalize signature context");
665 2c88ced1 2021-05-16 martijn goto fail;
667 6bcbc798 2021-05-16 martijn #ifdef HAVE_ED25519
668 6bcbc798 2021-05-16 martijn } else {
669 6bcbc798 2021-05-16 martijn if (EVP_DigestFinal_ex(message->dctx, bdigest,
670 6bcbc798 2021-05-16 martijn &digestsz) != 1) {
671 6bcbc798 2021-05-16 martijn dkim_errx(message, "Can't finalize hash context");
672 2c88ced1 2021-05-16 martijn goto fail;
674 6bcbc798 2021-05-16 martijn EVP_MD_CTX_reset(message->dctx);
675 6bcbc798 2021-05-16 martijn if (EVP_DigestSignInit(message->dctx, NULL, NULL, NULL,
676 6bcbc798 2021-05-16 martijn pkey) != 1) {
677 6bcbc798 2021-05-16 martijn dkim_errx(message, "Failed to initialize signature "
678 6bcbc798 2021-05-16 martijn "context");
679 2c88ced1 2021-05-16 martijn goto fail;
681 6bcbc798 2021-05-16 martijn if (EVP_DigestSign(message->dctx, NULL, &linelen, bdigest,
682 6bcbc798 2021-05-16 martijn digestsz) != 1) {
683 6bcbc798 2021-05-16 martijn dkim_errx(message, "Failed to finalize signature");
684 2c88ced1 2021-05-16 martijn goto fail;
688 365862b1 2019-05-02 martijn if ((tmp = malloc(linelen)) == NULL) {
689 6bcbc798 2021-05-16 martijn dkim_err(message, "Can't allocate space for signature");
690 2c88ced1 2021-05-16 martijn goto fail;
692 6bcbc798 2021-05-16 martijn if (!sephash) {
693 6bcbc798 2021-05-16 martijn if (EVP_DigestSignFinal(message->dctx, tmp, &linelen) != 1) {
694 6bcbc798 2021-05-16 martijn dkim_errx(message, "Failed to finalize signature");
695 2c88ced1 2021-05-16 martijn goto fail;
697 6bcbc798 2021-05-16 martijn #ifdef HAVE_ED25519
698 6bcbc798 2021-05-16 martijn } else {
699 6bcbc798 2021-05-16 martijn if (EVP_DigestSign(message->dctx, tmp, &linelen, bdigest,
700 6bcbc798 2021-05-16 martijn digestsz) != 1) {
701 6bcbc798 2021-05-16 martijn dkim_errx(message, "Failed to finalize signature");
702 2c88ced1 2021-05-16 martijn goto fail;
706 365862b1 2019-05-02 martijn if ((b = malloc((((linelen + 2) / 3) * 4) + 1)) == NULL) {
707 6840f1a1 2019-08-22 martijn dkim_err(message, "Can't create DKIM signature");
708 2c88ced1 2021-05-16 martijn goto fail;
710 365862b1 2019-05-02 martijn EVP_EncodeBlock(b, tmp, linelen);
711 365862b1 2019-05-02 martijn free(tmp);
712 6840f1a1 2019-08-22 martijn dkim_signature_printf(message, "%s\r\n", b);
713 365862b1 2019-05-02 martijn free(b);
714 6840f1a1 2019-08-22 martijn dkim_signature_normalize(message);
715 6840f1a1 2019-08-22 martijn tmp = message->signature.signature;
716 365862b1 2019-05-02 martijn while ((tmp2 = strchr(tmp, '\r')) != NULL) {
717 365862b1 2019-05-02 martijn tmp2[0] = '\0';
718 6840f1a1 2019-08-22 martijn osmtpd_filter_dataline(ctx, "%s", tmp);
719 365862b1 2019-05-02 martijn tmp = tmp2 + 2;
721 365862b1 2019-05-02 martijn tmp = NULL;
722 365862b1 2019-05-02 martijn linelen = 0;
723 6840f1a1 2019-08-22 martijn rewind(message->origf);
724 6840f1a1 2019-08-22 martijn while ((i = getline(&tmp, &linelen, message->origf)) != -1) {
725 365862b1 2019-05-02 martijn tmp[i - 1] = '\0';
726 6840f1a1 2019-08-22 martijn osmtpd_filter_dataline(ctx, "%s", tmp);
728 be6d950d 2021-03-21 martijn free(tmp);
731 2c88ced1 2021-05-16 martijn osmtpd_filter_dataline(ctx, ".");
735 6840f1a1 2019-08-22 martijn dkim_signature_normalize(struct dkim_message *message)
737 06f6b49b 2019-04-06 martijn size_t i;
738 06f6b49b 2019-04-06 martijn size_t linelen;
739 06f6b49b 2019-04-06 martijn size_t checkpoint;
740 06f6b49b 2019-04-06 martijn size_t skip;
741 6840f1a1 2019-08-22 martijn size_t *headerlen = &(message->signature.len);
742 06f6b49b 2019-04-06 martijn int headername = 1;
743 06f6b49b 2019-04-06 martijn char tag = '\0';
744 6840f1a1 2019-08-22 martijn char *sig = message->signature.signature;
746 06f6b49b 2019-04-06 martijn for (linelen = i = 0; sig[i] != '\0'; i++) {
747 ffa37923 2019-04-09 martijn if (sig[i] == '\r' && sig[i + 1] == '\n') {
749 06f6b49b 2019-04-06 martijn checkpoint = 0;
750 06f6b49b 2019-04-06 martijn linelen = 0;
751 06f6b49b 2019-04-06 martijn continue;
753 06f6b49b 2019-04-06 martijn if (sig[i] == '\t')
754 06f6b49b 2019-04-06 martijn linelen = (linelen + 8) & ~7;
756 06f6b49b 2019-04-06 martijn linelen++;
757 06f6b49b 2019-04-06 martijn if (headername) {
758 06f6b49b 2019-04-06 martijn if (sig[i] == ':') {
759 06f6b49b 2019-04-06 martijn headername = 0;
760 06f6b49b 2019-04-06 martijn checkpoint = i;
762 06f6b49b 2019-04-06 martijn continue;
764 ffa37923 2019-04-09 martijn if (linelen > DKIM_SIGNATURE_LINELEN && checkpoint != 0) {
765 06f6b49b 2019-04-06 martijn for (skip = checkpoint + 1;
766 06f6b49b 2019-04-06 martijn sig[skip] == ' ' || sig[skip] == '\t';
768 06f6b49b 2019-04-06 martijn continue;
769 06f6b49b 2019-04-06 martijn skip -= checkpoint + 1;
770 6840f1a1 2019-08-22 martijn if (!dkim_signature_need(message,
771 226e5da8 2019-04-08 martijn skip > 3 ? 0 : 3 - skip + 1))
772 06f6b49b 2019-04-06 martijn return 0;
773 6840f1a1 2019-08-22 martijn sig = message->signature.signature;
775 06f6b49b 2019-04-06 martijn memmove(sig + checkpoint + 3,
776 06f6b49b 2019-04-06 martijn sig + checkpoint + skip,
777 540f552a 2019-04-06 martijn *headerlen - skip - checkpoint + 1);
778 06f6b49b 2019-04-06 martijn sig[checkpoint + 1] = '\r';
779 06f6b49b 2019-04-06 martijn sig[checkpoint + 2] = '\n';
780 06f6b49b 2019-04-06 martijn sig[checkpoint + 3] = '\t';
781 06f6b49b 2019-04-06 martijn linelen = 8;
782 06f6b49b 2019-04-06 martijn *headerlen = *headerlen + 3 - skip;
783 06f6b49b 2019-04-06 martijn i = checkpoint + 3;
784 06f6b49b 2019-04-06 martijn checkpoint = 0;
786 06f6b49b 2019-04-06 martijn if (sig[i] == ';') {
787 06f6b49b 2019-04-06 martijn checkpoint = i;
788 06f6b49b 2019-04-06 martijn tag = '\0';
789 06f6b49b 2019-04-06 martijn continue;
791 06f6b49b 2019-04-06 martijn switch (tag) {
792 06f6b49b 2019-04-06 martijn case 'B':
793 06f6b49b 2019-04-06 martijn case 'b':
794 c826412d 2019-05-02 martijn case 'z':
795 06f6b49b 2019-04-06 martijn checkpoint = i;
797 06f6b49b 2019-04-06 martijn case 'h':
798 06f6b49b 2019-04-06 martijn if (sig[i] == ':')
799 06f6b49b 2019-04-06 martijn checkpoint = i;
802 06f6b49b 2019-04-06 martijn if (tag == '\0' && sig[i] != ' ' && sig[i] != '\t') {
803 06f6b49b 2019-04-06 martijn if ((tag = sig[i]) == 'b' && sig[i + 1] == 'h' &&
804 06f6b49b 2019-04-06 martijn sig[i + 2] == '=') {
805 06f6b49b 2019-04-06 martijn tag = 'B';
806 06f6b49b 2019-04-06 martijn linelen += 2;
809 06f6b49b 2019-04-06 martijn tag = sig[i];
812 06f6b49b 2019-04-06 martijn return 1;
816 6840f1a1 2019-08-22 martijn dkim_signature_printheader(struct dkim_message *message, const char *header)
818 c826412d 2019-05-02 martijn size_t i, j, len;
819 c826412d 2019-05-02 martijn static char *fmtheader = NULL;
820 c826412d 2019-05-02 martijn char *tmp;
821 c826412d 2019-05-02 martijn static size_t size = 0;
822 c826412d 2019-05-02 martijn int first;
824 c826412d 2019-05-02 martijn len = strlen(header);
825 c826412d 2019-05-02 martijn if ((len + 3) * 3 < len) {
826 c826412d 2019-05-02 martijn errno = EOVERFLOW;
827 6840f1a1 2019-08-22 martijn dkim_err(message, "Can't add z-component to header");
828 c826412d 2019-05-02 martijn return 0;
830 c826412d 2019-05-02 martijn if ((len + 3) * 3 > size) {
831 c826412d 2019-05-02 martijn if ((tmp = reallocarray(fmtheader, 3, len + 3)) == NULL) {
832 6840f1a1 2019-08-22 martijn dkim_err(message, "Can't add z-component to header");
833 c826412d 2019-05-02 martijn return 0;
835 c826412d 2019-05-02 martijn fmtheader = tmp;
836 c826412d 2019-05-02 martijn size = (len + 1) * 3;
839 6840f1a1 2019-08-22 martijn first = message->signature.signature[message->signature.len - 1] == '=';
840 c826412d 2019-05-02 martijn for (j = i = 0; header[i] != '\0'; i++, j++) {
841 c826412d 2019-05-02 martijn if (i == 0 && header[i] != ' ' && header[i] != '\t' && !first)
842 c826412d 2019-05-02 martijn fmtheader[j++] = '|';
843 c826412d 2019-05-02 martijn if ((header[i] >= 0x21 && header[i] <= 0x3A) ||
844 6840f1a1 2019-08-22 martijn (header[i] == 0x3C) ||
845 c826412d 2019-05-02 martijn (header[i] >= 0x3E && header[i] <= 0x7B) ||
846 c826412d 2019-05-02 martijn (header[i] >= 0x7D && header[i] <= 0x7E))
847 c826412d 2019-05-02 martijn fmtheader[j] = header[i];
849 c826412d 2019-05-02 martijn fmtheader[j++] = '=';
850 c826412d 2019-05-02 martijn (void) sprintf(fmtheader + j, "%02hhX", header[i]);
854 c826412d 2019-05-02 martijn (void) sprintf(fmtheader + j, "=%02hhX=%02hhX", (unsigned char) '\r',
855 c826412d 2019-05-02 martijn (unsigned char) '\n');
857 6840f1a1 2019-08-22 martijn return dkim_signature_printf(message, "%s", fmtheader);
861 6840f1a1 2019-08-22 martijn dkim_signature_printf(struct dkim_message *message, char *fmt, ...)
863 6840f1a1 2019-08-22 martijn struct dkim_signature *sig = &(message->signature);
864 06f6b49b 2019-04-06 martijn va_list ap;
865 06f6b49b 2019-04-06 martijn size_t len;
867 06f6b49b 2019-04-06 martijn va_start(ap, fmt);
868 06f6b49b 2019-04-06 martijn if ((len = vsnprintf(sig->signature + sig->len, sig->size - sig->len,
869 06f6b49b 2019-04-06 martijn fmt, ap)) >= sig->size - sig->len) {
870 06f6b49b 2019-04-06 martijn va_end(ap);
871 6840f1a1 2019-08-22 martijn if (!dkim_signature_need(message, len + 1))
872 06f6b49b 2019-04-06 martijn return 0;
873 06f6b49b 2019-04-06 martijn va_start(ap, fmt);
874 6840f1a1 2019-08-22 martijn if ((len = vsnprintf(sig->signature + sig->len,
875 6840f1a1 2019-08-22 martijn sig->size - sig->len, fmt, ap)) >= sig->size - sig->len)
876 6840f1a1 2019-08-22 martijn osmtpd_errx(1, "Miscalculated header size");
878 06f6b49b 2019-04-06 martijn sig->len += len;
879 06f6b49b 2019-04-06 martijn va_end(ap);
880 06f6b49b 2019-04-06 martijn return 1;
883 40cd76f4 2020-08-30 martijn const char *
884 40cd76f4 2020-08-30 martijn dkim_domain_select(struct dkim_message *message, char *from)
886 40cd76f4 2020-08-30 martijn char *mdomain0, *mdomain;
887 40cd76f4 2020-08-30 martijn size_t i;
889 40cd76f4 2020-08-30 martijn if ((mdomain = mdomain0 = osmtpd_mheader_from_domain(from)) == NULL) {
890 40cd76f4 2020-08-30 martijn if (errno != EINVAL) {
891 71b92702 2021-05-14 martijn dkim_errx(message, "Couldn't parse from header");
892 40cd76f4 2020-08-30 martijn return NULL;
894 40cd76f4 2020-08-30 martijn return NULL;
897 40cd76f4 2020-08-30 martijn while (mdomain != NULL && mdomain[0] != '\0') {
898 40cd76f4 2020-08-30 martijn for (i = 0; i < ndomains; i++) {
899 40cd76f4 2020-08-30 martijn if (strcasecmp(mdomain, domain[i]) == 0) {
900 40cd76f4 2020-08-30 martijn free(mdomain0);
901 40cd76f4 2020-08-30 martijn return domain[i];
904 40cd76f4 2020-08-30 martijn if ((mdomain = strchr(mdomain, '.')) != NULL)
905 40cd76f4 2020-08-30 martijn mdomain++;
907 40cd76f4 2020-08-30 martijn free(mdomain0);
908 40cd76f4 2020-08-30 martijn return NULL;
912 6840f1a1 2019-08-22 martijn dkim_signature_need(struct dkim_message *message, size_t len)
914 6840f1a1 2019-08-22 martijn struct dkim_signature *sig = &(message->signature);
915 06f6b49b 2019-04-06 martijn char *tmp;
917 a27b5ff7 2019-04-08 martijn if (sig->len + len < sig->size)
918 06f6b49b 2019-04-06 martijn return 1;
919 503597cc 2019-04-08 martijn sig->size = (((len + sig->len) / 512) + 1) * 512;
920 06f6b49b 2019-04-06 martijn if ((tmp = realloc(sig->signature, sig->size)) == NULL) {
921 6840f1a1 2019-08-22 martijn dkim_err(message, "No room for signature");
922 06f6b49b 2019-04-06 martijn return 0;
924 06f6b49b 2019-04-06 martijn sig->signature = tmp;
925 06f6b49b 2019-04-06 martijn return 1;
928 48c4bdc1 2019-04-04 martijn __dead void
929 48c4bdc1 2019-04-04 martijn usage(void)
931 fe371fc9 2019-09-05 martijn fprintf(stderr, "usage: filter-dkimsign [-tz] [-a signalg] "
932 fe371fc9 2019-09-05 martijn "[-c canonicalization] \n [-h headerfields]"
933 6840f1a1 2019-08-22 martijn "[-x seconds] -d domain -k keyfile -s selector\n");
934 48c4bdc1 2019-04-04 martijn exit(1);