You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

keypairs_cache.c 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "config.h"
  17. #include "rspamd.h"
  18. #include "keypairs_cache.h"
  19. #include "keypair_private.h"
  20. #include "hash.h"
  21. #include "xxhash.h"
  22. struct rspamd_keypair_elt {
  23. guchar nm[rspamd_cryptobox_MAX_NMBYTES];
  24. guchar pair[rspamd_cryptobox_MAX_PKBYTES + rspamd_cryptobox_MAX_SKBYTES];
  25. };
  26. struct rspamd_keypair_cache {
  27. rspamd_lru_hash_t *hash;
  28. };
  29. static void
  30. rspamd_keypair_destroy (gpointer ptr)
  31. {
  32. struct rspamd_keypair_elt *elt = (struct rspamd_keypair_elt *)ptr;
  33. rspamd_explicit_memzero (elt, sizeof (*elt));
  34. g_slice_free1 (sizeof (*elt), elt);
  35. }
  36. static guint
  37. rspamd_keypair_hash (gconstpointer ptr)
  38. {
  39. struct rspamd_keypair_elt *elt = (struct rspamd_keypair_elt *)ptr;
  40. return XXH64 (elt->pair, sizeof (elt->pair), rspamd_hash_seed ());
  41. }
  42. static gboolean
  43. rspamd_keypair_equal (gconstpointer p1, gconstpointer p2)
  44. {
  45. struct rspamd_keypair_elt *e1 = (struct rspamd_keypair_elt *)p1,
  46. *e2 = (struct rspamd_keypair_elt *)p2;
  47. return memcmp (e1->pair, e2->pair, sizeof (e1->pair)) == 0;
  48. }
  49. struct rspamd_keypair_cache *
  50. rspamd_keypair_cache_new (guint max_items)
  51. {
  52. struct rspamd_keypair_cache *c;
  53. g_assert (max_items > 0);
  54. c = g_slice_alloc (sizeof (*c));
  55. c->hash = rspamd_lru_hash_new_full (max_items, -1, NULL,
  56. rspamd_keypair_destroy, rspamd_keypair_hash, rspamd_keypair_equal);
  57. return c;
  58. }
  59. void
  60. rspamd_keypair_cache_process (struct rspamd_keypair_cache *c,
  61. gpointer lk, gpointer rk)
  62. {
  63. struct rspamd_http_keypair *kp_local = (struct rspamd_http_keypair *)lk,
  64. *kp_remote = (struct rspamd_http_keypair *)rk;
  65. struct rspamd_keypair_elt search, *new;
  66. g_assert (kp_local != NULL);
  67. g_assert (kp_remote != NULL);
  68. memset (&search, 0, sizeof (search));
  69. memcpy (search.pair, kp_remote->pk, rspamd_cryptobox_pk_bytes ());
  70. memcpy (&search.pair[rspamd_cryptobox_MAX_PKBYTES], kp_local->sk,
  71. rspamd_cryptobox_sk_bytes ());
  72. new = rspamd_lru_hash_lookup (c->hash, &search, time (NULL));
  73. if (new == NULL) {
  74. new = g_slice_alloc0 (sizeof (*new));
  75. memcpy (new->pair, kp_remote->pk, rspamd_cryptobox_pk_bytes ());
  76. memcpy (&new->pair[rspamd_cryptobox_MAX_PKBYTES], kp_local->sk,
  77. rspamd_cryptobox_sk_bytes ());
  78. rspamd_cryptobox_nm (new->nm, kp_remote->pk, kp_local->sk);
  79. rspamd_lru_hash_insert (c->hash, new, new, time (NULL), -1);
  80. }
  81. g_assert (new != NULL);
  82. memcpy (kp_remote->nm, new->nm, rspamd_cryptobox_nm_bytes ());
  83. kp_remote->has_nm = TRUE;
  84. #if 0
  85. memcpy (kp_local->nm, new->nm, rspamd_cryptobox_NMBYTES);
  86. #endif
  87. }
  88. void
  89. rspamd_keypair_cache_destroy (struct rspamd_keypair_cache *c)
  90. {
  91. if (c != NULL) {
  92. rspamd_lru_hash_destroy (c->hash);
  93. g_slice_free1 (sizeof (*c), c);
  94. }
  95. }