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.3 2009/09/09 08:13:58 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));
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;
867 if ( (c == '\t') || (c == ' ') ) {
868 c = ' '; /* Turns WSP into SP */
876 relaxed_data[q++] = c;
879 relaxed_data[q] = '\0';
882 canon_data = relaxed_data;
883 canon_len = relaxed_len;
886 /* Make sure we don't exceed the to-be-signed body length */
887 if ((sig->bodylength >= 0) &&
888 ((sig->signed_body_bytes+(unsigned long)canon_len) > sig->bodylength))
889 canon_len = (sig->bodylength - sig->signed_body_bytes);
892 if (sig->algo == PDKIM_ALGO_RSA_SHA1)
893 sha1_update(sig->sha1_body,(unsigned char *)canon_data,canon_len);
895 sha2_update(sig->sha2_body,(unsigned char *)canon_data,canon_len);
896 sig->signed_body_bytes += canon_len;
898 if (ctx->debug_stream!=NULL)
899 pdkim_quoteprint(ctx->debug_stream,canon_data,canon_len,0);
906 if (relaxed_data != NULL) free(relaxed_data);
911 /* -------------------------------------------------------------------------- */
912 int pdkim_finish_bodyhash(pdkim_ctx *ctx) {
913 pdkim_signature *sig = ctx->sig;
915 /* Traverse all signatures */
916 while (sig != NULL) {
919 unsigned char bh[32]; /* SHA-256 = 32 Bytes, SHA-1 = 20 Bytes */
920 if (sig->algo == PDKIM_ALGO_RSA_SHA1)
921 sha1_finish(sig->sha1_body,bh);
923 sha2_finish(sig->sha2_body,bh);
926 if (ctx->debug_stream) {
927 fprintf(ctx->debug_stream, "PDKIM [%s] Body bytes hashed: %lu\n",
928 sig->domain, sig->signed_body_bytes);
929 fprintf(ctx->debug_stream, "PDKIM [%s] bh computed: ", sig->domain);
930 pdkim_hexprint(ctx->debug_stream, (char *)bh,
931 (sig->algo == PDKIM_ALGO_RSA_SHA1)?20:32,1);
935 /* SIGNING -------------------------------------------------------------- */
936 if (ctx->mode == PDKIM_MODE_SIGN) {
937 sig->bodyhash_len = (sig->algo == PDKIM_ALGO_RSA_SHA1)?20:32;
938 sig->bodyhash = malloc(sig->bodyhash_len);
939 if (sig->bodyhash == NULL) return PDKIM_ERR_OOM;
940 memcpy(sig->bodyhash,bh,sig->bodyhash_len);
942 /* If bodylength limit is set, and we have received less bytes
943 than the requested amount, effectively remove the limit tag. */
944 if (sig->signed_body_bytes < sig->bodylength) sig->bodylength = -1;
946 /* VERIFICATION --------------------------------------------------------- */
948 /* Compare bodyhash */
949 if (memcmp(bh,sig->bodyhash,
950 (sig->algo == PDKIM_ALGO_RSA_SHA1)?20:32) == 0) {
952 if (ctx->debug_stream)
953 fprintf(ctx->debug_stream, "PDKIM [%s] Body hash verified OK\n",
959 if (ctx->debug_stream) {
960 fprintf(ctx->debug_stream, "PDKIM [%s] Body hash did NOT verify\n",
962 fprintf(ctx->debug_stream, "PDKIM [%s] bh signature: ", sig->domain);
963 pdkim_hexprint(ctx->debug_stream, sig->bodyhash,
964 (sig->algo == PDKIM_ALGO_RSA_SHA1)?20:32,1);
967 sig->verify_status = PDKIM_VERIFY_FAIL;
968 sig->verify_ext_status = PDKIM_VERIFY_FAIL_BODY;
980 /* -------------------------------------------------------------------------- */
981 /* Callback from pdkim_feed below for processing complete body lines */
982 int pdkim_bodyline_complete(pdkim_ctx *ctx) {
983 char *p = ctx->linebuf;
984 int n = ctx->linebuf_offset;
986 /* Ignore extra data if we've seen the end-of-data marker */
987 if (ctx->seen_eod) goto BAIL;
989 /* We've always got one extra byte to stuff a zero ... */
990 ctx->linebuf[(ctx->linebuf_offset)] = '\0';
992 if (ctx->input_mode == PDKIM_INPUT_SMTP) {
993 /* Terminate on EOD marker */
994 if (memcmp(p,".\r\n",3) == 0) {
999 if (memcmp(p,"..",2) == 0) {
1005 /* Empty lines need to be buffered until we find a non-empty line */
1006 if (memcmp(p,"\r\n",2) == 0) {
1007 ctx->num_buffered_crlf++;
1011 /* At this point, we have a non-empty line, so release the buffered ones. */
1012 while (ctx->num_buffered_crlf) {
1013 pdkim_update_bodyhash(ctx,"\r\n",2);
1014 ctx->num_buffered_crlf--;
1017 pdkim_update_bodyhash(ctx,p,n);
1020 ctx->linebuf_offset = 0;
1025 /* -------------------------------------------------------------------------- */
1026 /* Callback from pdkim_feed below for processing complete headers */
1027 #define DKIM_SIGNATURE_HEADERNAME "DKIM-Signature:"
1028 int pdkim_header_complete(pdkim_ctx *ctx) {
1029 pdkim_signature *sig = ctx->sig;
1031 /* Special case: The last header can have an extra \r appended */
1032 if ( (ctx->cur_header->len > 1) &&
1033 (ctx->cur_header->str[(ctx->cur_header->len)-1] == '\r') ) {
1034 ctx->cur_header->str[(ctx->cur_header->len)-1] = '\0';
1035 ctx->cur_header->len--;
1039 if (ctx->num_headers > PDKIM_MAX_HEADERS) goto BAIL;
1041 /* Traverse all signatures */
1042 while (sig != NULL) {
1043 pdkim_stringlist *list;
1045 /* SIGNING -------------------------------------------------------------- */
1046 if (ctx->mode == PDKIM_MODE_SIGN) {
1047 if (header_name_match(ctx->cur_header->str,
1050 PDKIM_DEFAULT_SIGN_HEADERS, 0) != PDKIM_OK) goto NEXT_SIG;
1052 /* VERIFICATION --------------------------------------------------------- */
1054 /* Header is not included or all instances were already 'ticked off' */
1055 if (header_name_match(ctx->cur_header->str,
1056 sig->hnames_check, 1) != PDKIM_OK) goto NEXT_SIG;
1059 /* Add header to the signed headers list (in reverse order) */
1060 list = pdkim_prepend_stringlist(sig->headers,
1061 ctx->cur_header->str);
1062 if (list == NULL) return PDKIM_ERR_OOM;
1063 sig->headers = list;
1069 /* DKIM-Signature: headers are added to the verification list */
1070 if ( (ctx->mode == PDKIM_MODE_VERIFY) &&
1071 (strncasecmp(ctx->cur_header->str,
1072 DKIM_SIGNATURE_HEADERNAME,
1073 strlen(DKIM_SIGNATURE_HEADERNAME)) == 0) ) {
1074 pdkim_signature *new_sig;
1075 /* Create and chain new signature block */
1077 if (ctx->debug_stream)
1078 fprintf(ctx->debug_stream,
1079 "PDKIM >> Found sig, trying to parse >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
1081 new_sig = pdkim_parse_sig_header(ctx, ctx->cur_header->str);
1082 if (new_sig != NULL) {
1083 pdkim_signature *last_sig = ctx->sig;
1084 if (last_sig == NULL) {
1088 while (last_sig->next != NULL) { last_sig = last_sig->next; };
1089 last_sig->next = new_sig;
1094 if (ctx->debug_stream) {
1095 fprintf(ctx->debug_stream,"Error while parsing signature header\n");
1096 fprintf(ctx->debug_stream,
1097 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1104 pdkim_strclear(ctx->cur_header); /* Re-use existing pdkim_str */
1110 /* -------------------------------------------------------------------------- */
1111 #define HEADER_BUFFER_FRAG_SIZE 256
1112 DLLEXPORT int pdkim_feed (pdkim_ctx *ctx,
1116 for (p=0;p<len;p++) {
1118 if (ctx->past_headers) {
1119 /* Processing body byte */
1120 ctx->linebuf[(ctx->linebuf_offset)++] = c;
1122 int rc = pdkim_bodyline_complete(ctx); /* End of line */
1123 if (rc != PDKIM_OK) return rc;
1125 if (ctx->linebuf_offset == (PDKIM_MAX_BODY_LINE_LEN-1))
1126 return PDKIM_ERR_LONG_LINE;
1129 /* Processing header byte */
1133 int rc = pdkim_header_complete(ctx); /* Seen last header line */
1134 if (rc != PDKIM_OK) return rc;
1135 ctx->past_headers = 1;
1138 if (ctx->debug_stream)
1139 fprintf(ctx->debug_stream,
1140 "PDKIM >> Hashed body data, canonicalized >>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
1144 else ctx->seen_lf = 1;
1146 else if (ctx->seen_lf) {
1147 if (! ((c == '\t') || (c == ' '))) {
1148 int rc = pdkim_header_complete(ctx); /* End of header */
1149 if (rc != PDKIM_OK) return rc;
1154 if (ctx->cur_header == NULL) {
1155 ctx->cur_header = pdkim_strnew(NULL);
1156 if (ctx->cur_header == NULL) return PDKIM_ERR_OOM;
1158 if (ctx->cur_header->len < PDKIM_MAX_HEADER_LEN)
1159 if (pdkim_strncat(ctx->cur_header,&data[p],1) == NULL)
1160 return PDKIM_ERR_OOM;
1167 /* -------------------------------------------------------------------------- */
1168 char *pdkim_create_header(pdkim_signature *sig, int final) {
1170 char *base64_bh = NULL;
1171 char *base64_b = NULL;
1172 pdkim_str *hdr = pdkim_strnew("DKIM-Signature: v="PDKIM_SIGNATURE_VERSION);
1173 if (hdr == NULL) return NULL;
1175 base64_bh = pdkim_encode_base64(sig->bodyhash, sig->bodyhash_len);
1176 if (base64_bh == NULL) goto BAIL;
1178 /* Required and static bits */
1180 pdkim_strcat(hdr,"; a=") &&
1181 pdkim_strcat(hdr,pdkim_algos[sig->algo]) &&
1182 pdkim_strcat(hdr,"; q=") &&
1183 pdkim_strcat(hdr,pdkim_querymethods[sig->querymethod]) &&
1184 pdkim_strcat(hdr,"; c=") &&
1185 pdkim_strcat(hdr,pdkim_canons[sig->canon_headers]) &&
1186 pdkim_strcat(hdr,"/") &&
1187 pdkim_strcat(hdr,pdkim_canons[sig->canon_body]) &&
1188 pdkim_strcat(hdr,"; d=") &&
1189 pdkim_strcat(hdr,sig->domain) &&
1190 pdkim_strcat(hdr,"; s=") &&
1191 pdkim_strcat(hdr,sig->selector) &&
1192 pdkim_strcat(hdr,";\r\n\th=") &&
1193 pdkim_strcat(hdr,sig->headernames) &&
1194 pdkim_strcat(hdr,"; bh=") &&
1195 pdkim_strcat(hdr,base64_bh) &&
1196 pdkim_strcat(hdr,";\r\n\t")
1199 if (sig->identity != NULL) {
1200 if (!( pdkim_strcat(hdr,"i=") &&
1201 pdkim_strcat(hdr,sig->identity) &&
1202 pdkim_strcat(hdr,";") ) ) {
1206 if (sig->created > 0) {
1207 if (!( pdkim_strcat(hdr,"t=") &&
1208 pdkim_numcat(hdr,sig->created) &&
1209 pdkim_strcat(hdr,";") ) ) {
1213 if (sig->expires > 0) {
1214 if (!( pdkim_strcat(hdr,"x=") &&
1215 pdkim_numcat(hdr,sig->expires) &&
1216 pdkim_strcat(hdr,";") ) ) {
1220 if (sig->bodylength >= 0) {
1221 if (!( pdkim_strcat(hdr,"l=") &&
1222 pdkim_numcat(hdr,sig->bodylength) &&
1223 pdkim_strcat(hdr,";") ) ) {
1227 /* Extra linebreak */
1228 if (hdr->str[(hdr->len)-1] == ';') {
1229 if (!pdkim_strcat(hdr," \r\n\t")) goto BAIL;
1231 /* Preliminary or final version? */
1233 base64_b = pdkim_encode_base64(sig->sigdata, sig->sigdata_len);
1234 if (base64_b == NULL) goto BAIL;
1236 pdkim_strcat(hdr,"b=") &&
1237 pdkim_strcat(hdr,base64_b) &&
1238 pdkim_strcat(hdr,";")
1242 if (pdkim_strcat(hdr,"b=;")) goto DONE;
1249 rc = strdup(hdr->str);
1253 if (base64_bh != NULL) free(base64_bh);
1254 if (base64_b != NULL) free(base64_b);
1259 /* -------------------------------------------------------------------------- */
1260 DLLEXPORT int pdkim_feed_finish(pdkim_ctx *ctx, pdkim_signature **return_signatures) {
1261 pdkim_signature *sig = ctx->sig;
1262 pdkim_str *headernames = NULL; /* Collected signed header names */
1264 /* Check if we must still flush a (partial) header. If that is the
1265 case, the message has no body, and we must compute a body hash
1266 out of '<CR><LF>' */
1267 if (ctx->cur_header->len) {
1268 int rc = pdkim_header_complete(ctx);
1269 if (rc != PDKIM_OK) return rc;
1270 pdkim_update_bodyhash(ctx,"\r\n",2);
1273 /* For non-smtp input, check if there's an unfinished line in the
1274 body line buffer. If that is the case, we must add a CRLF to the
1275 hash to properly terminate the message. */
1276 if ((ctx->input_mode == PDKIM_INPUT_NORMAL) && ctx->linebuf_offset) {
1277 pdkim_update_bodyhash(ctx, ctx->linebuf, ctx->linebuf_offset);
1278 pdkim_update_bodyhash(ctx,"\r\n",2);
1281 if (ctx->debug_stream)
1282 fprintf(ctx->debug_stream,
1283 "\nPDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1287 /* Build (and/or evaluate) body hash */
1288 if (pdkim_finish_bodyhash(ctx) != PDKIM_OK) return PDKIM_ERR_OOM;
1290 /* SIGNING -------------------------------------------------------------- */
1291 if (ctx->mode == PDKIM_MODE_SIGN) {
1292 headernames = pdkim_strnew(NULL);
1293 if (headernames == NULL) return PDKIM_ERR_OOM;
1295 /* ---------------------------------------------------------------------- */
1297 while (sig != NULL) {
1298 sha1_context sha1_headers;
1299 sha2_context sha2_headers;
1301 char headerhash[32];
1303 if (sig->algo == PDKIM_ALGO_RSA_SHA1)
1304 sha1_starts(&sha1_headers);
1306 sha2_starts(&sha2_headers,0);
1309 if (ctx->debug_stream)
1310 fprintf(ctx->debug_stream,
1311 "PDKIM >> Hashed header data, canonicalized, in sequence >>>>>>>>>>>>>>\n");
1314 /* SIGNING ---------------------------------------------------------------- */
1315 /* When signing, walk through our header list and add them to the hash. As we
1316 go, construct a list of the header's names to use for the h= parameter. */
1317 if (ctx->mode == PDKIM_MODE_SIGN) {
1318 pdkim_stringlist *p = sig->headers;
1321 /* Collect header names (Note: colon presence is guaranteed here) */
1322 char *q = strchr(p->value,':');
1323 if (pdkim_strncat(headernames, p->value,
1324 (q-(p->value))+((p->next==NULL)?0:1)) == NULL)
1325 return PDKIM_ERR_OOM;
1327 if (sig->canon_headers == PDKIM_CANON_RELAXED)
1328 rh = pdkim_relax_header(p->value,1); /* cook header for relaxed canon */
1330 rh = strdup(p->value); /* just copy it for simple canon */
1332 if (rh == NULL) return PDKIM_ERR_OOM;
1334 /* Feed header to the hash algorithm */
1335 if (sig->algo == PDKIM_ALGO_RSA_SHA1)
1336 sha1_update(&(sha1_headers),(unsigned char *)rh,strlen(rh));
1338 sha2_update(&(sha2_headers),(unsigned char *)rh,strlen(rh));
1340 if (ctx->debug_stream)
1341 pdkim_quoteprint(ctx->debug_stream, rh, strlen(rh), 1);
1347 /* VERIFICATION ----------------------------------------------------------- */
1348 /* When verifying, walk through the header name list in the h= parameter and
1349 add the headers to the hash in that order. */
1351 char *b = strdup(sig->headernames);
1354 if (b == NULL) return PDKIM_ERR_OOM;
1357 pdkim_stringlist *hdrs = sig->headers;
1359 if (q != NULL) *q = '\0';
1360 while (hdrs != NULL) {
1361 if ( (strncasecmp(hdrs->value,p,strlen(p)) == 0) &&
1362 ((hdrs->value)[strlen(p)] == ':') ) {
1364 if (sig->canon_headers == PDKIM_CANON_RELAXED)
1365 rh = pdkim_relax_header(hdrs->value,1); /* cook header for relaxed canon */
1367 rh = strdup(hdrs->value); /* just copy it for simple canon */
1368 if (rh == NULL) return PDKIM_ERR_OOM;
1369 /* Feed header to the hash algorithm */
1370 if (sig->algo == PDKIM_ALGO_RSA_SHA1)
1371 sha1_update(&(sha1_headers),(unsigned char *)rh,strlen(rh));
1373 sha2_update(&(sha2_headers),(unsigned char *)rh,strlen(rh));
1375 if (ctx->debug_stream)
1376 pdkim_quoteprint(ctx->debug_stream, rh, strlen(rh), 1);
1379 (hdrs->value)[0] = '_';
1384 if (q == NULL) break;
1391 if (ctx->debug_stream)
1392 fprintf(ctx->debug_stream,
1393 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1396 /* SIGNING ---------------------------------------------------------------- */
1397 if (ctx->mode == PDKIM_MODE_SIGN) {
1398 /* Copy headernames to signature struct */
1399 sig->headernames = strdup(headernames->str);
1400 pdkim_strfree(headernames);
1402 /* Create signature header with b= omitted */
1403 sig_hdr = pdkim_create_header(ctx->sig,0);
1405 /* VERIFICATION ----------------------------------------------------------- */
1407 sig_hdr = strdup(sig->rawsig_no_b_val);
1409 /* ------------------------------------------------------------------------ */
1411 if (sig_hdr == NULL) return PDKIM_ERR_OOM;
1413 /* Relax header if necessary */
1414 if (sig->canon_headers == PDKIM_CANON_RELAXED) {
1415 char *relaxed_hdr = pdkim_relax_header(sig_hdr,0);
1417 if (relaxed_hdr == NULL) return PDKIM_ERR_OOM;
1418 sig_hdr = relaxed_hdr;
1422 if (ctx->debug_stream) {
1423 fprintf(ctx->debug_stream,
1424 "PDKIM >> Signed DKIM-Signature header, canonicalized >>>>>>>>>>>>>>>>>\n");
1425 pdkim_quoteprint(ctx->debug_stream, sig_hdr, strlen(sig_hdr), 1);
1426 fprintf(ctx->debug_stream,
1427 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1431 /* Finalize header hash */
1432 if (sig->algo == PDKIM_ALGO_RSA_SHA1) {
1433 sha1_update(&(sha1_headers),(unsigned char *)sig_hdr,strlen(sig_hdr));
1434 sha1_finish(&(sha1_headers),(unsigned char *)headerhash);
1436 if (ctx->debug_stream) {
1437 fprintf(ctx->debug_stream, "PDKIM [%s] hh computed: ", sig->domain);
1438 pdkim_hexprint(ctx->debug_stream, headerhash, 20, 1);
1443 sha2_update(&(sha2_headers),(unsigned char *)sig_hdr,strlen(sig_hdr));
1444 sha2_finish(&(sha2_headers),(unsigned char *)headerhash);
1446 if (ctx->debug_stream) {
1447 fprintf(ctx->debug_stream, "PDKIM [%s] hh computed: ", sig->domain);
1448 pdkim_hexprint(ctx->debug_stream, headerhash, 32, 1);
1455 /* SIGNING ---------------------------------------------------------------- */
1456 if (ctx->mode == PDKIM_MODE_SIGN) {
1459 rsa_init(&rsa,RSA_PKCS_V15,0,NULL,NULL);
1461 /* Perform private key operation */
1462 if (rsa_parse_key(&rsa, (unsigned char *)sig->rsa_privkey,
1463 strlen(sig->rsa_privkey), NULL, 0) != 0) {
1464 return PDKIM_ERR_RSA_PRIVKEY;
1467 sig->sigdata_len = mpi_size(&(rsa.N));
1468 sig->sigdata = malloc(sig->sigdata_len);
1469 if (sig->sigdata == NULL) return PDKIM_ERR_OOM;
1471 if (rsa_pkcs1_sign( &rsa, RSA_PRIVATE,
1472 ((sig->algo == PDKIM_ALGO_RSA_SHA1)?
1473 RSA_SHA1:RSA_SHA256),
1475 (unsigned char *)headerhash,
1476 (unsigned char *)sig->sigdata ) != 0) {
1477 return PDKIM_ERR_RSA_SIGNING;
1483 if (ctx->debug_stream) {
1484 fprintf(ctx->debug_stream, "PDKIM [%s] b computed: ",
1486 pdkim_hexprint(ctx->debug_stream, sig->sigdata, sig->sigdata_len, 1);
1490 sig->signature_header = pdkim_create_header(ctx->sig,1);
1491 if (sig->signature_header == NULL) return PDKIM_ERR_OOM;
1493 /* VERIFICATION ----------------------------------------------------------- */
1496 char *dns_txt_name, *dns_txt_reply;
1498 rsa_init(&rsa,RSA_PKCS_V15,0,NULL,NULL);
1500 dns_txt_name = malloc(PDKIM_DNS_TXT_MAX_NAMELEN);
1501 if (dns_txt_name == NULL) return PDKIM_ERR_OOM;
1502 dns_txt_reply = malloc(PDKIM_DNS_TXT_MAX_RECLEN);
1503 if (dns_txt_reply == NULL) {
1505 return PDKIM_ERR_OOM;
1507 memset(dns_txt_reply,0,PDKIM_DNS_TXT_MAX_RECLEN);
1508 memset(dns_txt_name ,0,PDKIM_DNS_TXT_MAX_NAMELEN);
1510 if (snprintf(dns_txt_name,PDKIM_DNS_TXT_MAX_NAMELEN,
1511 "%s._domainkey.%s.",
1512 sig->selector,sig->domain) >= PDKIM_DNS_TXT_MAX_NAMELEN) {
1513 sig->verify_status = PDKIM_VERIFY_INVALID;
1514 sig->verify_ext_status = PDKIM_VERIFY_INVALID_BUFFER_SIZE;
1518 if ((ctx->dns_txt_callback(dns_txt_name, dns_txt_reply) != PDKIM_OK) ||
1519 (dns_txt_reply[0] == '\0')) {
1520 sig->verify_status = PDKIM_VERIFY_INVALID;
1521 sig->verify_ext_status = PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE;
1526 if (ctx->debug_stream) {
1527 fprintf(ctx->debug_stream,
1528 "PDKIM >> Parsing public key record >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
1529 fprintf(ctx->debug_stream,"Raw record: ");
1530 pdkim_quoteprint(ctx->debug_stream, dns_txt_reply, strlen(dns_txt_reply), 1);
1534 sig->pubkey = pdkim_parse_pubkey_record(ctx,dns_txt_reply);
1535 if (sig->pubkey == NULL) {
1536 sig->verify_status = PDKIM_VERIFY_INVALID;
1537 sig->verify_ext_status = PDKIM_VERIFY_INVALID_PUBKEY_PARSING;
1539 if (ctx->debug_stream) {
1540 fprintf(ctx->debug_stream,"Error while parsing public key record\n");
1541 fprintf(ctx->debug_stream,
1542 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1549 if (ctx->debug_stream) {
1550 fprintf(ctx->debug_stream,
1551 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1555 if (rsa_parse_public_key(&rsa,
1556 (unsigned char *)sig->pubkey->key,
1557 sig->pubkey->key_len) != 0) {
1558 sig->verify_status = PDKIM_VERIFY_INVALID;
1559 sig->verify_ext_status = PDKIM_VERIFY_INVALID_PUBKEY_PARSING;
1563 /* Check the signature */
1564 if (rsa_pkcs1_verify(&rsa,
1566 ((sig->algo == PDKIM_ALGO_RSA_SHA1)?
1567 RSA_SHA1:RSA_SHA256),
1569 (unsigned char *)headerhash,
1570 (unsigned char *)sig->sigdata) != 0) {
1571 sig->verify_status = PDKIM_VERIFY_FAIL;
1572 sig->verify_ext_status = PDKIM_VERIFY_FAIL_MESSAGE;
1574 if (ctx->debug_stream) {
1575 fprintf(ctx->debug_stream, "PDKIM [%s] signature did NOT verify OK\n",
1582 /* We have a winner! */
1583 sig->verify_status = PDKIM_VERIFY_PASS;
1586 if (ctx->debug_stream) {
1587 fprintf(ctx->debug_stream, "PDKIM [%s] signature verified OK\n",
1595 free(dns_txt_reply);
1601 /* If requested, set return pointer to signature(s) */
1602 if (return_signatures != NULL) {
1603 *return_signatures = ctx->sig;
1610 /* -------------------------------------------------------------------------- */
1611 DLLEXPORT pdkim_ctx *pdkim_init_verify(int input_mode,
1612 int(*dns_txt_callback)(char *, char *)
1614 pdkim_ctx *ctx = malloc(sizeof(pdkim_ctx));
1615 if (ctx == NULL) return NULL;
1616 memset(ctx,0,sizeof(pdkim_ctx));
1618 ctx->linebuf = malloc(PDKIM_MAX_BODY_LINE_LEN);
1619 if (ctx->linebuf == NULL) {
1624 ctx->mode = PDKIM_MODE_VERIFY;
1625 ctx->input_mode = input_mode;
1626 ctx->dns_txt_callback = dns_txt_callback;
1632 /* -------------------------------------------------------------------------- */
1633 DLLEXPORT pdkim_ctx *pdkim_init_sign(int input_mode,
1636 char *rsa_privkey) {
1638 pdkim_signature *sig;
1640 if (!domain || !selector || !rsa_privkey) return NULL;
1642 ctx = malloc(sizeof(pdkim_ctx));
1643 if (ctx == NULL) return NULL;
1644 memset(ctx,0,sizeof(pdkim_ctx));
1646 ctx->linebuf = malloc(PDKIM_MAX_BODY_LINE_LEN);
1647 if (ctx->linebuf == NULL) {
1652 sig = malloc(sizeof(pdkim_signature));
1658 memset(sig,0,sizeof(pdkim_signature));
1659 sig->bodylength = -1;
1661 ctx->mode = PDKIM_MODE_SIGN;
1662 ctx->input_mode = input_mode;
1665 ctx->sig->domain = strdup(domain);
1666 ctx->sig->selector = strdup(selector);
1667 ctx->sig->rsa_privkey = strdup(rsa_privkey);
1669 if (!ctx->sig->domain || !ctx->sig->selector || !ctx->sig->rsa_privkey) {
1670 pdkim_free_ctx(ctx);
1674 ctx->sig->sha1_body = malloc(sizeof(sha1_context));
1675 if (ctx->sig->sha1_body == NULL) {
1676 pdkim_free_ctx(ctx);
1679 sha1_starts(ctx->sig->sha1_body);
1681 ctx->sig->sha2_body = malloc(sizeof(sha2_context));
1682 if (ctx->sig->sha2_body == NULL) {
1683 pdkim_free_ctx(ctx);
1686 sha2_starts(ctx->sig->sha2_body,0);
1692 /* -------------------------------------------------------------------------- */
1693 DLLEXPORT void pdkim_set_debug_stream(pdkim_ctx *ctx,
1694 FILE *debug_stream) {
1695 ctx->debug_stream = debug_stream;
1699 /* -------------------------------------------------------------------------- */
1700 DLLEXPORT int pdkim_set_optional(pdkim_ctx *ctx,
1707 unsigned long created,
1708 unsigned long expires) {
1710 if (identity != NULL) {
1711 ctx->sig->identity = strdup(identity);
1712 if (ctx->sig->identity == NULL) return PDKIM_ERR_OOM;
1715 if (sign_headers != NULL) {
1716 ctx->sig->sign_headers = strdup(sign_headers);
1717 if (ctx->sig->sign_headers == NULL) return PDKIM_ERR_OOM;
1720 ctx->sig->canon_headers = canon_headers;
1721 ctx->sig->canon_body = canon_body;
1722 ctx->sig->bodylength = bodylength;
1723 ctx->sig->algo = algo;
1724 ctx->sig->created = created;
1725 ctx->sig->expires = expires;