plugin_block_revocation.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2017 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file block/plugin_block_revocation.c
  18. * @brief revocation for a block plugin
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_signatures.h"
  23. #include "gnunet_block_plugin.h"
  24. #include "gnunet_block_group_lib.h"
  25. #include "revocation.h"
  26. #include "gnunet_revocation_service.h"
  27. #define DEBUG_REVOCATION GNUNET_EXTRA_LOGGING
  28. /**
  29. * Number of bits we set per entry in the bloomfilter.
  30. * Do not change!
  31. */
  32. #define BLOOMFILTER_K 16
  33. /**
  34. * How big is the BF we use for DHT blocks?
  35. */
  36. #define REVOCATION_BF_SIZE 8
  37. /**
  38. * Context used inside the plugin.
  39. */
  40. struct InternalContext
  41. {
  42. unsigned int matching_bits;
  43. };
  44. /**
  45. * Create a new block group.
  46. *
  47. * @param ctx block context in which the block group is created
  48. * @param type type of the block for which we are creating the group
  49. * @param nonce random value used to seed the group creation
  50. * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh
  51. * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh
  52. * @param va variable arguments specific to @a type
  53. * @return block group handle, NULL if block groups are not supported
  54. * by this @a type of block (this is not an error)
  55. */
  56. static struct GNUNET_BLOCK_Group *
  57. block_plugin_revocation_create_group (void *cls,
  58. enum GNUNET_BLOCK_Type type,
  59. uint32_t nonce,
  60. const void *raw_data,
  61. size_t raw_data_size,
  62. va_list va)
  63. {
  64. unsigned int bf_size;
  65. const char *guard;
  66. guard = va_arg (va, const char *);
  67. if (0 == strcmp (guard,
  68. "seen-set-size"))
  69. bf_size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size (va_arg (va, unsigned int),
  70. BLOOMFILTER_K);
  71. else if (0 == strcmp (guard,
  72. "filter-size"))
  73. bf_size = va_arg (va, unsigned int);
  74. else
  75. {
  76. GNUNET_break (0);
  77. bf_size = REVOCATION_BF_SIZE;
  78. }
  79. GNUNET_break (NULL == va_arg (va, const char *));
  80. return GNUNET_BLOCK_GROUP_bf_create (cls,
  81. bf_size,
  82. BLOOMFILTER_K,
  83. type,
  84. nonce,
  85. raw_data,
  86. raw_data_size);
  87. }
  88. /**
  89. * Function called to validate a reply or a request. For
  90. * request evaluation, simply pass "NULL" for the reply_block.
  91. *
  92. * @param cls our `struct InternalContext`
  93. * @param ctx context
  94. * @param type block type
  95. * @param group block group to use
  96. * @param eo control flags
  97. * @param query original query (hash)
  98. * @param xquery extrended query data (can be NULL, depending on type)
  99. * @param xquery_size number of bytes in xquery
  100. * @param reply_block response to validate
  101. * @param reply_block_size number of bytes in reply block
  102. * @return characterization of result
  103. */
  104. static enum GNUNET_BLOCK_EvaluationResult
  105. block_plugin_revocation_evaluate (void *cls,
  106. struct GNUNET_BLOCK_Context *ctx,
  107. enum GNUNET_BLOCK_Type type,
  108. struct GNUNET_BLOCK_Group *group,
  109. enum GNUNET_BLOCK_EvaluationOptions eo,
  110. const struct GNUNET_HashCode *query,
  111. const void *xquery,
  112. size_t xquery_size,
  113. const void *reply_block,
  114. size_t reply_block_size)
  115. {
  116. struct InternalContext *ic = cls;
  117. struct GNUNET_HashCode chash;
  118. const struct RevokeMessage *rm = reply_block;
  119. if (NULL == reply_block)
  120. return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
  121. if (reply_block_size != sizeof (*rm))
  122. {
  123. GNUNET_break_op (0);
  124. return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
  125. }
  126. if (GNUNET_YES !=
  127. GNUNET_REVOCATION_check_pow (&rm->public_key,
  128. rm->proof_of_work,
  129. ic->matching_bits))
  130. {
  131. GNUNET_break_op (0);
  132. return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
  133. }
  134. if (GNUNET_OK !=
  135. GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_REVOCATION,
  136. &rm->purpose,
  137. &rm->signature,
  138. &rm->public_key))
  139. {
  140. GNUNET_break_op (0);
  141. return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
  142. }
  143. GNUNET_CRYPTO_hash (&rm->public_key,
  144. sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey),
  145. &chash);
  146. if (GNUNET_YES ==
  147. GNUNET_BLOCK_GROUP_bf_test_and_set (group,
  148. &chash))
  149. return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
  150. return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
  151. }
  152. /**
  153. * Function called to obtain the key for a block.
  154. *
  155. * @param cls closure
  156. * @param type block type
  157. * @param block block to get the key for
  158. * @param block_size number of bytes in block
  159. * @param key set to the key (query) for the given block
  160. * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
  161. * (or if extracting a key from a block of this type does not work)
  162. */
  163. static int
  164. block_plugin_revocation_get_key (void *cls,
  165. enum GNUNET_BLOCK_Type type,
  166. const void *block,
  167. size_t block_size,
  168. struct GNUNET_HashCode *key)
  169. {
  170. const struct RevokeMessage *rm = block;
  171. if (block_size != sizeof (*rm))
  172. {
  173. GNUNET_break_op (0);
  174. return GNUNET_SYSERR;
  175. }
  176. GNUNET_CRYPTO_hash (&rm->public_key,
  177. sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey),
  178. key);
  179. return GNUNET_OK;
  180. }
  181. /**
  182. * Entry point for the plugin.
  183. *
  184. * @param cls the configuration to use
  185. */
  186. void *
  187. libgnunet_plugin_block_revocation_init (void *cls)
  188. {
  189. static enum GNUNET_BLOCK_Type types[] =
  190. {
  191. GNUNET_BLOCK_TYPE_REVOCATION,
  192. GNUNET_BLOCK_TYPE_ANY /* end of list */
  193. };
  194. const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
  195. struct GNUNET_BLOCK_PluginFunctions *api;
  196. struct InternalContext *ic;
  197. unsigned long long matching_bits;
  198. if (GNUNET_OK !=
  199. GNUNET_CONFIGURATION_get_value_number (cfg,
  200. "REVOCATION",
  201. "WORKBITS",
  202. &matching_bits))
  203. return NULL;
  204. api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
  205. api->evaluate = &block_plugin_revocation_evaluate;
  206. api->get_key = &block_plugin_revocation_get_key;
  207. api->create_group = &block_plugin_revocation_create_group;
  208. api->types = types;
  209. ic = GNUNET_new (struct InternalContext);
  210. ic->matching_bits = (unsigned int) matching_bits;
  211. api->cls = ic;
  212. return api;
  213. }
  214. /**
  215. * Exit point from the plugin.
  216. */
  217. void *
  218. libgnunet_plugin_block_revocation_done (void *cls)
  219. {
  220. struct GNUNET_BLOCK_PluginFunctions *api = cls;
  221. struct InternalContext *ic = api->cls;
  222. GNUNET_free (ic);
  223. GNUNET_free (api);
  224. return NULL;
  225. }
  226. /* end of plugin_block_revocation.c */