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