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