2 * PDKIM - a RFC4871 (DKIM) implementation
4 * Copyright (C) 2009 Tom Kistner <tom@duncanthrax.net>
6 * http://duncanthrax.net/pdkim/
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.
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.
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.
23 /* $Cambridge: exim/src/src/pdkim/pdkim.c,v 1.1.2.10 2009/04/09 14:47:50 tom Exp $ */
37 #define PDKIM_SIGNATURE_VERSION "1"
38 #define PDKIM_PUB_RECORD_VERSION "DKIM1"
40 #define PDKIM_MAX_HEADER_LEN 65536
41 #define PDKIM_MAX_HEADERS 512
42 #define PDKIM_MAX_BODY_LINE_LEN 1024
43 #define PDKIM_DNS_TXT_MAX_NAMELEN 1024
44 #define PDKIM_DEFAULT_SIGN_HEADERS "From:Sender:Reply-To:Subject:Date:"\
45 "Message-ID:To:Cc:MIME-Version:Content-Type:"\
46 "Content-Transfer-Encoding:Content-ID:"\
47 "Content-Description:Resent-Date:Resent-From:"\
48 "Resent-Sender:Resent-To:Resent-Cc:"\
49 "Resent-Message-ID:In-Reply-To:References:"\
50 "List-Id:List-Help:List-Unsubscribe:"\
51 "List-Subscribe:List-Post:List-Owner:List-Archive"
53 /* -------------------------------------------------------------------------- */
54 struct pdkim_stringlist {
59 #define PDKIM_STR_ALLOC_FRAG 256
63 unsigned int allocated;
66 /* -------------------------------------------------------------------------- */
67 /* A bunch of list constants */
68 char *pdkim_querymethods[] = {
72 char *pdkim_algos[] = {
77 char *pdkim_canons[] = {
82 char *pdkim_hashes[] = {
87 char *pdkim_keytypes[] = {
92 typedef struct pdkim_combined_canon_entry {
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 },
108 /* -------------------------------------------------------------------------- */
109 /* Print debugging functions */
111 void pdkim_quoteprint(FILE *stream, char *data, int len, int lf) {
113 unsigned char *p = (unsigned char *)data;
115 for (i=0;i<len;i++) {
118 case ' ' : fprintf(stream,"{SP}"); break;
119 case '\t': fprintf(stream,"{TB}"); break;
120 case '\r': fprintf(stream,"{CR}"); break;
121 case '\n': fprintf(stream,"{LF}"); break;
122 case '{' : fprintf(stream,"{BO}"); break;
123 case '}' : fprintf(stream,"{BC}"); break;
125 if ( (c < 32) || (c > 127) )
126 fprintf(stream,"{%02x}",c);
135 void pdkim_hexprint(FILE *stream, char *data, int len, int lf) {
137 unsigned char *p = (unsigned char *)data;
139 for (i=0;i<len;i++) {
141 fprintf(stream,"%02x",c);
149 /* -------------------------------------------------------------------------- */
150 /* Simple string list implementation for convinience */
151 pdkim_stringlist *pdkim_append_stringlist(pdkim_stringlist *base, char *str) {
152 pdkim_stringlist *new_entry = malloc(sizeof(pdkim_stringlist));
153 if (new_entry == NULL) return NULL;
154 memset(new_entry,0,sizeof(pdkim_stringlist));
155 new_entry->value = strdup(str);
156 if (new_entry->value == NULL) return NULL;
158 pdkim_stringlist *last = base;
159 while (last->next != NULL) { last = last->next; };
160 last->next = new_entry;
163 else return new_entry;
167 /* -------------------------------------------------------------------------- */
168 /* A small "growing string" implementation to escape malloc/realloc hell */
169 pdkim_str *pdkim_strnew (char *cstr) {
170 unsigned int len = cstr?strlen(cstr):0;
171 pdkim_str *p = malloc(sizeof(pdkim_str));
172 if (p == NULL) return NULL;
173 memset(p,0,sizeof(pdkim_str));
174 p->str = malloc(len+1);
175 if (p->str == NULL) {
179 p->allocated=(len+1);
181 if (cstr) strcpy(p->str,cstr);
184 char *pdkim_strncat(pdkim_str *str, char *data, int len) {
185 if ((str->allocated - str->len) < (len+1)) {
186 /* Extend the buffer */
187 int num_frags = ((len+1)/PDKIM_STR_ALLOC_FRAG)+1;
188 char *n = realloc(str->str,
189 (str->allocated+(num_frags*PDKIM_STR_ALLOC_FRAG)));
190 if (n == NULL) return NULL;
192 str->allocated += (num_frags*PDKIM_STR_ALLOC_FRAG);
194 strncpy(&(str->str[str->len]),data,len);
196 str->str[str->len] = '\0';
199 char *pdkim_strcat(pdkim_str *str, char *cstr) {
200 return pdkim_strncat(str, cstr, strlen(cstr));
202 char *pdkim_numcat(pdkim_str *str, unsigned long num) {
204 snprintf(minibuf,20,"%lu",num);
205 return pdkim_strcat(str,minibuf);
207 char *pdkim_strtrim(pdkim_str *str) {
210 while ( (*p != '\0') && ((*p == '\t') || (*p == ' ')) ) p++;
211 while (*p != '\0') {*q = *p; q++; p++;};
213 while ( (q != str->str) && ( (*q == '\0') || (*q == '\t') || (*q == ' ') ) ) {
217 str->len = strlen(str->str);
220 char *pdkim_strclear(pdkim_str *str) {
225 void pdkim_strfree(pdkim_str *str) {
226 if (str == NULL) return;
227 if (str->str != NULL) free(str->str);
233 /* -------------------------------------------------------------------------- */
234 void pdkim_free_pubkey(pdkim_pubkey *pub) {
236 if (pub->version != NULL) free(pub->version);
237 if (pub->granularity != NULL) free(pub->granularity);
238 if (pub->hashes != NULL) free(pub->hashes);
239 if (pub->keytype != NULL) free(pub->keytype);
240 if (pub->srvtype != NULL) free(pub->srvtype);
241 if (pub->notes != NULL) free(pub->notes);
242 if (pub->key != NULL) free(pub->key);
248 /* -------------------------------------------------------------------------- */
249 void pdkim_free_sig(pdkim_signature *sig) {
251 pdkim_signature *next = (pdkim_signature *)sig->next;
253 pdkim_stringlist *e = sig->headers;
255 pdkim_stringlist *c = e;
256 if (e->value != NULL) free(e->value);
261 if (sig->sigdata != NULL) free(sig->sigdata);
262 if (sig->bodyhash != NULL) free(sig->bodyhash);
263 if (sig->selector != NULL) free(sig->selector);
264 if (sig->domain != NULL) free(sig->domain);
265 if (sig->identity != NULL) free(sig->identity);
266 if (sig->headernames != NULL) free(sig->headernames);
267 if (sig->copiedheaders != NULL) free(sig->copiedheaders);
268 if (sig->rsa_privkey != NULL) free(sig->rsa_privkey);
269 if (sig->sign_headers != NULL) free(sig->sign_headers);
270 if (sig->signature_header != NULL) free(sig->signature_header);
271 if (sig->sha1_body != NULL) free(sig->sha1_body);
272 if (sig->sha2_body != NULL) free(sig->sha2_body);
274 if (sig->pubkey != NULL) pdkim_free_pubkey(sig->pubkey);
277 if (next != NULL) pdkim_free_sig(next);
282 /* -------------------------------------------------------------------------- */
283 DLLEXPORT void pdkim_free_ctx(pdkim_ctx *ctx) {
285 pdkim_free_sig(ctx->sig);
286 pdkim_strfree(ctx->cur_header);
292 /* -------------------------------------------------------------------------- */
293 /* Matches the name of the passed raw "header" against
294 the passed colon-separated "list", starting at entry
295 "start". Returns the position of the header name in
297 int header_name_match(char *header,
306 char *hcolon = strchr(header,':');
307 if (hcolon == NULL) return rc; /* This isn't a header */
308 hname = malloc((hcolon-header)+1);
309 if (hname == NULL) return PDKIM_ERR_OOM;
310 memset(hname,0,(hcolon-header)+1);
311 strncpy(hname,header,(hcolon-header));
312 lcopy = strdup(list);
315 return PDKIM_ERR_OOM;
322 if (strcasecmp(p,hname) == 0) {
332 if (strcasecmp(p,hname) == 0)
342 /* -------------------------------------------------------------------------- */
343 /* Performs "relaxed" canonicalization of a header. The returned pointer needs
345 char *pdkim_relax_header (char *header, int crlf) {
346 int past_field_name = 0;
350 char *relaxed = malloc(strlen(header));
351 if (relaxed == NULL) return NULL;
356 if ( (c == '\r') || (c == '\n') ) {
360 if ( (c == '\t') || (c == ' ') ) {
361 c = ' '; /* Turns WSP into SP */
369 if ( (!past_field_name) && (c == ':') ) {
370 if (seen_wsp) q--; /* This removes WSP before the colon */
371 seen_wsp = 1; /* This removes WSP after the colon */
376 /* Lowercase header name */
377 if (!past_field_name) c = tolower(c);
383 if (crlf) strcat(relaxed,"\r\n");
388 /* -------------------------------------------------------------------------- */
389 #define PDKIM_QP_ERROR_DECODE -1
390 char *pdkim_decode_qp_char(char *qp_p, int *c) {
391 char *initial_pos = qp_p;
393 /* Advance one char */
396 /* Check for two hex digits and decode them */
397 if (isxdigit(*qp_p) && isxdigit(qp_p[1])) {
398 /* Do hex conversion */
399 if (isdigit(*qp_p)) {*c = *qp_p - '0';}
400 else {*c = toupper(*qp_p) - 'A' + 10;};
402 if (isdigit(qp_p[1])) {*c |= qp_p[1] - '0';}
403 else {*c |= toupper(qp_p[1]) - 'A' + 10;};
407 /* Illegal char here */
408 *c = PDKIM_QP_ERROR_DECODE;
413 /* -------------------------------------------------------------------------- */
414 char *pdkim_decode_qp(char *str) {
418 char *n = malloc(strlen(p)+1);
419 if (n == NULL) return NULL;
424 p = pdkim_decode_qp_char(p,&nchar);
441 /* -------------------------------------------------------------------------- */
442 char *pdkim_decode_base64(char *str, int *num_decoded) {
446 base64_decode(NULL, &dlen, (unsigned char *)str, strlen(str));
447 res = malloc(dlen+1);
448 if (res == NULL) return NULL;
449 if (base64_decode((unsigned char *)res,&dlen,(unsigned char *)str,strlen(str)) != 0) {
453 if (num_decoded != NULL) *num_decoded = dlen;
457 /* -------------------------------------------------------------------------- */
458 char *pdkim_encode_base64(char *str, int num) {
462 base64_encode(NULL, &dlen, (unsigned char *)str, num);
463 res = malloc(dlen+1);
464 if (res == NULL) return NULL;
465 if (base64_encode((unsigned char *)res,&dlen,(unsigned char *)str,num) != 0) {
473 /* -------------------------------------------------------------------------- */
474 #define PDKIM_HDR_LIMBO 0
475 #define PDKIM_HDR_TAG 1
476 #define PDKIM_HDR_VALUE 2
477 pdkim_signature *pdkim_parse_sig_header(pdkim_ctx *ctx, char *raw_hdr) {
478 pdkim_signature *sig ;
480 pdkim_str *cur_tag = NULL;
481 pdkim_str *cur_val = NULL;
484 int where = PDKIM_HDR_LIMBO;
487 sig = malloc(sizeof(pdkim_signature));
488 if (sig == NULL) return NULL;
489 memset(sig,0,sizeof(pdkim_signature));
490 sig->bodylength = -1;
492 sig->rawsig_no_b_val = malloc(strlen(raw_hdr)+1);
493 if (sig->rawsig_no_b_val == NULL) {
499 q = sig->rawsig_no_b_val;
504 if ( (*p == '\r') || (*p == '\n') )
507 /* Fast-forward through header name */
509 if (*p == ':') past_hname = 1;
513 if (where == PDKIM_HDR_LIMBO) {
514 /* In limbo, just wait for a tag-char to appear */
515 if (!((*p >= 'a') && (*p <= 'z')))
518 where = PDKIM_HDR_TAG;
521 if (where == PDKIM_HDR_TAG) {
523 cur_tag = pdkim_strnew(NULL);
525 if ((*p >= 'a') && (*p <= 'z'))
526 pdkim_strncat(cur_tag,p,1);
529 if (strcmp(cur_tag->str,"b") == 0) {
533 where = PDKIM_HDR_VALUE;
538 if (where == PDKIM_HDR_VALUE) {
540 cur_val = pdkim_strnew(NULL);
542 if ( (*p == '\r') || (*p == '\n') || (*p == ' ') || (*p == '\t') )
545 if ( (*p == ';') || (*p == '\0') ) {
546 if (cur_tag->len > 0) {
547 pdkim_strtrim(cur_val);
549 if (ctx->debug_stream)
550 fprintf(ctx->debug_stream, "%s=%s\n", cur_tag->str, cur_val->str);
552 switch (cur_tag->str[0]) {
554 switch (cur_tag->str[1]) {
556 sig->bodyhash = pdkim_decode_base64(cur_val->str,&(sig->bodyhash_len));
559 sig->sigdata = pdkim_decode_base64(cur_val->str,&(sig->sigdata_len));
564 if (strcmp(cur_val->str,PDKIM_SIGNATURE_VERSION) == 0) {
565 /* We only support version 1, and that is currently the
566 only version there is. */
572 while (pdkim_algos[i] != NULL) {
573 if (strcmp(cur_val->str,pdkim_algos[i]) == 0 ) {
582 while (pdkim_combined_canons[i].str != NULL) {
583 if (strcmp(cur_val->str,pdkim_combined_canons[i].str) == 0 ) {
584 sig->canon_headers = pdkim_combined_canons[i].canon_headers;
585 sig->canon_body = pdkim_combined_canons[i].canon_body;
593 while (pdkim_querymethods[i] != NULL) {
594 if (strcmp(cur_val->str,pdkim_querymethods[i]) == 0 ) {
595 sig->querymethod = i;
602 sig->selector = strdup(cur_val->str);
605 sig->domain = strdup(cur_val->str);
608 sig->identity = pdkim_decode_qp(cur_val->str);
611 sig->created = strtoul(cur_val->str,NULL,10);
614 sig->expires = strtoul(cur_val->str,NULL,10);
617 sig->bodylength = strtol(cur_val->str,NULL,10);
620 sig->headernames = strdup(cur_val->str);
623 sig->copiedheaders = pdkim_decode_qp(cur_val->str);
627 if (ctx->debug_stream)
628 fprintf(ctx->debug_stream, "Unknown tag encountered\n");
633 pdkim_strclear(cur_tag);
634 pdkim_strclear(cur_val);
636 where = PDKIM_HDR_LIMBO;
639 else pdkim_strncat(cur_val,p,1);
643 if (*p == '\0') break;
652 /* Make sure the most important bits are there. */
653 if (!(sig->domain && (*(sig->domain) != '\0') &&
654 sig->selector && (*(sig->selector) != '\0') &&
655 sig->headernames && (*(sig->headernames) != '\0') &&
664 /* Chomp raw header. The final newline must not be added to the signature. */
666 while( (q > sig->rawsig_no_b_val) && ((*q == '\r') || (*q == '\n')) ) {
671 if (ctx->debug_stream) {
672 fprintf(ctx->debug_stream,
673 "PDKIM >> Raw signature w/o b= tag value >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
674 pdkim_quoteprint(ctx->debug_stream,
675 sig->rawsig_no_b_val,
676 strlen(sig->rawsig_no_b_val), 1);
677 fprintf(ctx->debug_stream,
678 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
682 sig->sha1_body = malloc(sizeof(sha1_context));
683 if (sig->sha1_body == NULL) {
687 sig->sha2_body = malloc(sizeof(sha2_context));
688 if (sig->sha2_body == NULL) {
693 sha1_starts(sig->sha1_body);
694 sha2_starts(sig->sha2_body,0);
700 /* -------------------------------------------------------------------------- */
701 pdkim_pubkey *pdkim_parse_pubkey_record(pdkim_ctx *ctx, char *raw_record) {
704 pdkim_str *cur_tag = NULL;
705 pdkim_str *cur_val = NULL;
706 int where = PDKIM_HDR_LIMBO;
708 pub = malloc(sizeof(pdkim_pubkey));
709 if (pub == NULL) return NULL;
710 memset(pub,0,sizeof(pdkim_pubkey));
717 if ( (*p == '\r') || (*p == '\n') )
720 if (where == PDKIM_HDR_LIMBO) {
721 /* In limbo, just wait for a tag-char to appear */
722 if (!((*p >= 'a') && (*p <= 'z')))
725 where = PDKIM_HDR_TAG;
728 if (where == PDKIM_HDR_TAG) {
730 cur_tag = pdkim_strnew(NULL);
732 if ((*p >= 'a') && (*p <= 'z'))
733 pdkim_strncat(cur_tag,p,1);
736 where = PDKIM_HDR_VALUE;
741 if (where == PDKIM_HDR_VALUE) {
743 cur_val = pdkim_strnew(NULL);
745 if ( (*p == '\r') || (*p == '\n') )
748 if ( (*p == ';') || (*p == '\0') ) {
749 if (cur_tag->len > 0) {
750 pdkim_strtrim(cur_val);
752 if (ctx->debug_stream)
753 fprintf(ctx->debug_stream, "%s=%s\n", cur_tag->str, cur_val->str);
755 switch (cur_tag->str[0]) {
757 /* This tag isn't evaluated because:
758 - We only support version DKIM1.
759 - Which is the default for this value (set below)
760 - Other versions are currently not specified. */
763 pub->hashes = strdup(cur_val->str);
766 pub->granularity = strdup(cur_val->str);
769 pub->notes = pdkim_decode_qp(cur_val->str);
772 pub->key = pdkim_decode_base64(cur_val->str,&(pub->key_len));
775 pub->hashes = strdup(cur_val->str);
778 pub->srvtype = strdup(cur_val->str);
781 if (strchr(cur_val->str,'t') != NULL) pub->testing = 1;
782 if (strchr(cur_val->str,'s') != NULL) pub->no_subdomaining = 1;
786 if (ctx->debug_stream)
787 fprintf(ctx->debug_stream, "Unknown tag encountered\n");
792 pdkim_strclear(cur_tag);
793 pdkim_strclear(cur_val);
794 where = PDKIM_HDR_LIMBO;
797 else pdkim_strncat(cur_val,p,1);
801 if (*p == '\0') break;
805 /* Set fallback defaults */
806 if (pub->version == NULL) pub->version = strdup(PDKIM_PUB_RECORD_VERSION);
807 if (pub->granularity == NULL) pub->granularity = strdup("*");
808 if (pub->keytype == NULL) pub->keytype = strdup("rsa");
809 if (pub->srvtype == NULL) pub->srvtype = strdup("*");
812 if (pub->key == NULL) {
813 pdkim_free_pubkey(pub);
821 /* -------------------------------------------------------------------------- */
822 int pdkim_update_bodyhash(pdkim_ctx *ctx, char *data, int len) {
823 pdkim_signature *sig = ctx->sig;
824 /* Cache relaxed version of data */
825 char *relaxed_data = NULL;
828 /* Traverse all signatures, updating their hashes. */
829 while (sig != NULL) {
830 /* Defaults to simple canon (no further treatment necessary) */
831 char *canon_data = data;
834 if (sig->canon_body == PDKIM_CANON_RELAXED) {
835 /* Relax the line if not done already */
836 if (relaxed_data == NULL) {
840 relaxed_data = malloc(len+1);
841 if (relaxed_data == NULL) return PDKIM_ERR_OOM;
844 if ( (c == '\t') || (c == ' ') ) {
845 c = ' '; /* Turns WSP into SP */
853 relaxed_data[q++] = c;
856 relaxed_data[q] = '\0';
859 canon_data = relaxed_data;
860 canon_len = relaxed_len;
863 /* Make sure we don't exceed the to-be-signed body length */
864 if ((sig->bodylength >= 0) &&
865 ((sig->signed_body_bytes+(unsigned long)canon_len) > sig->bodylength))
866 canon_len = (sig->bodylength - sig->signed_body_bytes);
869 if (sig->algo == PDKIM_ALGO_RSA_SHA1)
870 sha1_update(sig->sha1_body,(unsigned char *)canon_data,canon_len);
872 sha2_update(sig->sha2_body,(unsigned char *)canon_data,canon_len);
873 sig->signed_body_bytes += canon_len;
875 if (ctx->debug_stream!=NULL)
876 pdkim_quoteprint(ctx->debug_stream,canon_data,canon_len,0);
883 if (relaxed_data != NULL) free(relaxed_data);
888 /* -------------------------------------------------------------------------- */
889 int pdkim_finish_bodyhash(pdkim_ctx *ctx) {
890 pdkim_signature *sig = ctx->sig;
892 /* Traverse all signatures */
893 while (sig != NULL) {
896 unsigned char bh[32]; /* SHA-256 = 32 Bytes, SHA-1 = 20 Bytes */
897 if (sig->algo == PDKIM_ALGO_RSA_SHA1)
898 sha1_finish(sig->sha1_body,bh);
900 sha2_finish(sig->sha2_body,bh);
903 if (ctx->debug_stream) {
904 fprintf(ctx->debug_stream, "PDKIM [%s] Body bytes hashed: %lu\n",
905 sig->domain, sig->signed_body_bytes);
906 fprintf(ctx->debug_stream, "PDKIM [%s] bh computed: ", sig->domain);
907 pdkim_hexprint(ctx->debug_stream, (char *)bh,
908 (sig->algo == PDKIM_ALGO_RSA_SHA1)?20:32,1);
912 /* SIGNING -------------------------------------------------------------- */
913 if (ctx->mode == PDKIM_MODE_SIGN) {
914 sig->bodyhash_len = (sig->algo == PDKIM_ALGO_RSA_SHA1)?20:32;
915 sig->bodyhash = malloc(sig->bodyhash_len);
916 if (sig->bodyhash == NULL) return PDKIM_ERR_OOM;
917 memcpy(sig->bodyhash,bh,sig->bodyhash_len);
919 /* If bodylength limit is set, and we have received less bytes
920 than the requested amount, effectively remove the limit tag. */
921 if (sig->signed_body_bytes < sig->bodylength) sig->bodylength = -1;
923 /* VERIFICATION --------------------------------------------------------- */
925 /* Compare bodyhash */
926 if (memcmp(bh,sig->bodyhash,
927 (sig->algo == PDKIM_ALGO_RSA_SHA1)?20:32) == 0) {
929 if (ctx->debug_stream)
930 fprintf(ctx->debug_stream, "PDKIM [%s] Body hash verified OK\n",
936 if (ctx->debug_stream) {
937 fprintf(ctx->debug_stream, "PDKIM [%s] Body hash did NOT verify\n",
939 fprintf(ctx->debug_stream, "PDKIM [%s] bh signature: ", sig->domain);
940 pdkim_hexprint(ctx->debug_stream, sig->bodyhash,
941 (sig->algo == PDKIM_ALGO_RSA_SHA1)?20:32,1);
944 sig->verify_status = PDKIM_VERIFY_FAIL;
945 sig->verify_ext_status = PDKIM_VERIFY_FAIL_BODY;
957 /* -------------------------------------------------------------------------- */
958 /* Callback from pdkim_feed below for processing complete body lines */
959 int pdkim_bodyline_complete(pdkim_ctx *ctx) {
960 char *p = ctx->linebuf;
961 int n = ctx->linebuf_offset;
963 /* Ignore extra data if we've seen the end-of-data marker */
964 if (ctx->seen_eod) goto BAIL;
966 /* We've always got one extra byte to stuff a zero ... */
967 ctx->linebuf[(ctx->linebuf_offset)] = '\0';
969 if (ctx->input_mode == PDKIM_INPUT_SMTP) {
970 /* Terminate on EOD marker */
971 if (memcmp(p,".\r\n",3) == 0) {
976 if (memcmp(p,"..",2) == 0) {
982 /* Empty lines need to be buffered until we find a non-empty line */
983 if (memcmp(p,"\r\n",2) == 0) {
984 ctx->num_buffered_crlf++;
988 /* At this point, we have a non-empty line, so release the buffered ones. */
989 while (ctx->num_buffered_crlf) {
990 pdkim_update_bodyhash(ctx,"\r\n",2);
991 ctx->num_buffered_crlf--;
994 pdkim_update_bodyhash(ctx,p,n);
997 ctx->linebuf_offset = 0;
1002 /* -------------------------------------------------------------------------- */
1003 /* Callback from pdkim_feed below for processing complete headers */
1004 #define DKIM_SIGNATURE_HEADERNAME "DKIM-Signature:"
1005 int pdkim_header_complete(pdkim_ctx *ctx) {
1006 pdkim_signature *sig = ctx->sig;
1008 /* Special case: The last header can have an extra \r appended */
1009 if ( (ctx->cur_header->len > 1) &&
1010 (ctx->cur_header->str[(ctx->cur_header->len)-1] == '\r') ) {
1011 ctx->cur_header->str[(ctx->cur_header->len)-1] = '\0';
1012 ctx->cur_header->len--;
1016 if (ctx->num_headers > PDKIM_MAX_HEADERS) goto BAIL;
1018 /* Traverse all signatures */
1019 while (sig != NULL) {
1020 pdkim_stringlist *list;
1022 /* SIGNING -------------------------------------------------------------- */
1023 if (ctx->mode == PDKIM_MODE_SIGN) {
1024 if (header_name_match(ctx->cur_header->str,
1027 PDKIM_DEFAULT_SIGN_HEADERS, 0) < 0) goto NEXT_SIG;
1029 /* VERIFICATION --------------------------------------------------------- */
1031 int rc = header_name_match(ctx->cur_header->str,
1033 sig->headernames_pos);
1034 /* Header is not included or out-of-sequence */
1035 if (rc < 0) goto NEXT_SIG;
1036 sig->headernames_pos = rc;
1039 /* Add header to the signed headers list */
1040 list = pdkim_append_stringlist(sig->headers,
1041 ctx->cur_header->str);
1042 if (list == NULL) return PDKIM_ERR_OOM;
1043 sig->headers = list;
1049 /* DKIM-Signature: headers are added to the verification list */
1050 if ( (ctx->mode == PDKIM_MODE_VERIFY) &&
1051 (strncasecmp(ctx->cur_header->str,
1052 DKIM_SIGNATURE_HEADERNAME,
1053 strlen(DKIM_SIGNATURE_HEADERNAME)) == 0) ) {
1054 pdkim_signature *new_sig;
1055 /* Create and chain new signature block */
1057 if (ctx->debug_stream)
1058 fprintf(ctx->debug_stream,
1059 "PDKIM >> Found sig, trying to parse >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
1061 new_sig = pdkim_parse_sig_header(ctx, ctx->cur_header->str);
1062 if (new_sig != NULL) {
1063 pdkim_signature *last_sig = ctx->sig;
1064 if (last_sig == NULL) {
1068 while (last_sig->next != NULL) { last_sig = last_sig->next; };
1069 last_sig->next = new_sig;
1074 if (ctx->debug_stream) {
1075 fprintf(ctx->debug_stream,"Error while parsing signature header\n");
1076 fprintf(ctx->debug_stream,
1077 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1084 pdkim_strclear(ctx->cur_header); /* Re-use existing pdkim_str */
1090 /* -------------------------------------------------------------------------- */
1091 #define HEADER_BUFFER_FRAG_SIZE 256
1092 DLLEXPORT int pdkim_feed (pdkim_ctx *ctx,
1096 for (p=0;p<len;p++) {
1098 if (ctx->past_headers) {
1099 /* Processing body byte */
1100 ctx->linebuf[(ctx->linebuf_offset)++] = c;
1102 int rc = pdkim_bodyline_complete(ctx); /* End of line */
1103 if (rc != PDKIM_OK) return rc;
1105 if (ctx->linebuf_offset == (PDKIM_MAX_BODY_LINE_LEN-1))
1106 return PDKIM_ERR_LONG_LINE;
1109 /* Processing header byte */
1113 int rc = pdkim_header_complete(ctx); /* Seen last header line */
1114 if (rc != PDKIM_OK) return rc;
1115 ctx->past_headers = 1;
1118 if (ctx->debug_stream)
1119 fprintf(ctx->debug_stream,
1120 "PDKIM >> Hashed body data, canonicalized >>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
1124 else ctx->seen_lf = 1;
1126 else if (ctx->seen_lf) {
1127 if (! ((c == '\t') || (c == ' '))) {
1128 int rc = pdkim_header_complete(ctx); /* End of header */
1129 if (rc != PDKIM_OK) return rc;
1134 if (ctx->cur_header == NULL) {
1135 ctx->cur_header = pdkim_strnew(NULL);
1136 if (ctx->cur_header == NULL) return PDKIM_ERR_OOM;
1138 if (ctx->cur_header->len < PDKIM_MAX_HEADER_LEN)
1139 if (pdkim_strncat(ctx->cur_header,&data[p],1) == NULL)
1140 return PDKIM_ERR_OOM;
1147 /* -------------------------------------------------------------------------- */
1148 char *pdkim_create_header(pdkim_signature *sig, int final) {
1150 char *base64_bh = NULL;
1151 char *base64_b = NULL;
1152 pdkim_str *hdr = pdkim_strnew("DKIM-Signature: v="PDKIM_SIGNATURE_VERSION);
1153 if (hdr == NULL) return NULL;
1155 base64_bh = pdkim_encode_base64(sig->bodyhash, sig->bodyhash_len);
1156 if (base64_bh == NULL) goto BAIL;
1158 /* Required and static bits */
1160 pdkim_strcat(hdr,"; a=") &&
1161 pdkim_strcat(hdr,pdkim_algos[sig->algo]) &&
1162 pdkim_strcat(hdr,"; q=") &&
1163 pdkim_strcat(hdr,pdkim_querymethods[sig->querymethod]) &&
1164 pdkim_strcat(hdr,"; c=") &&
1165 pdkim_strcat(hdr,pdkim_canons[sig->canon_headers]) &&
1166 pdkim_strcat(hdr,"/") &&
1167 pdkim_strcat(hdr,pdkim_canons[sig->canon_body]) &&
1168 pdkim_strcat(hdr,"; d=") &&
1169 pdkim_strcat(hdr,sig->domain) &&
1170 pdkim_strcat(hdr,"; s=") &&
1171 pdkim_strcat(hdr,sig->selector) &&
1172 pdkim_strcat(hdr,";\r\n\th=") &&
1173 pdkim_strcat(hdr,sig->headernames) &&
1174 pdkim_strcat(hdr,"; bh=") &&
1175 pdkim_strcat(hdr,base64_bh) &&
1176 pdkim_strcat(hdr,";\r\n\t")
1179 if (sig->identity != NULL) {
1180 if (!( pdkim_strcat(hdr,"i=") &&
1181 pdkim_strcat(hdr,sig->identity) &&
1182 pdkim_strcat(hdr,";") ) ) {
1186 if (sig->created > 0) {
1187 if (!( pdkim_strcat(hdr,"t=") &&
1188 pdkim_numcat(hdr,sig->created) &&
1189 pdkim_strcat(hdr,";") ) ) {
1193 if (sig->expires > 0) {
1194 if (!( pdkim_strcat(hdr,"x=") &&
1195 pdkim_numcat(hdr,sig->expires) &&
1196 pdkim_strcat(hdr,";") ) ) {
1200 if (sig->bodylength >= 0) {
1201 if (!( pdkim_strcat(hdr,"l=") &&
1202 pdkim_numcat(hdr,sig->bodylength) &&
1203 pdkim_strcat(hdr,";") ) ) {
1207 /* Extra linebreak */
1208 if (hdr->str[(hdr->len)-1] == ';') {
1209 if (!pdkim_strcat(hdr," \r\n\t")) goto BAIL;
1211 /* Preliminary or final version? */
1213 base64_b = pdkim_encode_base64(sig->sigdata, sig->sigdata_len);
1214 if (base64_b == NULL) goto BAIL;
1216 pdkim_strcat(hdr,"b=") &&
1217 pdkim_strcat(hdr,base64_b) &&
1218 pdkim_strcat(hdr,";")
1222 if (pdkim_strcat(hdr,"b=;")) goto DONE;
1229 rc = strdup(hdr->str);
1233 if (base64_bh != NULL) free(base64_bh);
1234 if (base64_b != NULL) free(base64_b);
1239 /* -------------------------------------------------------------------------- */
1240 DLLEXPORT int pdkim_feed_finish(pdkim_ctx *ctx, pdkim_signature **return_signatures) {
1241 pdkim_signature *sig = ctx->sig;
1242 pdkim_str *headernames = NULL; /* Collected signed header names */
1244 /* Check if we must still flush a (partial) header. If that is the
1245 case, the message has no body, and we must compute a body hash
1246 out of '<CR><LF>' */
1247 if (ctx->cur_header->len) {
1248 int rc = pdkim_header_complete(ctx);
1249 if (rc != PDKIM_OK) return rc;
1250 pdkim_update_bodyhash(ctx,"\r\n",2);
1253 /* For non-smtp input, check if there's an unfinished line in the
1254 body line buffer. If that is the case, we must add a CRLF to the
1255 hash to properly terminate the message. */
1256 if ((ctx->input_mode == PDKIM_INPUT_NORMAL) && ctx->linebuf_offset) {
1257 pdkim_update_bodyhash(ctx, ctx->linebuf, ctx->linebuf_offset);
1258 pdkim_update_bodyhash(ctx,"\r\n",2);
1261 if (ctx->debug_stream)
1262 fprintf(ctx->debug_stream,
1263 "\nPDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1267 /* Build (and/or evaluate) body hash */
1268 if (pdkim_finish_bodyhash(ctx) != PDKIM_OK) return PDKIM_ERR_OOM;
1270 /* SIGNING -------------------------------------------------------------- */
1271 if (ctx->mode == PDKIM_MODE_SIGN) {
1272 headernames = pdkim_strnew(NULL);
1273 if (headernames == NULL) return PDKIM_ERR_OOM;
1275 /* ---------------------------------------------------------------------- */
1277 while (sig != NULL) {
1278 sha1_context sha1_headers;
1279 sha2_context sha2_headers;
1280 pdkim_stringlist *p = sig->headers;
1282 char headerhash[32];
1284 if (sig->algo == PDKIM_ALGO_RSA_SHA1)
1285 sha1_starts(&sha1_headers);
1287 sha2_starts(&sha2_headers,0);
1290 if (ctx->debug_stream)
1291 fprintf(ctx->debug_stream,
1292 "PDKIM >> Hashed header data, canonicalized, in sequence >>>>>>>>>>>>>>\n");
1298 /* SIGNING -------------------------------------------------------------- */
1299 if (ctx->mode == PDKIM_MODE_SIGN) {
1300 /* Collect header names (Note: colon presence is guaranteed here) */
1301 char *q = strchr(p->value,':');
1302 if (pdkim_strncat(headernames, p->value,
1303 (q-(p->value))+((p->next==NULL)?0:1)) == NULL)
1304 return PDKIM_ERR_OOM;
1306 /* ---------------------------------------------------------------------- */
1308 if (sig->canon_body == PDKIM_CANON_RELAXED)
1309 rh = pdkim_relax_header(p->value,1); /* cook header for relaxed canon */
1311 rh = strdup(p->value); /* just copy it for simple canon */
1313 if (rh == NULL) return PDKIM_ERR_OOM;
1315 /* Feed header to the hash algorithm */
1316 if (sig->algo == PDKIM_ALGO_RSA_SHA1)
1317 sha1_update(&(sha1_headers),(unsigned char *)rh,strlen(rh));
1319 sha2_update(&(sha2_headers),(unsigned char *)rh,strlen(rh));
1321 if (ctx->debug_stream)
1322 pdkim_quoteprint(ctx->debug_stream, rh, strlen(rh), 1);
1329 if (ctx->debug_stream)
1330 fprintf(ctx->debug_stream,
1331 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1335 /* SIGNING ---------------------------------------------------------------- */
1336 if (ctx->mode == PDKIM_MODE_SIGN) {
1337 /* Copy headernames to signature struct */
1338 sig->headernames = strdup(headernames->str);
1339 pdkim_strfree(headernames);
1341 /* Create signature header with b= omitted */
1342 sig_hdr = pdkim_create_header(ctx->sig,0);
1344 /* VERIFICATION ----------------------------------------------------------- */
1346 sig_hdr = strdup(sig->rawsig_no_b_val);
1348 /* ------------------------------------------------------------------------ */
1350 if (sig_hdr == NULL) return PDKIM_ERR_OOM;
1352 /* Relax header if necessary */
1353 if (sig->canon_headers == PDKIM_CANON_RELAXED) {
1354 char *relaxed_hdr = pdkim_relax_header(sig_hdr,0);
1356 if (relaxed_hdr == NULL) return PDKIM_ERR_OOM;
1357 sig_hdr = relaxed_hdr;
1361 if (ctx->debug_stream) {
1362 fprintf(ctx->debug_stream,
1363 "PDKIM >> Signed DKIM-Signature header, canonicalized >>>>>>>>>>>>>>>>>\n");
1364 pdkim_quoteprint(ctx->debug_stream, sig_hdr, strlen(sig_hdr), 1);
1365 fprintf(ctx->debug_stream,
1366 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1370 /* Finalize header hash */
1371 if (sig->algo == PDKIM_ALGO_RSA_SHA1) {
1372 sha1_update(&(sha1_headers),(unsigned char *)sig_hdr,strlen(sig_hdr));
1373 sha1_finish(&(sha1_headers),(unsigned char *)headerhash);
1374 fprintf(ctx->debug_stream, "PDKIM [%s] hh computed: ", sig->domain);
1375 pdkim_hexprint(ctx->debug_stream, headerhash, 20, 1);
1378 sha2_update(&(sha2_headers),(unsigned char *)sig_hdr,strlen(sig_hdr));
1379 sha2_finish(&(sha2_headers),(unsigned char *)headerhash);
1380 fprintf(ctx->debug_stream, "PDKIM [%s] hh computed: ", sig->domain);
1381 pdkim_hexprint(ctx->debug_stream, headerhash, 32, 1);
1386 /* SIGNING ---------------------------------------------------------------- */
1387 if (ctx->mode == PDKIM_MODE_SIGN) {
1390 rsa_init(&rsa,RSA_PKCS_V15,0,NULL,NULL);
1392 /* Perform private key operation */
1393 if (rsa_parse_key(&rsa, (unsigned char *)sig->rsa_privkey,
1394 strlen(sig->rsa_privkey), NULL, 0) != 0) {
1395 return PDKIM_ERR_RSA_PRIVKEY;
1398 sig->sigdata_len = mpi_size(&(rsa.N));
1399 sig->sigdata = malloc(sig->sigdata_len);
1400 if (sig->sigdata == NULL) return PDKIM_ERR_OOM;
1402 if (rsa_pkcs1_sign( &rsa, RSA_PRIVATE,
1403 ((sig->algo == PDKIM_ALGO_RSA_SHA1)?
1404 RSA_SHA1:RSA_SHA256),
1406 (unsigned char *)headerhash,
1407 (unsigned char *)sig->sigdata ) != 0) {
1408 return PDKIM_ERR_RSA_SIGNING;
1414 if (ctx->debug_stream) {
1415 fprintf(ctx->debug_stream, "PDKIM [%s] b computed: ",
1417 pdkim_hexprint(ctx->debug_stream, sig->sigdata, sig->sigdata_len, 1);
1421 sig->signature_header = pdkim_create_header(ctx->sig,1);
1422 if (sig->signature_header == NULL) return PDKIM_ERR_OOM;
1424 /* VERIFICATION ----------------------------------------------------------- */
1427 char *dns_txt_name, *dns_txt_reply;
1429 rsa_init(&rsa,RSA_PKCS_V15,0,NULL,NULL);
1431 dns_txt_name = malloc(PDKIM_DNS_TXT_MAX_NAMELEN);
1432 if (dns_txt_name == NULL) return PDKIM_ERR_OOM;
1433 dns_txt_reply = malloc(PDKIM_DNS_TXT_MAX_RECLEN);
1434 if (dns_txt_reply == NULL) {
1436 return PDKIM_ERR_OOM;
1438 memset(dns_txt_reply,0,PDKIM_DNS_TXT_MAX_RECLEN);
1439 memset(dns_txt_name ,0,PDKIM_DNS_TXT_MAX_NAMELEN);
1441 if (snprintf(dns_txt_name,PDKIM_DNS_TXT_MAX_NAMELEN,
1442 "%s._domainkey.%s.",
1443 sig->selector,sig->domain) >= PDKIM_DNS_TXT_MAX_NAMELEN) {
1444 sig->verify_status = PDKIM_VERIFY_INVALID;
1445 sig->verify_ext_status = PDKIM_VERIFY_INVALID_BUFFER_SIZE;
1449 if ((ctx->dns_txt_callback(dns_txt_name, dns_txt_reply) != PDKIM_OK) ||
1450 (dns_txt_reply[0] == '\0')) {
1451 sig->verify_status = PDKIM_VERIFY_INVALID;
1452 sig->verify_ext_status = PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE;
1457 if (ctx->debug_stream) {
1458 fprintf(ctx->debug_stream,
1459 "PDKIM >> Parsing public key record >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
1460 fprintf(ctx->debug_stream,"Raw record: ");
1461 pdkim_quoteprint(ctx->debug_stream, dns_txt_reply, strlen(dns_txt_reply), 1);
1465 sig->pubkey = pdkim_parse_pubkey_record(ctx,dns_txt_reply);
1466 if (sig->pubkey == NULL) {
1467 sig->verify_status = PDKIM_VERIFY_INVALID;
1468 sig->verify_ext_status = PDKIM_VERIFY_INVALID_PUBKEY_PARSING;
1470 if (ctx->debug_stream) {
1471 fprintf(ctx->debug_stream,"Error while parsing public key record\n");
1472 fprintf(ctx->debug_stream,
1473 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1480 if (ctx->debug_stream) {
1481 fprintf(ctx->debug_stream,
1482 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1486 if (rsa_parse_public_key(&rsa,
1487 (unsigned char *)sig->pubkey->key,
1488 sig->pubkey->key_len) != 0) {
1489 sig->verify_status = PDKIM_VERIFY_INVALID;
1490 sig->verify_ext_status = PDKIM_VERIFY_INVALID_PUBKEY_PARSING;
1494 /* Check the signature */
1495 if (rsa_pkcs1_verify(&rsa,
1497 ((sig->algo == PDKIM_ALGO_RSA_SHA1)?
1498 RSA_SHA1:RSA_SHA256),
1500 (unsigned char *)headerhash,
1501 (unsigned char *)sig->sigdata) != 0) {
1502 sig->verify_status = PDKIM_VERIFY_FAIL;
1503 sig->verify_ext_status = PDKIM_VERIFY_FAIL_MESSAGE;
1505 if (ctx->debug_stream) {
1506 fprintf(ctx->debug_stream, "PDKIM [%s] signature did NOT verify OK\n",
1513 /* We have a winner! */
1514 sig->verify_status = PDKIM_VERIFY_PASS;
1517 if (ctx->debug_stream) {
1518 fprintf(ctx->debug_stream, "PDKIM [%s] signature verified OK\n",
1526 free(dns_txt_reply);
1532 /* If requested, set return pointer to signature(s) */
1533 if (return_signatures != NULL) {
1534 *return_signatures = ctx->sig;
1541 /* -------------------------------------------------------------------------- */
1542 DLLEXPORT pdkim_ctx *pdkim_init_verify(int input_mode,
1543 int(*dns_txt_callback)(char *, char *)
1545 pdkim_ctx *ctx = malloc(sizeof(pdkim_ctx));
1546 if (ctx == NULL) return NULL;
1547 memset(ctx,0,sizeof(pdkim_ctx));
1549 ctx->linebuf = malloc(PDKIM_MAX_BODY_LINE_LEN);
1550 if (ctx->linebuf == NULL) {
1555 ctx->mode = PDKIM_MODE_VERIFY;
1556 ctx->input_mode = input_mode;
1557 ctx->dns_txt_callback = dns_txt_callback;
1563 /* -------------------------------------------------------------------------- */
1564 DLLEXPORT pdkim_ctx *pdkim_init_sign(int input_mode,
1567 char *rsa_privkey) {
1569 pdkim_signature *sig;
1571 if (!domain || !selector || !rsa_privkey) return NULL;
1573 ctx = malloc(sizeof(pdkim_ctx));
1574 if (ctx == NULL) return NULL;
1575 memset(ctx,0,sizeof(pdkim_ctx));
1577 ctx->linebuf = malloc(PDKIM_MAX_BODY_LINE_LEN);
1578 if (ctx->linebuf == NULL) {
1583 sig = malloc(sizeof(pdkim_signature));
1589 memset(sig,0,sizeof(pdkim_signature));
1590 sig->bodylength = -1;
1592 ctx->mode = PDKIM_MODE_SIGN;
1593 ctx->input_mode = input_mode;
1596 ctx->sig->domain = strdup(domain);
1597 ctx->sig->selector = strdup(selector);
1598 ctx->sig->rsa_privkey = strdup(rsa_privkey);
1600 if (!ctx->sig->domain || !ctx->sig->selector || !ctx->sig->rsa_privkey) {
1601 pdkim_free_ctx(ctx);
1605 ctx->sig->sha1_body = malloc(sizeof(sha1_context));
1606 if (ctx->sig->sha1_body == NULL) {
1607 pdkim_free_ctx(ctx);
1610 sha1_starts(ctx->sig->sha1_body);
1612 ctx->sig->sha2_body = malloc(sizeof(sha2_context));
1613 if (ctx->sig->sha2_body == NULL) {
1614 pdkim_free_ctx(ctx);
1617 sha2_starts(ctx->sig->sha2_body,0);
1623 /* -------------------------------------------------------------------------- */
1624 DLLEXPORT void pdkim_set_debug_stream(pdkim_ctx *ctx,
1625 FILE *debug_stream) {
1626 ctx->debug_stream = debug_stream;
1630 /* -------------------------------------------------------------------------- */
1631 DLLEXPORT int pdkim_set_optional(pdkim_ctx *ctx,
1638 unsigned long created,
1639 unsigned long expires) {
1641 if (identity != NULL) {
1642 ctx->sig->identity = strdup(identity);
1643 if (ctx->sig->identity == NULL) return PDKIM_ERR_OOM;
1646 if (sign_headers != NULL) {
1647 ctx->sig->sign_headers = strdup(sign_headers);
1648 if (ctx->sig->sign_headers == NULL) return PDKIM_ERR_OOM;
1651 ctx->sig->canon_headers = canon_headers;
1652 ctx->sig->canon_body = canon_body;
1653 ctx->sig->bodylength = bodylength;
1654 ctx->sig->algo = algo;
1655 ctx->sig->created = created;
1656 ctx->sig->expires = expires;