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.12 2009/12/07 13:05:07 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 char *pdkim_verify_status_str(int status) {
110 case PDKIM_VERIFY_NONE: return "PDKIM_VERIFY_NONE";
111 case PDKIM_VERIFY_INVALID: return "PDKIM_VERIFY_INVALID";
112 case PDKIM_VERIFY_FAIL: return "PDKIM_VERIFY_FAIL";
113 case PDKIM_VERIFY_PASS: return "PDKIM_VERIFY_PASS";
114 default: return "PDKIM_VERIFY_UNKNOWN";
117 char *pdkim_verify_ext_status_str(int ext_status) {
119 case PDKIM_VERIFY_FAIL_BODY: return "PDKIM_VERIFY_FAIL_BODY";
120 case PDKIM_VERIFY_FAIL_MESSAGE: return "PDKIM_VERIFY_FAIL_MESSAGE";
121 case PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE: return "PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE";
122 case PDKIM_VERIFY_INVALID_BUFFER_SIZE: return "PDKIM_VERIFY_INVALID_BUFFER_SIZE";
123 case PDKIM_VERIFY_INVALID_PUBKEY_PARSING: return "PDKIM_VERIFY_INVALID_PUBKEY_PARSING";
124 default: return "PDKIM_VERIFY_UNKNOWN";
129 /* -------------------------------------------------------------------------- */
130 /* Print debugging functions */
132 void pdkim_quoteprint(FILE *stream, char *data, int len, int lf) {
134 unsigned char *p = (unsigned char *)data;
136 for (i=0;i<len;i++) {
139 case ' ' : fprintf(stream,"{SP}"); break;
140 case '\t': fprintf(stream,"{TB}"); break;
141 case '\r': fprintf(stream,"{CR}"); break;
142 case '\n': fprintf(stream,"{LF}"); break;
143 case '{' : fprintf(stream,"{BO}"); break;
144 case '}' : fprintf(stream,"{BC}"); break;
146 if ( (c < 32) || (c > 127) )
147 fprintf(stream,"{%02x}",c);
156 void pdkim_hexprint(FILE *stream, char *data, int len, int lf) {
158 unsigned char *p = (unsigned char *)data;
160 for (i=0;i<len;i++) {
162 fprintf(stream,"%02x",c);
170 /* -------------------------------------------------------------------------- */
171 /* Simple string list implementation for convinience */
172 pdkim_stringlist *pdkim_append_stringlist(pdkim_stringlist *base, char *str) {
173 pdkim_stringlist *new_entry = malloc(sizeof(pdkim_stringlist));
174 if (new_entry == NULL) return NULL;
175 memset(new_entry,0,sizeof(pdkim_stringlist));
176 new_entry->value = strdup(str);
177 if (new_entry->value == NULL) return NULL;
179 pdkim_stringlist *last = base;
180 while (last->next != NULL) { last = last->next; }
181 last->next = new_entry;
184 else return new_entry;
186 pdkim_stringlist *pdkim_prepend_stringlist(pdkim_stringlist *base, char *str) {
187 pdkim_stringlist *new_entry = malloc(sizeof(pdkim_stringlist));
188 if (new_entry == NULL) return NULL;
189 memset(new_entry,0,sizeof(pdkim_stringlist));
190 new_entry->value = strdup(str);
191 if (new_entry->value == NULL) return NULL;
193 new_entry->next = base;
199 /* -------------------------------------------------------------------------- */
200 /* A small "growing string" implementation to escape malloc/realloc hell */
201 pdkim_str *pdkim_strnew (char *cstr) {
202 unsigned int len = cstr?strlen(cstr):0;
203 pdkim_str *p = malloc(sizeof(pdkim_str));
204 if (p == NULL) return NULL;
205 memset(p,0,sizeof(pdkim_str));
206 p->str = malloc(len+1);
207 if (p->str == NULL) {
211 p->allocated=(len+1);
213 if (cstr) strcpy(p->str,cstr);
216 char *pdkim_strncat(pdkim_str *str, char *data, int len) {
217 if ((str->allocated - str->len) < (len+1)) {
218 /* Extend the buffer */
219 int num_frags = ((len+1)/PDKIM_STR_ALLOC_FRAG)+1;
220 char *n = realloc(str->str,
221 (str->allocated+(num_frags*PDKIM_STR_ALLOC_FRAG)));
222 if (n == NULL) return NULL;
224 str->allocated += (num_frags*PDKIM_STR_ALLOC_FRAG);
226 strncpy(&(str->str[str->len]),data,len);
228 str->str[str->len] = '\0';
231 char *pdkim_strcat(pdkim_str *str, char *cstr) {
232 return pdkim_strncat(str, cstr, strlen(cstr));
234 char *pdkim_numcat(pdkim_str *str, unsigned long num) {
236 snprintf(minibuf,20,"%lu",num);
237 return pdkim_strcat(str,minibuf);
239 char *pdkim_strtrim(pdkim_str *str) {
242 while ( (*p != '\0') && ((*p == '\t') || (*p == ' ')) ) p++;
243 while (*p != '\0') {*q = *p; q++; p++;}
245 while ( (q != str->str) && ( (*q == '\0') || (*q == '\t') || (*q == ' ') ) ) {
249 str->len = strlen(str->str);
252 char *pdkim_strclear(pdkim_str *str) {
257 void pdkim_strfree(pdkim_str *str) {
258 if (str == NULL) return;
259 if (str->str != NULL) free(str->str);
265 /* -------------------------------------------------------------------------- */
266 void pdkim_free_pubkey(pdkim_pubkey *pub) {
268 if (pub->version != NULL) free(pub->version);
269 if (pub->granularity != NULL) free(pub->granularity);
270 if (pub->hashes != NULL) free(pub->hashes);
271 if (pub->keytype != NULL) free(pub->keytype);
272 if (pub->srvtype != NULL) free(pub->srvtype);
273 if (pub->notes != NULL) free(pub->notes);
274 if (pub->key != NULL) free(pub->key);
280 /* -------------------------------------------------------------------------- */
281 void pdkim_free_sig(pdkim_signature *sig) {
283 pdkim_signature *next = (pdkim_signature *)sig->next;
285 pdkim_stringlist *e = sig->headers;
287 pdkim_stringlist *c = e;
288 if (e->value != NULL) free(e->value);
293 if (sig->sigdata != NULL) free(sig->sigdata);
294 if (sig->bodyhash != NULL) free(sig->bodyhash);
295 if (sig->selector != NULL) free(sig->selector);
296 if (sig->domain != NULL) free(sig->domain);
297 if (sig->identity != NULL) free(sig->identity);
298 if (sig->headernames != NULL) free(sig->headernames);
299 if (sig->copiedheaders != NULL) free(sig->copiedheaders);
300 if (sig->rsa_privkey != NULL) free(sig->rsa_privkey);
301 if (sig->sign_headers != NULL) free(sig->sign_headers);
302 if (sig->signature_header != NULL) free(sig->signature_header);
303 if (sig->sha1_body != NULL) free(sig->sha1_body);
304 if (sig->sha2_body != NULL) free(sig->sha2_body);
305 if (sig->hnames_check != NULL) free(sig->hnames_check);
307 if (sig->pubkey != NULL) pdkim_free_pubkey(sig->pubkey);
310 if (next != NULL) pdkim_free_sig(next);
315 /* -------------------------------------------------------------------------- */
316 DLLEXPORT void pdkim_free_ctx(pdkim_ctx *ctx) {
318 pdkim_free_sig(ctx->sig);
319 pdkim_strfree(ctx->cur_header);
325 /* -------------------------------------------------------------------------- */
326 /* Matches the name of the passed raw "header" against
327 the passed colon-separated "list", starting at entry
328 "start". Returns the position of the header name in
330 int header_name_match(char *header,
339 /* Get header name */
340 char *hcolon = strchr(header,':');
341 if (hcolon == NULL) return rc; /* This isn't a header */
342 hname = malloc((hcolon-header)+1);
343 if (hname == NULL) return PDKIM_ERR_OOM;
344 memset(hname,0,(hcolon-header)+1);
345 strncpy(hname,header,(hcolon-header));
347 /* Copy tick-off list locally, so we can punch zeroes into it */
348 lcopy = strdup(tick);
351 return PDKIM_ERR_OOM;
358 if (strcasecmp(p,hname) == 0) {
360 /* Invalidate header name instance in tick-off list */
361 if (do_tick) tick[p-lcopy] = '_';
369 if (strcasecmp(p,hname) == 0) {
371 /* Invalidate header name instance in tick-off list */
372 if (do_tick) tick[p-lcopy] = '_';
382 /* -------------------------------------------------------------------------- */
383 /* Performs "relaxed" canonicalization of a header. The returned pointer needs
385 char *pdkim_relax_header (char *header, int crlf) {
386 int past_field_name = 0;
390 char *relaxed = malloc(strlen(header)+3);
391 if (relaxed == NULL) return NULL;
396 if ( (c == '\r') || (c == '\n') ) {
400 if ( (c == '\t') || (c == ' ') ) {
401 c = ' '; /* Turns WSP into SP */
409 if ( (!past_field_name) && (c == ':') ) {
410 if (seen_wsp) q--; /* This removes WSP before the colon */
411 seen_wsp = 1; /* This removes WSP after the colon */
416 /* Lowercase header name */
417 if (!past_field_name) c = tolower(c);
423 if (crlf) strcat(relaxed,"\r\n");
428 /* -------------------------------------------------------------------------- */
429 #define PDKIM_QP_ERROR_DECODE -1
430 char *pdkim_decode_qp_char(char *qp_p, int *c) {
431 char *initial_pos = qp_p;
433 /* Advance one char */
436 /* Check for two hex digits and decode them */
437 if (isxdigit(*qp_p) && isxdigit(qp_p[1])) {
438 /* Do hex conversion */
439 if (isdigit(*qp_p)) {*c = *qp_p - '0';}
440 else {*c = toupper(*qp_p) - 'A' + 10;}
442 if (isdigit(qp_p[1])) {*c |= qp_p[1] - '0';}
443 else {*c |= toupper(qp_p[1]) - 'A' + 10;}
447 /* Illegal char here */
448 *c = PDKIM_QP_ERROR_DECODE;
453 /* -------------------------------------------------------------------------- */
454 char *pdkim_decode_qp(char *str) {
458 char *n = malloc(strlen(p)+1);
459 if (n == NULL) return NULL;
464 p = pdkim_decode_qp_char(p,&nchar);
482 /* -------------------------------------------------------------------------- */
483 char *pdkim_decode_base64(char *str, int *num_decoded) {
487 base64_decode(NULL, &dlen, (unsigned char *)str, strlen(str));
488 res = malloc(dlen+1);
489 if (res == NULL) return NULL;
490 if (base64_decode((unsigned char *)res,&dlen,(unsigned char *)str,strlen(str)) != 0) {
494 if (num_decoded != NULL) *num_decoded = dlen;
498 /* -------------------------------------------------------------------------- */
499 char *pdkim_encode_base64(char *str, int num) {
503 base64_encode(NULL, &dlen, (unsigned char *)str, num);
504 res = malloc(dlen+1);
505 if (res == NULL) return NULL;
506 if (base64_encode((unsigned char *)res,&dlen,(unsigned char *)str,num) != 0) {
514 /* -------------------------------------------------------------------------- */
515 #define PDKIM_HDR_LIMBO 0
516 #define PDKIM_HDR_TAG 1
517 #define PDKIM_HDR_VALUE 2
518 pdkim_signature *pdkim_parse_sig_header(pdkim_ctx *ctx, char *raw_hdr) {
519 pdkim_signature *sig ;
521 pdkim_str *cur_tag = NULL;
522 pdkim_str *cur_val = NULL;
525 int where = PDKIM_HDR_LIMBO;
528 sig = malloc(sizeof(pdkim_signature));
529 if (sig == NULL) return NULL;
530 memset(sig,0,sizeof(pdkim_signature));
531 sig->bodylength = -1;
533 sig->rawsig_no_b_val = malloc(strlen(raw_hdr)+1);
534 if (sig->rawsig_no_b_val == NULL) {
540 q = sig->rawsig_no_b_val;
545 if ( (*p == '\r') || (*p == '\n') )
548 /* Fast-forward through header name */
550 if (*p == ':') past_hname = 1;
554 if (where == PDKIM_HDR_LIMBO) {
555 /* In limbo, just wait for a tag-char to appear */
556 if (!((*p >= 'a') && (*p <= 'z')))
559 where = PDKIM_HDR_TAG;
562 if (where == PDKIM_HDR_TAG) {
564 cur_tag = pdkim_strnew(NULL);
566 if ((*p >= 'a') && (*p <= 'z'))
567 pdkim_strncat(cur_tag,p,1);
570 if (strcmp(cur_tag->str,"b") == 0) {
574 where = PDKIM_HDR_VALUE;
579 if (where == PDKIM_HDR_VALUE) {
581 cur_val = pdkim_strnew(NULL);
583 if ( (*p == '\r') || (*p == '\n') || (*p == ' ') || (*p == '\t') )
586 if ( (*p == ';') || (*p == '\0') ) {
587 if (cur_tag->len > 0) {
588 pdkim_strtrim(cur_val);
590 if (ctx->debug_stream)
591 fprintf(ctx->debug_stream, "%s=%s\n", cur_tag->str, cur_val->str);
593 switch (cur_tag->str[0]) {
595 switch (cur_tag->str[1]) {
597 sig->bodyhash = pdkim_decode_base64(cur_val->str,&(sig->bodyhash_len));
600 sig->sigdata = pdkim_decode_base64(cur_val->str,&(sig->sigdata_len));
605 if (strcmp(cur_val->str,PDKIM_SIGNATURE_VERSION) == 0) {
606 /* We only support version 1, and that is currently the
607 only version there is. */
613 while (pdkim_algos[i] != NULL) {
614 if (strcmp(cur_val->str,pdkim_algos[i]) == 0 ) {
623 while (pdkim_combined_canons[i].str != NULL) {
624 if (strcmp(cur_val->str,pdkim_combined_canons[i].str) == 0 ) {
625 sig->canon_headers = pdkim_combined_canons[i].canon_headers;
626 sig->canon_body = pdkim_combined_canons[i].canon_body;
634 while (pdkim_querymethods[i] != NULL) {
635 if (strcmp(cur_val->str,pdkim_querymethods[i]) == 0 ) {
636 sig->querymethod = i;
643 sig->selector = strdup(cur_val->str);
646 sig->domain = strdup(cur_val->str);
649 sig->identity = pdkim_decode_qp(cur_val->str);
652 sig->created = strtoul(cur_val->str,NULL,10);
655 sig->expires = strtoul(cur_val->str,NULL,10);
658 sig->bodylength = strtol(cur_val->str,NULL,10);
661 sig->headernames = strdup(cur_val->str);
664 sig->copiedheaders = pdkim_decode_qp(cur_val->str);
668 if (ctx->debug_stream)
669 fprintf(ctx->debug_stream, "Unknown tag encountered\n");
674 pdkim_strclear(cur_tag);
675 pdkim_strclear(cur_val);
677 where = PDKIM_HDR_LIMBO;
680 else pdkim_strncat(cur_val,p,1);
684 if (*p == '\0') break;
693 /* Make sure the most important bits are there. */
694 if (!(sig->domain && (*(sig->domain) != '\0') &&
695 sig->selector && (*(sig->selector) != '\0') &&
696 sig->headernames && (*(sig->headernames) != '\0') &&
704 /* Copy header list to 'tick-off' header list */
705 sig->hnames_check = strdup(sig->headernames);
708 /* Chomp raw header. The final newline must not be added to the signature. */
710 while( (q > sig->rawsig_no_b_val) && ((*q == '\r') || (*q == '\n')) ) {
715 if (ctx->debug_stream) {
716 fprintf(ctx->debug_stream,
717 "PDKIM >> Raw signature w/o b= tag value >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
718 pdkim_quoteprint(ctx->debug_stream,
719 sig->rawsig_no_b_val,
720 strlen(sig->rawsig_no_b_val), 1);
721 fprintf(ctx->debug_stream,
722 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
726 sig->sha1_body = malloc(sizeof(sha1_context));
727 if (sig->sha1_body == NULL) {
731 sig->sha2_body = malloc(sizeof(sha2_context));
732 if (sig->sha2_body == NULL) {
737 sha1_starts(sig->sha1_body);
738 sha2_starts(sig->sha2_body,0);
744 /* -------------------------------------------------------------------------- */
745 pdkim_pubkey *pdkim_parse_pubkey_record(pdkim_ctx *ctx, char *raw_record) {
748 pdkim_str *cur_tag = NULL;
749 pdkim_str *cur_val = NULL;
750 int where = PDKIM_HDR_LIMBO;
752 pub = malloc(sizeof(pdkim_pubkey));
753 if (pub == NULL) return NULL;
754 memset(pub,0,sizeof(pdkim_pubkey));
761 if ( (*p == '\r') || (*p == '\n') )
764 if (where == PDKIM_HDR_LIMBO) {
765 /* In limbo, just wait for a tag-char to appear */
766 if (!((*p >= 'a') && (*p <= 'z')))
769 where = PDKIM_HDR_TAG;
772 if (where == PDKIM_HDR_TAG) {
774 cur_tag = pdkim_strnew(NULL);
776 if ((*p >= 'a') && (*p <= 'z'))
777 pdkim_strncat(cur_tag,p,1);
780 where = PDKIM_HDR_VALUE;
785 if (where == PDKIM_HDR_VALUE) {
787 cur_val = pdkim_strnew(NULL);
789 if ( (*p == '\r') || (*p == '\n') )
792 if ( (*p == ';') || (*p == '\0') ) {
793 if (cur_tag->len > 0) {
794 pdkim_strtrim(cur_val);
796 if (ctx->debug_stream)
797 fprintf(ctx->debug_stream, "%s=%s\n", cur_tag->str, cur_val->str);
799 switch (cur_tag->str[0]) {
801 /* This tag isn't evaluated because:
802 - We only support version DKIM1.
803 - Which is the default for this value (set below)
804 - Other versions are currently not specified. */
807 pub->hashes = strdup(cur_val->str);
810 pub->granularity = strdup(cur_val->str);
813 pub->notes = pdkim_decode_qp(cur_val->str);
816 pub->key = pdkim_decode_base64(cur_val->str,&(pub->key_len));
819 pub->hashes = strdup(cur_val->str);
822 pub->srvtype = strdup(cur_val->str);
825 if (strchr(cur_val->str,'y') != NULL) pub->testing = 1;
826 if (strchr(cur_val->str,'s') != NULL) pub->no_subdomaining = 1;
830 if (ctx->debug_stream)
831 fprintf(ctx->debug_stream, "Unknown tag encountered\n");
836 pdkim_strclear(cur_tag);
837 pdkim_strclear(cur_val);
838 where = PDKIM_HDR_LIMBO;
841 else pdkim_strncat(cur_val,p,1);
845 if (*p == '\0') break;
849 /* Set fallback defaults */
850 if (pub->version == NULL) pub->version = strdup(PDKIM_PUB_RECORD_VERSION);
851 if (pub->granularity == NULL) pub->granularity = strdup("*");
852 if (pub->keytype == NULL) pub->keytype = strdup("rsa");
853 if (pub->srvtype == NULL) pub->srvtype = strdup("*");
856 if (pub->key == NULL) {
857 pdkim_free_pubkey(pub);
865 /* -------------------------------------------------------------------------- */
866 int pdkim_update_bodyhash(pdkim_ctx *ctx, char *data, int len) {
867 pdkim_signature *sig = ctx->sig;
868 /* Cache relaxed version of data */
869 char *relaxed_data = NULL;
872 /* Traverse all signatures, updating their hashes. */
873 while (sig != NULL) {
874 /* Defaults to simple canon (no further treatment necessary) */
875 char *canon_data = data;
878 if (sig->canon_body == PDKIM_CANON_RELAXED) {
879 /* Relax the line if not done already */
880 if (relaxed_data == NULL) {
884 relaxed_data = malloc(len+1);
885 if (relaxed_data == NULL) return PDKIM_ERR_OOM;
889 if ( (q > 0) && (relaxed_data[q-1] == ' ') ) q--;
891 else if ( (c == '\t') || (c == ' ') ) {
892 c = ' '; /* Turns WSP into SP */
900 relaxed_data[q++] = c;
903 relaxed_data[q] = '\0';
906 canon_data = relaxed_data;
907 canon_len = relaxed_len;
910 /* Make sure we don't exceed the to-be-signed body length */
911 if ((sig->bodylength >= 0) &&
912 ((sig->signed_body_bytes+(unsigned long)canon_len) > sig->bodylength))
913 canon_len = (sig->bodylength - sig->signed_body_bytes);
916 if (sig->algo == PDKIM_ALGO_RSA_SHA1)
917 sha1_update(sig->sha1_body,(unsigned char *)canon_data,canon_len);
919 sha2_update(sig->sha2_body,(unsigned char *)canon_data,canon_len);
920 sig->signed_body_bytes += canon_len;
922 if (ctx->debug_stream!=NULL)
923 pdkim_quoteprint(ctx->debug_stream,canon_data,canon_len,0);
930 if (relaxed_data != NULL) free(relaxed_data);
935 /* -------------------------------------------------------------------------- */
936 int pdkim_finish_bodyhash(pdkim_ctx *ctx) {
937 pdkim_signature *sig = ctx->sig;
939 /* Traverse all signatures */
940 while (sig != NULL) {
943 unsigned char bh[32]; /* SHA-256 = 32 Bytes, SHA-1 = 20 Bytes */
944 if (sig->algo == PDKIM_ALGO_RSA_SHA1)
945 sha1_finish(sig->sha1_body,bh);
947 sha2_finish(sig->sha2_body,bh);
950 if (ctx->debug_stream) {
951 fprintf(ctx->debug_stream, "PDKIM [%s] Body bytes hashed: %lu\n",
952 sig->domain, sig->signed_body_bytes);
953 fprintf(ctx->debug_stream, "PDKIM [%s] bh computed: ", sig->domain);
954 pdkim_hexprint(ctx->debug_stream, (char *)bh,
955 (sig->algo == PDKIM_ALGO_RSA_SHA1)?20:32,1);
959 /* SIGNING -------------------------------------------------------------- */
960 if (ctx->mode == PDKIM_MODE_SIGN) {
961 sig->bodyhash_len = (sig->algo == PDKIM_ALGO_RSA_SHA1)?20:32;
962 sig->bodyhash = malloc(sig->bodyhash_len);
963 if (sig->bodyhash == NULL) return PDKIM_ERR_OOM;
964 memcpy(sig->bodyhash,bh,sig->bodyhash_len);
966 /* If bodylength limit is set, and we have received less bytes
967 than the requested amount, effectively remove the limit tag. */
968 if (sig->signed_body_bytes < sig->bodylength) sig->bodylength = -1;
970 /* VERIFICATION --------------------------------------------------------- */
972 /* Compare bodyhash */
973 if (memcmp(bh,sig->bodyhash,
974 (sig->algo == PDKIM_ALGO_RSA_SHA1)?20:32) == 0) {
976 if (ctx->debug_stream)
977 fprintf(ctx->debug_stream, "PDKIM [%s] Body hash verified OK\n",
983 if (ctx->debug_stream) {
984 fprintf(ctx->debug_stream, "PDKIM [%s] Body hash did NOT verify\n",
986 fprintf(ctx->debug_stream, "PDKIM [%s] bh signature: ", sig->domain);
987 pdkim_hexprint(ctx->debug_stream, sig->bodyhash,
988 (sig->algo == PDKIM_ALGO_RSA_SHA1)?20:32,1);
991 sig->verify_status = PDKIM_VERIFY_FAIL;
992 sig->verify_ext_status = PDKIM_VERIFY_FAIL_BODY;
1004 /* -------------------------------------------------------------------------- */
1005 /* Callback from pdkim_feed below for processing complete body lines */
1006 int pdkim_bodyline_complete(pdkim_ctx *ctx) {
1007 char *p = ctx->linebuf;
1008 int n = ctx->linebuf_offset;
1010 /* Ignore extra data if we've seen the end-of-data marker */
1011 if (ctx->seen_eod) goto BAIL;
1013 /* We've always got one extra byte to stuff a zero ... */
1014 ctx->linebuf[(ctx->linebuf_offset)] = '\0';
1016 if (ctx->input_mode == PDKIM_INPUT_SMTP) {
1017 /* Terminate on EOD marker */
1018 if (memcmp(p,".\r\n",3) == 0) {
1023 if (memcmp(p,"..",2) == 0) {
1029 /* Empty lines need to be buffered until we find a non-empty line */
1030 if (memcmp(p,"\r\n",2) == 0) {
1031 ctx->num_buffered_crlf++;
1035 /* At this point, we have a non-empty line, so release the buffered ones. */
1036 while (ctx->num_buffered_crlf) {
1037 pdkim_update_bodyhash(ctx,"\r\n",2);
1038 ctx->num_buffered_crlf--;
1041 pdkim_update_bodyhash(ctx,p,n);
1044 ctx->linebuf_offset = 0;
1049 /* -------------------------------------------------------------------------- */
1050 /* Callback from pdkim_feed below for processing complete headers */
1051 #define DKIM_SIGNATURE_HEADERNAME "DKIM-Signature:"
1052 int pdkim_header_complete(pdkim_ctx *ctx) {
1053 pdkim_signature *sig = ctx->sig;
1055 /* Special case: The last header can have an extra \r appended */
1056 if ( (ctx->cur_header->len > 1) &&
1057 (ctx->cur_header->str[(ctx->cur_header->len)-1] == '\r') ) {
1058 ctx->cur_header->str[(ctx->cur_header->len)-1] = '\0';
1059 ctx->cur_header->len--;
1063 if (ctx->num_headers > PDKIM_MAX_HEADERS) goto BAIL;
1065 /* Traverse all signatures */
1066 while (sig != NULL) {
1067 pdkim_stringlist *list;
1069 /* SIGNING -------------------------------------------------------------- */
1070 if (ctx->mode == PDKIM_MODE_SIGN) {
1071 if (header_name_match(ctx->cur_header->str,
1074 PDKIM_DEFAULT_SIGN_HEADERS, 0) != PDKIM_OK) goto NEXT_SIG;
1076 /* VERIFICATION --------------------------------------------------------- */
1078 /* Header is not included or all instances were already 'ticked off' */
1079 if (header_name_match(ctx->cur_header->str,
1080 sig->hnames_check, 1) != PDKIM_OK) goto NEXT_SIG;
1083 /* Add header to the signed headers list (in reverse order) */
1084 list = pdkim_prepend_stringlist(sig->headers,
1085 ctx->cur_header->str);
1086 if (list == NULL) return PDKIM_ERR_OOM;
1087 sig->headers = list;
1093 /* DKIM-Signature: headers are added to the verification list */
1094 if ( (ctx->mode == PDKIM_MODE_VERIFY) &&
1095 (strncasecmp(ctx->cur_header->str,
1096 DKIM_SIGNATURE_HEADERNAME,
1097 strlen(DKIM_SIGNATURE_HEADERNAME)) == 0) ) {
1098 pdkim_signature *new_sig;
1099 /* Create and chain new signature block */
1101 if (ctx->debug_stream)
1102 fprintf(ctx->debug_stream,
1103 "PDKIM >> Found sig, trying to parse >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
1105 new_sig = pdkim_parse_sig_header(ctx, ctx->cur_header->str);
1106 if (new_sig != NULL) {
1107 pdkim_signature *last_sig = ctx->sig;
1108 if (last_sig == NULL) {
1112 while (last_sig->next != NULL) { last_sig = last_sig->next; }
1113 last_sig->next = new_sig;
1118 if (ctx->debug_stream) {
1119 fprintf(ctx->debug_stream,"Error while parsing signature header\n");
1120 fprintf(ctx->debug_stream,
1121 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1128 pdkim_strclear(ctx->cur_header); /* Re-use existing pdkim_str */
1134 /* -------------------------------------------------------------------------- */
1135 #define HEADER_BUFFER_FRAG_SIZE 256
1136 DLLEXPORT int pdkim_feed (pdkim_ctx *ctx,
1140 for (p=0;p<len;p++) {
1142 if (ctx->past_headers) {
1143 /* Processing body byte */
1144 ctx->linebuf[(ctx->linebuf_offset)++] = c;
1146 int rc = pdkim_bodyline_complete(ctx); /* End of line */
1147 if (rc != PDKIM_OK) return rc;
1149 if (ctx->linebuf_offset == (PDKIM_MAX_BODY_LINE_LEN-1))
1150 return PDKIM_ERR_LONG_LINE;
1153 /* Processing header byte */
1157 int rc = pdkim_header_complete(ctx); /* Seen last header line */
1158 if (rc != PDKIM_OK) return rc;
1159 ctx->past_headers = 1;
1162 if (ctx->debug_stream)
1163 fprintf(ctx->debug_stream,
1164 "PDKIM >> Hashed body data, canonicalized >>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
1168 else ctx->seen_lf = 1;
1170 else if (ctx->seen_lf) {
1171 if (! ((c == '\t') || (c == ' '))) {
1172 int rc = pdkim_header_complete(ctx); /* End of header */
1173 if (rc != PDKIM_OK) return rc;
1178 if (ctx->cur_header == NULL) {
1179 ctx->cur_header = pdkim_strnew(NULL);
1180 if (ctx->cur_header == NULL) return PDKIM_ERR_OOM;
1182 if (ctx->cur_header->len < PDKIM_MAX_HEADER_LEN)
1183 if (pdkim_strncat(ctx->cur_header,&data[p],1) == NULL)
1184 return PDKIM_ERR_OOM;
1191 /* -------------------------------------------------------------------------- */
1192 char *pdkim_create_header(pdkim_signature *sig, int final) {
1194 char *base64_bh = NULL;
1195 char *base64_b = NULL;
1196 pdkim_str *hdr = pdkim_strnew("DKIM-Signature: v="PDKIM_SIGNATURE_VERSION);
1197 if (hdr == NULL) return NULL;
1199 base64_bh = pdkim_encode_base64(sig->bodyhash, sig->bodyhash_len);
1200 if (base64_bh == NULL) goto BAIL;
1202 /* Required and static bits */
1204 pdkim_strcat(hdr,"; a=") &&
1205 pdkim_strcat(hdr,pdkim_algos[sig->algo]) &&
1206 pdkim_strcat(hdr,"; q=") &&
1207 pdkim_strcat(hdr,pdkim_querymethods[sig->querymethod]) &&
1208 pdkim_strcat(hdr,"; c=") &&
1209 pdkim_strcat(hdr,pdkim_canons[sig->canon_headers]) &&
1210 pdkim_strcat(hdr,"/") &&
1211 pdkim_strcat(hdr,pdkim_canons[sig->canon_body]) &&
1212 pdkim_strcat(hdr,"; d=") &&
1213 pdkim_strcat(hdr,sig->domain) &&
1214 pdkim_strcat(hdr,"; s=") &&
1215 pdkim_strcat(hdr,sig->selector) &&
1216 pdkim_strcat(hdr,";\r\n\th=") &&
1217 pdkim_strcat(hdr,sig->headernames) &&
1218 pdkim_strcat(hdr,"; bh=") &&
1219 pdkim_strcat(hdr,base64_bh) &&
1220 pdkim_strcat(hdr,";\r\n\t")
1223 if (sig->identity != NULL) {
1224 if (!( pdkim_strcat(hdr,"i=") &&
1225 pdkim_strcat(hdr,sig->identity) &&
1226 pdkim_strcat(hdr,";") ) ) {
1230 if (sig->created > 0) {
1231 if (!( pdkim_strcat(hdr,"t=") &&
1232 pdkim_numcat(hdr,sig->created) &&
1233 pdkim_strcat(hdr,";") ) ) {
1237 if (sig->expires > 0) {
1238 if (!( pdkim_strcat(hdr,"x=") &&
1239 pdkim_numcat(hdr,sig->expires) &&
1240 pdkim_strcat(hdr,";") ) ) {
1244 if (sig->bodylength >= 0) {
1245 if (!( pdkim_strcat(hdr,"l=") &&
1246 pdkim_numcat(hdr,sig->bodylength) &&
1247 pdkim_strcat(hdr,";") ) ) {
1251 /* Extra linebreak */
1252 if (hdr->str[(hdr->len)-1] == ';') {
1253 if (!pdkim_strcat(hdr," \r\n\t")) goto BAIL;
1255 /* Preliminary or final version? */
1257 base64_b = pdkim_encode_base64(sig->sigdata, sig->sigdata_len);
1258 if (base64_b == NULL) goto BAIL;
1260 pdkim_strcat(hdr,"b=") &&
1261 pdkim_strcat(hdr,base64_b) &&
1262 pdkim_strcat(hdr,";")
1266 if (pdkim_strcat(hdr,"b=;")) goto DONE;
1273 rc = strdup(hdr->str);
1277 if (base64_bh != NULL) free(base64_bh);
1278 if (base64_b != NULL) free(base64_b);
1283 /* -------------------------------------------------------------------------- */
1284 DLLEXPORT int pdkim_feed_finish(pdkim_ctx *ctx, pdkim_signature **return_signatures) {
1285 pdkim_signature *sig = ctx->sig;
1286 pdkim_str *headernames = NULL; /* Collected signed header names */
1288 /* Check if we must still flush a (partial) header. If that is the
1289 case, the message has no body, and we must compute a body hash
1290 out of '<CR><LF>' */
1291 if (ctx->cur_header && ctx->cur_header->len) {
1292 int rc = pdkim_header_complete(ctx);
1293 if (rc != PDKIM_OK) return rc;
1294 pdkim_update_bodyhash(ctx,"\r\n",2);
1297 /* For non-smtp input, check if there's an unfinished line in the
1298 body line buffer. If that is the case, we must add a CRLF to the
1299 hash to properly terminate the message. */
1300 if ((ctx->input_mode == PDKIM_INPUT_NORMAL) && ctx->linebuf_offset) {
1301 pdkim_update_bodyhash(ctx, ctx->linebuf, ctx->linebuf_offset);
1302 pdkim_update_bodyhash(ctx,"\r\n",2);
1305 if (ctx->debug_stream)
1306 fprintf(ctx->debug_stream,
1307 "\nPDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1311 /* Build (and/or evaluate) body hash */
1312 if (pdkim_finish_bodyhash(ctx) != PDKIM_OK) return PDKIM_ERR_OOM;
1314 /* SIGNING -------------------------------------------------------------- */
1315 if (ctx->mode == PDKIM_MODE_SIGN) {
1316 headernames = pdkim_strnew(NULL);
1317 if (headernames == NULL) return PDKIM_ERR_OOM;
1319 /* ---------------------------------------------------------------------- */
1321 while (sig != NULL) {
1322 sha1_context sha1_headers;
1323 sha2_context sha2_headers;
1325 char headerhash[32];
1327 if (sig->algo == PDKIM_ALGO_RSA_SHA1)
1328 sha1_starts(&sha1_headers);
1330 sha2_starts(&sha2_headers,0);
1333 if (ctx->debug_stream)
1334 fprintf(ctx->debug_stream,
1335 "PDKIM >> Hashed header data, canonicalized, in sequence >>>>>>>>>>>>>>\n");
1338 /* SIGNING ---------------------------------------------------------------- */
1339 /* When signing, walk through our header list and add them to the hash. As we
1340 go, construct a list of the header's names to use for the h= parameter. */
1341 if (ctx->mode == PDKIM_MODE_SIGN) {
1342 pdkim_stringlist *p = sig->headers;
1345 /* Collect header names (Note: colon presence is guaranteed here) */
1346 char *q = strchr(p->value,':');
1347 if (pdkim_strncat(headernames, p->value,
1348 (q-(p->value))+((p->next==NULL)?0:1)) == NULL)
1349 return PDKIM_ERR_OOM;
1351 if (sig->canon_headers == PDKIM_CANON_RELAXED)
1352 rh = pdkim_relax_header(p->value,1); /* cook header for relaxed canon */
1354 rh = strdup(p->value); /* just copy it for simple canon */
1356 if (rh == NULL) return PDKIM_ERR_OOM;
1358 /* Feed header to the hash algorithm */
1359 if (sig->algo == PDKIM_ALGO_RSA_SHA1)
1360 sha1_update(&(sha1_headers),(unsigned char *)rh,strlen(rh));
1362 sha2_update(&(sha2_headers),(unsigned char *)rh,strlen(rh));
1364 if (ctx->debug_stream)
1365 pdkim_quoteprint(ctx->debug_stream, rh, strlen(rh), 1);
1371 /* VERIFICATION ----------------------------------------------------------- */
1372 /* When verifying, walk through the header name list in the h= parameter and
1373 add the headers to the hash in that order. */
1375 char *b = strdup(sig->headernames);
1378 if (b == NULL) return PDKIM_ERR_OOM;
1381 pdkim_stringlist *hdrs = sig->headers;
1383 if (q != NULL) *q = '\0';
1384 while (hdrs != NULL) {
1385 if ( (strncasecmp(hdrs->value,p,strlen(p)) == 0) &&
1386 ((hdrs->value)[strlen(p)] == ':') ) {
1388 if (sig->canon_headers == PDKIM_CANON_RELAXED)
1389 rh = pdkim_relax_header(hdrs->value,1); /* cook header for relaxed canon */
1391 rh = strdup(hdrs->value); /* just copy it for simple canon */
1392 if (rh == NULL) return PDKIM_ERR_OOM;
1393 /* Feed header to the hash algorithm */
1394 if (sig->algo == PDKIM_ALGO_RSA_SHA1)
1395 sha1_update(&(sha1_headers),(unsigned char *)rh,strlen(rh));
1397 sha2_update(&(sha2_headers),(unsigned char *)rh,strlen(rh));
1399 if (ctx->debug_stream)
1400 pdkim_quoteprint(ctx->debug_stream, rh, strlen(rh), 1);
1403 (hdrs->value)[0] = '_';
1408 if (q == NULL) break;
1415 if (ctx->debug_stream)
1416 fprintf(ctx->debug_stream,
1417 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1420 /* SIGNING ---------------------------------------------------------------- */
1421 if (ctx->mode == PDKIM_MODE_SIGN) {
1422 /* Copy headernames to signature struct */
1423 sig->headernames = strdup(headernames->str);
1424 pdkim_strfree(headernames);
1426 /* Create signature header with b= omitted */
1427 sig_hdr = pdkim_create_header(ctx->sig,0);
1429 /* VERIFICATION ----------------------------------------------------------- */
1431 sig_hdr = strdup(sig->rawsig_no_b_val);
1433 /* ------------------------------------------------------------------------ */
1435 if (sig_hdr == NULL) return PDKIM_ERR_OOM;
1437 /* Relax header if necessary */
1438 if (sig->canon_headers == PDKIM_CANON_RELAXED) {
1439 char *relaxed_hdr = pdkim_relax_header(sig_hdr,0);
1441 if (relaxed_hdr == NULL) return PDKIM_ERR_OOM;
1442 sig_hdr = relaxed_hdr;
1446 if (ctx->debug_stream) {
1447 fprintf(ctx->debug_stream,
1448 "PDKIM >> Signed DKIM-Signature header, canonicalized >>>>>>>>>>>>>>>>>\n");
1449 pdkim_quoteprint(ctx->debug_stream, sig_hdr, strlen(sig_hdr), 1);
1450 fprintf(ctx->debug_stream,
1451 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1455 /* Finalize header hash */
1456 if (sig->algo == PDKIM_ALGO_RSA_SHA1) {
1457 sha1_update(&(sha1_headers),(unsigned char *)sig_hdr,strlen(sig_hdr));
1458 sha1_finish(&(sha1_headers),(unsigned char *)headerhash);
1460 if (ctx->debug_stream) {
1461 fprintf(ctx->debug_stream, "PDKIM [%s] hh computed: ", sig->domain);
1462 pdkim_hexprint(ctx->debug_stream, headerhash, 20, 1);
1467 sha2_update(&(sha2_headers),(unsigned char *)sig_hdr,strlen(sig_hdr));
1468 sha2_finish(&(sha2_headers),(unsigned char *)headerhash);
1470 if (ctx->debug_stream) {
1471 fprintf(ctx->debug_stream, "PDKIM [%s] hh computed: ", sig->domain);
1472 pdkim_hexprint(ctx->debug_stream, headerhash, 32, 1);
1479 /* SIGNING ---------------------------------------------------------------- */
1480 if (ctx->mode == PDKIM_MODE_SIGN) {
1483 rsa_init(&rsa,RSA_PKCS_V15,0,NULL,NULL);
1485 /* Perform private key operation */
1486 if (rsa_parse_key(&rsa, (unsigned char *)sig->rsa_privkey,
1487 strlen(sig->rsa_privkey), NULL, 0) != 0) {
1488 return PDKIM_ERR_RSA_PRIVKEY;
1491 sig->sigdata_len = mpi_size(&(rsa.N));
1492 sig->sigdata = malloc(sig->sigdata_len);
1493 if (sig->sigdata == NULL) return PDKIM_ERR_OOM;
1495 if (rsa_pkcs1_sign( &rsa, RSA_PRIVATE,
1496 ((sig->algo == PDKIM_ALGO_RSA_SHA1)?
1497 SIG_RSA_SHA1:SIG_RSA_SHA256),
1499 (unsigned char *)headerhash,
1500 (unsigned char *)sig->sigdata ) != 0) {
1501 return PDKIM_ERR_RSA_SIGNING;
1507 if (ctx->debug_stream) {
1508 fprintf(ctx->debug_stream, "PDKIM [%s] b computed: ",
1510 pdkim_hexprint(ctx->debug_stream, sig->sigdata, sig->sigdata_len, 1);
1514 sig->signature_header = pdkim_create_header(ctx->sig,1);
1515 if (sig->signature_header == NULL) return PDKIM_ERR_OOM;
1517 /* VERIFICATION ----------------------------------------------------------- */
1520 char *dns_txt_name, *dns_txt_reply;
1522 rsa_init(&rsa,RSA_PKCS_V15,0,NULL,NULL);
1524 dns_txt_name = malloc(PDKIM_DNS_TXT_MAX_NAMELEN);
1525 if (dns_txt_name == NULL) return PDKIM_ERR_OOM;
1526 dns_txt_reply = malloc(PDKIM_DNS_TXT_MAX_RECLEN);
1527 if (dns_txt_reply == NULL) {
1529 return PDKIM_ERR_OOM;
1531 memset(dns_txt_reply,0,PDKIM_DNS_TXT_MAX_RECLEN);
1532 memset(dns_txt_name ,0,PDKIM_DNS_TXT_MAX_NAMELEN);
1534 if (snprintf(dns_txt_name,PDKIM_DNS_TXT_MAX_NAMELEN,
1535 "%s._domainkey.%s.",
1536 sig->selector,sig->domain) >= PDKIM_DNS_TXT_MAX_NAMELEN) {
1537 sig->verify_status = PDKIM_VERIFY_INVALID;
1538 sig->verify_ext_status = PDKIM_VERIFY_INVALID_BUFFER_SIZE;
1542 if ((ctx->dns_txt_callback(dns_txt_name, dns_txt_reply) != PDKIM_OK) ||
1543 (dns_txt_reply[0] == '\0')) {
1544 sig->verify_status = PDKIM_VERIFY_INVALID;
1545 sig->verify_ext_status = PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE;
1550 if (ctx->debug_stream) {
1551 fprintf(ctx->debug_stream,
1552 "PDKIM >> Parsing public key record >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
1553 fprintf(ctx->debug_stream,"Raw record: ");
1554 pdkim_quoteprint(ctx->debug_stream, dns_txt_reply, strlen(dns_txt_reply), 1);
1558 sig->pubkey = pdkim_parse_pubkey_record(ctx,dns_txt_reply);
1559 if (sig->pubkey == NULL) {
1560 sig->verify_status = PDKIM_VERIFY_INVALID;
1561 sig->verify_ext_status = PDKIM_VERIFY_INVALID_PUBKEY_PARSING;
1563 if (ctx->debug_stream) {
1564 fprintf(ctx->debug_stream,"Error while parsing public key record\n");
1565 fprintf(ctx->debug_stream,
1566 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1573 if (ctx->debug_stream) {
1574 fprintf(ctx->debug_stream,
1575 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1579 if (rsa_parse_public_key(&rsa,
1580 (unsigned char *)sig->pubkey->key,
1581 sig->pubkey->key_len) != 0) {
1582 sig->verify_status = PDKIM_VERIFY_INVALID;
1583 sig->verify_ext_status = PDKIM_VERIFY_INVALID_PUBKEY_PARSING;
1587 /* Check the signature */
1588 if (rsa_pkcs1_verify(&rsa,
1590 ((sig->algo == PDKIM_ALGO_RSA_SHA1)?
1591 SIG_RSA_SHA1:SIG_RSA_SHA256),
1593 (unsigned char *)headerhash,
1594 (unsigned char *)sig->sigdata) != 0) {
1595 sig->verify_status = PDKIM_VERIFY_FAIL;
1596 sig->verify_ext_status = PDKIM_VERIFY_FAIL_MESSAGE;
1600 /* We have a winner! (if bodydhash was correct earlier) */
1601 if (sig->verify_status == PDKIM_VERIFY_NONE) {
1602 sig->verify_status = PDKIM_VERIFY_PASS;
1608 if (ctx->debug_stream) {
1609 fprintf(ctx->debug_stream, "PDKIM [%s] signature status: %s",
1610 sig->domain, pdkim_verify_status_str(sig->verify_status));
1611 if (sig->verify_ext_status > 0) {
1612 fprintf(ctx->debug_stream, " (%s)\n",
1613 pdkim_verify_ext_status_str(sig->verify_ext_status));
1616 fprintf(ctx->debug_stream, "\n");
1623 free(dns_txt_reply);
1629 /* If requested, set return pointer to signature(s) */
1630 if (return_signatures != NULL) {
1631 *return_signatures = ctx->sig;
1638 /* -------------------------------------------------------------------------- */
1639 DLLEXPORT pdkim_ctx *pdkim_init_verify(int input_mode,
1640 int(*dns_txt_callback)(char *, char *)
1642 pdkim_ctx *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 ctx->mode = PDKIM_MODE_VERIFY;
1653 ctx->input_mode = input_mode;
1654 ctx->dns_txt_callback = dns_txt_callback;
1660 /* -------------------------------------------------------------------------- */
1661 DLLEXPORT pdkim_ctx *pdkim_init_sign(int input_mode,
1664 char *rsa_privkey) {
1666 pdkim_signature *sig;
1668 if (!domain || !selector || !rsa_privkey) return NULL;
1670 ctx = malloc(sizeof(pdkim_ctx));
1671 if (ctx == NULL) return NULL;
1672 memset(ctx,0,sizeof(pdkim_ctx));
1674 ctx->linebuf = malloc(PDKIM_MAX_BODY_LINE_LEN);
1675 if (ctx->linebuf == NULL) {
1680 sig = malloc(sizeof(pdkim_signature));
1686 memset(sig,0,sizeof(pdkim_signature));
1687 sig->bodylength = -1;
1689 ctx->mode = PDKIM_MODE_SIGN;
1690 ctx->input_mode = input_mode;
1693 ctx->sig->domain = strdup(domain);
1694 ctx->sig->selector = strdup(selector);
1695 ctx->sig->rsa_privkey = strdup(rsa_privkey);
1697 if (!ctx->sig->domain || !ctx->sig->selector || !ctx->sig->rsa_privkey) {
1698 pdkim_free_ctx(ctx);
1702 ctx->sig->sha1_body = malloc(sizeof(sha1_context));
1703 if (ctx->sig->sha1_body == NULL) {
1704 pdkim_free_ctx(ctx);
1707 sha1_starts(ctx->sig->sha1_body);
1709 ctx->sig->sha2_body = malloc(sizeof(sha2_context));
1710 if (ctx->sig->sha2_body == NULL) {
1711 pdkim_free_ctx(ctx);
1714 sha2_starts(ctx->sig->sha2_body,0);
1720 /* -------------------------------------------------------------------------- */
1721 DLLEXPORT void pdkim_set_debug_stream(pdkim_ctx *ctx,
1722 FILE *debug_stream) {
1723 ctx->debug_stream = debug_stream;
1727 /* -------------------------------------------------------------------------- */
1728 DLLEXPORT int pdkim_set_optional(pdkim_ctx *ctx,
1735 unsigned long created,
1736 unsigned long expires) {
1738 if (identity != NULL) {
1739 ctx->sig->identity = strdup(identity);
1740 if (ctx->sig->identity == NULL) return PDKIM_ERR_OOM;
1743 if (sign_headers != NULL) {
1744 ctx->sig->sign_headers = strdup(sign_headers);
1745 if (ctx->sig->sign_headers == NULL) return PDKIM_ERR_OOM;
1748 ctx->sig->canon_headers = canon_headers;
1749 ctx->sig->canon_body = canon_body;
1750 ctx->sig->bodylength = bodylength;
1751 ctx->sig->algo = algo;
1752 ctx->sig->created = created;
1753 ctx->sig->expires = expires;