fix no-ssl build
[exim.git] / src / src / pdkim / pdkim.c
CommitLineData
80a47a2c
TK
1/*
2 * PDKIM - a RFC4871 (DKIM) implementation
3 *
3386088d 4 * Copyright (C) 2009 - 2015 Tom Kistner <tom@duncanthrax.net>
80a47a2c
TK
5 *
6 * http://duncanthrax.net/pdkim/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
0d04a285 23#include "../exim.h"
80a47a2c 24#include "pdkim.h"
ac3ad426 25#include "pdkim-rsa.h"
80a47a2c 26
ac3ad426
AM
27#include "polarssl/sha1.h"
28#include "polarssl/sha2.h"
29#include "polarssl/rsa.h"
80a47a2c
TK
30
31#define PDKIM_SIGNATURE_VERSION "1"
32#define PDKIM_PUB_RECORD_VERSION "DKIM1"
33
34#define PDKIM_MAX_HEADER_LEN 65536
35#define PDKIM_MAX_HEADERS 512
36#define PDKIM_MAX_BODY_LINE_LEN 16384
37#define PDKIM_DNS_TXT_MAX_NAMELEN 1024
38#define PDKIM_DEFAULT_SIGN_HEADERS "From:Sender:Reply-To:Subject:Date:"\
39 "Message-ID:To:Cc:MIME-Version:Content-Type:"\
40 "Content-Transfer-Encoding:Content-ID:"\
41 "Content-Description:Resent-Date:Resent-From:"\
42 "Resent-Sender:Resent-To:Resent-Cc:"\
43 "Resent-Message-ID:In-Reply-To:References:"\
44 "List-Id:List-Help:List-Unsubscribe:"\
45 "List-Subscribe:List-Post:List-Owner:List-Archive"
46
47/* -------------------------------------------------------------------------- */
48struct pdkim_stringlist {
49 char *value;
37f8b554 50 int tag;
80a47a2c
TK
51 void *next;
52};
53
54#define PDKIM_STR_ALLOC_FRAG 256
55struct pdkim_str {
56 char *str;
57 unsigned int len;
58 unsigned int allocated;
59};
60
61/* -------------------------------------------------------------------------- */
62/* A bunch of list constants */
1ba28e2b 63const char *pdkim_querymethods[] = {
80a47a2c
TK
64 "dns/txt",
65 NULL
66};
1ba28e2b 67const char *pdkim_algos[] = {
80a47a2c
TK
68 "rsa-sha256",
69 "rsa-sha1",
70 NULL
71};
1ba28e2b 72const char *pdkim_canons[] = {
80a47a2c
TK
73 "simple",
74 "relaxed",
75 NULL
76};
1ba28e2b 77const char *pdkim_hashes[] = {
80a47a2c
TK
78 "sha256",
79 "sha1",
80 NULL
81};
1ba28e2b 82const char *pdkim_keytypes[] = {
80a47a2c
TK
83 "rsa",
84 NULL
85};
86
87typedef struct pdkim_combined_canon_entry {
1ba28e2b 88 const char *str;
80a47a2c
TK
89 int canon_headers;
90 int canon_body;
91} pdkim_combined_canon_entry;
92pdkim_combined_canon_entry pdkim_combined_canons[] = {
93 { "simple/simple", PDKIM_CANON_SIMPLE, PDKIM_CANON_SIMPLE },
94 { "simple/relaxed", PDKIM_CANON_SIMPLE, PDKIM_CANON_RELAXED },
95 { "relaxed/simple", PDKIM_CANON_RELAXED, PDKIM_CANON_SIMPLE },
96 { "relaxed/relaxed", PDKIM_CANON_RELAXED, PDKIM_CANON_RELAXED },
97 { "simple", PDKIM_CANON_SIMPLE, PDKIM_CANON_SIMPLE },
98 { "relaxed", PDKIM_CANON_RELAXED, PDKIM_CANON_SIMPLE },
99 { NULL, 0, 0 }
100};
101
102
1ba28e2b 103const char *pdkim_verify_status_str(int status) {
ff7ddfd7
TK
104 switch(status) {
105 case PDKIM_VERIFY_NONE: return "PDKIM_VERIFY_NONE";
106 case PDKIM_VERIFY_INVALID: return "PDKIM_VERIFY_INVALID";
107 case PDKIM_VERIFY_FAIL: return "PDKIM_VERIFY_FAIL";
108 case PDKIM_VERIFY_PASS: return "PDKIM_VERIFY_PASS";
109 default: return "PDKIM_VERIFY_UNKNOWN";
110 }
111}
1ba28e2b 112const char *pdkim_verify_ext_status_str(int ext_status) {
ff7ddfd7
TK
113 switch(ext_status) {
114 case PDKIM_VERIFY_FAIL_BODY: return "PDKIM_VERIFY_FAIL_BODY";
115 case PDKIM_VERIFY_FAIL_MESSAGE: return "PDKIM_VERIFY_FAIL_MESSAGE";
116 case PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE: return "PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE";
117 case PDKIM_VERIFY_INVALID_BUFFER_SIZE: return "PDKIM_VERIFY_INVALID_BUFFER_SIZE";
118 case PDKIM_VERIFY_INVALID_PUBKEY_PARSING: return "PDKIM_VERIFY_INVALID_PUBKEY_PARSING";
119 default: return "PDKIM_VERIFY_UNKNOWN";
120 }
121}
122
123
80a47a2c
TK
124/* -------------------------------------------------------------------------- */
125/* Print debugging functions */
3045f050 126void
0d04a285 127pdkim_quoteprint(const char *data, int len, int lf)
3045f050
JH
128{
129int i;
130const unsigned char *p = (const unsigned char *)data;
131
0d04a285 132for (i = 0; i < len; i++)
3045f050
JH
133 {
134 const int c = p[i];
135 switch (c)
136 {
0d04a285
JH
137 case ' ' : debug_printf("{SP}"); break;
138 case '\t': debug_printf("{TB}"); break;
139 case '\r': debug_printf("{CR}"); break;
140 case '\n': debug_printf("{LF}"); break;
141 case '{' : debug_printf("{BO}"); break;
142 case '}' : debug_printf("{BC}"); break;
3045f050
JH
143 default:
144 if ( (c < 32) || (c > 127) )
0d04a285 145 debug_printf("{%02x}", c);
3045f050 146 else
0d04a285 147 debug_printf("%c", c);
80a47a2c
TK
148 break;
149 }
150 }
3045f050 151if (lf)
0d04a285 152 debug_printf("\n");
80a47a2c 153}
80a47a2c 154
3045f050 155void
0d04a285 156pdkim_hexprint(const char *data, int len, int lf)
3045f050
JH
157{
158int i;
159const unsigned char *p = (const unsigned char *)data;
160
0d04a285
JH
161for (i = 0 ; i < len; i++)
162 debug_printf("%02x", p[i]);
3045f050 163if (lf)
0d04a285 164 debug_printf("\n");
80a47a2c 165}
80a47a2c
TK
166
167
168/* -------------------------------------------------------------------------- */
169/* Simple string list implementation for convinience */
3045f050
JH
170pdkim_stringlist *
171pdkim_append_stringlist(pdkim_stringlist *base, char *str)
172{
173pdkim_stringlist *new_entry = malloc(sizeof(pdkim_stringlist));
174
175if (!new_entry) return NULL;
abe1010c 176memset(new_entry, 0, sizeof(pdkim_stringlist));
3045f050
JH
177if (!(new_entry->value = strdup(str))) return NULL;
178if (base)
179 {
180 pdkim_stringlist *last = base;
181 while (last->next != NULL) { last = last->next; }
182 last->next = new_entry;
183 return base;
97713345 184 }
3045f050 185else
97713345 186 return new_entry;
6ab02e3f 187}
80a47a2c 188
3045f050
JH
189pdkim_stringlist *
190pdkim_prepend_stringlist(pdkim_stringlist *base, char *str)
191{
192pdkim_stringlist *new_entry = malloc(sizeof(pdkim_stringlist));
193
194if (!new_entry) return NULL;
abe1010c 195memset(new_entry, 0, sizeof(pdkim_stringlist));
3045f050
JH
196if (!(new_entry->value = strdup(str))) return NULL;
197if (base)
198 new_entry->next = base;
199return new_entry;
200}
201
80a47a2c
TK
202
203/* -------------------------------------------------------------------------- */
204/* A small "growing string" implementation to escape malloc/realloc hell */
3045f050
JH
205
206pdkim_str *
207pdkim_strnew (const char *cstr)
208{
209unsigned int len = cstr ? strlen(cstr) : 0;
210pdkim_str *p = malloc(sizeof(pdkim_str));
211
212if (!p) return NULL;
abe1010c 213memset(p, 0, sizeof(pdkim_str));
3045f050
JH
214if (!(p->str = malloc(len+1)))
215 {
216 free(p);
217 return NULL;
80a47a2c 218 }
3045f050
JH
219p->allocated = len+1;
220p->len = len;
221if (cstr)
abe1010c 222 strcpy(p->str, cstr);
3045f050
JH
223else
224 p->str[p->len] = '\0';
225return p;
6ab02e3f 226}
3045f050
JH
227
228char *
229pdkim_strncat(pdkim_str *str, const char *data, int len)
230{
231if ((str->allocated - str->len) < (len+1))
232 {
233 /* Extend the buffer */
234 int num_frags = ((len+1)/PDKIM_STR_ALLOC_FRAG)+1;
235 char *n = realloc(str->str,
236 (str->allocated+(num_frags*PDKIM_STR_ALLOC_FRAG)));
237 if (n == NULL) return NULL;
238 str->str = n;
239 str->allocated += (num_frags*PDKIM_STR_ALLOC_FRAG);
80a47a2c 240 }
3045f050
JH
241strncpy(&(str->str[str->len]), data, len);
242str->len += len;
243str->str[str->len] = '\0';
244return str->str;
6ab02e3f 245}
3045f050
JH
246
247char *
248pdkim_strcat(pdkim_str *str, const char *cstr)
249{
250return pdkim_strncat(str, cstr, strlen(cstr));
6ab02e3f 251}
05b7d6de 252
3045f050
JH
253char *
254pdkim_numcat(pdkim_str *str, unsigned long num)
255{
256char minibuf[20];
abe1010c
JH
257snprintf(minibuf, 20, "%lu", num);
258return pdkim_strcat(str, minibuf);
6ab02e3f 259}
3045f050
JH
260
261char *
262pdkim_strtrim(pdkim_str *str)
263{
264char *p = str->str;
265char *q = str->str;
266while ( (*p != '\0') && ((*p == '\t') || (*p == ' ')) ) p++;
267while (*p != '\0') {*q = *p; q++; p++;}
268*q = '\0';
269while ( (q != str->str) && ( (*q == '\0') || (*q == '\t') || (*q == ' ') ) )
270 {
80a47a2c 271 *q = '\0';
3045f050 272 q--;
80a47a2c 273 }
3045f050
JH
274str->len = strlen(str->str);
275return str->str;
6ab02e3f 276}
3045f050
JH
277
278char *
279pdkim_strclear(pdkim_str *str)
280{
281str->str[0] = '\0';
282str->len = 0;
283return str->str;
6ab02e3f 284}
3045f050
JH
285
286void
287pdkim_strfree(pdkim_str *str)
288{
289if (!str) return;
290if (str->str) free(str->str);
291free(str);
6ab02e3f 292}
80a47a2c
TK
293
294
295
296/* -------------------------------------------------------------------------- */
3045f050
JH
297
298void
299pdkim_free_pubkey(pdkim_pubkey *pub)
300{
301if (pub)
302 {
303 if (pub->version ) free(pub->version);
304 if (pub->granularity) free(pub->granularity);
305 if (pub->hashes ) free(pub->hashes);
306 if (pub->keytype ) free(pub->keytype);
307 if (pub->srvtype ) free(pub->srvtype);
308 if (pub->notes ) free(pub->notes);
f4d091fb 309/* if (pub->key ) free(pub->key); */
3045f050 310 free(pub);
80a47a2c
TK
311 }
312}
313
314
315/* -------------------------------------------------------------------------- */
3045f050
JH
316
317void
318pdkim_free_sig(pdkim_signature *sig)
319{
320if (sig)
321 {
322 pdkim_signature *next = (pdkim_signature *)sig->next;
323
324 pdkim_stringlist *e = sig->headers;
325 while(e)
326 {
327 pdkim_stringlist *c = e;
328 if (e->value) free(e->value);
329 e = e->next;
330 free(c);
80a47a2c
TK
331 }
332
f4d091fb
JH
333/* if (sig->sigdata ) free(sig->sigdata); */
334/* if (sig->bodyhash ) free(sig->bodyhash); */
3045f050
JH
335 if (sig->selector ) free(sig->selector);
336 if (sig->domain ) free(sig->domain);
337 if (sig->identity ) free(sig->identity);
338 if (sig->headernames ) free(sig->headernames);
339 if (sig->copiedheaders ) free(sig->copiedheaders);
340 if (sig->rsa_privkey ) free(sig->rsa_privkey);
341 if (sig->sign_headers ) free(sig->sign_headers);
342 if (sig->signature_header) free(sig->signature_header);
343 if (sig->sha1_body ) free(sig->sha1_body);
344 if (sig->sha2_body ) free(sig->sha2_body);
345
346 if (sig->pubkey) pdkim_free_pubkey(sig->pubkey);
347
348 free(sig);
349 if (next) pdkim_free_sig(next);
80a47a2c 350 }
6ab02e3f 351}
80a47a2c
TK
352
353
354/* -------------------------------------------------------------------------- */
3045f050
JH
355
356DLLEXPORT void
357pdkim_free_ctx(pdkim_ctx *ctx)
358{
359if (ctx)
360 {
361 pdkim_stringlist *e = ctx->headers;
362 while(e)
363 {
364 pdkim_stringlist *c = e;
365 if (e->value) free(e->value);
366 e = e->next;
367 free(c);
37f8b554 368 }
3045f050
JH
369 pdkim_free_sig(ctx->sig);
370 pdkim_strfree(ctx->cur_header);
371 free(ctx);
80a47a2c 372 }
6ab02e3f 373}
80a47a2c
TK
374
375
376/* -------------------------------------------------------------------------- */
377/* Matches the name of the passed raw "header" against
378 the passed colon-separated "list", starting at entry
379 "start". Returns the position of the header name in
380 the list. */
3045f050
JH
381
382int
383header_name_match(const char *header,
1ba28e2b 384 char *tick,
3045f050
JH
385 int do_tick)
386{
387char *hname;
388char *lcopy;
389char *p;
390char *q;
391int rc = PDKIM_FAIL;
392
393/* Get header name */
abe1010c 394char *hcolon = strchr(header, ':');
3045f050
JH
395
396if (!hcolon) return rc; /* This isn't a header */
397
398if (!(hname = malloc((hcolon-header)+1)))
399 return PDKIM_ERR_OOM;
abe1010c
JH
400memset(hname, 0, (hcolon-header)+1);
401strncpy(hname, header, (hcolon-header));
3045f050
JH
402
403/* Copy tick-off list locally, so we can punch zeroes into it */
404if (!(lcopy = strdup(tick)))
405 {
406 free(hname);
407 return PDKIM_ERR_OOM;
80a47a2c 408 }
3045f050 409p = lcopy;
abe1010c 410q = strchr(p, ':');
3045f050
JH
411while (q)
412 {
413 *q = '\0';
80a47a2c 414
abe1010c 415 if (strcasecmp(p, hname) == 0)
3045f050
JH
416 {
417 rc = PDKIM_OK;
418 /* Invalidate header name instance in tick-off list */
419 if (do_tick) tick[p-lcopy] = '_';
420 goto BAIL;
80a47a2c
TK
421 }
422
3045f050 423 p = q+1;
abe1010c 424 q = strchr(p, ':');
80a47a2c
TK
425 }
426
abe1010c 427if (strcasecmp(p, hname) == 0)
3045f050
JH
428 {
429 rc = PDKIM_OK;
430 /* Invalidate header name instance in tick-off list */
431 if (do_tick) tick[p-lcopy] = '_';
80a47a2c
TK
432 }
433
3045f050
JH
434BAIL:
435free(hname);
436free(lcopy);
437return rc;
80a47a2c
TK
438}
439
440
441/* -------------------------------------------------------------------------- */
442/* Performs "relaxed" canonicalization of a header. The returned pointer needs
443 to be free()d. */
3045f050
JH
444
445char *
446pdkim_relax_header (char *header, int crlf)
447{
448BOOL past_field_name = FALSE;
449BOOL seen_wsp = FALSE;
450char *p;
451char *q;
452char *relaxed = malloc(strlen(header)+3);
453
454if (!relaxed) return NULL;
455
456q = relaxed;
457for (p = header; *p != '\0'; p++)
458 {
459 int c = *p;
460 /* Ignore CR & LF */
461 if (c == '\r' || c == '\n')
462 continue;
463 if (c == '\t' || c == ' ')
464 {
465 if (seen_wsp)
80a47a2c 466 continue;
3045f050
JH
467 c = ' '; /* Turns WSP into SP */
468 seen_wsp = TRUE;
80a47a2c 469 }
3045f050
JH
470 else
471 if (!past_field_name && c == ':')
472 {
473 if (seen_wsp) q--; /* This removes WSP before the colon */
474 seen_wsp = TRUE; /* This removes WSP after the colon */
475 past_field_name = TRUE;
80a47a2c 476 }
3045f050
JH
477 else
478 seen_wsp = FALSE;
479
480 /* Lowercase header name */
481 if (!past_field_name) c = tolower(c);
482 *q++ = c;
80a47a2c 483 }
3045f050
JH
484
485if (q > relaxed && q[-1] == ' ') q--; /* Squash eventual trailing SP */
486*q = '\0';
487
abe1010c 488if (crlf) strcat(relaxed, "\r\n");
3045f050 489return relaxed;
6ab02e3f 490}
80a47a2c
TK
491
492
493/* -------------------------------------------------------------------------- */
494#define PDKIM_QP_ERROR_DECODE -1
3045f050 495
a5840e10 496static char *
3045f050
JH
497pdkim_decode_qp_char(char *qp_p, int *c)
498{
499char *initial_pos = qp_p;
500
501/* Advance one char */
502qp_p++;
503
504/* Check for two hex digits and decode them */
505if (isxdigit(*qp_p) && isxdigit(qp_p[1]))
506 {
507 /* Do hex conversion */
508 *c = (isdigit(*qp_p) ? *qp_p - '0' : toupper(*qp_p) - 'A' + 10) << 4;
a5840e10 509 *c |= isdigit(qp_p[1]) ? qp_p[1] - '0' : toupper(qp_p[1]) - 'A' + 10;
3045f050 510 return qp_p + 2;
6ab02e3f 511 }
80a47a2c 512
3045f050
JH
513/* Illegal char here */
514*c = PDKIM_QP_ERROR_DECODE;
515return initial_pos;
80a47a2c
TK
516}
517
518
519/* -------------------------------------------------------------------------- */
3045f050
JH
520
521char *
522pdkim_decode_qp(char *str)
523{
524int nchar = 0;
525char *q;
526char *p = str;
527char *n = malloc(strlen(p)+1);
528
529if (!n) return NULL;
530
531*n = '\0';
532q = n;
533while (*p != '\0')
534 {
535 if (*p == '=')
536 {
537 p = pdkim_decode_qp_char(p, &nchar);
538 if (nchar >= 0)
539 {
540 *q++ = nchar;
541 continue;
80a47a2c
TK
542 }
543 }
3045f050
JH
544 else
545 *q++ = *p;
546 p++;
80a47a2c 547 }
3045f050
JH
548*q = '\0';
549return n;
80a47a2c
TK
550}
551
552
553/* -------------------------------------------------------------------------- */
3045f050
JH
554
555char *
556pdkim_decode_base64(char *str, int *num_decoded)
557{
558int dlen = 0;
559char *res;
f4d091fb 560int old_pool = store_pool;
3045f050 561
f4d091fb
JH
562/* There is a store-reset between header & body reception
563so cannot use the main pool */
3045f050 564
f4d091fb
JH
565store_pool = POOL_PERM;
566dlen = b64decode(US str, USS &res);
567store_pool = old_pool;
568
569if (dlen < 0) return NULL;
3045f050
JH
570
571if (num_decoded) *num_decoded = dlen;
572return res;
80a47a2c
TK
573}
574
3045f050 575
80a47a2c 576/* -------------------------------------------------------------------------- */
3045f050
JH
577
578char *
579pdkim_encode_base64(char *str, int num)
580{
f4d091fb
JH
581char * ret;
582int old_pool = store_pool;
3045f050 583
f4d091fb
JH
584store_pool = POOL_PERM;
585ret = CS b64encode(US str, num);
586store_pool = old_pool;
587return ret;
80a47a2c
TK
588}
589
590
591/* -------------------------------------------------------------------------- */
592#define PDKIM_HDR_LIMBO 0
593#define PDKIM_HDR_TAG 1
594#define PDKIM_HDR_VALUE 2
3045f050
JH
595
596pdkim_signature *
597pdkim_parse_sig_header(pdkim_ctx *ctx, char *raw_hdr)
598{
599pdkim_signature *sig ;
abe1010c 600char *p, *q;
3045f050
JH
601pdkim_str *cur_tag = NULL;
602pdkim_str *cur_val = NULL;
603BOOL past_hname = FALSE;
604BOOL in_b_val = FALSE;
605int where = PDKIM_HDR_LIMBO;
606int i;
607
608if (!(sig = malloc(sizeof(pdkim_signature)))) return NULL;
abe1010c 609memset(sig, 0, sizeof(pdkim_signature));
3045f050
JH
610sig->bodylength = -1;
611
612if (!(sig->rawsig_no_b_val = malloc(strlen(raw_hdr)+1)))
613 {
614 free(sig);
615 return NULL;
80a47a2c
TK
616 }
617
3045f050 618q = sig->rawsig_no_b_val;
80a47a2c 619
3045f050
JH
620for (p = raw_hdr; ; p++)
621 {
622 char c = *p;
80a47a2c 623
3045f050
JH
624 /* Ignore FWS */
625 if (c == '\r' || c == '\n')
626 goto NEXT_CHAR;
80a47a2c 627
3045f050
JH
628 /* Fast-forward through header name */
629 if (!past_hname)
630 {
631 if (c == ':') past_hname = TRUE;
632 goto NEXT_CHAR;
80a47a2c
TK
633 }
634
3045f050
JH
635 if (where == PDKIM_HDR_LIMBO)
636 {
637 /* In limbo, just wait for a tag-char to appear */
638 if (!(c >= 'a' && c <= 'z'))
639 goto NEXT_CHAR;
80a47a2c 640
3045f050 641 where = PDKIM_HDR_TAG;
80a47a2c
TK
642 }
643
3045f050
JH
644 if (where == PDKIM_HDR_TAG)
645 {
646 if (!cur_tag)
647 cur_tag = pdkim_strnew(NULL);
80a47a2c 648
3045f050
JH
649 if (c >= 'a' && c <= 'z')
650 pdkim_strncat(cur_tag, p, 1);
80a47a2c 651
3045f050
JH
652 if (c == '=')
653 {
654 if (strcmp(cur_tag->str, "b") == 0)
655 {
656 *q = '='; q++;
657 in_b_val = TRUE;
658 }
659 where = PDKIM_HDR_VALUE;
660 goto NEXT_CHAR;
80a47a2c
TK
661 }
662 }
663
3045f050
JH
664 if (where == PDKIM_HDR_VALUE)
665 {
666 if (!cur_val)
667 cur_val = pdkim_strnew(NULL);
668
669 if (c == '\r' || c == '\n' || c == ' ' || c == '\t')
670 goto NEXT_CHAR;
671
672 if (c == ';' || c == '\0')
673 {
674 if (cur_tag->len > 0)
675 {
676 pdkim_strtrim(cur_val);
677
0d04a285 678 DEBUG(D_acl) debug_printf(" %s=%s\n", cur_tag->str, cur_val->str);
3045f050
JH
679
680 switch (cur_tag->str[0])
681 {
682 case 'b':
683 if (cur_tag->str[1] == 'h')
684 sig->bodyhash = pdkim_decode_base64(cur_val->str,
685 &sig->bodyhash_len);
686 else
687 sig->sigdata = pdkim_decode_base64(cur_val->str,
688 &sig->sigdata_len);
689 break;
690 case 'v':
691 /* We only support version 1, and that is currently the
692 only version there is. */
693 if (strcmp(cur_val->str, PDKIM_SIGNATURE_VERSION) == 0)
694 sig->version = 1;
695 break;
696 case 'a':
697 for (i = 0; pdkim_algos[i]; i++)
698 if (strcmp(cur_val->str, pdkim_algos[i]) == 0)
699 {
700 sig->algo = i;
701 break;
702 }
703 break;
704 case 'c':
705 for (i = 0; pdkim_combined_canons[i].str; i++)
706 if (strcmp(cur_val->str, pdkim_combined_canons[i].str) == 0)
707 {
708 sig->canon_headers = pdkim_combined_canons[i].canon_headers;
709 sig->canon_body = pdkim_combined_canons[i].canon_body;
710 break;
711 }
712 break;
713 case 'q':
714 for (i = 0; pdkim_querymethods[i]; i++)
715 if (strcmp(cur_val->str, pdkim_querymethods[i]) == 0)
716 {
717 sig->querymethod = i;
718 break;
719 }
720 break;
721 case 's':
722 sig->selector = strdup(cur_val->str); break;
723 case 'd':
724 sig->domain = strdup(cur_val->str); break;
725 case 'i':
726 sig->identity = pdkim_decode_qp(cur_val->str); break;
727 case 't':
728 sig->created = strtoul(cur_val->str, NULL, 10); break;
729 case 'x':
730 sig->expires = strtoul(cur_val->str, NULL, 10); break;
731 case 'l':
732 sig->bodylength = strtol(cur_val->str, NULL, 10); break;
733 case 'h':
734 sig->headernames = strdup(cur_val->str); break;
735 case 'z':
736 sig->copiedheaders = pdkim_decode_qp(cur_val->str); break;
737 default:
0d04a285 738 DEBUG(D_acl) debug_printf(" Unknown tag encountered\n");
3045f050
JH
739 break;
740 }
741 }
742 pdkim_strclear(cur_tag);
743 pdkim_strclear(cur_val);
744 in_b_val = FALSE;
745 where = PDKIM_HDR_LIMBO;
80a47a2c 746 }
3045f050
JH
747 else
748 pdkim_strncat(cur_val, p, 1);
80a47a2c
TK
749 }
750
3045f050
JH
751NEXT_CHAR:
752 if (c == '\0')
753 break;
80a47a2c 754
3045f050
JH
755 if (!in_b_val)
756 *q++ = c;
80a47a2c
TK
757 }
758
3045f050
JH
759/* Make sure the most important bits are there. */
760if (!(sig->domain && (*(sig->domain) != '\0') &&
761 sig->selector && (*(sig->selector) != '\0') &&
762 sig->headernames && (*(sig->headernames) != '\0') &&
763 sig->bodyhash &&
764 sig->sigdata &&
765 sig->version))
766 {
767 pdkim_free_sig(sig);
768 return NULL;
80a47a2c
TK
769 }
770
3045f050
JH
771*q = '\0';
772/* Chomp raw header. The final newline must not be added to the signature. */
773q--;
774while (q > sig->rawsig_no_b_val && (*q == '\r' || *q == '\n'))
775 *q = '\0'; q--; /*XXX questionable code layout; possible bug */
80a47a2c 776
0d04a285 777DEBUG(D_acl)
3045f050 778 {
0d04a285 779 debug_printf(
3045f050 780 "PDKIM >> Raw signature w/o b= tag value >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
0d04a285
JH
781 pdkim_quoteprint(sig->rawsig_no_b_val, strlen(sig->rawsig_no_b_val), 1);
782 debug_printf(
abe1010c 783 "PDKIM >> Sig size: %4d bits\n", sig->sigdata_len*8);
0d04a285 784 debug_printf(
3045f050 785 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
80a47a2c 786 }
80a47a2c 787
3045f050
JH
788if ( !(sig->sha1_body = malloc(sizeof(sha1_context)))
789 || !(sig->sha2_body = malloc(sizeof(sha2_context)))
790 )
791 {
792 pdkim_free_sig(sig);
793 return NULL;
80a47a2c
TK
794 }
795
3045f050
JH
796sha1_starts(sig->sha1_body);
797sha2_starts(sig->sha2_body, 0);
80a47a2c 798
3045f050 799return sig;
80a47a2c
TK
800}
801
802
803/* -------------------------------------------------------------------------- */
80a47a2c 804
3045f050
JH
805pdkim_pubkey *
806pdkim_parse_pubkey_record(pdkim_ctx *ctx, char *raw_record)
807{
808pdkim_pubkey *pub;
809char *p;
810pdkim_str *cur_tag = NULL;
811pdkim_str *cur_val = NULL;
812int where = PDKIM_HDR_LIMBO;
80a47a2c 813
3045f050 814if (!(pub = malloc(sizeof(pdkim_pubkey)))) return NULL;
abe1010c 815memset(pub, 0, sizeof(pdkim_pubkey));
80a47a2c 816
3045f050
JH
817for (p = raw_record; ; p++)
818 {
819 char c = *p;
80a47a2c 820
3045f050
JH
821 /* Ignore FWS */
822 if (c == '\r' || c == '\n')
823 goto NEXT_CHAR;
80a47a2c 824
3045f050
JH
825 if (where == PDKIM_HDR_LIMBO)
826 {
827 /* In limbo, just wait for a tag-char to appear */
828 if (!(c >= 'a' && c <= 'z'))
829 goto NEXT_CHAR;
80a47a2c 830
3045f050 831 where = PDKIM_HDR_TAG;
80a47a2c
TK
832 }
833
3045f050
JH
834 if (where == PDKIM_HDR_TAG)
835 {
836 if (!cur_tag)
837 cur_tag = pdkim_strnew(NULL);
80a47a2c 838
3045f050
JH
839 if (c >= 'a' && c <= 'z')
840 pdkim_strncat(cur_tag, p, 1);
80a47a2c 841
3045f050
JH
842 if (c == '=')
843 {
844 where = PDKIM_HDR_VALUE;
845 goto NEXT_CHAR;
80a47a2c
TK
846 }
847 }
848
3045f050
JH
849 if (where == PDKIM_HDR_VALUE)
850 {
851 if (!cur_val)
852 cur_val = pdkim_strnew(NULL);
853
854 if (c == '\r' || c == '\n')
855 goto NEXT_CHAR;
856
857 if (c == ';' || c == '\0')
858 {
859 if (cur_tag->len > 0)
860 {
861 pdkim_strtrim(cur_val);
0d04a285 862 DEBUG(D_acl) debug_printf(" %s=%s\n", cur_tag->str, cur_val->str);
3045f050
JH
863
864 switch (cur_tag->str[0])
865 {
866 case 'v':
867 /* This tag isn't evaluated because:
868 - We only support version DKIM1.
869 - Which is the default for this value (set below)
870 - Other versions are currently not specified. */
871 break;
872 case 'h':
873 pub->hashes = strdup(cur_val->str); break;
874 case 'g':
875 pub->granularity = strdup(cur_val->str); break;
876 case 'n':
877 pub->notes = pdkim_decode_qp(cur_val->str); break;
878 case 'p':
879 pub->key = pdkim_decode_base64(cur_val->str, &(pub->key_len)); break;
880 case 'k':
881 pub->hashes = strdup(cur_val->str); break;
882 case 's':
883 pub->srvtype = strdup(cur_val->str); break;
884 case 't':
abe1010c
JH
885 if (strchr(cur_val->str, 'y') != NULL) pub->testing = 1;
886 if (strchr(cur_val->str, 's') != NULL) pub->no_subdomaining = 1;
3045f050
JH
887 break;
888 default:
0d04a285 889 DEBUG(D_acl) debug_printf(" Unknown tag encountered\n");
3045f050
JH
890 break;
891 }
892 }
893 pdkim_strclear(cur_tag);
894 pdkim_strclear(cur_val);
895 where = PDKIM_HDR_LIMBO;
80a47a2c 896 }
3045f050
JH
897 else
898 pdkim_strncat(cur_val, p, 1);
80a47a2c
TK
899 }
900
3045f050
JH
901NEXT_CHAR:
902 if (c == '\0') break;
80a47a2c
TK
903 }
904
3045f050
JH
905/* Set fallback defaults */
906if (!pub->version ) pub->version = strdup(PDKIM_PUB_RECORD_VERSION);
907if (!pub->granularity) pub->granularity = strdup("*");
908if (!pub->keytype ) pub->keytype = strdup("rsa");
909if (!pub->srvtype ) pub->srvtype = strdup("*");
80a47a2c 910
3045f050
JH
911/* p= is required */
912if (pub->key)
80a47a2c 913 return pub;
3045f050
JH
914
915pdkim_free_pubkey(pub);
916return NULL;
80a47a2c
TK
917}
918
919
920/* -------------------------------------------------------------------------- */
3045f050
JH
921
922int
923pdkim_update_bodyhash(pdkim_ctx *ctx, const char *data, int len)
924{
925pdkim_signature *sig = ctx->sig;
926/* Cache relaxed version of data */
927char *relaxed_data = NULL;
928int relaxed_len = 0;
929
930/* Traverse all signatures, updating their hashes. */
931while (sig)
932 {
933 /* Defaults to simple canon (no further treatment necessary) */
934 const char *canon_data = data;
935 int canon_len = len;
936
937 if (sig->canon_body == PDKIM_CANON_RELAXED)
938 {
939 /* Relax the line if not done already */
940 if (!relaxed_data)
941 {
942 BOOL seen_wsp = FALSE;
943 const char *p;
944 int q = 0;
945
946 if (!(relaxed_data = malloc(len+1)))
947 return PDKIM_ERR_OOM;
948
949 for (p = data; *p; p++)
950 {
951 char c = *p;
952 if (c == '\r')
953 {
954 if (q > 0 && relaxed_data[q-1] == ' ')
955 q--;
956 }
957 else if (c == '\t' || c == ' ')
958 {
959 c = ' '; /* Turns WSP into SP */
960 if (seen_wsp)
961 continue;
962 seen_wsp = TRUE;
963 }
964 else
965 seen_wsp = FALSE;
966 relaxed_data[q++] = c;
967 }
968 relaxed_data[q] = '\0';
969 relaxed_len = q;
80a47a2c 970 }
3045f050
JH
971 canon_data = relaxed_data;
972 canon_len = relaxed_len;
80a47a2c
TK
973 }
974
3045f050
JH
975 /* Make sure we don't exceed the to-be-signed body length */
976 if ( sig->bodylength >= 0
977 && sig->signed_body_bytes + (unsigned long)canon_len > sig->bodylength
978 )
979 canon_len = sig->bodylength - sig->signed_body_bytes;
80a47a2c 980
3045f050
JH
981 if (canon_len > 0)
982 {
983 if (sig->algo == PDKIM_ALGO_RSA_SHA1)
abe1010c 984 sha1_update(sig->sha1_body, (unsigned char *)canon_data, canon_len);
3045f050 985 else
abe1010c 986 sha2_update(sig->sha2_body, (unsigned char *)canon_data, canon_len);
3045f050
JH
987
988 sig->signed_body_bytes += canon_len;
0d04a285 989 DEBUG(D_acl) pdkim_quoteprint(canon_data, canon_len, 1);
80a47a2c
TK
990 }
991
3045f050 992 sig = sig->next;
80a47a2c
TK
993 }
994
3045f050
JH
995if (relaxed_data) free(relaxed_data);
996return PDKIM_OK;
6ab02e3f 997}
80a47a2c
TK
998
999
1000/* -------------------------------------------------------------------------- */
80a47a2c 1001
3045f050
JH
1002int
1003pdkim_finish_bodyhash(pdkim_ctx *ctx)
1004{
f4d091fb 1005pdkim_signature *sig;
80a47a2c 1006
3045f050 1007/* Traverse all signatures */
f4d091fb 1008for (sig = ctx->sig; sig; sig = sig->next)
3045f050
JH
1009 { /* Finish hashes */
1010 unsigned char bh[32]; /* SHA-256 = 32 Bytes, SHA-1 = 20 Bytes */
1011
1012 if (sig->algo == PDKIM_ALGO_RSA_SHA1)
abe1010c 1013 sha1_finish(sig->sha1_body, bh);
3045f050 1014 else
abe1010c 1015 sha2_finish(sig->sha2_body, bh);
3045f050 1016
0d04a285 1017 DEBUG(D_acl)
3045f050 1018 {
0d04a285
JH
1019 debug_printf("PDKIM [%s] Body bytes hashed: %lu\n"
1020 "PDKIM [%s] bh computed: ",
1021 sig->domain, sig->signed_body_bytes, sig->domain);
1022 pdkim_hexprint((char *)bh, sig->algo == PDKIM_ALGO_RSA_SHA1 ? 20 : 32, 1);
80a47a2c 1023 }
3045f050
JH
1024
1025 /* SIGNING -------------------------------------------------------------- */
1026 if (ctx->mode == PDKIM_MODE_SIGN)
1027 {
1028 sig->bodyhash_len = (sig->algo == PDKIM_ALGO_RSA_SHA1)?20:32;
1029
f4d091fb 1030 sig->bodyhash = string_copyn(US bh, sig->bodyhash_len);
3045f050
JH
1031
1032 /* If bodylength limit is set, and we have received less bytes
1033 than the requested amount, effectively remove the limit tag. */
1034 if (sig->signed_body_bytes < sig->bodylength)
1035 sig->bodylength = -1;
80a47a2c 1036 }
3045f050
JH
1037
1038 /* VERIFICATION --------------------------------------------------------- */
1039 else
1040 {
1041 /* Compare bodyhash */
1042 if (memcmp(bh, sig->bodyhash,
1043 (sig->algo == PDKIM_ALGO_RSA_SHA1)?20:32) == 0)
1044 {
0d04a285 1045 DEBUG(D_acl) debug_printf("PDKIM [%s] Body hash verified OK\n", sig->domain);
80a47a2c 1046 }
3045f050
JH
1047 else
1048 {
0d04a285 1049 DEBUG(D_acl)
3045f050 1050 {
0d04a285
JH
1051 debug_printf("PDKIM [%s] bh signature: ", sig->domain);
1052 pdkim_hexprint(sig->bodyhash,
1053 sig->algo == PDKIM_ALGO_RSA_SHA1 ? 20 : 32, 1);
1054 debug_printf("PDKIM [%s] Body hash did NOT verify\n", sig->domain);
3045f050 1055 }
3045f050
JH
1056 sig->verify_status = PDKIM_VERIFY_FAIL;
1057 sig->verify_ext_status = PDKIM_VERIFY_FAIL_BODY;
80a47a2c
TK
1058 }
1059 }
80a47a2c
TK
1060 }
1061
3045f050 1062return PDKIM_OK;
6ab02e3f 1063}
80a47a2c
TK
1064
1065
1066
1067/* -------------------------------------------------------------------------- */
1068/* Callback from pdkim_feed below for processing complete body lines */
3045f050 1069
0d04a285 1070static int
3045f050
JH
1071pdkim_bodyline_complete(pdkim_ctx *ctx)
1072{
1073char *p = ctx->linebuf;
1074int n = ctx->linebuf_offset;
c14470c3 1075pdkim_signature *sig = ctx->sig; /*XXX assumes only one sig */
3045f050
JH
1076
1077/* Ignore extra data if we've seen the end-of-data marker */
1078if (ctx->seen_eod) goto BAIL;
1079
1080/* We've always got one extra byte to stuff a zero ... */
0d04a285 1081ctx->linebuf[ctx->linebuf_offset] = '\0';
3045f050 1082
0d04a285
JH
1083/* Terminate on EOD marker */
1084if (memcmp(p, ".\r\n", 3) == 0)
3045f050 1085 {
0d04a285
JH
1086 /* In simple body mode, if any empty lines were buffered,
1087 replace with one. rfc 4871 3.4.3 */
1088 /*XXX checking the signed-body-bytes is a gross hack; I think
1089 it indicates that all linebreaks should be buffered, including
1090 the one terminating a text line */
1091 if ( sig && sig->canon_body == PDKIM_CANON_SIMPLE
1092 && sig->signed_body_bytes == 0
1093 && ctx->num_buffered_crlf > 0
1094 )
1095 pdkim_update_bodyhash(ctx, "\r\n", 2);
1096
1097 ctx->seen_eod = TRUE;
1098 goto BAIL;
1099 }
1100/* Unstuff dots */
1101if (memcmp(p, "..", 2) == 0)
1102 {
1103 p++;
1104 n--;
80a47a2c
TK
1105 }
1106
3045f050
JH
1107/* Empty lines need to be buffered until we find a non-empty line */
1108if (memcmp(p, "\r\n", 2) == 0)
1109 {
1110 ctx->num_buffered_crlf++;
1111 goto BAIL;
80a47a2c
TK
1112 }
1113
c14470c3 1114if (sig && sig->canon_body == PDKIM_CANON_RELAXED)
3045f050
JH
1115 {
1116 /* Lines with just spaces need to be buffered too */
1117 char *check = p;
1118 while (memcmp(check, "\r\n", 2) != 0)
1119 {
1120 char c = *check;
6a11a9e6 1121
3045f050
JH
1122 if (c != '\t' && c != ' ')
1123 goto PROCESS;
1124 check++;
6a11a9e6
JH
1125 }
1126
3045f050
JH
1127 ctx->num_buffered_crlf++;
1128 goto BAIL;
1129}
6a11a9e6 1130
3045f050
JH
1131PROCESS:
1132/* At this point, we have a non-empty line, so release the buffered ones. */
1133while (ctx->num_buffered_crlf)
1134 {
1135 pdkim_update_bodyhash(ctx, "\r\n", 2);
1136 ctx->num_buffered_crlf--;
80a47a2c
TK
1137 }
1138
3045f050 1139pdkim_update_bodyhash(ctx, p, n);
80a47a2c 1140
3045f050
JH
1141BAIL:
1142ctx->linebuf_offset = 0;
1143return PDKIM_OK;
80a47a2c
TK
1144}
1145
1146
1147/* -------------------------------------------------------------------------- */
1148/* Callback from pdkim_feed below for processing complete headers */
1149#define DKIM_SIGNATURE_HEADERNAME "DKIM-Signature:"
3045f050
JH
1150
1151int
1152pdkim_header_complete(pdkim_ctx *ctx)
1153{
3045f050
JH
1154/* Special case: The last header can have an extra \r appended */
1155if ( (ctx->cur_header->len > 1) &&
1156 (ctx->cur_header->str[(ctx->cur_header->len)-1] == '\r') )
1157 {
1158 ctx->cur_header->str[(ctx->cur_header->len)-1] = '\0';
1159 ctx->cur_header->len--;
80a47a2c
TK
1160 }
1161
3045f050
JH
1162ctx->num_headers++;
1163if (ctx->num_headers > PDKIM_MAX_HEADERS) goto BAIL;
80a47a2c 1164
3045f050
JH
1165/* SIGNING -------------------------------------------------------------- */
1166if (ctx->mode == PDKIM_MODE_SIGN)
0d04a285
JH
1167 {
1168 pdkim_signature *sig;
1169
1170 for (sig = ctx->sig; sig; sig = sig->next) /* Traverse all signatures */
3045f050
JH
1171 if (header_name_match(ctx->cur_header->str,
1172 sig->sign_headers?
1173 sig->sign_headers:
1174 PDKIM_DEFAULT_SIGN_HEADERS, 0) == PDKIM_OK)
1175 {
37f8b554 1176 pdkim_stringlist *list;
80a47a2c 1177
37f8b554 1178 /* Add header to the signed headers list (in reverse order) */
3045f050
JH
1179 if (!(list = pdkim_prepend_stringlist(sig->headers,
1180 ctx->cur_header->str)))
1181 return PDKIM_ERR_OOM;
37f8b554 1182 sig->headers = list;
3045f050 1183 }
0d04a285 1184 }
94431adb 1185
0d04a285 1186/* VERIFICATION ----------------------------------------------------------- */
3045f050
JH
1187/* DKIM-Signature: headers are added to the verification list */
1188if (ctx->mode == PDKIM_MODE_VERIFY)
1189 {
1190 if (strncasecmp(ctx->cur_header->str,
1191 DKIM_SIGNATURE_HEADERNAME,
1192 strlen(DKIM_SIGNATURE_HEADERNAME)) == 0)
1193 {
1194 pdkim_signature *new_sig;
80a47a2c 1195
3045f050 1196 /* Create and chain new signature block */
0d04a285 1197 DEBUG(D_acl) debug_printf(
3045f050 1198 "PDKIM >> Found sig, trying to parse >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
3045f050
JH
1199
1200 if ((new_sig = pdkim_parse_sig_header(ctx, ctx->cur_header->str)))
1201 {
1202 pdkim_signature *last_sig = ctx->sig;
1203 if (!last_sig)
1204 ctx->sig = new_sig;
1205 else
1206 {
1207 while (last_sig->next) last_sig = last_sig->next;
1208 last_sig->next = new_sig;
1209 }
80a47a2c 1210 }
abe1010c 1211 else
0d04a285
JH
1212 DEBUG(D_acl) debug_printf(
1213 "Error while parsing signature header\n"
3045f050 1214 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
80a47a2c 1215 }
37f8b554 1216
3045f050
JH
1217 /* every other header is stored for signature verification */
1218 else
1219 {
1220 pdkim_stringlist *list;
1221
abe1010c 1222 if (!(list = pdkim_prepend_stringlist(ctx->headers, ctx->cur_header->str)))
3045f050
JH
1223 return PDKIM_ERR_OOM;
1224 ctx->headers = list;
80a47a2c
TK
1225 }
1226 }
1227
3045f050
JH
1228BAIL:
1229pdkim_strclear(ctx->cur_header); /* Re-use existing pdkim_str */
1230return PDKIM_OK;
6ab02e3f 1231}
80a47a2c
TK
1232
1233
1234
1235/* -------------------------------------------------------------------------- */
1236#define HEADER_BUFFER_FRAG_SIZE 256
3045f050
JH
1237
1238DLLEXPORT int
1239pdkim_feed (pdkim_ctx *ctx, char *data, int len)
1240{
1241int p;
1242
1243for (p = 0; p<len; p++)
1244 {
1245 char c = data[p];
1246
1247 if (ctx->past_headers)
1248 {
1249 /* Processing body byte */
0d04a285 1250 ctx->linebuf[ctx->linebuf_offset++] = c;
3045f050
JH
1251 if (c == '\n')
1252 {
1253 int rc = pdkim_bodyline_complete(ctx); /* End of line */
1254 if (rc != PDKIM_OK) return rc;
80a47a2c 1255 }
3045f050
JH
1256 if (ctx->linebuf_offset == (PDKIM_MAX_BODY_LINE_LEN-1))
1257 return PDKIM_ERR_LONG_LINE;
80a47a2c 1258 }
3045f050
JH
1259 else
1260 {
1261 /* Processing header byte */
1262 if (c != '\r')
1263 {
1264 if (c == '\n')
1265 {
1266 if (ctx->seen_lf)
1267 {
1268 int rc = pdkim_header_complete(ctx); /* Seen last header line */
1269 if (rc != PDKIM_OK) return rc;
1270
0d04a285 1271 ctx->past_headers = TRUE;
3045f050 1272 ctx->seen_lf = 0;
0d04a285 1273 DEBUG(D_acl) debug_printf(
3045f050 1274 "PDKIM >> Hashed body data, canonicalized >>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
3045f050
JH
1275 continue;
1276 }
1277 else
0d04a285 1278 ctx->seen_lf = TRUE;
3045f050
JH
1279 }
1280 else if (ctx->seen_lf)
1281 {
1282 if (!(c == '\t' || c == ' '))
1283 {
1284 int rc = pdkim_header_complete(ctx); /* End of header */
1285 if (rc != PDKIM_OK) return rc;
1286 }
0d04a285 1287 ctx->seen_lf = FALSE;
3045f050 1288 }
80a47a2c 1289 }
3045f050
JH
1290
1291 if (!ctx->cur_header)
1292 if (!(ctx->cur_header = pdkim_strnew(NULL)))
1293 return PDKIM_ERR_OOM;
1294
1295 if (ctx->cur_header->len < PDKIM_MAX_HEADER_LEN)
1296 if (!pdkim_strncat(ctx->cur_header, &data[p], 1))
1297 return PDKIM_ERR_OOM;
80a47a2c
TK
1298 }
1299 }
3045f050 1300return PDKIM_OK;
6ab02e3f 1301}
80a47a2c 1302
05b7d6de
JB
1303/*
1304 * RFC 5322 specifies that header line length SHOULD be no more than 78
1305 * lets make it so!
1306 * pdkim_headcat
1307 * returns char*
1308 *
1309 * col: this int holds and receives column number (octets since last '\n')
1310 * str: partial string to append to
94431adb 1311 * pad: padding, split line or space after before or after eg: ";"
05b7d6de
JB
1312 * intro: - must join to payload eg "h=", usually the tag name
1313 * payload: eg base64 data - long data can be split arbitrarily.
1314 *
1315 * this code doesn't fold the header in some of the places that RFC4871
1316 * allows: As per RFC5322(2.2.3) it only folds before or after tag-value
1317 * pairs and inside long values. it also always spaces or breaks after the
94431adb 1318 * "pad"
05b7d6de
JB
1319 *
1320 * no guarantees are made for output given out-of range input. like tag
1321 * names loinger than 78, or bogus col. Input is assumed to be free of line breaks.
1322 */
1323
3045f050
JH
1324static char *
1325pdkim_headcat(int *col, pdkim_str *str, const char * pad,
1326 const char *intro, const char *payload)
1327{
1328size_t l;
1329
1330if (pad)
05b7d6de 1331 {
3045f050
JH
1332 l = strlen(pad);
1333 if (*col + l > 78)
05b7d6de 1334 {
3045f050
JH
1335 pdkim_strcat(str, "\r\n\t");
1336 *col = 1;
05b7d6de 1337 }
3045f050
JH
1338 pdkim_strncat(str, pad, l);
1339 *col += l;
05b7d6de
JB
1340 }
1341
3045f050 1342l = (pad?1:0) + (intro?strlen(intro):0);
05b7d6de 1343
3045f050 1344if (*col + l > 78)
05b7d6de 1345 { /*can't fit intro - start a new line to make room.*/
3045f050
JH
1346 pdkim_strcat(str, "\r\n\t");
1347 *col = 1;
1348 l = intro?strlen(intro):0;
05b7d6de
JB
1349 }
1350
3045f050 1351l += payload ? strlen(payload):0 ;
05b7d6de 1352
3045f050 1353while (l>77)
05b7d6de 1354 { /* this fragment will not fit on a single line */
3045f050 1355 if (pad)
05b7d6de 1356 {
3045f050
JH
1357 pdkim_strcat(str, " ");
1358 *col += 1;
1359 pad = NULL; /* only want this once */
1360 l--;
05b7d6de 1361 }
3045f050
JH
1362
1363 if (intro)
05b7d6de 1364 {
3045f050
JH
1365 size_t sl = strlen(intro);
1366
abe1010c 1367 pdkim_strncat(str, intro, sl);
3045f050
JH
1368 *col += sl;
1369 l -= sl;
1370 intro = NULL; /* only want this once */
05b7d6de 1371 }
3045f050
JH
1372
1373 if (payload)
05b7d6de 1374 {
3045f050
JH
1375 size_t sl = strlen(payload);
1376 size_t chomp = *col+sl < 77 ? sl : 78-*col;
1377
abe1010c 1378 pdkim_strncat(str, payload, chomp);
3045f050
JH
1379 *col += chomp;
1380 payload += chomp;
1381 l -= chomp-1;
05b7d6de 1382 }
3045f050
JH
1383
1384 /* the while precondition tells us it didn't fit. */
1385 pdkim_strcat(str, "\r\n\t");
1386 *col = 1;
05b7d6de 1387 }
3045f050
JH
1388
1389if (*col + l > 78)
05b7d6de 1390 {
3045f050
JH
1391 pdkim_strcat(str, "\r\n\t");
1392 *col = 1;
1393 pad = NULL;
05b7d6de
JB
1394 }
1395
3045f050 1396if (pad)
05b7d6de 1397 {
3045f050
JH
1398 pdkim_strcat(str, " ");
1399 *col += 1;
1400 pad = NULL;
05b7d6de
JB
1401 }
1402
3045f050 1403if (intro)
05b7d6de 1404 {
3045f050
JH
1405 size_t sl = strlen(intro);
1406
1407 pdkim_strncat(str, intro, sl);
1408 *col += sl;
1409 l -= sl;
1410 intro = NULL;
05b7d6de 1411 }
3045f050
JH
1412
1413if (payload)
05b7d6de 1414 {
3045f050
JH
1415 size_t sl = strlen(payload);
1416
1417 pdkim_strncat(str, payload, sl);
1418 *col += sl;
05b7d6de
JB
1419 }
1420
3045f050 1421return str->str;
05b7d6de 1422}
80a47a2c 1423
3045f050 1424
80a47a2c 1425/* -------------------------------------------------------------------------- */
3045f050
JH
1426
1427char *
1428pdkim_create_header(pdkim_signature *sig, int final)
1429{
1430char *rc = NULL;
1431char *base64_bh = NULL;
1432char *base64_b = NULL;
1433int col = 0;
1434pdkim_str *hdr;
1435pdkim_str *canon_all;
1436
1437if (!(hdr = pdkim_strnew("DKIM-Signature: v="PDKIM_SIGNATURE_VERSION)))
1438 return NULL;
1439
1440if (!(canon_all = pdkim_strnew(pdkim_canons[sig->canon_headers])))
1441 goto BAIL;
1442
1443if (!(base64_bh = pdkim_encode_base64(sig->bodyhash, sig->bodyhash_len)))
1444 goto BAIL;
1445
1446col = strlen(hdr->str);
1447
1448/* Required and static bits */
abe1010c
JH
1449if ( pdkim_headcat(&col, hdr, ";", "a=", pdkim_algos[sig->algo])
1450 && pdkim_headcat(&col, hdr, ";", "q=", pdkim_querymethods[sig->querymethod])
1451 && pdkim_strcat(canon_all, "/")
1452 && pdkim_strcat(canon_all, pdkim_canons[sig->canon_body])
1453 && pdkim_headcat(&col, hdr, ";", "c=", canon_all->str)
1454 && pdkim_headcat(&col, hdr, ";", "d=", sig->domain)
1455 && pdkim_headcat(&col, hdr, ";", "s=", sig->selector)
3045f050
JH
1456 )
1457 {
1458 /* list of eader names can be split between items. */
05b7d6de 1459 {
3045f050
JH
1460 char *n = strdup(sig->headernames);
1461 char *f = n;
1462 char *i = "h=";
1463 char *s = ";";
1464
1465 if (!n) goto BAIL;
1466 while (*n)
05b7d6de 1467 {
abe1010c 1468 char *c = strchr(n, ':');
05b7d6de 1469
3045f050 1470 if (c) *c ='\0';
05b7d6de 1471
3045f050 1472 if (!i)
abe1010c 1473 if (!pdkim_headcat(&col, hdr, NULL, NULL, ":"))
3045f050
JH
1474 {
1475 free(f);
1476 goto BAIL;
1477 }
1478
abe1010c 1479 if (!pdkim_headcat(&col, hdr, s, i, n))
3045f050
JH
1480 {
1481 free(f);
1482 goto BAIL;
1483 }
1484
1485 if (!c)
1486 break;
1487
1488 n = c+1;
1489 s = NULL;
1490 i = NULL;
80a47a2c 1491 }
3045f050 1492 free(f);
80a47a2c 1493 }
05b7d6de 1494
3045f050
JH
1495 if(!pdkim_headcat(&col, hdr, ";", "bh=", base64_bh))
1496 goto BAIL;
1497
1498 /* Optional bits */
1499 if (sig->identity)
1500 if(!pdkim_headcat(&col, hdr, ";", "i=", sig->identity))
1501 goto BAIL;
1502
1503 if (sig->created > 0)
1504 {
1505 char minibuf[20];
1506
abe1010c 1507 snprintf(minibuf, 20, "%lu", sig->created);
3045f050
JH
1508 if(!pdkim_headcat(&col, hdr, ";", "t=", minibuf))
1509 goto BAIL;
1510 }
1511
1512 if (sig->expires > 0)
1513 {
1514 char minibuf[20];
1515
abe1010c 1516 snprintf(minibuf, 20, "%lu", sig->expires);
3045f050
JH
1517 if(!pdkim_headcat(&col, hdr, ";", "x=", minibuf))
1518 goto BAIL;
80a47a2c 1519 }
3045f050
JH
1520
1521 if (sig->bodylength >= 0)
1522 {
1523 char minibuf[20];
1524
abe1010c 1525 snprintf(minibuf, 20, "%lu", sig->bodylength);
3045f050
JH
1526 if(!pdkim_headcat(&col, hdr, ";", "l=", minibuf))
1527 goto BAIL;
80a47a2c
TK
1528 }
1529
3045f050
JH
1530 /* Preliminary or final version? */
1531 if (final)
1532 {
1533 if (!(base64_b = pdkim_encode_base64(sig->sigdata, sig->sigdata_len)))
1534 goto BAIL;
1535 if (!pdkim_headcat(&col, hdr, ";", "b=", base64_b))
1536 goto BAIL;
1537 }
1538 else
1539 if(!pdkim_headcat(&col, hdr, ";", "b=", ""))
1540 goto BAIL;
05b7d6de 1541
3045f050 1542 /* add trailing semicolon: I'm not sure if this is actually needed */
abe1010c 1543 if (!pdkim_headcat(&col, hdr, NULL, ";", ""))
3045f050 1544 goto BAIL;
80a47a2c
TK
1545 }
1546
3045f050 1547rc = strdup(hdr->str);
80a47a2c 1548
3045f050
JH
1549BAIL:
1550pdkim_strfree(hdr);
1551if (canon_all) pdkim_strfree(canon_all);
3045f050 1552return rc;
80a47a2c
TK
1553}
1554
1555
1556/* -------------------------------------------------------------------------- */
3045f050
JH
1557
1558DLLEXPORT int
1559pdkim_feed_finish(pdkim_ctx *ctx, pdkim_signature **return_signatures)
1560{
1561pdkim_signature *sig = ctx->sig;
1562pdkim_str *headernames = NULL; /* Collected signed header names */
1563
1564/* Check if we must still flush a (partial) header. If that is the
1565 case, the message has no body, and we must compute a body hash
1566 out of '<CR><LF>' */
1567if (ctx->cur_header && ctx->cur_header->len)
1568 {
1569 int rc = pdkim_header_complete(ctx);
1570 if (rc != PDKIM_OK) return rc;
1571 pdkim_update_bodyhash(ctx, "\r\n", 2);
80a47a2c 1572 }
3045f050 1573else
0d04a285 1574 DEBUG(D_acl) debug_printf(
3045f050 1575 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
80a47a2c 1576
3045f050
JH
1577/* Build (and/or evaluate) body hash */
1578if (pdkim_finish_bodyhash(ctx) != PDKIM_OK)
1579 return PDKIM_ERR_OOM;
80a47a2c 1580
3045f050
JH
1581/* SIGNING -------------------------------------------------------------- */
1582if (ctx->mode == PDKIM_MODE_SIGN)
1583 if (!(headernames = pdkim_strnew(NULL)))
1584 return PDKIM_ERR_OOM;
1585/* ---------------------------------------------------------------------- */
80a47a2c 1586
3045f050
JH
1587while (sig)
1588 {
1589 sha1_context sha1_headers;
1590 sha2_context sha2_headers;
1591 char *sig_hdr;
1592 char headerhash[32];
80a47a2c 1593
3045f050
JH
1594 if (sig->algo == PDKIM_ALGO_RSA_SHA1)
1595 sha1_starts(&sha1_headers);
1596 else
abe1010c 1597 sha2_starts(&sha2_headers, 0);
80a47a2c 1598
0d04a285
JH
1599 DEBUG(D_acl) debug_printf(
1600 "PDKIM >> Hashed header data, canonicalized, in sequence >>>>>>>>>>>>>>\n");
3045f050
JH
1601
1602 /* SIGNING ---------------------------------------------------------------- */
1603 /* When signing, walk through our header list and add them to the hash. As we
1604 go, construct a list of the header's names to use for the h= parameter. */
1605
1606 if (ctx->mode == PDKIM_MODE_SIGN)
1607 {
1608 pdkim_stringlist *p;
1609
1610 for (p = sig->headers; p; p = p->next)
1611 {
1612 char *rh = NULL;
1613 /* Collect header names (Note: colon presence is guaranteed here) */
abe1010c 1614 char *q = strchr(p->value, ':');
3045f050
JH
1615
1616 if (!(pdkim_strncat(headernames, p->value,
1617 (q-(p->value)) + (p->next ? 1 : 0))))
1618 return PDKIM_ERR_OOM;
1619
1620 rh = sig->canon_headers == PDKIM_CANON_RELAXED
abe1010c 1621 ? pdkim_relax_header(p->value, 1) /* cook header for relaxed canon */
3045f050
JH
1622 : strdup(p->value); /* just copy it for simple canon */
1623 if (!rh)
1624 return PDKIM_ERR_OOM;
1625
1626 /* Feed header to the hash algorithm */
1627 if (sig->algo == PDKIM_ALGO_RSA_SHA1)
1628 sha1_update(&(sha1_headers), (unsigned char *)rh, strlen(rh));
1629 else
1630 sha2_update(&(sha2_headers), (unsigned char *)rh, strlen(rh));
1631
0d04a285 1632 DEBUG(D_acl) pdkim_quoteprint(rh, strlen(rh), 1);
3045f050 1633 free(rh);
80a47a2c
TK
1634 }
1635 }
37f8b554 1636
3045f050
JH
1637 /* VERIFICATION ----------------------------------------------------------- */
1638 /* When verifying, walk through the header name list in the h= parameter and
1639 add the headers to the hash in that order. */
1640 else
1641 {
1642 char *b = strdup(sig->headernames);
1643 char *p = b;
1644 char *q = NULL;
1645 pdkim_stringlist *hdrs;
1646
1647 if (!b) return PDKIM_ERR_OOM;
1648
1649 /* clear tags */
1650 for (hdrs = ctx->headers; hdrs; hdrs = hdrs->next)
1651 hdrs->tag = 0;
1652
1653 while(1)
1654 {
abe1010c 1655 if ((q = strchr(p, ':')))
3045f050
JH
1656 *q = '\0';
1657
1658 for (hdrs = ctx->headers; hdrs; hdrs = hdrs->next)
1659 if ( hdrs->tag == 0
abe1010c 1660 && strncasecmp(hdrs->value, p, strlen(p)) == 0
3045f050
JH
1661 && (hdrs->value)[strlen(p)] == ':'
1662 )
1663 {
1664 char *rh;
1665
1666 rh = sig->canon_headers == PDKIM_CANON_RELAXED
abe1010c 1667 ? pdkim_relax_header(hdrs->value, 1) /* cook header for relaxed canon */
3045f050
JH
1668 : strdup(hdrs->value); /* just copy it for simple canon */
1669 if (!rh)
1670 return PDKIM_ERR_OOM;
1671
1672 /* Feed header to the hash algorithm */
1673 if (sig->algo == PDKIM_ALGO_RSA_SHA1)
abe1010c 1674 sha1_update(&sha1_headers, (unsigned char *)rh, strlen(rh));
3045f050 1675 else
abe1010c 1676 sha2_update(&sha2_headers, (unsigned char *)rh, strlen(rh));
3045f050 1677
0d04a285 1678 DEBUG(D_acl) pdkim_quoteprint(rh, strlen(rh), 1);
3045f050
JH
1679 free(rh);
1680 hdrs->tag = 1;
1681 break;
1682 }
1683
1684 if (!q) break;
1685 p = q+1;
80a47a2c 1686 }
3045f050 1687 free(b);
80a47a2c
TK
1688 }
1689
0d04a285 1690 DEBUG(D_acl) debug_printf(
3045f050 1691 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
80a47a2c 1692
3045f050
JH
1693 /* SIGNING ---------------------------------------------------------------- */
1694 if (ctx->mode == PDKIM_MODE_SIGN)
1695 {
1696 /* Copy headernames to signature struct */
1697 sig->headernames = strdup(headernames->str);
1698 pdkim_strfree(headernames);
80a47a2c 1699
3045f050
JH
1700 /* Create signature header with b= omitted */
1701 sig_hdr = pdkim_create_header(ctx->sig, 0);
80a47a2c 1702 }
80a47a2c 1703
3045f050
JH
1704 /* VERIFICATION ----------------------------------------------------------- */
1705 else
1706 sig_hdr = strdup(sig->rawsig_no_b_val);
1707 /* ------------------------------------------------------------------------ */
1708
1709 if (!sig_hdr)
1710 return PDKIM_ERR_OOM;
80a47a2c 1711
3045f050
JH
1712 /* Relax header if necessary */
1713 if (sig->canon_headers == PDKIM_CANON_RELAXED)
1714 {
1715 char *relaxed_hdr = pdkim_relax_header(sig_hdr, 0);
1716
1717 free(sig_hdr);
1718 if (!relaxed_hdr)
1719 return PDKIM_ERR_OOM;
1720 sig_hdr = relaxed_hdr;
80a47a2c
TK
1721 }
1722
0d04a285 1723 DEBUG(D_acl)
3045f050 1724 {
0d04a285 1725 debug_printf(
3045f050 1726 "PDKIM >> Signed DKIM-Signature header, canonicalized >>>>>>>>>>>>>>>>>\n");
0d04a285
JH
1727 pdkim_quoteprint(sig_hdr, strlen(sig_hdr), 1);
1728 debug_printf(
3045f050 1729 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
80a47a2c 1730 }
3045f050
JH
1731
1732 /* Finalize header hash */
1733 if (sig->algo == PDKIM_ALGO_RSA_SHA1)
1734 {
abe1010c
JH
1735 sha1_update(&sha1_headers, (unsigned char *)sig_hdr, strlen(sig_hdr));
1736 sha1_finish(&sha1_headers, (unsigned char *)headerhash);
3045f050 1737
0d04a285 1738 DEBUG(D_acl)
3045f050 1739 {
0d04a285
JH
1740 debug_printf( "PDKIM [%s] hh computed: ", sig->domain);
1741 pdkim_hexprint(headerhash, 20, 1);
80a47a2c 1742 }
80a47a2c 1743 }
3045f050
JH
1744 else
1745 {
abe1010c
JH
1746 sha2_update(&sha2_headers, (unsigned char *)sig_hdr, strlen(sig_hdr));
1747 sha2_finish(&sha2_headers, (unsigned char *)headerhash);
3045f050 1748
0d04a285 1749 DEBUG(D_acl)
3045f050 1750 {
0d04a285
JH
1751 debug_printf("PDKIM [%s] hh computed: ", sig->domain);
1752 pdkim_hexprint(headerhash, 32, 1);
80a47a2c 1753 }
80a47a2c
TK
1754 }
1755
3045f050 1756 free(sig_hdr);
80a47a2c 1757
3045f050
JH
1758 /* SIGNING ---------------------------------------------------------------- */
1759 if (ctx->mode == PDKIM_MODE_SIGN)
1760 {
1761 rsa_context rsa;
69a70afa 1762int perr;
80a47a2c 1763
3045f050 1764 rsa_init(&rsa, RSA_PKCS_V15, 0);
80a47a2c 1765
3045f050 1766 /* Perform private key operation */
69a70afa
JH
1767 if ((perr = rsa_parse_key(&rsa, (unsigned char *)sig->rsa_privkey,
1768 strlen(sig->rsa_privkey), NULL, 0)) != 0)
1769{
1770debug_printf("rsa_parse_key: perr 0x%x\n", perr);
3045f050 1771 return PDKIM_ERR_RSA_PRIVKEY;
69a70afa 1772}
80a47a2c 1773
3045f050 1774 sig->sigdata_len = mpi_size(&(rsa.N));
f4d091fb 1775 sig->sigdata = store_get(sig->sigdata_len);
3045f050
JH
1776
1777 if (rsa_pkcs1_sign( &rsa, RSA_PRIVATE,
1778 ((sig->algo == PDKIM_ALGO_RSA_SHA1)?
1779 SIG_RSA_SHA1:SIG_RSA_SHA256),
1780 0,
1781 (unsigned char *)headerhash,
1782 (unsigned char *)sig->sigdata ) != 0)
1783 return PDKIM_ERR_RSA_SIGNING;
80a47a2c 1784
3045f050 1785 rsa_free(&rsa);
80a47a2c 1786
0d04a285 1787 DEBUG(D_acl)
3045f050 1788 {
0d04a285
JH
1789 debug_printf( "PDKIM [%s] b computed: ", sig->domain);
1790 pdkim_hexprint(sig->sigdata, sig->sigdata_len, 1);
80a47a2c 1791 }
80a47a2c 1792
abe1010c 1793 if (!(sig->signature_header = pdkim_create_header(ctx->sig, 1)))
3045f050 1794 return PDKIM_ERR_OOM;
80a47a2c 1795 }
80a47a2c 1796
3045f050
JH
1797 /* VERIFICATION ----------------------------------------------------------- */
1798 else
1799 {
1800 rsa_context rsa;
1801 char *dns_txt_name, *dns_txt_reply;
1802
1803 rsa_init(&rsa, RSA_PKCS_V15, 0);
1804
1805 if (!(dns_txt_name = malloc(PDKIM_DNS_TXT_MAX_NAMELEN)))
1806 return PDKIM_ERR_OOM;
1807
1808 if (!(dns_txt_reply = malloc(PDKIM_DNS_TXT_MAX_RECLEN)))
1809 {
1810 free(dns_txt_name);
1811 return PDKIM_ERR_OOM;
80a47a2c
TK
1812 }
1813
3045f050
JH
1814 memset(dns_txt_reply, 0, PDKIM_DNS_TXT_MAX_RECLEN);
1815 memset(dns_txt_name , 0, PDKIM_DNS_TXT_MAX_NAMELEN);
1816
abe1010c 1817 if (snprintf(dns_txt_name, PDKIM_DNS_TXT_MAX_NAMELEN,
3045f050 1818 "%s._domainkey.%s.",
abe1010c 1819 sig->selector, sig->domain) >= PDKIM_DNS_TXT_MAX_NAMELEN)
3045f050
JH
1820 {
1821 sig->verify_status = PDKIM_VERIFY_INVALID;
1822 sig->verify_ext_status = PDKIM_VERIFY_INVALID_BUFFER_SIZE;
1823 goto NEXT_VERIFY;
80a47a2c 1824 }
3045f050
JH
1825
1826 if ( ctx->dns_txt_callback(dns_txt_name, dns_txt_reply) != PDKIM_OK
1827 || dns_txt_reply[0] == '\0')
1828 {
1829 sig->verify_status = PDKIM_VERIFY_INVALID;
1830 sig->verify_ext_status = PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE;
1831 goto NEXT_VERIFY;
80a47a2c
TK
1832 }
1833
0d04a285 1834 DEBUG(D_acl)
3045f050 1835 {
0d04a285
JH
1836 debug_printf(
1837 "PDKIM >> Parsing public key record >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"
1838 " Raw record: ");
1839 pdkim_quoteprint(dns_txt_reply, strlen(dns_txt_reply), 1);
80a47a2c 1840 }
3045f050 1841
abe1010c 1842 if (!(sig->pubkey = pdkim_parse_pubkey_record(ctx, dns_txt_reply)))
3045f050
JH
1843 {
1844 sig->verify_status = PDKIM_VERIFY_INVALID;
1845 sig->verify_ext_status = PDKIM_VERIFY_INVALID_PUBKEY_PARSING;
1846
0d04a285
JH
1847 DEBUG(D_acl) debug_printf(
1848 " Error while parsing public key record\n"
3045f050 1849 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
3045f050 1850 goto NEXT_VERIFY;
80a47a2c
TK
1851 }
1852
0d04a285 1853 DEBUG(D_acl) debug_printf(
3045f050 1854 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
3045f050
JH
1855
1856 if (rsa_parse_public_key(&rsa,
1857 (unsigned char *)sig->pubkey->key,
1858 sig->pubkey->key_len) != 0)
1859 {
1860 sig->verify_status = PDKIM_VERIFY_INVALID;
1861 sig->verify_ext_status = PDKIM_VERIFY_INVALID_PUBKEY_PARSING;
1862 goto NEXT_VERIFY;
80a47a2c
TK
1863 }
1864
3045f050
JH
1865 /* Check the signature */
1866 if (rsa_pkcs1_verify(&rsa,
1867 RSA_PUBLIC,
1868 ((sig->algo == PDKIM_ALGO_RSA_SHA1)?
1869 SIG_RSA_SHA1:SIG_RSA_SHA256),
1870 0,
1871 (unsigned char *)headerhash,
1872 (unsigned char *)sig->sigdata) != 0)
1873 {
1874 sig->verify_status = PDKIM_VERIFY_FAIL;
1875 sig->verify_ext_status = PDKIM_VERIFY_FAIL_MESSAGE;
1876 goto NEXT_VERIFY;
ff7ddfd7
TK
1877 }
1878
3045f050
JH
1879 /* We have a winner! (if bodydhash was correct earlier) */
1880 if (sig->verify_status == PDKIM_VERIFY_NONE)
1881 sig->verify_status = PDKIM_VERIFY_PASS;
1882
1883NEXT_VERIFY:
1884
0d04a285 1885 DEBUG(D_acl)
3045f050 1886 {
0d04a285 1887 debug_printf("PDKIM [%s] signature status: %s",
3045f050
JH
1888 sig->domain, pdkim_verify_status_str(sig->verify_status));
1889 if (sig->verify_ext_status > 0)
0d04a285 1890 debug_printf(" (%s)\n",
3045f050
JH
1891 pdkim_verify_ext_status_str(sig->verify_ext_status));
1892 else
0d04a285 1893 debug_printf("\n");
80a47a2c 1894 }
80a47a2c 1895
3045f050
JH
1896 rsa_free(&rsa);
1897 free(dns_txt_name);
1898 free(dns_txt_reply);
80a47a2c
TK
1899 }
1900
3045f050 1901 sig = sig->next;
80a47a2c
TK
1902 }
1903
3045f050
JH
1904/* If requested, set return pointer to signature(s) */
1905if (return_signatures)
1906 *return_signatures = ctx->sig;
80a47a2c 1907
3045f050 1908return PDKIM_OK;
80a47a2c
TK
1909}
1910
1911
1912/* -------------------------------------------------------------------------- */
3045f050
JH
1913
1914DLLEXPORT pdkim_ctx *
0d04a285 1915pdkim_init_verify(int(*dns_txt_callback)(char *, char *))
3045f050
JH
1916{
1917pdkim_ctx *ctx = malloc(sizeof(pdkim_ctx));
1918
1919if (!ctx)
1920 return NULL;
abe1010c 1921memset(ctx, 0, sizeof(pdkim_ctx));
3045f050
JH
1922
1923if (!(ctx->linebuf = malloc(PDKIM_MAX_BODY_LINE_LEN)))
1924 {
1925 free(ctx);
1926 return NULL;
80a47a2c
TK
1927 }
1928
3045f050 1929ctx->mode = PDKIM_MODE_VERIFY;
3045f050 1930ctx->dns_txt_callback = dns_txt_callback;
80a47a2c 1931
3045f050 1932return ctx;
80a47a2c
TK
1933}
1934
1935
1936/* -------------------------------------------------------------------------- */
80a47a2c 1937
3045f050 1938DLLEXPORT pdkim_ctx *
0d04a285 1939pdkim_init_sign(char *domain, char *selector, char *rsa_privkey)
3045f050
JH
1940{
1941pdkim_ctx *ctx;
1942pdkim_signature *sig;
80a47a2c 1943
3045f050
JH
1944if (!domain || !selector || !rsa_privkey)
1945 return NULL;
80a47a2c 1946
3045f050
JH
1947if (!(ctx = malloc(sizeof(pdkim_ctx))))
1948 return NULL;
abe1010c 1949memset(ctx, 0, sizeof(pdkim_ctx));
80a47a2c 1950
3045f050
JH
1951if (!(ctx->linebuf = malloc(PDKIM_MAX_BODY_LINE_LEN)))
1952 {
1953 free(ctx);
1954 return NULL;
80a47a2c
TK
1955 }
1956
3045f050
JH
1957if (!(sig = malloc(sizeof(pdkim_signature))))
1958 {
1959 free(ctx->linebuf);
1960 free(ctx);
1961 return NULL;
80a47a2c 1962 }
abe1010c 1963memset(sig, 0, sizeof(pdkim_signature));
80a47a2c 1964
3045f050 1965sig->bodylength = -1;
80a47a2c 1966
3045f050 1967ctx->mode = PDKIM_MODE_SIGN;
3045f050 1968ctx->sig = sig;
80a47a2c 1969
3045f050
JH
1970ctx->sig->domain = strdup(domain);
1971ctx->sig->selector = strdup(selector);
1972ctx->sig->rsa_privkey = strdup(rsa_privkey);
1973
1974if (!ctx->sig->domain || !ctx->sig->selector || !ctx->sig->rsa_privkey)
1975 goto BAIL;
1976
1977if (!(ctx->sig->sha1_body = malloc(sizeof(sha1_context))))
1978 goto BAIL;
1979sha1_starts(ctx->sig->sha1_body);
1980
1981if (!(ctx->sig->sha2_body = malloc(sizeof(sha2_context))))
1982 goto BAIL;
abe1010c 1983sha2_starts(ctx->sig->sha2_body, 0);
3045f050
JH
1984
1985return ctx;
1986
1987BAIL:
1988 pdkim_free_ctx(ctx);
1989 return NULL;
6ab02e3f 1990}
80a47a2c
TK
1991
1992/* -------------------------------------------------------------------------- */
3045f050
JH
1993
1994DLLEXPORT int
1995pdkim_set_optional(pdkim_ctx *ctx,
80a47a2c
TK
1996 char *sign_headers,
1997 char *identity,
1998 int canon_headers,
1999 int canon_body,
2000 long bodylength,
2001 int algo,
2002 unsigned long created,
3045f050
JH
2003 unsigned long expires)
2004{
80a47a2c 2005
3045f050
JH
2006if (identity)
2007 if (!(ctx->sig->identity = strdup(identity)))
2008 return PDKIM_ERR_OOM;
80a47a2c 2009
3045f050
JH
2010if (sign_headers)
2011 if (!(ctx->sig->sign_headers = strdup(sign_headers)))
2012 return PDKIM_ERR_OOM;
80a47a2c 2013
3045f050
JH
2014ctx->sig->canon_headers = canon_headers;
2015ctx->sig->canon_body = canon_body;
2016ctx->sig->bodylength = bodylength;
2017ctx->sig->algo = algo;
2018ctx->sig->created = created;
2019ctx->sig->expires = expires;
80a47a2c 2020
3045f050 2021return PDKIM_OK;
6ab02e3f 2022}
3045f050 2023
3045f050 2024