/*- * Copyright 2016 Vsevolod Stakhov * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "config.h" #include "rspamd.h" #include "message.h" #include "dkim.h" #include "dns.h" #include "utlist.h" #include "unix-std.h" #include "mempool_vars_internal.h" #include <openssl/evp.h> #include <openssl/rsa.h> #include <openssl/engine.h> /* special DNS tokens */ #define DKIM_DNSKEYNAME "_domainkey" /* ed25519 key lengths */ #define ED25519_B64_BYTES 45 #define ED25519_BYTES 32 /* Canonization methods */ #define DKIM_CANON_UNKNOWN (-1) /* unknown method */ #define DKIM_CANON_SIMPLE 0 /* as specified in DKIM spec */ #define DKIM_CANON_RELAXED 1 /* as specified in DKIM spec */ #define DKIM_CANON_DEFAULT DKIM_CANON_SIMPLE #define RSPAMD_SHORT_BH_LEN 8 /* Params */ enum rspamd_dkim_param_type { DKIM_PARAM_UNKNOWN = -1, DKIM_PARAM_SIGNATURE = 0, DKIM_PARAM_SIGNALG, DKIM_PARAM_DOMAIN, DKIM_PARAM_CANONALG, DKIM_PARAM_QUERYMETHOD, DKIM_PARAM_SELECTOR, DKIM_PARAM_HDRLIST, DKIM_PARAM_VERSION, DKIM_PARAM_IDENTITY, DKIM_PARAM_TIMESTAMP, DKIM_PARAM_EXPIRATION, DKIM_PARAM_COPIEDHDRS, DKIM_PARAM_BODYHASH, DKIM_PARAM_BODYLENGTH, DKIM_PARAM_IDX, DKIM_PARAM_CV, DKIM_PARAM_IGNORE }; #define RSPAMD_DKIM_MAX_ARC_IDX 10 #define msg_err_dkim(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \ "dkim", ctx->pool->tag.uid, \ G_STRFUNC, \ __VA_ARGS__) #define msg_warn_dkim(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \ "dkim", ctx->pool->tag.uid, \ G_STRFUNC, \ __VA_ARGS__) #define msg_info_dkim(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \ "dkim", ctx->pool->tag.uid, \ G_STRFUNC, \ __VA_ARGS__) #define msg_debug_dkim(...) rspamd_conditional_debug_fast (NULL, NULL, \ rspamd_dkim_log_id, "dkim", ctx->pool->tag.uid, \ G_STRFUNC, \ __VA_ARGS__) #define msg_debug_dkim_taskless(...) rspamd_conditional_debug_fast (NULL, NULL, \ rspamd_dkim_log_id, "dkim", "", \ G_STRFUNC, \ __VA_ARGS__) INIT_LOG_MODULE(dkim) #define RSPAMD_DKIM_FLAG_OVERSIGN (1u << 0u) #define RSPAMD_DKIM_FLAG_OVERSIGN_EXISTING (1u << 1u) union rspamd_dkim_header_stat { struct _st { guint16 count; guint16 flags; } s; guint32 n; }; struct rspamd_dkim_common_ctx { rspamd_mempool_t *pool; guint64 sig_hash; gsize len; GPtrArray *hlist; GHashTable *htable; /* header -> count mapping */ EVP_MD_CTX *headers_hash; EVP_MD_CTX *body_hash; enum rspamd_dkim_type type; guint idx; gint header_canon_type; gint body_canon_type; guint body_canonicalised; guint headers_canonicalised; gboolean is_sign; }; enum rspamd_arc_seal_cv { RSPAMD_ARC_UNKNOWN = 0, RSPAMD_ARC_NONE, RSPAMD_ARC_INVALID, RSPAMD_ARC_FAIL, RSPAMD_ARC_PASS }; struct rspamd_dkim_context_s { struct rspamd_dkim_common_ctx common; rspamd_mempool_t *pool; struct rspamd_dns_resolver *resolver; gsize blen; gsize bhlen; gint sig_alg; guint ver; time_t timestamp; time_t expiration; gchar *domain; gchar *selector; gint8 *b; gchar *short_b; gint8 *bh; gchar *dns_key; enum rspamd_arc_seal_cv cv; const gchar *dkim_header; }; #define RSPAMD_DKIM_KEY_ID_LEN 16 struct rspamd_dkim_key_s { guint8 *keydata; gsize keylen; gsize decoded_len; gchar key_id[RSPAMD_DKIM_KEY_ID_LEN]; union { RSA *key_rsa; EC_KEY *key_ecdsa; guchar *key_eddsa; } key; BIO *key_bio; EVP_PKEY *key_evp; time_t mtime; guint ttl; enum rspamd_dkim_key_type type; ref_entry_t ref; }; struct rspamd_dkim_sign_context_s { struct rspamd_dkim_common_ctx common; rspamd_dkim_sign_key_t *key; }; struct rspamd_dkim_header { const gchar *name; gint count; }; /* Parser of dkim params */ typedef gboolean (*dkim_parse_param_f) (rspamd_dkim_context_t * ctx, const gchar *param, gsize len, GError **err); static gboolean rspamd_dkim_parse_signature (rspamd_dkim_context_t * ctx, const gchar *param, gsize len, GError **err); static gboolean rspamd_dkim_parse_signalg (rspamd_dkim_context_t * ctx, const gchar *param, gsize len, GError **err); static gboolean rspamd_dkim_parse_domain (rspamd_dkim_context_t * ctx, const gchar *param, gsize len, GError **err); static gboolean rspamd_dkim_parse_canonalg (rspamd_dkim_context_t * ctx, const gchar *param, gsize len, GError **err); static gboolean rspamd_dkim_parse_ignore (rspamd_dkim_context_t * ctx, const gchar *param, gsize len, GError **err); static gboolean rspamd_dkim_parse_selector (rspamd_dkim_context_t * ctx, const gchar *param, gsize len, GError **err); static gboolean rspamd_dkim_parse_hdrlist (rspamd_dkim_context_t * ctx, const gchar *param, gsize len, GError **err); static gboolean rspamd_dkim_parse_version (rspamd_dkim_context_t * ctx, const gchar *param, gsize len, GError **err); static gboolean rspamd_dkim_parse_timestamp (rspamd_dkim_context_t * ctx, const gchar *param, gsize len, GError **err); static gboolean rspamd_dkim_parse_expiration (rspamd_dkim_context_t * ctx, const gchar *param, gsize len, GError **err); static gboolean rspamd_dkim_parse_bodyhash (rspamd_dkim_context_t * ctx, const gchar *param, gsize len, GError **err); static gboolean rspamd_dkim_parse_bodylength (rspamd_dkim_context_t * ctx, const gchar *param, gsize len, GError **err); static gboolean rspamd_dkim_parse_idx (rspamd_dkim_context_t * ctx, const gchar *param, gsize len, GError **err); static gboolean rspamd_dkim_parse_cv (rspamd_dkim_context_t * ctx, const gchar *param, gsize len, GError **err); static const dkim_parse_param_f parser_funcs[] = { [DKIM_PARAM_SIGNATURE] = rspamd_dkim_parse_signature, [DKIM_PARAM_SIGNALG] = rspamd_dkim_parse_signalg, [DKIM_PARAM_DOMAIN] = rspamd_dkim_parse_domain, [DKIM_PARAM_CANONALG] = rspamd_dkim_parse_canonalg, [DKIM_PARAM_QUERYMETHOD] = rspamd_dkim_parse_ignore, [DKIM_PARAM_SELECTOR] = rspamd_dkim_parse_selector, [DKIM_PARAM_HDRLIST] = rspamd_dkim_parse_hdrlist, [DKIM_PARAM_VERSION] = rspamd_dkim_parse_version, [DKIM_PARAM_IDENTITY] = rspamd_dkim_parse_ignore, [DKIM_PARAM_TIMESTAMP] = rspamd_dkim_parse_timestamp, [DKIM_PARAM_EXPIRATION] = rspamd_dkim_parse_expiration, [DKIM_PARAM_COPIEDHDRS] = rspamd_dkim_parse_ignore, [DKIM_PARAM_BODYHASH] = rspamd_dkim_parse_bodyhash, [DKIM_PARAM_BODYLENGTH] = rspamd_dkim_parse_bodylength, [DKIM_PARAM_IDX] = rspamd_dkim_parse_idx, [DKIM_PARAM_CV] = rspamd_dkim_parse_cv, [DKIM_PARAM_IGNORE] = rspamd_dkim_parse_ignore, }; #define DKIM_ERROR dkim_error_quark () GQuark dkim_error_quark (void) { return g_quark_from_static_string ("dkim-error-quark"); } /* Parsers implementation */ static gboolean rspamd_dkim_parse_signature (rspamd_dkim_context_t * ctx, const gchar *param, gsize len, GError **err) { ctx->b = rspamd_mempool_alloc0 (ctx->pool, len); ctx->short_b = rspamd_mempool_alloc0 (ctx->pool, RSPAMD_SHORT_BH_LEN + 1); rspamd_strlcpy (ctx->short_b, param, MIN (len, RSPAMD_SHORT_BH_LEN + 1)); (void)rspamd_cryptobox_base64_decode (param, len, ctx->b, &ctx->blen); return TRUE; } static gboolean rspamd_dkim_parse_signalg (rspamd_dkim_context_t * ctx, const gchar *param, gsize len, GError **err) { /* XXX: ugly size comparison, improve this code style some day */ if (len == 8) { if (memcmp (param, "rsa-sha1", len) == 0) { ctx->sig_alg = DKIM_SIGN_RSASHA1; return TRUE; } } else if (len == 10) { if (memcmp (param, "rsa-sha256", len) == 0) { ctx->sig_alg = DKIM_SIGN_RSASHA256; return TRUE; } else if (memcmp (param, "rsa-sha512", len) == 0) { ctx->sig_alg = DKIM_SIGN_RSASHA512; return TRUE; } } else if (len == 15) { if (memcmp (param, "ecdsa256-sha256", len) == 0) { ctx->sig_alg = DKIM_SIGN_ECDSASHA256; return TRUE; } else if (memcmp (param, "ecdsa256-sha512", len) == 0) { ctx->sig_alg = DKIM_SIGN_ECDSASHA512; return TRUE; } } else if (len == 14) { if (memcmp (param, "ed25519-sha256", len) == 0) { ctx->sig_alg = DKIM_SIGN_EDDSASHA256; return TRUE; } } g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_INVALID_A, "invalid dkim sign algorithm"); return FALSE; } static gboolean rspamd_dkim_parse_domain (rspamd_dkim_context_t * ctx, const gchar *param, gsize len, GError **err) { if (!rspamd_str_has_8bit (param, len)) { ctx->domain = rspamd_mempool_alloc (ctx->pool, len + 1); rspamd_strlcpy (ctx->domain, param, len + 1); } else { ctx->domain = rspamd_dns_resolver_idna_convert_utf8 (ctx->resolver, ctx->pool, param, len, NULL); if (!ctx->domain) { g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_INVALID_H, "invalid dkim domain tag %*.s: idna failed", (int)len, param); return FALSE; } } return TRUE; } static gboolean rspamd_dkim_parse_canonalg (rspamd_dkim_context_t * ctx, const gchar *param, gsize len, GError **err) { const gchar *p, *slash = NULL, *end = param + len; gsize sl = 0; p = param; while (p != end) { if (*p == '/') { slash = p; break; } p++; sl++; } if (slash == NULL) { /* Only check header */ if (len == 6 && memcmp (param, "simple", len) == 0) { ctx->common.header_canon_type = DKIM_CANON_SIMPLE; return TRUE; } else if (len == 7 && memcmp (param, "relaxed", len) == 0) { ctx->common.header_canon_type = DKIM_CANON_RELAXED; return TRUE; } } else { /* First check header */ if (sl == 6 && memcmp (param, "simple", sl) == 0) { ctx->common.header_canon_type = DKIM_CANON_SIMPLE; } else if (sl == 7 && memcmp (param, "relaxed", sl) == 0) { ctx->common.header_canon_type = DKIM_CANON_RELAXED; } else { goto err; } /* Check body */ len -= sl + 1; slash++; if (len == 6 && memcmp (slash, "simple", len) == 0) { ctx->common.body_canon_type = DKIM_CANON_SIMPLE; return TRUE; } else if (len == 7 && memcmp (slash, "relaxed", len) == 0) { ctx->common.body_canon_type = DKIM_CANON_RELAXED; return TRUE; } } err: g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_INVALID_A, "invalid dkim canonization algorithm"); return FALSE; } static gboolean rspamd_dkim_parse_ignore (rspamd_dkim_context_t * ctx, const gchar *param, gsize len, GError **err) { /* Just ignore unused params */ return TRUE; } static gboolean rspamd_dkim_parse_selector (rspamd_dkim_context_t * ctx, const gchar *param, gsize len, GError **err) { if (!rspamd_str_has_8bit (param, len)) { ctx->selector = rspamd_mempool_alloc (ctx->pool, len + 1); rspamd_strlcpy (ctx->selector, param, len + 1); } else { ctx->selector = rspamd_dns_resolver_idna_convert_utf8 (ctx->resolver, ctx->pool, param, len, NULL); if (!ctx->selector) { g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_INVALID_H, "invalid dkim selector tag %*.s: idna failed", (int)len, param); return FALSE; } } return TRUE; } static void rspamd_dkim_hlist_free (void *ud) { GPtrArray *a = ud; g_ptr_array_free (a, TRUE); } static gboolean rspamd_dkim_parse_hdrlist_common (struct rspamd_dkim_common_ctx *ctx, const gchar *param, gsize len, gboolean sign, GError **err) { const gchar *c, *p, *end = param + len; gchar *h; gboolean from_found = FALSE, oversign, existing; guint count = 0; struct rspamd_dkim_header *new; gpointer found; union rspamd_dkim_header_stat u; p = param; while (p <= end) { if ((p == end || *p == ':')) { count++; } p++; } if (count > 0) { ctx->hlist = g_ptr_array_sized_new (count); } else { return FALSE; } c = param; p = param; ctx->htable = g_hash_table_new (rspamd_strcase_hash, rspamd_strcase_equal); while (p <= end) { if ((p == end || *p == ':') && p - c > 0) { oversign = FALSE; existing = FALSE; h = rspamd_mempool_alloc (ctx->pool, p - c + 1); rspamd_strlcpy (h, c, p - c + 1); g_strstrip (h); if (sign) { if (rspamd_lc_cmp (h, "(o)", 3) == 0) { oversign = TRUE; h += 3; msg_debug_dkim ("oversign header: %s", h); } else if (rspamd_lc_cmp (h, "(x)", 3) == 0) { oversign = TRUE; existing = TRUE; h += 3; msg_debug_dkim ("oversign existing header: %s", h); } } /* Check mandatory from */ if (!from_found && g_ascii_strcasecmp (h, "from") == 0) { from_found = TRUE; } new = rspamd_mempool_alloc (ctx->pool, sizeof (struct rspamd_dkim_header)); new->name = h; new->count = 0; u.n = 0; g_ptr_array_add (ctx->hlist, new); found = g_hash_table_lookup (ctx->htable, h); if (oversign) { if (found) { msg_err_dkim ("specified oversigned header more than once: %s", h); } u.s.flags |= RSPAMD_DKIM_FLAG_OVERSIGN; if (existing) { u.s.flags |= RSPAMD_DKIM_FLAG_OVERSIGN_EXISTING; } u.s.count = 0; } else { if (found != NULL) { u.n = GPOINTER_TO_UINT (found); new->count = u.s.count; u.s.count ++; } else { /* Insert new header order to the list */ u.s.count = new->count + 1; } } g_hash_table_insert (ctx->htable, h, GUINT_TO_POINTER (u.n)); c = p + 1; p++; } else { p++; } } if (!ctx->hlist) { g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_INVALID_H, "invalid dkim header list"); return FALSE; } else { if (!from_found) { g_ptr_array_free (ctx->hlist, TRUE); g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_INVALID_H, "invalid dkim header list, from header is missing"); return FALSE; } rspamd_mempool_add_destructor (ctx->pool, (rspamd_mempool_destruct_t)rspamd_dkim_hlist_free, ctx->hlist); rspamd_mempool_add_destructor (ctx->pool, (rspamd_mempool_destruct_t)g_hash_table_unref, ctx->htable); } return TRUE; } static gboolean rspamd_dkim_parse_hdrlist (rspamd_dkim_context_t *ctx, const gchar *param, gsize len, GError **err) { return rspamd_dkim_parse_hdrlist_common (&ctx->common, param, len, FALSE, err); } static gboolean rspamd_dkim_parse_version (rspamd_dkim_context_t * ctx, const gchar *param, gsize len, GError **err) { if (len != 1 || *param != '1') { g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_VERSION, "invalid dkim version"); return FALSE; } ctx->ver = 1; return TRUE; } static gboolean rspamd_dkim_parse_timestamp (rspamd_dkim_context_t * ctx, const gchar *param, gsize len, GError **err) { gulong val; if (!rspamd_strtoul (param, len, &val)) { g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_UNKNOWN, "invalid dkim timestamp"); return FALSE; } ctx->timestamp = val; return TRUE; } static gboolean rspamd_dkim_parse_expiration (rspamd_dkim_context_t * ctx, const gchar *param, gsize len, GError **err) { gulong val; if (!rspamd_strtoul (param, len, &val)) { g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_UNKNOWN, "invalid dkim expiration"); return FALSE; } ctx->expiration = val; return TRUE; } static gboolean rspamd_dkim_parse_bodyhash (rspamd_dkim_context_t * ctx, const gchar *param, gsize len, GError **err) { ctx->bh = rspamd_mempool_alloc0 (ctx->pool, len); (void)rspamd_cryptobox_base64_decode (param, len, ctx->bh, &ctx->bhlen); return TRUE; } static gboolean rspamd_dkim_parse_bodylength (rspamd_dkim_context_t * ctx, const gchar *param, gsize len, GError **err) { gulong val; if (!rspamd_strtoul (param, len, &val)) { g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_INVALID_L, "invalid dkim body length"); return FALSE; } ctx->common.len = val; return TRUE; } static gboolean rspamd_dkim_parse_idx (rspamd_dkim_context_t * ctx, const gchar *param, gsize len, GError **err) { gulong val; if (!rspamd_strtoul (param, len, &val)) { g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_INVALID_L, "invalid ARC idx"); return FALSE; } ctx->common.idx = val; return TRUE; } static gboolean rspamd_dkim_parse_cv (rspamd_dkim_context_t * ctx, const gchar *param, gsize len, GError **err) { /* Only check header */ if (len == 4 && memcmp (param, "fail", len) == 0) { ctx->cv = RSPAMD_ARC_FAIL; return TRUE; } else if (len == 4 && memcmp (param, "pass", len) == 0) { ctx->cv = RSPAMD_ARC_PASS; return TRUE; } else if (len == 4 && memcmp (param, "none", len) == 0) { ctx->cv = RSPAMD_ARC_NONE; return TRUE; } else if (len == 7 && memcmp (param, "invalid", len) == 0) { ctx->cv = RSPAMD_ARC_INVALID; return TRUE; } g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_UNKNOWN, "invalid arc seal verification result"); return FALSE; } static void rspamd_dkim_add_arc_seal_headers (rspamd_mempool_t *pool, struct rspamd_dkim_common_ctx *ctx) { struct rspamd_dkim_header *hdr; gint count = ctx->idx, i; ctx->hlist = g_ptr_array_sized_new (count * 3 - 1); for (i = 0; i < count; i ++) { /* Authentication results */ hdr = rspamd_mempool_alloc (pool, sizeof (*hdr)); hdr->name = RSPAMD_DKIM_ARC_AUTHHEADER; hdr->count = -(i + 1); g_ptr_array_add (ctx->hlist, hdr); /* Arc signature */ hdr = rspamd_mempool_alloc (pool, sizeof (*hdr)); hdr->name = RSPAMD_DKIM_ARC_SIGNHEADER; hdr->count = -(i + 1); g_ptr_array_add (ctx->hlist, hdr); /* Arc seal (except last one) */ if (i != count - 1) { hdr = rspamd_mempool_alloc (pool, sizeof (*hdr)); hdr->name = RSPAMD_DKIM_ARC_SEALHEADER; hdr->count = -(i + 1); g_ptr_array_add (ctx->hlist, hdr); } } rspamd_mempool_add_destructor (ctx->pool, (rspamd_mempool_destruct_t)rspamd_dkim_hlist_free, ctx->hlist); } /** * Create new dkim context from signature * @param sig message's signature * @param pool pool to allocate memory from * @param err pointer to error object * @return new context or NULL */ rspamd_dkim_context_t * rspamd_create_dkim_context (const gchar *sig, rspamd_mempool_t *pool, struct rspamd_dns_resolver *resolver, guint time_jitter, enum rspamd_dkim_type type, GError **err) { const gchar *p, *c, *tag = NULL, *end; gsize taglen; gint param = DKIM_PARAM_UNKNOWN; const EVP_MD *md_alg; time_t now; rspamd_dkim_context_t *ctx; enum { DKIM_STATE_TAG = 0, DKIM_STATE_AFTER_TAG, DKIM_STATE_VALUE, DKIM_STATE_SKIP_SPACES = 99, DKIM_STATE_ERROR = 100 } state, next_state; if (sig == NULL) { g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_EMPTY_B, "empty signature"); return NULL; } ctx = rspamd_mempool_alloc0 (pool, sizeof (rspamd_dkim_context_t)); ctx->pool = pool; ctx->resolver = resolver; if (type == RSPAMD_DKIM_ARC_SEAL) { ctx->common.header_canon_type = DKIM_CANON_RELAXED; ctx->common.body_canon_type = DKIM_CANON_RELAXED; } else { ctx->common.header_canon_type = DKIM_CANON_DEFAULT; ctx->common.body_canon_type = DKIM_CANON_DEFAULT; } ctx->sig_alg = DKIM_SIGN_UNKNOWN; ctx->common.pool = pool; ctx->common.type = type; /* A simple state machine of parsing tags */ state = DKIM_STATE_SKIP_SPACES; next_state = DKIM_STATE_TAG; taglen = 0; p = sig; c = sig; end = p + strlen (p); ctx->common.sig_hash = rspamd_cryptobox_fast_hash (sig, end - sig, rspamd_hash_seed ()); msg_debug_dkim ("create dkim context sig = %L", ctx->common.sig_hash); while (p <= end) { switch (state) { case DKIM_STATE_TAG: if (g_ascii_isspace (*p)) { taglen = p - c; while (*p && g_ascii_isspace (*p)) { /* Skip spaces before '=' sign */ p++; } if (*p != '=') { g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_UNKNOWN, "invalid dkim param"); state = DKIM_STATE_ERROR; } else { state = DKIM_STATE_SKIP_SPACES; next_state = DKIM_STATE_AFTER_TAG; param = DKIM_PARAM_UNKNOWN; p++; tag = c; } } else if (*p == '=') { state = DKIM_STATE_SKIP_SPACES; next_state = DKIM_STATE_AFTER_TAG; param = DKIM_PARAM_UNKNOWN; p++; tag = c; } else { taglen++; p++; } break; case DKIM_STATE_AFTER_TAG: /* We got tag at tag and len at taglen */ switch (taglen) { case 0: g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_UNKNOWN, "zero length dkim param"); state = DKIM_STATE_ERROR; break; case 1: /* Simple tags */ switch (*tag) { case 'v': if (type == RSPAMD_DKIM_NORMAL) { param = DKIM_PARAM_VERSION; } else { g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_UNKNOWN, "invalid ARC v param"); state = DKIM_STATE_ERROR; break; } break; case 'a': param = DKIM_PARAM_SIGNALG; break; case 'b': param = DKIM_PARAM_SIGNATURE; break; case 'c': param = DKIM_PARAM_CANONALG; break; case 'd': param = DKIM_PARAM_DOMAIN; break; case 'h': if (type == RSPAMD_DKIM_ARC_SEAL) { g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_UNKNOWN, "ARC seal must NOT have h= tag"); state = DKIM_STATE_ERROR; break; } else { param = DKIM_PARAM_HDRLIST; } break; case 'i': if (type == RSPAMD_DKIM_NORMAL) { param = DKIM_PARAM_IDENTITY; } else { param = DKIM_PARAM_IDX; } break; case 'l': param = DKIM_PARAM_BODYLENGTH; break; case 'q': param = DKIM_PARAM_QUERYMETHOD; break; case 's': param = DKIM_PARAM_SELECTOR; break; case 't': param = DKIM_PARAM_TIMESTAMP; break; case 'x': param = DKIM_PARAM_EXPIRATION; break; case 'z': param = DKIM_PARAM_COPIEDHDRS; break; case 'r': param = DKIM_PARAM_IGNORE; break; default: g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_UNKNOWN, "invalid dkim param: %c", *tag); state = DKIM_STATE_ERROR; break; } break; case 2: if (tag[0] == 'b' && tag[1] == 'h') { if (type == RSPAMD_DKIM_ARC_SEAL) { g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_UNKNOWN, "ARC seal must NOT have bh= tag"); state = DKIM_STATE_ERROR; break; } else { param = DKIM_PARAM_BODYHASH; } } else if (tag[0] == 'c' && tag[1] == 'v') { if (type != RSPAMD_DKIM_ARC_SEAL) { g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_UNKNOWN, "cv tag is valid for ARC-Seal only"); state = DKIM_STATE_ERROR; break; } else { param = DKIM_PARAM_CV; } } else { g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_UNKNOWN, "invalid dkim param: %c%c", tag[0], tag[1]); state = DKIM_STATE_ERROR; } break; default: g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_UNKNOWN, "invalid dkim param length: %zd", taglen); state = DKIM_STATE_ERROR; break; } if (state != DKIM_STATE_ERROR) { /* Skip spaces */ state = DKIM_STATE_SKIP_SPACES; next_state = DKIM_STATE_VALUE; } break; case DKIM_STATE_VALUE: if (*p == ';') { if (param == DKIM_PARAM_UNKNOWN || p - c == 0) { state = DKIM_STATE_ERROR; } else { /* Cut trailing spaces for value */ gint tlen = p - c; const gchar *tmp = p - 1; while (tlen > 0) { if (!g_ascii_isspace (*tmp)) { break; } tlen --; tmp --; } if (!parser_funcs[param](ctx, c, tlen, err)) { state = DKIM_STATE_ERROR; } else { state = DKIM_STATE_SKIP_SPACES; next_state = DKIM_STATE_TAG; p++; taglen = 0; } } } else if (p == end) { if (param == DKIM_PARAM_UNKNOWN) { state = DKIM_STATE_ERROR; } else { gint tlen = p - c; const gchar *tmp = p - 1; while (tlen > 0) { if (!g_ascii_isspace (*tmp)) { break; } tlen --; tmp --; } if (!parser_funcs[param](ctx, c, tlen, err)) { state = DKIM_STATE_ERROR; } if (state == DKIM_STATE_ERROR) { /* * We need to return from here as state machine won't * do any more steps after p == end */ if (err) { msg_info_dkim ("dkim parse failed: %e", *err); } return NULL; } /* Finish processing */ p++; } } else { p++; } break; case DKIM_STATE_SKIP_SPACES: if (g_ascii_isspace (*p)) { p++; } else { c = p; state = next_state; } break; case DKIM_STATE_ERROR: if (err && *err) { msg_info_dkim ("dkim parse failed: %s", (*err)->message); return NULL; } else { msg_info_dkim ("dkim parse failed: unknown error when parsing %c tag", tag ? *tag : '?'); return NULL; } break; } } if (type == RSPAMD_DKIM_ARC_SEAL) { rspamd_dkim_add_arc_seal_headers (pool, &ctx->common); } /* Now check validity of signature */ if (ctx->b == NULL) { g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_EMPTY_B, "b parameter missing"); return NULL; } if (ctx->common.type != RSPAMD_DKIM_ARC_SEAL && ctx->bh == NULL) { g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_EMPTY_BH, "bh parameter missing"); return NULL; } if (ctx->domain == NULL) { g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_EMPTY_D, "domain parameter missing"); return NULL; } if (ctx->selector == NULL) { g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_EMPTY_S, "selector parameter missing"); return NULL; } if (ctx->common.type == RSPAMD_DKIM_NORMAL && ctx->ver == 0) { g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_EMPTY_V, "v parameter missing"); return NULL; } if (ctx->common.hlist == NULL) { g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_EMPTY_H, "h parameter missing"); return NULL; } if (ctx->sig_alg == DKIM_SIGN_UNKNOWN) { g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_EMPTY_S, "s parameter missing"); return NULL; } if (type != RSPAMD_DKIM_ARC_SEAL) { if (ctx->sig_alg == DKIM_SIGN_RSASHA1) { /* Check bh length */ if (ctx->bhlen != (guint) EVP_MD_size (EVP_sha1 ())) { g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_BADSIG, "signature has incorrect length: %zu", ctx->bhlen); return NULL; } } else if (ctx->sig_alg == DKIM_SIGN_RSASHA256 || ctx->sig_alg == DKIM_SIGN_ECDSASHA256) { if (ctx->bhlen != (guint) EVP_MD_size (EVP_sha256 ())) { g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_BADSIG, "signature has incorrect length: %zu", ctx->bhlen); return NULL; } } else if (ctx->sig_alg == DKIM_SIGN_RSASHA512 || ctx->sig_alg == DKIM_SIGN_ECDSASHA512) { if (ctx->bhlen != (guint) EVP_MD_size (EVP_sha512 ())) { g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_BADSIG, "signature has incorrect length: %zu", ctx->bhlen); return NULL; } } } /* Check expiration */ now = time (NULL); if (ctx->timestamp && now < ctx->timestamp && ctx->timestamp - now > (gint)time_jitter) { g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_FUTURE, "signature was made in future, ignoring"); return NULL; } if (ctx->expiration && ctx->expiration < now) { g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_EXPIRED, "signature has expired"); return NULL; } if (ctx->common.type != RSPAMD_DKIM_NORMAL && (ctx->common.idx == 0 || ctx->common.idx > RSPAMD_DKIM_MAX_ARC_IDX)) { g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_UNKNOWN, "i parameter missing or invalid for ARC"); return NULL; } if (ctx->common.type == RSPAMD_DKIM_ARC_SEAL) { if (ctx->cv == RSPAMD_ARC_UNKNOWN) { g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_UNKNOWN, "cv parameter missing or invalid for ARC"); return NULL; } } /* Now create dns key to request further */ taglen = strlen (ctx->domain) + strlen (ctx->selector) + sizeof (DKIM_DNSKEYNAME) + 2; ctx->dns_key = rspamd_mempool_alloc (ctx->pool, taglen); rspamd_snprintf (ctx->dns_key, taglen, "%s.%s.%s", ctx->selector, DKIM_DNSKEYNAME, ctx->domain); /* Create checksums for further operations */ if (ctx->sig_alg == DKIM_SIGN_RSASHA1) { md_alg = EVP_sha1 (); } else if (ctx->sig_alg == DKIM_SIGN_RSASHA256 || ctx->sig_alg == DKIM_SIGN_ECDSASHA256 || ctx->sig_alg == DKIM_SIGN_EDDSASHA256) { md_alg = EVP_sha256 (); } else if (ctx->sig_alg == DKIM_SIGN_RSASHA512 || ctx->sig_alg == DKIM_SIGN_ECDSASHA512) { md_alg = EVP_sha512 (); } else { g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_BADSIG, "signature has unsupported signature algorithm"); return NULL; } #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) ctx->common.body_hash = EVP_MD_CTX_create (); EVP_DigestInit_ex (ctx->common.body_hash, md_alg, NULL); ctx->common.headers_hash = EVP_MD_CTX_create (); EVP_DigestInit_ex (ctx->common.headers_hash, md_alg, NULL); rspamd_mempool_add_destructor (pool, (rspamd_mempool_destruct_t)EVP_MD_CTX_destroy, ctx->common.body_hash); rspamd_mempool_add_destructor (pool, (rspamd_mempool_destruct_t)EVP_MD_CTX_destroy, ctx->common.headers_hash); #else ctx->common.body_hash = EVP_MD_CTX_new (); EVP_DigestInit_ex (ctx->common.body_hash, md_alg, NULL); ctx->common.headers_hash = EVP_MD_CTX_new (); EVP_DigestInit_ex (ctx->common.headers_hash, md_alg, NULL); rspamd_mempool_add_destructor (pool, (rspamd_mempool_destruct_t)EVP_MD_CTX_free, ctx->common.body_hash); rspamd_mempool_add_destructor (pool, (rspamd_mempool_destruct_t)EVP_MD_CTX_free, ctx->common.headers_hash); #endif ctx->dkim_header = sig; return ctx; } struct rspamd_dkim_key_cbdata { rspamd_dkim_context_t *ctx; dkim_key_handler_f handler; gpointer ud; }; rspamd_dkim_key_t * rspamd_dkim_make_key (const gchar *keydata, guint keylen, enum rspamd_dkim_key_type type, GError **err) { rspamd_dkim_key_t *key = NULL; if (keylen < 3) { g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_KEYFAIL, "DKIM key is too short to be valid"); return NULL; } key = g_malloc0 (sizeof (rspamd_dkim_key_t)); REF_INIT_RETAIN (key, rspamd_dkim_key_free); key->keydata = g_malloc0 (keylen + 1); key->decoded_len = keylen; key->keylen = keylen; key->type = type; if (!rspamd_cryptobox_base64_decode (keydata, keylen, key->keydata, &key->decoded_len)) { REF_RELEASE (key); g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_KEYFAIL, "DKIM key is not a valid base64 string"); return NULL; } /* Calculate ID -> md5 */ EVP_MD_CTX *mdctx = EVP_MD_CTX_create (); #ifdef EVP_MD_CTX_FLAG_NON_FIPS_ALLOW EVP_MD_CTX_set_flags (mdctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW); #endif if (EVP_DigestInit_ex (mdctx, EVP_md5 (), NULL) == 1) { guint dlen = sizeof (key->key_id); EVP_DigestUpdate (mdctx, key->keydata, key->decoded_len); EVP_DigestFinal_ex (mdctx, key->key_id, &dlen); } EVP_MD_CTX_destroy (mdctx); if (key->type == RSPAMD_DKIM_KEY_EDDSA) { key->key.key_eddsa = key->keydata; if (key->decoded_len != rspamd_cryptobox_pk_sig_bytes ( RSPAMD_CRYPTOBOX_MODE_25519)) { g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_KEYFAIL, "DKIM key is has invalid length %d for eddsa; expected %d", (gint)key->decoded_len, rspamd_cryptobox_pk_sig_bytes (RSPAMD_CRYPTOBOX_MODE_25519)); REF_RELEASE (key); return NULL; } } else { key->key_bio = BIO_new_mem_buf (key->keydata, key->decoded_len); if (key->key_bio == NULL) { g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_KEYFAIL, "cannot make ssl bio from key"); REF_RELEASE (key); return NULL; } key->key_evp = d2i_PUBKEY_bio (key->key_bio, NULL); if (key->key_evp == NULL) { g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_KEYFAIL, "cannot extract pubkey from bio"); REF_RELEASE (key); return NULL; } if (type == RSPAMD_DKIM_KEY_RSA) { key->key.key_rsa = EVP_PKEY_get1_RSA (key->key_evp); if (key->key.key_rsa == NULL) { g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_KEYFAIL, "cannot extract rsa key from evp key"); REF_RELEASE (key); return NULL; } } else { key->key.key_ecdsa = EVP_PKEY_get1_EC_KEY (key->key_evp); if (key->key.key_ecdsa == NULL) { g_set_error (err, DKIM_ERROR, DKIM_SIGERROR_KEYFAIL, "cannot extract ecdsa key from evp key"); REF_RELEASE (key); return NULL; } } } return key; } const guchar * rspamd_dkim_key_id (rspamd_dkim_key_t *key) { if (key) { return key->key_id; } return NULL; } /** * Free DKIM key * @param key */ void rspamd_dkim_key_free (rspamd_dkim_key_t *key) { if (key->key_evp) { EVP_PKEY_free (key->key_evp); } if (key->type == RSPAMD_DKIM_KEY_RSA) { if (key->key.key_rsa) { RSA_free (key->key.key_rsa); } } else if (key->type == RSPAMD_DKIM_KEY_ECDSA) { if (key->key.key_ecdsa) { EC_KEY_free (key->key.key_ecdsa); } } /* Nothing in case of eddsa key */ if (key->key_bio) { BIO_free (key->key_bio); } g_free (key->keydata); g_free (key); } void rspamd_dkim_sign_key_free (rspamd_dkim_sign_key_t *key) { if (key->key_evp) { EVP_PKEY_free (key->key_evp); } if (key->type == RSPAMD_DKIM_KEY_RSA) { if (key->key.key_rsa) { RSA_free (key->key.key_rsa); } } if (key->key_bio) { BIO_free (key->key_bio); } if (key->type == RSPAMD_DKIM_KEY_EDDSA) { rspamd_explicit_memzero (key->key.key_eddsa, key->keylen); g_free (key->keydata); } g_free (key); } rspamd_dkim_key_t * rspamd_dkim_parse_key (const gchar *txt, gsize *keylen, GError **err) { const gchar *c, *p, *end, *key = NULL, *alg = "rsa"; enum { read_tag = 0, read_tag_before_eqsign, read_eqsign, read_p_tag, read_k_tag, ignore_value, skip_spaces, } state = read_tag, next_state; gchar tag = '\0'; gsize klen = 0, alglen = 0; c = txt; p = txt; end = txt + strlen (txt); while (p < end) { switch (state) { case read_tag: if (*p == '=') { state = read_eqsign; } else if (g_ascii_isspace (*p)) { state = skip_spaces; if (tag != '\0') { /* We had tag letter */ next_state = read_tag_before_eqsign; } else { /* We had no tag letter, so we ignore empty tag */ next_state = read_tag; } } else { tag = *p; } p++; break; case read_tag_before_eqsign: /* Input: spaces before eqsign * Output: either read a next tag (previous had no value), or read value * p is moved forward */ if (*p == '=') { state = read_eqsign; } else { tag = *p; state = read_tag; } p ++; break; case read_eqsign: /* Always switch to skip spaces state and do not advance p */ state = skip_spaces; if (tag == 'p') { next_state = read_p_tag; } else if (tag == 'k') { next_state = read_k_tag; } else { /* Unknown tag, ignore */ next_state = ignore_value; tag = '\0'; } break; case read_p_tag: if (*p == ';') { klen = p - c; key = c; state = read_tag; tag = '\0'; p++; } else if (g_ascii_isspace (*p)) { klen = p - c; key = c; state = skip_spaces; next_state = read_tag; tag = '\0'; } else { p ++; } break; case read_k_tag: if (*p == ';') { alglen = p - c; alg = c; state = read_tag; tag = '\0'; p++; } else if (g_ascii_isspace (*p)) { alglen = p - c; alg = c; state = skip_spaces; next_state = read_tag; tag = '\0'; } else { p ++; } break; case ignore_value: if (*p == ';') { state = read_tag; tag = '\0'; p ++; } else if (g_ascii_isspace pre { line-height: 125%; } td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; } span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; } td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; } span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; } .highlight .hll { background-color: #ffffcc } .highlight .c { color: #888888 } /* Comment */ .highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */ .highlight .k { color: #008800; font-weight: bold } /* Keyword */ .highlight .ch { color: #888888 } /* Comment.Hashbang */ .highlight .cm { color: #888888 } /* Comment.Multiline */ .highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */ .highlight .cpf { color: #888888 } /* Comment.PreprocFile */ .highlight .c1 { color: #888888 } /* Comment.Single */ .highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long *//** * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later */ import { getRootUrl as realGetRootUrl, } from '@nextcloud/router' /** * Creates a relative url for remote use * * @param {string} service id * @return {string} the url */ export const linkToRemoteBase = service => { return realGetRootUrl() + '/remote.php/' + service }
/** * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later */ import { getRootUrl as realGetRootUrl, } from '@nextcloud/router' /** * Creates a relative url for remote use * * @param {string} service id * @return {string} the url */ export const linkToRemoteBase = service => { return realGetRootUrl() + '/remote.php/' + service }