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.
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
));
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
);
307 if (sig
->pubkey
!= NULL
) pdkim_free_pubkey(sig
->pubkey
);
310 if (next
!= NULL
) pdkim_free_sig(next
);
315 /* -------------------------------------------------------------------------- */
316 DLLEXPORT
void pdkim_free_ctx(pdkim_ctx
*ctx
) {
318 pdkim_stringlist
*e
= ctx
->headers
;
320 pdkim_stringlist
*c
= e
;
321 if (e
->value
!= NULL
) free(e
->value
);
325 pdkim_free_sig(ctx
->sig
);
326 pdkim_strfree(ctx
->cur_header
);
332 /* -------------------------------------------------------------------------- */
333 /* Matches the name of the passed raw "header" against
334 the passed colon-separated "list", starting at entry
335 "start". Returns the position of the header name in
337 int header_name_match(const char *header
,
346 /* Get header name */
347 char *hcolon
= strchr(header
,':');
348 if (hcolon
== NULL
) return rc
; /* This isn't a header */
349 hname
= malloc((hcolon
-header
)+1);
350 if (hname
== NULL
) return PDKIM_ERR_OOM
;
351 memset(hname
,0,(hcolon
-header
)+1);
352 strncpy(hname
,header
,(hcolon
-header
));
354 /* Copy tick-off list locally, so we can punch zeroes into it */
355 lcopy
= strdup(tick
);
358 return PDKIM_ERR_OOM
;
365 if (strcasecmp(p
,hname
) == 0) {
367 /* Invalidate header name instance in tick-off list */
368 if (do_tick
) tick
[p
-lcopy
] = '_';
376 if (strcasecmp(p
,hname
) == 0) {
378 /* Invalidate header name instance in tick-off list */
379 if (do_tick
) tick
[p
-lcopy
] = '_';
389 /* -------------------------------------------------------------------------- */
390 /* Performs "relaxed" canonicalization of a header. The returned pointer needs
392 char *pdkim_relax_header (char *header
, int crlf
) {
393 int past_field_name
= 0;
397 char *relaxed
= malloc(strlen(header
)+3);
398 if (relaxed
== NULL
) return NULL
;
403 if ( (c
== '\r') || (c
== '\n') ) {
407 if ( (c
== '\t') || (c
== ' ') ) {
408 c
= ' '; /* Turns WSP into SP */
416 if ( (!past_field_name
) && (c
== ':') ) {
417 if (seen_wsp
) q
--; /* This removes WSP before the colon */
418 seen_wsp
= 1; /* This removes WSP after the colon */
423 /* Lowercase header name */
424 if (!past_field_name
) c
= tolower(c
);
429 if ((q
>relaxed
) && (*(q
-1) == ' ')) q
--; /* Squash eventual trailing SP */
431 if (crlf
) strcat(relaxed
,"\r\n");
436 /* -------------------------------------------------------------------------- */
437 #define PDKIM_QP_ERROR_DECODE -1
438 char *pdkim_decode_qp_char(char *qp_p
, int *c
) {
439 char *initial_pos
= qp_p
;
441 /* Advance one char */
444 /* Check for two hex digits and decode them */
445 if (isxdigit(*qp_p
) && isxdigit(qp_p
[1])) {
446 /* Do hex conversion */
447 if (isdigit(*qp_p
)) {*c
= *qp_p
- '0';}
448 else {*c
= toupper(*qp_p
) - 'A' + 10;}
450 if (isdigit(qp_p
[1])) {*c
|= qp_p
[1] - '0';}
451 else {*c
|= toupper(qp_p
[1]) - 'A' + 10;}
455 /* Illegal char here */
456 *c
= PDKIM_QP_ERROR_DECODE
;
461 /* -------------------------------------------------------------------------- */
462 char *pdkim_decode_qp(char *str
) {
466 char *n
= malloc(strlen(p
)+1);
467 if (n
== NULL
) return NULL
;
472 p
= pdkim_decode_qp_char(p
,&nchar
);
490 /* -------------------------------------------------------------------------- */
491 char *pdkim_decode_base64(char *str
, int *num_decoded
) {
495 base64_decode(NULL
, &dlen
, (unsigned char *)str
, strlen(str
));
496 res
= malloc(dlen
+1);
497 if (res
== NULL
) return NULL
;
498 if (base64_decode((unsigned char *)res
,&dlen
,(unsigned char *)str
,strlen(str
)) != 0) {
502 if (num_decoded
!= NULL
) *num_decoded
= dlen
;
506 /* -------------------------------------------------------------------------- */
507 char *pdkim_encode_base64(char *str
, int num
) {
511 base64_encode(NULL
, &dlen
, (unsigned char *)str
, num
);
512 res
= malloc(dlen
+1);
513 if (res
== NULL
) return NULL
;
514 if (base64_encode((unsigned char *)res
,&dlen
,(unsigned char *)str
,num
) != 0) {
522 /* -------------------------------------------------------------------------- */
523 #define PDKIM_HDR_LIMBO 0
524 #define PDKIM_HDR_TAG 1
525 #define PDKIM_HDR_VALUE 2
526 pdkim_signature
*pdkim_parse_sig_header(pdkim_ctx
*ctx
, char *raw_hdr
) {
527 pdkim_signature
*sig
;
529 pdkim_str
*cur_tag
= NULL
;
530 pdkim_str
*cur_val
= NULL
;
533 int where
= PDKIM_HDR_LIMBO
;
536 sig
= malloc(sizeof(pdkim_signature
));
537 if (sig
== NULL
) return NULL
;
538 memset(sig
,0,sizeof(pdkim_signature
));
539 sig
->bodylength
= -1;
541 sig
->rawsig_no_b_val
= malloc(strlen(raw_hdr
)+1);
542 if (sig
->rawsig_no_b_val
== NULL
) {
548 q
= sig
->rawsig_no_b_val
;
553 if ( (*p
== '\r') || (*p
== '\n') )
556 /* Fast-forward through header name */
558 if (*p
== ':') past_hname
= 1;
562 if (where
== PDKIM_HDR_LIMBO
) {
563 /* In limbo, just wait for a tag-char to appear */
564 if (!((*p
>= 'a') && (*p
<= 'z')))
567 where
= PDKIM_HDR_TAG
;
570 if (where
== PDKIM_HDR_TAG
) {
572 cur_tag
= pdkim_strnew(NULL
);
574 if ((*p
>= 'a') && (*p
<= 'z'))
575 pdkim_strncat(cur_tag
,p
,1);
578 if (strcmp(cur_tag
->str
,"b") == 0) {
582 where
= PDKIM_HDR_VALUE
;
587 if (where
== PDKIM_HDR_VALUE
) {
589 cur_val
= pdkim_strnew(NULL
);
591 if ( (*p
== '\r') || (*p
== '\n') || (*p
== ' ') || (*p
== '\t') )
594 if ( (*p
== ';') || (*p
== '\0') ) {
595 if (cur_tag
->len
> 0) {
596 pdkim_strtrim(cur_val
);
598 if (ctx
->debug_stream
)
599 fprintf(ctx
->debug_stream
, " %s=%s\n", cur_tag
->str
, cur_val
->str
);
601 switch (cur_tag
->str
[0]) {
603 switch (cur_tag
->str
[1]) {
605 sig
->bodyhash
= pdkim_decode_base64(cur_val
->str
,&(sig
->bodyhash_len
));
608 sig
->sigdata
= pdkim_decode_base64(cur_val
->str
,&(sig
->sigdata_len
));
613 if (strcmp(cur_val
->str
,PDKIM_SIGNATURE_VERSION
) == 0) {
614 /* We only support version 1, and that is currently the
615 only version there is. */
621 while (pdkim_algos
[i
] != NULL
) {
622 if (strcmp(cur_val
->str
,pdkim_algos
[i
]) == 0 ) {
631 while (pdkim_combined_canons
[i
].str
!= NULL
) {
632 if (strcmp(cur_val
->str
,pdkim_combined_canons
[i
].str
) == 0 ) {
633 sig
->canon_headers
= pdkim_combined_canons
[i
].canon_headers
;
634 sig
->canon_body
= pdkim_combined_canons
[i
].canon_body
;
642 while (pdkim_querymethods
[i
] != NULL
) {
643 if (strcmp(cur_val
->str
,pdkim_querymethods
[i
]) == 0 ) {
644 sig
->querymethod
= i
;
651 sig
->selector
= strdup(cur_val
->str
);
654 sig
->domain
= strdup(cur_val
->str
);
657 sig
->identity
= pdkim_decode_qp(cur_val
->str
);
660 sig
->created
= strtoul(cur_val
->str
,NULL
,10);
663 sig
->expires
= strtoul(cur_val
->str
,NULL
,10);
666 sig
->bodylength
= strtol(cur_val
->str
,NULL
,10);
669 sig
->headernames
= strdup(cur_val
->str
);
672 sig
->copiedheaders
= pdkim_decode_qp(cur_val
->str
);
676 if (ctx
->debug_stream
)
677 fprintf(ctx
->debug_stream
, " Unknown tag encountered\n");
682 pdkim_strclear(cur_tag
);
683 pdkim_strclear(cur_val
);
685 where
= PDKIM_HDR_LIMBO
;
688 else pdkim_strncat(cur_val
,p
,1);
692 if (*p
== '\0') break;
701 /* Make sure the most important bits are there. */
702 if (!(sig
->domain
&& (*(sig
->domain
) != '\0') &&
703 sig
->selector
&& (*(sig
->selector
) != '\0') &&
704 sig
->headernames
&& (*(sig
->headernames
) != '\0') &&
713 /* Chomp raw header. The final newline must not be added to the signature. */
715 while( (q
> sig
->rawsig_no_b_val
) && ((*q
== '\r') || (*q
== '\n')) ) {
720 if (ctx
->debug_stream
) {
721 fprintf(ctx
->debug_stream
,
722 "PDKIM >> Raw signature w/o b= tag value >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
723 pdkim_quoteprint(ctx
->debug_stream
,
724 sig
->rawsig_no_b_val
,
725 strlen(sig
->rawsig_no_b_val
), 1);
726 fprintf(ctx
->debug_stream
,
727 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
731 sig
->sha1_body
= malloc(sizeof(sha1_context
));
732 if (sig
->sha1_body
== NULL
) {
736 sig
->sha2_body
= malloc(sizeof(sha2_context
));
737 if (sig
->sha2_body
== NULL
) {
742 sha1_starts(sig
->sha1_body
);
743 sha2_starts(sig
->sha2_body
,0);
749 /* -------------------------------------------------------------------------- */
750 pdkim_pubkey
*pdkim_parse_pubkey_record(pdkim_ctx
*ctx
, char *raw_record
) {
753 pdkim_str
*cur_tag
= NULL
;
754 pdkim_str
*cur_val
= NULL
;
755 int where
= PDKIM_HDR_LIMBO
;
757 pub
= malloc(sizeof(pdkim_pubkey
));
758 if (pub
== NULL
) return NULL
;
759 memset(pub
,0,sizeof(pdkim_pubkey
));
766 if ( (*p
== '\r') || (*p
== '\n') )
769 if (where
== PDKIM_HDR_LIMBO
) {
770 /* In limbo, just wait for a tag-char to appear */
771 if (!((*p
>= 'a') && (*p
<= 'z')))
774 where
= PDKIM_HDR_TAG
;
777 if (where
== PDKIM_HDR_TAG
) {
779 cur_tag
= pdkim_strnew(NULL
);
781 if ((*p
>= 'a') && (*p
<= 'z'))
782 pdkim_strncat(cur_tag
,p
,1);
785 where
= PDKIM_HDR_VALUE
;
790 if (where
== PDKIM_HDR_VALUE
) {
792 cur_val
= pdkim_strnew(NULL
);
794 if ( (*p
== '\r') || (*p
== '\n') )
797 if ( (*p
== ';') || (*p
== '\0') ) {
798 if (cur_tag
->len
> 0) {
799 pdkim_strtrim(cur_val
);
801 if (ctx
->debug_stream
)
802 fprintf(ctx
->debug_stream
, " %s=%s\n", cur_tag
->str
, cur_val
->str
);
804 switch (cur_tag
->str
[0]) {
806 /* This tag isn't evaluated because:
807 - We only support version DKIM1.
808 - Which is the default for this value (set below)
809 - Other versions are currently not specified. */
812 pub
->hashes
= strdup(cur_val
->str
);
815 pub
->granularity
= strdup(cur_val
->str
);
818 pub
->notes
= pdkim_decode_qp(cur_val
->str
);
821 pub
->key
= pdkim_decode_base64(cur_val
->str
,&(pub
->key_len
));
824 pub
->hashes
= strdup(cur_val
->str
);
827 pub
->srvtype
= strdup(cur_val
->str
);
830 if (strchr(cur_val
->str
,'y') != NULL
) pub
->testing
= 1;
831 if (strchr(cur_val
->str
,'s') != NULL
) pub
->no_subdomaining
= 1;
835 if (ctx
->debug_stream
)
836 fprintf(ctx
->debug_stream
, " Unknown tag encountered\n");
841 pdkim_strclear(cur_tag
);
842 pdkim_strclear(cur_val
);
843 where
= PDKIM_HDR_LIMBO
;
846 else pdkim_strncat(cur_val
,p
,1);
850 if (*p
== '\0') break;
854 /* Set fallback defaults */
855 if (pub
->version
== NULL
) pub
->version
= strdup(PDKIM_PUB_RECORD_VERSION
);
856 if (pub
->granularity
== NULL
) pub
->granularity
= strdup("*");
857 if (pub
->keytype
== NULL
) pub
->keytype
= strdup("rsa");
858 if (pub
->srvtype
== NULL
) pub
->srvtype
= strdup("*");
861 if (pub
->key
== NULL
) {
862 pdkim_free_pubkey(pub
);
870 /* -------------------------------------------------------------------------- */
871 int pdkim_update_bodyhash(pdkim_ctx
*ctx
, const char *data
, int len
) {
872 pdkim_signature
*sig
= ctx
->sig
;
873 /* Cache relaxed version of data */
874 char *relaxed_data
= NULL
;
877 /* Traverse all signatures, updating their hashes. */
878 while (sig
!= NULL
) {
879 /* Defaults to simple canon (no further treatment necessary) */
880 const char *canon_data
= data
;
883 if (sig
->canon_body
== PDKIM_CANON_RELAXED
) {
884 /* Relax the line if not done already */
885 if (relaxed_data
== NULL
) {
887 const char *p
= data
;
889 relaxed_data
= malloc(len
+1);
890 if (relaxed_data
== NULL
) return PDKIM_ERR_OOM
;
894 if ( (q
> 0) && (relaxed_data
[q
-1] == ' ') ) q
--;
896 else if ( (c
== '\t') || (c
== ' ') ) {
897 c
= ' '; /* Turns WSP into SP */
905 relaxed_data
[q
++] = c
;
908 relaxed_data
[q
] = '\0';
911 canon_data
= relaxed_data
;
912 canon_len
= relaxed_len
;
915 /* Make sure we don't exceed the to-be-signed body length */
916 if ((sig
->bodylength
>= 0) &&
917 ((sig
->signed_body_bytes
+(unsigned long)canon_len
) > sig
->bodylength
))
918 canon_len
= (sig
->bodylength
- sig
->signed_body_bytes
);
921 if (sig
->algo
== PDKIM_ALGO_RSA_SHA1
)
922 sha1_update(sig
->sha1_body
,(unsigned char *)canon_data
,canon_len
);
924 sha2_update(sig
->sha2_body
,(unsigned char *)canon_data
,canon_len
);
925 sig
->signed_body_bytes
+= canon_len
;
927 if (ctx
->debug_stream
!=NULL
)
928 pdkim_quoteprint(ctx
->debug_stream
,canon_data
,canon_len
,1);
935 if (relaxed_data
!= NULL
) free(relaxed_data
);
940 /* -------------------------------------------------------------------------- */
941 int pdkim_finish_bodyhash(pdkim_ctx
*ctx
) {
942 pdkim_signature
*sig
= ctx
->sig
;
944 /* Traverse all signatures */
945 while (sig
!= NULL
) {
948 unsigned char bh
[32]; /* SHA-256 = 32 Bytes, SHA-1 = 20 Bytes */
949 if (sig
->algo
== PDKIM_ALGO_RSA_SHA1
)
950 sha1_finish(sig
->sha1_body
,bh
);
952 sha2_finish(sig
->sha2_body
,bh
);
955 if (ctx
->debug_stream
) {
956 fprintf(ctx
->debug_stream
, "PDKIM [%s] Body bytes hashed: %lu\n",
957 sig
->domain
, sig
->signed_body_bytes
);
958 fprintf(ctx
->debug_stream
, "PDKIM [%s] bh computed: ", sig
->domain
);
959 pdkim_hexprint(ctx
->debug_stream
, (char *)bh
,
960 (sig
->algo
== PDKIM_ALGO_RSA_SHA1
)?20:32,1);
964 /* SIGNING -------------------------------------------------------------- */
965 if (ctx
->mode
== PDKIM_MODE_SIGN
) {
966 sig
->bodyhash_len
= (sig
->algo
== PDKIM_ALGO_RSA_SHA1
)?20:32;
967 sig
->bodyhash
= malloc(sig
->bodyhash_len
);
968 if (sig
->bodyhash
== NULL
) return PDKIM_ERR_OOM
;
969 memcpy(sig
->bodyhash
,bh
,sig
->bodyhash_len
);
971 /* If bodylength limit is set, and we have received less bytes
972 than the requested amount, effectively remove the limit tag. */
973 if (sig
->signed_body_bytes
< sig
->bodylength
) sig
->bodylength
= -1;
975 /* VERIFICATION --------------------------------------------------------- */
977 /* Compare bodyhash */
978 if (memcmp(bh
,sig
->bodyhash
,
979 (sig
->algo
== PDKIM_ALGO_RSA_SHA1
)?20:32) == 0) {
981 if (ctx
->debug_stream
)
982 fprintf(ctx
->debug_stream
, "PDKIM [%s] Body hash verified OK\n",
988 if (ctx
->debug_stream
) {
989 fprintf(ctx
->debug_stream
, "PDKIM [%s] Body hash did NOT verify\n",
991 fprintf(ctx
->debug_stream
, "PDKIM [%s] bh signature: ", sig
->domain
);
992 pdkim_hexprint(ctx
->debug_stream
, sig
->bodyhash
,
993 (sig
->algo
== PDKIM_ALGO_RSA_SHA1
)?20:32,1);
996 sig
->verify_status
= PDKIM_VERIFY_FAIL
;
997 sig
->verify_ext_status
= PDKIM_VERIFY_FAIL_BODY
;
1009 /* -------------------------------------------------------------------------- */
1010 /* Callback from pdkim_feed below for processing complete body lines */
1011 int pdkim_bodyline_complete(pdkim_ctx
*ctx
) {
1012 char *p
= ctx
->linebuf
;
1013 int n
= ctx
->linebuf_offset
;
1015 /* Ignore extra data if we've seen the end-of-data marker */
1016 if (ctx
->seen_eod
) goto BAIL
;
1018 /* We've always got one extra byte to stuff a zero ... */
1019 ctx
->linebuf
[(ctx
->linebuf_offset
)] = '\0';
1021 if (ctx
->input_mode
== PDKIM_INPUT_SMTP
) {
1022 /* Terminate on EOD marker */
1023 if (memcmp(p
,".\r\n",3) == 0) {
1028 if (memcmp(p
,"..",2) == 0) {
1034 /* Empty lines need to be buffered until we find a non-empty line */
1035 if (memcmp(p
,"\r\n",2) == 0) {
1036 ctx
->num_buffered_crlf
++;
1041 && ctx
->sig
->canon_body
== PDKIM_CANON_RELAXED
) {
1042 /* Lines with just spaces need to be buffered too */
1044 while(memcmp(check
,"\r\n",2) != 0) {
1047 if (c
!= '\t' && c
!= ' ')
1052 ctx
->num_buffered_crlf
++;
1057 /* At this point, we have a non-empty line, so release the buffered ones. */
1058 while (ctx
->num_buffered_crlf
) {
1059 pdkim_update_bodyhash(ctx
,"\r\n",2);
1060 ctx
->num_buffered_crlf
--;
1063 pdkim_update_bodyhash(ctx
,p
,n
);
1066 ctx
->linebuf_offset
= 0;
1071 /* -------------------------------------------------------------------------- */
1072 /* Callback from pdkim_feed below for processing complete headers */
1073 #define DKIM_SIGNATURE_HEADERNAME "DKIM-Signature:"
1074 int pdkim_header_complete(pdkim_ctx
*ctx
) {
1075 pdkim_signature
*sig
= ctx
->sig
;
1077 /* Special case: The last header can have an extra \r appended */
1078 if ( (ctx
->cur_header
->len
> 1) &&
1079 (ctx
->cur_header
->str
[(ctx
->cur_header
->len
)-1] == '\r') ) {
1080 ctx
->cur_header
->str
[(ctx
->cur_header
->len
)-1] = '\0';
1081 ctx
->cur_header
->len
--;
1085 if (ctx
->num_headers
> PDKIM_MAX_HEADERS
) goto BAIL
;
1087 /* SIGNING -------------------------------------------------------------- */
1088 if (ctx
->mode
== PDKIM_MODE_SIGN
) {
1089 /* Traverse all signatures */
1090 while (sig
!= NULL
) {
1091 pdkim_stringlist
*list
;
1093 if (header_name_match(ctx
->cur_header
->str
,
1096 PDKIM_DEFAULT_SIGN_HEADERS
, 0) != PDKIM_OK
) goto NEXT_SIG
;
1098 /* Add header to the signed headers list (in reverse order) */
1099 list
= pdkim_prepend_stringlist(sig
->headers
,
1100 ctx
->cur_header
->str
);
1101 if (list
== NULL
) return PDKIM_ERR_OOM
;
1102 sig
->headers
= list
;
1109 /* DKIM-Signature: headers are added to the verification list */
1110 if (ctx
->mode
== PDKIM_MODE_VERIFY
) {
1111 if (strncasecmp(ctx
->cur_header
->str
,
1112 DKIM_SIGNATURE_HEADERNAME
,
1113 strlen(DKIM_SIGNATURE_HEADERNAME
)) == 0) {
1114 pdkim_signature
*new_sig
;
1115 /* Create and chain new signature block */
1117 if (ctx
->debug_stream
)
1118 fprintf(ctx
->debug_stream
,
1119 "PDKIM >> Found sig, trying to parse >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
1121 new_sig
= pdkim_parse_sig_header(ctx
, ctx
->cur_header
->str
);
1122 if (new_sig
!= NULL
) {
1123 pdkim_signature
*last_sig
= ctx
->sig
;
1124 if (last_sig
== NULL
) {
1128 while (last_sig
->next
!= NULL
) { last_sig
= last_sig
->next
; }
1129 last_sig
->next
= new_sig
;
1134 if (ctx
->debug_stream
) {
1135 fprintf(ctx
->debug_stream
,"Error while parsing signature header\n");
1136 fprintf(ctx
->debug_stream
,
1137 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1142 /* every other header is stored for signature verification */
1144 pdkim_stringlist
*list
;
1146 list
= pdkim_prepend_stringlist(ctx
->headers
,
1147 ctx
->cur_header
->str
);
1148 if (list
== NULL
) return PDKIM_ERR_OOM
;
1149 ctx
->headers
= list
;
1154 pdkim_strclear(ctx
->cur_header
); /* Re-use existing pdkim_str */
1160 /* -------------------------------------------------------------------------- */
1161 #define HEADER_BUFFER_FRAG_SIZE 256
1162 DLLEXPORT
int pdkim_feed (pdkim_ctx
*ctx
,
1166 for (p
=0;p
<len
;p
++) {
1168 if (ctx
->past_headers
) {
1169 /* Processing body byte */
1170 ctx
->linebuf
[(ctx
->linebuf_offset
)++] = c
;
1172 int rc
= pdkim_bodyline_complete(ctx
); /* End of line */
1173 if (rc
!= PDKIM_OK
) return rc
;
1175 if (ctx
->linebuf_offset
== (PDKIM_MAX_BODY_LINE_LEN
-1))
1176 return PDKIM_ERR_LONG_LINE
;
1179 /* Processing header byte */
1183 int rc
= pdkim_header_complete(ctx
); /* Seen last header line */
1184 if (rc
!= PDKIM_OK
) return rc
;
1185 ctx
->past_headers
= 1;
1188 if (ctx
->debug_stream
)
1189 fprintf(ctx
->debug_stream
,
1190 "PDKIM >> Hashed body data, canonicalized >>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
1194 else ctx
->seen_lf
= 1;
1196 else if (ctx
->seen_lf
) {
1197 if (! ((c
== '\t') || (c
== ' '))) {
1198 int rc
= pdkim_header_complete(ctx
); /* End of header */
1199 if (rc
!= PDKIM_OK
) return rc
;
1204 if (ctx
->cur_header
== NULL
) {
1205 ctx
->cur_header
= pdkim_strnew(NULL
);
1206 if (ctx
->cur_header
== NULL
) return PDKIM_ERR_OOM
;
1208 if (ctx
->cur_header
->len
< PDKIM_MAX_HEADER_LEN
)
1209 if (pdkim_strncat(ctx
->cur_header
,&data
[p
],1) == NULL
)
1210 return PDKIM_ERR_OOM
;
1217 * RFC 5322 specifies that header line length SHOULD be no more than 78
1222 * col: this int holds and receives column number (octets since last '\n')
1223 * str: partial string to append to
1224 * pad: padding, split line or space after before or after eg: ";"
1225 * intro: - must join to payload eg "h=", usually the tag name
1226 * payload: eg base64 data - long data can be split arbitrarily.
1228 * this code doesn't fold the header in some of the places that RFC4871
1229 * allows: As per RFC5322(2.2.3) it only folds before or after tag-value
1230 * pairs and inside long values. it also always spaces or breaks after the
1233 * no guarantees are made for output given out-of range input. like tag
1234 * names loinger than 78, or bogus col. Input is assumed to be free of line breaks.
1237 static char *pdkim_headcat(int *col
, pdkim_str
*str
, const char*pad
,const char *intro
, const char *payload
) {
1244 pdkim_strcat(str
, "\r\n\t");
1247 pdkim_strncat(str
, pad
,l
);
1251 l
=(pad
?1:0) + (intro
?strlen(intro
):0 );
1254 { /*can't fit intro - start a new line to make room.*/
1255 pdkim_strcat(str
, "\r\n\t");
1257 l
= intro
?strlen(intro
):0;
1260 l
+= payload
? strlen(payload
):0 ;
1263 { /* this fragment will not fit on a single line */
1266 pdkim_strcat(str
, " ");
1268 pad
=NULL
; // only want this once
1273 size_t sl
=strlen(intro
);
1274 pdkim_strncat(str
, intro
,sl
);
1277 intro
=NULL
; // only want this once
1281 size_t sl
=strlen(payload
);
1282 size_t chomp
= *col
+sl
< 77 ? sl
: 78-*col
;
1283 pdkim_strncat(str
, payload
,chomp
);
1288 // the while precondition tells us it didn't fit.
1289 pdkim_strcat(str
, "\r\n\t");
1294 pdkim_strcat(str
, "\r\n\t");
1301 pdkim_strcat(str
, " ");
1308 size_t sl
=strlen(intro
);
1309 pdkim_strncat(str
, intro
,sl
);
1316 size_t sl
=strlen(payload
);
1317 pdkim_strncat(str
, payload
,sl
);
1324 /* -------------------------------------------------------------------------- */
1325 char *pdkim_create_header(pdkim_signature
*sig
, int final
) {
1327 char *base64_bh
= NULL
;
1328 char *base64_b
= NULL
;
1330 pdkim_str
*hdr
= pdkim_strnew("DKIM-Signature: v="PDKIM_SIGNATURE_VERSION
);
1331 if (hdr
== NULL
) return NULL
;
1332 pdkim_str
*canon_all
= pdkim_strnew(pdkim_canons
[sig
->canon_headers
]);
1333 if (canon_all
== NULL
) goto BAIL
;
1335 base64_bh
= pdkim_encode_base64(sig
->bodyhash
, sig
->bodyhash_len
);
1336 if (base64_bh
== NULL
) goto BAIL
;
1338 col
=strlen(hdr
->str
);
1340 /* Required and static bits */
1342 pdkim_headcat(&col
,hdr
,";","a=",pdkim_algos
[sig
->algo
]) &&
1343 pdkim_headcat(&col
,hdr
,";","q=",pdkim_querymethods
[sig
->querymethod
]) &&
1344 pdkim_strcat(canon_all
,"/") &&
1345 pdkim_strcat(canon_all
,pdkim_canons
[sig
->canon_body
]) &&
1346 pdkim_headcat(&col
,hdr
,";","c=",canon_all
->str
) &&
1347 pdkim_headcat(&col
,hdr
,";","d=",sig
->domain
) &&
1348 pdkim_headcat(&col
,hdr
,";","s=",sig
->selector
)
1350 /* list of eader names can be split between items. */
1352 char *n
=strdup(sig
->headernames
);
1359 char *c
=strchr(n
,':');
1363 if (!pdkim_headcat(&col
,hdr
,NULL
,NULL
,":"))
1369 if( !pdkim_headcat(&col
,hdr
,s
,i
,n
))
1374 if(c
) n
=c
+1 ; else break;
1380 if(!pdkim_headcat(&col
,hdr
,";","bh=",base64_bh
))
1384 if (sig
->identity
!= NULL
) {
1385 if(!pdkim_headcat(&col
,hdr
,";","i=",sig
->identity
)){
1390 if (sig
->created
> 0) {
1392 snprintf(minibuf
,20,"%lu",sig
->created
);
1393 if(!pdkim_headcat(&col
,hdr
,";","t=",minibuf
)) {
1397 if (sig
->expires
> 0) {
1399 snprintf(minibuf
,20,"%lu",sig
->expires
);
1400 if(!pdkim_headcat(&col
,hdr
,";","x=",minibuf
)) {
1404 if (sig
->bodylength
>= 0) {
1406 snprintf(minibuf
,20,"%lu",sig
->bodylength
);
1407 if(!pdkim_headcat(&col
,hdr
,";","l=",minibuf
)) {
1412 /* Preliminary or final version? */
1414 base64_b
= pdkim_encode_base64(sig
->sigdata
, sig
->sigdata_len
);
1415 if (base64_b
== NULL
) goto BAIL
;
1416 if(!pdkim_headcat(&col
,hdr
,";","b=",base64_b
)) goto BAIL
;
1419 if(!pdkim_headcat(&col
,hdr
,";","b=","")) goto BAIL
;
1422 /* add trailing semicolon: I'm not sure if this is actually needed */
1423 if(!pdkim_headcat(&col
,hdr
,NULL
,";","")) goto BAIL
;
1429 rc
= strdup(hdr
->str
);
1433 if (canon_all
!= NULL
) pdkim_strfree(canon_all
);
1434 if (base64_bh
!= NULL
) free(base64_bh
);
1435 if (base64_b
!= NULL
) free(base64_b
);
1440 /* -------------------------------------------------------------------------- */
1441 DLLEXPORT
int pdkim_feed_finish(pdkim_ctx
*ctx
, pdkim_signature
**return_signatures
) {
1442 pdkim_signature
*sig
= ctx
->sig
;
1443 pdkim_str
*headernames
= NULL
; /* Collected signed header names */
1445 /* Check if we must still flush a (partial) header. If that is the
1446 case, the message has no body, and we must compute a body hash
1447 out of '<CR><LF>' */
1448 if (ctx
->cur_header
&& ctx
->cur_header
->len
) {
1449 int rc
= pdkim_header_complete(ctx
);
1450 if (rc
!= PDKIM_OK
) return rc
;
1451 pdkim_update_bodyhash(ctx
,"\r\n",2);
1454 /* For non-smtp input, check if there's an unfinished line in the
1455 body line buffer. If that is the case, we must add a CRLF to the
1456 hash to properly terminate the message. */
1457 if ((ctx
->input_mode
== PDKIM_INPUT_NORMAL
) && ctx
->linebuf_offset
) {
1458 pdkim_update_bodyhash(ctx
, ctx
->linebuf
, ctx
->linebuf_offset
);
1459 pdkim_update_bodyhash(ctx
,"\r\n",2);
1462 if (ctx
->debug_stream
)
1463 fprintf(ctx
->debug_stream
,
1464 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1468 /* Build (and/or evaluate) body hash */
1469 if (pdkim_finish_bodyhash(ctx
) != PDKIM_OK
) return PDKIM_ERR_OOM
;
1471 /* SIGNING -------------------------------------------------------------- */
1472 if (ctx
->mode
== PDKIM_MODE_SIGN
) {
1473 headernames
= pdkim_strnew(NULL
);
1474 if (headernames
== NULL
) return PDKIM_ERR_OOM
;
1476 /* ---------------------------------------------------------------------- */
1478 while (sig
!= NULL
) {
1479 sha1_context sha1_headers
;
1480 sha2_context sha2_headers
;
1482 char headerhash
[32];
1484 if (sig
->algo
== PDKIM_ALGO_RSA_SHA1
)
1485 sha1_starts(&sha1_headers
);
1487 sha2_starts(&sha2_headers
,0);
1490 if (ctx
->debug_stream
)
1491 fprintf(ctx
->debug_stream
,
1492 "PDKIM >> Hashed header data, canonicalized, in sequence >>>>>>>>>>>>>>\n");
1495 /* SIGNING ---------------------------------------------------------------- */
1496 /* When signing, walk through our header list and add them to the hash. As we
1497 go, construct a list of the header's names to use for the h= parameter. */
1498 if (ctx
->mode
== PDKIM_MODE_SIGN
) {
1499 pdkim_stringlist
*p
= sig
->headers
;
1502 /* Collect header names (Note: colon presence is guaranteed here) */
1503 char *q
= strchr(p
->value
,':');
1504 if (pdkim_strncat(headernames
, p
->value
,
1505 (q
-(p
->value
))+((p
->next
==NULL
)?0:1)) == NULL
)
1506 return PDKIM_ERR_OOM
;
1508 if (sig
->canon_headers
== PDKIM_CANON_RELAXED
)
1509 rh
= pdkim_relax_header(p
->value
,1); /* cook header for relaxed canon */
1511 rh
= strdup(p
->value
); /* just copy it for simple canon */
1513 if (rh
== NULL
) return PDKIM_ERR_OOM
;
1515 /* Feed header to the hash algorithm */
1516 if (sig
->algo
== PDKIM_ALGO_RSA_SHA1
)
1517 sha1_update(&(sha1_headers
),(unsigned char *)rh
,strlen(rh
));
1519 sha2_update(&(sha2_headers
),(unsigned char *)rh
,strlen(rh
));
1521 if (ctx
->debug_stream
)
1522 pdkim_quoteprint(ctx
->debug_stream
, rh
, strlen(rh
), 1);
1528 /* VERIFICATION ----------------------------------------------------------- */
1529 /* When verifying, walk through the header name list in the h= parameter and
1530 add the headers to the hash in that order. */
1532 char *b
= strdup(sig
->headernames
);
1535 pdkim_stringlist
*hdrs
= ctx
->headers
;
1537 if (b
== NULL
) return PDKIM_ERR_OOM
;
1540 while (hdrs
!= NULL
) {
1546 hdrs
= ctx
->headers
;
1548 if (q
!= NULL
) *q
= '\0';
1549 while (hdrs
!= NULL
) {
1550 if ( (hdrs
->tag
== 0) &&
1551 (strncasecmp(hdrs
->value
,p
,strlen(p
)) == 0) &&
1552 ((hdrs
->value
)[strlen(p
)] == ':') ) {
1554 if (sig
->canon_headers
== PDKIM_CANON_RELAXED
)
1555 rh
= pdkim_relax_header(hdrs
->value
,1); /* cook header for relaxed canon */
1557 rh
= strdup(hdrs
->value
); /* just copy it for simple canon */
1558 if (rh
== NULL
) return PDKIM_ERR_OOM
;
1559 /* Feed header to the hash algorithm */
1560 if (sig
->algo
== PDKIM_ALGO_RSA_SHA1
)
1561 sha1_update(&(sha1_headers
),(unsigned char *)rh
,strlen(rh
));
1563 sha2_update(&(sha2_headers
),(unsigned char *)rh
,strlen(rh
));
1565 if (ctx
->debug_stream
)
1566 pdkim_quoteprint(ctx
->debug_stream
, rh
, strlen(rh
), 1);
1574 if (q
== NULL
) break;
1581 if (ctx
->debug_stream
)
1582 fprintf(ctx
->debug_stream
,
1583 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1586 /* SIGNING ---------------------------------------------------------------- */
1587 if (ctx
->mode
== PDKIM_MODE_SIGN
) {
1588 /* Copy headernames to signature struct */
1589 sig
->headernames
= strdup(headernames
->str
);
1590 pdkim_strfree(headernames
);
1592 /* Create signature header with b= omitted */
1593 sig_hdr
= pdkim_create_header(ctx
->sig
,0);
1595 /* VERIFICATION ----------------------------------------------------------- */
1597 sig_hdr
= strdup(sig
->rawsig_no_b_val
);
1599 /* ------------------------------------------------------------------------ */
1601 if (sig_hdr
== NULL
) return PDKIM_ERR_OOM
;
1603 /* Relax header if necessary */
1604 if (sig
->canon_headers
== PDKIM_CANON_RELAXED
) {
1605 char *relaxed_hdr
= pdkim_relax_header(sig_hdr
,0);
1607 if (relaxed_hdr
== NULL
) return PDKIM_ERR_OOM
;
1608 sig_hdr
= relaxed_hdr
;
1612 if (ctx
->debug_stream
) {
1613 fprintf(ctx
->debug_stream
,
1614 "PDKIM >> Signed DKIM-Signature header, canonicalized >>>>>>>>>>>>>>>>>\n");
1615 pdkim_quoteprint(ctx
->debug_stream
, sig_hdr
, strlen(sig_hdr
), 1);
1616 fprintf(ctx
->debug_stream
,
1617 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1621 /* Finalize header hash */
1622 if (sig
->algo
== PDKIM_ALGO_RSA_SHA1
) {
1623 sha1_update(&(sha1_headers
),(unsigned char *)sig_hdr
,strlen(sig_hdr
));
1624 sha1_finish(&(sha1_headers
),(unsigned char *)headerhash
);
1626 if (ctx
->debug_stream
) {
1627 fprintf(ctx
->debug_stream
, "PDKIM [%s] hh computed: ", sig
->domain
);
1628 pdkim_hexprint(ctx
->debug_stream
, headerhash
, 20, 1);
1633 sha2_update(&(sha2_headers
),(unsigned char *)sig_hdr
,strlen(sig_hdr
));
1634 sha2_finish(&(sha2_headers
),(unsigned char *)headerhash
);
1636 if (ctx
->debug_stream
) {
1637 fprintf(ctx
->debug_stream
, "PDKIM [%s] hh computed: ", sig
->domain
);
1638 pdkim_hexprint(ctx
->debug_stream
, headerhash
, 32, 1);
1645 /* SIGNING ---------------------------------------------------------------- */
1646 if (ctx
->mode
== PDKIM_MODE_SIGN
) {
1649 rsa_init(&rsa
,RSA_PKCS_V15
,0);
1651 /* Perform private key operation */
1652 if (rsa_parse_key(&rsa
, (unsigned char *)sig
->rsa_privkey
,
1653 strlen(sig
->rsa_privkey
), NULL
, 0) != 0) {
1654 return PDKIM_ERR_RSA_PRIVKEY
;
1657 sig
->sigdata_len
= mpi_size(&(rsa
.N
));
1658 sig
->sigdata
= malloc(sig
->sigdata_len
);
1659 if (sig
->sigdata
== NULL
) return PDKIM_ERR_OOM
;
1661 if (rsa_pkcs1_sign( &rsa
, RSA_PRIVATE
,
1662 ((sig
->algo
== PDKIM_ALGO_RSA_SHA1
)?
1663 SIG_RSA_SHA1
:SIG_RSA_SHA256
),
1665 (unsigned char *)headerhash
,
1666 (unsigned char *)sig
->sigdata
) != 0) {
1667 return PDKIM_ERR_RSA_SIGNING
;
1673 if (ctx
->debug_stream
) {
1674 fprintf(ctx
->debug_stream
, "PDKIM [%s] b computed: ",
1676 pdkim_hexprint(ctx
->debug_stream
, sig
->sigdata
, sig
->sigdata_len
, 1);
1680 sig
->signature_header
= pdkim_create_header(ctx
->sig
,1);
1681 if (sig
->signature_header
== NULL
) return PDKIM_ERR_OOM
;
1683 /* VERIFICATION ----------------------------------------------------------- */
1686 char *dns_txt_name
, *dns_txt_reply
;
1688 rsa_init(&rsa
,RSA_PKCS_V15
,0);
1690 dns_txt_name
= malloc(PDKIM_DNS_TXT_MAX_NAMELEN
);
1691 if (dns_txt_name
== NULL
) return PDKIM_ERR_OOM
;
1692 dns_txt_reply
= malloc(PDKIM_DNS_TXT_MAX_RECLEN
);
1693 if (dns_txt_reply
== NULL
) {
1695 return PDKIM_ERR_OOM
;
1697 memset(dns_txt_reply
,0,PDKIM_DNS_TXT_MAX_RECLEN
);
1698 memset(dns_txt_name
,0,PDKIM_DNS_TXT_MAX_NAMELEN
);
1700 if (snprintf(dns_txt_name
,PDKIM_DNS_TXT_MAX_NAMELEN
,
1701 "%s._domainkey.%s.",
1702 sig
->selector
,sig
->domain
) >= PDKIM_DNS_TXT_MAX_NAMELEN
) {
1703 sig
->verify_status
= PDKIM_VERIFY_INVALID
;
1704 sig
->verify_ext_status
= PDKIM_VERIFY_INVALID_BUFFER_SIZE
;
1708 if ((ctx
->dns_txt_callback(dns_txt_name
, dns_txt_reply
) != PDKIM_OK
) ||
1709 (dns_txt_reply
[0] == '\0')) {
1710 sig
->verify_status
= PDKIM_VERIFY_INVALID
;
1711 sig
->verify_ext_status
= PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE
;
1716 if (ctx
->debug_stream
) {
1717 fprintf(ctx
->debug_stream
,
1718 "PDKIM >> Parsing public key record >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
1719 fprintf(ctx
->debug_stream
," Raw record: ");
1720 pdkim_quoteprint(ctx
->debug_stream
, dns_txt_reply
, strlen(dns_txt_reply
), 1);
1724 sig
->pubkey
= pdkim_parse_pubkey_record(ctx
,dns_txt_reply
);
1725 if (sig
->pubkey
== NULL
) {
1726 sig
->verify_status
= PDKIM_VERIFY_INVALID
;
1727 sig
->verify_ext_status
= PDKIM_VERIFY_INVALID_PUBKEY_PARSING
;
1729 if (ctx
->debug_stream
) {
1730 fprintf(ctx
->debug_stream
," Error while parsing public key record\n");
1731 fprintf(ctx
->debug_stream
,
1732 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1739 if (ctx
->debug_stream
) {
1740 fprintf(ctx
->debug_stream
,
1741 "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
1745 if (rsa_parse_public_key(&rsa
,
1746 (unsigned char *)sig
->pubkey
->key
,
1747 sig
->pubkey
->key_len
) != 0) {
1748 sig
->verify_status
= PDKIM_VERIFY_INVALID
;
1749 sig
->verify_ext_status
= PDKIM_VERIFY_INVALID_PUBKEY_PARSING
;
1753 /* Check the signature */
1754 if (rsa_pkcs1_verify(&rsa
,
1756 ((sig
->algo
== PDKIM_ALGO_RSA_SHA1
)?
1757 SIG_RSA_SHA1
:SIG_RSA_SHA256
),
1759 (unsigned char *)headerhash
,
1760 (unsigned char *)sig
->sigdata
) != 0) {
1761 sig
->verify_status
= PDKIM_VERIFY_FAIL
;
1762 sig
->verify_ext_status
= PDKIM_VERIFY_FAIL_MESSAGE
;
1766 /* We have a winner! (if bodydhash was correct earlier) */
1767 if (sig
->verify_status
== PDKIM_VERIFY_NONE
) {
1768 sig
->verify_status
= PDKIM_VERIFY_PASS
;
1774 if (ctx
->debug_stream
) {
1775 fprintf(ctx
->debug_stream
, "PDKIM [%s] signature status: %s",
1776 sig
->domain
, pdkim_verify_status_str(sig
->verify_status
));
1777 if (sig
->verify_ext_status
> 0) {
1778 fprintf(ctx
->debug_stream
, " (%s)\n",
1779 pdkim_verify_ext_status_str(sig
->verify_ext_status
));
1782 fprintf(ctx
->debug_stream
, "\n");
1789 free(dns_txt_reply
);
1795 /* If requested, set return pointer to signature(s) */
1796 if (return_signatures
!= NULL
) {
1797 *return_signatures
= ctx
->sig
;
1804 /* -------------------------------------------------------------------------- */
1805 DLLEXPORT pdkim_ctx
*pdkim_init_verify(int input_mode
,
1806 int(*dns_txt_callback
)(char *, char *)
1808 pdkim_ctx
*ctx
= malloc(sizeof(pdkim_ctx
));
1809 if (ctx
== NULL
) return NULL
;
1810 memset(ctx
,0,sizeof(pdkim_ctx
));
1812 ctx
->linebuf
= malloc(PDKIM_MAX_BODY_LINE_LEN
);
1813 if (ctx
->linebuf
== NULL
) {
1818 ctx
->mode
= PDKIM_MODE_VERIFY
;
1819 ctx
->input_mode
= input_mode
;
1820 ctx
->dns_txt_callback
= dns_txt_callback
;
1826 /* -------------------------------------------------------------------------- */
1827 DLLEXPORT pdkim_ctx
*pdkim_init_sign(int input_mode
,
1830 char *rsa_privkey
) {
1832 pdkim_signature
*sig
;
1834 if (!domain
|| !selector
|| !rsa_privkey
) return NULL
;
1836 ctx
= malloc(sizeof(pdkim_ctx
));
1837 if (ctx
== NULL
) return NULL
;
1838 memset(ctx
,0,sizeof(pdkim_ctx
));
1840 ctx
->linebuf
= malloc(PDKIM_MAX_BODY_LINE_LEN
);
1841 if (ctx
->linebuf
== NULL
) {
1846 sig
= malloc(sizeof(pdkim_signature
));
1852 memset(sig
,0,sizeof(pdkim_signature
));
1853 sig
->bodylength
= -1;
1855 ctx
->mode
= PDKIM_MODE_SIGN
;
1856 ctx
->input_mode
= input_mode
;
1859 ctx
->sig
->domain
= strdup(domain
);
1860 ctx
->sig
->selector
= strdup(selector
);
1861 ctx
->sig
->rsa_privkey
= strdup(rsa_privkey
);
1863 if (!ctx
->sig
->domain
|| !ctx
->sig
->selector
|| !ctx
->sig
->rsa_privkey
) {
1864 pdkim_free_ctx(ctx
);
1868 ctx
->sig
->sha1_body
= malloc(sizeof(sha1_context
));
1869 if (ctx
->sig
->sha1_body
== NULL
) {
1870 pdkim_free_ctx(ctx
);
1873 sha1_starts(ctx
->sig
->sha1_body
);
1875 ctx
->sig
->sha2_body
= malloc(sizeof(sha2_context
));
1876 if (ctx
->sig
->sha2_body
== NULL
) {
1877 pdkim_free_ctx(ctx
);
1880 sha2_starts(ctx
->sig
->sha2_body
,0);
1886 /* -------------------------------------------------------------------------- */
1887 DLLEXPORT
void pdkim_set_debug_stream(pdkim_ctx
*ctx
,
1888 FILE *debug_stream
) {
1889 ctx
->debug_stream
= debug_stream
;
1893 /* -------------------------------------------------------------------------- */
1894 DLLEXPORT
int pdkim_set_optional(pdkim_ctx
*ctx
,
1901 unsigned long created
,
1902 unsigned long expires
) {
1904 if (identity
!= NULL
) {
1905 ctx
->sig
->identity
= strdup(identity
);
1906 if (ctx
->sig
->identity
== NULL
) return PDKIM_ERR_OOM
;
1909 if (sign_headers
!= NULL
) {
1910 ctx
->sig
->sign_headers
= strdup(sign_headers
);
1911 if (ctx
->sig
->sign_headers
== NULL
) return PDKIM_ERR_OOM
;
1914 ctx
->sig
->canon_headers
= canon_headers
;
1915 ctx
->sig
->canon_body
= canon_body
;
1916 ctx
->sig
->bodylength
= bodylength
;
1917 ctx
->sig
->algo
= algo
;
1918 ctx
->sig
->created
= created
;
1919 ctx
->sig
->expires
= expires
;