Commit | Line | Data |
---|---|---|
80a47a2c TK |
1 | /* |
2 | * PDKIM - a RFC4871 (DKIM) implementation | |
3 | * | |
3386088d | 4 | * Copyright (C) 2009 - 2015 Tom Kistner <tom@duncanthrax.net> |
80a47a2c TK |
5 | * |
6 | * http://duncanthrax.net/pdkim/ | |
7 | * | |
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. | |
12 | * | |
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. | |
17 | * | |
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. | |
21 | */ | |
22 | ||
80a47a2c TK |
23 | #include <stdlib.h> |
24 | #include <stdio.h> | |
25 | #include <string.h> | |
26 | #include <ctype.h> | |
27 | ||
3045f050 | 28 | #include "../mytypes.h" |
80a47a2c | 29 | #include "pdkim.h" |
ac3ad426 | 30 | #include "pdkim-rsa.h" |
80a47a2c | 31 | |
ac3ad426 AM |
32 | #include "polarssl/sha1.h" |
33 | #include "polarssl/sha2.h" | |
34 | #include "polarssl/rsa.h" | |
35 | #include "polarssl/base64.h" | |
80a47a2c TK |
36 | |
37 | #define PDKIM_SIGNATURE_VERSION "1" | |
38 | #define PDKIM_PUB_RECORD_VERSION "DKIM1" | |
39 | ||
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" | |
52 | ||
53 | /* -------------------------------------------------------------------------- */ | |
54 | struct pdkim_stringlist { | |
55 | char *value; | |
37f8b554 | 56 | int tag; |
80a47a2c TK |
57 | void *next; |
58 | }; | |
59 | ||
60 | #define PDKIM_STR_ALLOC_FRAG 256 | |
61 | struct pdkim_str { | |
62 | char *str; | |
63 | unsigned int len; | |
64 | unsigned int allocated; | |
65 | }; | |
66 | ||
67 | /* -------------------------------------------------------------------------- */ | |
68 | /* A bunch of list constants */ | |
1ba28e2b | 69 | const char *pdkim_querymethods[] = { |
80a47a2c TK |
70 | "dns/txt", |
71 | NULL | |
72 | }; | |
1ba28e2b | 73 | const char *pdkim_algos[] = { |
80a47a2c TK |
74 | "rsa-sha256", |
75 | "rsa-sha1", | |
76 | NULL | |
77 | }; | |
1ba28e2b | 78 | const char *pdkim_canons[] = { |
80a47a2c TK |
79 | "simple", |
80 | "relaxed", | |
81 | NULL | |
82 | }; | |
1ba28e2b | 83 | const char *pdkim_hashes[] = { |
80a47a2c TK |
84 | "sha256", |
85 | "sha1", | |
86 | NULL | |
87 | }; | |
1ba28e2b | 88 | const char *pdkim_keytypes[] = { |
80a47a2c TK |
89 | "rsa", |
90 | NULL | |
91 | }; | |
92 | ||
93 | typedef struct pdkim_combined_canon_entry { | |
1ba28e2b | 94 | const char *str; |
80a47a2c TK |
95 | int canon_headers; |
96 | int canon_body; | |
97 | } pdkim_combined_canon_entry; | |
98 | pdkim_combined_canon_entry pdkim_combined_canons[] = { | |
99 | { "simple/simple", PDKIM_CANON_SIMPLE, PDKIM_CANON_SIMPLE }, | |
100 | { "simple/relaxed", PDKIM_CANON_SIMPLE, PDKIM_CANON_RELAXED }, | |
101 | { "relaxed/simple", PDKIM_CANON_RELAXED, PDKIM_CANON_SIMPLE }, | |
102 | { "relaxed/relaxed", PDKIM_CANON_RELAXED, PDKIM_CANON_RELAXED }, | |
103 | { "simple", PDKIM_CANON_SIMPLE, PDKIM_CANON_SIMPLE }, | |
104 | { "relaxed", PDKIM_CANON_RELAXED, PDKIM_CANON_SIMPLE }, | |
105 | { NULL, 0, 0 } | |
106 | }; | |
107 | ||
108 | ||
1ba28e2b | 109 | const char *pdkim_verify_status_str(int status) { |
ff7ddfd7 TK |
110 | switch(status) { |
111 | case PDKIM_VERIFY_NONE: return "PDKIM_VERIFY_NONE"; | |
112 | case PDKIM_VERIFY_INVALID: return "PDKIM_VERIFY_INVALID"; | |
113 | case PDKIM_VERIFY_FAIL: return "PDKIM_VERIFY_FAIL"; | |
114 | case PDKIM_VERIFY_PASS: return "PDKIM_VERIFY_PASS"; | |
115 | default: return "PDKIM_VERIFY_UNKNOWN"; | |
116 | } | |
117 | } | |
1ba28e2b | 118 | const char *pdkim_verify_ext_status_str(int ext_status) { |
ff7ddfd7 TK |
119 | switch(ext_status) { |
120 | case PDKIM_VERIFY_FAIL_BODY: return "PDKIM_VERIFY_FAIL_BODY"; | |
121 | case PDKIM_VERIFY_FAIL_MESSAGE: return "PDKIM_VERIFY_FAIL_MESSAGE"; | |
122 | case PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE: return "PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE"; | |
123 | case PDKIM_VERIFY_INVALID_BUFFER_SIZE: return "PDKIM_VERIFY_INVALID_BUFFER_SIZE"; | |
124 | case PDKIM_VERIFY_INVALID_PUBKEY_PARSING: return "PDKIM_VERIFY_INVALID_PUBKEY_PARSING"; | |
125 | default: return "PDKIM_VERIFY_UNKNOWN"; | |
126 | } | |
127 | } | |
128 | ||
129 | ||
80a47a2c TK |
130 | /* -------------------------------------------------------------------------- */ |
131 | /* Print debugging functions */ | |
132 | #ifdef PDKIM_DEBUG | |
3045f050 JH |
133 | void |
134 | pdkim_quoteprint(FILE *stream, const char *data, int len, int lf) | |
135 | { | |
136 | int i; | |
137 | const unsigned char *p = (const unsigned char *)data; | |
138 | ||
139 | for (i = 0; i<len; i++) | |
140 | { | |
141 | const int c = p[i]; | |
142 | switch (c) | |
143 | { | |
144 | case ' ' : fprintf(stream,"{SP}"); break; | |
145 | case '\t': fprintf(stream,"{TB}"); break; | |
146 | case '\r': fprintf(stream,"{CR}"); break; | |
147 | case '\n': fprintf(stream,"{LF}"); break; | |
148 | case '{' : fprintf(stream,"{BO}"); break; | |
149 | case '}' : fprintf(stream,"{BC}"); break; | |
150 | default: | |
151 | if ( (c < 32) || (c > 127) ) | |
152 | fprintf(stream,"{%02x}",c); | |
153 | else | |
154 | fputc(c,stream); | |
80a47a2c TK |
155 | break; |
156 | } | |
157 | } | |
3045f050 JH |
158 | if (lf) |
159 | fputc('\n',stream); | |
80a47a2c | 160 | } |
80a47a2c | 161 | |
3045f050 JH |
162 | void |
163 | pdkim_hexprint(FILE *stream, const char *data, int len, int lf) | |
164 | { | |
165 | int i; | |
166 | const unsigned char *p = (const unsigned char *)data; | |
167 | ||
168 | for (i =0 ; i<len; i++) | |
169 | { | |
170 | const int c = p[i]; | |
171 | fprintf(stream,"%02x",c); | |
80a47a2c | 172 | } |
3045f050 JH |
173 | if (lf) |
174 | fputc('\n',stream); | |
80a47a2c TK |
175 | } |
176 | #endif | |
177 | ||
178 | ||
179 | /* -------------------------------------------------------------------------- */ | |
180 | /* Simple string list implementation for convinience */ | |
3045f050 JH |
181 | pdkim_stringlist * |
182 | pdkim_append_stringlist(pdkim_stringlist *base, char *str) | |
183 | { | |
184 | pdkim_stringlist *new_entry = malloc(sizeof(pdkim_stringlist)); | |
185 | ||
186 | if (!new_entry) return NULL; | |
187 | memset(new_entry,0,sizeof(pdkim_stringlist)); | |
188 | if (!(new_entry->value = strdup(str))) return NULL; | |
189 | if (base) | |
190 | { | |
191 | pdkim_stringlist *last = base; | |
192 | while (last->next != NULL) { last = last->next; } | |
193 | last->next = new_entry; | |
194 | return base; | |
97713345 | 195 | } |
3045f050 | 196 | else |
97713345 | 197 | return new_entry; |
6ab02e3f | 198 | } |
80a47a2c | 199 | |
3045f050 JH |
200 | pdkim_stringlist * |
201 | pdkim_prepend_stringlist(pdkim_stringlist *base, char *str) | |
202 | { | |
203 | pdkim_stringlist *new_entry = malloc(sizeof(pdkim_stringlist)); | |
204 | ||
205 | if (!new_entry) return NULL; | |
206 | memset(new_entry,0,sizeof(pdkim_stringlist)); | |
207 | if (!(new_entry->value = strdup(str))) return NULL; | |
208 | if (base) | |
209 | new_entry->next = base; | |
210 | return new_entry; | |
211 | } | |
212 | ||
80a47a2c TK |
213 | |
214 | /* -------------------------------------------------------------------------- */ | |
215 | /* A small "growing string" implementation to escape malloc/realloc hell */ | |
3045f050 JH |
216 | |
217 | pdkim_str * | |
218 | pdkim_strnew (const char *cstr) | |
219 | { | |
220 | unsigned int len = cstr ? strlen(cstr) : 0; | |
221 | pdkim_str *p = malloc(sizeof(pdkim_str)); | |
222 | ||
223 | if (!p) return NULL; | |
224 | memset(p,0,sizeof(pdkim_str)); | |
225 | if (!(p->str = malloc(len+1))) | |
226 | { | |
227 | free(p); | |
228 | return NULL; | |
80a47a2c | 229 | } |
3045f050 JH |
230 | p->allocated = len+1; |
231 | p->len = len; | |
232 | if (cstr) | |
233 | strcpy(p->str,cstr); | |
234 | else | |
235 | p->str[p->len] = '\0'; | |
236 | return p; | |
6ab02e3f | 237 | } |
3045f050 JH |
238 | |
239 | char * | |
240 | pdkim_strncat(pdkim_str *str, const char *data, int len) | |
241 | { | |
242 | if ((str->allocated - str->len) < (len+1)) | |
243 | { | |
244 | /* Extend the buffer */ | |
245 | int num_frags = ((len+1)/PDKIM_STR_ALLOC_FRAG)+1; | |
246 | char *n = realloc(str->str, | |
247 | (str->allocated+(num_frags*PDKIM_STR_ALLOC_FRAG))); | |
248 | if (n == NULL) return NULL; | |
249 | str->str = n; | |
250 | str->allocated += (num_frags*PDKIM_STR_ALLOC_FRAG); | |
80a47a2c | 251 | } |
3045f050 JH |
252 | strncpy(&(str->str[str->len]), data, len); |
253 | str->len += len; | |
254 | str->str[str->len] = '\0'; | |
255 | return str->str; | |
6ab02e3f | 256 | } |
3045f050 JH |
257 | |
258 | char * | |
259 | pdkim_strcat(pdkim_str *str, const char *cstr) | |
260 | { | |
261 | return pdkim_strncat(str, cstr, strlen(cstr)); | |
6ab02e3f | 262 | } |
05b7d6de | 263 | |
3045f050 JH |
264 | char * |
265 | pdkim_numcat(pdkim_str *str, unsigned long num) | |
266 | { | |
267 | char minibuf[20]; | |
268 | snprintf(minibuf,20,"%lu",num); | |
269 | return pdkim_strcat(str,minibuf); | |
6ab02e3f | 270 | } |
3045f050 JH |
271 | |
272 | char * | |
273 | pdkim_strtrim(pdkim_str *str) | |
274 | { | |
275 | char *p = str->str; | |
276 | char *q = str->str; | |
277 | while ( (*p != '\0') && ((*p == '\t') || (*p == ' ')) ) p++; | |
278 | while (*p != '\0') {*q = *p; q++; p++;} | |
279 | *q = '\0'; | |
280 | while ( (q != str->str) && ( (*q == '\0') || (*q == '\t') || (*q == ' ') ) ) | |
281 | { | |
80a47a2c | 282 | *q = '\0'; |
3045f050 | 283 | q--; |
80a47a2c | 284 | } |
3045f050 JH |
285 | str->len = strlen(str->str); |
286 | return str->str; | |
6ab02e3f | 287 | } |
3045f050 JH |
288 | |
289 | char * | |
290 | pdkim_strclear(pdkim_str *str) | |
291 | { | |
292 | str->str[0] = '\0'; | |
293 | str->len = 0; | |
294 | return str->str; | |
6ab02e3f | 295 | } |
3045f050 JH |
296 | |
297 | void | |
298 | pdkim_strfree(pdkim_str *str) | |
299 | { | |
300 | if (!str) return; | |
301 | if (str->str) free(str->str); | |
302 | free(str); | |
6ab02e3f | 303 | } |
80a47a2c TK |
304 | |
305 | ||
306 | ||
307 | /* -------------------------------------------------------------------------- */ | |
3045f050 JH |
308 | |
309 | void | |
310 | pdkim_free_pubkey(pdkim_pubkey *pub) | |
311 | { | |
312 | if (pub) | |
313 | { | |
314 | if (pub->version ) free(pub->version); | |
315 | if (pub->granularity) free(pub->granularity); | |
316 | if (pub->hashes ) free(pub->hashes); | |
317 | if (pub->keytype ) free(pub->keytype); | |
318 | if (pub->srvtype ) free(pub->srvtype); | |
319 | if (pub->notes ) free(pub->notes); | |
320 | if (pub->key ) free(pub->key); | |
321 | free(pub); | |
80a47a2c TK |
322 | } |
323 | } | |
324 | ||
325 | ||
326 | /* -------------------------------------------------------------------------- */ | |
3045f050 JH |
327 | |
328 | void | |
329 | pdkim_free_sig(pdkim_signature *sig) | |
330 | { | |
331 | if (sig) | |
332 | { | |
333 | pdkim_signature *next = (pdkim_signature *)sig->next; | |
334 | ||
335 | pdkim_stringlist *e = sig->headers; | |
336 | while(e) | |
337 | { | |
338 | pdkim_stringlist *c = e; | |
339 | if (e->value) free(e->value); | |
340 | e = e->next; | |
341 | free(c); | |
80a47a2c TK |
342 | } |
343 | ||
3045f050 JH |
344 | if (sig->sigdata ) free(sig->sigdata); |
345 | if (sig->bodyhash ) free(sig->bodyhash); | |
346 | if (sig->selector ) free(sig->selector); | |
347 | if (sig->domain ) free(sig->domain); | |
348 | if (sig->identity ) free(sig->identity); | |
349 | if (sig->headernames ) free(sig->headernames); | |
350 | if (sig->copiedheaders ) free(sig->copiedheaders); | |
351 | if (sig->rsa_privkey ) free(sig->rsa_privkey); | |
352 | if (sig->sign_headers ) free(sig->sign_headers); | |
353 | if (sig->signature_header) free(sig->signature_header); | |
354 | if (sig->sha1_body ) free(sig->sha1_body); | |
355 | if (sig->sha2_body ) free(sig->sha2_body); | |
356 | ||
357 | if (sig->pubkey) pdkim_free_pubkey(sig->pubkey); | |
358 | ||
359 | free(sig); | |
360 | if (next) pdkim_free_sig(next); | |
80a47a2c | 361 | } |
6ab02e3f | 362 | } |
80a47a2c TK |
363 | |
364 | ||
365 | /* -------------------------------------------------------------------------- */ | |
3045f050 JH |
366 | |
367 | DLLEXPORT void | |
368 | pdkim_free_ctx(pdkim_ctx *ctx) | |
369 | { | |
370 | if (ctx) | |
371 | { | |
372 | pdkim_stringlist *e = ctx->headers; | |
373 | while(e) | |
374 | { | |
375 | pdkim_stringlist *c = e; | |
376 | if (e->value) free(e->value); | |
377 | e = e->next; | |
378 | free(c); | |
37f8b554 | 379 | } |
3045f050 JH |
380 | pdkim_free_sig(ctx->sig); |
381 | pdkim_strfree(ctx->cur_header); | |
382 | free(ctx); | |
80a47a2c | 383 | } |
6ab02e3f | 384 | } |
80a47a2c TK |
385 | |
386 | ||
387 | /* -------------------------------------------------------------------------- */ | |
388 | /* Matches the name of the passed raw "header" against | |
389 | the passed colon-separated "list", starting at entry | |
390 | "start". Returns the position of the header name in | |
391 | the list. */ | |
3045f050 JH |
392 | |
393 | int | |
394 | header_name_match(const char *header, | |
1ba28e2b | 395 | char *tick, |
3045f050 JH |
396 | int do_tick) |
397 | { | |
398 | char *hname; | |
399 | char *lcopy; | |
400 | char *p; | |
401 | char *q; | |
402 | int rc = PDKIM_FAIL; | |
403 | ||
404 | /* Get header name */ | |
405 | char *hcolon = strchr(header,':'); | |
406 | ||
407 | if (!hcolon) return rc; /* This isn't a header */ | |
408 | ||
409 | if (!(hname = malloc((hcolon-header)+1))) | |
410 | return PDKIM_ERR_OOM; | |
411 | memset(hname,0,(hcolon-header)+1); | |
412 | strncpy(hname,header,(hcolon-header)); | |
413 | ||
414 | /* Copy tick-off list locally, so we can punch zeroes into it */ | |
415 | if (!(lcopy = strdup(tick))) | |
416 | { | |
417 | free(hname); | |
418 | return PDKIM_ERR_OOM; | |
80a47a2c | 419 | } |
3045f050 JH |
420 | p = lcopy; |
421 | q = strchr(p,':'); | |
422 | while (q) | |
423 | { | |
424 | *q = '\0'; | |
80a47a2c | 425 | |
3045f050 JH |
426 | if (strcasecmp(p,hname) == 0) |
427 | { | |
428 | rc = PDKIM_OK; | |
429 | /* Invalidate header name instance in tick-off list */ | |
430 | if (do_tick) tick[p-lcopy] = '_'; | |
431 | goto BAIL; | |
80a47a2c TK |
432 | } |
433 | ||
3045f050 JH |
434 | p = q+1; |
435 | q = strchr(p,':'); | |
80a47a2c TK |
436 | } |
437 | ||
3045f050 JH |
438 | if (strcasecmp(p,hname) == 0) |
439 | { | |
440 | rc = PDKIM_OK; | |
441 | /* Invalidate header name instance in tick-off list */ | |
442 | if (do_tick) tick[p-lcopy] = '_'; | |
80a47a2c TK |
443 | } |
444 | ||
3045f050 JH |
445 | BAIL: |
446 | free(hname); | |
447 | free(lcopy); | |
448 | return rc; | |
80a47a2c TK |
449 | } |
450 | ||
451 | ||
452 | /* -------------------------------------------------------------------------- */ | |
453 | /* Performs "relaxed" canonicalization of a header. The returned pointer needs | |
454 | to be free()d. */ | |
3045f050 JH |
455 | |
456 | char * | |
457 | pdkim_relax_header (char *header, int crlf) | |
458 | { | |
459 | BOOL past_field_name = FALSE; | |
460 | BOOL seen_wsp = FALSE; | |
461 | char *p; | |
462 | char *q; | |
463 | char *relaxed = malloc(strlen(header)+3); | |
464 | ||
465 | if (!relaxed) return NULL; | |
466 | ||
467 | q = relaxed; | |
468 | for (p = header; *p != '\0'; p++) | |
469 | { | |
470 | int c = *p; | |
471 | /* Ignore CR & LF */ | |
472 | if (c == '\r' || c == '\n') | |
473 | continue; | |
474 | if (c == '\t' || c == ' ') | |
475 | { | |
476 | if (seen_wsp) | |
80a47a2c | 477 | continue; |
3045f050 JH |
478 | c = ' '; /* Turns WSP into SP */ |
479 | seen_wsp = TRUE; | |
80a47a2c | 480 | } |
3045f050 JH |
481 | else |
482 | if (!past_field_name && c == ':') | |
483 | { | |
484 | if (seen_wsp) q--; /* This removes WSP before the colon */ | |
485 | seen_wsp = TRUE; /* This removes WSP after the colon */ | |
486 | past_field_name = TRUE; | |
80a47a2c | 487 | } |
3045f050 JH |
488 | else |
489 | seen_wsp = FALSE; | |
490 | ||
491 | /* Lowercase header name */ | |
492 | if (!past_field_name) c = tolower(c); | |
493 | *q++ = c; | |
80a47a2c | 494 | } |
3045f050 JH |
495 | |
496 | if (q > relaxed && q[-1] == ' ') q--; /* Squash eventual trailing SP */ | |
497 | *q = '\0'; | |
498 | ||
499 | if (crlf) strcat(relaxed,"\r\n"); | |
500 | return relaxed; | |
6ab02e3f | 501 | } |
80a47a2c TK |
502 | |
503 | ||
504 | /* -------------------------------------------------------------------------- */ | |
505 | #define PDKIM_QP_ERROR_DECODE -1 | |
3045f050 JH |
506 | |
507 | char * | |
508 | pdkim_decode_qp_char(char *qp_p, int *c) | |
509 | { | |
510 | char *initial_pos = qp_p; | |
511 | ||
512 | /* Advance one char */ | |
513 | qp_p++; | |
514 | ||
515 | /* Check for two hex digits and decode them */ | |
516 | if (isxdigit(*qp_p) && isxdigit(qp_p[1])) | |
517 | { | |
518 | /* Do hex conversion */ | |
519 | *c = (isdigit(*qp_p) ? *qp_p - '0' : toupper(*qp_p) - 'A' + 10) << 4; | |
520 | *c != isdigit(qp_p[1]) ? qp_p[1] - '0' : toupper(qp_p[1]) - 'A' + 10; | |
521 | return qp_p + 2; | |
6ab02e3f | 522 | } |
80a47a2c | 523 | |
3045f050 JH |
524 | /* Illegal char here */ |
525 | *c = PDKIM_QP_ERROR_DECODE; | |
526 | return initial_pos; | |
80a47a2c TK |
527 | } |
528 | ||
529 | ||
530 | /* -------------------------------------------------------------------------- */ | |
3045f050 JH |
531 | |
532 | char * | |
533 | pdkim_decode_qp(char *str) | |
534 | { | |
535 | int nchar = 0; | |
536 | char *q; | |
537 | char *p = str; | |
538 | char *n = malloc(strlen(p)+1); | |
539 | ||
540 | if (!n) return NULL; | |
541 | ||
542 | *n = '\0'; | |
543 | q = n; | |
544 | while (*p != '\0') | |
545 | { | |
546 | if (*p == '=') | |
547 | { | |
548 | p = pdkim_decode_qp_char(p, &nchar); | |
549 | if (nchar >= 0) | |
550 | { | |
551 | *q++ = nchar; | |
552 | continue; | |
80a47a2c TK |
553 | } |
554 | } | |
3045f050 JH |
555 | else |
556 | *q++ = *p; | |
557 | p++; | |
80a47a2c | 558 | } |
3045f050 JH |
559 | *q = '\0'; |
560 | return n; | |
80a47a2c TK |
561 | } |
562 | ||
563 | ||
564 | /* -------------------------------------------------------------------------- */ | |
3045f050 JH |
565 | |
566 | char * | |
567 | pdkim_decode_base64(char *str, int *num_decoded) | |
568 | { | |
569 | int dlen = 0; | |
570 | char *res; | |
571 | ||
572 | base64_decode(NULL, &dlen, (unsigned char *)str, strlen(str)); | |
573 | ||
574 | if (!(res = malloc(dlen+1))) | |
575 | return NULL; | |
576 | if (base64_decode((unsigned char *)res, &dlen, (unsigned char *)str, strlen(str)) != 0) | |
577 | { | |
578 | free(res); | |
579 | return NULL; | |
80a47a2c | 580 | } |
3045f050 JH |
581 | |
582 | if (num_decoded) *num_decoded = dlen; | |
583 | return res; | |
80a47a2c TK |
584 | } |
585 | ||
3045f050 | 586 | |
80a47a2c | 587 | /* -------------------------------------------------------------------------- */ |
3045f050 JH |
588 | |
589 | char * | |
590 | pdkim_encode_base64(char *str, int num) | |
591 | { | |
592 | int dlen = 0; | |
593 | char *res; | |
594 | ||
595 | base64_encode(NULL, &dlen, (unsigned char *)str, num); | |
596 | ||
597 | if (!(res = malloc(dlen+1))) | |
598 | return NULL; | |
599 | if (base64_encode((unsigned char *)res, &dlen, (unsigned char *)str, num) != 0) | |
600 | { | |
601 | free(res); | |
602 | return NULL; | |
80a47a2c | 603 | } |
3045f050 | 604 | return res; |
80a47a2c TK |
605 | } |
606 | ||
607 | ||
608 | /* -------------------------------------------------------------------------- */ | |
609 | #define PDKIM_HDR_LIMBO 0 | |
610 | #define PDKIM_HDR_TAG 1 | |
611 | #define PDKIM_HDR_VALUE 2 | |
3045f050 JH |
612 | |
613 | pdkim_signature * | |
614 | pdkim_parse_sig_header(pdkim_ctx *ctx, char *raw_hdr) | |
615 | { | |
616 | pdkim_signature *sig ; | |
617 | char *p,*q; | |
618 | pdkim_str *cur_tag = NULL; | |
619 | pdkim_str *cur_val = NULL; | |
620 | BOOL past_hname = FALSE; | |
621 | BOOL in_b_val = FALSE; | |
622 | int where = PDKIM_HDR_LIMBO; | |
623 | int i; | |
624 | ||
625 | if (!(sig = malloc(sizeof(pdkim_signature)))) return NULL; | |
626 | memset(sig,0,sizeof(pdkim_signature)); | |
627 | sig->bodylength = -1; | |
628 | ||
629 | if (!(sig->rawsig_no_b_val = malloc(strlen(raw_hdr)+1))) | |
630 | { | |
631 | free(sig); | |
632 | return NULL; | |
80a47a2c TK |
633 | } |
634 | ||
3045f050 | 635 | q = sig->rawsig_no_b_val; |
80a47a2c | 636 | |
3045f050 JH |
637 | for (p = raw_hdr; ; p++) |
638 | { | |
639 | char c = *p; | |
80a47a2c | 640 | |
3045f050 JH |
641 | /* Ignore FWS */ |
642 | if (c == '\r' || c == '\n') | |
643 | goto NEXT_CHAR; | |
80a47a2c | 644 | |
3045f050 JH |
645 | /* Fast-forward through header name */ |
646 | if (!past_hname) | |
647 | { | |
648 | if (c == ':') past_hname = TRUE; | |
649 | goto NEXT_CHAR; | |
80a47a2c TK |
650 | } |
651 | ||
3045f050 JH |
652 | if (where == PDKIM_HDR_LIMBO) |
653 | { | |
654 | /* In limbo, just wait for a tag-char to appear */ | |
655 | if (!(c >= 'a' && c <= 'z')) | |
656 | goto NEXT_CHAR; | |
80a47a2c | 657 | |
3045f050 | 658 | where = PDKIM_HDR_TAG; |
80a47a2c TK |
659 | } |
660 | ||
3045f050 JH |
661 | if (where == PDKIM_HDR_TAG) |
662 | { | |
663 | if (!cur_tag) | |
664 | cur_tag = pdkim_strnew(NULL); | |
80a47a2c | 665 | |
3045f050 JH |
666 | if (c >= 'a' && c <= 'z') |
667 | pdkim_strncat(cur_tag, p, 1); | |
80a47a2c | 668 | |
3045f050 JH |
669 | if (c == '=') |
670 | { | |
671 | if (strcmp(cur_tag->str, "b") == 0) | |
672 | { | |
673 | *q = '='; q++; | |
674 | in_b_val = TRUE; | |
675 | } | |
676 | where = PDKIM_HDR_VALUE; | |
677 | goto NEXT_CHAR; | |
80a47a2c TK |
678 | } |
679 | } | |
680 | ||
3045f050 JH |
681 | if (where == PDKIM_HDR_VALUE) |
682 | { | |
683 | if (!cur_val) | |
684 | cur_val = pdkim_strnew(NULL); | |
685 | ||
686 | if (c == '\r' || c == '\n' || c == ' ' || c == '\t') | |
687 | goto NEXT_CHAR; | |
688 | ||
689 | if (c == ';' || c == '\0') | |
690 | { | |
691 | if (cur_tag->len > 0) | |
692 | { | |
693 | pdkim_strtrim(cur_val); | |
694 | ||
695 | #ifdef PDKIM_DEBUG | |
696 | if (ctx->debug_stream) | |
697 | fprintf(ctx->debug_stream, " %s=%s\n", cur_tag->str, cur_val->str); | |
698 | #endif | |
699 | ||
700 | switch (cur_tag->str[0]) | |
701 | { | |
702 | case 'b': | |
703 | if (cur_tag->str[1] == 'h') | |
704 | sig->bodyhash = pdkim_decode_base64(cur_val->str, | |
705 | &sig->bodyhash_len); | |
706 | else | |
707 | sig->sigdata = pdkim_decode_base64(cur_val->str, | |
708 | &sig->sigdata_len); | |
709 | break; | |
710 | case 'v': | |
711 | /* We only support version 1, and that is currently the | |
712 | only version there is. */ | |
713 | if (strcmp(cur_val->str, PDKIM_SIGNATURE_VERSION) == 0) | |
714 | sig->version = 1; | |
715 | break; | |
716 | case 'a': | |
717 | for (i = 0; pdkim_algos[i]; i++) | |
718 | if (strcmp(cur_val->str, pdkim_algos[i]) == 0) | |
719 | { | |
720 | sig->algo = i; | |
721 | break; | |
722 | } | |
723 | break; | |
724 | case 'c': | |
725 | for (i = 0; pdkim_combined_canons[i].str; i++) | |
726 | if (strcmp(cur_val->str, pdkim_combined_canons[i].str) == 0) | |
727 | { | |
728 | sig->canon_headers = pdkim_combined_canons[i].canon_headers; | |
729 | sig->canon_body = pdkim_combined_canons[i].canon_body; | |
730 | break; | |
731 | } | |
732 | break; | |
733 | case 'q': | |
734 | for (i = 0; pdkim_querymethods[i]; i++) | |
735 | if (strcmp(cur_val->str, pdkim_querymethods[i]) == 0) | |
736 | { | |
737 | sig->querymethod = i; | |
738 | break; | |
739 | } | |
740 | break; | |
741 | case 's': | |
742 | sig->selector = strdup(cur_val->str); break; | |
743 | case 'd': | |
744 | sig->domain = strdup(cur_val->str); break; | |
745 | case 'i': | |
746 | sig->identity = pdkim_decode_qp(cur_val->str); break; | |
747 | case 't': | |
748 | sig->created = strtoul(cur_val->str, NULL, 10); break; | |
749 | case 'x': | |
750 | sig->expires = strtoul(cur_val->str, NULL, 10); break; | |
751 | case 'l': | |
752 | sig->bodylength = strtol(cur_val->str, NULL, 10); break; | |
753 | case 'h': | |
754 | sig->headernames = strdup(cur_val->str); break; | |
755 | case 'z': | |
756 | sig->copiedheaders = pdkim_decode_qp(cur_val->str); break; | |
757 | default: | |
758 | #ifdef PDKIM_DEBUG | |
759 | if (ctx->debug_stream) | |
760 | fprintf(ctx->debug_stream, " Unknown tag encountered\n"); | |
761 | #endif | |
762 | break; | |
763 | } | |
764 | } | |
765 | pdkim_strclear(cur_tag); | |
766 | pdkim_strclear(cur_val); | |
767 | in_b_val = FALSE; | |
768 | where = PDKIM_HDR_LIMBO; | |
80a47a2c | 769 | } |
3045f050 JH |
770 | else |
771 | pdkim_strncat(cur_val, p, 1); | |
80a47a2c TK |
772 | } |
773 | ||
3045f050 JH |
774 | NEXT_CHAR: |
775 | if (c == '\0') | |
776 | break; | |
80a47a2c | 777 | |
3045f050 JH |
778 | if (!in_b_val) |
779 | *q++ = c; | |
80a47a2c TK |
780 | } |
781 | ||
3045f050 JH |
782 | /* Make sure the most important bits are there. */ |
783 | if (!(sig->domain && (*(sig->domain) != '\0') && | |
784 | sig->selector && (*(sig->selector) != '\0') && | |
785 | sig->headernames && (*(sig->headernames) != '\0') && | |
786 | sig->bodyhash && | |
787 | sig->sigdata && | |
788 | sig->version)) | |
789 | { | |
790 | pdkim_free_sig(sig); | |
791 | return NULL; | |
80a47a2c TK |
792 | } |
793 | ||
3045f050 JH |
794 | *q = '\0'; |
795 | /* Chomp raw header. The final newline must not be added to the signature. */ | |
796 | q--; | |
797 | while (q > sig->rawsig_no_b_val && (*q == '\r' || *q == '\n')) | |
798 | *q = '\0'; q--; /*XXX questionable code layout; possible bug */ | |
80a47a2c | 799 | |
3045f050 JH |
800 | #ifdef PDKIM_DEBUG |
801 | if (ctx->debug_stream) | |
802 | { | |
803 | fprintf(ctx->debug_stream, | |
804 | "PDKIM >> Raw signature w/o b= tag value >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"); | |
805 | pdkim_quoteprint(ctx->debug_stream, | |
806 | sig->rawsig_no_b_val, | |
807 | strlen(sig->rawsig_no_b_val), 1); | |
808 | fprintf(ctx->debug_stream, | |
809 | "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"); | |
80a47a2c | 810 | } |
3045f050 | 811 | #endif |
80a47a2c | 812 | |
3045f050 JH |
813 | if ( !(sig->sha1_body = malloc(sizeof(sha1_context))) |
814 | || !(sig->sha2_body = malloc(sizeof(sha2_context))) | |
815 | ) | |
816 | { | |
817 | pdkim_free_sig(sig); | |
818 | return NULL; | |
80a47a2c TK |
819 | } |
820 | ||
3045f050 JH |
821 | sha1_starts(sig->sha1_body); |
822 | sha2_starts(sig->sha2_body, 0); | |
80a47a2c | 823 | |
3045f050 | 824 | return sig; |
80a47a2c TK |
825 | } |
826 | ||
827 | ||
828 | /* -------------------------------------------------------------------------- */ | |
80a47a2c | 829 | |
3045f050 JH |
830 | pdkim_pubkey * |
831 | pdkim_parse_pubkey_record(pdkim_ctx *ctx, char *raw_record) | |
832 | { | |
833 | pdkim_pubkey *pub; | |
834 | char *p; | |
835 | pdkim_str *cur_tag = NULL; | |
836 | pdkim_str *cur_val = NULL; | |
837 | int where = PDKIM_HDR_LIMBO; | |
80a47a2c | 838 | |
3045f050 JH |
839 | if (!(pub = malloc(sizeof(pdkim_pubkey)))) return NULL; |
840 | memset(pub,0,sizeof(pdkim_pubkey)); | |
80a47a2c | 841 | |
3045f050 JH |
842 | for (p = raw_record; ; p++) |
843 | { | |
844 | char c = *p; | |
80a47a2c | 845 | |
3045f050 JH |
846 | /* Ignore FWS */ |
847 | if (c == '\r' || c == '\n') | |
848 | goto NEXT_CHAR; | |
80a47a2c | 849 | |
3045f050 JH |
850 | if (where == PDKIM_HDR_LIMBO) |
851 | { | |
852 | /* In limbo, just wait for a tag-char to appear */ | |
853 | if (!(c >= 'a' && c <= 'z')) | |
854 | goto NEXT_CHAR; | |
80a47a2c | 855 | |
3045f050 | 856 | where = PDKIM_HDR_TAG; |
80a47a2c TK |
857 | } |
858 | ||
3045f050 JH |
859 | if (where == PDKIM_HDR_TAG) |
860 | { | |
861 | if (!cur_tag) | |
862 | cur_tag = pdkim_strnew(NULL); | |
80a47a2c | 863 | |
3045f050 JH |
864 | if (c >= 'a' && c <= 'z') |
865 | pdkim_strncat(cur_tag, p, 1); | |
80a47a2c | 866 | |
3045f050 JH |
867 | if (c == '=') |
868 | { | |
869 | where = PDKIM_HDR_VALUE; | |
870 | goto NEXT_CHAR; | |
80a47a2c TK |
871 | } |
872 | } | |
873 | ||
3045f050 JH |
874 | if (where == PDKIM_HDR_VALUE) |
875 | { | |
876 | if (!cur_val) | |
877 | cur_val = pdkim_strnew(NULL); | |
878 | ||
879 | if (c == '\r' || c == '\n') | |
880 | goto NEXT_CHAR; | |
881 | ||
882 | if (c == ';' || c == '\0') | |
883 | { | |
884 | if (cur_tag->len > 0) | |
885 | { | |
886 | pdkim_strtrim(cur_val); | |
887 | #ifdef PDKIM_DEBUG | |
888 | if (ctx->debug_stream) | |
889 | fprintf(ctx->debug_stream, " %s=%s\n", cur_tag->str, cur_val->str); | |
890 | #endif | |
891 | ||
892 | switch (cur_tag->str[0]) | |
893 | { | |
894 | case 'v': | |
895 | /* This tag isn't evaluated because: | |
896 | - We only support version DKIM1. | |
897 | - Which is the default for this value (set below) | |
898 | - Other versions are currently not specified. */ | |
899 | break; | |
900 | case 'h': | |
901 | pub->hashes = strdup(cur_val->str); break; | |
902 | case 'g': | |
903 | pub->granularity = strdup(cur_val->str); break; | |
904 | case 'n': | |
905 | pub->notes = pdkim_decode_qp(cur_val->str); break; | |
906 | case 'p': | |
907 | pub->key = pdkim_decode_base64(cur_val->str, &(pub->key_len)); break; | |
908 | case 'k': | |
909 | pub->hashes = strdup(cur_val->str); break; | |
910 | case 's': | |
911 | pub->srvtype = strdup(cur_val->str); break; | |
912 | case 't': | |
913 | if (strchr(cur_val->str,'y') != NULL) pub->testing = 1; | |
914 | if (strchr(cur_val->str,'s') != NULL) pub->no_subdomaining = 1; | |
915 | break; | |
916 | default: | |
917 | #ifdef PDKIM_DEBUG | |
918 | if (ctx->debug_stream) | |
919 | fprintf(ctx->debug_stream, " Unknown tag encountered\n"); | |
920 | #endif | |
921 | break; | |
922 | } | |
923 | } | |
924 | pdkim_strclear(cur_tag); | |
925 | pdkim_strclear(cur_val); | |
926 | where = PDKIM_HDR_LIMBO; | |
80a47a2c | 927 | } |
3045f050 JH |
928 | else |
929 | pdkim_strncat(cur_val, p, 1); | |
80a47a2c TK |
930 | } |
931 | ||
3045f050 JH |
932 | NEXT_CHAR: |
933 | if (c == '\0') break; | |
80a47a2c TK |
934 | } |
935 | ||
3045f050 JH |
936 | /* Set fallback defaults */ |
937 | if (!pub->version ) pub->version = strdup(PDKIM_PUB_RECORD_VERSION); | |
938 | if (!pub->granularity) pub->granularity = strdup("*"); | |
939 | if (!pub->keytype ) pub->keytype = strdup("rsa"); | |
940 | if (!pub->srvtype ) pub->srvtype = strdup("*"); | |
80a47a2c | 941 | |
3045f050 JH |
942 | /* p= is required */ |
943 | if (pub->key) | |
80a47a2c | 944 | return pub; |
3045f050 JH |
945 | |
946 | pdkim_free_pubkey(pub); | |
947 | return NULL; | |
80a47a2c TK |
948 | } |
949 | ||
950 | ||
951 | /* -------------------------------------------------------------------------- */ | |
3045f050 JH |
952 | |
953 | int | |
954 | pdkim_update_bodyhash(pdkim_ctx *ctx, const char *data, int len) | |
955 | { | |
956 | pdkim_signature *sig = ctx->sig; | |
957 | /* Cache relaxed version of data */ | |
958 | char *relaxed_data = NULL; | |
959 | int relaxed_len = 0; | |
960 | ||
961 | /* Traverse all signatures, updating their hashes. */ | |
962 | while (sig) | |
963 | { | |
964 | /* Defaults to simple canon (no further treatment necessary) */ | |
965 | const char *canon_data = data; | |
966 | int canon_len = len; | |
967 | ||
968 | if (sig->canon_body == PDKIM_CANON_RELAXED) | |
969 | { | |
970 | /* Relax the line if not done already */ | |
971 | if (!relaxed_data) | |
972 | { | |
973 | BOOL seen_wsp = FALSE; | |
974 | const char *p; | |
975 | int q = 0; | |
976 | ||
977 | if (!(relaxed_data = malloc(len+1))) | |
978 | return PDKIM_ERR_OOM; | |
979 | ||
980 | for (p = data; *p; p++) | |
981 | { | |
982 | char c = *p; | |
983 | if (c == '\r') | |
984 | { | |
985 | if (q > 0 && relaxed_data[q-1] == ' ') | |
986 | q--; | |
987 | } | |
988 | else if (c == '\t' || c == ' ') | |
989 | { | |
990 | c = ' '; /* Turns WSP into SP */ | |
991 | if (seen_wsp) | |
992 | continue; | |
993 | seen_wsp = TRUE; | |
994 | } | |
995 | else | |
996 | seen_wsp = FALSE; | |
997 | relaxed_data[q++] = c; | |
998 | } | |
999 | relaxed_data[q] = '\0'; | |
1000 | relaxed_len = q; | |
80a47a2c | 1001 | } |
3045f050 JH |
1002 | canon_data = relaxed_data; |
1003 | canon_len = relaxed_len; | |
80a47a2c TK |
1004 | } |
1005 | ||
3045f050 JH |
1006 | /* Make sure we don't exceed the to-be-signed body length */ |
1007 | if ( sig->bodylength >= 0 | |
1008 | && sig->signed_body_bytes + (unsigned long)canon_len > sig->bodylength | |
1009 | ) | |
1010 | canon_len = sig->bodylength - sig->signed_body_bytes; | |
80a47a2c | 1011 | |
3045f050 JH |
1012 | if (canon_len > 0) |
1013 | { | |
1014 | if (sig->algo == PDKIM_ALGO_RSA_SHA1) | |
1015 | sha1_update(sig->sha1_body,(unsigned char *)canon_data,canon_len); | |
1016 | else | |
1017 | sha2_update(sig->sha2_body,(unsigned char *)canon_data,canon_len); | |
1018 | ||
1019 | sig->signed_body_bytes += canon_len; | |
80a47a2c | 1020 | #ifdef PDKIM_DEBUG |
3045f050 JH |
1021 | if (ctx->debug_stream) |
1022 | pdkim_quoteprint(ctx->debug_stream, canon_data, canon_len, 1); | |
80a47a2c TK |
1023 | #endif |
1024 | } | |
1025 | ||
3045f050 | 1026 | sig = sig->next; |
80a47a2c TK |
1027 | } |
1028 | ||
3045f050 JH |
1029 | if (relaxed_data) free(relaxed_data); |
1030 | return PDKIM_OK; | |
6ab02e3f | 1031 | } |
80a47a2c TK |
1032 | |
1033 | ||
1034 | /* -------------------------------------------------------------------------- */ | |
80a47a2c | 1035 | |
3045f050 JH |
1036 | int |
1037 | pdkim_finish_bodyhash(pdkim_ctx *ctx) | |
1038 | { | |
1039 | pdkim_signature *sig = ctx->sig; | |
80a47a2c | 1040 | |
3045f050 JH |
1041 | /* Traverse all signatures */ |
1042 | while (sig) | |
1043 | { /* Finish hashes */ | |
1044 | unsigned char bh[32]; /* SHA-256 = 32 Bytes, SHA-1 = 20 Bytes */ | |
1045 | ||
1046 | if (sig->algo == PDKIM_ALGO_RSA_SHA1) | |
1047 | sha1_finish(sig->sha1_body,bh); | |
1048 | else | |
1049 | sha2_finish(sig->sha2_body,bh); | |
1050 | ||
1051 | #ifdef PDKIM_DEBUG | |
1052 | if (ctx->debug_stream) | |
1053 | { | |
1054 | fprintf(ctx->debug_stream, "PDKIM [%s] Body bytes hashed: %lu\n", | |
1055 | sig->domain, sig->signed_body_bytes); | |
1056 | fprintf(ctx->debug_stream, "PDKIM [%s] bh computed: ", sig->domain); | |
1057 | pdkim_hexprint(ctx->debug_stream, (char *)bh, | |
1058 | (sig->algo == PDKIM_ALGO_RSA_SHA1)?20:32,1); | |
80a47a2c | 1059 | } |
3045f050 JH |
1060 | #endif |
1061 | ||
1062 | /* SIGNING -------------------------------------------------------------- */ | |
1063 | if (ctx->mode == PDKIM_MODE_SIGN) | |
1064 | { | |
1065 | sig->bodyhash_len = (sig->algo == PDKIM_ALGO_RSA_SHA1)?20:32; | |
1066 | ||
1067 | if (!(sig->bodyhash = malloc(sig->bodyhash_len))) | |
1068 | return PDKIM_ERR_OOM; | |
1069 | memcpy(sig->bodyhash, bh, sig->bodyhash_len); | |
1070 | ||
1071 | /* If bodylength limit is set, and we have received less bytes | |
1072 | than the requested amount, effectively remove the limit tag. */ | |
1073 | if (sig->signed_body_bytes < sig->bodylength) | |
1074 | sig->bodylength = -1; | |
80a47a2c | 1075 | } |
3045f050 JH |
1076 | |
1077 | /* VERIFICATION --------------------------------------------------------- */ | |
1078 | else | |
1079 | { | |
1080 | /* Compare bodyhash */ | |
1081 | if (memcmp(bh, sig->bodyhash, | |
1082 | (sig->algo == PDKIM_ALGO_RSA_SHA1)?20:32) == 0) | |
1083 | { | |
1084 | #ifdef PDKIM_DEBUG | |
1085 | if (ctx->debug_stream) | |
1086 | fprintf(ctx->debug_stream, "PDKIM [%s] Body hash verified OK\n", | |
1087 | sig->domain); | |
1088 | #endif | |
80a47a2c | 1089 | } |
3045f050 JH |
1090 | else |
1091 | { | |
1092 | #ifdef PDKIM_DEBUG | |
1093 | if (ctx->debug_stream) | |
1094 | { | |
1095 | fprintf(ctx->debug_stream, "PDKIM [%s] bh signature: ", sig->domain); | |
1096 | pdkim_hexprint(ctx->debug_stream, sig->bodyhash, | |
1097 | (sig->algo == PDKIM_ALGO_RSA_SHA1)?20:32,1); | |
1098 | fprintf(ctx->debug_stream, "PDKIM [%s] Body hash did NOT verify\n", | |
1099 | sig->domain); | |
1100 | } | |
1101 | #endif | |
1102 | sig->verify_status = PDKIM_VERIFY_FAIL; | |
1103 | sig->verify_ext_status = PDKIM_VERIFY_FAIL_BODY; | |
80a47a2c TK |
1104 | } |
1105 | } | |
1106 | ||
3045f050 | 1107 | sig = sig->next; |
80a47a2c TK |
1108 | } |
1109 | ||
3045f050 | 1110 | return PDKIM_OK; |
6ab02e3f | 1111 | } |
80a47a2c TK |
1112 | |
1113 | ||
1114 | ||
1115 | /* -------------------------------------------------------------------------- */ | |
1116 | /* Callback from pdkim_feed below for processing complete body lines */ | |
3045f050 JH |
1117 | |
1118 | int | |
1119 | pdkim_bodyline_complete(pdkim_ctx *ctx) | |
1120 | { | |
1121 | char *p = ctx->linebuf; | |
1122 | int n = ctx->linebuf_offset; | |
1123 | ||
1124 | /* Ignore extra data if we've seen the end-of-data marker */ | |
1125 | if (ctx->seen_eod) goto BAIL; | |
1126 | ||
1127 | /* We've always got one extra byte to stuff a zero ... */ | |
1128 | ctx->linebuf[(ctx->linebuf_offset)] = '\0'; | |
1129 | ||
1130 | if (ctx->input_mode == PDKIM_INPUT_SMTP) | |
1131 | { | |
1132 | /* Terminate on EOD marker */ | |
1133 | if (memcmp(p, ".\r\n", 3) == 0) | |
1134 | { | |
1135 | /* In simple body mode, if any empty lines were buffered, | |
1136 | replace with one. rfc 4871 3.4.3 */ | |
1137 | if ( ctx->sig && ctx->sig->canon_body == PDKIM_CANON_SIMPLE | |
1138 | && ctx->num_buffered_crlf > 0 | |
1139 | ) | |
1140 | pdkim_update_bodyhash(ctx, "\r\n",2); | |
1141 | ||
1142 | ctx->seen_eod = 1; | |
1143 | goto BAIL; | |
80a47a2c | 1144 | } |
3045f050 JH |
1145 | /* Unstuff dots */ |
1146 | if (memcmp(p, "..", 2) == 0) | |
1147 | { | |
1148 | p++; | |
1149 | n--; | |
80a47a2c TK |
1150 | } |
1151 | } | |
1152 | ||
3045f050 JH |
1153 | /* Empty lines need to be buffered until we find a non-empty line */ |
1154 | if (memcmp(p, "\r\n", 2) == 0) | |
1155 | { | |
1156 | ctx->num_buffered_crlf++; | |
1157 | goto BAIL; | |
80a47a2c TK |
1158 | } |
1159 | ||
3045f050 JH |
1160 | if ( ctx->sig |
1161 | && ctx->sig->canon_body == PDKIM_CANON_RELAXED | |
1162 | ) | |
1163 | { | |
1164 | /* Lines with just spaces need to be buffered too */ | |
1165 | char *check = p; | |
1166 | while (memcmp(check, "\r\n", 2) != 0) | |
1167 | { | |
1168 | char c = *check; | |
6a11a9e6 | 1169 | |
3045f050 JH |
1170 | if (c != '\t' && c != ' ') |
1171 | goto PROCESS; | |
1172 | check++; | |
6a11a9e6 JH |
1173 | } |
1174 | ||
3045f050 JH |
1175 | ctx->num_buffered_crlf++; |
1176 | goto BAIL; | |
1177 | } | |
6a11a9e6 | 1178 | |
3045f050 JH |
1179 | PROCESS: |
1180 | /* At this point, we have a non-empty line, so release the buffered ones. */ | |
1181 | while (ctx->num_buffered_crlf) | |
1182 | { | |
1183 | pdkim_update_bodyhash(ctx, "\r\n", 2); | |
1184 | ctx->num_buffered_crlf--; | |
80a47a2c TK |
1185 | } |
1186 | ||
3045f050 | 1187 | pdkim_update_bodyhash(ctx, p, n); |
80a47a2c | 1188 | |
3045f050 JH |
1189 | BAIL: |
1190 | ctx->linebuf_offset = 0; | |
1191 | return PDKIM_OK; | |
80a47a2c TK |
1192 | } |
1193 | ||
1194 | ||
1195 | /* -------------------------------------------------------------------------- */ | |
1196 | /* Callback from pdkim_feed below for processing complete headers */ | |
1197 | #define DKIM_SIGNATURE_HEADERNAME "DKIM-Signature:" | |
3045f050 JH |
1198 | |
1199 | int | |
1200 | pdkim_header_complete(pdkim_ctx *ctx) | |
1201 | { | |
1202 | pdkim_signature *sig = ctx->sig; | |
1203 | ||
1204 | /* Special case: The last header can have an extra \r appended */ | |
1205 | if ( (ctx->cur_header->len > 1) && | |
1206 | (ctx->cur_header->str[(ctx->cur_header->len)-1] == '\r') ) | |
1207 | { | |
1208 | ctx->cur_header->str[(ctx->cur_header->len)-1] = '\0'; | |
1209 | ctx->cur_header->len--; | |
80a47a2c TK |
1210 | } |
1211 | ||
3045f050 JH |
1212 | ctx->num_headers++; |
1213 | if (ctx->num_headers > PDKIM_MAX_HEADERS) goto BAIL; | |
80a47a2c | 1214 | |
3045f050 JH |
1215 | /* SIGNING -------------------------------------------------------------- */ |
1216 | if (ctx->mode == PDKIM_MODE_SIGN) | |
1217 | for ( ; sig; sig = sig->next) /* Traverse all signatures */ | |
1218 | if (header_name_match(ctx->cur_header->str, | |
1219 | sig->sign_headers? | |
1220 | sig->sign_headers: | |
1221 | PDKIM_DEFAULT_SIGN_HEADERS, 0) == PDKIM_OK) | |
1222 | { | |
37f8b554 | 1223 | pdkim_stringlist *list; |
80a47a2c | 1224 | |
37f8b554 | 1225 | /* Add header to the signed headers list (in reverse order) */ |
3045f050 JH |
1226 | if (!(list = pdkim_prepend_stringlist(sig->headers, |
1227 | ctx->cur_header->str))) | |
1228 | return PDKIM_ERR_OOM; | |
37f8b554 | 1229 | sig->headers = list; |
3045f050 | 1230 | } |
94431adb | 1231 | |
3045f050 JH |
1232 | /* DKIM-Signature: headers are added to the verification list */ |
1233 | if (ctx->mode == PDKIM_MODE_VERIFY) | |
1234 | { | |
1235 | if (strncasecmp(ctx->cur_header->str, | |
1236 | DKIM_SIGNATURE_HEADERNAME, | |
1237 | strlen(DKIM_SIGNATURE_HEADERNAME)) == 0) | |
1238 | { | |
1239 | pdkim_signature *new_sig; | |
80a47a2c | 1240 | |
3045f050 JH |
1241 | /* Create and chain new signature block */ |
1242 | #ifdef PDKIM_DEBUG | |
1243 | if (ctx->debug_stream) | |
1244 | fprintf(ctx->debug_stream, | |
1245 | "PDKIM >> Found sig, trying to parse >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"); | |
1246 | #endif | |
1247 | ||
1248 | if ((new_sig = pdkim_parse_sig_header(ctx, ctx->cur_header->str))) | |
1249 | { | |
1250 | pdkim_signature *last_sig = ctx->sig; | |
1251 | if (!last_sig) | |
1252 | ctx->sig = new_sig; | |
1253 | else | |
1254 | { | |
1255 | while (last_sig->next) last_sig = last_sig->next; | |
1256 | last_sig->next = new_sig; | |
1257 | } | |
80a47a2c | 1258 | } |
3045f050 JH |
1259 | else |
1260 | #ifdef PDKIM_DEBUG | |
1261 | if (ctx->debug_stream) | |
1262 | { | |
1263 | fprintf(ctx->debug_stream,"Error while parsing signature header\n"); | |
1264 | fprintf(ctx->debug_stream, | |
1265 | "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"); | |
1266 | } | |
1267 | #endif | |
80a47a2c | 1268 | } |
37f8b554 | 1269 | |
3045f050 JH |
1270 | /* every other header is stored for signature verification */ |
1271 | else | |
1272 | { | |
1273 | pdkim_stringlist *list; | |
1274 | ||
1275 | if (!(list = pdkim_prepend_stringlist(ctx->headers, | |
1276 | ctx->cur_header->str))) | |
1277 | return PDKIM_ERR_OOM; | |
1278 | ctx->headers = list; | |
80a47a2c TK |
1279 | } |
1280 | } | |
1281 | ||
3045f050 JH |
1282 | BAIL: |
1283 | pdkim_strclear(ctx->cur_header); /* Re-use existing pdkim_str */ | |
1284 | return PDKIM_OK; | |
6ab02e3f | 1285 | } |
80a47a2c TK |
1286 | |
1287 | ||
1288 | ||
1289 | /* -------------------------------------------------------------------------- */ | |
1290 | #define HEADER_BUFFER_FRAG_SIZE 256 | |
3045f050 JH |
1291 | |
1292 | DLLEXPORT int | |
1293 | pdkim_feed (pdkim_ctx *ctx, char *data, int len) | |
1294 | { | |
1295 | int p; | |
1296 | ||
1297 | for (p = 0; p<len; p++) | |
1298 | { | |
1299 | char c = data[p]; | |
1300 | ||
1301 | if (ctx->past_headers) | |
1302 | { | |
1303 | /* Processing body byte */ | |
1304 | ctx->linebuf[(ctx->linebuf_offset)++] = c; | |
1305 | if (c == '\n') | |
1306 | { | |
1307 | int rc = pdkim_bodyline_complete(ctx); /* End of line */ | |
1308 | if (rc != PDKIM_OK) return rc; | |
80a47a2c | 1309 | } |
3045f050 JH |
1310 | if (ctx->linebuf_offset == (PDKIM_MAX_BODY_LINE_LEN-1)) |
1311 | return PDKIM_ERR_LONG_LINE; | |
80a47a2c | 1312 | } |
3045f050 JH |
1313 | else |
1314 | { | |
1315 | /* Processing header byte */ | |
1316 | if (c != '\r') | |
1317 | { | |
1318 | if (c == '\n') | |
1319 | { | |
1320 | if (ctx->seen_lf) | |
1321 | { | |
1322 | int rc = pdkim_header_complete(ctx); /* Seen last header line */ | |
1323 | if (rc != PDKIM_OK) return rc; | |
1324 | ||
1325 | ctx->past_headers = 1; | |
1326 | ctx->seen_lf = 0; | |
80a47a2c | 1327 | #ifdef PDKIM_DEBUG |
3045f050 JH |
1328 | if (ctx->debug_stream) |
1329 | fprintf(ctx->debug_stream, | |
1330 | "PDKIM >> Hashed body data, canonicalized >>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"); | |
80a47a2c | 1331 | #endif |
3045f050 JH |
1332 | continue; |
1333 | } | |
1334 | else | |
1335 | ctx->seen_lf = 1; | |
1336 | } | |
1337 | else if (ctx->seen_lf) | |
1338 | { | |
1339 | if (!(c == '\t' || c == ' ')) | |
1340 | { | |
1341 | int rc = pdkim_header_complete(ctx); /* End of header */ | |
1342 | if (rc != PDKIM_OK) return rc; | |
1343 | } | |
1344 | ctx->seen_lf = 0; | |
1345 | } | |
80a47a2c | 1346 | } |
3045f050 JH |
1347 | |
1348 | if (!ctx->cur_header) | |
1349 | if (!(ctx->cur_header = pdkim_strnew(NULL))) | |
1350 | return PDKIM_ERR_OOM; | |
1351 | ||
1352 | if (ctx->cur_header->len < PDKIM_MAX_HEADER_LEN) | |
1353 | if (!pdkim_strncat(ctx->cur_header, &data[p], 1)) | |
1354 | return PDKIM_ERR_OOM; | |
80a47a2c TK |
1355 | } |
1356 | } | |
3045f050 | 1357 | return PDKIM_OK; |
6ab02e3f | 1358 | } |
80a47a2c | 1359 | |
05b7d6de JB |
1360 | /* |
1361 | * RFC 5322 specifies that header line length SHOULD be no more than 78 | |
1362 | * lets make it so! | |
1363 | * pdkim_headcat | |
1364 | * returns char* | |
1365 | * | |
1366 | * col: this int holds and receives column number (octets since last '\n') | |
1367 | * str: partial string to append to | |
94431adb | 1368 | * pad: padding, split line or space after before or after eg: ";" |
05b7d6de JB |
1369 | * intro: - must join to payload eg "h=", usually the tag name |
1370 | * payload: eg base64 data - long data can be split arbitrarily. | |
1371 | * | |
1372 | * this code doesn't fold the header in some of the places that RFC4871 | |
1373 | * allows: As per RFC5322(2.2.3) it only folds before or after tag-value | |
1374 | * pairs and inside long values. it also always spaces or breaks after the | |
94431adb | 1375 | * "pad" |
05b7d6de JB |
1376 | * |
1377 | * no guarantees are made for output given out-of range input. like tag | |
1378 | * names loinger than 78, or bogus col. Input is assumed to be free of line breaks. | |
1379 | */ | |
1380 | ||
3045f050 JH |
1381 | static char * |
1382 | pdkim_headcat(int *col, pdkim_str *str, const char * pad, | |
1383 | const char *intro, const char *payload) | |
1384 | { | |
1385 | size_t l; | |
1386 | ||
1387 | if (pad) | |
05b7d6de | 1388 | { |
3045f050 JH |
1389 | l = strlen(pad); |
1390 | if (*col + l > 78) | |
05b7d6de | 1391 | { |
3045f050 JH |
1392 | pdkim_strcat(str, "\r\n\t"); |
1393 | *col = 1; | |
05b7d6de | 1394 | } |
3045f050 JH |
1395 | pdkim_strncat(str, pad, l); |
1396 | *col += l; | |
05b7d6de JB |
1397 | } |
1398 | ||
3045f050 | 1399 | l = (pad?1:0) + (intro?strlen(intro):0); |
05b7d6de | 1400 | |
3045f050 | 1401 | if (*col + l > 78) |
05b7d6de | 1402 | { /*can't fit intro - start a new line to make room.*/ |
3045f050 JH |
1403 | pdkim_strcat(str, "\r\n\t"); |
1404 | *col = 1; | |
1405 | l = intro?strlen(intro):0; | |
05b7d6de JB |
1406 | } |
1407 | ||
3045f050 | 1408 | l += payload ? strlen(payload):0 ; |
05b7d6de | 1409 | |
3045f050 | 1410 | while (l>77) |
05b7d6de | 1411 | { /* this fragment will not fit on a single line */ |
3045f050 | 1412 | if (pad) |
05b7d6de | 1413 | { |
3045f050 JH |
1414 | pdkim_strcat(str, " "); |
1415 | *col += 1; | |
1416 | pad = NULL; /* only want this once */ | |
1417 | l--; | |
05b7d6de | 1418 | } |
3045f050 JH |
1419 | |
1420 | if (intro) | |
05b7d6de | 1421 | { |
3045f050 JH |
1422 | size_t sl = strlen(intro); |
1423 | ||
1424 | pdkim_strncat(str, intro,sl); | |
1425 | *col += sl; | |
1426 | l -= sl; | |
1427 | intro = NULL; /* only want this once */ | |
05b7d6de | 1428 | } |
3045f050 JH |
1429 | |
1430 | if (payload) | |
05b7d6de | 1431 | { |
3045f050 JH |
1432 | size_t sl = strlen(payload); |
1433 | size_t chomp = *col+sl < 77 ? sl : 78-*col; | |
1434 | ||
1435 | pdkim_strncat(str, payload,chomp); | |
1436 | *col += chomp; | |
1437 | payload += chomp; | |
1438 | l -= chomp-1; | |
05b7d6de | 1439 | } |
3045f050 JH |
1440 | |
1441 | /* the while precondition tells us it didn't fit. */ | |
1442 | pdkim_strcat(str, "\r\n\t"); | |
1443 | *col = 1; | |
05b7d6de | 1444 | } |
3045f050 JH |
1445 | |
1446 | if (*col + l > 78) | |
05b7d6de | 1447 | { |
3045f050 JH |
1448 | pdkim_strcat(str, "\r\n\t"); |
1449 | *col = 1; | |
1450 | pad = NULL; | |
05b7d6de JB |
1451 | } |
1452 | ||
3045f050 | 1453 | if (pad) |
05b7d6de | 1454 | { |
3045f050 JH |
1455 | pdkim_strcat(str, " "); |
1456 | *col += 1; | |
1457 | pad = NULL; | |
05b7d6de JB |
1458 | } |
1459 | ||
3045f050 | 1460 | if (intro) |
05b7d6de | 1461 | { |
3045f050 JH |
1462 | size_t sl = strlen(intro); |
1463 | ||
1464 | pdkim_strncat(str, intro, sl); | |
1465 | *col += sl; | |
1466 | l -= sl; | |
1467 | intro = NULL; | |
05b7d6de | 1468 | } |
3045f050 JH |
1469 | |
1470 | if (payload) | |
05b7d6de | 1471 | { |
3045f050 JH |
1472 | size_t sl = strlen(payload); |
1473 | ||
1474 | pdkim_strncat(str, payload, sl); | |
1475 | *col += sl; | |
05b7d6de JB |
1476 | } |
1477 | ||
3045f050 | 1478 | return str->str; |
05b7d6de | 1479 | } |
80a47a2c | 1480 | |
3045f050 | 1481 | |
80a47a2c | 1482 | /* -------------------------------------------------------------------------- */ |
3045f050 JH |
1483 | |
1484 | char * | |
1485 | pdkim_create_header(pdkim_signature *sig, int final) | |
1486 | { | |
1487 | char *rc = NULL; | |
1488 | char *base64_bh = NULL; | |
1489 | char *base64_b = NULL; | |
1490 | int col = 0; | |
1491 | pdkim_str *hdr; | |
1492 | pdkim_str *canon_all; | |
1493 | ||
1494 | if (!(hdr = pdkim_strnew("DKIM-Signature: v="PDKIM_SIGNATURE_VERSION))) | |
1495 | return NULL; | |
1496 | ||
1497 | if (!(canon_all = pdkim_strnew(pdkim_canons[sig->canon_headers]))) | |
1498 | goto BAIL; | |
1499 | ||
1500 | if (!(base64_bh = pdkim_encode_base64(sig->bodyhash, sig->bodyhash_len))) | |
1501 | goto BAIL; | |
1502 | ||
1503 | col = strlen(hdr->str); | |
1504 | ||
1505 | /* Required and static bits */ | |
1506 | if ( | |
1507 | pdkim_headcat(&col,hdr,";","a=",pdkim_algos[sig->algo]) && | |
1508 | pdkim_headcat(&col,hdr,";","q=",pdkim_querymethods[sig->querymethod]) && | |
1509 | pdkim_strcat(canon_all,"/") && | |
1510 | pdkim_strcat(canon_all,pdkim_canons[sig->canon_body]) && | |
1511 | pdkim_headcat(&col,hdr,";","c=",canon_all->str) && | |
1512 | pdkim_headcat(&col,hdr,";","d=",sig->domain) && | |
1513 | pdkim_headcat(&col,hdr,";","s=",sig->selector) | |
1514 | ) | |
1515 | { | |
1516 | /* list of eader names can be split between items. */ | |
05b7d6de | 1517 | { |
3045f050 JH |
1518 | char *n = strdup(sig->headernames); |
1519 | char *f = n; | |
1520 | char *i = "h="; | |
1521 | char *s = ";"; | |
1522 | ||
1523 | if (!n) goto BAIL; | |
1524 | while (*n) | |
05b7d6de | 1525 | { |
3045f050 | 1526 | char *c = strchr(n,':'); |
05b7d6de | 1527 | |
3045f050 | 1528 | if (c) *c ='\0'; |
05b7d6de | 1529 | |
3045f050 JH |
1530 | if (!i) |
1531 | if (!pdkim_headcat(&col,hdr,NULL,NULL,":")) | |
1532 | { | |
1533 | free(f); | |
1534 | goto BAIL; | |
1535 | } | |
1536 | ||
1537 | if (!pdkim_headcat(&col,hdr,s,i,n)) | |
1538 | { | |
1539 | free(f); | |
1540 | goto BAIL; | |
1541 | } | |
1542 | ||
1543 | if (!c) | |
1544 | break; | |
1545 | ||
1546 | n = c+1; | |
1547 | s = NULL; | |
1548 | i = NULL; | |
80a47a2c | 1549 | } |
3045f050 | 1550 | free(f); |
80a47a2c | 1551 | } |
05b7d6de | 1552 | |
3045f050 JH |
1553 | if(!pdkim_headcat(&col, hdr, ";", "bh=", base64_bh)) |
1554 | goto BAIL; | |
1555 | ||
1556 | /* Optional bits */ | |
1557 | if (sig->identity) | |
1558 | if(!pdkim_headcat(&col, hdr, ";", "i=", sig->identity)) | |
1559 | goto BAIL; | |
1560 | ||
1561 | if (sig->created > 0) | |
1562 | { | |
1563 | char minibuf[20]; | |
1564 | ||
1565 | snprintf(minibuf,20,"%lu",sig->created); | |
1566 | if(!pdkim_headcat(&col, hdr, ";", "t=", minibuf)) | |
1567 | goto BAIL; | |
1568 | } | |
1569 | ||
1570 | if (sig->expires > 0) | |
1571 | { | |
1572 | char minibuf[20]; | |
1573 | ||
1574 | snprintf(minibuf,20,"%lu",sig->expires); | |
1575 | if(!pdkim_headcat(&col, hdr, ";", "x=", minibuf)) | |
1576 | goto BAIL; | |
80a47a2c | 1577 | } |
3045f050 JH |
1578 | |
1579 | if (sig->bodylength >= 0) | |
1580 | { | |
1581 | char minibuf[20]; | |
1582 | ||
1583 | snprintf(minibuf,20,"%lu",sig->bodylength); | |
1584 | if(!pdkim_headcat(&col, hdr, ";", "l=", minibuf)) | |
1585 | goto BAIL; | |
80a47a2c TK |
1586 | } |
1587 | ||
3045f050 JH |
1588 | /* Preliminary or final version? */ |
1589 | if (final) | |
1590 | { | |
1591 | if (!(base64_b = pdkim_encode_base64(sig->sigdata, sig->sigdata_len))) | |
1592 | goto BAIL; | |
1593 | if (!pdkim_headcat(&col, hdr, ";", "b=", base64_b)) | |
1594 | goto BAIL; | |
1595 | } | |
1596 | else | |
1597 | if(!pdkim_headcat(&col, hdr, ";", "b=", "")) | |
1598 | goto BAIL; | |
05b7d6de | 1599 | |
3045f050 JH |
1600 | /* add trailing semicolon: I'm not sure if this is actually needed */ |
1601 | if (!pdkim_headcat(&col,hdr,NULL,";","")) | |
1602 | goto BAIL; | |
80a47a2c TK |
1603 | } |
1604 | ||
3045f050 | 1605 | rc = strdup(hdr->str); |
80a47a2c | 1606 | |
3045f050 JH |
1607 | BAIL: |
1608 | pdkim_strfree(hdr); | |
1609 | if (canon_all) pdkim_strfree(canon_all); | |
1610 | if (base64_bh) free(base64_bh); | |
1611 | if (base64_b ) free(base64_b); | |
1612 | return rc; | |
80a47a2c TK |
1613 | } |
1614 | ||
1615 | ||
1616 | /* -------------------------------------------------------------------------- */ | |
3045f050 JH |
1617 | |
1618 | DLLEXPORT int | |
1619 | pdkim_feed_finish(pdkim_ctx *ctx, pdkim_signature **return_signatures) | |
1620 | { | |
1621 | pdkim_signature *sig = ctx->sig; | |
1622 | pdkim_str *headernames = NULL; /* Collected signed header names */ | |
1623 | ||
1624 | /* Check if we must still flush a (partial) header. If that is the | |
1625 | case, the message has no body, and we must compute a body hash | |
1626 | out of '<CR><LF>' */ | |
1627 | if (ctx->cur_header && ctx->cur_header->len) | |
1628 | { | |
1629 | int rc = pdkim_header_complete(ctx); | |
1630 | if (rc != PDKIM_OK) return rc; | |
1631 | pdkim_update_bodyhash(ctx, "\r\n", 2); | |
80a47a2c | 1632 | } |
3045f050 JH |
1633 | else |
1634 | { | |
1635 | /* For non-smtp input, check if there's an unfinished line in the | |
1636 | body line buffer. If that is the case, we must add a CRLF to the | |
1637 | hash to properly terminate the message. */ | |
1638 | if ((ctx->input_mode == PDKIM_INPUT_NORMAL) && ctx->linebuf_offset) | |
1639 | { | |
1640 | pdkim_update_bodyhash(ctx, ctx->linebuf, ctx->linebuf_offset); | |
1641 | pdkim_update_bodyhash(ctx, "\r\n", 2); | |
80a47a2c | 1642 | } |
3045f050 JH |
1643 | #ifdef PDKIM_DEBUG |
1644 | if (ctx->debug_stream) | |
1645 | fprintf(ctx->debug_stream, | |
1646 | "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"); | |
1647 | #endif | |
80a47a2c TK |
1648 | } |
1649 | ||
3045f050 JH |
1650 | /* Build (and/or evaluate) body hash */ |
1651 | if (pdkim_finish_bodyhash(ctx) != PDKIM_OK) | |
1652 | return PDKIM_ERR_OOM; | |
80a47a2c | 1653 | |
3045f050 JH |
1654 | /* SIGNING -------------------------------------------------------------- */ |
1655 | if (ctx->mode == PDKIM_MODE_SIGN) | |
1656 | if (!(headernames = pdkim_strnew(NULL))) | |
1657 | return PDKIM_ERR_OOM; | |
1658 | /* ---------------------------------------------------------------------- */ | |
80a47a2c | 1659 | |
3045f050 JH |
1660 | while (sig) |
1661 | { | |
1662 | sha1_context sha1_headers; | |
1663 | sha2_context sha2_headers; | |
1664 | char *sig_hdr; | |
1665 | char headerhash[32]; | |
80a47a2c | 1666 | |
3045f050 JH |
1667 | if (sig->algo == PDKIM_ALGO_RSA_SHA1) |
1668 | sha1_starts(&sha1_headers); | |
1669 | else | |
1670 | sha2_starts(&sha2_headers,0); | |
80a47a2c | 1671 | |
3045f050 JH |
1672 | #ifdef PDKIM_DEBUG |
1673 | if (ctx->debug_stream) | |
1674 | fprintf(ctx->debug_stream, | |
1675 | "PDKIM >> Hashed header data, canonicalized, in sequence >>>>>>>>>>>>>>\n"); | |
1676 | #endif | |
1677 | ||
1678 | /* SIGNING ---------------------------------------------------------------- */ | |
1679 | /* When signing, walk through our header list and add them to the hash. As we | |
1680 | go, construct a list of the header's names to use for the h= parameter. */ | |
1681 | ||
1682 | if (ctx->mode == PDKIM_MODE_SIGN) | |
1683 | { | |
1684 | pdkim_stringlist *p; | |
1685 | ||
1686 | for (p = sig->headers; p; p = p->next) | |
1687 | { | |
1688 | char *rh = NULL; | |
1689 | /* Collect header names (Note: colon presence is guaranteed here) */ | |
1690 | char *q = strchr(p->value,':'); | |
1691 | ||
1692 | if (!(pdkim_strncat(headernames, p->value, | |
1693 | (q-(p->value)) + (p->next ? 1 : 0)))) | |
1694 | return PDKIM_ERR_OOM; | |
1695 | ||
1696 | rh = sig->canon_headers == PDKIM_CANON_RELAXED | |
1697 | ? pdkim_relax_header(p->value,1) /* cook header for relaxed canon */ | |
1698 | : strdup(p->value); /* just copy it for simple canon */ | |
1699 | if (!rh) | |
1700 | return PDKIM_ERR_OOM; | |
1701 | ||
1702 | /* Feed header to the hash algorithm */ | |
1703 | if (sig->algo == PDKIM_ALGO_RSA_SHA1) | |
1704 | sha1_update(&(sha1_headers), (unsigned char *)rh, strlen(rh)); | |
1705 | else | |
1706 | sha2_update(&(sha2_headers), (unsigned char *)rh, strlen(rh)); | |
1707 | ||
1708 | #ifdef PDKIM_DEBUG | |
1709 | if (ctx->debug_stream) | |
1710 | pdkim_quoteprint(ctx->debug_stream, rh, strlen(rh), 1); | |
1711 | #endif | |
1712 | free(rh); | |
80a47a2c TK |
1713 | } |
1714 | } | |
37f8b554 | 1715 | |
3045f050 JH |
1716 | /* VERIFICATION ----------------------------------------------------------- */ |
1717 | /* When verifying, walk through the header name list in the h= parameter and | |
1718 | add the headers to the hash in that order. */ | |
1719 | else | |
1720 | { | |
1721 | char *b = strdup(sig->headernames); | |
1722 | char *p = b; | |
1723 | char *q = NULL; | |
1724 | pdkim_stringlist *hdrs; | |
1725 | ||
1726 | if (!b) return PDKIM_ERR_OOM; | |
1727 | ||
1728 | /* clear tags */ | |
1729 | for (hdrs = ctx->headers; hdrs; hdrs = hdrs->next) | |
1730 | hdrs->tag = 0; | |
1731 | ||
1732 | while(1) | |
1733 | { | |
1734 | if ((q = strchr(p,':'))) | |
1735 | *q = '\0'; | |
1736 | ||
1737 | for (hdrs = ctx->headers; hdrs; hdrs = hdrs->next) | |
1738 | if ( hdrs->tag == 0 | |
1739 | && strncasecmp(hdrs->value,p,strlen(p)) == 0 | |
1740 | && (hdrs->value)[strlen(p)] == ':' | |
1741 | ) | |
1742 | { | |
1743 | char *rh; | |
1744 | ||
1745 | rh = sig->canon_headers == PDKIM_CANON_RELAXED | |
1746 | ? pdkim_relax_header(hdrs->value,1) /* cook header for relaxed canon */ | |
1747 | : strdup(hdrs->value); /* just copy it for simple canon */ | |
1748 | if (!rh) | |
1749 | return PDKIM_ERR_OOM; | |
1750 | ||
1751 | /* Feed header to the hash algorithm */ | |
1752 | if (sig->algo == PDKIM_ALGO_RSA_SHA1) | |
1753 | sha1_update(&(sha1_headers), (unsigned char *)rh, strlen(rh)); | |
1754 | else | |
1755 | sha2_update(&(sha2_headers), (unsigned char *)rh, strlen(rh)); | |
1756 | ||
1757 | #ifdef PDKIM_DEBUG | |
1758 | if (ctx->debug_stream) | |
1759 | pdkim_quoteprint(ctx->debug_stream, rh, strlen(rh), 1); | |
1760 | #endif | |
1761 | free(rh); | |
1762 | hdrs->tag = 1; | |
1763 | break; | |
1764 | } | |
1765 | ||
1766 | if (!q) break; | |
1767 | p = q+1; | |
80a47a2c | 1768 | } |
3045f050 | 1769 | free(b); |
80a47a2c TK |
1770 | } |
1771 | ||
3045f050 JH |
1772 | #ifdef PDKIM_DEBUG |
1773 | if (ctx->debug_stream) | |
1774 | fprintf(ctx->debug_stream, | |
1775 | "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"); | |
1776 | #endif | |
80a47a2c | 1777 | |
3045f050 JH |
1778 | /* SIGNING ---------------------------------------------------------------- */ |
1779 | if (ctx->mode == PDKIM_MODE_SIGN) | |
1780 | { | |
1781 | /* Copy headernames to signature struct */ | |
1782 | sig->headernames = strdup(headernames->str); | |
1783 | pdkim_strfree(headernames); | |
80a47a2c | 1784 | |
3045f050 JH |
1785 | /* Create signature header with b= omitted */ |
1786 | sig_hdr = pdkim_create_header(ctx->sig, 0); | |
80a47a2c | 1787 | } |
80a47a2c | 1788 | |
3045f050 JH |
1789 | /* VERIFICATION ----------------------------------------------------------- */ |
1790 | else | |
1791 | sig_hdr = strdup(sig->rawsig_no_b_val); | |
1792 | /* ------------------------------------------------------------------------ */ | |
1793 | ||
1794 | if (!sig_hdr) | |
1795 | return PDKIM_ERR_OOM; | |
80a47a2c | 1796 | |
3045f050 JH |
1797 | /* Relax header if necessary */ |
1798 | if (sig->canon_headers == PDKIM_CANON_RELAXED) | |
1799 | { | |
1800 | char *relaxed_hdr = pdkim_relax_header(sig_hdr, 0); | |
1801 | ||
1802 | free(sig_hdr); | |
1803 | if (!relaxed_hdr) | |
1804 | return PDKIM_ERR_OOM; | |
1805 | sig_hdr = relaxed_hdr; | |
80a47a2c TK |
1806 | } |
1807 | ||
3045f050 JH |
1808 | #ifdef PDKIM_DEBUG |
1809 | if (ctx->debug_stream) | |
1810 | { | |
1811 | fprintf(ctx->debug_stream, | |
1812 | "PDKIM >> Signed DKIM-Signature header, canonicalized >>>>>>>>>>>>>>>>>\n"); | |
1813 | pdkim_quoteprint(ctx->debug_stream, sig_hdr, strlen(sig_hdr), 1); | |
1814 | fprintf(ctx->debug_stream, | |
1815 | "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"); | |
80a47a2c | 1816 | } |
3045f050 JH |
1817 | #endif |
1818 | ||
1819 | /* Finalize header hash */ | |
1820 | if (sig->algo == PDKIM_ALGO_RSA_SHA1) | |
1821 | { | |
1822 | sha1_update(&(sha1_headers), (unsigned char *)sig_hdr, strlen(sig_hdr)); | |
1823 | sha1_finish(&(sha1_headers), (unsigned char *)headerhash); | |
1824 | ||
1825 | #ifdef PDKIM_DEBUG | |
1826 | if (ctx->debug_stream) | |
1827 | { | |
1828 | fprintf(ctx->debug_stream, "PDKIM [%s] hh computed: ", sig->domain); | |
1829 | pdkim_hexprint(ctx->debug_stream, headerhash, 20, 1); | |
80a47a2c | 1830 | } |
3045f050 | 1831 | #endif |
80a47a2c | 1832 | } |
3045f050 JH |
1833 | else |
1834 | { | |
1835 | sha2_update(&(sha2_headers), (unsigned char *)sig_hdr, strlen(sig_hdr)); | |
1836 | sha2_finish(&(sha2_headers), (unsigned char *)headerhash); | |
1837 | ||
1838 | #ifdef PDKIM_DEBUG | |
1839 | if (ctx->debug_stream) | |
1840 | { | |
1841 | fprintf(ctx->debug_stream, "PDKIM [%s] hh computed: ", sig->domain); | |
1842 | pdkim_hexprint(ctx->debug_stream, headerhash, 32, 1); | |
80a47a2c | 1843 | } |
3045f050 | 1844 | #endif |
80a47a2c TK |
1845 | } |
1846 | ||
3045f050 | 1847 | free(sig_hdr); |
80a47a2c | 1848 | |
3045f050 JH |
1849 | /* SIGNING ---------------------------------------------------------------- */ |
1850 | if (ctx->mode == PDKIM_MODE_SIGN) | |
1851 | { | |
1852 | rsa_context rsa; | |
80a47a2c | 1853 | |
3045f050 | 1854 | rsa_init(&rsa, RSA_PKCS_V15, 0); |
80a47a2c | 1855 | |
3045f050 JH |
1856 | /* Perform private key operation */ |
1857 | if (rsa_parse_key(&rsa, (unsigned char *)sig->rsa_privkey, | |
1858 | strlen(sig->rsa_privkey), NULL, 0) != 0) | |
1859 | return PDKIM_ERR_RSA_PRIVKEY; | |
80a47a2c | 1860 | |
3045f050 JH |
1861 | sig->sigdata_len = mpi_size(&(rsa.N)); |
1862 | if (!(sig->sigdata = malloc(sig->sigdata_len))) | |
1863 | return PDKIM_ERR_OOM; | |
1864 | ||
1865 | if (rsa_pkcs1_sign( &rsa, RSA_PRIVATE, | |
1866 | ((sig->algo == PDKIM_ALGO_RSA_SHA1)? | |
1867 | SIG_RSA_SHA1:SIG_RSA_SHA256), | |
1868 | 0, | |
1869 | (unsigned char *)headerhash, | |
1870 | (unsigned char *)sig->sigdata ) != 0) | |
1871 | return PDKIM_ERR_RSA_SIGNING; | |
80a47a2c | 1872 | |
3045f050 | 1873 | rsa_free(&rsa); |
80a47a2c | 1874 | |
3045f050 JH |
1875 | #ifdef PDKIM_DEBUG |
1876 | if (ctx->debug_stream) | |
1877 | { | |
1878 | fprintf(ctx->debug_stream, "PDKIM [%s] b computed: ", sig->domain); | |
1879 | pdkim_hexprint(ctx->debug_stream, sig->sigdata, sig->sigdata_len, 1); | |
80a47a2c | 1880 | } |
3045f050 | 1881 | #endif |
80a47a2c | 1882 | |
3045f050 JH |
1883 | if (!(sig->signature_header = pdkim_create_header(ctx->sig,1))) |
1884 | return PDKIM_ERR_OOM; | |
80a47a2c | 1885 | } |
80a47a2c | 1886 | |
3045f050 JH |
1887 | /* VERIFICATION ----------------------------------------------------------- */ |
1888 | else | |
1889 | { | |
1890 | rsa_context rsa; | |
1891 | char *dns_txt_name, *dns_txt_reply; | |
1892 | ||
1893 | rsa_init(&rsa, RSA_PKCS_V15, 0); | |
1894 | ||
1895 | if (!(dns_txt_name = malloc(PDKIM_DNS_TXT_MAX_NAMELEN))) | |
1896 | return PDKIM_ERR_OOM; | |
1897 | ||
1898 | if (!(dns_txt_reply = malloc(PDKIM_DNS_TXT_MAX_RECLEN))) | |
1899 | { | |
1900 | free(dns_txt_name); | |
1901 | return PDKIM_ERR_OOM; | |
80a47a2c TK |
1902 | } |
1903 | ||
3045f050 JH |
1904 | memset(dns_txt_reply, 0, PDKIM_DNS_TXT_MAX_RECLEN); |
1905 | memset(dns_txt_name , 0, PDKIM_DNS_TXT_MAX_NAMELEN); | |
1906 | ||
1907 | if (snprintf(dns_txt_name,PDKIM_DNS_TXT_MAX_NAMELEN, | |
1908 | "%s._domainkey.%s.", | |
1909 | sig->selector,sig->domain) >= PDKIM_DNS_TXT_MAX_NAMELEN) | |
1910 | { | |
1911 | sig->verify_status = PDKIM_VERIFY_INVALID; | |
1912 | sig->verify_ext_status = PDKIM_VERIFY_INVALID_BUFFER_SIZE; | |
1913 | goto NEXT_VERIFY; | |
80a47a2c | 1914 | } |
3045f050 JH |
1915 | |
1916 | if ( ctx->dns_txt_callback(dns_txt_name, dns_txt_reply) != PDKIM_OK | |
1917 | || dns_txt_reply[0] == '\0') | |
1918 | { | |
1919 | sig->verify_status = PDKIM_VERIFY_INVALID; | |
1920 | sig->verify_ext_status = PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE; | |
1921 | goto NEXT_VERIFY; | |
80a47a2c TK |
1922 | } |
1923 | ||
3045f050 JH |
1924 | #ifdef PDKIM_DEBUG |
1925 | if (ctx->debug_stream) | |
1926 | { | |
1927 | fprintf(ctx->debug_stream, | |
1928 | "PDKIM >> Parsing public key record >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"); | |
1929 | fprintf(ctx->debug_stream," Raw record: "); | |
1930 | pdkim_quoteprint(ctx->debug_stream, dns_txt_reply, strlen(dns_txt_reply), 1); | |
80a47a2c | 1931 | } |
3045f050 JH |
1932 | #endif |
1933 | ||
1934 | if (!(sig->pubkey = pdkim_parse_pubkey_record(ctx,dns_txt_reply))) | |
1935 | { | |
1936 | sig->verify_status = PDKIM_VERIFY_INVALID; | |
1937 | sig->verify_ext_status = PDKIM_VERIFY_INVALID_PUBKEY_PARSING; | |
1938 | ||
1939 | #ifdef PDKIM_DEBUG | |
1940 | if (ctx->debug_stream) | |
1941 | { | |
1942 | fprintf(ctx->debug_stream," Error while parsing public key record\n"); | |
1943 | fprintf(ctx->debug_stream, | |
1944 | "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"); | |
1945 | } | |
1946 | #endif | |
1947 | goto NEXT_VERIFY; | |
80a47a2c TK |
1948 | } |
1949 | ||
3045f050 JH |
1950 | #ifdef PDKIM_DEBUG |
1951 | if (ctx->debug_stream) | |
1952 | fprintf(ctx->debug_stream, | |
1953 | "PDKIM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"); | |
1954 | #endif | |
1955 | ||
1956 | if (rsa_parse_public_key(&rsa, | |
1957 | (unsigned char *)sig->pubkey->key, | |
1958 | sig->pubkey->key_len) != 0) | |
1959 | { | |
1960 | sig->verify_status = PDKIM_VERIFY_INVALID; | |
1961 | sig->verify_ext_status = PDKIM_VERIFY_INVALID_PUBKEY_PARSING; | |
1962 | goto NEXT_VERIFY; | |
80a47a2c TK |
1963 | } |
1964 | ||
3045f050 JH |
1965 | /* Check the signature */ |
1966 | if (rsa_pkcs1_verify(&rsa, | |
1967 | RSA_PUBLIC, | |
1968 | ((sig->algo == PDKIM_ALGO_RSA_SHA1)? | |
1969 | SIG_RSA_SHA1:SIG_RSA_SHA256), | |
1970 | 0, | |
1971 | (unsigned char *)headerhash, | |
1972 | (unsigned char *)sig->sigdata) != 0) | |
1973 | { | |
1974 | sig->verify_status = PDKIM_VERIFY_FAIL; | |
1975 | sig->verify_ext_status = PDKIM_VERIFY_FAIL_MESSAGE; | |
1976 | goto NEXT_VERIFY; | |
ff7ddfd7 TK |
1977 | } |
1978 | ||
3045f050 JH |
1979 | /* We have a winner! (if bodydhash was correct earlier) */ |
1980 | if (sig->verify_status == PDKIM_VERIFY_NONE) | |
1981 | sig->verify_status = PDKIM_VERIFY_PASS; | |
1982 | ||
1983 | NEXT_VERIFY: | |
1984 | ||
1985 | #ifdef PDKIM_DEBUG | |
1986 | if (ctx->debug_stream) | |
1987 | { | |
1988 | fprintf(ctx->debug_stream, "PDKIM [%s] signature status: %s", | |
1989 | sig->domain, pdkim_verify_status_str(sig->verify_status)); | |
1990 | if (sig->verify_ext_status > 0) | |
1991 | fprintf(ctx->debug_stream, " (%s)\n", | |
1992 | pdkim_verify_ext_status_str(sig->verify_ext_status)); | |
1993 | else | |
1994 | fprintf(ctx->debug_stream, "\n"); | |
80a47a2c | 1995 | } |
3045f050 | 1996 | #endif |
80a47a2c | 1997 | |
3045f050 JH |
1998 | rsa_free(&rsa); |
1999 | free(dns_txt_name); | |
2000 | free(dns_txt_reply); | |
80a47a2c TK |
2001 | } |
2002 | ||
3045f050 | 2003 | sig = sig->next; |
80a47a2c TK |
2004 | } |
2005 | ||
3045f050 JH |
2006 | /* If requested, set return pointer to signature(s) */ |
2007 | if (return_signatures) | |
2008 | *return_signatures = ctx->sig; | |
80a47a2c | 2009 | |
3045f050 | 2010 | return PDKIM_OK; |
80a47a2c TK |
2011 | } |
2012 | ||
2013 | ||
2014 | /* -------------------------------------------------------------------------- */ | |
3045f050 JH |
2015 | |
2016 | DLLEXPORT pdkim_ctx * | |
2017 | pdkim_init_verify(int input_mode, int(*dns_txt_callback)(char *, char *)) | |
2018 | { | |
2019 | pdkim_ctx *ctx = malloc(sizeof(pdkim_ctx)); | |
2020 | ||
2021 | if (!ctx) | |
2022 | return NULL; | |
2023 | memset(ctx,0,sizeof(pdkim_ctx)); | |
2024 | ||
2025 | if (!(ctx->linebuf = malloc(PDKIM_MAX_BODY_LINE_LEN))) | |
2026 | { | |
2027 | free(ctx); | |
2028 | return NULL; | |
80a47a2c TK |
2029 | } |
2030 | ||
3045f050 JH |
2031 | ctx->mode = PDKIM_MODE_VERIFY; |
2032 | ctx->input_mode = input_mode; | |
2033 | ctx->dns_txt_callback = dns_txt_callback; | |
80a47a2c | 2034 | |
3045f050 | 2035 | return ctx; |
80a47a2c TK |
2036 | } |
2037 | ||
2038 | ||
2039 | /* -------------------------------------------------------------------------- */ | |
80a47a2c | 2040 | |
3045f050 JH |
2041 | DLLEXPORT pdkim_ctx * |
2042 | pdkim_init_sign(int input_mode, char *domain, char *selector, char *rsa_privkey) | |
2043 | { | |
2044 | pdkim_ctx *ctx; | |
2045 | pdkim_signature *sig; | |
80a47a2c | 2046 | |
3045f050 JH |
2047 | if (!domain || !selector || !rsa_privkey) |
2048 | return NULL; | |
80a47a2c | 2049 | |
3045f050 JH |
2050 | if (!(ctx = malloc(sizeof(pdkim_ctx)))) |
2051 | return NULL; | |
2052 | memset(ctx,0,sizeof(pdkim_ctx)); | |
80a47a2c | 2053 | |
3045f050 JH |
2054 | if (!(ctx->linebuf = malloc(PDKIM_MAX_BODY_LINE_LEN))) |
2055 | { | |
2056 | free(ctx); | |
2057 | return NULL; | |
80a47a2c TK |
2058 | } |
2059 | ||
3045f050 JH |
2060 | if (!(sig = malloc(sizeof(pdkim_signature)))) |
2061 | { | |
2062 | free(ctx->linebuf); | |
2063 | free(ctx); | |
2064 | return NULL; | |
80a47a2c | 2065 | } |
3045f050 | 2066 | memset(sig,0,sizeof(pdkim_signature)); |
80a47a2c | 2067 | |
3045f050 | 2068 | sig->bodylength = -1; |
80a47a2c | 2069 | |
3045f050 JH |
2070 | ctx->mode = PDKIM_MODE_SIGN; |
2071 | ctx->input_mode = input_mode; | |
2072 | ctx->sig = sig; | |
80a47a2c | 2073 | |
3045f050 JH |
2074 | ctx->sig->domain = strdup(domain); |
2075 | ctx->sig->selector = strdup(selector); | |
2076 | ctx->sig->rsa_privkey = strdup(rsa_privkey); | |
2077 | ||
2078 | if (!ctx->sig->domain || !ctx->sig->selector || !ctx->sig->rsa_privkey) | |
2079 | goto BAIL; | |
2080 | ||
2081 | if (!(ctx->sig->sha1_body = malloc(sizeof(sha1_context)))) | |
2082 | goto BAIL; | |
2083 | sha1_starts(ctx->sig->sha1_body); | |
2084 | ||
2085 | if (!(ctx->sig->sha2_body = malloc(sizeof(sha2_context)))) | |
2086 | goto BAIL; | |
2087 | sha2_starts(ctx->sig->sha2_body,0); | |
2088 | ||
2089 | return ctx; | |
2090 | ||
2091 | BAIL: | |
2092 | pdkim_free_ctx(ctx); | |
2093 | return NULL; | |
6ab02e3f | 2094 | } |
80a47a2c TK |
2095 | |
2096 | /* -------------------------------------------------------------------------- */ | |
3045f050 JH |
2097 | |
2098 | DLLEXPORT int | |
2099 | pdkim_set_optional(pdkim_ctx *ctx, | |
80a47a2c TK |
2100 | char *sign_headers, |
2101 | char *identity, | |
2102 | int canon_headers, | |
2103 | int canon_body, | |
2104 | long bodylength, | |
2105 | int algo, | |
2106 | unsigned long created, | |
3045f050 JH |
2107 | unsigned long expires) |
2108 | { | |
80a47a2c | 2109 | |
3045f050 JH |
2110 | if (identity) |
2111 | if (!(ctx->sig->identity = strdup(identity))) | |
2112 | return PDKIM_ERR_OOM; | |
80a47a2c | 2113 | |
3045f050 JH |
2114 | if (sign_headers) |
2115 | if (!(ctx->sig->sign_headers = strdup(sign_headers))) | |
2116 | return PDKIM_ERR_OOM; | |
80a47a2c | 2117 | |
3045f050 JH |
2118 | ctx->sig->canon_headers = canon_headers; |
2119 | ctx->sig->canon_body = canon_body; | |
2120 | ctx->sig->bodylength = bodylength; | |
2121 | ctx->sig->algo = algo; | |
2122 | ctx->sig->created = created; | |
2123 | ctx->sig->expires = expires; | |
80a47a2c | 2124 | |
3045f050 | 2125 | return PDKIM_OK; |
6ab02e3f | 2126 | } |
3045f050 JH |
2127 | |
2128 | /* -------------------------------------------------------------------------- */ | |
2129 | ||
2130 | #ifdef PDKIM_DEBUG | |
2131 | DLLEXPORT void | |
2132 | pdkim_set_debug_stream(pdkim_ctx *ctx, FILE *debug_stream) | |
2133 | { | |
2134 | ctx->debug_stream = debug_stream; | |
2135 | } | |
2136 | #endif | |
2137 |