2 * PDKIM - a RFC4871 (DKIM) implementation
4 * Copyright (C) 2009 - 2015 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.
25 #include "pdkim-rsa.h"
27 #include "polarssl/sha1.h"
28 #include "polarssl/sha2.h"
29 #include "polarssl/rsa.h"
31 #define PDKIM_SIGNATURE_VERSION "1"
32 #define PDKIM_PUB_RECORD_VERSION "DKIM1"
34 #define PDKIM_MAX_HEADER_LEN 65536
35 #define PDKIM_MAX_HEADERS 512
36 #define PDKIM_MAX_BODY_LINE_LEN 16384
37 #define PDKIM_DNS_TXT_MAX_NAMELEN 1024
38 #define PDKIM_DEFAULT_SIGN_HEADERS "From:Sender:Reply-To:Subject:Date:"\
39 "Message-ID:To:Cc:MIME-Version:Content-Type:"\
40 "Content-Transfer-Encoding:Content-ID:"\
41 "Content-Description:Resent-Date:Resent-From:"\
42 "Resent-Sender:Resent-To:Resent-Cc:"\
43 "Resent-Message-ID:In-Reply-To:References:"\
44 "List-Id:List-Help:List-Unsubscribe:"\
45 "List-Subscribe:List-Post:List-Owner:List-Archive"
47 /* -------------------------------------------------------------------------- */
48 struct pdkim_stringlist
{
54 #define PDKIM_STR_ALLOC_FRAG 256
58 unsigned int allocated
;
61 /* -------------------------------------------------------------------------- */
62 /* A bunch of list constants */
63 const char *pdkim_querymethods
[] = {
67 const char *pdkim_algos
[] = {
72 const char *pdkim_canons
[] = {
77 const char *pdkim_hashes
[] = {
82 const char *pdkim_keytypes
[] = {
87 typedef struct pdkim_combined_canon_entry
{
91 } pdkim_combined_canon_entry
;
92 pdkim_combined_canon_entry pdkim_combined_canons
[] = {
93 { "simple/simple", PDKIM_CANON_SIMPLE
, PDKIM_CANON_SIMPLE
},
94 { "simple/relaxed", PDKIM_CANON_SIMPLE
, PDKIM_CANON_RELAXED
},
95 { "relaxed/simple", PDKIM_CANON_RELAXED
, PDKIM_CANON_SIMPLE
},
96 { "relaxed/relaxed", PDKIM_CANON_RELAXED
, PDKIM_CANON_RELAXED
},
97 { "simple", PDKIM_CANON_SIMPLE
, PDKIM_CANON_SIMPLE
},
98 { "relaxed", PDKIM_CANON_RELAXED
, PDKIM_CANON_SIMPLE
},
103 const char *pdkim_verify_status_str(int status
) {
105 case PDKIM_VERIFY_NONE
: return "PDKIM_VERIFY_NONE";
106 case PDKIM_VERIFY_INVALID
: return "PDKIM_VERIFY_INVALID";
107 case PDKIM_VERIFY_FAIL
: return "PDKIM_VERIFY_FAIL";
108 case PDKIM_VERIFY_PASS
: return "PDKIM_VERIFY_PASS";
109 default: return "PDKIM_VERIFY_UNKNOWN";
112 const char *pdkim_verify_ext_status_str(int ext_status
) {
114 case PDKIM_VERIFY_FAIL_BODY
: return "PDKIM_VERIFY_FAIL_BODY";
115 case PDKIM_VERIFY_FAIL_MESSAGE
: return "PDKIM_VERIFY_FAIL_MESSAGE";
116 case PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE
: return "PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE";
117 case PDKIM_VERIFY_INVALID_BUFFER_SIZE
: return "PDKIM_VERIFY_INVALID_BUFFER_SIZE";
118 case PDKIM_VERIFY_INVALID_PUBKEY_PARSING
: return "PDKIM_VERIFY_INVALID_PUBKEY_PARSING";
119 default: return "PDKIM_VERIFY_UNKNOWN";
124 /* -------------------------------------------------------------------------- */
125 /* Print debugging functions */
127 pdkim_quoteprint(const char *data
, int len
, int lf
)
130 const unsigned char *p
= (const unsigned char *)data
;
132 for (i
= 0; i
< len
; i
++)
137 case ' ' : debug_printf("{SP}"); break;
138 case '\t': debug_printf("{TB}"); break;
139 case '\r': debug_printf("{CR}"); break;
140 case '\n': debug_printf("{LF}"); break;
141 case '{' : debug_printf("{BO}"); break;
142 case '}' : debug_printf("{BC}"); break;
144 if ( (c
< 32) || (c
> 127) )
145 debug_printf("{%02x}", c
);
147 debug_printf("%c", c
);
156 pdkim_hexprint(const char *data
, int len
, int lf
)
159 const unsigned char *p
= (const unsigned char *)data
;
161 for (i
= 0 ; i
< len
; i
++)
162 debug_printf("%02x", p
[i
]);
168 /* -------------------------------------------------------------------------- */
169 /* Simple string list implementation for convinience */
171 pdkim_append_stringlist(pdkim_stringlist
*base
, char *str
)
173 pdkim_stringlist
*new_entry
= malloc(sizeof(pdkim_stringlist
));
175 if (!new_entry
) return NULL
;
176 memset(new_entry
, 0, sizeof(pdkim_stringlist
));
177 if (!(new_entry
->value
= strdup(str
))) return NULL
;
180 pdkim_stringlist
*last
= base
;
181 while (last
->next
!= NULL
) { last
= last
->next
; }
182 last
->next
= new_entry
;
190 pdkim_prepend_stringlist(pdkim_stringlist
*base
, char *str
)
192 pdkim_stringlist
*new_entry
= malloc(sizeof(pdkim_stringlist
));
194 if (!new_entry
) return NULL
;
195 memset(new_entry
, 0, sizeof(pdkim_stringlist
));
196 if (!(new_entry
->value
= strdup(str
))) return NULL
;
198 new_entry
->next
= base
;
203 /* -------------------------------------------------------------------------- */
204 /* A small "growing string" implementation to escape malloc/realloc hell */
207 pdkim_strnew (const char *cstr
)
209 unsigned int len
= cstr
? strlen(cstr
) : 0;
210 pdkim_str
*p
= malloc(sizeof(pdkim_str
));
213 memset(p
, 0, sizeof(pdkim_str
));
214 if (!(p
->str
= malloc(len
+1)))
219 p
->allocated
= len
+1;
222 strcpy(p
->str
, cstr
);
224 p
->str
[p
->len
] = '\0';
229 pdkim_strncat(pdkim_str
*str
, const char *data
, int len
)
231 if ((str
->allocated
- str
->len
) < (len
+1))
233 /* Extend the buffer */
234 int num_frags
= ((len
+1)/PDKIM_STR_ALLOC_FRAG
)+1;
235 char *n
= realloc(str
->str
,
236 (str
->allocated
+(num_frags
*PDKIM_STR_ALLOC_FRAG
)));
237 if (n
== NULL
) return NULL
;
239 str
->allocated
+= (num_frags
*PDKIM_STR_ALLOC_FRAG
);
241 strncpy(&(str
->str
[str
->len
]), data
, len
);
243 str
->str
[str
->len
] = '\0';
248 pdkim_strcat(pdkim_str
*str
, const char *cstr
)
250 return pdkim_strncat(str
, cstr
, strlen(cstr
));
254 pdkim_numcat(pdkim_str
*str
, unsigned long num
)
257 snprintf(minibuf
, 20, "%lu", num
);
258 return pdkim_strcat(str
, minibuf
);
262 pdkim_strtrim(pdkim_str
*str
)
266 while ( (*p
!= '\0') && ((*p
== '\t') || (*p
== ' ')) ) p
++;
267 while (*p
!= '\0') {*q
= *p
; q
++; p
++;}
269 while ( (q
!= str
->str
) && ( (*q
== '\0') || (*q
== '\t') || (*q
== ' ') ) )
274 str
->len
= strlen(str
->str
);
279 pdkim_strclear(pdkim_str
*str
)
287 pdkim_strfree(pdkim_str
*str
)
290 if (str
->str
) free(str
->str
);
296 /* -------------------------------------------------------------------------- */
299 pdkim_free_pubkey(pdkim_pubkey
*pub
)
303 if (pub
->version
) free(pub
->version
);
304 if (pub
->granularity
) free(pub
->granularity
);
305 if (pub
->hashes
) free(pub
->hashes
);
306 if (pub
->keytype
) free(pub
->keytype
);
307 if (pub
->srvtype
) free(pub
->srvtype
);
308 if (pub
->notes
) free(pub
->notes
);
309 /* if (pub->key ) free(pub->key); */
315 /* -------------------------------------------------------------------------- */
318 pdkim_free_sig(pdkim_signature
*sig
)
322 pdkim_signature
*next
= (pdkim_signature
*)sig
->next
;
324 pdkim_stringlist
*e
= sig
->headers
;
327 pdkim_stringlist
*c
= e
;
328 if (e
->value
) free(e
->value
);
333 /* if (sig->sigdata ) free(sig->sigdata); */
334 /* if (sig->bodyhash ) free(sig->bodyhash); */
335 if (sig
->selector
) free(sig
->selector
);
336 if (sig
->domain
) free(sig
->domain
);
337 if (sig
->identity
) free(sig
->identity
);
338 if (sig
->headernames
) free(sig
->headernames
);
339 if (sig
->copiedheaders
) free(sig
->copiedheaders
);
340 if (sig
->rsa_privkey
) free(sig
->rsa_privkey
);
341 if (sig
->sign_headers
) free(sig
->sign_headers
);
342 if (sig
->signature_header
) free(sig
->signature_header
);
343 if (sig
->sha1_body
) free(sig
->sha1_body
);
344 if (sig
->sha2_body
) free(sig
->sha2_body
);
346 if (sig
->pubkey
) pdkim_free_pubkey(sig
->pubkey
);
349 if (next
) pdkim_free_sig(next
);
354 /* -------------------------------------------------------------------------- */
357 pdkim_free_ctx(pdkim_ctx
*ctx
)
361 pdkim_stringlist
*e
= ctx
->headers
;
364 pdkim_stringlist
*c
= e
;
365 if (e
->value
) free(e
->value
);
369 pdkim_free_sig(ctx
->sig
);
370 pdkim_strfree(ctx
->cur_header
);
376 /* -------------------------------------------------------------------------- */
377 /* Matches the name of the passed raw "header" against
378 the passed colon-separated "list", starting at entry
379 "start". Returns the position of the header name in
383 header_name_match(const char *header
,
393 /* Get header name */
394 char *hcolon
= strchr(header
, ':');
396 if (!hcolon
) return rc
; /* This isn't a header */
398 if (!(hname
= malloc((hcolon
-header
)+1)))
399 return PDKIM_ERR_OOM
;
400 memset(hname
, 0, (hcolon
-header
)+1);
401 strncpy(hname
, header
, (hcolon
-header
));
403 /* Copy tick-off list locally, so we can punch zeroes into it */
404 if (!(lcopy
= strdup(tick
)))
407 return PDKIM_ERR_OOM
;
415 if (strcasecmp(p
, hname
) == 0)
418 /* Invalidate header name instance in tick-off list */
419 if (do_tick
) tick
[p
-lcopy
] = '_';
427 if (strcasecmp(p
, hname
) == 0)
430 /* Invalidate header name instance in tick-off list */
431 if (do_tick
) tick
[p
-lcopy
] = '_';
441 /* -------------------------------------------------------------------------- */
442 /* Performs "relaxed" canonicalization of a header. The returned pointer needs
446 pdkim_relax_header (char *header
, int crlf
)
448 BOOL past_field_name
= FALSE
;
449 BOOL seen_wsp
= FALSE
;
452 char *relaxed
= malloc(strlen(header
)+3);
454 if (!relaxed
) return NULL
;
457 for (p
= header
; *p
!= '\0'; p
++)
461 if (c
== '\r' || c
== '\n')
463 if (c
== '\t' || c
== ' ')
467 c
= ' '; /* Turns WSP into SP */
471 if (!past_field_name
&& c
== ':')
473 if (seen_wsp
) q
--; /* This removes WSP before the colon */
474 seen_wsp
= TRUE
; /* This removes WSP after the colon */
475 past_field_name
= TRUE
;
480 /* Lowercase header name */
481 if (!past_field_name
) c
= tolower(c
);
485 if (q
> relaxed
&& q
[-1] == ' ') q
--; /* Squash eventual trailing SP */
488 if (crlf
) strcat(relaxed
, "\r\n");
493 /* -------------------------------------------------------------------------- */
494 #define PDKIM_QP_ERROR_DECODE -1
497 pdkim_decode_qp_char(char *qp_p
, int *c
)
499 char *initial_pos
= qp_p
;
501 /* Advance one char */
504 /* Check for two hex digits and decode them */
505 if (isxdigit(*qp_p
) && isxdigit(qp_p
[1]))
507 /* Do hex conversion */
508 *c
= (isdigit(*qp_p
) ? *qp_p
- '0' : toupper(*qp_p
) - 'A' + 10) << 4;
509 *c
|= isdigit(qp_p
[1]) ? qp_p
[1] - '0' : toupper(qp_p
[1]) - 'A' + 10;
513 /* Illegal char here */
514 *c
= PDKIM_QP_ERROR_DECODE
;
519 /* -------------------------------------------------------------------------- */
522 pdkim_decode_qp(char *str
)
527 char *n
= malloc(strlen(p
)+1);
537 p
= pdkim_decode_qp_char(p
, &nchar
);
553 /* -------------------------------------------------------------------------- */
556 pdkim_decode_base64(char *str
, int *num_decoded
)
560 int old_pool
= store_pool
;
562 /* There is a store-reset between header & body reception
563 so cannot use the main pool */
565 store_pool
= POOL_PERM
;
566 dlen
= b64decode(US str
, USS
&res
);
567 store_pool
= old_pool
;
569 if (dlen
< 0) return NULL
;
571 if (num_decoded
) *num_decoded
= dlen
;
576 /* -------------------------------------------------------------------------- */
579 pdkim_encode_base64(char *str
, int num
)
582 int old_pool
= store_pool
;
584 store_pool
= POOL_PERM
;
585 ret
= CS
b64encode(US str
, num
);
586 store_pool
= old_pool
;
591 /* -------------------------------------------------------------------------- */
592 #define PDKIM_HDR_LIMBO 0
593 #define PDKIM_HDR_TAG 1
594 #define PDKIM_HDR_VALUE 2
597 pdkim_parse_sig_header(pdkim_ctx
*ctx
, char *raw_hdr
)
599 pdkim_signature
*sig
;
601 pdkim_str
*cur_tag
= NULL
;
602 pdkim_str
*cur_val
= NULL
;
603 BOOL past_hname
= FALSE
;
604 BOOL in_b_val
= FALSE
;
605 int where
= PDKIM_HDR_LIMBO
;
608 if (!(sig
= malloc(sizeof(pdkim_signature
)))) return NULL
;
609 memset(sig
, 0, sizeof(pdkim_signature
));
610 sig
->bodylength
= -1;
612 if (!(sig
->rawsig_no_b_val
= malloc(strlen(raw_hdr
)+1)))
618 q
= sig
->rawsig_no_b_val
;
620 for (p
= raw_hdr
; ; p
++)
625 if (c
== '\r' || c
== '\n')
628 /* Fast-forward through header name */
631 if (c
== ':') past_hname
= TRUE
;
635 if (where
== PDKIM_HDR_LIMBO
)
637 /* In limbo, just wait for a tag-char to appear */
638 if (!(c
>= 'a' && c
<= 'z'))
641 where
= PDKIM_HDR_TAG
;
644 if (where
== PDKIM_HDR_TAG
)
647 cur_tag
= pdkim_strnew(NULL
);
649 if (c
>= 'a' && c
<= 'z')
650 pdkim_strncat(cur_tag
, p
, 1);
654 if (strcmp(cur_tag
->str
, "b") == 0)
659 where
= PDKIM_HDR_VALUE
;
664 if (where
== PDKIM_HDR_VALUE
)
667 cur_val
= pdkim_strnew(NULL
);
669 if (c
== '\r' || c
== '\n' || c
== ' ' || c
== '\t')
672 if (c
== ';' || c
== '\0')
674 if (cur_tag
->len
> 0)
676 pdkim_strtrim(cur_val
);
678 DEBUG(D_acl
) debug_printf(" %s=%s\n", cur_tag
->str
, cur_val
->str
);
680 switch (cur_tag
->str
[0])
683 if (cur_tag
->str
[1] == 'h')
684 sig
->bodyhash
= pdkim_decode_base64(cur_val
->str
,
687 sig
->sigdata
= pdkim_decode_base64(cur_val
->str
,
691 /* We only support version 1, and that is currently the
692 only version there is. */
693 if (strcmp(cur_val
->str
, PDKIM_SIGNATURE_VERSION
) == 0)
697 for (i
= 0; pdkim_algos
[i
]; i
++)
698 if (strcmp(cur_val
->str
, pdkim_algos
[i
]) == 0)
705 for (i
= 0; pdkim_combined_canons
[i
].str
; i
++)
706 if (strcmp(cur_val
->str
, pdkim_combined_canons
[i
].str
) == 0)
708 sig
->canon_headers
= pdkim_combined_canons
[i
].canon_headers
;
709 sig
->canon_body
= pdkim_combined_canons
[i
].canon_body
;
714 for (i
= 0; pdkim_querymethods
[i
]; i
++)
715 if (strcmp(cur_val
->str
, pdkim_querymethods
[i
]) == 0)
717 sig
->querymethod
= i
;
722 sig
->selector
= strdup(cur_val
->str
); break;
724 sig
->domain
= strdup(cur_val
->str
); break;
726 sig
->identity
= pdkim_decode_qp(cur_val
->str
); break;
728 sig
->created
= strtoul(cur_val
->str
, NULL
, 10); break;
730 sig
->expires
= strtoul(cur_val
->str
, NULL
, 10); break;
732 sig
->bodylength
= strtol(cur_val
->str
, NULL
, 10); break;
734 sig
->headernames
= strdup(cur_val
->str
); break;
736 sig
->copiedheaders
= pdkim_decode_qp(cur_val
->str
); break;
738 DEBUG(D_acl
) debug_printf(" Unknown tag encountered\n");
742 pdkim_strclear(cur_tag
);
743 pdkim_strclear(cur_val
);
745 where
= PDKIM_HDR_LIMBO
;
748 pdkim_strncat(cur_val
, p
, 1);
759 /* Make sure the most important bits are there. */
760 if (!(sig
->domain
&& (*(sig
->domain
) != '\0') &&
761 sig
->selector
&& (*(sig
->selector
) != '\0') &&
762 sig
->headernames
&& (*(sig
->headernames
) != '\0') &&
772 /* Chomp raw header. The final newline must not be added to the signature. */
774 while (q
> sig
->rawsig_no_b_val
&& (*q
== '\r' || *q
== '\n'))
775 *q
= '\0'; q
--; /*XXX questionable code layout; possible bug */
780 "PDKIM >> Raw signature w/o b= tag value >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
781 pdkim_quoteprint(sig
->rawsig_no_b_val
, strlen(sig
->rawsig_no_b_val
), 1);
783 "PDKIM >> Sig size: %4d bits\n", sig
->sigdata_len
*8);
785 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
788 if ( !(sig
->sha1_body
= malloc(sizeof(sha1_context
)))
789 || !(sig
->sha2_body
= malloc(sizeof(sha2_context
)))
796 sha1_starts(sig
->sha1_body
);
797 sha2_starts(sig
->sha2_body
, 0);
803 /* -------------------------------------------------------------------------- */
806 pdkim_parse_pubkey_record(pdkim_ctx
*ctx
, char *raw_record
)
810 pdkim_str
*cur_tag
= NULL
;
811 pdkim_str
*cur_val
= NULL
;
812 int where
= PDKIM_HDR_LIMBO
;
814 if (!(pub
= malloc(sizeof(pdkim_pubkey
)))) return NULL
;
815 memset(pub
, 0, sizeof(pdkim_pubkey
));
817 for (p
= raw_record
; ; p
++)
822 if (c
== '\r' || c
== '\n')
825 if (where
== PDKIM_HDR_LIMBO
)
827 /* In limbo, just wait for a tag-char to appear */
828 if (!(c
>= 'a' && c
<= 'z'))
831 where
= PDKIM_HDR_TAG
;
834 if (where
== PDKIM_HDR_TAG
)
837 cur_tag
= pdkim_strnew(NULL
);
839 if (c
>= 'a' && c
<= 'z')
840 pdkim_strncat(cur_tag
, p
, 1);
844 where
= PDKIM_HDR_VALUE
;
849 if (where
== PDKIM_HDR_VALUE
)
852 cur_val
= pdkim_strnew(NULL
);
854 if (c
== '\r' || c
== '\n')
857 if (c
== ';' || c
== '\0')
859 if (cur_tag
->len
> 0)
861 pdkim_strtrim(cur_val
);
862 DEBUG(D_acl
) debug_printf(" %s=%s\n", cur_tag
->str
, cur_val
->str
);
864 switch (cur_tag
->str
[0])
867 /* This tag isn't evaluated because:
868 - We only support version DKIM1.
869 - Which is the default for this value (set below)
870 - Other versions are currently not specified. */
873 pub
->hashes
= strdup(cur_val
->str
); break;
875 pub
->granularity
= strdup(cur_val
->str
); break;
877 pub
->notes
= pdkim_decode_qp(cur_val
->str
); break;
879 pub
->key
= pdkim_decode_base64(cur_val
->str
, &(pub
->key_len
)); break;
881 pub
->hashes
= strdup(cur_val
->str
); break;
883 pub
->srvtype
= strdup(cur_val
->str
); break;
885 if (strchr(cur_val
->str
, 'y') != NULL
) pub
->testing
= 1;
886 if (strchr(cur_val
->str
, 's') != NULL
) pub
->no_subdomaining
= 1;
889 DEBUG(D_acl
) debug_printf(" Unknown tag encountered\n");
893 pdkim_strclear(cur_tag
);
894 pdkim_strclear(cur_val
);
895 where
= PDKIM_HDR_LIMBO
;
898 pdkim_strncat(cur_val
, p
, 1);
902 if (c
== '\0') break;
905 /* Set fallback defaults */
906 if (!pub
->version
) pub
->version
= strdup(PDKIM_PUB_RECORD_VERSION
);
907 if (!pub
->granularity
) pub
->granularity
= strdup("*");
908 if (!pub
->keytype
) pub
->keytype
= strdup("rsa");
909 if (!pub
->srvtype
) pub
->srvtype
= strdup("*");
915 pdkim_free_pubkey(pub
);
920 /* -------------------------------------------------------------------------- */
923 pdkim_update_bodyhash(pdkim_ctx
*ctx
, const char *data
, int len
)
925 pdkim_signature
*sig
= ctx
->sig
;
926 /* Cache relaxed version of data */
927 char *relaxed_data
= NULL
;
930 /* Traverse all signatures, updating their hashes. */
933 /* Defaults to simple canon (no further treatment necessary) */
934 const char *canon_data
= data
;
937 if (sig
->canon_body
== PDKIM_CANON_RELAXED
)
939 /* Relax the line if not done already */
942 BOOL seen_wsp
= FALSE
;
946 if (!(relaxed_data
= malloc(len
+1)))
947 return PDKIM_ERR_OOM
;
949 for (p
= data
; *p
; p
++)
954 if (q
> 0 && relaxed_data
[q
-1] == ' ')
957 else if (c
== '\t' || c
== ' ')
959 c
= ' '; /* Turns WSP into SP */
966 relaxed_data
[q
++] = c
;
968 relaxed_data
[q
] = '\0';
971 canon_data
= relaxed_data
;
972 canon_len
= relaxed_len
;
975 /* Make sure we don't exceed the to-be-signed body length */
976 if ( sig
->bodylength
>= 0
977 && sig
->signed_body_bytes
+ (unsigned long)canon_len
> sig
->bodylength
979 canon_len
= sig
->bodylength
- sig
->signed_body_bytes
;
983 if (sig
->algo
== PDKIM_ALGO_RSA_SHA1
)
984 sha1_update(sig
->sha1_body
, (unsigned char *)canon_data
, canon_len
);
986 sha2_update(sig
->sha2_body
, (unsigned char *)canon_data
, canon_len
);
988 sig
->signed_body_bytes
+= canon_len
;
989 DEBUG(D_acl
) pdkim_quoteprint(canon_data
, canon_len
, 1);
995 if (relaxed_data
) free(relaxed_data
);
1000 /* -------------------------------------------------------------------------- */
1003 pdkim_finish_bodyhash(pdkim_ctx
*ctx
)
1005 pdkim_signature
*sig
;
1007 /* Traverse all signatures */
1008 for (sig
= ctx
->sig
; sig
; sig
= sig
->next
)
1009 { /* Finish hashes */
1010 unsigned char bh
[32]; /* SHA-256 = 32 Bytes, SHA-1 = 20 Bytes */
1012 if (sig
->algo
== PDKIM_ALGO_RSA_SHA1
)
1013 sha1_finish(sig
->sha1_body
, bh
);
1015 sha2_finish(sig
->sha2_body
, bh
);
1019 debug_printf("PDKIM [%s] Body bytes hashed: %lu\n"
1020 "PDKIM [%s] bh computed: ",
1021 sig
->domain
, sig
->signed_body_bytes
, sig
->domain
);
1022 pdkim_hexprint((char *)bh
, sig
->algo
== PDKIM_ALGO_RSA_SHA1
? 20 : 32, 1);
1025 /* SIGNING -------------------------------------------------------------- */
1026 if (ctx
->mode
== PDKIM_MODE_SIGN
)
1028 sig
->bodyhash_len
= (sig
->algo
== PDKIM_ALGO_RSA_SHA1
)?20:32;
1030 sig
->bodyhash
= string_copyn(US bh
, sig
->bodyhash_len
);
1032 /* If bodylength limit is set, and we have received less bytes
1033 than the requested amount, effectively remove the limit tag. */
1034 if (sig
->signed_body_bytes
< sig
->bodylength
)
1035 sig
->bodylength
= -1;
1038 /* VERIFICATION --------------------------------------------------------- */
1041 /* Compare bodyhash */
1042 if (memcmp(bh
, sig
->bodyhash
,
1043 (sig
->algo
== PDKIM_ALGO_RSA_SHA1
)?20:32) == 0)
1045 DEBUG(D_acl
) debug_printf("PDKIM [%s] Body hash verified OK\n", sig
->domain
);
1051 debug_printf("PDKIM [%s] bh signature: ", sig
->domain
);
1052 pdkim_hexprint(sig
->bodyhash
,
1053 sig
->algo
== PDKIM_ALGO_RSA_SHA1
? 20 : 32, 1);
1054 debug_printf("PDKIM [%s] Body hash did NOT verify\n", sig
->domain
);
1056 sig
->verify_status
= PDKIM_VERIFY_FAIL
;
1057 sig
->verify_ext_status
= PDKIM_VERIFY_FAIL_BODY
;
1067 /* -------------------------------------------------------------------------- */
1068 /* Callback from pdkim_feed below for processing complete body lines */
1071 pdkim_bodyline_complete(pdkim_ctx
*ctx
)
1073 char *p
= ctx
->linebuf
;
1074 int n
= ctx
->linebuf_offset
;
1075 pdkim_signature
*sig
= ctx
->sig
; /*XXX assumes only one sig */
1077 /* Ignore extra data if we've seen the end-of-data marker */
1078 if (ctx
->seen_eod
) goto BAIL
;
1080 /* We've always got one extra byte to stuff a zero ... */
1081 ctx
->linebuf
[ctx
->linebuf_offset
] = '\0';
1083 /* Terminate on EOD marker */
1084 if (memcmp(p
, ".\r\n", 3) == 0)
1086 /* In simple body mode, if any empty lines were buffered,
1087 replace with one. rfc 4871 3.4.3 */
1088 /*XXX checking the signed-body-bytes is a gross hack; I think
1089 it indicates that all linebreaks should be buffered, including
1090 the one terminating a text line */
1091 if ( sig
&& sig
->canon_body
== PDKIM_CANON_SIMPLE
1092 && sig
->signed_body_bytes
== 0
1093 && ctx
->num_buffered_crlf
> 0
1095 pdkim_update_bodyhash(ctx
, "\r\n", 2);
1097 ctx
->seen_eod
= TRUE
;
1101 if (memcmp(p
, "..", 2) == 0)
1107 /* Empty lines need to be buffered until we find a non-empty line */
1108 if (memcmp(p
, "\r\n", 2) == 0)
1110 ctx
->num_buffered_crlf
++;
1114 if (sig
&& sig
->canon_body
== PDKIM_CANON_RELAXED
)
1116 /* Lines with just spaces need to be buffered too */
1118 while (memcmp(check
, "\r\n", 2) != 0)
1122 if (c
!= '\t' && c
!= ' ')
1127 ctx
->num_buffered_crlf
++;
1132 /* At this point, we have a non-empty line, so release the buffered ones. */
1133 while (ctx
->num_buffered_crlf
)
1135 pdkim_update_bodyhash(ctx
, "\r\n", 2);
1136 ctx
->num_buffered_crlf
--;
1139 pdkim_update_bodyhash(ctx
, p
, n
);
1142 ctx
->linebuf_offset
= 0;
1147 /* -------------------------------------------------------------------------- */
1148 /* Callback from pdkim_feed below for processing complete headers */
1149 #define DKIM_SIGNATURE_HEADERNAME "DKIM-Signature:"
1152 pdkim_header_complete(pdkim_ctx
*ctx
)
1154 /* Special case: The last header can have an extra \r appended */
1155 if ( (ctx
->cur_header
->len
> 1) &&
1156 (ctx
->cur_header
->str
[(ctx
->cur_header
->len
)-1] == '\r') )
1158 ctx
->cur_header
->str
[(ctx
->cur_header
->len
)-1] = '\0';
1159 ctx
->cur_header
->len
--;
1163 if (ctx
->num_headers
> PDKIM_MAX_HEADERS
) goto BAIL
;
1165 /* SIGNING -------------------------------------------------------------- */
1166 if (ctx
->mode
== PDKIM_MODE_SIGN
)
1168 pdkim_signature
*sig
;
1170 for (sig
= ctx
->sig
; sig
; sig
= sig
->next
) /* Traverse all signatures */
1171 if (header_name_match(ctx
->cur_header
->str
,
1174 PDKIM_DEFAULT_SIGN_HEADERS
, 0) == PDKIM_OK
)
1176 pdkim_stringlist
*list
;
1178 /* Add header to the signed headers list (in reverse order) */
1179 if (!(list
= pdkim_prepend_stringlist(sig
->headers
,
1180 ctx
->cur_header
->str
)))
1181 return PDKIM_ERR_OOM
;
1182 sig
->headers
= list
;
1186 /* VERIFICATION ----------------------------------------------------------- */
1187 /* DKIM-Signature: headers are added to the verification list */
1188 if (ctx
->mode
== PDKIM_MODE_VERIFY
)
1190 if (strncasecmp(ctx
->cur_header
->str
,
1191 DKIM_SIGNATURE_HEADERNAME
,
1192 strlen(DKIM_SIGNATURE_HEADERNAME
)) == 0)
1194 pdkim_signature
*new_sig
;
1196 /* Create and chain new signature block */
1197 DEBUG(D_acl
) debug_printf(
1198 "PDKIM >> Found sig, trying to parse >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
1200 if ((new_sig
= pdkim_parse_sig_header(ctx
, ctx
->cur_header
->str
)))
1202 pdkim_signature
*last_sig
= ctx
->sig
;
1207 while (last_sig
->next
) last_sig
= last_sig
->next
;
1208 last_sig
->next
= new_sig
;
1212 DEBUG(D_acl
) debug_printf(
1213 "Error while parsing signature header\n"
1214 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1217 /* every other header is stored for signature verification */
1220 pdkim_stringlist
*list
;
1222 if (!(list
= pdkim_prepend_stringlist(ctx
->headers
, ctx
->cur_header
->str
)))
1223 return PDKIM_ERR_OOM
;
1224 ctx
->headers
= list
;
1229 pdkim_strclear(ctx
->cur_header
); /* Re-use existing pdkim_str */
1235 /* -------------------------------------------------------------------------- */
1236 #define HEADER_BUFFER_FRAG_SIZE 256
1239 pdkim_feed (pdkim_ctx
*ctx
, char *data
, int len
)
1243 for (p
= 0; p
<len
; p
++)
1247 if (ctx
->past_headers
)
1249 /* Processing body byte */
1250 ctx
->linebuf
[ctx
->linebuf_offset
++] = c
;
1253 int rc
= pdkim_bodyline_complete(ctx
); /* End of line */
1254 if (rc
!= PDKIM_OK
) return rc
;
1256 if (ctx
->linebuf_offset
== (PDKIM_MAX_BODY_LINE_LEN
-1))
1257 return PDKIM_ERR_LONG_LINE
;
1261 /* Processing header byte */
1268 int rc
= pdkim_header_complete(ctx
); /* Seen last header line */
1269 if (rc
!= PDKIM_OK
) return rc
;
1271 ctx
->past_headers
= TRUE
;
1273 DEBUG(D_acl
) debug_printf(
1274 "PDKIM >> Hashed body data, canonicalized >>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
1278 ctx
->seen_lf
= TRUE
;
1280 else if (ctx
->seen_lf
)
1282 if (!(c
== '\t' || c
== ' '))
1284 int rc
= pdkim_header_complete(ctx
); /* End of header */
1285 if (rc
!= PDKIM_OK
) return rc
;
1287 ctx
->seen_lf
= FALSE
;
1291 if (!ctx
->cur_header
)
1292 if (!(ctx
->cur_header
= pdkim_strnew(NULL
)))
1293 return PDKIM_ERR_OOM
;
1295 if (ctx
->cur_header
->len
< PDKIM_MAX_HEADER_LEN
)
1296 if (!pdkim_strncat(ctx
->cur_header
, &data
[p
], 1))
1297 return PDKIM_ERR_OOM
;
1304 * RFC 5322 specifies that header line length SHOULD be no more than 78
1309 * col: this int holds and receives column number (octets since last '\n')
1310 * str: partial string to append to
1311 * pad: padding, split line or space after before or after eg: ";"
1312 * intro: - must join to payload eg "h=", usually the tag name
1313 * payload: eg base64 data - long data can be split arbitrarily.
1315 * this code doesn't fold the header in some of the places that RFC4871
1316 * allows: As per RFC5322(2.2.3) it only folds before or after tag-value
1317 * pairs and inside long values. it also always spaces or breaks after the
1320 * no guarantees are made for output given out-of range input. like tag
1321 * names loinger than 78, or bogus col. Input is assumed to be free of line breaks.
1325 pdkim_headcat(int *col
, pdkim_str
*str
, const char * pad
,
1326 const char *intro
, const char *payload
)
1335 pdkim_strcat(str
, "\r\n\t");
1338 pdkim_strncat(str
, pad
, l
);
1342 l
= (pad
?1:0) + (intro
?strlen(intro
):0);
1345 { /*can't fit intro - start a new line to make room.*/
1346 pdkim_strcat(str
, "\r\n\t");
1348 l
= intro
?strlen(intro
):0;
1351 l
+= payload
? strlen(payload
):0 ;
1354 { /* this fragment will not fit on a single line */
1357 pdkim_strcat(str
, " ");
1359 pad
= NULL
; /* only want this once */
1365 size_t sl
= strlen(intro
);
1367 pdkim_strncat(str
, intro
, sl
);
1370 intro
= NULL
; /* only want this once */
1375 size_t sl
= strlen(payload
);
1376 size_t chomp
= *col
+sl
< 77 ? sl
: 78-*col
;
1378 pdkim_strncat(str
, payload
, chomp
);
1384 /* the while precondition tells us it didn't fit. */
1385 pdkim_strcat(str
, "\r\n\t");
1391 pdkim_strcat(str
, "\r\n\t");
1398 pdkim_strcat(str
, " ");
1405 size_t sl
= strlen(intro
);
1407 pdkim_strncat(str
, intro
, sl
);
1415 size_t sl
= strlen(payload
);
1417 pdkim_strncat(str
, payload
, sl
);
1425 /* -------------------------------------------------------------------------- */
1428 pdkim_create_header(pdkim_signature
*sig
, int final
)
1431 char *base64_bh
= NULL
;
1432 char *base64_b
= NULL
;
1435 pdkim_str
*canon_all
;
1437 if (!(hdr
= pdkim_strnew("DKIM-Signature: v="PDKIM_SIGNATURE_VERSION
)))
1440 if (!(canon_all
= pdkim_strnew(pdkim_canons
[sig
->canon_headers
])))
1443 if (!(base64_bh
= pdkim_encode_base64(sig
->bodyhash
, sig
->bodyhash_len
)))
1446 col
= strlen(hdr
->str
);
1448 /* Required and static bits */
1449 if ( pdkim_headcat(&col
, hdr
, ";", "a=", pdkim_algos
[sig
->algo
])
1450 && pdkim_headcat(&col
, hdr
, ";", "q=", pdkim_querymethods
[sig
->querymethod
])
1451 && pdkim_strcat(canon_all
, "/")
1452 && pdkim_strcat(canon_all
, pdkim_canons
[sig
->canon_body
])
1453 && pdkim_headcat(&col
, hdr
, ";", "c=", canon_all
->str
)
1454 && pdkim_headcat(&col
, hdr
, ";", "d=", sig
->domain
)
1455 && pdkim_headcat(&col
, hdr
, ";", "s=", sig
->selector
)
1458 /* list of eader names can be split between items. */
1460 char *n
= strdup(sig
->headernames
);
1468 char *c
= strchr(n
, ':');
1473 if (!pdkim_headcat(&col
, hdr
, NULL
, NULL
, ":"))
1479 if (!pdkim_headcat(&col
, hdr
, s
, i
, n
))
1495 if(!pdkim_headcat(&col
, hdr
, ";", "bh=", base64_bh
))
1500 if(!pdkim_headcat(&col
, hdr
, ";", "i=", sig
->identity
))
1503 if (sig
->created
> 0)
1507 snprintf(minibuf
, 20, "%lu", sig
->created
);
1508 if(!pdkim_headcat(&col
, hdr
, ";", "t=", minibuf
))
1512 if (sig
->expires
> 0)
1516 snprintf(minibuf
, 20, "%lu", sig
->expires
);
1517 if(!pdkim_headcat(&col
, hdr
, ";", "x=", minibuf
))
1521 if (sig
->bodylength
>= 0)
1525 snprintf(minibuf
, 20, "%lu", sig
->bodylength
);
1526 if(!pdkim_headcat(&col
, hdr
, ";", "l=", minibuf
))
1530 /* Preliminary or final version? */
1533 if (!(base64_b
= pdkim_encode_base64(sig
->sigdata
, sig
->sigdata_len
)))
1535 if (!pdkim_headcat(&col
, hdr
, ";", "b=", base64_b
))
1539 if(!pdkim_headcat(&col
, hdr
, ";", "b=", ""))
1542 /* add trailing semicolon: I'm not sure if this is actually needed */
1543 if (!pdkim_headcat(&col
, hdr
, NULL
, ";", ""))
1547 rc
= strdup(hdr
->str
);
1551 if (canon_all
) pdkim_strfree(canon_all
);
1556 /* -------------------------------------------------------------------------- */
1559 pdkim_feed_finish(pdkim_ctx
*ctx
, pdkim_signature
**return_signatures
)
1561 pdkim_signature
*sig
= ctx
->sig
;
1562 pdkim_str
*headernames
= NULL
; /* Collected signed header names */
1564 /* Check if we must still flush a (partial) header. If that is the
1565 case, the message has no body, and we must compute a body hash
1566 out of '<CR><LF>' */
1567 if (ctx
->cur_header
&& ctx
->cur_header
->len
)
1569 int rc
= pdkim_header_complete(ctx
);
1570 if (rc
!= PDKIM_OK
) return rc
;
1571 pdkim_update_bodyhash(ctx
, "\r\n", 2);
1574 DEBUG(D_acl
) debug_printf(
1575 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1577 /* Build (and/or evaluate) body hash */
1578 if (pdkim_finish_bodyhash(ctx
) != PDKIM_OK
)
1579 return PDKIM_ERR_OOM
;
1581 /* SIGNING -------------------------------------------------------------- */
1582 if (ctx
->mode
== PDKIM_MODE_SIGN
)
1583 if (!(headernames
= pdkim_strnew(NULL
)))
1584 return PDKIM_ERR_OOM
;
1585 /* ---------------------------------------------------------------------- */
1589 sha1_context sha1_headers
;
1590 sha2_context sha2_headers
;
1592 char headerhash
[32];
1594 if (sig
->algo
== PDKIM_ALGO_RSA_SHA1
)
1595 sha1_starts(&sha1_headers
);
1597 sha2_starts(&sha2_headers
, 0);
1599 DEBUG(D_acl
) debug_printf(
1600 "PDKIM >> Hashed header data, canonicalized, in sequence >>>>>>>>>>>>>>\n");
1602 /* SIGNING ---------------------------------------------------------------- */
1603 /* When signing, walk through our header list and add them to the hash. As we
1604 go, construct a list of the header's names to use for the h= parameter. */
1606 if (ctx
->mode
== PDKIM_MODE_SIGN
)
1608 pdkim_stringlist
*p
;
1610 for (p
= sig
->headers
; p
; p
= p
->next
)
1613 /* Collect header names (Note: colon presence is guaranteed here) */
1614 char *q
= strchr(p
->value
, ':');
1616 if (!(pdkim_strncat(headernames
, p
->value
,
1617 (q
-(p
->value
)) + (p
->next
? 1 : 0))))
1618 return PDKIM_ERR_OOM
;
1620 rh
= sig
->canon_headers
== PDKIM_CANON_RELAXED
1621 ? pdkim_relax_header(p
->value
, 1) /* cook header for relaxed canon */
1622 : strdup(p
->value
); /* just copy it for simple canon */
1624 return PDKIM_ERR_OOM
;
1626 /* Feed header to the hash algorithm */
1627 if (sig
->algo
== PDKIM_ALGO_RSA_SHA1
)
1628 sha1_update(&(sha1_headers
), (unsigned char *)rh
, strlen(rh
));
1630 sha2_update(&(sha2_headers
), (unsigned char *)rh
, strlen(rh
));
1632 DEBUG(D_acl
) pdkim_quoteprint(rh
, strlen(rh
), 1);
1637 /* VERIFICATION ----------------------------------------------------------- */
1638 /* When verifying, walk through the header name list in the h= parameter and
1639 add the headers to the hash in that order. */
1642 char *b
= strdup(sig
->headernames
);
1645 pdkim_stringlist
*hdrs
;
1647 if (!b
) return PDKIM_ERR_OOM
;
1650 for (hdrs
= ctx
->headers
; hdrs
; hdrs
= hdrs
->next
)
1655 if ((q
= strchr(p
, ':')))
1658 for (hdrs
= ctx
->headers
; hdrs
; hdrs
= hdrs
->next
)
1660 && strncasecmp(hdrs
->value
, p
, strlen(p
)) == 0
1661 && (hdrs
->value
)[strlen(p
)] == ':'
1666 rh
= sig
->canon_headers
== PDKIM_CANON_RELAXED
1667 ? pdkim_relax_header(hdrs
->value
, 1) /* cook header for relaxed canon */
1668 : strdup(hdrs
->value
); /* just copy it for simple canon */
1670 return PDKIM_ERR_OOM
;
1672 /* Feed header to the hash algorithm */
1673 if (sig
->algo
== PDKIM_ALGO_RSA_SHA1
)
1674 sha1_update(&sha1_headers
, (unsigned char *)rh
, strlen(rh
));
1676 sha2_update(&sha2_headers
, (unsigned char *)rh
, strlen(rh
));
1678 DEBUG(D_acl
) pdkim_quoteprint(rh
, strlen(rh
), 1);
1690 DEBUG(D_acl
) debug_printf(
1691 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1693 /* SIGNING ---------------------------------------------------------------- */
1694 if (ctx
->mode
== PDKIM_MODE_SIGN
)
1696 /* Copy headernames to signature struct */
1697 sig
->headernames
= strdup(headernames
->str
);
1698 pdkim_strfree(headernames
);
1700 /* Create signature header with b= omitted */
1701 sig_hdr
= pdkim_create_header(ctx
->sig
, 0);
1704 /* VERIFICATION ----------------------------------------------------------- */
1706 sig_hdr
= strdup(sig
->rawsig_no_b_val
);
1707 /* ------------------------------------------------------------------------ */
1710 return PDKIM_ERR_OOM
;
1712 /* Relax header if necessary */
1713 if (sig
->canon_headers
== PDKIM_CANON_RELAXED
)
1715 char *relaxed_hdr
= pdkim_relax_header(sig_hdr
, 0);
1719 return PDKIM_ERR_OOM
;
1720 sig_hdr
= relaxed_hdr
;
1726 "PDKIM >> Signed DKIM-Signature header, canonicalized >>>>>>>>>>>>>>>>>\n");
1727 pdkim_quoteprint(sig_hdr
, strlen(sig_hdr
), 1);
1729 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1732 /* Finalize header hash */
1733 if (sig
->algo
== PDKIM_ALGO_RSA_SHA1
)
1735 sha1_update(&sha1_headers
, (unsigned char *)sig_hdr
, strlen(sig_hdr
));
1736 sha1_finish(&sha1_headers
, (unsigned char *)headerhash
);
1740 debug_printf( "PDKIM [%s] hh computed: ", sig
->domain
);
1741 pdkim_hexprint(headerhash
, 20, 1);
1746 sha2_update(&sha2_headers
, (unsigned char *)sig_hdr
, strlen(sig_hdr
));
1747 sha2_finish(&sha2_headers
, (unsigned char *)headerhash
);
1751 debug_printf("PDKIM [%s] hh computed: ", sig
->domain
);
1752 pdkim_hexprint(headerhash
, 32, 1);
1758 /* SIGNING ---------------------------------------------------------------- */
1759 if (ctx
->mode
== PDKIM_MODE_SIGN
)
1764 rsa_init(&rsa
, RSA_PKCS_V15
, 0);
1766 /* Perform private key operation */
1767 if ((perr
= rsa_parse_key(&rsa
, (unsigned char *)sig
->rsa_privkey
,
1768 strlen(sig
->rsa_privkey
), NULL
, 0)) != 0)
1770 debug_printf("rsa_parse_key: perr 0x%x\n", perr
);
1771 return PDKIM_ERR_RSA_PRIVKEY
;
1774 sig
->sigdata_len
= mpi_size(&(rsa
.N
));
1775 sig
->sigdata
= store_get(sig
->sigdata_len
);
1777 if (rsa_pkcs1_sign( &rsa
, RSA_PRIVATE
,
1778 ((sig
->algo
== PDKIM_ALGO_RSA_SHA1
)?
1779 SIG_RSA_SHA1
:SIG_RSA_SHA256
),
1781 (unsigned char *)headerhash
,
1782 (unsigned char *)sig
->sigdata
) != 0)
1783 return PDKIM_ERR_RSA_SIGNING
;
1789 debug_printf( "PDKIM [%s] b computed: ", sig
->domain
);
1790 pdkim_hexprint(sig
->sigdata
, sig
->sigdata_len
, 1);
1793 if (!(sig
->signature_header
= pdkim_create_header(ctx
->sig
, 1)))
1794 return PDKIM_ERR_OOM
;
1797 /* VERIFICATION ----------------------------------------------------------- */
1801 char *dns_txt_name
, *dns_txt_reply
;
1803 rsa_init(&rsa
, RSA_PKCS_V15
, 0);
1805 if (!(dns_txt_name
= malloc(PDKIM_DNS_TXT_MAX_NAMELEN
)))
1806 return PDKIM_ERR_OOM
;
1808 if (!(dns_txt_reply
= malloc(PDKIM_DNS_TXT_MAX_RECLEN
)))
1811 return PDKIM_ERR_OOM
;
1814 memset(dns_txt_reply
, 0, PDKIM_DNS_TXT_MAX_RECLEN
);
1815 memset(dns_txt_name
, 0, PDKIM_DNS_TXT_MAX_NAMELEN
);
1817 if (snprintf(dns_txt_name
, PDKIM_DNS_TXT_MAX_NAMELEN
,
1818 "%s._domainkey.%s.",
1819 sig
->selector
, sig
->domain
) >= PDKIM_DNS_TXT_MAX_NAMELEN
)
1821 sig
->verify_status
= PDKIM_VERIFY_INVALID
;
1822 sig
->verify_ext_status
= PDKIM_VERIFY_INVALID_BUFFER_SIZE
;
1826 if ( ctx
->dns_txt_callback(dns_txt_name
, dns_txt_reply
) != PDKIM_OK
1827 || dns_txt_reply
[0] == '\0')
1829 sig
->verify_status
= PDKIM_VERIFY_INVALID
;
1830 sig
->verify_ext_status
= PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE
;
1837 "PDKIM >> Parsing public key record >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"
1839 pdkim_quoteprint(dns_txt_reply
, strlen(dns_txt_reply
), 1);
1842 if (!(sig
->pubkey
= pdkim_parse_pubkey_record(ctx
, dns_txt_reply
)))
1844 sig
->verify_status
= PDKIM_VERIFY_INVALID
;
1845 sig
->verify_ext_status
= PDKIM_VERIFY_INVALID_PUBKEY_PARSING
;
1847 DEBUG(D_acl
) debug_printf(
1848 " Error while parsing public key record\n"
1849 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1853 DEBUG(D_acl
) debug_printf(
1854 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1856 if (rsa_parse_public_key(&rsa
,
1857 (unsigned char *)sig
->pubkey
->key
,
1858 sig
->pubkey
->key_len
) != 0)
1860 sig
->verify_status
= PDKIM_VERIFY_INVALID
;
1861 sig
->verify_ext_status
= PDKIM_VERIFY_INVALID_PUBKEY_PARSING
;
1865 /* Check the signature */
1866 if (rsa_pkcs1_verify(&rsa
,
1868 ((sig
->algo
== PDKIM_ALGO_RSA_SHA1
)?
1869 SIG_RSA_SHA1
:SIG_RSA_SHA256
),
1871 (unsigned char *)headerhash
,
1872 (unsigned char *)sig
->sigdata
) != 0)
1874 sig
->verify_status
= PDKIM_VERIFY_FAIL
;
1875 sig
->verify_ext_status
= PDKIM_VERIFY_FAIL_MESSAGE
;
1879 /* We have a winner! (if bodydhash was correct earlier) */
1880 if (sig
->verify_status
== PDKIM_VERIFY_NONE
)
1881 sig
->verify_status
= PDKIM_VERIFY_PASS
;
1887 debug_printf("PDKIM [%s] signature status: %s",
1888 sig
->domain
, pdkim_verify_status_str(sig
->verify_status
));
1889 if (sig
->verify_ext_status
> 0)
1890 debug_printf(" (%s)\n",
1891 pdkim_verify_ext_status_str(sig
->verify_ext_status
));
1898 free(dns_txt_reply
);
1904 /* If requested, set return pointer to signature(s) */
1905 if (return_signatures
)
1906 *return_signatures
= ctx
->sig
;
1912 /* -------------------------------------------------------------------------- */
1914 DLLEXPORT pdkim_ctx
*
1915 pdkim_init_verify(int(*dns_txt_callback
)(char *, char *))
1917 pdkim_ctx
*ctx
= malloc(sizeof(pdkim_ctx
));
1921 memset(ctx
, 0, sizeof(pdkim_ctx
));
1923 if (!(ctx
->linebuf
= malloc(PDKIM_MAX_BODY_LINE_LEN
)))
1929 ctx
->mode
= PDKIM_MODE_VERIFY
;
1930 ctx
->dns_txt_callback
= dns_txt_callback
;
1936 /* -------------------------------------------------------------------------- */
1938 DLLEXPORT pdkim_ctx
*
1939 pdkim_init_sign(char *domain
, char *selector
, char *rsa_privkey
)
1942 pdkim_signature
*sig
;
1944 if (!domain
|| !selector
|| !rsa_privkey
)
1947 if (!(ctx
= malloc(sizeof(pdkim_ctx
))))
1949 memset(ctx
, 0, sizeof(pdkim_ctx
));
1951 if (!(ctx
->linebuf
= malloc(PDKIM_MAX_BODY_LINE_LEN
)))
1957 if (!(sig
= malloc(sizeof(pdkim_signature
))))
1963 memset(sig
, 0, sizeof(pdkim_signature
));
1965 sig
->bodylength
= -1;
1967 ctx
->mode
= PDKIM_MODE_SIGN
;
1970 ctx
->sig
->domain
= strdup(domain
);
1971 ctx
->sig
->selector
= strdup(selector
);
1972 ctx
->sig
->rsa_privkey
= strdup(rsa_privkey
);
1974 if (!ctx
->sig
->domain
|| !ctx
->sig
->selector
|| !ctx
->sig
->rsa_privkey
)
1977 if (!(ctx
->sig
->sha1_body
= malloc(sizeof(sha1_context
))))
1979 sha1_starts(ctx
->sig
->sha1_body
);
1981 if (!(ctx
->sig
->sha2_body
= malloc(sizeof(sha2_context
))))
1983 sha2_starts(ctx
->sig
->sha2_body
, 0);
1988 pdkim_free_ctx(ctx
);
1992 /* -------------------------------------------------------------------------- */
1995 pdkim_set_optional(pdkim_ctx
*ctx
,
2002 unsigned long created
,
2003 unsigned long expires
)
2007 if (!(ctx
->sig
->identity
= strdup(identity
)))
2008 return PDKIM_ERR_OOM
;
2011 if (!(ctx
->sig
->sign_headers
= strdup(sign_headers
)))
2012 return PDKIM_ERR_OOM
;
2014 ctx
->sig
->canon_headers
= canon_headers
;
2015 ctx
->sig
->canon_body
= canon_body
;
2016 ctx
->sig
->bodylength
= bodylength
;
2017 ctx
->sig
->algo
= algo
;
2018 ctx
->sig
->created
= created
;
2019 ctx
->sig
->expires
= expires
;