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