X-Git-Url: https://git.exim.org/exim.git/blobdiff_plain/80a47a2c9633437d4ceebd214cd44abfbd4f4543..6ab02e3ff7ada14165079ae4d2213e13d32bfad6:/src/src/pdkim/pdkim.c diff --git a/src/src/pdkim/pdkim.c b/src/src/pdkim/pdkim.c index e269f7796..d875e1f89 100644 --- a/src/src/pdkim/pdkim.c +++ b/src/src/pdkim/pdkim.c @@ -20,7 +20,7 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -/* $Cambridge: exim/src/src/pdkim/pdkim.c,v 1.2 2009/06/10 07:34:05 tom Exp $ */ +/* $Cambridge: exim/src/src/pdkim/pdkim.c,v 1.7 2009/11/09 14:19:48 tom Exp $ */ #include #include @@ -156,12 +156,23 @@ pdkim_stringlist *pdkim_append_stringlist(pdkim_stringlist *base, char *str) { if (new_entry->value == NULL) return NULL; if (base != NULL) { pdkim_stringlist *last = base; - while (last->next != NULL) { last = last->next; }; + while (last->next != NULL) { last = last->next; } last->next = new_entry; return base; } else return new_entry; -}; +} +pdkim_stringlist *pdkim_prepend_stringlist(pdkim_stringlist *base, char *str) { + pdkim_stringlist *new_entry = malloc(sizeof(pdkim_stringlist)); + if (new_entry == NULL) return NULL; + memset(new_entry,0,sizeof(pdkim_stringlist)); + new_entry->value = strdup(str); + if (new_entry->value == NULL) return NULL; + if (base != NULL) { + new_entry->next = base; + } + return new_entry; +} /* -------------------------------------------------------------------------- */ @@ -180,7 +191,7 @@ pdkim_str *pdkim_strnew (char *cstr) { p->len=len; if (cstr) strcpy(p->str,cstr); return p; -}; +} char *pdkim_strncat(pdkim_str *str, char *data, int len) { if ((str->allocated - str->len) < (len+1)) { /* Extend the buffer */ @@ -195,20 +206,20 @@ char *pdkim_strncat(pdkim_str *str, char *data, int len) { str->len+=len; str->str[str->len] = '\0'; return str->str; -}; +} char *pdkim_strcat(pdkim_str *str, char *cstr) { return pdkim_strncat(str, cstr, strlen(cstr)); -}; +} char *pdkim_numcat(pdkim_str *str, unsigned long num) { char minibuf[20]; snprintf(minibuf,20,"%lu",num); return pdkim_strcat(str,minibuf); -}; +} char *pdkim_strtrim(pdkim_str *str) { char *p = str->str; char *q = str->str; while ( (*p != '\0') && ((*p == '\t') || (*p == ' ')) ) p++; - while (*p != '\0') {*q = *p; q++; p++;}; + while (*p != '\0') {*q = *p; q++; p++;} *q = '\0'; while ( (q != str->str) && ( (*q == '\0') || (*q == '\t') || (*q == ' ') ) ) { *q = '\0'; @@ -216,17 +227,17 @@ char *pdkim_strtrim(pdkim_str *str) { } str->len = strlen(str->str); return str->str; -}; +} char *pdkim_strclear(pdkim_str *str) { str->str[0] = '\0'; str->len = 0; return str->str; -}; +} void pdkim_strfree(pdkim_str *str) { if (str == NULL) return; if (str->str != NULL) free(str->str); free(str); -}; +} @@ -277,7 +288,7 @@ void pdkim_free_sig(pdkim_signature *sig) { free(sig); if (next != NULL) pdkim_free_sig(next); } -}; +} /* -------------------------------------------------------------------------- */ @@ -287,7 +298,7 @@ DLLEXPORT void pdkim_free_ctx(pdkim_ctx *ctx) { pdkim_strfree(ctx->cur_header); free(ctx); } -}; +} /* -------------------------------------------------------------------------- */ @@ -355,7 +366,7 @@ char *pdkim_relax_header (char *header, int crlf) { int seen_wsp = 0; char *p = header; char *q; - char *relaxed = malloc(strlen(header)); + char *relaxed = malloc(strlen(header)+3); if (relaxed == NULL) return NULL; q = relaxed; while (*p != '\0') { @@ -390,7 +401,7 @@ char *pdkim_relax_header (char *header, int crlf) { *q = '\0'; if (crlf) strcat(relaxed,"\r\n"); return relaxed; -}; +} /* -------------------------------------------------------------------------- */ @@ -405,12 +416,12 @@ char *pdkim_decode_qp_char(char *qp_p, int *c) { if (isxdigit(*qp_p) && isxdigit(qp_p[1])) { /* Do hex conversion */ if (isdigit(*qp_p)) {*c = *qp_p - '0';} - else {*c = toupper(*qp_p) - 'A' + 10;}; + else {*c = toupper(*qp_p) - 'A' + 10;} *c <<= 4; if (isdigit(qp_p[1])) {*c |= qp_p[1] - '0';} - else {*c |= toupper(qp_p[1]) - 'A' + 10;}; + else {*c |= toupper(qp_p[1]) - 'A' + 10;} return qp_p + 2; - }; + } /* Illegal char here */ *c = PDKIM_QP_ERROR_DECODE; @@ -853,7 +864,10 @@ int pdkim_update_bodyhash(pdkim_ctx *ctx, char *data, int len) { if (relaxed_data == NULL) return PDKIM_ERR_OOM; while (*p != '\0') { char c = *p; - if ( (c == '\t') || (c == ' ') ) { + if (c == '\r') { + if ( (q > 0) && (relaxed_data[q-1] == ' ') ) q--; + } + else if ( (c == '\t') || (c == ' ') ) { c = ' '; /* Turns WSP into SP */ if (seen_wsp) { p++; @@ -894,7 +908,7 @@ int pdkim_update_bodyhash(pdkim_ctx *ctx, char *data, int len) { if (relaxed_data != NULL) free(relaxed_data); return PDKIM_OK; -}; +} /* -------------------------------------------------------------------------- */ @@ -962,7 +976,7 @@ int pdkim_finish_bodyhash(pdkim_ctx *ctx) { } return PDKIM_OK; -}; +} @@ -1045,9 +1059,9 @@ int pdkim_header_complete(pdkim_ctx *ctx) { sig->hnames_check, 1) != PDKIM_OK) goto NEXT_SIG; } - /* Add header to the signed headers list */ - list = pdkim_append_stringlist(sig->headers, - ctx->cur_header->str); + /* Add header to the signed headers list (in reverse order) */ + list = pdkim_prepend_stringlist(sig->headers, + ctx->cur_header->str); if (list == NULL) return PDKIM_ERR_OOM; sig->headers = list; @@ -1074,7 +1088,7 @@ int pdkim_header_complete(pdkim_ctx *ctx) { ctx->sig = new_sig; } else { - while (last_sig->next != NULL) { last_sig = last_sig->next; }; + while (last_sig->next != NULL) { last_sig = last_sig->next; } last_sig->next = new_sig; } } @@ -1092,7 +1106,7 @@ int pdkim_header_complete(pdkim_ctx *ctx) { BAIL: pdkim_strclear(ctx->cur_header); /* Re-use existing pdkim_str */ return PDKIM_OK; -}; +} @@ -1150,7 +1164,7 @@ DLLEXPORT int pdkim_feed (pdkim_ctx *ctx, } } return PDKIM_OK; -}; +} /* -------------------------------------------------------------------------- */ @@ -1347,7 +1361,8 @@ DLLEXPORT int pdkim_feed_finish(pdkim_ctx *ctx, pdkim_signature **return_signatu q = strchr(p,':'); if (q != NULL) *q = '\0'; while (hdrs != NULL) { - if (strncasecmp(hdrs->value,p,strlen(p)) == 0) { + if ( (strncasecmp(hdrs->value,p,strlen(p)) == 0) && + ((hdrs->value)[strlen(p)] == ':') ) { char *rh = NULL; if (sig->canon_headers == PDKIM_CANON_RELAXED) rh = pdkim_relax_header(hdrs->value,1); /* cook header for relaxed canon */ @@ -1364,6 +1379,8 @@ DLLEXPORT int pdkim_feed_finish(pdkim_ctx *ctx, pdkim_signature **return_signatu pdkim_quoteprint(ctx->debug_stream, rh, strlen(rh), 1); #endif free(rh); + (hdrs->value)[0] = '_'; + break; } hdrs = hdrs->next; } @@ -1499,7 +1516,7 @@ DLLEXPORT int pdkim_feed_finish(pdkim_ctx *ctx, pdkim_signature **return_signatu sig->verify_status = PDKIM_VERIFY_INVALID; sig->verify_ext_status = PDKIM_VERIFY_INVALID_BUFFER_SIZE; goto NEXT_VERIFY; - }; + } if ((ctx->dns_txt_callback(dns_txt_name, dns_txt_reply) != PDKIM_OK) || (dns_txt_reply[0] == '\0')) { @@ -1672,14 +1689,14 @@ DLLEXPORT pdkim_ctx *pdkim_init_sign(int input_mode, sha2_starts(ctx->sig->sha2_body,0); return ctx; -}; +} #ifdef PDKIM_DEBUG /* -------------------------------------------------------------------------- */ DLLEXPORT void pdkim_set_debug_stream(pdkim_ctx *ctx, FILE *debug_stream) { ctx->debug_stream = debug_stream; -}; +} #endif /* -------------------------------------------------------------------------- */ @@ -1711,4 +1728,4 @@ DLLEXPORT int pdkim_set_optional(pdkim_ctx *ctx, ctx->sig->expires = expires; return PDKIM_OK; -}; +}