Commit | Line | Data |
---|---|---|
80a47a2c TK |
1 | /* |
2 | * PDKIM - a RFC4871 (DKIM) implementation | |
3 | * | |
f444c2c7 | 4 | * Copyright (C) 2009 - 2016 Tom Kistner <tom@duncanthrax.net> |
d4e5e70b | 5 | * Copyright (C) 2016 - 2017 Jeremy Harris <jgh@exim.org> |
80a47a2c TK |
6 | * |
7 | * http://duncanthrax.net/pdkim/ | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License along | |
20 | * with this program; if not, write to the Free Software Foundation, Inc., | |
21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
22 | */ | |
23 | ||
0d04a285 | 24 | #include "../exim.h" |
80a47a2c | 25 | |
f444c2c7 JH |
26 | |
27 | #ifndef DISABLE_DKIM /* entire file */ | |
28 | ||
29 | #ifndef SUPPORT_TLS | |
30 | # error Need SUPPORT_TLS for DKIM | |
31 | #endif | |
32 | ||
cb224393 | 33 | #include "crypt_ver.h" |
f444c2c7 | 34 | |
d73e45df | 35 | #ifdef SIGN_OPENSSL |
cb224393 JH |
36 | # include <openssl/rsa.h> |
37 | # include <openssl/ssl.h> | |
38 | # include <openssl/err.h> | |
d73e45df | 39 | #elif defined(SIGN_GNUTLS) |
f444c2c7 JH |
40 | # include <gnutls/gnutls.h> |
41 | # include <gnutls/x509.h> | |
f444c2c7 JH |
42 | #endif |
43 | ||
44 | #include "pdkim.h" | |
9b2583c4 | 45 | #include "signing.h" |
80a47a2c TK |
46 | |
47 | #define PDKIM_SIGNATURE_VERSION "1" | |
e2e3255a | 48 | #define PDKIM_PUB_RECORD_VERSION US "DKIM1" |
80a47a2c TK |
49 | |
50 | #define PDKIM_MAX_HEADER_LEN 65536 | |
51 | #define PDKIM_MAX_HEADERS 512 | |
52 | #define PDKIM_MAX_BODY_LINE_LEN 16384 | |
53 | #define PDKIM_DNS_TXT_MAX_NAMELEN 1024 | |
54 | #define PDKIM_DEFAULT_SIGN_HEADERS "From:Sender:Reply-To:Subject:Date:"\ | |
55 | "Message-ID:To:Cc:MIME-Version:Content-Type:"\ | |
56 | "Content-Transfer-Encoding:Content-ID:"\ | |
57 | "Content-Description:Resent-Date:Resent-From:"\ | |
58 | "Resent-Sender:Resent-To:Resent-Cc:"\ | |
59 | "Resent-Message-ID:In-Reply-To:References:"\ | |
60 | "List-Id:List-Help:List-Unsubscribe:"\ | |
61 | "List-Subscribe:List-Post:List-Owner:List-Archive" | |
62 | ||
63 | /* -------------------------------------------------------------------------- */ | |
64 | struct pdkim_stringlist { | |
e2e3255a JH |
65 | uschar * value; |
66 | int tag; | |
67 | void * next; | |
80a47a2c TK |
68 | }; |
69 | ||
80a47a2c TK |
70 | /* -------------------------------------------------------------------------- */ |
71 | /* A bunch of list constants */ | |
e2e3255a JH |
72 | const uschar * pdkim_querymethods[] = { |
73 | US"dns/txt", | |
80a47a2c TK |
74 | NULL |
75 | }; | |
e2e3255a JH |
76 | const uschar * pdkim_canons[] = { |
77 | US"simple", | |
78 | US"relaxed", | |
80a47a2c TK |
79 | NULL |
80 | }; | |
d73e45df JH |
81 | |
82 | typedef struct { | |
83 | const uschar * dkim_hashname; | |
84 | hashmethod exim_hashmethod; | |
85 | } pdkim_hashtype; | |
86 | static const pdkim_hashtype pdkim_hashes[] = { | |
87 | { US"sha1", HASH_SHA1 }, | |
88 | { US"sha256", HASH_SHA2_256 }, | |
89 | { US"sha512", HASH_SHA2_512 } | |
80a47a2c | 90 | }; |
d73e45df | 91 | |
e2e3255a | 92 | const uschar * pdkim_keytypes[] = { |
d73e45df | 93 | US"rsa" |
80a47a2c TK |
94 | }; |
95 | ||
96 | typedef struct pdkim_combined_canon_entry { | |
e2e3255a | 97 | const uschar * str; |
80a47a2c TK |
98 | int canon_headers; |
99 | int canon_body; | |
100 | } pdkim_combined_canon_entry; | |
f444c2c7 | 101 | |
80a47a2c | 102 | pdkim_combined_canon_entry pdkim_combined_canons[] = { |
e2e3255a JH |
103 | { US"simple/simple", PDKIM_CANON_SIMPLE, PDKIM_CANON_SIMPLE }, |
104 | { US"simple/relaxed", PDKIM_CANON_SIMPLE, PDKIM_CANON_RELAXED }, | |
105 | { US"relaxed/simple", PDKIM_CANON_RELAXED, PDKIM_CANON_SIMPLE }, | |
106 | { US"relaxed/relaxed", PDKIM_CANON_RELAXED, PDKIM_CANON_RELAXED }, | |
107 | { US"simple", PDKIM_CANON_SIMPLE, PDKIM_CANON_SIMPLE }, | |
108 | { US"relaxed", PDKIM_CANON_RELAXED, PDKIM_CANON_SIMPLE }, | |
109 | { NULL, 0, 0 } | |
80a47a2c TK |
110 | }; |
111 | ||
112 | ||
9e70917d JH |
113 | static blob lineending = {.data = US"\r\n", .len = 2}; |
114 | ||
f444c2c7 | 115 | /* -------------------------------------------------------------------------- */ |
d73e45df | 116 | uschar * |
9e70917d | 117 | dkim_sig_to_a_tag(const pdkim_signature * sig) |
d73e45df JH |
118 | { |
119 | if ( sig->keytype < 0 || sig->keytype > nelem(pdkim_keytypes) | |
120 | || sig->hashtype < 0 || sig->hashtype > nelem(pdkim_hashes)) | |
121 | return US"err"; | |
122 | return string_sprintf("%s-%s", | |
123 | pdkim_keytypes[sig->keytype], pdkim_hashes[sig->hashtype].dkim_hashname); | |
124 | } | |
125 | ||
126 | ||
f444c2c7 JH |
127 | |
128 | const char * | |
129 | pdkim_verify_status_str(int status) | |
130 | { | |
f7302073 JH |
131 | switch(status) |
132 | { | |
133 | case PDKIM_VERIFY_NONE: return "PDKIM_VERIFY_NONE"; | |
134 | case PDKIM_VERIFY_INVALID: return "PDKIM_VERIFY_INVALID"; | |
135 | case PDKIM_VERIFY_FAIL: return "PDKIM_VERIFY_FAIL"; | |
136 | case PDKIM_VERIFY_PASS: return "PDKIM_VERIFY_PASS"; | |
137 | default: return "PDKIM_VERIFY_UNKNOWN"; | |
ff7ddfd7 TK |
138 | } |
139 | } | |
f444c2c7 JH |
140 | |
141 | const char * | |
142 | pdkim_verify_ext_status_str(int ext_status) | |
143 | { | |
f7302073 JH |
144 | switch(ext_status) |
145 | { | |
146 | case PDKIM_VERIFY_FAIL_BODY: return "PDKIM_VERIFY_FAIL_BODY"; | |
147 | case PDKIM_VERIFY_FAIL_MESSAGE: return "PDKIM_VERIFY_FAIL_MESSAGE"; | |
135e9496 | 148 | case PDKIM_VERIFY_FAIL_SIG_ALGO_MISMATCH: return "PDKIM_VERIFY_FAIL_SIG_ALGO_MISMATCH"; |
f7302073 JH |
149 | case PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE: return "PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE"; |
150 | case PDKIM_VERIFY_INVALID_BUFFER_SIZE: return "PDKIM_VERIFY_INVALID_BUFFER_SIZE"; | |
151 | case PDKIM_VERIFY_INVALID_PUBKEY_DNSRECORD: return "PDKIM_VERIFY_INVALID_PUBKEY_DNSRECORD"; | |
152 | case PDKIM_VERIFY_INVALID_PUBKEY_IMPORT: return "PDKIM_VERIFY_INVALID_PUBKEY_IMPORT"; | |
153 | case PDKIM_VERIFY_INVALID_SIGNATURE_ERROR: return "PDKIM_VERIFY_INVALID_SIGNATURE_ERROR"; | |
154 | case PDKIM_VERIFY_INVALID_DKIM_VERSION: return "PDKIM_VERIFY_INVALID_DKIM_VERSION"; | |
155 | default: return "PDKIM_VERIFY_UNKNOWN"; | |
156 | } | |
157 | } | |
158 | ||
b9df1829 | 159 | const uschar * |
f7302073 JH |
160 | pdkim_errstr(int status) |
161 | { | |
162 | switch(status) | |
163 | { | |
b9df1829 JH |
164 | case PDKIM_OK: return US"OK"; |
165 | case PDKIM_FAIL: return US"FAIL"; | |
166 | case PDKIM_ERR_RSA_PRIVKEY: return US"RSA_PRIVKEY"; | |
167 | case PDKIM_ERR_RSA_SIGNING: return US"RSA SIGNING"; | |
168 | case PDKIM_ERR_LONG_LINE: return US"RSA_LONG_LINE"; | |
169 | case PDKIM_ERR_BUFFER_TOO_SMALL: return US"BUFFER_TOO_SMALL"; | |
170 | case PDKIM_SIGN_PRIVKEY_WRAP: return US"PRIVKEY_WRAP"; | |
171 | case PDKIM_SIGN_PRIVKEY_B64D: return US"PRIVKEY_B64D"; | |
ef698bf6 | 172 | default: return US"(unknown)"; |
ff7ddfd7 TK |
173 | } |
174 | } | |
175 | ||
176 | ||
80a47a2c TK |
177 | /* -------------------------------------------------------------------------- */ |
178 | /* Print debugging functions */ | |
2592e6c0 | 179 | static void |
b78006ac | 180 | pdkim_quoteprint(const uschar *data, int len) |
3045f050 JH |
181 | { |
182 | int i; | |
0d04a285 | 183 | for (i = 0; i < len; i++) |
3045f050 | 184 | { |
b78006ac | 185 | const int c = data[i]; |
3045f050 JH |
186 | switch (c) |
187 | { | |
0d04a285 JH |
188 | case ' ' : debug_printf("{SP}"); break; |
189 | case '\t': debug_printf("{TB}"); break; | |
190 | case '\r': debug_printf("{CR}"); break; | |
191 | case '\n': debug_printf("{LF}"); break; | |
192 | case '{' : debug_printf("{BO}"); break; | |
193 | case '}' : debug_printf("{BC}"); break; | |
3045f050 JH |
194 | default: |
195 | if ( (c < 32) || (c > 127) ) | |
0d04a285 | 196 | debug_printf("{%02x}", c); |
3045f050 | 197 | else |
0d04a285 | 198 | debug_printf("%c", c); |
80a47a2c TK |
199 | break; |
200 | } | |
201 | } | |
2592e6c0 | 202 | debug_printf("\n"); |
80a47a2c | 203 | } |
80a47a2c | 204 | |
2592e6c0 | 205 | static void |
b78006ac | 206 | pdkim_hexprint(const uschar *data, int len) |
3045f050 JH |
207 | { |
208 | int i; | |
02c4f8fb JH |
209 | if (data) for (i = 0 ; i < len; i++) debug_printf("%02x", data[i]); |
210 | else debug_printf("<NULL>"); | |
2592e6c0 | 211 | debug_printf("\n"); |
80a47a2c | 212 | } |
80a47a2c TK |
213 | |
214 | ||
f444c2c7 | 215 | |
f444c2c7 | 216 | static pdkim_stringlist * |
ca9cb170 | 217 | pdkim_prepend_stringlist(pdkim_stringlist * base, const uschar * str) |
3045f050 | 218 | { |
ca9cb170 | 219 | pdkim_stringlist * new_entry = store_get(sizeof(pdkim_stringlist)); |
3045f050 | 220 | |
abe1010c | 221 | memset(new_entry, 0, sizeof(pdkim_stringlist)); |
ca9cb170 | 222 | new_entry->value = string_copy(str); |
ab9152ff JH |
223 | if (base) new_entry->next = base; |
224 | return new_entry; | |
6ab02e3f | 225 | } |
80a47a2c TK |
226 | |
227 | ||
8ef02a06 JH |
228 | |
229 | /* Trim whitespace fore & aft */ | |
230 | ||
ca9cb170 | 231 | static void |
acec9514 | 232 | pdkim_strtrim(gstring * str) |
3045f050 | 233 | { |
acec9514 JH |
234 | uschar * p = str->s; |
235 | uschar * q = p + str->ptr; | |
236 | ||
237 | while (*p == '\t' || *p == ' ') /* dump the leading whitespace */ | |
238 | { str->size--; str->ptr--; str->s++; } | |
239 | ||
240 | while ( str->ptr > 0 | |
241 | && (q = str->s + str->ptr - 1), *q == '\t' || *q == ' ' | |
242 | ) | |
243 | str->ptr--; /* dump trailing whitespace */ | |
244 | ||
245 | (void) string_from_gstring(str); | |
8ef02a06 JH |
246 | } |
247 | ||
248 | ||
80a47a2c | 249 | |
80a47a2c | 250 | /* -------------------------------------------------------------------------- */ |
3045f050 JH |
251 | |
252 | DLLEXPORT void | |
253 | pdkim_free_ctx(pdkim_ctx *ctx) | |
254 | { | |
6ab02e3f | 255 | } |
80a47a2c TK |
256 | |
257 | ||
258 | /* -------------------------------------------------------------------------- */ | |
259 | /* Matches the name of the passed raw "header" against | |
8ef02a06 JH |
260 | the passed colon-separated "tick", and invalidates |
261 | the entry in tick. Returns OK or fail-code */ | |
262 | /*XXX might be safer done using a pdkim_stringlist for "tick" */ | |
3045f050 | 263 | |
f444c2c7 | 264 | static int |
e2e3255a | 265 | header_name_match(const uschar * header, uschar * tick) |
3045f050 | 266 | { |
ca9cb170 JH |
267 | uschar * hname; |
268 | uschar * lcopy; | |
269 | uschar * p; | |
270 | uschar * q; | |
271 | uschar * hcolon = Ustrchr(header, ':'); /* Get header name */ | |
3045f050 | 272 | |
ca9cb170 JH |
273 | if (!hcolon) |
274 | return PDKIM_FAIL; /* This isn't a header */ | |
3045f050 | 275 | |
ca9cb170 JH |
276 | /* if we had strncmpic() we wouldn't need this copy */ |
277 | hname = string_copyn(header, hcolon-header); | |
3045f050 JH |
278 | |
279 | /* Copy tick-off list locally, so we can punch zeroes into it */ | |
ca9cb170 JH |
280 | p = lcopy = string_copy(tick); |
281 | ||
282 | for (q = Ustrchr(p, ':'); q; q = Ustrchr(p, ':')) | |
3045f050 JH |
283 | { |
284 | *q = '\0'; | |
ca9cb170 JH |
285 | if (strcmpic(p, hname) == 0) |
286 | goto found; | |
80a47a2c | 287 | |
3045f050 | 288 | p = q+1; |
80a47a2c TK |
289 | } |
290 | ||
ca9cb170 JH |
291 | if (strcmpic(p, hname) == 0) |
292 | goto found; | |
293 | ||
294 | return PDKIM_FAIL; | |
295 | ||
296 | found: | |
3045f050 | 297 | /* Invalidate header name instance in tick-off list */ |
8ef02a06 | 298 | tick[p-lcopy] = '_'; |
ca9cb170 | 299 | return PDKIM_OK; |
80a47a2c TK |
300 | } |
301 | ||
302 | ||
303 | /* -------------------------------------------------------------------------- */ | |
ca9cb170 | 304 | /* Performs "relaxed" canonicalization of a header. */ |
3045f050 | 305 | |
ca9cb170 | 306 | static uschar * |
ea18931d | 307 | pdkim_relax_header(const uschar * header, BOOL append_crlf) |
3045f050 JH |
308 | { |
309 | BOOL past_field_name = FALSE; | |
310 | BOOL seen_wsp = FALSE; | |
ca9cb170 JH |
311 | const uschar * p; |
312 | uschar * relaxed = store_get(Ustrlen(header)+3); | |
313 | uschar * q = relaxed; | |
3045f050 | 314 | |
ca9cb170 | 315 | for (p = header; *p; p++) |
3045f050 | 316 | { |
ca9cb170 | 317 | uschar c = *p; |
ea18931d JH |
318 | |
319 | if (c == '\r' || c == '\n') /* Ignore CR & LF */ | |
3045f050 JH |
320 | continue; |
321 | if (c == '\t' || c == ' ') | |
322 | { | |
323 | if (seen_wsp) | |
80a47a2c | 324 | continue; |
3045f050 JH |
325 | c = ' '; /* Turns WSP into SP */ |
326 | seen_wsp = TRUE; | |
80a47a2c | 327 | } |
3045f050 JH |
328 | else |
329 | if (!past_field_name && c == ':') | |
330 | { | |
ea18931d JH |
331 | if (seen_wsp) q--; /* This removes WSP immediately before the colon */ |
332 | seen_wsp = TRUE; /* This removes WSP immediately after the colon */ | |
3045f050 | 333 | past_field_name = TRUE; |
80a47a2c | 334 | } |
3045f050 JH |
335 | else |
336 | seen_wsp = FALSE; | |
337 | ||
338 | /* Lowercase header name */ | |
339 | if (!past_field_name) c = tolower(c); | |
340 | *q++ = c; | |
80a47a2c | 341 | } |
3045f050 JH |
342 | |
343 | if (q > relaxed && q[-1] == ' ') q--; /* Squash eventual trailing SP */ | |
3045f050 | 344 | |
ea18931d | 345 | if (append_crlf) { *q++ = '\r'; *q++ = '\n'; } |
ca9cb170 | 346 | *q = '\0'; |
3045f050 | 347 | return relaxed; |
6ab02e3f | 348 | } |
80a47a2c TK |
349 | |
350 | ||
351 | /* -------------------------------------------------------------------------- */ | |
352 | #define PDKIM_QP_ERROR_DECODE -1 | |
3045f050 | 353 | |
35cf75e9 JH |
354 | static const uschar * |
355 | pdkim_decode_qp_char(const uschar *qp_p, int *c) | |
3045f050 | 356 | { |
35cf75e9 | 357 | const uschar *initial_pos = qp_p; |
3045f050 JH |
358 | |
359 | /* Advance one char */ | |
360 | qp_p++; | |
361 | ||
362 | /* Check for two hex digits and decode them */ | |
363 | if (isxdigit(*qp_p) && isxdigit(qp_p[1])) | |
364 | { | |
365 | /* Do hex conversion */ | |
366 | *c = (isdigit(*qp_p) ? *qp_p - '0' : toupper(*qp_p) - 'A' + 10) << 4; | |
a5840e10 | 367 | *c |= isdigit(qp_p[1]) ? qp_p[1] - '0' : toupper(qp_p[1]) - 'A' + 10; |
3045f050 | 368 | return qp_p + 2; |
6ab02e3f | 369 | } |
80a47a2c | 370 | |
3045f050 JH |
371 | /* Illegal char here */ |
372 | *c = PDKIM_QP_ERROR_DECODE; | |
373 | return initial_pos; | |
80a47a2c TK |
374 | } |
375 | ||
376 | ||
377 | /* -------------------------------------------------------------------------- */ | |
3045f050 | 378 | |
e2e3255a | 379 | static uschar * |
35cf75e9 | 380 | pdkim_decode_qp(const uschar * str) |
3045f050 JH |
381 | { |
382 | int nchar = 0; | |
ca9cb170 | 383 | uschar * q; |
35cf75e9 | 384 | const uschar * p = str; |
ca9cb170 | 385 | uschar * n = store_get(Ustrlen(str)+1); |
3045f050 JH |
386 | |
387 | *n = '\0'; | |
388 | q = n; | |
ca9cb170 | 389 | while (*p) |
3045f050 JH |
390 | { |
391 | if (*p == '=') | |
392 | { | |
393 | p = pdkim_decode_qp_char(p, &nchar); | |
394 | if (nchar >= 0) | |
395 | { | |
396 | *q++ = nchar; | |
397 | continue; | |
80a47a2c TK |
398 | } |
399 | } | |
3045f050 JH |
400 | else |
401 | *q++ = *p; | |
402 | p++; | |
80a47a2c | 403 | } |
3045f050 JH |
404 | *q = '\0'; |
405 | return n; | |
80a47a2c TK |
406 | } |
407 | ||
408 | ||
409 | /* -------------------------------------------------------------------------- */ | |
3045f050 | 410 | |
2592e6c0 | 411 | static void |
35cf75e9 | 412 | pdkim_decode_base64(const uschar * str, blob * b) |
3045f050 | 413 | { |
2592e6c0 | 414 | int dlen; |
2592e6c0 JH |
415 | dlen = b64decode(str, &b->data); |
416 | if (dlen < 0) b->data = NULL; | |
417 | b->len = dlen; | |
80a47a2c TK |
418 | } |
419 | ||
ca9cb170 | 420 | static uschar * |
2592e6c0 | 421 | pdkim_encode_base64(blob * b) |
3045f050 | 422 | { |
ca9cb170 | 423 | return b64encode(b->data, b->len); |
80a47a2c TK |
424 | } |
425 | ||
426 | ||
427 | /* -------------------------------------------------------------------------- */ | |
428 | #define PDKIM_HDR_LIMBO 0 | |
429 | #define PDKIM_HDR_TAG 1 | |
430 | #define PDKIM_HDR_VALUE 2 | |
3045f050 | 431 | |
f444c2c7 | 432 | static pdkim_signature * |
9e70917d | 433 | pdkim_parse_sig_header(pdkim_ctx * ctx, uschar * raw_hdr) |
3045f050 | 434 | { |
2c0f3ea1 | 435 | pdkim_signature * sig; |
ca9cb170 | 436 | uschar *p, *q; |
acec9514 JH |
437 | gstring * cur_tag = NULL; |
438 | gstring * cur_val = NULL; | |
3045f050 JH |
439 | BOOL past_hname = FALSE; |
440 | BOOL in_b_val = FALSE; | |
441 | int where = PDKIM_HDR_LIMBO; | |
442 | int i; | |
443 | ||
ca9cb170 | 444 | sig = store_get(sizeof(pdkim_signature)); |
abe1010c | 445 | memset(sig, 0, sizeof(pdkim_signature)); |
3045f050 JH |
446 | sig->bodylength = -1; |
447 | ||
07eeb4df | 448 | /* Set so invalid/missing data error display is accurate */ |
07eeb4df | 449 | sig->version = 0; |
d73e45df JH |
450 | sig->keytype = -1; |
451 | sig->hashtype = -1; | |
07eeb4df | 452 | |
ca9cb170 | 453 | q = sig->rawsig_no_b_val = store_get(Ustrlen(raw_hdr)+1); |
80a47a2c | 454 | |
3045f050 JH |
455 | for (p = raw_hdr; ; p++) |
456 | { | |
457 | char c = *p; | |
80a47a2c | 458 | |
3045f050 JH |
459 | /* Ignore FWS */ |
460 | if (c == '\r' || c == '\n') | |
461 | goto NEXT_CHAR; | |
80a47a2c | 462 | |
3045f050 JH |
463 | /* Fast-forward through header name */ |
464 | if (!past_hname) | |
465 | { | |
466 | if (c == ':') past_hname = TRUE; | |
467 | goto NEXT_CHAR; | |
80a47a2c TK |
468 | } |
469 | ||
3045f050 JH |
470 | if (where == PDKIM_HDR_LIMBO) |
471 | { | |
472 | /* In limbo, just wait for a tag-char to appear */ | |
473 | if (!(c >= 'a' && c <= 'z')) | |
474 | goto NEXT_CHAR; | |
80a47a2c | 475 | |
3045f050 | 476 | where = PDKIM_HDR_TAG; |
80a47a2c TK |
477 | } |
478 | ||
3045f050 JH |
479 | if (where == PDKIM_HDR_TAG) |
480 | { | |
3045f050 | 481 | if (c >= 'a' && c <= 'z') |
acec9514 | 482 | cur_tag = string_catn(cur_tag, p, 1); |
80a47a2c | 483 | |
3045f050 JH |
484 | if (c == '=') |
485 | { | |
acec9514 | 486 | if (Ustrcmp(string_from_gstring(cur_tag), "b") == 0) |
3045f050 | 487 | { |
ca9cb170 | 488 | *q++ = '='; |
3045f050 JH |
489 | in_b_val = TRUE; |
490 | } | |
491 | where = PDKIM_HDR_VALUE; | |
492 | goto NEXT_CHAR; | |
80a47a2c TK |
493 | } |
494 | } | |
495 | ||
3045f050 JH |
496 | if (where == PDKIM_HDR_VALUE) |
497 | { | |
3045f050 JH |
498 | if (c == '\r' || c == '\n' || c == ' ' || c == '\t') |
499 | goto NEXT_CHAR; | |
500 | ||
501 | if (c == ';' || c == '\0') | |
502 | { | |
acec9514 | 503 | if (cur_tag && cur_val) |
3045f050 | 504 | { |
acec9514 | 505 | (void) string_from_gstring(cur_val); |
3045f050 JH |
506 | pdkim_strtrim(cur_val); |
507 | ||
acec9514 | 508 | DEBUG(D_acl) debug_printf(" %s=%s\n", cur_tag->s, cur_val->s); |
3045f050 | 509 | |
acec9514 | 510 | switch (*cur_tag->s) |
3045f050 JH |
511 | { |
512 | case 'b': | |
acec9514 JH |
513 | pdkim_decode_base64(cur_val->s, |
514 | cur_tag->s[1] == 'h' ? &sig->bodyhash : &sig->sighash); | |
3045f050 JH |
515 | break; |
516 | case 'v': | |
517 | /* We only support version 1, and that is currently the | |
518 | only version there is. */ | |
07eeb4df | 519 | sig->version = |
acec9514 | 520 | Ustrcmp(cur_val->s, PDKIM_SIGNATURE_VERSION) == 0 ? 1 : -1; |
3045f050 JH |
521 | break; |
522 | case 'a': | |
d73e45df | 523 | { |
acec9514 | 524 | uschar * s = Ustrchr(cur_val->s, '-'); |
d73e45df JH |
525 | |
526 | for(i = 0; i < nelem(pdkim_keytypes); i++) | |
acec9514 | 527 | if (Ustrncmp(cur_val->s, pdkim_keytypes[i], s - cur_val->s) == 0) |
d73e45df JH |
528 | { sig->keytype = i; break; } |
529 | for (++s, i = 0; i < nelem(pdkim_hashes); i++) | |
530 | if (Ustrcmp(s, pdkim_hashes[i].dkim_hashname) == 0) | |
531 | { sig->hashtype = i; break; } | |
3045f050 | 532 | break; |
d73e45df JH |
533 | } |
534 | ||
3045f050 JH |
535 | case 'c': |
536 | for (i = 0; pdkim_combined_canons[i].str; i++) | |
acec9514 | 537 | if (Ustrcmp(cur_val->s, pdkim_combined_canons[i].str) == 0) |
3045f050 JH |
538 | { |
539 | sig->canon_headers = pdkim_combined_canons[i].canon_headers; | |
540 | sig->canon_body = pdkim_combined_canons[i].canon_body; | |
541 | break; | |
542 | } | |
543 | break; | |
544 | case 'q': | |
545 | for (i = 0; pdkim_querymethods[i]; i++) | |
acec9514 | 546 | if (Ustrcmp(cur_val->s, pdkim_querymethods[i]) == 0) |
3045f050 JH |
547 | { |
548 | sig->querymethod = i; | |
549 | break; | |
550 | } | |
551 | break; | |
552 | case 's': | |
acec9514 | 553 | sig->selector = string_copyn(cur_val->s, cur_val->ptr); break; |
3045f050 | 554 | case 'd': |
acec9514 | 555 | sig->domain = string_copyn(cur_val->s, cur_val->ptr); break; |
3045f050 | 556 | case 'i': |
acec9514 | 557 | sig->identity = pdkim_decode_qp(cur_val->s); break; |
3045f050 | 558 | case 't': |
acec9514 | 559 | sig->created = strtoul(CS cur_val->s, NULL, 10); break; |
3045f050 | 560 | case 'x': |
acec9514 | 561 | sig->expires = strtoul(CS cur_val->s, NULL, 10); break; |
3045f050 | 562 | case 'l': |
acec9514 | 563 | sig->bodylength = strtol(CS cur_val->s, NULL, 10); break; |
3045f050 | 564 | case 'h': |
acec9514 | 565 | sig->headernames = string_copyn(cur_val->s, cur_val->ptr); break; |
3045f050 | 566 | case 'z': |
acec9514 | 567 | sig->copiedheaders = pdkim_decode_qp(cur_val->s); break; |
3045f050 | 568 | default: |
0d04a285 | 569 | DEBUG(D_acl) debug_printf(" Unknown tag encountered\n"); |
3045f050 JH |
570 | break; |
571 | } | |
572 | } | |
acec9514 | 573 | cur_tag = cur_val = NULL; |
3045f050 JH |
574 | in_b_val = FALSE; |
575 | where = PDKIM_HDR_LIMBO; | |
80a47a2c | 576 | } |
3045f050 | 577 | else |
acec9514 | 578 | cur_val = string_catn(cur_val, p, 1); |
80a47a2c TK |
579 | } |
580 | ||
3045f050 JH |
581 | NEXT_CHAR: |
582 | if (c == '\0') | |
583 | break; | |
80a47a2c | 584 | |
3045f050 JH |
585 | if (!in_b_val) |
586 | *q++ = c; | |
80a47a2c TK |
587 | } |
588 | ||
3045f050 JH |
589 | *q = '\0'; |
590 | /* Chomp raw header. The final newline must not be added to the signature. */ | |
37f3dc43 JH |
591 | while (--q > sig->rawsig_no_b_val && (*q == '\r' || *q == '\n')) |
592 | *q = '\0'; | |
80a47a2c | 593 | |
0d04a285 | 594 | DEBUG(D_acl) |
3045f050 | 595 | { |
0d04a285 | 596 | debug_printf( |
3045f050 | 597 | "PDKIM >> Raw signature w/o b= tag value >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"); |
e2e3255a | 598 | pdkim_quoteprint(US sig->rawsig_no_b_val, Ustrlen(sig->rawsig_no_b_val)); |
0d04a285 | 599 | debug_printf( |
dcd03763 | 600 | "PDKIM >> Sig size: %4u bits\n", (unsigned) sig->sighash.len*8); |
0d04a285 | 601 | debug_printf( |
3045f050 | 602 | "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"); |
80a47a2c | 603 | } |
80a47a2c | 604 | |
9b2583c4 | 605 | /*XXX hash method: extend for sha512 */ |
7b83389d | 606 | if (!exim_sha_init(&sig->body_hash_ctx, |
d73e45df | 607 | pdkim_hashes[sig->hashtype].exim_hashmethod)) |
7b83389d | 608 | { |
d73e45df JH |
609 | DEBUG(D_acl) |
610 | debug_printf("PDKIM: hash init error, possibly nonhandled hashtype\n"); | |
7b83389d JH |
611 | return NULL; |
612 | } | |
3045f050 | 613 | return sig; |
80a47a2c TK |
614 | } |
615 | ||
616 | ||
617 | /* -------------------------------------------------------------------------- */ | |
80a47a2c | 618 | |
f444c2c7 | 619 | static pdkim_pubkey * |
e2e3255a | 620 | pdkim_parse_pubkey_record(pdkim_ctx *ctx, const uschar *raw_record) |
3045f050 | 621 | { |
35cf75e9 JH |
622 | const uschar * ele; |
623 | int sep = ';'; | |
624 | pdkim_pubkey * pub; | |
80a47a2c | 625 | |
ca9cb170 | 626 | pub = store_get(sizeof(pdkim_pubkey)); |
abe1010c | 627 | memset(pub, 0, sizeof(pdkim_pubkey)); |
80a47a2c | 628 | |
35cf75e9 JH |
629 | while ((ele = string_nextinlist(&raw_record, &sep, NULL, 0))) |
630 | { | |
631 | const uschar * val; | |
632 | ||
633 | if ((val = Ustrchr(ele, '='))) | |
3045f050 | 634 | { |
35cf75e9 | 635 | int taglen = val++ - ele; |
80a47a2c | 636 | |
35cf75e9 JH |
637 | DEBUG(D_acl) debug_printf(" %.*s=%s\n", taglen, ele, val); |
638 | switch (ele[0]) | |
3045f050 | 639 | { |
35cf75e9 JH |
640 | case 'v': pub->version = val; break; |
641 | case 'h': pub->hashes = val; break; | |
642 | case 'k': break; | |
643 | case 'g': pub->granularity = val; break; | |
644 | case 'n': pub->notes = pdkim_decode_qp(val); break; | |
645 | case 'p': pdkim_decode_base64(val, &pub->key); break; | |
646 | case 's': pub->srvtype = val; break; | |
647 | case 't': if (Ustrchr(val, 'y')) pub->testing = 1; | |
648 | if (Ustrchr(val, 's')) pub->no_subdomaining = 1; | |
e21a4d00 | 649 | break; |
35cf75e9 | 650 | default: DEBUG(D_acl) debug_printf(" Unknown tag encountered\n"); break; |
80a47a2c | 651 | } |
e21a4d00 | 652 | } |
35cf75e9 | 653 | } |
80a47a2c | 654 | |
3045f050 | 655 | /* Set fallback defaults */ |
ca9cb170 | 656 | if (!pub->version ) pub->version = string_copy(PDKIM_PUB_RECORD_VERSION); |
135e9496 JH |
657 | else if (Ustrcmp(pub->version, PDKIM_PUB_RECORD_VERSION) != 0) |
658 | { | |
659 | DEBUG(D_acl) debug_printf(" Bad v= field\n"); | |
660 | return NULL; | |
661 | } | |
e21a4d00 | 662 | |
35cf75e9 | 663 | if (!pub->granularity) pub->granularity = US"*"; |
e21a4d00 | 664 | /* |
35cf75e9 | 665 | if (!pub->keytype ) pub->keytype = US"rsa"; |
e21a4d00 | 666 | */ |
35cf75e9 | 667 | if (!pub->srvtype ) pub->srvtype = US"*"; |
80a47a2c | 668 | |
3045f050 | 669 | /* p= is required */ |
2592e6c0 | 670 | if (pub->key.data) |
80a47a2c | 671 | return pub; |
3045f050 | 672 | |
135e9496 | 673 | DEBUG(D_acl) debug_printf(" Missing p= field\n"); |
3045f050 | 674 | return NULL; |
80a47a2c TK |
675 | } |
676 | ||
677 | ||
678 | /* -------------------------------------------------------------------------- */ | |
3045f050 | 679 | |
9e70917d JH |
680 | /* Update the bodyhash for one sig, with some additional data. |
681 | If we have to relax the data for this sig, return our copy of it. */ | |
682 | ||
683 | /*XXX Currently we calculate a hash for each sig. But it is possible | |
684 | that multi-signing will be wanted using different signing algos | |
685 | (rsa, ec) using the same hash and canonicalization. Consider in future | |
686 | hanging the hash+cacnon from the ctx and only referencing from the sig, | |
687 | so that it can be calculated only once - being over the body this | |
688 | caould be meagbytes, hence expensive. */ | |
689 | ||
690 | static blob * | |
691 | pdkim_update_sig_bodyhash(pdkim_signature * sig, blob * orig_data, blob * relaxed_data) | |
3045f050 | 692 | { |
9e70917d JH |
693 | blob * canon_data = orig_data; |
694 | /* Defaults to simple canon (no further treatment necessary) */ | |
3045f050 | 695 | |
9e70917d | 696 | if (sig->canon_body == PDKIM_CANON_RELAXED) |
3045f050 | 697 | { |
9e70917d JH |
698 | /* Relax the line if not done already */ |
699 | if (!relaxed_data) | |
3045f050 | 700 | { |
9e70917d | 701 | BOOL seen_wsp = FALSE; |
7845dbb3 | 702 | const uschar * p; |
9e70917d | 703 | int q = 0; |
3045f050 | 704 | |
9e70917d JH |
705 | /* We want to be able to free this else we allocate |
706 | for the entire message which could be many MB. Since | |
707 | we don't know what allocations the SHA routines might | |
708 | do, not safe to use store_get()/store_reset(). */ | |
d5bccfc8 | 709 | |
9e70917d JH |
710 | relaxed_data = store_malloc(sizeof(blob) + orig_data->len+1); |
711 | relaxed_data->data = US (relaxed_data+1); | |
3045f050 | 712 | |
9e70917d JH |
713 | for (p = orig_data->data; *p; p++) |
714 | { | |
715 | char c = *p; | |
716 | if (c == '\r') | |
717 | { | |
718 | if (q > 0 && relaxed_data->data[q-1] == ' ') | |
719 | q--; | |
720 | } | |
721 | else if (c == '\t' || c == ' ') | |
722 | { | |
723 | c = ' '; /* Turns WSP into SP */ | |
724 | if (seen_wsp) | |
725 | continue; | |
726 | seen_wsp = TRUE; | |
3045f050 | 727 | } |
9e70917d JH |
728 | else |
729 | seen_wsp = FALSE; | |
730 | relaxed_data->data[q++] = c; | |
80a47a2c | 731 | } |
9e70917d JH |
732 | relaxed_data->data[q] = '\0'; |
733 | relaxed_data->len = q; | |
80a47a2c | 734 | } |
9e70917d JH |
735 | canon_data = relaxed_data; |
736 | } | |
80a47a2c | 737 | |
9e70917d JH |
738 | /* Make sure we don't exceed the to-be-signed body length */ |
739 | if ( sig->bodylength >= 0 | |
740 | && sig->signed_body_bytes + (unsigned long)canon_data->len > sig->bodylength | |
741 | ) | |
742 | canon_data->len = sig->bodylength - sig->signed_body_bytes; | |
80a47a2c | 743 | |
9e70917d JH |
744 | if (canon_data->len > 0) |
745 | { | |
746 | exim_sha_update(&sig->body_hash_ctx, CUS canon_data->data, canon_data->len); | |
747 | sig->signed_body_bytes += canon_data->len; | |
748 | DEBUG(D_acl) pdkim_quoteprint(canon_data->data, canon_data->len); | |
80a47a2c TK |
749 | } |
750 | ||
9e70917d | 751 | return relaxed_data; |
6ab02e3f | 752 | } |
80a47a2c TK |
753 | |
754 | ||
755 | /* -------------------------------------------------------------------------- */ | |
80a47a2c | 756 | |
ca9cb170 | 757 | static void |
9e70917d | 758 | pdkim_finish_bodyhash(pdkim_ctx * ctx) |
3045f050 | 759 | { |
9e70917d | 760 | pdkim_signature * sig; |
80a47a2c | 761 | |
3045f050 | 762 | /* Traverse all signatures */ |
f4d091fb | 763 | for (sig = ctx->sig; sig; sig = sig->next) |
3045f050 | 764 | { /* Finish hashes */ |
2592e6c0 JH |
765 | blob bh; |
766 | ||
dcd03763 | 767 | exim_sha_finish(&sig->body_hash_ctx, &bh); |
3045f050 | 768 | |
0d04a285 | 769 | DEBUG(D_acl) |
3045f050 | 770 | { |
0d04a285 | 771 | debug_printf("PDKIM [%s] Body bytes hashed: %lu\n" |
9e70917d JH |
772 | "PDKIM [%s] Body %s computed: ", |
773 | sig->domain, sig->signed_body_bytes, | |
774 | sig->domain, pdkim_hashes[sig->hashtype].dkim_hashname); | |
b78006ac | 775 | pdkim_hexprint(CUS bh.data, bh.len); |
80a47a2c | 776 | } |
3045f050 JH |
777 | |
778 | /* SIGNING -------------------------------------------------------------- */ | |
e983e85a | 779 | if (ctx->flags & PDKIM_MODE_SIGN) |
3045f050 | 780 | { |
2592e6c0 | 781 | sig->bodyhash = bh; |
3045f050 JH |
782 | |
783 | /* If bodylength limit is set, and we have received less bytes | |
784 | than the requested amount, effectively remove the limit tag. */ | |
785 | if (sig->signed_body_bytes < sig->bodylength) | |
786 | sig->bodylength = -1; | |
80a47a2c | 787 | } |
3045f050 | 788 | |
3045f050 | 789 | else |
02c4f8fb JH |
790 | /* VERIFICATION --------------------------------------------------------- */ |
791 | /* Be careful that the header sig included a bodyash */ | |
792 | ||
793 | if (sig->bodyhash.data && memcmp(bh.data, sig->bodyhash.data, bh.len) == 0) | |
3045f050 | 794 | { |
0d04a285 | 795 | DEBUG(D_acl) debug_printf("PDKIM [%s] Body hash verified OK\n", sig->domain); |
80a47a2c | 796 | } |
3045f050 JH |
797 | else |
798 | { | |
0d04a285 | 799 | DEBUG(D_acl) |
3045f050 | 800 | { |
e21a4d00 | 801 | debug_printf("PDKIM [%s] Body hash signature from headers: ", sig->domain); |
dcd03763 | 802 | pdkim_hexprint(sig->bodyhash.data, sig->bodyhash.len); |
0d04a285 | 803 | debug_printf("PDKIM [%s] Body hash did NOT verify\n", sig->domain); |
3045f050 | 804 | } |
3045f050 JH |
805 | sig->verify_status = PDKIM_VERIFY_FAIL; |
806 | sig->verify_ext_status = PDKIM_VERIFY_FAIL_BODY; | |
80a47a2c | 807 | } |
80a47a2c | 808 | } |
6ab02e3f | 809 | } |
80a47a2c TK |
810 | |
811 | ||
812 | ||
9e70917d | 813 | static void |
e983e85a JH |
814 | pdkim_body_complete(pdkim_ctx * ctx) |
815 | { | |
9e70917d | 816 | pdkim_signature * sig; |
e983e85a JH |
817 | |
818 | /* In simple body mode, if any empty lines were buffered, | |
819 | replace with one. rfc 4871 3.4.3 */ | |
820 | /*XXX checking the signed-body-bytes is a gross hack; I think | |
821 | it indicates that all linebreaks should be buffered, including | |
822 | the one terminating a text line */ | |
823 | ||
9e70917d JH |
824 | for (sig = ctx->sig; sig; sig = sig->next) |
825 | if ( sig->canon_body == PDKIM_CANON_SIMPLE | |
826 | && sig->signed_body_bytes == 0 | |
827 | && sig->num_buffered_blanklines > 0 | |
828 | ) | |
829 | (void) pdkim_update_sig_bodyhash(sig, &lineending, NULL); | |
e983e85a JH |
830 | |
831 | ctx->flags |= PDKIM_SEEN_EOD; | |
832 | ctx->linebuf_offset = 0; | |
e983e85a JH |
833 | } |
834 | ||
835 | ||
836 | ||
80a47a2c | 837 | /* -------------------------------------------------------------------------- */ |
e983e85a | 838 | /* Call from pdkim_feed below for processing complete body lines */ |
3045f050 | 839 | |
9e70917d JH |
840 | static void |
841 | pdkim_bodyline_complete(pdkim_ctx * ctx) | |
3045f050 | 842 | { |
9e70917d JH |
843 | blob line = {.data = ctx->linebuf, .len = ctx->linebuf_offset}; |
844 | pdkim_signature * sig; | |
845 | blob * rnl = NULL; | |
846 | blob * rline = NULL; | |
3045f050 JH |
847 | |
848 | /* Ignore extra data if we've seen the end-of-data marker */ | |
9e70917d | 849 | if (ctx->flags & PDKIM_SEEN_EOD) goto all_skip; |
3045f050 JH |
850 | |
851 | /* We've always got one extra byte to stuff a zero ... */ | |
9e70917d | 852 | ctx->linebuf[line.len] = '\0'; |
3045f050 | 853 | |
0d04a285 | 854 | /* Terminate on EOD marker */ |
e983e85a | 855 | if (ctx->flags & PDKIM_DOT_TERM) |
3045f050 | 856 | { |
9e70917d JH |
857 | if (memcmp(line.data, ".\r\n", 3) == 0) |
858 | { pdkim_body_complete(ctx); return; } | |
0d04a285 | 859 | |
e983e85a | 860 | /* Unstuff dots */ |
9e70917d JH |
861 | if (memcmp(line.data, "..", 2) == 0) |
862 | { line.data++; line.len--; } | |
80a47a2c TK |
863 | } |
864 | ||
3045f050 | 865 | /* Empty lines need to be buffered until we find a non-empty line */ |
9e70917d | 866 | if (memcmp(line.data, "\r\n", 2) == 0) |
3045f050 | 867 | { |
9e70917d JH |
868 | for (sig = ctx->sig; sig; sig = sig->next) sig->num_buffered_blanklines++; |
869 | goto all_skip; | |
80a47a2c TK |
870 | } |
871 | ||
9e70917d JH |
872 | /* Process line for each sig separately */ |
873 | for (sig = ctx->sig; sig; sig = sig->next) | |
3045f050 | 874 | { |
9e70917d | 875 | if (sig->canon_body == PDKIM_CANON_RELAXED) |
3045f050 | 876 | { |
9e70917d | 877 | /* Lines with just spaces need to be buffered too */ |
7845dbb3 | 878 | uschar * cp = line.data; |
9e70917d | 879 | char c; |
6a11a9e6 | 880 | |
9e70917d JH |
881 | while ((c = *cp)) |
882 | { | |
883 | if (c == '\r' && cp[1] == '\n') break; | |
884 | if (c != ' ' && c != '\t') goto sig_process; | |
885 | cp++; | |
886 | } | |
887 | ||
888 | sig->num_buffered_blanklines++; | |
889 | goto sig_skip; | |
6a11a9e6 JH |
890 | } |
891 | ||
9e70917d JH |
892 | sig_process: |
893 | /* At this point, we have a non-empty line, so release the buffered ones. */ | |
6a11a9e6 | 894 | |
9e70917d JH |
895 | while (sig->num_buffered_blanklines) |
896 | { | |
897 | rnl = pdkim_update_sig_bodyhash(sig, &lineending, rnl); | |
898 | sig->num_buffered_blanklines--; | |
899 | } | |
900 | ||
901 | rline = pdkim_update_sig_bodyhash(sig, &line, rline); | |
902 | sig_skip: ; | |
80a47a2c TK |
903 | } |
904 | ||
9e70917d JH |
905 | if (rnl) store_free(rnl); |
906 | if (rline) store_free(rline); | |
907 | ||
908 | all_skip: | |
80a47a2c | 909 | |
3045f050 | 910 | ctx->linebuf_offset = 0; |
9e70917d | 911 | return; |
80a47a2c TK |
912 | } |
913 | ||
914 | ||
915 | /* -------------------------------------------------------------------------- */ | |
916 | /* Callback from pdkim_feed below for processing complete headers */ | |
917 | #define DKIM_SIGNATURE_HEADERNAME "DKIM-Signature:" | |
3045f050 | 918 | |
f444c2c7 | 919 | static int |
2c0f3ea1 | 920 | pdkim_header_complete(pdkim_ctx * ctx) |
3045f050 | 921 | { |
2c0f3ea1 JH |
922 | pdkim_signature * sig, * last_sig; |
923 | ||
3045f050 | 924 | /* Special case: The last header can have an extra \r appended */ |
acec9514 JH |
925 | if ( (ctx->cur_header->ptr > 1) && |
926 | (ctx->cur_header->s[ctx->cur_header->ptr-1] == '\r') ) | |
927 | --ctx->cur_header->ptr; | |
928 | (void) string_from_gstring(ctx->cur_header); | |
80a47a2c | 929 | |
2c0f3ea1 | 930 | if (++ctx->num_headers > PDKIM_MAX_HEADERS) goto BAIL; |
80a47a2c | 931 | |
3045f050 | 932 | /* SIGNING -------------------------------------------------------------- */ |
e983e85a | 933 | if (ctx->flags & PDKIM_MODE_SIGN) |
0d04a285 | 934 | for (sig = ctx->sig; sig; sig = sig->next) /* Traverse all signatures */ |
80a47a2c | 935 | |
ab9152ff | 936 | /* Add header to the signed headers list (in reverse order) */ |
acec9514 | 937 | sig->headers = pdkim_prepend_stringlist(sig->headers, ctx->cur_header->s); |
94431adb | 938 | |
0d04a285 | 939 | /* VERIFICATION ----------------------------------------------------------- */ |
3045f050 | 940 | /* DKIM-Signature: headers are added to the verification list */ |
e983e85a | 941 | else |
3045f050 | 942 | { |
f6ee24a2 | 943 | #ifdef notdef |
bd8fbe36 JH |
944 | DEBUG(D_acl) |
945 | { | |
946 | debug_printf("PDKIM >> raw hdr: "); | |
acec9514 | 947 | pdkim_quoteprint(CUS ctx->cur_header->s, ctx->cur_header->ptr); |
bd8fbe36 | 948 | } |
f6ee24a2 | 949 | #endif |
acec9514 | 950 | if (strncasecmp(CCS ctx->cur_header->s, |
3045f050 | 951 | DKIM_SIGNATURE_HEADERNAME, |
e2e3255a | 952 | Ustrlen(DKIM_SIGNATURE_HEADERNAME)) == 0) |
3045f050 | 953 | { |
02c4f8fb JH |
954 | /* Create and chain new signature block. We could error-check for all |
955 | required tags here, but prefer to create the internal sig and expicitly | |
956 | fail verification of it later. */ | |
80a47a2c | 957 | |
0d04a285 | 958 | DEBUG(D_acl) debug_printf( |
3045f050 | 959 | "PDKIM >> Found sig, trying to parse >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"); |
3045f050 | 960 | |
acec9514 | 961 | sig = pdkim_parse_sig_header(ctx, ctx->cur_header->s); |
02c4f8fb JH |
962 | |
963 | if (!(last_sig = ctx->sig)) | |
2c0f3ea1 | 964 | ctx->sig = sig; |
02c4f8fb | 965 | else |
3045f050 | 966 | { |
02c4f8fb | 967 | while (last_sig->next) last_sig = last_sig->next; |
2c0f3ea1 | 968 | last_sig->next = sig; |
80a47a2c TK |
969 | } |
970 | } | |
37f8b554 | 971 | |
eea19017 | 972 | /* all headers are stored for signature verification */ |
acec9514 | 973 | ctx->headers = pdkim_prepend_stringlist(ctx->headers, ctx->cur_header->s); |
80a47a2c TK |
974 | } |
975 | ||
3045f050 | 976 | BAIL: |
acec9514 | 977 | ctx->cur_header->s[ctx->cur_header->ptr = 0] = '\0'; /* leave buffer for reuse */ |
3045f050 | 978 | return PDKIM_OK; |
6ab02e3f | 979 | } |
80a47a2c TK |
980 | |
981 | ||
982 | ||
983 | /* -------------------------------------------------------------------------- */ | |
984 | #define HEADER_BUFFER_FRAG_SIZE 256 | |
3045f050 JH |
985 | |
986 | DLLEXPORT int | |
ef698bf6 | 987 | pdkim_feed(pdkim_ctx * ctx, uschar * data, int len) |
3045f050 | 988 | { |
02c4f8fb | 989 | int p, rc; |
3045f050 | 990 | |
e983e85a JH |
991 | /* Alternate EOD signal, used in non-dotstuffing mode */ |
992 | if (!data) | |
993 | pdkim_body_complete(ctx); | |
994 | ||
10c50704 | 995 | else for (p = 0; p<len; p++) |
3045f050 | 996 | { |
ca9cb170 | 997 | uschar c = data[p]; |
3045f050 | 998 | |
e983e85a | 999 | if (ctx->flags & PDKIM_PAST_HDRS) |
3045f050 | 1000 | { |
b895f4b2 JH |
1001 | if (c == '\n' && !(ctx->flags & PDKIM_SEEN_CR)) /* emulate the CR */ |
1002 | { | |
1003 | ctx->linebuf[ctx->linebuf_offset++] = '\r'; | |
1004 | if (ctx->linebuf_offset == PDKIM_MAX_BODY_LINE_LEN-1) | |
1005 | return PDKIM_ERR_LONG_LINE; | |
1006 | } | |
1007 | ||
3045f050 | 1008 | /* Processing body byte */ |
0d04a285 | 1009 | ctx->linebuf[ctx->linebuf_offset++] = c; |
b895f4b2 JH |
1010 | if (c == '\r') |
1011 | ctx->flags |= PDKIM_SEEN_CR; | |
1012 | else if (c == '\n') | |
3045f050 | 1013 | { |
b895f4b2 | 1014 | ctx->flags &= ~PDKIM_SEEN_CR; |
9e70917d | 1015 | pdkim_bodyline_complete(ctx); |
80a47a2c | 1016 | } |
b895f4b2 JH |
1017 | |
1018 | if (ctx->linebuf_offset == PDKIM_MAX_BODY_LINE_LEN-1) | |
3045f050 | 1019 | return PDKIM_ERR_LONG_LINE; |
80a47a2c | 1020 | } |
3045f050 JH |
1021 | else |
1022 | { | |
1023 | /* Processing header byte */ | |
b895f4b2 JH |
1024 | if (c == '\r') |
1025 | ctx->flags |= PDKIM_SEEN_CR; | |
1026 | else if (c == '\n') | |
3045f050 | 1027 | { |
b895f4b2 | 1028 | if (!(ctx->flags & PDKIM_SEEN_CR)) /* emulate the CR */ |
acec9514 | 1029 | ctx->cur_header = string_catn(ctx->cur_header, CUS "\r", 1); |
b895f4b2 | 1030 | |
02c4f8fb | 1031 | if (ctx->flags & PDKIM_SEEN_LF) /* Seen last header line */ |
b895f4b2 | 1032 | { |
02c4f8fb JH |
1033 | if ((rc = pdkim_header_complete(ctx)) != PDKIM_OK) |
1034 | return rc; | |
b895f4b2 | 1035 | |
863bd541 | 1036 | ctx->flags = (ctx->flags & ~(PDKIM_SEEN_LF|PDKIM_SEEN_CR)) | PDKIM_PAST_HDRS; |
b895f4b2 | 1037 | DEBUG(D_acl) debug_printf( |
02c4f8fb | 1038 | "PDKIM >> Body data for hash, canonicalized >>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"); |
b895f4b2 | 1039 | continue; |
3045f050 | 1040 | } |
b895f4b2 | 1041 | else |
863bd541 | 1042 | ctx->flags = (ctx->flags & ~PDKIM_SEEN_CR) | PDKIM_SEEN_LF; |
b895f4b2 JH |
1043 | } |
1044 | else if (ctx->flags & PDKIM_SEEN_LF) | |
1045 | { | |
02c4f8fb JH |
1046 | if (!(c == '\t' || c == ' ')) /* End of header */ |
1047 | if ((rc = pdkim_header_complete(ctx)) != PDKIM_OK) | |
1048 | return rc; | |
b895f4b2 | 1049 | ctx->flags &= ~PDKIM_SEEN_LF; |
80a47a2c | 1050 | } |
3045f050 | 1051 | |
acec9514 JH |
1052 | if (!ctx->cur_header || ctx->cur_header->ptr < PDKIM_MAX_HEADER_LEN) |
1053 | ctx->cur_header = string_catn(ctx->cur_header, CUS &data[p], 1); | |
80a47a2c TK |
1054 | } |
1055 | } | |
3045f050 | 1056 | return PDKIM_OK; |
6ab02e3f | 1057 | } |
80a47a2c | 1058 | |
ca9cb170 JH |
1059 | |
1060 | ||
acec9514 JH |
1061 | /* Extend a growing header with a continuation-linebreak */ |
1062 | static gstring * | |
1063 | pdkim_hdr_cont(gstring * str, int * col) | |
ca9cb170 JH |
1064 | { |
1065 | *col = 1; | |
acec9514 | 1066 | return string_catn(str, US"\r\n\t", 3); |
ca9cb170 JH |
1067 | } |
1068 | ||
1069 | ||
1070 | ||
05b7d6de JB |
1071 | /* |
1072 | * RFC 5322 specifies that header line length SHOULD be no more than 78 | |
1073 | * lets make it so! | |
1074 | * pdkim_headcat | |
ca9cb170 JH |
1075 | * |
1076 | * returns uschar * (not nul-terminated) | |
05b7d6de JB |
1077 | * |
1078 | * col: this int holds and receives column number (octets since last '\n') | |
1079 | * str: partial string to append to | |
94431adb | 1080 | * pad: padding, split line or space after before or after eg: ";" |
05b7d6de JB |
1081 | * intro: - must join to payload eg "h=", usually the tag name |
1082 | * payload: eg base64 data - long data can be split arbitrarily. | |
1083 | * | |
1084 | * this code doesn't fold the header in some of the places that RFC4871 | |
1085 | * allows: As per RFC5322(2.2.3) it only folds before or after tag-value | |
1086 | * pairs and inside long values. it also always spaces or breaks after the | |
94431adb | 1087 | * "pad" |
05b7d6de JB |
1088 | * |
1089 | * no guarantees are made for output given out-of range input. like tag | |
f444c2c7 | 1090 | * names longer than 78, or bogus col. Input is assumed to be free of line breaks. |
05b7d6de JB |
1091 | */ |
1092 | ||
acec9514 JH |
1093 | static gstring * |
1094 | pdkim_headcat(int * col, gstring * str, | |
ca9cb170 | 1095 | const uschar * pad, const uschar * intro, const uschar * payload) |
3045f050 JH |
1096 | { |
1097 | size_t l; | |
1098 | ||
1099 | if (pad) | |
05b7d6de | 1100 | { |
ca9cb170 | 1101 | l = Ustrlen(pad); |
3045f050 | 1102 | if (*col + l > 78) |
acec9514 JH |
1103 | str = pdkim_hdr_cont(str, col); |
1104 | str = string_catn(str, pad, l); | |
3045f050 | 1105 | *col += l; |
05b7d6de JB |
1106 | } |
1107 | ||
ca9cb170 | 1108 | l = (pad?1:0) + (intro?Ustrlen(intro):0); |
05b7d6de | 1109 | |
3045f050 | 1110 | if (*col + l > 78) |
05b7d6de | 1111 | { /*can't fit intro - start a new line to make room.*/ |
acec9514 | 1112 | str = pdkim_hdr_cont(str, col); |
ca9cb170 | 1113 | l = intro?Ustrlen(intro):0; |
05b7d6de JB |
1114 | } |
1115 | ||
ca9cb170 | 1116 | l += payload ? Ustrlen(payload):0 ; |
05b7d6de | 1117 | |
3045f050 | 1118 | while (l>77) |
05b7d6de | 1119 | { /* this fragment will not fit on a single line */ |
3045f050 | 1120 | if (pad) |
05b7d6de | 1121 | { |
acec9514 | 1122 | str = string_catn(str, US" ", 1); |
3045f050 JH |
1123 | *col += 1; |
1124 | pad = NULL; /* only want this once */ | |
1125 | l--; | |
05b7d6de | 1126 | } |
3045f050 JH |
1127 | |
1128 | if (intro) | |
05b7d6de | 1129 | { |
ca9cb170 | 1130 | size_t sl = Ustrlen(intro); |
3045f050 | 1131 | |
acec9514 | 1132 | str = string_catn(str, intro, sl); |
3045f050 JH |
1133 | *col += sl; |
1134 | l -= sl; | |
1135 | intro = NULL; /* only want this once */ | |
05b7d6de | 1136 | } |
3045f050 JH |
1137 | |
1138 | if (payload) | |
05b7d6de | 1139 | { |
ca9cb170 | 1140 | size_t sl = Ustrlen(payload); |
3045f050 JH |
1141 | size_t chomp = *col+sl < 77 ? sl : 78-*col; |
1142 | ||
acec9514 | 1143 | str = string_catn(str, payload, chomp); |
3045f050 JH |
1144 | *col += chomp; |
1145 | payload += chomp; | |
1146 | l -= chomp-1; | |
05b7d6de | 1147 | } |
3045f050 JH |
1148 | |
1149 | /* the while precondition tells us it didn't fit. */ | |
acec9514 | 1150 | str = pdkim_hdr_cont(str, col); |
05b7d6de | 1151 | } |
3045f050 JH |
1152 | |
1153 | if (*col + l > 78) | |
05b7d6de | 1154 | { |
acec9514 | 1155 | str = pdkim_hdr_cont(str, col); |
3045f050 | 1156 | pad = NULL; |
05b7d6de JB |
1157 | } |
1158 | ||
3045f050 | 1159 | if (pad) |
05b7d6de | 1160 | { |
acec9514 | 1161 | str = string_catn(str, US" ", 1); |
3045f050 JH |
1162 | *col += 1; |
1163 | pad = NULL; | |
05b7d6de JB |
1164 | } |
1165 | ||
3045f050 | 1166 | if (intro) |
05b7d6de | 1167 | { |
ca9cb170 | 1168 | size_t sl = Ustrlen(intro); |
3045f050 | 1169 | |
acec9514 | 1170 | str = string_catn(str, intro, sl); |
3045f050 JH |
1171 | *col += sl; |
1172 | l -= sl; | |
1173 | intro = NULL; | |
05b7d6de | 1174 | } |
3045f050 JH |
1175 | |
1176 | if (payload) | |
05b7d6de | 1177 | { |
ca9cb170 | 1178 | size_t sl = Ustrlen(payload); |
3045f050 | 1179 | |
acec9514 | 1180 | str = string_catn(str, payload, sl); |
3045f050 | 1181 | *col += sl; |
05b7d6de JB |
1182 | } |
1183 | ||
ca9cb170 | 1184 | return str; |
05b7d6de | 1185 | } |
80a47a2c | 1186 | |
3045f050 | 1187 | |
80a47a2c | 1188 | /* -------------------------------------------------------------------------- */ |
3045f050 | 1189 | |
ca9cb170 | 1190 | static uschar * |
9e70917d | 1191 | pdkim_create_header(pdkim_signature * sig, BOOL final) |
3045f050 | 1192 | { |
ca9cb170 JH |
1193 | uschar * base64_bh; |
1194 | uschar * base64_b; | |
3045f050 | 1195 | int col = 0; |
acec9514 JH |
1196 | gstring * hdr; |
1197 | gstring * canon_all; | |
3045f050 | 1198 | |
acec9514 JH |
1199 | canon_all = string_cat (NULL, pdkim_canons[sig->canon_headers]); |
1200 | canon_all = string_catn(canon_all, US"/", 1); | |
1201 | canon_all = string_cat (canon_all, pdkim_canons[sig->canon_body]); | |
1202 | (void) string_from_gstring(canon_all); | |
3045f050 | 1203 | |
acec9514 JH |
1204 | hdr = string_cat(NULL, US"DKIM-Signature: v="PDKIM_SIGNATURE_VERSION); |
1205 | col = hdr->ptr; | |
3045f050 JH |
1206 | |
1207 | /* Required and static bits */ | |
acec9514 JH |
1208 | hdr = pdkim_headcat(&col, hdr, US";", US"a=", dkim_sig_to_a_tag(sig)); |
1209 | hdr = pdkim_headcat(&col, hdr, US";", US"q=", pdkim_querymethods[sig->querymethod]); | |
1210 | hdr = pdkim_headcat(&col, hdr, US";", US"c=", canon_all->s); | |
1211 | hdr = pdkim_headcat(&col, hdr, US";", US"d=", sig->domain); | |
1212 | hdr = pdkim_headcat(&col, hdr, US";", US"s=", sig->selector); | |
ca9cb170 JH |
1213 | |
1214 | /* list of header names can be split between items. */ | |
3045f050 | 1215 | { |
e2e3255a | 1216 | uschar * n = string_copy(sig->headernames); |
ca9cb170 JH |
1217 | uschar * i = US"h="; |
1218 | uschar * s = US";"; | |
1219 | ||
1220 | while (*n) | |
05b7d6de | 1221 | { |
ca9cb170 | 1222 | uschar * c = Ustrchr(n, ':'); |
3045f050 | 1223 | |
ca9cb170 | 1224 | if (c) *c ='\0'; |
05b7d6de | 1225 | |
ca9cb170 | 1226 | if (!i) |
acec9514 | 1227 | hdr = pdkim_headcat(&col, hdr, NULL, NULL, US":"); |
05b7d6de | 1228 | |
acec9514 | 1229 | hdr = pdkim_headcat(&col, hdr, s, i, n); |
3045f050 | 1230 | |
ca9cb170 JH |
1231 | if (!c) |
1232 | break; | |
3045f050 | 1233 | |
ca9cb170 JH |
1234 | n = c+1; |
1235 | s = NULL; | |
1236 | i = NULL; | |
80a47a2c | 1237 | } |
ca9cb170 | 1238 | } |
05b7d6de | 1239 | |
ca9cb170 | 1240 | base64_bh = pdkim_encode_base64(&sig->bodyhash); |
acec9514 | 1241 | hdr = pdkim_headcat(&col, hdr, US";", US"bh=", base64_bh); |
3045f050 | 1242 | |
ca9cb170 JH |
1243 | /* Optional bits */ |
1244 | if (sig->identity) | |
acec9514 | 1245 | hdr = pdkim_headcat(&col, hdr, US";", US"i=", sig->identity); |
3045f050 | 1246 | |
ca9cb170 JH |
1247 | if (sig->created > 0) |
1248 | { | |
e2e3255a | 1249 | uschar minibuf[20]; |
3045f050 | 1250 | |
e2e3255a | 1251 | snprintf(CS minibuf, sizeof(minibuf), "%lu", sig->created); |
acec9514 | 1252 | hdr = pdkim_headcat(&col, hdr, US";", US"t=", minibuf); |
ca9cb170 | 1253 | } |
3045f050 | 1254 | |
ca9cb170 JH |
1255 | if (sig->expires > 0) |
1256 | { | |
e2e3255a | 1257 | uschar minibuf[20]; |
3045f050 | 1258 | |
e2e3255a | 1259 | snprintf(CS minibuf, sizeof(minibuf), "%lu", sig->expires); |
acec9514 | 1260 | hdr = pdkim_headcat(&col, hdr, US";", US"x=", minibuf); |
ca9cb170 | 1261 | } |
3045f050 | 1262 | |
ca9cb170 JH |
1263 | if (sig->bodylength >= 0) |
1264 | { | |
e2e3255a | 1265 | uschar minibuf[20]; |
80a47a2c | 1266 | |
e2e3255a | 1267 | snprintf(CS minibuf, sizeof(minibuf), "%lu", sig->bodylength); |
acec9514 | 1268 | hdr = pdkim_headcat(&col, hdr, US";", US"l=", minibuf); |
3045f050 | 1269 | } |
05b7d6de | 1270 | |
ca9cb170 | 1271 | /* Preliminary or final version? */ |
ea18931d JH |
1272 | if (final) |
1273 | { | |
1274 | base64_b = pdkim_encode_base64(&sig->sighash); | |
acec9514 | 1275 | hdr = pdkim_headcat(&col, hdr, US";", US"b=", base64_b); |
80a47a2c | 1276 | |
ea18931d | 1277 | /* add trailing semicolon: I'm not sure if this is actually needed */ |
acec9514 | 1278 | hdr = pdkim_headcat(&col, hdr, NULL, US";", US""); |
ea18931d JH |
1279 | } |
1280 | else | |
1281 | { | |
1282 | /* To satisfy the rule "all surrounding whitespace [...] deleted" | |
1283 | ( RFC 6376 section 3.7 ) we ensure there is no whitespace here. Otherwise | |
1284 | the headcat routine could insert a linebreak which the relaxer would reduce | |
1285 | to a single space preceding the terminating semicolon, resulting in an | |
1286 | incorrect header-hash. */ | |
acec9514 | 1287 | hdr = pdkim_headcat(&col, hdr, US";", US"b=;", US""); |
ea18931d | 1288 | } |
80a47a2c | 1289 | |
acec9514 | 1290 | return string_from_gstring(hdr); |
80a47a2c TK |
1291 | } |
1292 | ||
1293 | ||
cd1a5fe0 JH |
1294 | /* -------------------------------------------------------------------------- */ |
1295 | ||
1296 | static pdkim_pubkey * | |
b9df1829 JH |
1297 | pdkim_key_from_dns(pdkim_ctx * ctx, pdkim_signature * sig, ev_ctx * vctx, |
1298 | const uschar ** errstr) | |
cd1a5fe0 JH |
1299 | { |
1300 | uschar * dns_txt_name, * dns_txt_reply; | |
1301 | pdkim_pubkey * p; | |
cd1a5fe0 JH |
1302 | |
1303 | /* Fetch public key for signing domain, from DNS */ | |
1304 | ||
1305 | dns_txt_name = string_sprintf("%s._domainkey.%s.", sig->selector, sig->domain); | |
1306 | ||
1307 | dns_txt_reply = store_get(PDKIM_DNS_TXT_MAX_RECLEN); | |
1308 | memset(dns_txt_reply, 0, PDKIM_DNS_TXT_MAX_RECLEN); | |
1309 | ||
1310 | if ( ctx->dns_txt_callback(CS dns_txt_name, CS dns_txt_reply) != PDKIM_OK | |
1311 | || dns_txt_reply[0] == '\0' | |
1312 | ) | |
1313 | { | |
1314 | sig->verify_status = PDKIM_VERIFY_INVALID; | |
1315 | sig->verify_ext_status = PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE; | |
1316 | return NULL; | |
1317 | } | |
1318 | ||
1319 | DEBUG(D_acl) | |
1320 | { | |
1321 | debug_printf( | |
1322 | "PDKIM >> Parsing public key record >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n" | |
9e70917d JH |
1323 | " %s\n" |
1324 | " Raw record: ", | |
1325 | dns_txt_name); | |
cd1a5fe0 JH |
1326 | pdkim_quoteprint(CUS dns_txt_reply, Ustrlen(dns_txt_reply)); |
1327 | } | |
1328 | ||
1329 | if ( !(p = pdkim_parse_pubkey_record(ctx, CUS dns_txt_reply)) | |
1330 | || (Ustrcmp(p->srvtype, "*") != 0 && Ustrcmp(p->srvtype, "email") != 0) | |
1331 | ) | |
1332 | { | |
1333 | sig->verify_status = PDKIM_VERIFY_INVALID; | |
1334 | sig->verify_ext_status = PDKIM_VERIFY_INVALID_PUBKEY_DNSRECORD; | |
1335 | ||
1336 | DEBUG(D_acl) | |
1337 | { | |
1338 | if (p) | |
1339 | debug_printf(" Invalid public key service type '%s'\n", p->srvtype); | |
1340 | else | |
1341 | debug_printf(" Error while parsing public key record\n"); | |
1342 | debug_printf( | |
1343 | "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"); | |
1344 | } | |
1345 | return NULL; | |
1346 | } | |
1347 | ||
1348 | DEBUG(D_acl) debug_printf( | |
1349 | "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"); | |
1350 | ||
1351 | /* Import public key */ | |
9b2583c4 | 1352 | if ((*errstr = exim_dkim_verify_init(&p->key, vctx))) |
cd1a5fe0 | 1353 | { |
b9df1829 | 1354 | DEBUG(D_acl) debug_printf("verify_init: %s\n", *errstr); |
cd1a5fe0 JH |
1355 | sig->verify_status = PDKIM_VERIFY_INVALID; |
1356 | sig->verify_ext_status = PDKIM_VERIFY_INVALID_PUBKEY_IMPORT; | |
1357 | return NULL; | |
1358 | } | |
1359 | ||
1360 | return p; | |
1361 | } | |
1362 | ||
1363 | ||
80a47a2c | 1364 | /* -------------------------------------------------------------------------- */ |
3045f050 JH |
1365 | |
1366 | DLLEXPORT int | |
b9df1829 JH |
1367 | pdkim_feed_finish(pdkim_ctx * ctx, pdkim_signature ** return_signatures, |
1368 | const uschar ** err) | |
3045f050 | 1369 | { |
9e70917d | 1370 | pdkim_signature * sig; |
3045f050 JH |
1371 | |
1372 | /* Check if we must still flush a (partial) header. If that is the | |
1373 | case, the message has no body, and we must compute a body hash | |
1374 | out of '<CR><LF>' */ | |
acec9514 | 1375 | if (ctx->cur_header && ctx->cur_header->ptr > 0) |
3045f050 | 1376 | { |
9e70917d JH |
1377 | blob * rnl = NULL; |
1378 | int rc; | |
1379 | ||
1380 | if ((rc = pdkim_header_complete(ctx)) != PDKIM_OK) | |
1381 | return rc; | |
1382 | ||
1383 | for (sig = ctx->sig; sig; sig = sig->next) | |
1384 | rnl = pdkim_update_sig_bodyhash(sig, &lineending, rnl); | |
1385 | if (rnl) store_free(rnl); | |
80a47a2c | 1386 | } |
3045f050 | 1387 | else |
0d04a285 | 1388 | DEBUG(D_acl) debug_printf( |
3045f050 | 1389 | "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"); |
80a47a2c | 1390 | |
3045f050 | 1391 | /* Build (and/or evaluate) body hash */ |
ca9cb170 | 1392 | pdkim_finish_bodyhash(ctx); |
80a47a2c | 1393 | |
9e70917d | 1394 | for (sig = ctx->sig; sig; sig = sig->next) |
3045f050 | 1395 | { |
2592e6c0 | 1396 | hctx hhash_ctx; |
ad6f5499 | 1397 | uschar * sig_hdr = US""; |
2592e6c0 | 1398 | blob hhash; |
acec9514 | 1399 | gstring * hdata = NULL; |
2592e6c0 | 1400 | |
d73e45df | 1401 | if (!exim_sha_init(&hhash_ctx, pdkim_hashes[sig->hashtype].exim_hashmethod)) |
7b83389d | 1402 | { |
d73e45df JH |
1403 | DEBUG(D_acl) |
1404 | debug_printf("PDKIM: hash setup error, possibly nonhandled hashtype\n"); | |
7b83389d JH |
1405 | break; |
1406 | } | |
80a47a2c | 1407 | |
9e70917d JH |
1408 | if (ctx->flags & PDKIM_MODE_SIGN) |
1409 | DEBUG(D_acl) debug_printf( | |
1410 | "PDKIM >> Headers to be signed: >>>>>>>>>>>>\n" | |
1411 | " %s\n", | |
1412 | sig->sign_headers); | |
1413 | ||
0d04a285 | 1414 | DEBUG(D_acl) debug_printf( |
328c5688 | 1415 | "PDKIM >> Header data for hash, canonicalized, in sequence >>>>>>>>>>>>\n"); |
3045f050 | 1416 | |
9e70917d | 1417 | |
3045f050 JH |
1418 | /* SIGNING ---------------------------------------------------------------- */ |
1419 | /* When signing, walk through our header list and add them to the hash. As we | |
8ef02a06 JH |
1420 | go, construct a list of the header's names to use for the h= parameter. |
1421 | Then append to that list any remaining header names for which there was no | |
1422 | header to sign. */ | |
3045f050 | 1423 | |
e983e85a | 1424 | if (ctx->flags & PDKIM_MODE_SIGN) |
3045f050 | 1425 | { |
acec9514 | 1426 | gstring * g = NULL; |
3045f050 | 1427 | pdkim_stringlist *p; |
8ef02a06 JH |
1428 | const uschar * l; |
1429 | uschar * s; | |
1430 | int sep = 0; | |
3045f050 | 1431 | |
9e70917d JH |
1432 | sig->headernames = NULL; /* Collected signed header names */ |
1433 | ||
3045f050 | 1434 | for (p = sig->headers; p; p = p->next) |
9e70917d JH |
1435 | { |
1436 | uschar * rh = p->value; | |
1437 | ||
1438 | if (header_name_match(rh, sig->sign_headers) == PDKIM_OK) | |
ab9152ff | 1439 | { |
ab9152ff | 1440 | /* Collect header names (Note: colon presence is guaranteed here) */ |
acec9514 | 1441 | g = string_append_listele_n(g, ':', rh, Ustrchr(rh, ':') - rh); |
3045f050 | 1442 | |
9e70917d JH |
1443 | if (sig->canon_headers == PDKIM_CANON_RELAXED) |
1444 | rh = pdkim_relax_header(rh, TRUE); /* cook header for relaxed canon */ | |
3045f050 | 1445 | |
ab9152ff | 1446 | /* Feed header to the hash algorithm */ |
e2e3255a | 1447 | exim_sha_update(&hhash_ctx, CUS rh, Ustrlen(rh)); |
f444c2c7 | 1448 | |
ab9152ff | 1449 | /* Remember headers block for signing (when the library cannot do incremental) */ |
acec9514 | 1450 | hdata = exim_dkim_data_append(hdata, rh); |
3045f050 | 1451 | |
ab9152ff JH |
1452 | DEBUG(D_acl) pdkim_quoteprint(rh, Ustrlen(rh)); |
1453 | } | |
9e70917d | 1454 | } |
8ef02a06 | 1455 | |
9e70917d | 1456 | /* Any headers we wanted to sign but were not present must also be listed */ |
ca9cb170 | 1457 | l = sig->sign_headers; |
8ef02a06 JH |
1458 | while((s = string_nextinlist(&l, &sep, NULL, 0))) |
1459 | if (*s != '_') | |
acec9514 JH |
1460 | g = string_append_listele(g, ':', s); |
1461 | sig->headernames = string_from_gstring(g); | |
ca9cb170 JH |
1462 | |
1463 | /* Create signature header with b= omitted */ | |
1464 | sig_hdr = pdkim_create_header(sig, FALSE); | |
80a47a2c | 1465 | } |
37f8b554 | 1466 | |
3045f050 JH |
1467 | /* VERIFICATION ----------------------------------------------------------- */ |
1468 | /* When verifying, walk through the header name list in the h= parameter and | |
1469 | add the headers to the hash in that order. */ | |
1470 | else | |
1471 | { | |
10c50704 | 1472 | uschar * p = sig->headernames; |
2592e6c0 JH |
1473 | uschar * q; |
1474 | pdkim_stringlist * hdrs; | |
3045f050 | 1475 | |
10c50704 | 1476 | if (p) |
3045f050 | 1477 | { |
10c50704 | 1478 | /* clear tags */ |
3045f050 | 1479 | for (hdrs = ctx->headers; hdrs; hdrs = hdrs->next) |
10c50704 | 1480 | hdrs->tag = 0; |
3045f050 | 1481 | |
10c50704 JH |
1482 | p = string_copy(p); |
1483 | while(1) | |
1484 | { | |
1485 | if ((q = Ustrchr(p, ':'))) | |
1486 | *q = '\0'; | |
1487 | ||
1488 | /*XXX walk the list of headers in same order as received. */ | |
1489 | for (hdrs = ctx->headers; hdrs; hdrs = hdrs->next) | |
1490 | if ( hdrs->tag == 0 | |
4dc2379a | 1491 | && strncasecmp(CCS hdrs->value, CCS p, Ustrlen(p)) == 0 |
10c50704 JH |
1492 | && (hdrs->value)[Ustrlen(p)] == ':' |
1493 | ) | |
1494 | { | |
1495 | /* cook header for relaxed canon, or just copy it for simple */ | |
1496 | ||
1497 | uschar * rh = sig->canon_headers == PDKIM_CANON_RELAXED | |
ea18931d | 1498 | ? pdkim_relax_header(hdrs->value, TRUE) |
10c50704 JH |
1499 | : string_copy(CUS hdrs->value); |
1500 | ||
1501 | /* Feed header to the hash algorithm */ | |
1502 | exim_sha_update(&hhash_ctx, CUS rh, Ustrlen(rh)); | |
1503 | ||
1504 | DEBUG(D_acl) pdkim_quoteprint(rh, Ustrlen(rh)); | |
1505 | hdrs->tag = 1; | |
1506 | break; | |
1507 | } | |
3045f050 | 1508 | |
10c50704 JH |
1509 | if (!q) break; |
1510 | p = q+1; | |
1511 | } | |
3045f050 | 1512 | |
10c50704 | 1513 | sig_hdr = string_copy(sig->rawsig_no_b_val); |
80a47a2c | 1514 | } |
80a47a2c TK |
1515 | } |
1516 | ||
0d04a285 | 1517 | DEBUG(D_acl) debug_printf( |
3045f050 | 1518 | "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"); |
80a47a2c | 1519 | |
9e70917d JH |
1520 | DEBUG(D_acl) |
1521 | { | |
1522 | debug_printf( | |
1523 | "PDKIM >> Signed DKIM-Signature header, pre-canonicalized >>>>>>>>>>>>>\n"); | |
1524 | pdkim_quoteprint(CUS sig_hdr, Ustrlen(sig_hdr)); | |
1525 | debug_printf( | |
1526 | "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"); | |
1527 | } | |
1528 | ||
3045f050 JH |
1529 | /* Relax header if necessary */ |
1530 | if (sig->canon_headers == PDKIM_CANON_RELAXED) | |
ea18931d | 1531 | sig_hdr = pdkim_relax_header(sig_hdr, FALSE); |
80a47a2c | 1532 | |
0d04a285 | 1533 | DEBUG(D_acl) |
3045f050 | 1534 | { |
0d04a285 | 1535 | debug_printf( |
3045f050 | 1536 | "PDKIM >> Signed DKIM-Signature header, canonicalized >>>>>>>>>>>>>>>>>\n"); |
e2e3255a | 1537 | pdkim_quoteprint(CUS sig_hdr, Ustrlen(sig_hdr)); |
0d04a285 | 1538 | debug_printf( |
3045f050 | 1539 | "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"); |
80a47a2c | 1540 | } |
3045f050 JH |
1541 | |
1542 | /* Finalize header hash */ | |
e2e3255a | 1543 | exim_sha_update(&hhash_ctx, CUS sig_hdr, Ustrlen(sig_hdr)); |
2592e6c0 | 1544 | exim_sha_finish(&hhash_ctx, &hhash); |
3045f050 | 1545 | |
f444c2c7 JH |
1546 | DEBUG(D_acl) |
1547 | { | |
9e70917d JH |
1548 | debug_printf("PDKIM [%s] Header %s computed: ", |
1549 | sig->domain, pdkim_hashes[sig->hashtype].dkim_hashname); | |
2592e6c0 | 1550 | pdkim_hexprint(hhash.data, hhash.len); |
80a47a2c TK |
1551 | } |
1552 | ||
9e70917d JH |
1553 | /* Remember headers block for signing (when the signing library cannot do |
1554 | incremental) */ | |
e983e85a | 1555 | if (ctx->flags & PDKIM_MODE_SIGN) |
acec9514 | 1556 | hdata = exim_dkim_data_append(hdata, US sig_hdr); |
f444c2c7 | 1557 | |
3045f050 | 1558 | /* SIGNING ---------------------------------------------------------------- */ |
e983e85a | 1559 | if (ctx->flags & PDKIM_MODE_SIGN) |
3045f050 | 1560 | { |
2592e6c0 | 1561 | es_ctx sctx; |
f444c2c7 | 1562 | |
d73e45df | 1563 | /* Import private key, including the keytype */ |
9b2583c4 | 1564 | /*XXX extend for non-RSA algos */ |
d73e45df | 1565 | if ((*err = exim_dkim_signing_init(US sig->privkey, &sctx))) |
f444c2c7 | 1566 | { |
b9df1829 | 1567 | DEBUG(D_acl) debug_printf("signing_init: %s\n", *err); |
f444c2c7 JH |
1568 | return PDKIM_ERR_RSA_PRIVKEY; |
1569 | } | |
80a47a2c | 1570 | |
2592e6c0 JH |
1571 | /* Do signing. With OpenSSL we are signing the hash of headers just |
1572 | calculated, with GnuTLS we have to sign an entire block of headers | |
1573 | (due to available interfaces) and it recalculates the hash internally. */ | |
f444c2c7 | 1574 | |
acec9514 JH |
1575 | #if defined(SIGN_GNUTLS) |
1576 | hhash.data = hdata->s; | |
1577 | hhash.len = hdata->ptr; | |
f444c2c7 JH |
1578 | #endif |
1579 | ||
9b2583c4 | 1580 | /*XXX extend for non-RSA algos */ |
d73e45df JH |
1581 | if ((*err = exim_dkim_sign(&sctx, |
1582 | pdkim_hashes[sig->hashtype].exim_hashmethod, | |
acec9514 | 1583 | &hhash, &sig->sighash))) |
f444c2c7 | 1584 | { |
b9df1829 | 1585 | DEBUG(D_acl) debug_printf("signing: %s\n", *err); |
f444c2c7 JH |
1586 | return PDKIM_ERR_RSA_SIGNING; |
1587 | } | |
80a47a2c | 1588 | |
0d04a285 | 1589 | DEBUG(D_acl) |
3045f050 | 1590 | { |
0d04a285 | 1591 | debug_printf( "PDKIM [%s] b computed: ", sig->domain); |
dcd03763 | 1592 | pdkim_hexprint(sig->sighash.data, sig->sighash.len); |
80a47a2c | 1593 | } |
80a47a2c | 1594 | |
ca9cb170 | 1595 | sig->signature_header = pdkim_create_header(sig, TRUE); |
80a47a2c | 1596 | } |
80a47a2c | 1597 | |
3045f050 JH |
1598 | /* VERIFICATION ----------------------------------------------------------- */ |
1599 | else | |
1600 | { | |
2592e6c0 | 1601 | ev_ctx vctx; |
3045f050 | 1602 | |
07eeb4df | 1603 | /* Make sure we have all required signature tags */ |
1604 | if (!( sig->domain && *sig->domain | |
1605 | && sig->selector && *sig->selector | |
1606 | && sig->headernames && *sig->headernames | |
1607 | && sig->bodyhash.data | |
dcd03763 | 1608 | && sig->sighash.data |
d73e45df JH |
1609 | && sig->keytype >= 0 |
1610 | && sig->hashtype >= 0 | |
07eeb4df | 1611 | && sig->version |
1612 | ) ) | |
1613 | { | |
1614 | sig->verify_status = PDKIM_VERIFY_INVALID; | |
1615 | sig->verify_ext_status = PDKIM_VERIFY_INVALID_SIGNATURE_ERROR; | |
1616 | ||
1617 | DEBUG(D_acl) debug_printf( | |
1618 | " Error in DKIM-Signature header: tags missing or invalid\n" | |
1619 | "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"); | |
1620 | goto NEXT_VERIFY; | |
1621 | } | |
1622 | ||
1623 | /* Make sure sig uses supported DKIM version (only v1) */ | |
1624 | if (sig->version != 1) | |
1625 | { | |
1626 | sig->verify_status = PDKIM_VERIFY_INVALID; | |
1627 | sig->verify_ext_status = PDKIM_VERIFY_INVALID_DKIM_VERSION; | |
1628 | ||
1629 | DEBUG(D_acl) debug_printf( | |
1630 | " Error in DKIM-Signature header: unsupported DKIM version\n" | |
1631 | "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"); | |
1632 | goto NEXT_VERIFY; | |
1633 | } | |
1634 | ||
9e70917d JH |
1635 | DEBUG(D_acl) |
1636 | { | |
1637 | debug_printf( "PDKIM [%s] b from mail: ", sig->domain); | |
1638 | pdkim_hexprint(sig->sighash.data, sig->sighash.len); | |
1639 | } | |
1640 | ||
b9df1829 | 1641 | if (!(sig->pubkey = pdkim_key_from_dns(ctx, sig, &vctx, err))) |
3045f050 | 1642 | goto NEXT_VERIFY; |
80a47a2c | 1643 | |
135e9496 JH |
1644 | /* If the pubkey limits to a list of specific hashes, ignore sigs that |
1645 | do not have the hash part of the sig algorithm matching */ | |
1646 | ||
1647 | if (sig->pubkey->hashes) | |
1648 | { | |
1649 | const uschar * list = sig->pubkey->hashes, * ele; | |
1650 | int sep = ':'; | |
1651 | while ((ele = string_nextinlist(&list, &sep, NULL, 0))) | |
d73e45df | 1652 | if (Ustrcmp(ele, pdkim_hashes[sig->hashtype].dkim_hashname) == 0) break; |
135e9496 JH |
1653 | if (!ele) |
1654 | { | |
d73e45df JH |
1655 | DEBUG(D_acl) debug_printf("pubkey h=%s vs. sig a=%s_%s\n", |
1656 | sig->pubkey->hashes, | |
1657 | pdkim_keytypes[sig->keytype], | |
1658 | pdkim_hashes[sig->hashtype].dkim_hashname); | |
135e9496 JH |
1659 | sig->verify_status = PDKIM_VERIFY_FAIL; |
1660 | sig->verify_ext_status = PDKIM_VERIFY_FAIL_SIG_ALGO_MISMATCH; | |
1661 | goto NEXT_VERIFY; | |
1662 | } | |
1663 | } | |
1664 | ||
3045f050 | 1665 | /* Check the signature */ |
9b2583c4 | 1666 | /*XXX needs extension for non-RSA */ |
d73e45df JH |
1667 | if ((*err = exim_dkim_verify(&vctx, |
1668 | pdkim_hashes[sig->hashtype].exim_hashmethod, | |
1669 | &hhash, &sig->sighash))) | |
3045f050 | 1670 | { |
b9df1829 | 1671 | DEBUG(D_acl) debug_printf("headers verify: %s\n", *err); |
3045f050 JH |
1672 | sig->verify_status = PDKIM_VERIFY_FAIL; |
1673 | sig->verify_ext_status = PDKIM_VERIFY_FAIL_MESSAGE; | |
1674 | goto NEXT_VERIFY; | |
ff7ddfd7 TK |
1675 | } |
1676 | ||
2592e6c0 | 1677 | |
4c04137d | 1678 | /* We have a winner! (if bodyhash was correct earlier) */ |
3045f050 JH |
1679 | if (sig->verify_status == PDKIM_VERIFY_NONE) |
1680 | sig->verify_status = PDKIM_VERIFY_PASS; | |
1681 | ||
1682 | NEXT_VERIFY: | |
1683 | ||
0d04a285 | 1684 | DEBUG(D_acl) |
3045f050 | 1685 | { |
0d04a285 | 1686 | debug_printf("PDKIM [%s] signature status: %s", |
3045f050 JH |
1687 | sig->domain, pdkim_verify_status_str(sig->verify_status)); |
1688 | if (sig->verify_ext_status > 0) | |
0d04a285 | 1689 | debug_printf(" (%s)\n", |
3045f050 JH |
1690 | pdkim_verify_ext_status_str(sig->verify_ext_status)); |
1691 | else | |
0d04a285 | 1692 | debug_printf("\n"); |
80a47a2c | 1693 | } |
80a47a2c | 1694 | } |
80a47a2c TK |
1695 | } |
1696 | ||
3045f050 JH |
1697 | /* If requested, set return pointer to signature(s) */ |
1698 | if (return_signatures) | |
1699 | *return_signatures = ctx->sig; | |
80a47a2c | 1700 | |
3045f050 | 1701 | return PDKIM_OK; |
80a47a2c TK |
1702 | } |
1703 | ||
1704 | ||
1705 | /* -------------------------------------------------------------------------- */ | |
3045f050 JH |
1706 | |
1707 | DLLEXPORT pdkim_ctx * | |
e983e85a | 1708 | pdkim_init_verify(int(*dns_txt_callback)(char *, char *), BOOL dot_stuffing) |
3045f050 | 1709 | { |
ca9cb170 | 1710 | pdkim_ctx * ctx; |
3045f050 | 1711 | |
ca9cb170 | 1712 | ctx = store_get(sizeof(pdkim_ctx)); |
abe1010c | 1713 | memset(ctx, 0, sizeof(pdkim_ctx)); |
3045f050 | 1714 | |
e983e85a | 1715 | if (dot_stuffing) ctx->flags = PDKIM_DOT_TERM; |
ca9cb170 | 1716 | ctx->linebuf = store_get(PDKIM_MAX_BODY_LINE_LEN); |
3045f050 | 1717 | ctx->dns_txt_callback = dns_txt_callback; |
80a47a2c | 1718 | |
3045f050 | 1719 | return ctx; |
80a47a2c TK |
1720 | } |
1721 | ||
1722 | ||
1723 | /* -------------------------------------------------------------------------- */ | |
80a47a2c | 1724 | |
d73e45df | 1725 | /*XXX ? needs extension to cover non-RSA algo? */ |
9b2583c4 | 1726 | |
9e70917d JH |
1727 | DLLEXPORT pdkim_signature * |
1728 | pdkim_init_sign(pdkim_ctx * ctx, | |
1729 | uschar * domain, uschar * selector, uschar * privkey, | |
1730 | uschar * hashname, const uschar ** errstr) | |
3045f050 | 1731 | { |
d73e45df | 1732 | int hashtype; |
cd1a5fe0 | 1733 | pdkim_signature * sig; |
80a47a2c | 1734 | |
d73e45df | 1735 | if (!domain || !selector || !privkey) |
3045f050 | 1736 | return NULL; |
80a47a2c | 1737 | |
9e70917d | 1738 | /* Allocate & init one signature struct */ |
80a47a2c | 1739 | |
9e70917d | 1740 | sig = store_get(sizeof(pdkim_signature)); |
abe1010c | 1741 | memset(sig, 0, sizeof(pdkim_signature)); |
80a47a2c | 1742 | |
3045f050 | 1743 | sig->bodylength = -1; |
80a47a2c | 1744 | |
e2e3255a JH |
1745 | sig->domain = string_copy(US domain); |
1746 | sig->selector = string_copy(US selector); | |
d73e45df JH |
1747 | sig->privkey = string_copy(US privkey); |
1748 | /*XXX no keytype yet; comes from privkey */ | |
cb224393 | 1749 | |
d73e45df JH |
1750 | for (hashtype = 0; hashtype < nelem(pdkim_hashes); hashtype++) |
1751 | if (Ustrcmp(hashname, pdkim_hashes[hashtype].dkim_hashname) == 0) | |
1752 | { sig->hashtype = hashtype; break; } | |
1753 | if (hashtype >= nelem(pdkim_hashes)) | |
7b83389d | 1754 | { |
d73e45df JH |
1755 | DEBUG(D_acl) |
1756 | debug_printf("PDKIM: unrecognised hashname '%s'\n", hashname); | |
1757 | return NULL; | |
1758 | } | |
1759 | ||
1760 | if (!exim_sha_init(&sig->body_hash_ctx, pdkim_hashes[hashtype].exim_hashmethod)) | |
1761 | { | |
1762 | DEBUG(D_acl) | |
1763 | debug_printf("PDKIM: hash setup error, possibly nonhandled hashtype\n"); | |
7b83389d JH |
1764 | return NULL; |
1765 | } | |
1766 | ||
cd1a5fe0 JH |
1767 | DEBUG(D_acl) |
1768 | { | |
1769 | pdkim_signature s = *sig; | |
1770 | ev_ctx vctx; | |
1771 | ||
328c5688 | 1772 | debug_printf("PDKIM (checking verify key)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"); |
b9df1829 | 1773 | if (!pdkim_key_from_dns(ctx, &s, &vctx, errstr)) |
cd1a5fe0 | 1774 | debug_printf("WARNING: bad dkim key in dns\n"); |
328c5688 | 1775 | debug_printf("PDKIM (finished checking verify key)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"); |
cd1a5fe0 | 1776 | } |
9e70917d | 1777 | return sig; |
6ab02e3f | 1778 | } |
80a47a2c | 1779 | |
f444c2c7 | 1780 | |
80a47a2c | 1781 | /* -------------------------------------------------------------------------- */ |
3045f050 | 1782 | |
9e70917d JH |
1783 | DLLEXPORT void |
1784 | pdkim_set_optional(pdkim_signature * sig, | |
1785 | char * sign_headers, | |
1786 | char * identity, | |
80a47a2c TK |
1787 | int canon_headers, |
1788 | int canon_body, | |
1789 | long bodylength, | |
80a47a2c | 1790 | unsigned long created, |
3045f050 JH |
1791 | unsigned long expires) |
1792 | { | |
3045f050 | 1793 | if (identity) |
e2e3255a | 1794 | sig->identity = string_copy(US identity); |
80a47a2c | 1795 | |
ca9cb170 JH |
1796 | sig->sign_headers = string_copy(sign_headers |
1797 | ? US sign_headers : US PDKIM_DEFAULT_SIGN_HEADERS); | |
80a47a2c | 1798 | |
8ef02a06 JH |
1799 | sig->canon_headers = canon_headers; |
1800 | sig->canon_body = canon_body; | |
1801 | sig->bodylength = bodylength; | |
1802 | sig->created = created; | |
1803 | sig->expires = expires; | |
80a47a2c | 1804 | |
9e70917d JH |
1805 | return; |
1806 | } | |
1807 | ||
1808 | ||
1809 | ||
1810 | void | |
1811 | pdkim_init_context(pdkim_ctx * ctx, BOOL dot_stuffed, | |
1812 | int(*dns_txt_callback)(char *, char *)) | |
1813 | { | |
1814 | memset(ctx, 0, sizeof(pdkim_ctx)); | |
1815 | ctx->flags = dot_stuffed ? PDKIM_MODE_SIGN | PDKIM_DOT_TERM : PDKIM_MODE_SIGN; | |
1816 | ctx->linebuf = store_get(PDKIM_MAX_BODY_LINE_LEN); | |
1817 | DEBUG(D_acl) ctx->dns_txt_callback = dns_txt_callback; | |
6ab02e3f | 1818 | } |
3045f050 | 1819 | |
3045f050 | 1820 | |
2592e6c0 JH |
1821 | void |
1822 | pdkim_init(void) | |
1823 | { | |
9b2583c4 | 1824 | exim_dkim_init(); |
2592e6c0 JH |
1825 | } |
1826 | ||
1827 | ||
1828 | ||
f444c2c7 | 1829 | #endif /*DISABLE_DKIM*/ |