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