]> source.dussan.org Git - rspamd.git/commitdiff
[CritFix] Check NM part of pubkey to match it with rotating keypairs
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 29 Jun 2018 10:24:43 +0000 (11:24 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 29 Jun 2018 10:24:43 +0000 (11:24 +0100)
src/fuzzy_storage.c
src/libcryptobox/keypair.c
src/libcryptobox/keypair.h
src/libcryptobox/keypair_private.h
src/libutil/http.c
src/libutil/logger.c
src/plugins/fuzzy_check.c

index 5499fa732ade8631f77dcd40a5923c939824d451..9243e369ef1675ff0bfdd29bd6bc6ee4bf934e8d 100644 (file)
@@ -1060,7 +1060,7 @@ rspamd_fuzzy_decrypt_command (struct fuzzy_session *s)
 
        /* Now decrypt request */
        if (!rspamd_cryptobox_decrypt_nm_inplace (payload, payload_len, hdr->nonce,
-                       rspamd_pubkey_get_nm (rk),
+                       rspamd_pubkey_get_nm (rk, key->key),
                        hdr->mac, RSPAMD_CRYPTOBOX_MODE_25519)) {
                msg_err ("decryption failed");
                rspamd_pubkey_unref (rk);
@@ -1068,7 +1068,7 @@ rspamd_fuzzy_decrypt_command (struct fuzzy_session *s)
                return FALSE;
        }
 
-       memcpy (s->nm, rspamd_pubkey_get_nm (rk), sizeof (s->nm));
+       memcpy (s->nm, rspamd_pubkey_get_nm (rk, key->key), sizeof (s->nm));
        rspamd_pubkey_unref (rk);
 
        return TRUE;
index c8fa5633aeab3a50ea4ccff791030501138d576b..ee9fa4649d7bbbf55624526e78f0ae859e6ada82 100644 (file)
@@ -444,12 +444,19 @@ rspamd_pubkey_from_bin (const guchar *raw,
 
 
 const guchar *
-rspamd_pubkey_get_nm (struct rspamd_cryptobox_pubkey *p)
+rspamd_pubkey_get_nm (struct rspamd_cryptobox_pubkey *p,
+               struct rspamd_cryptobox_keypair *kp)
 {
        g_assert (p != NULL);
 
        if (p->nm) {
-               return p->nm->nm;
+               if (memcmp (kp->id, (const guchar *)&p->nm->sk_id, sizeof (guint64)) == 0) {
+                       return p->nm->nm;
+               }
+
+               /* Wrong ID, need to recalculate */
+               REF_RELEASE (p->nm);
+               p->nm = NULL;
        }
 
        return NULL;
@@ -468,6 +475,7 @@ rspamd_pubkey_calculate_nm (struct rspamd_cryptobox_pubkey *p,
                        abort ();
                }
 
+               memcpy (&p->nm->sk_id, kp->id, sizeof (guint64));
                REF_INIT_RETAIN (p->nm, rspamd_cryptobox_nm_dtor);
        }
 
index d7c386b9191bb34a08882530a8bccab7d38baa54..92af13b683b337925d611b331562969c494107fb 100644 (file)
@@ -139,7 +139,8 @@ enum rspamd_cryptobox_mode rspamd_pubkey_alg (struct rspamd_cryptobox_pubkey *p)
  * @param p
  * @return
  */
-const guchar * rspamd_pubkey_get_nm (struct rspamd_cryptobox_pubkey *p);
+const guchar * rspamd_pubkey_get_nm (struct rspamd_cryptobox_pubkey *p,
+                                                                        struct rspamd_cryptobox_keypair *kp);
 
 /**
  * Calculate and store nm value for the specified local key (performs ECDH)
index d91d1c68ee91d6fab1e4289f4b5a1991bae4c208..78b894d387ba359bd60a8965f6d82f070c614f6a 100644 (file)
@@ -25,6 +25,7 @@
  */
 struct RSPAMD_ALIGNED(32) rspamd_cryptobox_nm {
        guchar RSPAMD_ALIGNED(32) nm[rspamd_cryptobox_MAX_NMBYTES];
+       guint64 sk_id; /* Used to store secret key id */
        ref_entry_t ref;
 };
 
index fea3cadb430acf690cd7f0cbc9e702541ac6ab32..2f78def4755d57e59c9d124fbc40670a4143be2e 100644 (file)
@@ -844,7 +844,7 @@ rspamd_http_decrypt_message (struct rspamd_http_connection *conn,
        dec_len = msg->body_buf.len - rspamd_cryptobox_nonce_bytes (mode) -
                        rspamd_cryptobox_mac_bytes (mode);
 
-       if ((nm = rspamd_pubkey_get_nm (peer_key)) == NULL) {
+       if ((nm = rspamd_pubkey_get_nm (peer_key, priv->local_key)) == NULL) {
                nm = rspamd_pubkey_calculate_nm (peer_key, priv->local_key);
        }
 
@@ -1703,7 +1703,7 @@ rspamd_http_connection_encrypt_message (
 
        cnt = i;
 
-       if ((nm = rspamd_pubkey_get_nm (peer_key)) == NULL) {
+       if ((nm = rspamd_pubkey_get_nm (peer_key, priv->local_key)) == NULL) {
                nm = rspamd_pubkey_calculate_nm (peer_key, priv->local_key);
        }
 
index 99c22390fc1fcd02a67309f2f7c660fabc19a2de..bbdc69e974c88e42010d81be7db801aa177103c9 100644 (file)
@@ -584,7 +584,7 @@ rspamd_log_encrypt_message (const gchar *begin, const gchar *end,
        mac = p;
        p += rspamd_cryptobox_mac_bytes (RSPAMD_CRYPTOBOX_MODE_25519);
        memcpy (p, begin, end - begin);
-       comp = rspamd_pubkey_get_nm (rspamd_log->pk);
+       comp = rspamd_pubkey_get_nm (rspamd_log->pk, rspamd_log->keypair);
        g_assert (comp != NULL);
        rspamd_cryptobox_encrypt_nm_inplace (p, end - begin, nonce, comp, mac,
                        RSPAMD_CRYPTOBOX_MODE_25519);
index e1ac4743bd8c6e743557ae6e18059210011753cb..777f6fa62c4baae2bcdd7a2974bc716355b09386 100644 (file)
@@ -1214,7 +1214,8 @@ fuzzy_encrypt_cmd (struct fuzzy_rule *rule,
        rspamd_keypair_cache_process (fuzzy_module_ctx->keypairs_cache,
                        rule->local_key, rule->peer_key);
        rspamd_cryptobox_encrypt_nm_inplace (data, datalen,
-                       hdr->nonce, rspamd_pubkey_get_nm (rule->peer_key), hdr->mac,
+                       hdr->nonce, rspamd_pubkey_get_nm (rule->peer_key, rule->local_key),
+                       hdr->mac,
                        rspamd_pubkey_alg (rule->peer_key));
 }
 
@@ -1769,7 +1770,7 @@ fuzzy_process_reply (guchar **pos, gint *r, GPtrArray *req,
                if (!rspamd_cryptobox_decrypt_nm_inplace ((guchar *)&encrep.rep,
                                sizeof (encrep.rep),
                                encrep.hdr.nonce,
-                               rspamd_pubkey_get_nm (rule->peer_key),
+                               rspamd_pubkey_get_nm (rule->peer_key, rule->local_key),
                                encrep.hdr.mac,
                                rspamd_pubkey_alg (rule->peer_key))) {
                        msg_info ("cannot decrypt reply");