/* 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);
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;
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;
abort ();
}
+ memcpy (&p->nm->sk_id, kp->id, sizeof (guint64));
REF_INIT_RETAIN (p->nm, rspamd_cryptobox_nm_dtor);
}
* @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)
*/
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;
};
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);
}
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);
}
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);
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));
}
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");