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.14 2010/05/29 19:14:06 nm4 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 const char *pdkim_querymethods
[] = {
72 const char *pdkim_algos
[] = {
77 const char *pdkim_canons
[] = {
82 const char *pdkim_hashes
[] = {
87 const 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 const 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 const 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
, const char *data
, int len
, int lf
) {
134 const unsigned char *p
= (const 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
, const char *data
, int len
, int lf
) {
158 const unsigned char *p
= (const 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 (const 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
);
214 else p
->str
[p
->len
] = '\0';
217 char *pdkim_strncat(pdkim_str
*str
, const char *data
, int len
) {
218 if ((str
->allocated
- str
->len
) < (len
+1)) {
219 /* Extend the buffer */
220 int num_frags
= ((len
+1)/PDKIM_STR_ALLOC_FRAG
)+1;
221 char *n
= realloc(str
->str
,
222 (str
->allocated
+(num_frags
*PDKIM_STR_ALLOC_FRAG
)));
223 if (n
== NULL
) return NULL
;
225 str
->allocated
+= (num_frags
*PDKIM_STR_ALLOC_FRAG
);
227 strncpy(&(str
->str
[str
->len
]),data
,len
);
229 str
->str
[str
->len
] = '\0';
232 char *pdkim_strcat(pdkim_str
*str
, const char *cstr
) {
233 return pdkim_strncat(str
, cstr
, strlen(cstr
));
235 char *pdkim_numcat(pdkim_str
*str
, unsigned long num
) {
237 snprintf(minibuf
,20,"%lu",num
);
238 return pdkim_strcat(str
,minibuf
);
240 char *pdkim_strtrim(pdkim_str
*str
) {
243 while ( (*p
!= '\0') && ((*p
== '\t') || (*p
== ' ')) ) p
++;
244 while (*p
!= '\0') {*q
= *p
; q
++; p
++;}
246 while ( (q
!= str
->str
) && ( (*q
== '\0') || (*q
== '\t') || (*q
== ' ') ) ) {
250 str
->len
= strlen(str
->str
);
253 char *pdkim_strclear(pdkim_str
*str
) {
258 void pdkim_strfree(pdkim_str
*str
) {
259 if (str
== NULL
) return;
260 if (str
->str
!= NULL
) free(str
->str
);
266 /* -------------------------------------------------------------------------- */
267 void pdkim_free_pubkey(pdkim_pubkey
*pub
) {
269 if (pub
->version
!= NULL
) free(pub
->version
);
270 if (pub
->granularity
!= NULL
) free(pub
->granularity
);
271 if (pub
->hashes
!= NULL
) free(pub
->hashes
);
272 if (pub
->keytype
!= NULL
) free(pub
->keytype
);
273 if (pub
->srvtype
!= NULL
) free(pub
->srvtype
);
274 if (pub
->notes
!= NULL
) free(pub
->notes
);
275 if (pub
->key
!= NULL
) free(pub
->key
);
281 /* -------------------------------------------------------------------------- */
282 void pdkim_free_sig(pdkim_signature
*sig
) {
284 pdkim_signature
*next
= (pdkim_signature
*)sig
->next
;
286 pdkim_stringlist
*e
= sig
->headers
;
288 pdkim_stringlist
*c
= e
;
289 if (e
->value
!= NULL
) free(e
->value
);
294 if (sig
->sigdata
!= NULL
) free(sig
->sigdata
);
295 if (sig
->bodyhash
!= NULL
) free(sig
->bodyhash
);
296 if (sig
->selector
!= NULL
) free(sig
->selector
);
297 if (sig
->domain
!= NULL
) free(sig
->domain
);
298 if (sig
->identity
!= NULL
) free(sig
->identity
);
299 if (sig
->headernames
!= NULL
) free(sig
->headernames
);
300 if (sig
->copiedheaders
!= NULL
) free(sig
->copiedheaders
);
301 if (sig
->rsa_privkey
!= NULL
) free(sig
->rsa_privkey
);
302 if (sig
->sign_headers
!= NULL
) free(sig
->sign_headers
);
303 if (sig
->signature_header
!= NULL
) free(sig
->signature_header
);
304 if (sig
->sha1_body
!= NULL
) free(sig
->sha1_body
);
305 if (sig
->sha2_body
!= NULL
) free(sig
->sha2_body
);
306 if (sig
->hnames_check
!= NULL
) free(sig
->hnames_check
);
308 if (sig
->pubkey
!= NULL
) pdkim_free_pubkey(sig
->pubkey
);
311 if (next
!= NULL
) pdkim_free_sig(next
);
316 /* -------------------------------------------------------------------------- */
317 DLLEXPORT
void pdkim_free_ctx(pdkim_ctx
*ctx
) {
319 pdkim_free_sig(ctx
->sig
);
320 pdkim_strfree(ctx
->cur_header
);
326 /* -------------------------------------------------------------------------- */
327 /* Matches the name of the passed raw "header" against
328 the passed colon-separated "list", starting at entry
329 "start". Returns the position of the header name in
331 int header_name_match(const char *header
,
340 /* Get header name */
341 char *hcolon
= strchr(header
,':');
342 if (hcolon
== NULL
) return rc
; /* This isn't a header */
343 hname
= malloc((hcolon
-header
)+1);
344 if (hname
== NULL
) return PDKIM_ERR_OOM
;
345 memset(hname
,0,(hcolon
-header
)+1);
346 strncpy(hname
,header
,(hcolon
-header
));
348 /* Copy tick-off list locally, so we can punch zeroes into it */
349 lcopy
= strdup(tick
);
352 return PDKIM_ERR_OOM
;
359 if (strcasecmp(p
,hname
) == 0) {
361 /* Invalidate header name instance in tick-off list */
362 if (do_tick
) tick
[p
-lcopy
] = '_';
370 if (strcasecmp(p
,hname
) == 0) {
372 /* Invalidate header name instance in tick-off list */
373 if (do_tick
) tick
[p
-lcopy
] = '_';
383 /* -------------------------------------------------------------------------- */
384 /* Performs "relaxed" canonicalization of a header. The returned pointer needs
386 char *pdkim_relax_header (char *header
, int crlf
) {
387 int past_field_name
= 0;
391 char *relaxed
= malloc(strlen(header
)+3);
392 if (relaxed
== NULL
) return NULL
;
397 if ( (c
== '\r') || (c
== '\n') ) {
401 if ( (c
== '\t') || (c
== ' ') ) {
402 c
= ' '; /* Turns WSP into SP */
410 if ( (!past_field_name
) && (c
== ':') ) {
411 if (seen_wsp
) q
--; /* This removes WSP before the colon */
412 seen_wsp
= 1; /* This removes WSP after the colon */
417 /* Lowercase header name */
418 if (!past_field_name
) c
= tolower(c
);
425 if (crlf
) strcat(relaxed
,"\r\n");
430 /* -------------------------------------------------------------------------- */
431 #define PDKIM_QP_ERROR_DECODE -1
432 char *pdkim_decode_qp_char(char *qp_p
, int *c
) {
433 char *initial_pos
= qp_p
;
435 /* Advance one char */
438 /* Check for two hex digits and decode them */
439 if (isxdigit(*qp_p
) && isxdigit(qp_p
[1])) {
440 /* Do hex conversion */
441 if (isdigit(*qp_p
)) {*c
= *qp_p
- '0';}
442 else {*c
= toupper(*qp_p
) - 'A' + 10;}
444 if (isdigit(qp_p
[1])) {*c
|= qp_p
[1] - '0';}
445 else {*c
|= toupper(qp_p
[1]) - 'A' + 10;}
449 /* Illegal char here */
450 *c
= PDKIM_QP_ERROR_DECODE
;
455 /* -------------------------------------------------------------------------- */
456 char *pdkim_decode_qp(char *str
) {
460 char *n
= malloc(strlen(p
)+1);
461 if (n
== NULL
) return NULL
;
466 p
= pdkim_decode_qp_char(p
,&nchar
);
484 /* -------------------------------------------------------------------------- */
485 char *pdkim_decode_base64(char *str
, int *num_decoded
) {
489 base64_decode(NULL
, &dlen
, (unsigned char *)str
, strlen(str
));
490 res
= malloc(dlen
+1);
491 if (res
== NULL
) return NULL
;
492 if (base64_decode((unsigned char *)res
,&dlen
,(unsigned char *)str
,strlen(str
)) != 0) {
496 if (num_decoded
!= NULL
) *num_decoded
= dlen
;
500 /* -------------------------------------------------------------------------- */
501 char *pdkim_encode_base64(char *str
, int num
) {
505 base64_encode(NULL
, &dlen
, (unsigned char *)str
, num
);
506 res
= malloc(dlen
+1);
507 if (res
== NULL
) return NULL
;
508 if (base64_encode((unsigned char *)res
,&dlen
,(unsigned char *)str
,num
) != 0) {
516 /* -------------------------------------------------------------------------- */
517 #define PDKIM_HDR_LIMBO 0
518 #define PDKIM_HDR_TAG 1
519 #define PDKIM_HDR_VALUE 2
520 pdkim_signature
*pdkim_parse_sig_header(pdkim_ctx
*ctx
, char *raw_hdr
) {
521 pdkim_signature
*sig
;
523 pdkim_str
*cur_tag
= NULL
;
524 pdkim_str
*cur_val
= NULL
;
527 int where
= PDKIM_HDR_LIMBO
;
530 sig
= malloc(sizeof(pdkim_signature
));
531 if (sig
== NULL
) return NULL
;
532 memset(sig
,0,sizeof(pdkim_signature
));
533 sig
->bodylength
= -1;
535 sig
->rawsig_no_b_val
= malloc(strlen(raw_hdr
)+1);
536 if (sig
->rawsig_no_b_val
== NULL
) {
542 q
= sig
->rawsig_no_b_val
;
547 if ( (*p
== '\r') || (*p
== '\n') )
550 /* Fast-forward through header name */
552 if (*p
== ':') past_hname
= 1;
556 if (where
== PDKIM_HDR_LIMBO
) {
557 /* In limbo, just wait for a tag-char to appear */
558 if (!((*p
>= 'a') && (*p
<= 'z')))
561 where
= PDKIM_HDR_TAG
;
564 if (where
== PDKIM_HDR_TAG
) {
566 cur_tag
= pdkim_strnew(NULL
);
568 if ((*p
>= 'a') && (*p
<= 'z'))
569 pdkim_strncat(cur_tag
,p
,1);
572 if (strcmp(cur_tag
->str
,"b") == 0) {
576 where
= PDKIM_HDR_VALUE
;
581 if (where
== PDKIM_HDR_VALUE
) {
583 cur_val
= pdkim_strnew(NULL
);
585 if ( (*p
== '\r') || (*p
== '\n') || (*p
== ' ') || (*p
== '\t') )
588 if ( (*p
== ';') || (*p
== '\0') ) {
589 if (cur_tag
->len
> 0) {
590 pdkim_strtrim(cur_val
);
592 if (ctx
->debug_stream
)
593 fprintf(ctx
->debug_stream
, "%s=%s\n", cur_tag
->str
, cur_val
->str
);
595 switch (cur_tag
->str
[0]) {
597 switch (cur_tag
->str
[1]) {
599 sig
->bodyhash
= pdkim_decode_base64(cur_val
->str
,&(sig
->bodyhash_len
));
602 sig
->sigdata
= pdkim_decode_base64(cur_val
->str
,&(sig
->sigdata_len
));
607 if (strcmp(cur_val
->str
,PDKIM_SIGNATURE_VERSION
) == 0) {
608 /* We only support version 1, and that is currently the
609 only version there is. */
615 while (pdkim_algos
[i
] != NULL
) {
616 if (strcmp(cur_val
->str
,pdkim_algos
[i
]) == 0 ) {
625 while (pdkim_combined_canons
[i
].str
!= NULL
) {
626 if (strcmp(cur_val
->str
,pdkim_combined_canons
[i
].str
) == 0 ) {
627 sig
->canon_headers
= pdkim_combined_canons
[i
].canon_headers
;
628 sig
->canon_body
= pdkim_combined_canons
[i
].canon_body
;
636 while (pdkim_querymethods
[i
] != NULL
) {
637 if (strcmp(cur_val
->str
,pdkim_querymethods
[i
]) == 0 ) {
638 sig
->querymethod
= i
;
645 sig
->selector
= strdup(cur_val
->str
);
648 sig
->domain
= strdup(cur_val
->str
);
651 sig
->identity
= pdkim_decode_qp(cur_val
->str
);
654 sig
->created
= strtoul(cur_val
->str
,NULL
,10);
657 sig
->expires
= strtoul(cur_val
->str
,NULL
,10);
660 sig
->bodylength
= strtol(cur_val
->str
,NULL
,10);
663 sig
->headernames
= strdup(cur_val
->str
);
666 sig
->copiedheaders
= pdkim_decode_qp(cur_val
->str
);
670 if (ctx
->debug_stream
)
671 fprintf(ctx
->debug_stream
, "Unknown tag encountered\n");
676 pdkim_strclear(cur_tag
);
677 pdkim_strclear(cur_val
);
679 where
= PDKIM_HDR_LIMBO
;
682 else pdkim_strncat(cur_val
,p
,1);
686 if (*p
== '\0') break;
695 /* Make sure the most important bits are there. */
696 if (!(sig
->domain
&& (*(sig
->domain
) != '\0') &&
697 sig
->selector
&& (*(sig
->selector
) != '\0') &&
698 sig
->headernames
&& (*(sig
->headernames
) != '\0') &&
706 /* Copy header list to 'tick-off' header list */
707 sig
->hnames_check
= strdup(sig
->headernames
);
710 /* Chomp raw header. The final newline must not be added to the signature. */
712 while( (q
> sig
->rawsig_no_b_val
) && ((*q
== '\r') || (*q
== '\n')) ) {
717 if (ctx
->debug_stream
) {
718 fprintf(ctx
->debug_stream
,
719 "PDKIM >> Raw signature w/o b= tag value >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
720 pdkim_quoteprint(ctx
->debug_stream
,
721 sig
->rawsig_no_b_val
,
722 strlen(sig
->rawsig_no_b_val
), 1);
723 fprintf(ctx
->debug_stream
,
724 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
728 sig
->sha1_body
= malloc(sizeof(sha1_context
));
729 if (sig
->sha1_body
== NULL
) {
733 sig
->sha2_body
= malloc(sizeof(sha2_context
));
734 if (sig
->sha2_body
== NULL
) {
739 sha1_starts(sig
->sha1_body
);
740 sha2_starts(sig
->sha2_body
,0);
746 /* -------------------------------------------------------------------------- */
747 pdkim_pubkey
*pdkim_parse_pubkey_record(pdkim_ctx
*ctx
, char *raw_record
) {
750 pdkim_str
*cur_tag
= NULL
;
751 pdkim_str
*cur_val
= NULL
;
752 int where
= PDKIM_HDR_LIMBO
;
754 pub
= malloc(sizeof(pdkim_pubkey
));
755 if (pub
== NULL
) return NULL
;
756 memset(pub
,0,sizeof(pdkim_pubkey
));
763 if ( (*p
== '\r') || (*p
== '\n') )
766 if (where
== PDKIM_HDR_LIMBO
) {
767 /* In limbo, just wait for a tag-char to appear */
768 if (!((*p
>= 'a') && (*p
<= 'z')))
771 where
= PDKIM_HDR_TAG
;
774 if (where
== PDKIM_HDR_TAG
) {
776 cur_tag
= pdkim_strnew(NULL
);
778 if ((*p
>= 'a') && (*p
<= 'z'))
779 pdkim_strncat(cur_tag
,p
,1);
782 where
= PDKIM_HDR_VALUE
;
787 if (where
== PDKIM_HDR_VALUE
) {
789 cur_val
= pdkim_strnew(NULL
);
791 if ( (*p
== '\r') || (*p
== '\n') )
794 if ( (*p
== ';') || (*p
== '\0') ) {
795 if (cur_tag
->len
> 0) {
796 pdkim_strtrim(cur_val
);
798 if (ctx
->debug_stream
)
799 fprintf(ctx
->debug_stream
, "%s=%s\n", cur_tag
->str
, cur_val
->str
);
801 switch (cur_tag
->str
[0]) {
803 /* This tag isn't evaluated because:
804 - We only support version DKIM1.
805 - Which is the default for this value (set below)
806 - Other versions are currently not specified. */
809 pub
->hashes
= strdup(cur_val
->str
);
812 pub
->granularity
= strdup(cur_val
->str
);
815 pub
->notes
= pdkim_decode_qp(cur_val
->str
);
818 pub
->key
= pdkim_decode_base64(cur_val
->str
,&(pub
->key_len
));
821 pub
->hashes
= strdup(cur_val
->str
);
824 pub
->srvtype
= strdup(cur_val
->str
);
827 if (strchr(cur_val
->str
,'y') != NULL
) pub
->testing
= 1;
828 if (strchr(cur_val
->str
,'s') != NULL
) pub
->no_subdomaining
= 1;
832 if (ctx
->debug_stream
)
833 fprintf(ctx
->debug_stream
, "Unknown tag encountered\n");
838 pdkim_strclear(cur_tag
);
839 pdkim_strclear(cur_val
);
840 where
= PDKIM_HDR_LIMBO
;
843 else pdkim_strncat(cur_val
,p
,1);
847 if (*p
== '\0') break;
851 /* Set fallback defaults */
852 if (pub
->version
== NULL
) pub
->version
= strdup(PDKIM_PUB_RECORD_VERSION
);
853 if (pub
->granularity
== NULL
) pub
->granularity
= strdup("*");
854 if (pub
->keytype
== NULL
) pub
->keytype
= strdup("rsa");
855 if (pub
->srvtype
== NULL
) pub
->srvtype
= strdup("*");
858 if (pub
->key
== NULL
) {
859 pdkim_free_pubkey(pub
);
867 /* -------------------------------------------------------------------------- */
868 int pdkim_update_bodyhash(pdkim_ctx
*ctx
, const char *data
, int len
) {
869 pdkim_signature
*sig
= ctx
->sig
;
870 /* Cache relaxed version of data */
871 char *relaxed_data
= NULL
;
874 /* Traverse all signatures, updating their hashes. */
875 while (sig
!= NULL
) {
876 /* Defaults to simple canon (no further treatment necessary) */
877 const char *canon_data
= data
;
880 if (sig
->canon_body
== PDKIM_CANON_RELAXED
) {
881 /* Relax the line if not done already */
882 if (relaxed_data
== NULL
) {
884 const char *p
= data
;
886 relaxed_data
= malloc(len
+1);
887 if (relaxed_data
== NULL
) return PDKIM_ERR_OOM
;
891 if ( (q
> 0) && (relaxed_data
[q
-1] == ' ') ) q
--;
893 else if ( (c
== '\t') || (c
== ' ') ) {
894 c
= ' '; /* Turns WSP into SP */
902 relaxed_data
[q
++] = c
;
905 relaxed_data
[q
] = '\0';
908 canon_data
= relaxed_data
;
909 canon_len
= relaxed_len
;
912 /* Make sure we don't exceed the to-be-signed body length */
913 if ((sig
->bodylength
>= 0) &&
914 ((sig
->signed_body_bytes
+(unsigned long)canon_len
) > sig
->bodylength
))
915 canon_len
= (sig
->bodylength
- sig
->signed_body_bytes
);
918 if (sig
->algo
== PDKIM_ALGO_RSA_SHA1
)
919 sha1_update(sig
->sha1_body
,(unsigned char *)canon_data
,canon_len
);
921 sha2_update(sig
->sha2_body
,(unsigned char *)canon_data
,canon_len
);
922 sig
->signed_body_bytes
+= canon_len
;
924 if (ctx
->debug_stream
!=NULL
)
925 pdkim_quoteprint(ctx
->debug_stream
,canon_data
,canon_len
,0);
932 if (relaxed_data
!= NULL
) free(relaxed_data
);
937 /* -------------------------------------------------------------------------- */
938 int pdkim_finish_bodyhash(pdkim_ctx
*ctx
) {
939 pdkim_signature
*sig
= ctx
->sig
;
941 /* Traverse all signatures */
942 while (sig
!= NULL
) {
945 unsigned char bh
[32]; /* SHA-256 = 32 Bytes, SHA-1 = 20 Bytes */
946 if (sig
->algo
== PDKIM_ALGO_RSA_SHA1
)
947 sha1_finish(sig
->sha1_body
,bh
);
949 sha2_finish(sig
->sha2_body
,bh
);
952 if (ctx
->debug_stream
) {
953 fprintf(ctx
->debug_stream
, "PDKIM [%s] Body bytes hashed: %lu\n",
954 sig
->domain
, sig
->signed_body_bytes
);
955 fprintf(ctx
->debug_stream
, "PDKIM [%s] bh computed: ", sig
->domain
);
956 pdkim_hexprint(ctx
->debug_stream
, (char *)bh
,
957 (sig
->algo
== PDKIM_ALGO_RSA_SHA1
)?20:32,1);
961 /* SIGNING -------------------------------------------------------------- */
962 if (ctx
->mode
== PDKIM_MODE_SIGN
) {
963 sig
->bodyhash_len
= (sig
->algo
== PDKIM_ALGO_RSA_SHA1
)?20:32;
964 sig
->bodyhash
= malloc(sig
->bodyhash_len
);
965 if (sig
->bodyhash
== NULL
) return PDKIM_ERR_OOM
;
966 memcpy(sig
->bodyhash
,bh
,sig
->bodyhash_len
);
968 /* If bodylength limit is set, and we have received less bytes
969 than the requested amount, effectively remove the limit tag. */
970 if (sig
->signed_body_bytes
< sig
->bodylength
) sig
->bodylength
= -1;
972 /* VERIFICATION --------------------------------------------------------- */
974 /* Compare bodyhash */
975 if (memcmp(bh
,sig
->bodyhash
,
976 (sig
->algo
== PDKIM_ALGO_RSA_SHA1
)?20:32) == 0) {
978 if (ctx
->debug_stream
)
979 fprintf(ctx
->debug_stream
, "PDKIM [%s] Body hash verified OK\n",
985 if (ctx
->debug_stream
) {
986 fprintf(ctx
->debug_stream
, "PDKIM [%s] Body hash did NOT verify\n",
988 fprintf(ctx
->debug_stream
, "PDKIM [%s] bh signature: ", sig
->domain
);
989 pdkim_hexprint(ctx
->debug_stream
, sig
->bodyhash
,
990 (sig
->algo
== PDKIM_ALGO_RSA_SHA1
)?20:32,1);
993 sig
->verify_status
= PDKIM_VERIFY_FAIL
;
994 sig
->verify_ext_status
= PDKIM_VERIFY_FAIL_BODY
;
1006 /* -------------------------------------------------------------------------- */
1007 /* Callback from pdkim_feed below for processing complete body lines */
1008 int pdkim_bodyline_complete(pdkim_ctx
*ctx
) {
1009 char *p
= ctx
->linebuf
;
1010 int n
= ctx
->linebuf_offset
;
1012 /* Ignore extra data if we've seen the end-of-data marker */
1013 if (ctx
->seen_eod
) goto BAIL
;
1015 /* We've always got one extra byte to stuff a zero ... */
1016 ctx
->linebuf
[(ctx
->linebuf_offset
)] = '\0';
1018 if (ctx
->input_mode
== PDKIM_INPUT_SMTP
) {
1019 /* Terminate on EOD marker */
1020 if (memcmp(p
,".\r\n",3) == 0) {
1025 if (memcmp(p
,"..",2) == 0) {
1031 /* Empty lines need to be buffered until we find a non-empty line */
1032 if (memcmp(p
,"\r\n",2) == 0) {
1033 ctx
->num_buffered_crlf
++;
1037 /* At this point, we have a non-empty line, so release the buffered ones. */
1038 while (ctx
->num_buffered_crlf
) {
1039 pdkim_update_bodyhash(ctx
,"\r\n",2);
1040 ctx
->num_buffered_crlf
--;
1043 pdkim_update_bodyhash(ctx
,p
,n
);
1046 ctx
->linebuf_offset
= 0;
1051 /* -------------------------------------------------------------------------- */
1052 /* Callback from pdkim_feed below for processing complete headers */
1053 #define DKIM_SIGNATURE_HEADERNAME "DKIM-Signature:"
1054 int pdkim_header_complete(pdkim_ctx
*ctx
) {
1055 pdkim_signature
*sig
= ctx
->sig
;
1057 /* Special case: The last header can have an extra \r appended */
1058 if ( (ctx
->cur_header
->len
> 1) &&
1059 (ctx
->cur_header
->str
[(ctx
->cur_header
->len
)-1] == '\r') ) {
1060 ctx
->cur_header
->str
[(ctx
->cur_header
->len
)-1] = '\0';
1061 ctx
->cur_header
->len
--;
1065 if (ctx
->num_headers
> PDKIM_MAX_HEADERS
) goto BAIL
;
1067 /* Traverse all signatures */
1068 while (sig
!= NULL
) {
1069 pdkim_stringlist
*list
;
1071 /* SIGNING -------------------------------------------------------------- */
1072 if (ctx
->mode
== PDKIM_MODE_SIGN
) {
1073 if (header_name_match(ctx
->cur_header
->str
,
1076 PDKIM_DEFAULT_SIGN_HEADERS
, 0) != PDKIM_OK
) goto NEXT_SIG
;
1078 /* VERIFICATION --------------------------------------------------------- */
1080 /* Header is not included or all instances were already 'ticked off' */
1081 if (header_name_match(ctx
->cur_header
->str
,
1082 sig
->hnames_check
, 1) != PDKIM_OK
) goto NEXT_SIG
;
1085 /* Add header to the signed headers list (in reverse order) */
1086 list
= pdkim_prepend_stringlist(sig
->headers
,
1087 ctx
->cur_header
->str
);
1088 if (list
== NULL
) return PDKIM_ERR_OOM
;
1089 sig
->headers
= list
;
1095 /* DKIM-Signature: headers are added to the verification list */
1096 if ( (ctx
->mode
== PDKIM_MODE_VERIFY
) &&
1097 (strncasecmp(ctx
->cur_header
->str
,
1098 DKIM_SIGNATURE_HEADERNAME
,
1099 strlen(DKIM_SIGNATURE_HEADERNAME
)) == 0) ) {
1100 pdkim_signature
*new_sig
;
1101 /* Create and chain new signature block */
1103 if (ctx
->debug_stream
)
1104 fprintf(ctx
->debug_stream
,
1105 "PDKIM >> Found sig, trying to parse >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
1107 new_sig
= pdkim_parse_sig_header(ctx
, ctx
->cur_header
->str
);
1108 if (new_sig
!= NULL
) {
1109 pdkim_signature
*last_sig
= ctx
->sig
;
1110 if (last_sig
== NULL
) {
1114 while (last_sig
->next
!= NULL
) { last_sig
= last_sig
->next
; }
1115 last_sig
->next
= new_sig
;
1120 if (ctx
->debug_stream
) {
1121 fprintf(ctx
->debug_stream
,"Error while parsing signature header\n");
1122 fprintf(ctx
->debug_stream
,
1123 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1130 pdkim_strclear(ctx
->cur_header
); /* Re-use existing pdkim_str */
1136 /* -------------------------------------------------------------------------- */
1137 #define HEADER_BUFFER_FRAG_SIZE 256
1138 DLLEXPORT
int pdkim_feed (pdkim_ctx
*ctx
,
1142 for (p
=0;p
<len
;p
++) {
1144 if (ctx
->past_headers
) {
1145 /* Processing body byte */
1146 ctx
->linebuf
[(ctx
->linebuf_offset
)++] = c
;
1148 int rc
= pdkim_bodyline_complete(ctx
); /* End of line */
1149 if (rc
!= PDKIM_OK
) return rc
;
1151 if (ctx
->linebuf_offset
== (PDKIM_MAX_BODY_LINE_LEN
-1))
1152 return PDKIM_ERR_LONG_LINE
;
1155 /* Processing header byte */
1159 int rc
= pdkim_header_complete(ctx
); /* Seen last header line */
1160 if (rc
!= PDKIM_OK
) return rc
;
1161 ctx
->past_headers
= 1;
1164 if (ctx
->debug_stream
)
1165 fprintf(ctx
->debug_stream
,
1166 "PDKIM >> Hashed body data, canonicalized >>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
1170 else ctx
->seen_lf
= 1;
1172 else if (ctx
->seen_lf
) {
1173 if (! ((c
== '\t') || (c
== ' '))) {
1174 int rc
= pdkim_header_complete(ctx
); /* End of header */
1175 if (rc
!= PDKIM_OK
) return rc
;
1180 if (ctx
->cur_header
== NULL
) {
1181 ctx
->cur_header
= pdkim_strnew(NULL
);
1182 if (ctx
->cur_header
== NULL
) return PDKIM_ERR_OOM
;
1184 if (ctx
->cur_header
->len
< PDKIM_MAX_HEADER_LEN
)
1185 if (pdkim_strncat(ctx
->cur_header
,&data
[p
],1) == NULL
)
1186 return PDKIM_ERR_OOM
;
1193 /* -------------------------------------------------------------------------- */
1194 char *pdkim_create_header(pdkim_signature
*sig
, int final
) {
1196 char *base64_bh
= NULL
;
1197 char *base64_b
= NULL
;
1198 pdkim_str
*hdr
= pdkim_strnew("DKIM-Signature: v="PDKIM_SIGNATURE_VERSION
);
1199 if (hdr
== NULL
) return NULL
;
1201 base64_bh
= pdkim_encode_base64(sig
->bodyhash
, sig
->bodyhash_len
);
1202 if (base64_bh
== NULL
) goto BAIL
;
1204 /* Required and static bits */
1206 pdkim_strcat(hdr
,"; a=") &&
1207 pdkim_strcat(hdr
,pdkim_algos
[sig
->algo
]) &&
1208 pdkim_strcat(hdr
,"; q=") &&
1209 pdkim_strcat(hdr
,pdkim_querymethods
[sig
->querymethod
]) &&
1210 pdkim_strcat(hdr
,"; c=") &&
1211 pdkim_strcat(hdr
,pdkim_canons
[sig
->canon_headers
]) &&
1212 pdkim_strcat(hdr
,"/") &&
1213 pdkim_strcat(hdr
,pdkim_canons
[sig
->canon_body
]) &&
1214 pdkim_strcat(hdr
,"; d=") &&
1215 pdkim_strcat(hdr
,sig
->domain
) &&
1216 pdkim_strcat(hdr
,"; s=") &&
1217 pdkim_strcat(hdr
,sig
->selector
) &&
1218 pdkim_strcat(hdr
,";\r\n\th=") &&
1219 pdkim_strcat(hdr
,sig
->headernames
) &&
1220 pdkim_strcat(hdr
,"; bh=") &&
1221 pdkim_strcat(hdr
,base64_bh
) &&
1222 pdkim_strcat(hdr
,";\r\n\t")
1225 if (sig
->identity
!= NULL
) {
1226 if (!( pdkim_strcat(hdr
,"i=") &&
1227 pdkim_strcat(hdr
,sig
->identity
) &&
1228 pdkim_strcat(hdr
,";") ) ) {
1232 if (sig
->created
> 0) {
1233 if (!( pdkim_strcat(hdr
,"t=") &&
1234 pdkim_numcat(hdr
,sig
->created
) &&
1235 pdkim_strcat(hdr
,";") ) ) {
1239 if (sig
->expires
> 0) {
1240 if (!( pdkim_strcat(hdr
,"x=") &&
1241 pdkim_numcat(hdr
,sig
->expires
) &&
1242 pdkim_strcat(hdr
,";") ) ) {
1246 if (sig
->bodylength
>= 0) {
1247 if (!( pdkim_strcat(hdr
,"l=") &&
1248 pdkim_numcat(hdr
,sig
->bodylength
) &&
1249 pdkim_strcat(hdr
,";") ) ) {
1253 /* Extra linebreak */
1254 if (hdr
->str
[(hdr
->len
)-1] == ';') {
1255 if (!pdkim_strcat(hdr
," \r\n\t")) goto BAIL
;
1257 /* Preliminary or final version? */
1259 base64_b
= pdkim_encode_base64(sig
->sigdata
, sig
->sigdata_len
);
1260 if (base64_b
== NULL
) goto BAIL
;
1262 pdkim_strcat(hdr
,"b=") &&
1263 pdkim_strcat(hdr
,base64_b
) &&
1264 pdkim_strcat(hdr
,";")
1268 if (pdkim_strcat(hdr
,"b=;")) goto DONE
;
1275 rc
= strdup(hdr
->str
);
1279 if (base64_bh
!= NULL
) free(base64_bh
);
1280 if (base64_b
!= NULL
) free(base64_b
);
1285 /* -------------------------------------------------------------------------- */
1286 DLLEXPORT
int pdkim_feed_finish(pdkim_ctx
*ctx
, pdkim_signature
**return_signatures
) {
1287 pdkim_signature
*sig
= ctx
->sig
;
1288 pdkim_str
*headernames
= NULL
; /* Collected signed header names */
1290 /* Check if we must still flush a (partial) header. If that is the
1291 case, the message has no body, and we must compute a body hash
1292 out of '<CR><LF>' */
1293 if (ctx
->cur_header
&& ctx
->cur_header
->len
) {
1294 int rc
= pdkim_header_complete(ctx
);
1295 if (rc
!= PDKIM_OK
) return rc
;
1296 pdkim_update_bodyhash(ctx
,"\r\n",2);
1299 /* For non-smtp input, check if there's an unfinished line in the
1300 body line buffer. If that is the case, we must add a CRLF to the
1301 hash to properly terminate the message. */
1302 if ((ctx
->input_mode
== PDKIM_INPUT_NORMAL
) && ctx
->linebuf_offset
) {
1303 pdkim_update_bodyhash(ctx
, ctx
->linebuf
, ctx
->linebuf_offset
);
1304 pdkim_update_bodyhash(ctx
,"\r\n",2);
1307 if (ctx
->debug_stream
)
1308 fprintf(ctx
->debug_stream
,
1309 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1313 /* Build (and/or evaluate) body hash */
1314 if (pdkim_finish_bodyhash(ctx
) != PDKIM_OK
) return PDKIM_ERR_OOM
;
1316 /* SIGNING -------------------------------------------------------------- */
1317 if (ctx
->mode
== PDKIM_MODE_SIGN
) {
1318 headernames
= pdkim_strnew(NULL
);
1319 if (headernames
== NULL
) return PDKIM_ERR_OOM
;
1321 /* ---------------------------------------------------------------------- */
1323 while (sig
!= NULL
) {
1324 sha1_context sha1_headers
;
1325 sha2_context sha2_headers
;
1327 char headerhash
[32];
1329 if (sig
->algo
== PDKIM_ALGO_RSA_SHA1
)
1330 sha1_starts(&sha1_headers
);
1332 sha2_starts(&sha2_headers
,0);
1335 if (ctx
->debug_stream
)
1336 fprintf(ctx
->debug_stream
,
1337 "PDKIM >> Hashed header data, canonicalized, in sequence >>>>>>>>>>>>>>\n");
1340 /* SIGNING ---------------------------------------------------------------- */
1341 /* When signing, walk through our header list and add them to the hash. As we
1342 go, construct a list of the header's names to use for the h= parameter. */
1343 if (ctx
->mode
== PDKIM_MODE_SIGN
) {
1344 pdkim_stringlist
*p
= sig
->headers
;
1347 /* Collect header names (Note: colon presence is guaranteed here) */
1348 char *q
= strchr(p
->value
,':');
1349 if (pdkim_strncat(headernames
, p
->value
,
1350 (q
-(p
->value
))+((p
->next
==NULL
)?0:1)) == NULL
)
1351 return PDKIM_ERR_OOM
;
1353 if (sig
->canon_headers
== PDKIM_CANON_RELAXED
)
1354 rh
= pdkim_relax_header(p
->value
,1); /* cook header for relaxed canon */
1356 rh
= strdup(p
->value
); /* just copy it for simple canon */
1358 if (rh
== NULL
) return PDKIM_ERR_OOM
;
1360 /* Feed header to the hash algorithm */
1361 if (sig
->algo
== PDKIM_ALGO_RSA_SHA1
)
1362 sha1_update(&(sha1_headers
),(unsigned char *)rh
,strlen(rh
));
1364 sha2_update(&(sha2_headers
),(unsigned char *)rh
,strlen(rh
));
1366 if (ctx
->debug_stream
)
1367 pdkim_quoteprint(ctx
->debug_stream
, rh
, strlen(rh
), 1);
1373 /* VERIFICATION ----------------------------------------------------------- */
1374 /* When verifying, walk through the header name list in the h= parameter and
1375 add the headers to the hash in that order. */
1377 char *b
= strdup(sig
->headernames
);
1380 if (b
== NULL
) return PDKIM_ERR_OOM
;
1383 pdkim_stringlist
*hdrs
= sig
->headers
;
1385 if (q
!= NULL
) *q
= '\0';
1386 while (hdrs
!= NULL
) {
1387 if ( (strncasecmp(hdrs
->value
,p
,strlen(p
)) == 0) &&
1388 ((hdrs
->value
)[strlen(p
)] == ':') ) {
1390 if (sig
->canon_headers
== PDKIM_CANON_RELAXED
)
1391 rh
= pdkim_relax_header(hdrs
->value
,1); /* cook header for relaxed canon */
1393 rh
= strdup(hdrs
->value
); /* just copy it for simple canon */
1394 if (rh
== NULL
) return PDKIM_ERR_OOM
;
1395 /* Feed header to the hash algorithm */
1396 if (sig
->algo
== PDKIM_ALGO_RSA_SHA1
)
1397 sha1_update(&(sha1_headers
),(unsigned char *)rh
,strlen(rh
));
1399 sha2_update(&(sha2_headers
),(unsigned char *)rh
,strlen(rh
));
1401 if (ctx
->debug_stream
)
1402 pdkim_quoteprint(ctx
->debug_stream
, rh
, strlen(rh
), 1);
1405 (hdrs
->value
)[0] = '_';
1410 if (q
== NULL
) break;
1417 if (ctx
->debug_stream
)
1418 fprintf(ctx
->debug_stream
,
1419 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1422 /* SIGNING ---------------------------------------------------------------- */
1423 if (ctx
->mode
== PDKIM_MODE_SIGN
) {
1424 /* Copy headernames to signature struct */
1425 sig
->headernames
= strdup(headernames
->str
);
1426 pdkim_strfree(headernames
);
1428 /* Create signature header with b= omitted */
1429 sig_hdr
= pdkim_create_header(ctx
->sig
,0);
1431 /* VERIFICATION ----------------------------------------------------------- */
1433 sig_hdr
= strdup(sig
->rawsig_no_b_val
);
1435 /* ------------------------------------------------------------------------ */
1437 if (sig_hdr
== NULL
) return PDKIM_ERR_OOM
;
1439 /* Relax header if necessary */
1440 if (sig
->canon_headers
== PDKIM_CANON_RELAXED
) {
1441 char *relaxed_hdr
= pdkim_relax_header(sig_hdr
,0);
1443 if (relaxed_hdr
== NULL
) return PDKIM_ERR_OOM
;
1444 sig_hdr
= relaxed_hdr
;
1448 if (ctx
->debug_stream
) {
1449 fprintf(ctx
->debug_stream
,
1450 "PDKIM >> Signed DKIM-Signature header, canonicalized >>>>>>>>>>>>>>>>>\n");
1451 pdkim_quoteprint(ctx
->debug_stream
, sig_hdr
, strlen(sig_hdr
), 1);
1452 fprintf(ctx
->debug_stream
,
1453 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1457 /* Finalize header hash */
1458 if (sig
->algo
== PDKIM_ALGO_RSA_SHA1
) {
1459 sha1_update(&(sha1_headers
),(unsigned char *)sig_hdr
,strlen(sig_hdr
));
1460 sha1_finish(&(sha1_headers
),(unsigned char *)headerhash
);
1462 if (ctx
->debug_stream
) {
1463 fprintf(ctx
->debug_stream
, "PDKIM [%s] hh computed: ", sig
->domain
);
1464 pdkim_hexprint(ctx
->debug_stream
, headerhash
, 20, 1);
1469 sha2_update(&(sha2_headers
),(unsigned char *)sig_hdr
,strlen(sig_hdr
));
1470 sha2_finish(&(sha2_headers
),(unsigned char *)headerhash
);
1472 if (ctx
->debug_stream
) {
1473 fprintf(ctx
->debug_stream
, "PDKIM [%s] hh computed: ", sig
->domain
);
1474 pdkim_hexprint(ctx
->debug_stream
, headerhash
, 32, 1);
1481 /* SIGNING ---------------------------------------------------------------- */
1482 if (ctx
->mode
== PDKIM_MODE_SIGN
) {
1485 rsa_init(&rsa
,RSA_PKCS_V15
,0);
1487 /* Perform private key operation */
1488 if (rsa_parse_key(&rsa
, (unsigned char *)sig
->rsa_privkey
,
1489 strlen(sig
->rsa_privkey
), NULL
, 0) != 0) {
1490 return PDKIM_ERR_RSA_PRIVKEY
;
1493 sig
->sigdata_len
= mpi_size(&(rsa
.N
));
1494 sig
->sigdata
= malloc(sig
->sigdata_len
);
1495 if (sig
->sigdata
== NULL
) return PDKIM_ERR_OOM
;
1497 if (rsa_pkcs1_sign( &rsa
, RSA_PRIVATE
,
1498 ((sig
->algo
== PDKIM_ALGO_RSA_SHA1
)?
1499 SIG_RSA_SHA1
:SIG_RSA_SHA256
),
1501 (unsigned char *)headerhash
,
1502 (unsigned char *)sig
->sigdata
) != 0) {
1503 return PDKIM_ERR_RSA_SIGNING
;
1509 if (ctx
->debug_stream
) {
1510 fprintf(ctx
->debug_stream
, "PDKIM [%s] b computed: ",
1512 pdkim_hexprint(ctx
->debug_stream
, sig
->sigdata
, sig
->sigdata_len
, 1);
1516 sig
->signature_header
= pdkim_create_header(ctx
->sig
,1);
1517 if (sig
->signature_header
== NULL
) return PDKIM_ERR_OOM
;
1519 /* VERIFICATION ----------------------------------------------------------- */
1522 char *dns_txt_name
, *dns_txt_reply
;
1524 rsa_init(&rsa
,RSA_PKCS_V15
,0);
1526 dns_txt_name
= malloc(PDKIM_DNS_TXT_MAX_NAMELEN
);
1527 if (dns_txt_name
== NULL
) return PDKIM_ERR_OOM
;
1528 dns_txt_reply
= malloc(PDKIM_DNS_TXT_MAX_RECLEN
);
1529 if (dns_txt_reply
== NULL
) {
1531 return PDKIM_ERR_OOM
;
1533 memset(dns_txt_reply
,0,PDKIM_DNS_TXT_MAX_RECLEN
);
1534 memset(dns_txt_name
,0,PDKIM_DNS_TXT_MAX_NAMELEN
);
1536 if (snprintf(dns_txt_name
,PDKIM_DNS_TXT_MAX_NAMELEN
,
1537 "%s._domainkey.%s.",
1538 sig
->selector
,sig
->domain
) >= PDKIM_DNS_TXT_MAX_NAMELEN
) {
1539 sig
->verify_status
= PDKIM_VERIFY_INVALID
;
1540 sig
->verify_ext_status
= PDKIM_VERIFY_INVALID_BUFFER_SIZE
;
1544 if ((ctx
->dns_txt_callback(dns_txt_name
, dns_txt_reply
) != PDKIM_OK
) ||
1545 (dns_txt_reply
[0] == '\0')) {
1546 sig
->verify_status
= PDKIM_VERIFY_INVALID
;
1547 sig
->verify_ext_status
= PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE
;
1552 if (ctx
->debug_stream
) {
1553 fprintf(ctx
->debug_stream
,
1554 "PDKIM >> Parsing public key record >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
1555 fprintf(ctx
->debug_stream
,"Raw record: ");
1556 pdkim_quoteprint(ctx
->debug_stream
, dns_txt_reply
, strlen(dns_txt_reply
), 1);
1560 sig
->pubkey
= pdkim_parse_pubkey_record(ctx
,dns_txt_reply
);
1561 if (sig
->pubkey
== NULL
) {
1562 sig
->verify_status
= PDKIM_VERIFY_INVALID
;
1563 sig
->verify_ext_status
= PDKIM_VERIFY_INVALID_PUBKEY_PARSING
;
1565 if (ctx
->debug_stream
) {
1566 fprintf(ctx
->debug_stream
,"Error while parsing public key record\n");
1567 fprintf(ctx
->debug_stream
,
1568 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1575 if (ctx
->debug_stream
) {
1576 fprintf(ctx
->debug_stream
,
1577 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1581 if (rsa_parse_public_key(&rsa
,
1582 (unsigned char *)sig
->pubkey
->key
,
1583 sig
->pubkey
->key_len
) != 0) {
1584 sig
->verify_status
= PDKIM_VERIFY_INVALID
;
1585 sig
->verify_ext_status
= PDKIM_VERIFY_INVALID_PUBKEY_PARSING
;
1589 /* Check the signature */
1590 if (rsa_pkcs1_verify(&rsa
,
1592 ((sig
->algo
== PDKIM_ALGO_RSA_SHA1
)?
1593 SIG_RSA_SHA1
:SIG_RSA_SHA256
),
1595 (unsigned char *)headerhash
,
1596 (unsigned char *)sig
->sigdata
) != 0) {
1597 sig
->verify_status
= PDKIM_VERIFY_FAIL
;
1598 sig
->verify_ext_status
= PDKIM_VERIFY_FAIL_MESSAGE
;
1602 /* We have a winner! (if bodydhash was correct earlier) */
1603 if (sig
->verify_status
== PDKIM_VERIFY_NONE
) {
1604 sig
->verify_status
= PDKIM_VERIFY_PASS
;
1610 if (ctx
->debug_stream
) {
1611 fprintf(ctx
->debug_stream
, "PDKIM [%s] signature status: %s",
1612 sig
->domain
, pdkim_verify_status_str(sig
->verify_status
));
1613 if (sig
->verify_ext_status
> 0) {
1614 fprintf(ctx
->debug_stream
, " (%s)\n",
1615 pdkim_verify_ext_status_str(sig
->verify_ext_status
));
1618 fprintf(ctx
->debug_stream
, "\n");
1625 free(dns_txt_reply
);
1631 /* If requested, set return pointer to signature(s) */
1632 if (return_signatures
!= NULL
) {
1633 *return_signatures
= ctx
->sig
;
1640 /* -------------------------------------------------------------------------- */
1641 DLLEXPORT pdkim_ctx
*pdkim_init_verify(int input_mode
,
1642 int(*dns_txt_callback
)(char *, char *)
1644 pdkim_ctx
*ctx
= malloc(sizeof(pdkim_ctx
));
1645 if (ctx
== NULL
) return NULL
;
1646 memset(ctx
,0,sizeof(pdkim_ctx
));
1648 ctx
->linebuf
= malloc(PDKIM_MAX_BODY_LINE_LEN
);
1649 if (ctx
->linebuf
== NULL
) {
1654 ctx
->mode
= PDKIM_MODE_VERIFY
;
1655 ctx
->input_mode
= input_mode
;
1656 ctx
->dns_txt_callback
= dns_txt_callback
;
1662 /* -------------------------------------------------------------------------- */
1663 DLLEXPORT pdkim_ctx
*pdkim_init_sign(int input_mode
,
1666 char *rsa_privkey
) {
1668 pdkim_signature
*sig
;
1670 if (!domain
|| !selector
|| !rsa_privkey
) return NULL
;
1672 ctx
= malloc(sizeof(pdkim_ctx
));
1673 if (ctx
== NULL
) return NULL
;
1674 memset(ctx
,0,sizeof(pdkim_ctx
));
1676 ctx
->linebuf
= malloc(PDKIM_MAX_BODY_LINE_LEN
);
1677 if (ctx
->linebuf
== NULL
) {
1682 sig
= malloc(sizeof(pdkim_signature
));
1688 memset(sig
,0,sizeof(pdkim_signature
));
1689 sig
->bodylength
= -1;
1691 ctx
->mode
= PDKIM_MODE_SIGN
;
1692 ctx
->input_mode
= input_mode
;
1695 ctx
->sig
->domain
= strdup(domain
);
1696 ctx
->sig
->selector
= strdup(selector
);
1697 ctx
->sig
->rsa_privkey
= strdup(rsa_privkey
);
1699 if (!ctx
->sig
->domain
|| !ctx
->sig
->selector
|| !ctx
->sig
->rsa_privkey
) {
1700 pdkim_free_ctx(ctx
);
1704 ctx
->sig
->sha1_body
= malloc(sizeof(sha1_context
));
1705 if (ctx
->sig
->sha1_body
== NULL
) {
1706 pdkim_free_ctx(ctx
);
1709 sha1_starts(ctx
->sig
->sha1_body
);
1711 ctx
->sig
->sha2_body
= malloc(sizeof(sha2_context
));
1712 if (ctx
->sig
->sha2_body
== NULL
) {
1713 pdkim_free_ctx(ctx
);
1716 sha2_starts(ctx
->sig
->sha2_body
,0);
1722 /* -------------------------------------------------------------------------- */
1723 DLLEXPORT
void pdkim_set_debug_stream(pdkim_ctx
*ctx
,
1724 FILE *debug_stream
) {
1725 ctx
->debug_stream
= debug_stream
;
1729 /* -------------------------------------------------------------------------- */
1730 DLLEXPORT
int pdkim_set_optional(pdkim_ctx
*ctx
,
1737 unsigned long created
,
1738 unsigned long expires
) {
1740 if (identity
!= NULL
) {
1741 ctx
->sig
->identity
= strdup(identity
);
1742 if (ctx
->sig
->identity
== NULL
) return PDKIM_ERR_OOM
;
1745 if (sign_headers
!= NULL
) {
1746 ctx
->sig
->sign_headers
= strdup(sign_headers
);
1747 if (ctx
->sig
->sign_headers
== NULL
) return PDKIM_ERR_OOM
;
1750 ctx
->sig
->canon_headers
= canon_headers
;
1751 ctx
->sig
->canon_body
= canon_body
;
1752 ctx
->sig
->bodylength
= bodylength
;
1753 ctx
->sig
->algo
= algo
;
1754 ctx
->sig
->created
= created
;
1755 ctx
->sig
->expires
= expires
;