2 * PDKIM - a RFC4871 (DKIM) implementation
4 * Copyright (C) 2009 - 2012 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.
35 #define PDKIM_SIGNATURE_VERSION "1"
36 #define PDKIM_PUB_RECORD_VERSION "DKIM1"
38 #define PDKIM_MAX_HEADER_LEN 65536
39 #define PDKIM_MAX_HEADERS 512
40 #define PDKIM_MAX_BODY_LINE_LEN 16384
41 #define PDKIM_DNS_TXT_MAX_NAMELEN 1024
42 #define PDKIM_DEFAULT_SIGN_HEADERS "From:Sender:Reply-To:Subject:Date:"\
43 "Message-ID:To:Cc:MIME-Version:Content-Type:"\
44 "Content-Transfer-Encoding:Content-ID:"\
45 "Content-Description:Resent-Date:Resent-From:"\
46 "Resent-Sender:Resent-To:Resent-Cc:"\
47 "Resent-Message-ID:In-Reply-To:References:"\
48 "List-Id:List-Help:List-Unsubscribe:"\
49 "List-Subscribe:List-Post:List-Owner:List-Archive"
51 /* -------------------------------------------------------------------------- */
52 struct pdkim_stringlist {
58 #define PDKIM_STR_ALLOC_FRAG 256
62 unsigned int allocated;
65 /* -------------------------------------------------------------------------- */
66 /* A bunch of list constants */
67 const char *pdkim_querymethods[] = {
71 const char *pdkim_algos[] = {
76 const char *pdkim_canons[] = {
81 const char *pdkim_hashes[] = {
86 const char *pdkim_keytypes[] = {
91 typedef struct pdkim_combined_canon_entry {
95 } pdkim_combined_canon_entry;
96 pdkim_combined_canon_entry pdkim_combined_canons[] = {
97 { "simple/simple", PDKIM_CANON_SIMPLE, PDKIM_CANON_SIMPLE },
98 { "simple/relaxed", PDKIM_CANON_SIMPLE, PDKIM_CANON_RELAXED },
99 { "relaxed/simple", PDKIM_CANON_RELAXED, PDKIM_CANON_SIMPLE },
100 { "relaxed/relaxed", PDKIM_CANON_RELAXED, PDKIM_CANON_RELAXED },
101 { "simple", PDKIM_CANON_SIMPLE, PDKIM_CANON_SIMPLE },
102 { "relaxed", PDKIM_CANON_RELAXED, PDKIM_CANON_SIMPLE },
107 const char *pdkim_verify_status_str(int status) {
109 case PDKIM_VERIFY_NONE: return "PDKIM_VERIFY_NONE";
110 case PDKIM_VERIFY_INVALID: return "PDKIM_VERIFY_INVALID";
111 case PDKIM_VERIFY_FAIL: return "PDKIM_VERIFY_FAIL";
112 case PDKIM_VERIFY_PASS: return "PDKIM_VERIFY_PASS";
113 default: return "PDKIM_VERIFY_UNKNOWN";
116 const char *pdkim_verify_ext_status_str(int ext_status) {
118 case PDKIM_VERIFY_FAIL_BODY: return "PDKIM_VERIFY_FAIL_BODY";
119 case PDKIM_VERIFY_FAIL_MESSAGE: return "PDKIM_VERIFY_FAIL_MESSAGE";
120 case PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE: return "PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE";
121 case PDKIM_VERIFY_INVALID_BUFFER_SIZE: return "PDKIM_VERIFY_INVALID_BUFFER_SIZE";
122 case PDKIM_VERIFY_INVALID_PUBKEY_PARSING: return "PDKIM_VERIFY_INVALID_PUBKEY_PARSING";
123 default: return "PDKIM_VERIFY_UNKNOWN";
128 /* -------------------------------------------------------------------------- */
129 /* Print debugging functions */
131 void pdkim_quoteprint(FILE *stream, const char *data, int len, int lf) {
133 const unsigned char *p = (const unsigned char *)data;
135 for (i=0;i<len;i++) {
138 case ' ' : fprintf(stream,"{SP}"); break;
139 case '\t': fprintf(stream,"{TB}"); break;
140 case '\r': fprintf(stream,"{CR}"); break;
141 case '\n': fprintf(stream,"{LF}"); break;
142 case '{' : fprintf(stream,"{BO}"); break;
143 case '}' : fprintf(stream,"{BC}"); break;
145 if ( (c < 32) || (c > 127) )
146 fprintf(stream,"{%02x}",c);
155 void pdkim_hexprint(FILE *stream, const char *data, int len, int lf) {
157 const unsigned char *p = (const unsigned char *)data;
159 for (i=0;i<len;i++) {
161 fprintf(stream,"%02x",c);
169 /* -------------------------------------------------------------------------- */
170 /* Simple string list implementation for convinience */
171 pdkim_stringlist *pdkim_append_stringlist(pdkim_stringlist *base, char *str) {
172 pdkim_stringlist *new_entry = malloc(sizeof(pdkim_stringlist));
173 if (new_entry == NULL) return NULL;
174 memset(new_entry,0,sizeof(pdkim_stringlist));
175 new_entry->value = strdup(str);
176 if (new_entry->value == NULL) return NULL;
178 pdkim_stringlist *last = base;
179 while (last->next != NULL) { last = last->next; }
180 last->next = new_entry;
183 else return new_entry;
185 pdkim_stringlist *pdkim_prepend_stringlist(pdkim_stringlist *base, char *str) {
186 pdkim_stringlist *new_entry = malloc(sizeof(pdkim_stringlist));
187 if (new_entry == NULL) return NULL;
188 memset(new_entry,0,sizeof(pdkim_stringlist));
189 new_entry->value = strdup(str);
190 if (new_entry->value == NULL) return NULL;
192 new_entry->next = base;
198 /* -------------------------------------------------------------------------- */
199 /* A small "growing string" implementation to escape malloc/realloc hell */
200 pdkim_str *pdkim_strnew (const char *cstr) {
201 unsigned int len = cstr?strlen(cstr):0;
202 pdkim_str *p = malloc(sizeof(pdkim_str));
203 if (p == NULL) return NULL;
204 memset(p,0,sizeof(pdkim_str));
205 p->str = malloc(len+1);
206 if (p->str == NULL) {
210 p->allocated=(len+1);
212 if (cstr) strcpy(p->str,cstr);
213 else p->str[p->len] = '\0';
216 char *pdkim_strncat(pdkim_str *str, const 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, const 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);
306 if (sig->pubkey != NULL) pdkim_free_pubkey(sig->pubkey);
309 if (next != NULL) pdkim_free_sig(next);
314 /* -------------------------------------------------------------------------- */
315 DLLEXPORT void pdkim_free_ctx(pdkim_ctx *ctx) {
317 pdkim_stringlist *e = ctx->headers;
319 pdkim_stringlist *c = e;
320 if (e->value != NULL) free(e->value);
324 pdkim_free_sig(ctx->sig);
325 pdkim_strfree(ctx->cur_header);
331 /* -------------------------------------------------------------------------- */
332 /* Matches the name of the passed raw "header" against
333 the passed colon-separated "list", starting at entry
334 "start". Returns the position of the header name in
336 int header_name_match(const char *header,
345 /* Get header name */
346 char *hcolon = strchr(header,':');
347 if (hcolon == NULL) return rc; /* This isn't a header */
348 hname = malloc((hcolon-header)+1);
349 if (hname == NULL) return PDKIM_ERR_OOM;
350 memset(hname,0,(hcolon-header)+1);
351 strncpy(hname,header,(hcolon-header));
353 /* Copy tick-off list locally, so we can punch zeroes into it */
354 lcopy = strdup(tick);
357 return PDKIM_ERR_OOM;
364 if (strcasecmp(p,hname) == 0) {
366 /* Invalidate header name instance in tick-off list */
367 if (do_tick) tick[p-lcopy] = '_';
375 if (strcasecmp(p,hname) == 0) {
377 /* Invalidate header name instance in tick-off list */
378 if (do_tick) tick[p-lcopy] = '_';
388 /* -------------------------------------------------------------------------- */
389 /* Performs "relaxed" canonicalization of a header. The returned pointer needs
391 char *pdkim_relax_header (char *header, int crlf) {
392 int past_field_name = 0;
396 char *relaxed = malloc(strlen(header)+3);
397 if (relaxed == NULL) return NULL;
402 if ( (c == '\r') || (c == '\n') ) {
406 if ( (c == '\t') || (c == ' ') ) {
407 c = ' '; /* Turns WSP into SP */
415 if ( (!past_field_name) && (c == ':') ) {
416 if (seen_wsp) q--; /* This removes WSP before the colon */
417 seen_wsp = 1; /* This removes WSP after the colon */
422 /* Lowercase header name */
423 if (!past_field_name) c = tolower(c);
428 if ((q>relaxed) && (*(q-1) == ' ')) q--; /* Squash eventual trailing SP */
430 if (crlf) strcat(relaxed,"\r\n");
435 /* -------------------------------------------------------------------------- */
436 #define PDKIM_QP_ERROR_DECODE -1
437 char *pdkim_decode_qp_char(char *qp_p, int *c) {
438 char *initial_pos = qp_p;
440 /* Advance one char */
443 /* Check for two hex digits and decode them */
444 if (isxdigit(*qp_p) && isxdigit(qp_p[1])) {
445 /* Do hex conversion */
446 if (isdigit(*qp_p)) {*c = *qp_p - '0';}
447 else {*c = toupper(*qp_p) - 'A' + 10;}
449 if (isdigit(qp_p[1])) {*c |= qp_p[1] - '0';}
450 else {*c |= toupper(qp_p[1]) - 'A' + 10;}
454 /* Illegal char here */
455 *c = PDKIM_QP_ERROR_DECODE;
460 /* -------------------------------------------------------------------------- */
461 char *pdkim_decode_qp(char *str) {
465 char *n = malloc(strlen(p)+1);
466 if (n == NULL) return NULL;
471 p = pdkim_decode_qp_char(p,&nchar);
489 /* -------------------------------------------------------------------------- */
490 char *pdkim_decode_base64(char *str, int *num_decoded) {
494 base64_decode(NULL, &dlen, (unsigned char *)str, strlen(str));
495 res = malloc(dlen+1);
496 if (res == NULL) return NULL;
497 if (base64_decode((unsigned char *)res,&dlen,(unsigned char *)str,strlen(str)) != 0) {
501 if (num_decoded != NULL) *num_decoded = dlen;
505 /* -------------------------------------------------------------------------- */
506 char *pdkim_encode_base64(char *str, int num) {
510 base64_encode(NULL, &dlen, (unsigned char *)str, num);
511 res = malloc(dlen+1);
512 if (res == NULL) return NULL;
513 if (base64_encode((unsigned char *)res,&dlen,(unsigned char *)str,num) != 0) {
521 /* -------------------------------------------------------------------------- */
522 #define PDKIM_HDR_LIMBO 0
523 #define PDKIM_HDR_TAG 1
524 #define PDKIM_HDR_VALUE 2
525 pdkim_signature *pdkim_parse_sig_header(pdkim_ctx *ctx, char *raw_hdr) {
526 pdkim_signature *sig ;
528 pdkim_str *cur_tag = NULL;
529 pdkim_str *cur_val = NULL;
532 int where = PDKIM_HDR_LIMBO;
535 sig = malloc(sizeof(pdkim_signature));
536 if (sig == NULL) return NULL;
537 memset(sig,0,sizeof(pdkim_signature));
538 sig->bodylength = -1;
540 sig->rawsig_no_b_val = malloc(strlen(raw_hdr)+1);
541 if (sig->rawsig_no_b_val == NULL) {
547 q = sig->rawsig_no_b_val;
552 if ( (*p == '\r') || (*p == '\n') )
555 /* Fast-forward through header name */
557 if (*p == ':') past_hname = 1;
561 if (where == PDKIM_HDR_LIMBO) {
562 /* In limbo, just wait for a tag-char to appear */
563 if (!((*p >= 'a') && (*p <= 'z')))
566 where = PDKIM_HDR_TAG;
569 if (where == PDKIM_HDR_TAG) {
571 cur_tag = pdkim_strnew(NULL);
573 if ((*p >= 'a') && (*p <= 'z'))
574 pdkim_strncat(cur_tag,p,1);
577 if (strcmp(cur_tag->str,"b") == 0) {
581 where = PDKIM_HDR_VALUE;
586 if (where == PDKIM_HDR_VALUE) {
588 cur_val = pdkim_strnew(NULL);
590 if ( (*p == '\r') || (*p == '\n') || (*p == ' ') || (*p == '\t') )
593 if ( (*p == ';') || (*p == '\0') ) {
594 if (cur_tag->len > 0) {
595 pdkim_strtrim(cur_val);
597 if (ctx->debug_stream)
598 fprintf(ctx->debug_stream, "%s=%s\n", cur_tag->str, cur_val->str);
600 switch (cur_tag->str[0]) {
602 switch (cur_tag->str[1]) {
604 sig->bodyhash = pdkim_decode_base64(cur_val->str,&(sig->bodyhash_len));
607 sig->sigdata = pdkim_decode_base64(cur_val->str,&(sig->sigdata_len));
612 if (strcmp(cur_val->str,PDKIM_SIGNATURE_VERSION) == 0) {
613 /* We only support version 1, and that is currently the
614 only version there is. */
620 while (pdkim_algos[i] != NULL) {
621 if (strcmp(cur_val->str,pdkim_algos[i]) == 0 ) {
630 while (pdkim_combined_canons[i].str != NULL) {
631 if (strcmp(cur_val->str,pdkim_combined_canons[i].str) == 0 ) {
632 sig->canon_headers = pdkim_combined_canons[i].canon_headers;
633 sig->canon_body = pdkim_combined_canons[i].canon_body;
641 while (pdkim_querymethods[i] != NULL) {
642 if (strcmp(cur_val->str,pdkim_querymethods[i]) == 0 ) {
643 sig->querymethod = i;
650 sig->selector = strdup(cur_val->str);
653 sig->domain = strdup(cur_val->str);
656 sig->identity = pdkim_decode_qp(cur_val->str);
659 sig->created = strtoul(cur_val->str,NULL,10);
662 sig->expires = strtoul(cur_val->str,NULL,10);
665 sig->bodylength = strtol(cur_val->str,NULL,10);
668 sig->headernames = strdup(cur_val->str);
671 sig->copiedheaders = pdkim_decode_qp(cur_val->str);
675 if (ctx->debug_stream)
676 fprintf(ctx->debug_stream, "Unknown tag encountered\n");
681 pdkim_strclear(cur_tag);
682 pdkim_strclear(cur_val);
684 where = PDKIM_HDR_LIMBO;
687 else pdkim_strncat(cur_val,p,1);
691 if (*p == '\0') break;
700 /* Make sure the most important bits are there. */
701 if (!(sig->domain && (*(sig->domain) != '\0') &&
702 sig->selector && (*(sig->selector) != '\0') &&
703 sig->headernames && (*(sig->headernames) != '\0') &&
712 /* Chomp raw header. The final newline must not be added to the signature. */
714 while( (q > sig->rawsig_no_b_val) && ((*q == '\r') || (*q == '\n')) ) {
719 if (ctx->debug_stream) {
720 fprintf(ctx->debug_stream,
721 "PDKIM >> Raw signature w/o b= tag value >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
722 pdkim_quoteprint(ctx->debug_stream,
723 sig->rawsig_no_b_val,
724 strlen(sig->rawsig_no_b_val), 1);
725 fprintf(ctx->debug_stream,
726 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
730 sig->sha1_body = malloc(sizeof(sha1_context));
731 if (sig->sha1_body == NULL) {
735 sig->sha2_body = malloc(sizeof(sha2_context));
736 if (sig->sha2_body == NULL) {
741 sha1_starts(sig->sha1_body);
742 sha2_starts(sig->sha2_body,0);
748 /* -------------------------------------------------------------------------- */
749 pdkim_pubkey *pdkim_parse_pubkey_record(pdkim_ctx *ctx, char *raw_record) {
752 pdkim_str *cur_tag = NULL;
753 pdkim_str *cur_val = NULL;
754 int where = PDKIM_HDR_LIMBO;
756 pub = malloc(sizeof(pdkim_pubkey));
757 if (pub == NULL) return NULL;
758 memset(pub,0,sizeof(pdkim_pubkey));
765 if ( (*p == '\r') || (*p == '\n') )
768 if (where == PDKIM_HDR_LIMBO) {
769 /* In limbo, just wait for a tag-char to appear */
770 if (!((*p >= 'a') && (*p <= 'z')))
773 where = PDKIM_HDR_TAG;
776 if (where == PDKIM_HDR_TAG) {
778 cur_tag = pdkim_strnew(NULL);
780 if ((*p >= 'a') && (*p <= 'z'))
781 pdkim_strncat(cur_tag,p,1);
784 where = PDKIM_HDR_VALUE;
789 if (where == PDKIM_HDR_VALUE) {
791 cur_val = pdkim_strnew(NULL);
793 if ( (*p == '\r') || (*p == '\n') )
796 if ( (*p == ';') || (*p == '\0') ) {
797 if (cur_tag->len > 0) {
798 pdkim_strtrim(cur_val);
800 if (ctx->debug_stream)
801 fprintf(ctx->debug_stream, "%s=%s\n", cur_tag->str, cur_val->str);
803 switch (cur_tag->str[0]) {
805 /* This tag isn't evaluated because:
806 - We only support version DKIM1.
807 - Which is the default for this value (set below)
808 - Other versions are currently not specified. */
811 pub->hashes = strdup(cur_val->str);
814 pub->granularity = strdup(cur_val->str);
817 pub->notes = pdkim_decode_qp(cur_val->str);
820 pub->key = pdkim_decode_base64(cur_val->str,&(pub->key_len));
823 pub->hashes = strdup(cur_val->str);
826 pub->srvtype = strdup(cur_val->str);
829 if (strchr(cur_val->str,'y') != NULL) pub->testing = 1;
830 if (strchr(cur_val->str,'s') != NULL) pub->no_subdomaining = 1;
834 if (ctx->debug_stream)
835 fprintf(ctx->debug_stream, "Unknown tag encountered\n");
840 pdkim_strclear(cur_tag);
841 pdkim_strclear(cur_val);
842 where = PDKIM_HDR_LIMBO;
845 else pdkim_strncat(cur_val,p,1);
849 if (*p == '\0') break;
853 /* Set fallback defaults */
854 if (pub->version == NULL) pub->version = strdup(PDKIM_PUB_RECORD_VERSION);
855 if (pub->granularity == NULL) pub->granularity = strdup("*");
856 if (pub->keytype == NULL) pub->keytype = strdup("rsa");
857 if (pub->srvtype == NULL) pub->srvtype = strdup("*");
860 if (pub->key == NULL) {
861 pdkim_free_pubkey(pub);
869 /* -------------------------------------------------------------------------- */
870 int pdkim_update_bodyhash(pdkim_ctx *ctx, const char *data, int len) {
871 pdkim_signature *sig = ctx->sig;
872 /* Cache relaxed version of data */
873 char *relaxed_data = NULL;
876 /* Traverse all signatures, updating their hashes. */
877 while (sig != NULL) {
878 /* Defaults to simple canon (no further treatment necessary) */
879 const char *canon_data = data;
882 if (sig->canon_body == PDKIM_CANON_RELAXED) {
883 /* Relax the line if not done already */
884 if (relaxed_data == NULL) {
886 const char *p = data;
888 relaxed_data = malloc(len+1);
889 if (relaxed_data == NULL) return PDKIM_ERR_OOM;
893 if ( (q > 0) && (relaxed_data[q-1] == ' ') ) q--;
895 else if ( (c == '\t') || (c == ' ') ) {
896 c = ' '; /* Turns WSP into SP */
904 relaxed_data[q++] = c;
907 relaxed_data[q] = '\0';
910 canon_data = relaxed_data;
911 canon_len = relaxed_len;
914 /* Make sure we don't exceed the to-be-signed body length */
915 if ((sig->bodylength >= 0) &&
916 ((sig->signed_body_bytes+(unsigned long)canon_len) > sig->bodylength))
917 canon_len = (sig->bodylength - sig->signed_body_bytes);
920 if (sig->algo == PDKIM_ALGO_RSA_SHA1)
921 sha1_update(sig->sha1_body,(unsigned char *)canon_data,canon_len);
923 sha2_update(sig->sha2_body,(unsigned char *)canon_data,canon_len);
924 sig->signed_body_bytes += canon_len;
926 if (ctx->debug_stream!=NULL)
927 pdkim_quoteprint(ctx->debug_stream,canon_data,canon_len,0);
934 if (relaxed_data != NULL) free(relaxed_data);
939 /* -------------------------------------------------------------------------- */
940 int pdkim_finish_bodyhash(pdkim_ctx *ctx) {
941 pdkim_signature *sig = ctx->sig;
943 /* Traverse all signatures */
944 while (sig != NULL) {
947 unsigned char bh[32]; /* SHA-256 = 32 Bytes, SHA-1 = 20 Bytes */
948 if (sig->algo == PDKIM_ALGO_RSA_SHA1)
949 sha1_finish(sig->sha1_body,bh);
951 sha2_finish(sig->sha2_body,bh);
954 if (ctx->debug_stream) {
955 fprintf(ctx->debug_stream, "PDKIM [%s] Body bytes hashed: %lu\n",
956 sig->domain, sig->signed_body_bytes);
957 fprintf(ctx->debug_stream, "PDKIM [%s] bh computed: ", sig->domain);
958 pdkim_hexprint(ctx->debug_stream, (char *)bh,
959 (sig->algo == PDKIM_ALGO_RSA_SHA1)?20:32,1);
963 /* SIGNING -------------------------------------------------------------- */
964 if (ctx->mode == PDKIM_MODE_SIGN) {
965 sig->bodyhash_len = (sig->algo == PDKIM_ALGO_RSA_SHA1)?20:32;
966 sig->bodyhash = malloc(sig->bodyhash_len);
967 if (sig->bodyhash == NULL) return PDKIM_ERR_OOM;
968 memcpy(sig->bodyhash,bh,sig->bodyhash_len);
970 /* If bodylength limit is set, and we have received less bytes
971 than the requested amount, effectively remove the limit tag. */
972 if (sig->signed_body_bytes < sig->bodylength) sig->bodylength = -1;
974 /* VERIFICATION --------------------------------------------------------- */
976 /* Compare bodyhash */
977 if (memcmp(bh,sig->bodyhash,
978 (sig->algo == PDKIM_ALGO_RSA_SHA1)?20:32) == 0) {
980 if (ctx->debug_stream)
981 fprintf(ctx->debug_stream, "PDKIM [%s] Body hash verified OK\n",
987 if (ctx->debug_stream) {
988 fprintf(ctx->debug_stream, "PDKIM [%s] Body hash did NOT verify\n",
990 fprintf(ctx->debug_stream, "PDKIM [%s] bh signature: ", sig->domain);
991 pdkim_hexprint(ctx->debug_stream, sig->bodyhash,
992 (sig->algo == PDKIM_ALGO_RSA_SHA1)?20:32,1);
995 sig->verify_status = PDKIM_VERIFY_FAIL;
996 sig->verify_ext_status = PDKIM_VERIFY_FAIL_BODY;
1008 /* -------------------------------------------------------------------------- */
1009 /* Callback from pdkim_feed below for processing complete body lines */
1010 int pdkim_bodyline_complete(pdkim_ctx *ctx) {
1011 char *p = ctx->linebuf;
1012 int n = ctx->linebuf_offset;
1014 /* Ignore extra data if we've seen the end-of-data marker */
1015 if (ctx->seen_eod) goto BAIL;
1017 /* We've always got one extra byte to stuff a zero ... */
1018 ctx->linebuf[(ctx->linebuf_offset)] = '\0';
1020 if (ctx->input_mode == PDKIM_INPUT_SMTP) {
1021 /* Terminate on EOD marker */
1022 if (memcmp(p,".\r\n",3) == 0) {
1027 if (memcmp(p,"..",2) == 0) {
1033 /* Empty lines need to be buffered until we find a non-empty line */
1034 if (memcmp(p,"\r\n",2) == 0) {
1035 ctx->num_buffered_crlf++;
1039 /* At this point, we have a non-empty line, so release the buffered ones. */
1040 while (ctx->num_buffered_crlf) {
1041 pdkim_update_bodyhash(ctx,"\r\n",2);
1042 ctx->num_buffered_crlf--;
1045 pdkim_update_bodyhash(ctx,p,n);
1048 ctx->linebuf_offset = 0;
1053 /* -------------------------------------------------------------------------- */
1054 /* Callback from pdkim_feed below for processing complete headers */
1055 #define DKIM_SIGNATURE_HEADERNAME "DKIM-Signature:"
1056 int pdkim_header_complete(pdkim_ctx *ctx) {
1057 pdkim_signature *sig = ctx->sig;
1059 /* Special case: The last header can have an extra \r appended */
1060 if ( (ctx->cur_header->len > 1) &&
1061 (ctx->cur_header->str[(ctx->cur_header->len)-1] == '\r') ) {
1062 ctx->cur_header->str[(ctx->cur_header->len)-1] = '\0';
1063 ctx->cur_header->len--;
1067 if (ctx->num_headers > PDKIM_MAX_HEADERS) goto BAIL;
1069 /* SIGNING -------------------------------------------------------------- */
1070 if (ctx->mode == PDKIM_MODE_SIGN) {
1071 /* Traverse all signatures */
1072 while (sig != NULL) {
1073 pdkim_stringlist *list;
1075 if (header_name_match(ctx->cur_header->str,
1078 PDKIM_DEFAULT_SIGN_HEADERS, 0) != PDKIM_OK) goto NEXT_SIG;
1080 /* Add header to the signed headers list (in reverse order) */
1081 list = pdkim_prepend_stringlist(sig->headers,
1082 ctx->cur_header->str);
1083 if (list == NULL) return PDKIM_ERR_OOM;
1084 sig->headers = list;
1091 /* DKIM-Signature: headers are added to the verification list */
1092 if (ctx->mode == PDKIM_MODE_VERIFY) {
1093 if (strncasecmp(ctx->cur_header->str,
1094 DKIM_SIGNATURE_HEADERNAME,
1095 strlen(DKIM_SIGNATURE_HEADERNAME)) == 0) {
1096 pdkim_signature *new_sig;
1097 /* Create and chain new signature block */
1099 if (ctx->debug_stream)
1100 fprintf(ctx->debug_stream,
1101 "PDKIM >> Found sig, trying to parse >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
1103 new_sig = pdkim_parse_sig_header(ctx, ctx->cur_header->str);
1104 if (new_sig != NULL) {
1105 pdkim_signature *last_sig = ctx->sig;
1106 if (last_sig == NULL) {
1110 while (last_sig->next != NULL) { last_sig = last_sig->next; }
1111 last_sig->next = new_sig;
1116 if (ctx->debug_stream) {
1117 fprintf(ctx->debug_stream,"Error while parsing signature header\n");
1118 fprintf(ctx->debug_stream,
1119 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1124 /* every other header is stored for signature verification */
1126 pdkim_stringlist *list;
1128 list = pdkim_prepend_stringlist(ctx->headers,
1129 ctx->cur_header->str);
1130 if (list == NULL) return PDKIM_ERR_OOM;
1131 ctx->headers = list;
1136 pdkim_strclear(ctx->cur_header); /* Re-use existing pdkim_str */
1142 /* -------------------------------------------------------------------------- */
1143 #define HEADER_BUFFER_FRAG_SIZE 256
1144 DLLEXPORT int pdkim_feed (pdkim_ctx *ctx,
1148 for (p=0;p<len;p++) {
1150 if (ctx->past_headers) {
1151 /* Processing body byte */
1152 ctx->linebuf[(ctx->linebuf_offset)++] = c;
1154 int rc = pdkim_bodyline_complete(ctx); /* End of line */
1155 if (rc != PDKIM_OK) return rc;
1157 if (ctx->linebuf_offset == (PDKIM_MAX_BODY_LINE_LEN-1))
1158 return PDKIM_ERR_LONG_LINE;
1161 /* Processing header byte */
1165 int rc = pdkim_header_complete(ctx); /* Seen last header line */
1166 if (rc != PDKIM_OK) return rc;
1167 ctx->past_headers = 1;
1170 if (ctx->debug_stream)
1171 fprintf(ctx->debug_stream,
1172 "PDKIM >> Hashed body data, canonicalized >>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
1176 else ctx->seen_lf = 1;
1178 else if (ctx->seen_lf) {
1179 if (! ((c == '\t') || (c == ' '))) {
1180 int rc = pdkim_header_complete(ctx); /* End of header */
1181 if (rc != PDKIM_OK) return rc;
1186 if (ctx->cur_header == NULL) {
1187 ctx->cur_header = pdkim_strnew(NULL);
1188 if (ctx->cur_header == NULL) return PDKIM_ERR_OOM;
1190 if (ctx->cur_header->len < PDKIM_MAX_HEADER_LEN)
1191 if (pdkim_strncat(ctx->cur_header,&data[p],1) == NULL)
1192 return PDKIM_ERR_OOM;
1199 /* -------------------------------------------------------------------------- */
1200 char *pdkim_create_header(pdkim_signature *sig, int final) {
1202 char *base64_bh = NULL;
1203 char *base64_b = NULL;
1204 pdkim_str *hdr = pdkim_strnew("DKIM-Signature: v="PDKIM_SIGNATURE_VERSION);
1205 if (hdr == NULL) return NULL;
1207 base64_bh = pdkim_encode_base64(sig->bodyhash, sig->bodyhash_len);
1208 if (base64_bh == NULL) goto BAIL;
1210 /* Required and static bits */
1212 pdkim_strcat(hdr,"; a=") &&
1213 pdkim_strcat(hdr,pdkim_algos[sig->algo]) &&
1214 pdkim_strcat(hdr,"; q=") &&
1215 pdkim_strcat(hdr,pdkim_querymethods[sig->querymethod]) &&
1216 pdkim_strcat(hdr,"; c=") &&
1217 pdkim_strcat(hdr,pdkim_canons[sig->canon_headers]) &&
1218 pdkim_strcat(hdr,"/") &&
1219 pdkim_strcat(hdr,pdkim_canons[sig->canon_body]) &&
1220 pdkim_strcat(hdr,"; d=") &&
1221 pdkim_strcat(hdr,sig->domain) &&
1222 pdkim_strcat(hdr,"; s=") &&
1223 pdkim_strcat(hdr,sig->selector) &&
1224 pdkim_strcat(hdr,";\r\n\th=") &&
1225 pdkim_strcat(hdr,sig->headernames) &&
1226 pdkim_strcat(hdr,"; bh=") &&
1227 pdkim_strcat(hdr,base64_bh) &&
1228 pdkim_strcat(hdr,";\r\n\t")
1231 if (sig->identity != NULL) {
1232 if (!( pdkim_strcat(hdr,"i=") &&
1233 pdkim_strcat(hdr,sig->identity) &&
1234 pdkim_strcat(hdr,";") ) ) {
1238 if (sig->created > 0) {
1239 if (!( pdkim_strcat(hdr,"t=") &&
1240 pdkim_numcat(hdr,sig->created) &&
1241 pdkim_strcat(hdr,";") ) ) {
1245 if (sig->expires > 0) {
1246 if (!( pdkim_strcat(hdr,"x=") &&
1247 pdkim_numcat(hdr,sig->expires) &&
1248 pdkim_strcat(hdr,";") ) ) {
1252 if (sig->bodylength >= 0) {
1253 if (!( pdkim_strcat(hdr,"l=") &&
1254 pdkim_numcat(hdr,sig->bodylength) &&
1255 pdkim_strcat(hdr,";") ) ) {
1259 /* Extra linebreak */
1260 if (hdr->str[(hdr->len)-1] == ';') {
1261 if (!pdkim_strcat(hdr," \r\n\t")) goto BAIL;
1263 /* Preliminary or final version? */
1265 base64_b = pdkim_encode_base64(sig->sigdata, sig->sigdata_len);
1266 if (base64_b == NULL) goto BAIL;
1268 pdkim_strcat(hdr,"b=") &&
1269 pdkim_strcat(hdr,base64_b) &&
1270 pdkim_strcat(hdr,";")
1274 if (pdkim_strcat(hdr,"b=;")) goto DONE;
1281 rc = strdup(hdr->str);
1285 if (base64_bh != NULL) free(base64_bh);
1286 if (base64_b != NULL) free(base64_b);
1291 /* -------------------------------------------------------------------------- */
1292 DLLEXPORT int pdkim_feed_finish(pdkim_ctx *ctx, pdkim_signature **return_signatures) {
1293 pdkim_signature *sig = ctx->sig;
1294 pdkim_str *headernames = NULL; /* Collected signed header names */
1296 /* Check if we must still flush a (partial) header. If that is the
1297 case, the message has no body, and we must compute a body hash
1298 out of '<CR><LF>' */
1299 if (ctx->cur_header && ctx->cur_header->len) {
1300 int rc = pdkim_header_complete(ctx);
1301 if (rc != PDKIM_OK) return rc;
1302 pdkim_update_bodyhash(ctx,"\r\n",2);
1305 /* For non-smtp input, check if there's an unfinished line in the
1306 body line buffer. If that is the case, we must add a CRLF to the
1307 hash to properly terminate the message. */
1308 if ((ctx->input_mode == PDKIM_INPUT_NORMAL) && ctx->linebuf_offset) {
1309 pdkim_update_bodyhash(ctx, ctx->linebuf, ctx->linebuf_offset);
1310 pdkim_update_bodyhash(ctx,"\r\n",2);
1313 if (ctx->debug_stream)
1314 fprintf(ctx->debug_stream,
1315 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1319 /* Build (and/or evaluate) body hash */
1320 if (pdkim_finish_bodyhash(ctx) != PDKIM_OK) return PDKIM_ERR_OOM;
1322 /* SIGNING -------------------------------------------------------------- */
1323 if (ctx->mode == PDKIM_MODE_SIGN) {
1324 headernames = pdkim_strnew(NULL);
1325 if (headernames == NULL) return PDKIM_ERR_OOM;
1327 /* ---------------------------------------------------------------------- */
1329 while (sig != NULL) {
1330 sha1_context sha1_headers;
1331 sha2_context sha2_headers;
1333 char headerhash[32];
1335 if (sig->algo == PDKIM_ALGO_RSA_SHA1)
1336 sha1_starts(&sha1_headers);
1338 sha2_starts(&sha2_headers,0);
1341 if (ctx->debug_stream)
1342 fprintf(ctx->debug_stream,
1343 "PDKIM >> Hashed header data, canonicalized, in sequence >>>>>>>>>>>>>>\n");
1346 /* SIGNING ---------------------------------------------------------------- */
1347 /* When signing, walk through our header list and add them to the hash. As we
1348 go, construct a list of the header's names to use for the h= parameter. */
1349 if (ctx->mode == PDKIM_MODE_SIGN) {
1350 pdkim_stringlist *p = sig->headers;
1353 /* Collect header names (Note: colon presence is guaranteed here) */
1354 char *q = strchr(p->value,':');
1355 if (pdkim_strncat(headernames, p->value,
1356 (q-(p->value))+((p->next==NULL)?0:1)) == NULL)
1357 return PDKIM_ERR_OOM;
1359 if (sig->canon_headers == PDKIM_CANON_RELAXED)
1360 rh = pdkim_relax_header(p->value,1); /* cook header for relaxed canon */
1362 rh = strdup(p->value); /* just copy it for simple canon */
1364 if (rh == NULL) return PDKIM_ERR_OOM;
1366 /* Feed header to the hash algorithm */
1367 if (sig->algo == PDKIM_ALGO_RSA_SHA1)
1368 sha1_update(&(sha1_headers),(unsigned char *)rh,strlen(rh));
1370 sha2_update(&(sha2_headers),(unsigned char *)rh,strlen(rh));
1372 if (ctx->debug_stream)
1373 pdkim_quoteprint(ctx->debug_stream, rh, strlen(rh), 1);
1379 /* VERIFICATION ----------------------------------------------------------- */
1380 /* When verifying, walk through the header name list in the h= parameter and
1381 add the headers to the hash in that order. */
1383 char *b = strdup(sig->headernames);
1386 pdkim_stringlist *hdrs = ctx->headers;
1388 if (b == NULL) return PDKIM_ERR_OOM;
1391 while (hdrs != NULL) {
1397 hdrs = ctx->headers;
1399 if (q != NULL) *q = '\0';
1400 while (hdrs != NULL) {
1401 if ( (hdrs->tag == 0) &&
1402 (strncasecmp(hdrs->value,p,strlen(p)) == 0) &&
1403 ((hdrs->value)[strlen(p)] == ':') ) {
1405 if (sig->canon_headers == PDKIM_CANON_RELAXED)
1406 rh = pdkim_relax_header(hdrs->value,1); /* cook header for relaxed canon */
1408 rh = strdup(hdrs->value); /* just copy it for simple canon */
1409 if (rh == NULL) return PDKIM_ERR_OOM;
1410 /* Feed header to the hash algorithm */
1411 if (sig->algo == PDKIM_ALGO_RSA_SHA1)
1412 sha1_update(&(sha1_headers),(unsigned char *)rh,strlen(rh));
1414 sha2_update(&(sha2_headers),(unsigned char *)rh,strlen(rh));
1416 if (ctx->debug_stream)
1417 pdkim_quoteprint(ctx->debug_stream, rh, strlen(rh), 1);
1425 if (q == NULL) break;
1432 if (ctx->debug_stream)
1433 fprintf(ctx->debug_stream,
1434 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1437 /* SIGNING ---------------------------------------------------------------- */
1438 if (ctx->mode == PDKIM_MODE_SIGN) {
1439 /* Copy headernames to signature struct */
1440 sig->headernames = strdup(headernames->str);
1441 pdkim_strfree(headernames);
1443 /* Create signature header with b= omitted */
1444 sig_hdr = pdkim_create_header(ctx->sig,0);
1446 /* VERIFICATION ----------------------------------------------------------- */
1448 sig_hdr = strdup(sig->rawsig_no_b_val);
1450 /* ------------------------------------------------------------------------ */
1452 if (sig_hdr == NULL) return PDKIM_ERR_OOM;
1454 /* Relax header if necessary */
1455 if (sig->canon_headers == PDKIM_CANON_RELAXED) {
1456 char *relaxed_hdr = pdkim_relax_header(sig_hdr,0);
1458 if (relaxed_hdr == NULL) return PDKIM_ERR_OOM;
1459 sig_hdr = relaxed_hdr;
1463 if (ctx->debug_stream) {
1464 fprintf(ctx->debug_stream,
1465 "PDKIM >> Signed DKIM-Signature header, canonicalized >>>>>>>>>>>>>>>>>\n");
1466 pdkim_quoteprint(ctx->debug_stream, sig_hdr, strlen(sig_hdr), 1);
1467 fprintf(ctx->debug_stream,
1468 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1472 /* Finalize header hash */
1473 if (sig->algo == PDKIM_ALGO_RSA_SHA1) {
1474 sha1_update(&(sha1_headers),(unsigned char *)sig_hdr,strlen(sig_hdr));
1475 sha1_finish(&(sha1_headers),(unsigned char *)headerhash);
1477 if (ctx->debug_stream) {
1478 fprintf(ctx->debug_stream, "PDKIM [%s] hh computed: ", sig->domain);
1479 pdkim_hexprint(ctx->debug_stream, headerhash, 20, 1);
1484 sha2_update(&(sha2_headers),(unsigned char *)sig_hdr,strlen(sig_hdr));
1485 sha2_finish(&(sha2_headers),(unsigned char *)headerhash);
1487 if (ctx->debug_stream) {
1488 fprintf(ctx->debug_stream, "PDKIM [%s] hh computed: ", sig->domain);
1489 pdkim_hexprint(ctx->debug_stream, headerhash, 32, 1);
1496 /* SIGNING ---------------------------------------------------------------- */
1497 if (ctx->mode == PDKIM_MODE_SIGN) {
1500 rsa_init(&rsa,RSA_PKCS_V15,0);
1502 /* Perform private key operation */
1503 if (rsa_parse_key(&rsa, (unsigned char *)sig->rsa_privkey,
1504 strlen(sig->rsa_privkey), NULL, 0) != 0) {
1505 return PDKIM_ERR_RSA_PRIVKEY;
1508 sig->sigdata_len = mpi_size(&(rsa.N));
1509 sig->sigdata = malloc(sig->sigdata_len);
1510 if (sig->sigdata == NULL) return PDKIM_ERR_OOM;
1512 if (rsa_pkcs1_sign( &rsa, RSA_PRIVATE,
1513 ((sig->algo == PDKIM_ALGO_RSA_SHA1)?
1514 SIG_RSA_SHA1:SIG_RSA_SHA256),
1516 (unsigned char *)headerhash,
1517 (unsigned char *)sig->sigdata ) != 0) {
1518 return PDKIM_ERR_RSA_SIGNING;
1524 if (ctx->debug_stream) {
1525 fprintf(ctx->debug_stream, "PDKIM [%s] b computed: ",
1527 pdkim_hexprint(ctx->debug_stream, sig->sigdata, sig->sigdata_len, 1);
1531 sig->signature_header = pdkim_create_header(ctx->sig,1);
1532 if (sig->signature_header == NULL) return PDKIM_ERR_OOM;
1534 /* VERIFICATION ----------------------------------------------------------- */
1537 char *dns_txt_name, *dns_txt_reply;
1539 rsa_init(&rsa,RSA_PKCS_V15,0);
1541 dns_txt_name = malloc(PDKIM_DNS_TXT_MAX_NAMELEN);
1542 if (dns_txt_name == NULL) return PDKIM_ERR_OOM;
1543 dns_txt_reply = malloc(PDKIM_DNS_TXT_MAX_RECLEN);
1544 if (dns_txt_reply == NULL) {
1546 return PDKIM_ERR_OOM;
1548 memset(dns_txt_reply,0,PDKIM_DNS_TXT_MAX_RECLEN);
1549 memset(dns_txt_name ,0,PDKIM_DNS_TXT_MAX_NAMELEN);
1551 if (snprintf(dns_txt_name,PDKIM_DNS_TXT_MAX_NAMELEN,
1552 "%s._domainkey.%s.",
1553 sig->selector,sig->domain) >= PDKIM_DNS_TXT_MAX_NAMELEN) {
1554 sig->verify_status = PDKIM_VERIFY_INVALID;
1555 sig->verify_ext_status = PDKIM_VERIFY_INVALID_BUFFER_SIZE;
1559 if ((ctx->dns_txt_callback(dns_txt_name, dns_txt_reply) != PDKIM_OK) ||
1560 (dns_txt_reply[0] == '\0')) {
1561 sig->verify_status = PDKIM_VERIFY_INVALID;
1562 sig->verify_ext_status = PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE;
1567 if (ctx->debug_stream) {
1568 fprintf(ctx->debug_stream,
1569 "PDKIM >> Parsing public key record >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
1570 fprintf(ctx->debug_stream,"Raw record: ");
1571 pdkim_quoteprint(ctx->debug_stream, dns_txt_reply, strlen(dns_txt_reply), 1);
1575 sig->pubkey = pdkim_parse_pubkey_record(ctx,dns_txt_reply);
1576 if (sig->pubkey == NULL) {
1577 sig->verify_status = PDKIM_VERIFY_INVALID;
1578 sig->verify_ext_status = PDKIM_VERIFY_INVALID_PUBKEY_PARSING;
1580 if (ctx->debug_stream) {
1581 fprintf(ctx->debug_stream,"Error while parsing public key record\n");
1582 fprintf(ctx->debug_stream,
1583 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1590 if (ctx->debug_stream) {
1591 fprintf(ctx->debug_stream,
1592 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1596 if (rsa_parse_public_key(&rsa,
1597 (unsigned char *)sig->pubkey->key,
1598 sig->pubkey->key_len) != 0) {
1599 sig->verify_status = PDKIM_VERIFY_INVALID;
1600 sig->verify_ext_status = PDKIM_VERIFY_INVALID_PUBKEY_PARSING;
1604 /* Check the signature */
1605 if (rsa_pkcs1_verify(&rsa,
1607 ((sig->algo == PDKIM_ALGO_RSA_SHA1)?
1608 SIG_RSA_SHA1:SIG_RSA_SHA256),
1610 (unsigned char *)headerhash,
1611 (unsigned char *)sig->sigdata) != 0) {
1612 sig->verify_status = PDKIM_VERIFY_FAIL;
1613 sig->verify_ext_status = PDKIM_VERIFY_FAIL_MESSAGE;
1617 /* We have a winner! (if bodydhash was correct earlier) */
1618 if (sig->verify_status == PDKIM_VERIFY_NONE) {
1619 sig->verify_status = PDKIM_VERIFY_PASS;
1625 if (ctx->debug_stream) {
1626 fprintf(ctx->debug_stream, "PDKIM [%s] signature status: %s",
1627 sig->domain, pdkim_verify_status_str(sig->verify_status));
1628 if (sig->verify_ext_status > 0) {
1629 fprintf(ctx->debug_stream, " (%s)\n",
1630 pdkim_verify_ext_status_str(sig->verify_ext_status));
1633 fprintf(ctx->debug_stream, "\n");
1640 free(dns_txt_reply);
1646 /* If requested, set return pointer to signature(s) */
1647 if (return_signatures != NULL) {
1648 *return_signatures = ctx->sig;
1655 /* -------------------------------------------------------------------------- */
1656 DLLEXPORT pdkim_ctx *pdkim_init_verify(int input_mode,
1657 int(*dns_txt_callback)(char *, char *)
1659 pdkim_ctx *ctx = malloc(sizeof(pdkim_ctx));
1660 if (ctx == NULL) return NULL;
1661 memset(ctx,0,sizeof(pdkim_ctx));
1663 ctx->linebuf = malloc(PDKIM_MAX_BODY_LINE_LEN);
1664 if (ctx->linebuf == NULL) {
1669 ctx->mode = PDKIM_MODE_VERIFY;
1670 ctx->input_mode = input_mode;
1671 ctx->dns_txt_callback = dns_txt_callback;
1677 /* -------------------------------------------------------------------------- */
1678 DLLEXPORT pdkim_ctx *pdkim_init_sign(int input_mode,
1681 char *rsa_privkey) {
1683 pdkim_signature *sig;
1685 if (!domain || !selector || !rsa_privkey) return NULL;
1687 ctx = malloc(sizeof(pdkim_ctx));
1688 if (ctx == NULL) return NULL;
1689 memset(ctx,0,sizeof(pdkim_ctx));
1691 ctx->linebuf = malloc(PDKIM_MAX_BODY_LINE_LEN);
1692 if (ctx->linebuf == NULL) {
1697 sig = malloc(sizeof(pdkim_signature));
1703 memset(sig,0,sizeof(pdkim_signature));
1704 sig->bodylength = -1;
1706 ctx->mode = PDKIM_MODE_SIGN;
1707 ctx->input_mode = input_mode;
1710 ctx->sig->domain = strdup(domain);
1711 ctx->sig->selector = strdup(selector);
1712 ctx->sig->rsa_privkey = strdup(rsa_privkey);
1714 if (!ctx->sig->domain || !ctx->sig->selector || !ctx->sig->rsa_privkey) {
1715 pdkim_free_ctx(ctx);
1719 ctx->sig->sha1_body = malloc(sizeof(sha1_context));
1720 if (ctx->sig->sha1_body == NULL) {
1721 pdkim_free_ctx(ctx);
1724 sha1_starts(ctx->sig->sha1_body);
1726 ctx->sig->sha2_body = malloc(sizeof(sha2_context));
1727 if (ctx->sig->sha2_body == NULL) {
1728 pdkim_free_ctx(ctx);
1731 sha2_starts(ctx->sig->sha2_body,0);
1737 /* -------------------------------------------------------------------------- */
1738 DLLEXPORT void pdkim_set_debug_stream(pdkim_ctx *ctx,
1739 FILE *debug_stream) {
1740 ctx->debug_stream = debug_stream;
1744 /* -------------------------------------------------------------------------- */
1745 DLLEXPORT int pdkim_set_optional(pdkim_ctx *ctx,
1752 unsigned long created,
1753 unsigned long expires) {
1755 if (identity != NULL) {
1756 ctx->sig->identity = strdup(identity);
1757 if (ctx->sig->identity == NULL) return PDKIM_ERR_OOM;
1760 if (sign_headers != NULL) {
1761 ctx->sig->sign_headers = strdup(sign_headers);
1762 if (ctx->sig->sign_headers == NULL) return PDKIM_ERR_OOM;
1765 ctx->sig->canon_headers = canon_headers;
1766 ctx->sig->canon_body = canon_body;
1767 ctx->sig->bodylength = bodylength;
1768 ctx->sig->algo = algo;
1769 ctx->sig->created = created;
1770 ctx->sig->expires = expires;