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.7 2009/11/09 14:19:48 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 16384
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;
165 pdkim_stringlist *pdkim_prepend_stringlist(pdkim_stringlist *base, char *str) {
166 pdkim_stringlist *new_entry = malloc(sizeof(pdkim_stringlist));
167 if (new_entry == NULL) return NULL;
168 memset(new_entry,0,sizeof(pdkim_stringlist));
169 new_entry->value = strdup(str);
170 if (new_entry->value == NULL) return NULL;
172 new_entry->next = base;
178 /* -------------------------------------------------------------------------- */
179 /* A small "growing string" implementation to escape malloc/realloc hell */
180 pdkim_str *pdkim_strnew (char *cstr) {
181 unsigned int len = cstr?strlen(cstr):0;
182 pdkim_str *p = malloc(sizeof(pdkim_str));
183 if (p == NULL) return NULL;
184 memset(p,0,sizeof(pdkim_str));
185 p->str = malloc(len+1);
186 if (p->str == NULL) {
190 p->allocated=(len+1);
192 if (cstr) strcpy(p->str,cstr);
195 char *pdkim_strncat(pdkim_str *str, char *data, int len) {
196 if ((str->allocated - str->len) < (len+1)) {
197 /* Extend the buffer */
198 int num_frags = ((len+1)/PDKIM_STR_ALLOC_FRAG)+1;
199 char *n = realloc(str->str,
200 (str->allocated+(num_frags*PDKIM_STR_ALLOC_FRAG)));
201 if (n == NULL) return NULL;
203 str->allocated += (num_frags*PDKIM_STR_ALLOC_FRAG);
205 strncpy(&(str->str[str->len]),data,len);
207 str->str[str->len] = '\0';
210 char *pdkim_strcat(pdkim_str *str, char *cstr) {
211 return pdkim_strncat(str, cstr, strlen(cstr));
213 char *pdkim_numcat(pdkim_str *str, unsigned long num) {
215 snprintf(minibuf,20,"%lu",num);
216 return pdkim_strcat(str,minibuf);
218 char *pdkim_strtrim(pdkim_str *str) {
221 while ( (*p != '\0') && ((*p == '\t') || (*p == ' ')) ) p++;
222 while (*p != '\0') {*q = *p; q++; p++;}
224 while ( (q != str->str) && ( (*q == '\0') || (*q == '\t') || (*q == ' ') ) ) {
228 str->len = strlen(str->str);
231 char *pdkim_strclear(pdkim_str *str) {
236 void pdkim_strfree(pdkim_str *str) {
237 if (str == NULL) return;
238 if (str->str != NULL) free(str->str);
244 /* -------------------------------------------------------------------------- */
245 void pdkim_free_pubkey(pdkim_pubkey *pub) {
247 if (pub->version != NULL) free(pub->version);
248 if (pub->granularity != NULL) free(pub->granularity);
249 if (pub->hashes != NULL) free(pub->hashes);
250 if (pub->keytype != NULL) free(pub->keytype);
251 if (pub->srvtype != NULL) free(pub->srvtype);
252 if (pub->notes != NULL) free(pub->notes);
253 if (pub->key != NULL) free(pub->key);
259 /* -------------------------------------------------------------------------- */
260 void pdkim_free_sig(pdkim_signature *sig) {
262 pdkim_signature *next = (pdkim_signature *)sig->next;
264 pdkim_stringlist *e = sig->headers;
266 pdkim_stringlist *c = e;
267 if (e->value != NULL) free(e->value);
272 if (sig->sigdata != NULL) free(sig->sigdata);
273 if (sig->bodyhash != NULL) free(sig->bodyhash);
274 if (sig->selector != NULL) free(sig->selector);
275 if (sig->domain != NULL) free(sig->domain);
276 if (sig->identity != NULL) free(sig->identity);
277 if (sig->headernames != NULL) free(sig->headernames);
278 if (sig->copiedheaders != NULL) free(sig->copiedheaders);
279 if (sig->rsa_privkey != NULL) free(sig->rsa_privkey);
280 if (sig->sign_headers != NULL) free(sig->sign_headers);
281 if (sig->signature_header != NULL) free(sig->signature_header);
282 if (sig->sha1_body != NULL) free(sig->sha1_body);
283 if (sig->sha2_body != NULL) free(sig->sha2_body);
284 if (sig->hnames_check != NULL) free(sig->hnames_check);
286 if (sig->pubkey != NULL) pdkim_free_pubkey(sig->pubkey);
289 if (next != NULL) pdkim_free_sig(next);
294 /* -------------------------------------------------------------------------- */
295 DLLEXPORT void pdkim_free_ctx(pdkim_ctx *ctx) {
297 pdkim_free_sig(ctx->sig);
298 pdkim_strfree(ctx->cur_header);
304 /* -------------------------------------------------------------------------- */
305 /* Matches the name of the passed raw "header" against
306 the passed colon-separated "list", starting at entry
307 "start". Returns the position of the header name in
309 int header_name_match(char *header,
318 /* Get header name */
319 char *hcolon = strchr(header,':');
320 if (hcolon == NULL) return rc; /* This isn't a header */
321 hname = malloc((hcolon-header)+1);
322 if (hname == NULL) return PDKIM_ERR_OOM;
323 memset(hname,0,(hcolon-header)+1);
324 strncpy(hname,header,(hcolon-header));
326 /* Copy tick-off list locally, so we can punch zeroes into it */
327 lcopy = strdup(tick);
330 return PDKIM_ERR_OOM;
337 if (strcasecmp(p,hname) == 0) {
339 /* Invalidate header name instance in tick-off list */
340 if (do_tick) tick[p-lcopy] = '_';
348 if (strcasecmp(p,hname) == 0) {
350 /* Invalidate header name instance in tick-off list */
351 if (do_tick) tick[p-lcopy] = '_';
361 /* -------------------------------------------------------------------------- */
362 /* Performs "relaxed" canonicalization of a header. The returned pointer needs
364 char *pdkim_relax_header (char *header, int crlf) {
365 int past_field_name = 0;
369 char *relaxed = malloc(strlen(header)+3);
370 if (relaxed == NULL) return NULL;
375 if ( (c == '\r') || (c == '\n') ) {
379 if ( (c == '\t') || (c == ' ') ) {
380 c = ' '; /* Turns WSP into SP */
388 if ( (!past_field_name) && (c == ':') ) {
389 if (seen_wsp) q--; /* This removes WSP before the colon */
390 seen_wsp = 1; /* This removes WSP after the colon */
395 /* Lowercase header name */
396 if (!past_field_name) c = tolower(c);
402 if (crlf) strcat(relaxed,"\r\n");
407 /* -------------------------------------------------------------------------- */
408 #define PDKIM_QP_ERROR_DECODE -1
409 char *pdkim_decode_qp_char(char *qp_p, int *c) {
410 char *initial_pos = qp_p;
412 /* Advance one char */
415 /* Check for two hex digits and decode them */
416 if (isxdigit(*qp_p) && isxdigit(qp_p[1])) {
417 /* Do hex conversion */
418 if (isdigit(*qp_p)) {*c = *qp_p - '0';}
419 else {*c = toupper(*qp_p) - 'A' + 10;}
421 if (isdigit(qp_p[1])) {*c |= qp_p[1] - '0';}
422 else {*c |= toupper(qp_p[1]) - 'A' + 10;}
426 /* Illegal char here */
427 *c = PDKIM_QP_ERROR_DECODE;
432 /* -------------------------------------------------------------------------- */
433 char *pdkim_decode_qp(char *str) {
437 char *n = malloc(strlen(p)+1);
438 if (n == NULL) return NULL;
443 p = pdkim_decode_qp_char(p,&nchar);
461 /* -------------------------------------------------------------------------- */
462 char *pdkim_decode_base64(char *str, int *num_decoded) {
466 base64_decode(NULL, &dlen, (unsigned char *)str, strlen(str));
467 res = malloc(dlen+1);
468 if (res == NULL) return NULL;
469 if (base64_decode((unsigned char *)res,&dlen,(unsigned char *)str,strlen(str)) != 0) {
473 if (num_decoded != NULL) *num_decoded = dlen;
477 /* -------------------------------------------------------------------------- */
478 char *pdkim_encode_base64(char *str, int num) {
482 base64_encode(NULL, &dlen, (unsigned char *)str, num);
483 res = malloc(dlen+1);
484 if (res == NULL) return NULL;
485 if (base64_encode((unsigned char *)res,&dlen,(unsigned char *)str,num) != 0) {
493 /* -------------------------------------------------------------------------- */
494 #define PDKIM_HDR_LIMBO 0
495 #define PDKIM_HDR_TAG 1
496 #define PDKIM_HDR_VALUE 2
497 pdkim_signature *pdkim_parse_sig_header(pdkim_ctx *ctx, char *raw_hdr) {
498 pdkim_signature *sig ;
500 pdkim_str *cur_tag = NULL;
501 pdkim_str *cur_val = NULL;
504 int where = PDKIM_HDR_LIMBO;
507 sig = malloc(sizeof(pdkim_signature));
508 if (sig == NULL) return NULL;
509 memset(sig,0,sizeof(pdkim_signature));
510 sig->bodylength = -1;
512 sig->rawsig_no_b_val = malloc(strlen(raw_hdr)+1);
513 if (sig->rawsig_no_b_val == NULL) {
519 q = sig->rawsig_no_b_val;
524 if ( (*p == '\r') || (*p == '\n') )
527 /* Fast-forward through header name */
529 if (*p == ':') past_hname = 1;
533 if (where == PDKIM_HDR_LIMBO) {
534 /* In limbo, just wait for a tag-char to appear */
535 if (!((*p >= 'a') && (*p <= 'z')))
538 where = PDKIM_HDR_TAG;
541 if (where == PDKIM_HDR_TAG) {
543 cur_tag = pdkim_strnew(NULL);
545 if ((*p >= 'a') && (*p <= 'z'))
546 pdkim_strncat(cur_tag,p,1);
549 if (strcmp(cur_tag->str,"b") == 0) {
553 where = PDKIM_HDR_VALUE;
558 if (where == PDKIM_HDR_VALUE) {
560 cur_val = pdkim_strnew(NULL);
562 if ( (*p == '\r') || (*p == '\n') || (*p == ' ') || (*p == '\t') )
565 if ( (*p == ';') || (*p == '\0') ) {
566 if (cur_tag->len > 0) {
567 pdkim_strtrim(cur_val);
569 if (ctx->debug_stream)
570 fprintf(ctx->debug_stream, "%s=%s\n", cur_tag->str, cur_val->str);
572 switch (cur_tag->str[0]) {
574 switch (cur_tag->str[1]) {
576 sig->bodyhash = pdkim_decode_base64(cur_val->str,&(sig->bodyhash_len));
579 sig->sigdata = pdkim_decode_base64(cur_val->str,&(sig->sigdata_len));
584 if (strcmp(cur_val->str,PDKIM_SIGNATURE_VERSION) == 0) {
585 /* We only support version 1, and that is currently the
586 only version there is. */
592 while (pdkim_algos[i] != NULL) {
593 if (strcmp(cur_val->str,pdkim_algos[i]) == 0 ) {
602 while (pdkim_combined_canons[i].str != NULL) {
603 if (strcmp(cur_val->str,pdkim_combined_canons[i].str) == 0 ) {
604 sig->canon_headers = pdkim_combined_canons[i].canon_headers;
605 sig->canon_body = pdkim_combined_canons[i].canon_body;
613 while (pdkim_querymethods[i] != NULL) {
614 if (strcmp(cur_val->str,pdkim_querymethods[i]) == 0 ) {
615 sig->querymethod = i;
622 sig->selector = strdup(cur_val->str);
625 sig->domain = strdup(cur_val->str);
628 sig->identity = pdkim_decode_qp(cur_val->str);
631 sig->created = strtoul(cur_val->str,NULL,10);
634 sig->expires = strtoul(cur_val->str,NULL,10);
637 sig->bodylength = strtol(cur_val->str,NULL,10);
640 sig->headernames = strdup(cur_val->str);
643 sig->copiedheaders = pdkim_decode_qp(cur_val->str);
647 if (ctx->debug_stream)
648 fprintf(ctx->debug_stream, "Unknown tag encountered\n");
653 pdkim_strclear(cur_tag);
654 pdkim_strclear(cur_val);
656 where = PDKIM_HDR_LIMBO;
659 else pdkim_strncat(cur_val,p,1);
663 if (*p == '\0') break;
672 /* Make sure the most important bits are there. */
673 if (!(sig->domain && (*(sig->domain) != '\0') &&
674 sig->selector && (*(sig->selector) != '\0') &&
675 sig->headernames && (*(sig->headernames) != '\0') &&
683 /* Copy header list to 'tick-off' header list */
684 sig->hnames_check = strdup(sig->headernames);
687 /* Chomp raw header. The final newline must not be added to the signature. */
689 while( (q > sig->rawsig_no_b_val) && ((*q == '\r') || (*q == '\n')) ) {
694 if (ctx->debug_stream) {
695 fprintf(ctx->debug_stream,
696 "PDKIM >> Raw signature w/o b= tag value >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
697 pdkim_quoteprint(ctx->debug_stream,
698 sig->rawsig_no_b_val,
699 strlen(sig->rawsig_no_b_val), 1);
700 fprintf(ctx->debug_stream,
701 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
705 sig->sha1_body = malloc(sizeof(sha1_context));
706 if (sig->sha1_body == NULL) {
710 sig->sha2_body = malloc(sizeof(sha2_context));
711 if (sig->sha2_body == NULL) {
716 sha1_starts(sig->sha1_body);
717 sha2_starts(sig->sha2_body,0);
723 /* -------------------------------------------------------------------------- */
724 pdkim_pubkey *pdkim_parse_pubkey_record(pdkim_ctx *ctx, char *raw_record) {
727 pdkim_str *cur_tag = NULL;
728 pdkim_str *cur_val = NULL;
729 int where = PDKIM_HDR_LIMBO;
731 pub = malloc(sizeof(pdkim_pubkey));
732 if (pub == NULL) return NULL;
733 memset(pub,0,sizeof(pdkim_pubkey));
740 if ( (*p == '\r') || (*p == '\n') )
743 if (where == PDKIM_HDR_LIMBO) {
744 /* In limbo, just wait for a tag-char to appear */
745 if (!((*p >= 'a') && (*p <= 'z')))
748 where = PDKIM_HDR_TAG;
751 if (where == PDKIM_HDR_TAG) {
753 cur_tag = pdkim_strnew(NULL);
755 if ((*p >= 'a') && (*p <= 'z'))
756 pdkim_strncat(cur_tag,p,1);
759 where = PDKIM_HDR_VALUE;
764 if (where == PDKIM_HDR_VALUE) {
766 cur_val = pdkim_strnew(NULL);
768 if ( (*p == '\r') || (*p == '\n') )
771 if ( (*p == ';') || (*p == '\0') ) {
772 if (cur_tag->len > 0) {
773 pdkim_strtrim(cur_val);
775 if (ctx->debug_stream)
776 fprintf(ctx->debug_stream, "%s=%s\n", cur_tag->str, cur_val->str);
778 switch (cur_tag->str[0]) {
780 /* This tag isn't evaluated because:
781 - We only support version DKIM1.
782 - Which is the default for this value (set below)
783 - Other versions are currently not specified. */
786 pub->hashes = strdup(cur_val->str);
789 pub->granularity = strdup(cur_val->str);
792 pub->notes = pdkim_decode_qp(cur_val->str);
795 pub->key = pdkim_decode_base64(cur_val->str,&(pub->key_len));
798 pub->hashes = strdup(cur_val->str);
801 pub->srvtype = strdup(cur_val->str);
804 if (strchr(cur_val->str,'t') != NULL) pub->testing = 1;
805 if (strchr(cur_val->str,'s') != NULL) pub->no_subdomaining = 1;
809 if (ctx->debug_stream)
810 fprintf(ctx->debug_stream, "Unknown tag encountered\n");
815 pdkim_strclear(cur_tag);
816 pdkim_strclear(cur_val);
817 where = PDKIM_HDR_LIMBO;
820 else pdkim_strncat(cur_val,p,1);
824 if (*p == '\0') break;
828 /* Set fallback defaults */
829 if (pub->version == NULL) pub->version = strdup(PDKIM_PUB_RECORD_VERSION);
830 if (pub->granularity == NULL) pub->granularity = strdup("*");
831 if (pub->keytype == NULL) pub->keytype = strdup("rsa");
832 if (pub->srvtype == NULL) pub->srvtype = strdup("*");
835 if (pub->key == NULL) {
836 pdkim_free_pubkey(pub);
844 /* -------------------------------------------------------------------------- */
845 int pdkim_update_bodyhash(pdkim_ctx *ctx, char *data, int len) {
846 pdkim_signature *sig = ctx->sig;
847 /* Cache relaxed version of data */
848 char *relaxed_data = NULL;
851 /* Traverse all signatures, updating their hashes. */
852 while (sig != NULL) {
853 /* Defaults to simple canon (no further treatment necessary) */
854 char *canon_data = data;
857 if (sig->canon_body == PDKIM_CANON_RELAXED) {
858 /* Relax the line if not done already */
859 if (relaxed_data == NULL) {
863 relaxed_data = malloc(len+1);
864 if (relaxed_data == NULL) return PDKIM_ERR_OOM;
868 if ( (q > 0) && (relaxed_data[q-1] == ' ') ) q--;
870 else if ( (c == '\t') || (c == ' ') ) {
871 c = ' '; /* Turns WSP into SP */
879 relaxed_data[q++] = c;
882 relaxed_data[q] = '\0';
885 canon_data = relaxed_data;
886 canon_len = relaxed_len;
889 /* Make sure we don't exceed the to-be-signed body length */
890 if ((sig->bodylength >= 0) &&
891 ((sig->signed_body_bytes+(unsigned long)canon_len) > sig->bodylength))
892 canon_len = (sig->bodylength - sig->signed_body_bytes);
895 if (sig->algo == PDKIM_ALGO_RSA_SHA1)
896 sha1_update(sig->sha1_body,(unsigned char *)canon_data,canon_len);
898 sha2_update(sig->sha2_body,(unsigned char *)canon_data,canon_len);
899 sig->signed_body_bytes += canon_len;
901 if (ctx->debug_stream!=NULL)
902 pdkim_quoteprint(ctx->debug_stream,canon_data,canon_len,0);
909 if (relaxed_data != NULL) free(relaxed_data);
914 /* -------------------------------------------------------------------------- */
915 int pdkim_finish_bodyhash(pdkim_ctx *ctx) {
916 pdkim_signature *sig = ctx->sig;
918 /* Traverse all signatures */
919 while (sig != NULL) {
922 unsigned char bh[32]; /* SHA-256 = 32 Bytes, SHA-1 = 20 Bytes */
923 if (sig->algo == PDKIM_ALGO_RSA_SHA1)
924 sha1_finish(sig->sha1_body,bh);
926 sha2_finish(sig->sha2_body,bh);
929 if (ctx->debug_stream) {
930 fprintf(ctx->debug_stream, "PDKIM [%s] Body bytes hashed: %lu\n",
931 sig->domain, sig->signed_body_bytes);
932 fprintf(ctx->debug_stream, "PDKIM [%s] bh computed: ", sig->domain);
933 pdkim_hexprint(ctx->debug_stream, (char *)bh,
934 (sig->algo == PDKIM_ALGO_RSA_SHA1)?20:32,1);
938 /* SIGNING -------------------------------------------------------------- */
939 if (ctx->mode == PDKIM_MODE_SIGN) {
940 sig->bodyhash_len = (sig->algo == PDKIM_ALGO_RSA_SHA1)?20:32;
941 sig->bodyhash = malloc(sig->bodyhash_len);
942 if (sig->bodyhash == NULL) return PDKIM_ERR_OOM;
943 memcpy(sig->bodyhash,bh,sig->bodyhash_len);
945 /* If bodylength limit is set, and we have received less bytes
946 than the requested amount, effectively remove the limit tag. */
947 if (sig->signed_body_bytes < sig->bodylength) sig->bodylength = -1;
949 /* VERIFICATION --------------------------------------------------------- */
951 /* Compare bodyhash */
952 if (memcmp(bh,sig->bodyhash,
953 (sig->algo == PDKIM_ALGO_RSA_SHA1)?20:32) == 0) {
955 if (ctx->debug_stream)
956 fprintf(ctx->debug_stream, "PDKIM [%s] Body hash verified OK\n",
962 if (ctx->debug_stream) {
963 fprintf(ctx->debug_stream, "PDKIM [%s] Body hash did NOT verify\n",
965 fprintf(ctx->debug_stream, "PDKIM [%s] bh signature: ", sig->domain);
966 pdkim_hexprint(ctx->debug_stream, sig->bodyhash,
967 (sig->algo == PDKIM_ALGO_RSA_SHA1)?20:32,1);
970 sig->verify_status = PDKIM_VERIFY_FAIL;
971 sig->verify_ext_status = PDKIM_VERIFY_FAIL_BODY;
983 /* -------------------------------------------------------------------------- */
984 /* Callback from pdkim_feed below for processing complete body lines */
985 int pdkim_bodyline_complete(pdkim_ctx *ctx) {
986 char *p = ctx->linebuf;
987 int n = ctx->linebuf_offset;
989 /* Ignore extra data if we've seen the end-of-data marker */
990 if (ctx->seen_eod) goto BAIL;
992 /* We've always got one extra byte to stuff a zero ... */
993 ctx->linebuf[(ctx->linebuf_offset)] = '\0';
995 if (ctx->input_mode == PDKIM_INPUT_SMTP) {
996 /* Terminate on EOD marker */
997 if (memcmp(p,".\r\n",3) == 0) {
1002 if (memcmp(p,"..",2) == 0) {
1008 /* Empty lines need to be buffered until we find a non-empty line */
1009 if (memcmp(p,"\r\n",2) == 0) {
1010 ctx->num_buffered_crlf++;
1014 /* At this point, we have a non-empty line, so release the buffered ones. */
1015 while (ctx->num_buffered_crlf) {
1016 pdkim_update_bodyhash(ctx,"\r\n",2);
1017 ctx->num_buffered_crlf--;
1020 pdkim_update_bodyhash(ctx,p,n);
1023 ctx->linebuf_offset = 0;
1028 /* -------------------------------------------------------------------------- */
1029 /* Callback from pdkim_feed below for processing complete headers */
1030 #define DKIM_SIGNATURE_HEADERNAME "DKIM-Signature:"
1031 int pdkim_header_complete(pdkim_ctx *ctx) {
1032 pdkim_signature *sig = ctx->sig;
1034 /* Special case: The last header can have an extra \r appended */
1035 if ( (ctx->cur_header->len > 1) &&
1036 (ctx->cur_header->str[(ctx->cur_header->len)-1] == '\r') ) {
1037 ctx->cur_header->str[(ctx->cur_header->len)-1] = '\0';
1038 ctx->cur_header->len--;
1042 if (ctx->num_headers > PDKIM_MAX_HEADERS) goto BAIL;
1044 /* Traverse all signatures */
1045 while (sig != NULL) {
1046 pdkim_stringlist *list;
1048 /* SIGNING -------------------------------------------------------------- */
1049 if (ctx->mode == PDKIM_MODE_SIGN) {
1050 if (header_name_match(ctx->cur_header->str,
1053 PDKIM_DEFAULT_SIGN_HEADERS, 0) != PDKIM_OK) goto NEXT_SIG;
1055 /* VERIFICATION --------------------------------------------------------- */
1057 /* Header is not included or all instances were already 'ticked off' */
1058 if (header_name_match(ctx->cur_header->str,
1059 sig->hnames_check, 1) != PDKIM_OK) goto NEXT_SIG;
1062 /* Add header to the signed headers list (in reverse order) */
1063 list = pdkim_prepend_stringlist(sig->headers,
1064 ctx->cur_header->str);
1065 if (list == NULL) return PDKIM_ERR_OOM;
1066 sig->headers = list;
1072 /* DKIM-Signature: headers are added to the verification list */
1073 if ( (ctx->mode == PDKIM_MODE_VERIFY) &&
1074 (strncasecmp(ctx->cur_header->str,
1075 DKIM_SIGNATURE_HEADERNAME,
1076 strlen(DKIM_SIGNATURE_HEADERNAME)) == 0) ) {
1077 pdkim_signature *new_sig;
1078 /* Create and chain new signature block */
1080 if (ctx->debug_stream)
1081 fprintf(ctx->debug_stream,
1082 "PDKIM >> Found sig, trying to parse >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
1084 new_sig = pdkim_parse_sig_header(ctx, ctx->cur_header->str);
1085 if (new_sig != NULL) {
1086 pdkim_signature *last_sig = ctx->sig;
1087 if (last_sig == NULL) {
1091 while (last_sig->next != NULL) { last_sig = last_sig->next; }
1092 last_sig->next = new_sig;
1097 if (ctx->debug_stream) {
1098 fprintf(ctx->debug_stream,"Error while parsing signature header\n");
1099 fprintf(ctx->debug_stream,
1100 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1107 pdkim_strclear(ctx->cur_header); /* Re-use existing pdkim_str */
1113 /* -------------------------------------------------------------------------- */
1114 #define HEADER_BUFFER_FRAG_SIZE 256
1115 DLLEXPORT int pdkim_feed (pdkim_ctx *ctx,
1119 for (p=0;p<len;p++) {
1121 if (ctx->past_headers) {
1122 /* Processing body byte */
1123 ctx->linebuf[(ctx->linebuf_offset)++] = c;
1125 int rc = pdkim_bodyline_complete(ctx); /* End of line */
1126 if (rc != PDKIM_OK) return rc;
1128 if (ctx->linebuf_offset == (PDKIM_MAX_BODY_LINE_LEN-1))
1129 return PDKIM_ERR_LONG_LINE;
1132 /* Processing header byte */
1136 int rc = pdkim_header_complete(ctx); /* Seen last header line */
1137 if (rc != PDKIM_OK) return rc;
1138 ctx->past_headers = 1;
1141 if (ctx->debug_stream)
1142 fprintf(ctx->debug_stream,
1143 "PDKIM >> Hashed body data, canonicalized >>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
1147 else ctx->seen_lf = 1;
1149 else if (ctx->seen_lf) {
1150 if (! ((c == '\t') || (c == ' '))) {
1151 int rc = pdkim_header_complete(ctx); /* End of header */
1152 if (rc != PDKIM_OK) return rc;
1157 if (ctx->cur_header == NULL) {
1158 ctx->cur_header = pdkim_strnew(NULL);
1159 if (ctx->cur_header == NULL) return PDKIM_ERR_OOM;
1161 if (ctx->cur_header->len < PDKIM_MAX_HEADER_LEN)
1162 if (pdkim_strncat(ctx->cur_header,&data[p],1) == NULL)
1163 return PDKIM_ERR_OOM;
1170 /* -------------------------------------------------------------------------- */
1171 char *pdkim_create_header(pdkim_signature *sig, int final) {
1173 char *base64_bh = NULL;
1174 char *base64_b = NULL;
1175 pdkim_str *hdr = pdkim_strnew("DKIM-Signature: v="PDKIM_SIGNATURE_VERSION);
1176 if (hdr == NULL) return NULL;
1178 base64_bh = pdkim_encode_base64(sig->bodyhash, sig->bodyhash_len);
1179 if (base64_bh == NULL) goto BAIL;
1181 /* Required and static bits */
1183 pdkim_strcat(hdr,"; a=") &&
1184 pdkim_strcat(hdr,pdkim_algos[sig->algo]) &&
1185 pdkim_strcat(hdr,"; q=") &&
1186 pdkim_strcat(hdr,pdkim_querymethods[sig->querymethod]) &&
1187 pdkim_strcat(hdr,"; c=") &&
1188 pdkim_strcat(hdr,pdkim_canons[sig->canon_headers]) &&
1189 pdkim_strcat(hdr,"/") &&
1190 pdkim_strcat(hdr,pdkim_canons[sig->canon_body]) &&
1191 pdkim_strcat(hdr,"; d=") &&
1192 pdkim_strcat(hdr,sig->domain) &&
1193 pdkim_strcat(hdr,"; s=") &&
1194 pdkim_strcat(hdr,sig->selector) &&
1195 pdkim_strcat(hdr,";\r\n\th=") &&
1196 pdkim_strcat(hdr,sig->headernames) &&
1197 pdkim_strcat(hdr,"; bh=") &&
1198 pdkim_strcat(hdr,base64_bh) &&
1199 pdkim_strcat(hdr,";\r\n\t")
1202 if (sig->identity != NULL) {
1203 if (!( pdkim_strcat(hdr,"i=") &&
1204 pdkim_strcat(hdr,sig->identity) &&
1205 pdkim_strcat(hdr,";") ) ) {
1209 if (sig->created > 0) {
1210 if (!( pdkim_strcat(hdr,"t=") &&
1211 pdkim_numcat(hdr,sig->created) &&
1212 pdkim_strcat(hdr,";") ) ) {
1216 if (sig->expires > 0) {
1217 if (!( pdkim_strcat(hdr,"x=") &&
1218 pdkim_numcat(hdr,sig->expires) &&
1219 pdkim_strcat(hdr,";") ) ) {
1223 if (sig->bodylength >= 0) {
1224 if (!( pdkim_strcat(hdr,"l=") &&
1225 pdkim_numcat(hdr,sig->bodylength) &&
1226 pdkim_strcat(hdr,";") ) ) {
1230 /* Extra linebreak */
1231 if (hdr->str[(hdr->len)-1] == ';') {
1232 if (!pdkim_strcat(hdr," \r\n\t")) goto BAIL;
1234 /* Preliminary or final version? */
1236 base64_b = pdkim_encode_base64(sig->sigdata, sig->sigdata_len);
1237 if (base64_b == NULL) goto BAIL;
1239 pdkim_strcat(hdr,"b=") &&
1240 pdkim_strcat(hdr,base64_b) &&
1241 pdkim_strcat(hdr,";")
1245 if (pdkim_strcat(hdr,"b=;")) goto DONE;
1252 rc = strdup(hdr->str);
1256 if (base64_bh != NULL) free(base64_bh);
1257 if (base64_b != NULL) free(base64_b);
1262 /* -------------------------------------------------------------------------- */
1263 DLLEXPORT int pdkim_feed_finish(pdkim_ctx *ctx, pdkim_signature **return_signatures) {
1264 pdkim_signature *sig = ctx->sig;
1265 pdkim_str *headernames = NULL; /* Collected signed header names */
1267 /* Check if we must still flush a (partial) header. If that is the
1268 case, the message has no body, and we must compute a body hash
1269 out of '<CR><LF>' */
1270 if (ctx->cur_header->len) {
1271 int rc = pdkim_header_complete(ctx);
1272 if (rc != PDKIM_OK) return rc;
1273 pdkim_update_bodyhash(ctx,"\r\n",2);
1276 /* For non-smtp input, check if there's an unfinished line in the
1277 body line buffer. If that is the case, we must add a CRLF to the
1278 hash to properly terminate the message. */
1279 if ((ctx->input_mode == PDKIM_INPUT_NORMAL) && ctx->linebuf_offset) {
1280 pdkim_update_bodyhash(ctx, ctx->linebuf, ctx->linebuf_offset);
1281 pdkim_update_bodyhash(ctx,"\r\n",2);
1284 if (ctx->debug_stream)
1285 fprintf(ctx->debug_stream,
1286 "\nPDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1290 /* Build (and/or evaluate) body hash */
1291 if (pdkim_finish_bodyhash(ctx) != PDKIM_OK) return PDKIM_ERR_OOM;
1293 /* SIGNING -------------------------------------------------------------- */
1294 if (ctx->mode == PDKIM_MODE_SIGN) {
1295 headernames = pdkim_strnew(NULL);
1296 if (headernames == NULL) return PDKIM_ERR_OOM;
1298 /* ---------------------------------------------------------------------- */
1300 while (sig != NULL) {
1301 sha1_context sha1_headers;
1302 sha2_context sha2_headers;
1304 char headerhash[32];
1306 if (sig->algo == PDKIM_ALGO_RSA_SHA1)
1307 sha1_starts(&sha1_headers);
1309 sha2_starts(&sha2_headers,0);
1312 if (ctx->debug_stream)
1313 fprintf(ctx->debug_stream,
1314 "PDKIM >> Hashed header data, canonicalized, in sequence >>>>>>>>>>>>>>\n");
1317 /* SIGNING ---------------------------------------------------------------- */
1318 /* When signing, walk through our header list and add them to the hash. As we
1319 go, construct a list of the header's names to use for the h= parameter. */
1320 if (ctx->mode == PDKIM_MODE_SIGN) {
1321 pdkim_stringlist *p = sig->headers;
1324 /* Collect header names (Note: colon presence is guaranteed here) */
1325 char *q = strchr(p->value,':');
1326 if (pdkim_strncat(headernames, p->value,
1327 (q-(p->value))+((p->next==NULL)?0:1)) == NULL)
1328 return PDKIM_ERR_OOM;
1330 if (sig->canon_headers == PDKIM_CANON_RELAXED)
1331 rh = pdkim_relax_header(p->value,1); /* cook header for relaxed canon */
1333 rh = strdup(p->value); /* just copy it for simple canon */
1335 if (rh == NULL) return PDKIM_ERR_OOM;
1337 /* Feed header to the hash algorithm */
1338 if (sig->algo == PDKIM_ALGO_RSA_SHA1)
1339 sha1_update(&(sha1_headers),(unsigned char *)rh,strlen(rh));
1341 sha2_update(&(sha2_headers),(unsigned char *)rh,strlen(rh));
1343 if (ctx->debug_stream)
1344 pdkim_quoteprint(ctx->debug_stream, rh, strlen(rh), 1);
1350 /* VERIFICATION ----------------------------------------------------------- */
1351 /* When verifying, walk through the header name list in the h= parameter and
1352 add the headers to the hash in that order. */
1354 char *b = strdup(sig->headernames);
1357 if (b == NULL) return PDKIM_ERR_OOM;
1360 pdkim_stringlist *hdrs = sig->headers;
1362 if (q != NULL) *q = '\0';
1363 while (hdrs != NULL) {
1364 if ( (strncasecmp(hdrs->value,p,strlen(p)) == 0) &&
1365 ((hdrs->value)[strlen(p)] == ':') ) {
1367 if (sig->canon_headers == PDKIM_CANON_RELAXED)
1368 rh = pdkim_relax_header(hdrs->value,1); /* cook header for relaxed canon */
1370 rh = strdup(hdrs->value); /* just copy it for simple canon */
1371 if (rh == NULL) return PDKIM_ERR_OOM;
1372 /* Feed header to the hash algorithm */
1373 if (sig->algo == PDKIM_ALGO_RSA_SHA1)
1374 sha1_update(&(sha1_headers),(unsigned char *)rh,strlen(rh));
1376 sha2_update(&(sha2_headers),(unsigned char *)rh,strlen(rh));
1378 if (ctx->debug_stream)
1379 pdkim_quoteprint(ctx->debug_stream, rh, strlen(rh), 1);
1382 (hdrs->value)[0] = '_';
1387 if (q == NULL) break;
1394 if (ctx->debug_stream)
1395 fprintf(ctx->debug_stream,
1396 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1399 /* SIGNING ---------------------------------------------------------------- */
1400 if (ctx->mode == PDKIM_MODE_SIGN) {
1401 /* Copy headernames to signature struct */
1402 sig->headernames = strdup(headernames->str);
1403 pdkim_strfree(headernames);
1405 /* Create signature header with b= omitted */
1406 sig_hdr = pdkim_create_header(ctx->sig,0);
1408 /* VERIFICATION ----------------------------------------------------------- */
1410 sig_hdr = strdup(sig->rawsig_no_b_val);
1412 /* ------------------------------------------------------------------------ */
1414 if (sig_hdr == NULL) return PDKIM_ERR_OOM;
1416 /* Relax header if necessary */
1417 if (sig->canon_headers == PDKIM_CANON_RELAXED) {
1418 char *relaxed_hdr = pdkim_relax_header(sig_hdr,0);
1420 if (relaxed_hdr == NULL) return PDKIM_ERR_OOM;
1421 sig_hdr = relaxed_hdr;
1425 if (ctx->debug_stream) {
1426 fprintf(ctx->debug_stream,
1427 "PDKIM >> Signed DKIM-Signature header, canonicalized >>>>>>>>>>>>>>>>>\n");
1428 pdkim_quoteprint(ctx->debug_stream, sig_hdr, strlen(sig_hdr), 1);
1429 fprintf(ctx->debug_stream,
1430 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1434 /* Finalize header hash */
1435 if (sig->algo == PDKIM_ALGO_RSA_SHA1) {
1436 sha1_update(&(sha1_headers),(unsigned char *)sig_hdr,strlen(sig_hdr));
1437 sha1_finish(&(sha1_headers),(unsigned char *)headerhash);
1439 if (ctx->debug_stream) {
1440 fprintf(ctx->debug_stream, "PDKIM [%s] hh computed: ", sig->domain);
1441 pdkim_hexprint(ctx->debug_stream, headerhash, 20, 1);
1446 sha2_update(&(sha2_headers),(unsigned char *)sig_hdr,strlen(sig_hdr));
1447 sha2_finish(&(sha2_headers),(unsigned char *)headerhash);
1449 if (ctx->debug_stream) {
1450 fprintf(ctx->debug_stream, "PDKIM [%s] hh computed: ", sig->domain);
1451 pdkim_hexprint(ctx->debug_stream, headerhash, 32, 1);
1458 /* SIGNING ---------------------------------------------------------------- */
1459 if (ctx->mode == PDKIM_MODE_SIGN) {
1462 rsa_init(&rsa,RSA_PKCS_V15,0,NULL,NULL);
1464 /* Perform private key operation */
1465 if (rsa_parse_key(&rsa, (unsigned char *)sig->rsa_privkey,
1466 strlen(sig->rsa_privkey), NULL, 0) != 0) {
1467 return PDKIM_ERR_RSA_PRIVKEY;
1470 sig->sigdata_len = mpi_size(&(rsa.N));
1471 sig->sigdata = malloc(sig->sigdata_len);
1472 if (sig->sigdata == NULL) return PDKIM_ERR_OOM;
1474 if (rsa_pkcs1_sign( &rsa, RSA_PRIVATE,
1475 ((sig->algo == PDKIM_ALGO_RSA_SHA1)?
1476 RSA_SHA1:RSA_SHA256),
1478 (unsigned char *)headerhash,
1479 (unsigned char *)sig->sigdata ) != 0) {
1480 return PDKIM_ERR_RSA_SIGNING;
1486 if (ctx->debug_stream) {
1487 fprintf(ctx->debug_stream, "PDKIM [%s] b computed: ",
1489 pdkim_hexprint(ctx->debug_stream, sig->sigdata, sig->sigdata_len, 1);
1493 sig->signature_header = pdkim_create_header(ctx->sig,1);
1494 if (sig->signature_header == NULL) return PDKIM_ERR_OOM;
1496 /* VERIFICATION ----------------------------------------------------------- */
1499 char *dns_txt_name, *dns_txt_reply;
1501 rsa_init(&rsa,RSA_PKCS_V15,0,NULL,NULL);
1503 dns_txt_name = malloc(PDKIM_DNS_TXT_MAX_NAMELEN);
1504 if (dns_txt_name == NULL) return PDKIM_ERR_OOM;
1505 dns_txt_reply = malloc(PDKIM_DNS_TXT_MAX_RECLEN);
1506 if (dns_txt_reply == NULL) {
1508 return PDKIM_ERR_OOM;
1510 memset(dns_txt_reply,0,PDKIM_DNS_TXT_MAX_RECLEN);
1511 memset(dns_txt_name ,0,PDKIM_DNS_TXT_MAX_NAMELEN);
1513 if (snprintf(dns_txt_name,PDKIM_DNS_TXT_MAX_NAMELEN,
1514 "%s._domainkey.%s.",
1515 sig->selector,sig->domain) >= PDKIM_DNS_TXT_MAX_NAMELEN) {
1516 sig->verify_status = PDKIM_VERIFY_INVALID;
1517 sig->verify_ext_status = PDKIM_VERIFY_INVALID_BUFFER_SIZE;
1521 if ((ctx->dns_txt_callback(dns_txt_name, dns_txt_reply) != PDKIM_OK) ||
1522 (dns_txt_reply[0] == '\0')) {
1523 sig->verify_status = PDKIM_VERIFY_INVALID;
1524 sig->verify_ext_status = PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE;
1529 if (ctx->debug_stream) {
1530 fprintf(ctx->debug_stream,
1531 "PDKIM >> Parsing public key record >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
1532 fprintf(ctx->debug_stream,"Raw record: ");
1533 pdkim_quoteprint(ctx->debug_stream, dns_txt_reply, strlen(dns_txt_reply), 1);
1537 sig->pubkey = pdkim_parse_pubkey_record(ctx,dns_txt_reply);
1538 if (sig->pubkey == NULL) {
1539 sig->verify_status = PDKIM_VERIFY_INVALID;
1540 sig->verify_ext_status = PDKIM_VERIFY_INVALID_PUBKEY_PARSING;
1542 if (ctx->debug_stream) {
1543 fprintf(ctx->debug_stream,"Error while parsing public key record\n");
1544 fprintf(ctx->debug_stream,
1545 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1552 if (ctx->debug_stream) {
1553 fprintf(ctx->debug_stream,
1554 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1558 if (rsa_parse_public_key(&rsa,
1559 (unsigned char *)sig->pubkey->key,
1560 sig->pubkey->key_len) != 0) {
1561 sig->verify_status = PDKIM_VERIFY_INVALID;
1562 sig->verify_ext_status = PDKIM_VERIFY_INVALID_PUBKEY_PARSING;
1566 /* Check the signature */
1567 if (rsa_pkcs1_verify(&rsa,
1569 ((sig->algo == PDKIM_ALGO_RSA_SHA1)?
1570 RSA_SHA1:RSA_SHA256),
1572 (unsigned char *)headerhash,
1573 (unsigned char *)sig->sigdata) != 0) {
1574 sig->verify_status = PDKIM_VERIFY_FAIL;
1575 sig->verify_ext_status = PDKIM_VERIFY_FAIL_MESSAGE;
1577 if (ctx->debug_stream) {
1578 fprintf(ctx->debug_stream, "PDKIM [%s] signature did NOT verify OK\n",
1585 /* We have a winner! */
1586 sig->verify_status = PDKIM_VERIFY_PASS;
1589 if (ctx->debug_stream) {
1590 fprintf(ctx->debug_stream, "PDKIM [%s] signature verified OK\n",
1598 free(dns_txt_reply);
1604 /* If requested, set return pointer to signature(s) */
1605 if (return_signatures != NULL) {
1606 *return_signatures = ctx->sig;
1613 /* -------------------------------------------------------------------------- */
1614 DLLEXPORT pdkim_ctx *pdkim_init_verify(int input_mode,
1615 int(*dns_txt_callback)(char *, char *)
1617 pdkim_ctx *ctx = malloc(sizeof(pdkim_ctx));
1618 if (ctx == NULL) return NULL;
1619 memset(ctx,0,sizeof(pdkim_ctx));
1621 ctx->linebuf = malloc(PDKIM_MAX_BODY_LINE_LEN);
1622 if (ctx->linebuf == NULL) {
1627 ctx->mode = PDKIM_MODE_VERIFY;
1628 ctx->input_mode = input_mode;
1629 ctx->dns_txt_callback = dns_txt_callback;
1635 /* -------------------------------------------------------------------------- */
1636 DLLEXPORT pdkim_ctx *pdkim_init_sign(int input_mode,
1639 char *rsa_privkey) {
1641 pdkim_signature *sig;
1643 if (!domain || !selector || !rsa_privkey) return NULL;
1645 ctx = malloc(sizeof(pdkim_ctx));
1646 if (ctx == NULL) return NULL;
1647 memset(ctx,0,sizeof(pdkim_ctx));
1649 ctx->linebuf = malloc(PDKIM_MAX_BODY_LINE_LEN);
1650 if (ctx->linebuf == NULL) {
1655 sig = malloc(sizeof(pdkim_signature));
1661 memset(sig,0,sizeof(pdkim_signature));
1662 sig->bodylength = -1;
1664 ctx->mode = PDKIM_MODE_SIGN;
1665 ctx->input_mode = input_mode;
1668 ctx->sig->domain = strdup(domain);
1669 ctx->sig->selector = strdup(selector);
1670 ctx->sig->rsa_privkey = strdup(rsa_privkey);
1672 if (!ctx->sig->domain || !ctx->sig->selector || !ctx->sig->rsa_privkey) {
1673 pdkim_free_ctx(ctx);
1677 ctx->sig->sha1_body = malloc(sizeof(sha1_context));
1678 if (ctx->sig->sha1_body == NULL) {
1679 pdkim_free_ctx(ctx);
1682 sha1_starts(ctx->sig->sha1_body);
1684 ctx->sig->sha2_body = malloc(sizeof(sha2_context));
1685 if (ctx->sig->sha2_body == NULL) {
1686 pdkim_free_ctx(ctx);
1689 sha2_starts(ctx->sig->sha2_body,0);
1695 /* -------------------------------------------------------------------------- */
1696 DLLEXPORT void pdkim_set_debug_stream(pdkim_ctx *ctx,
1697 FILE *debug_stream) {
1698 ctx->debug_stream = debug_stream;
1702 /* -------------------------------------------------------------------------- */
1703 DLLEXPORT int pdkim_set_optional(pdkim_ctx *ctx,
1710 unsigned long created,
1711 unsigned long expires) {
1713 if (identity != NULL) {
1714 ctx->sig->identity = strdup(identity);
1715 if (ctx->sig->identity == NULL) return PDKIM_ERR_OOM;
1718 if (sign_headers != NULL) {
1719 ctx->sig->sign_headers = strdup(sign_headers);
1720 if (ctx->sig->sign_headers == NULL) return PDKIM_ERR_OOM;
1723 ctx->sig->canon_headers = canon_headers;
1724 ctx->sig->canon_body = canon_body;
1725 ctx->sig->bodylength = bodylength;
1726 ctx->sig->algo = algo;
1727 ctx->sig->created = created;
1728 ctx->sig->expires = expires;