implement revocation block plugin, get revocation test to pass again
[oweals/gnunet.git] / src / fs / plugin_block_fs.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2010, 2013 GNUnet e.V.
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file fs/plugin_block_fs.c
23  * @brief blocks used for file-sharing
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_block_plugin.h"
28 #include "gnunet_fs_service.h"
29 #include "block_fs.h"
30 #include "gnunet_signatures.h"
31 #include "gnunet_block_group_lib.h"
32
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  * Create a new block group.
43  *
44  * @param ctx block context in which the block group is created
45  * @param type type of the block for which we are creating the group
46  * @param nonce random value used to seed the group creation
47  * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh
48  * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh
49  * @param va variable arguments specific to @a type
50  * @return block group handle, NULL if block groups are not supported
51  *         by this @a type of block (this is not an error)
52  */
53 static struct GNUNET_BLOCK_Group *
54 block_plugin_fs_create_group (void *cls,
55                               enum GNUNET_BLOCK_Type type,
56                               uint32_t nonce,
57                               const void *raw_data,
58                               size_t raw_data_size,
59                               va_list va)
60 {
61   unsigned int size;
62   const char *guard;
63
64   switch (type)
65   {
66   case GNUNET_BLOCK_TYPE_FS_DBLOCK:
67     GNUNET_break (NULL == va_arg (va, const char *));
68     return NULL;
69   case GNUNET_BLOCK_TYPE_FS_IBLOCK:
70     GNUNET_break (NULL == va_arg (va, const char *));
71     return NULL;
72   case GNUNET_BLOCK_TYPE_FS_UBLOCK:
73     guard = va_arg (va, const char *);
74     if (0 != strcmp (guard,
75                      "seen-set-size"))
76     {
77       /* va-args invalid! bad bug, complain! */
78       GNUNET_break (0);
79       size = 8;
80     }
81     else
82     {
83       size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size (va_arg (va, unsigned int),
84                                                           BLOOMFILTER_K);
85     }
86     if (0 == size)
87       size = raw_data_size; /* not for us to determine, use what we got! */
88     GNUNET_break (NULL == va_arg (va, const char *));
89     return GNUNET_BLOCK_GROUP_bf_create (cls,
90                                          size,
91                                          BLOOMFILTER_K,
92                                          type,
93                                          nonce,
94                                          raw_data,
95                                          raw_data_size);
96   default:
97     GNUNET_break (NULL == va_arg (va, const char *));
98     GNUNET_break (0);
99     return NULL;
100   }
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  * Note that it is assumed that the reply has already been
108  * matched to the key (and signatures checked) as it would
109  * be done with the #GNUNET_BLOCK_get_key() function.
110  *
111  * @param cls closure
112  * @param ctx block context
113  * @param type block type
114  * @param bg group to use for evaluation
115  * @param eo control flags
116  * @param query original query (hash)
117  * @param xquery extrended query data (can be NULL, depending on type)
118  * @param xquery_size number of bytes in @a xquery
119  * @param reply_block response to validate
120  * @param reply_block_size number of bytes in @a reply_block
121  * @return characterization of result
122  */
123 static enum GNUNET_BLOCK_EvaluationResult
124 block_plugin_fs_evaluate (void *cls,
125                           struct GNUNET_BLOCK_Context *ctx,
126                           enum GNUNET_BLOCK_Type type,
127                           struct GNUNET_BLOCK_Group *bg,
128                           enum GNUNET_BLOCK_EvaluationOptions eo,
129                           const struct GNUNET_HashCode *query,
130                           const void *xquery,
131                           size_t xquery_size,
132                           const void *reply_block,
133                           size_t reply_block_size)
134 {
135   const struct UBlock *ub;
136   struct GNUNET_HashCode hc;
137   struct GNUNET_HashCode chash;
138
139   switch (type)
140   {
141   case GNUNET_BLOCK_TYPE_FS_DBLOCK:
142   case GNUNET_BLOCK_TYPE_FS_IBLOCK:
143     if (0 != xquery_size)
144     {
145       GNUNET_break_op (0);
146       return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
147     }
148     if (NULL == reply_block)
149       return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
150     return GNUNET_BLOCK_EVALUATION_OK_LAST;
151   case GNUNET_BLOCK_TYPE_FS_UBLOCK:
152     if (0 != xquery_size)
153     {
154       GNUNET_break_op (0);
155       return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
156     }
157     if (NULL == reply_block)
158       return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
159
160     if (reply_block_size < sizeof (struct UBlock))
161     {
162       GNUNET_break_op (0);
163       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
164     }
165     ub = reply_block;
166     GNUNET_CRYPTO_hash (&ub->verification_key,
167                         sizeof (ub->verification_key),
168                         &hc);
169     if (0 != memcmp (&hc,
170                      query,
171                      sizeof (struct GNUNET_HashCode)))
172     {
173       GNUNET_break_op (0);
174       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
175     }
176     if (reply_block_size != ntohl (ub->purpose.size) + sizeof (struct GNUNET_CRYPTO_EcdsaSignature))
177     {
178       GNUNET_break_op (0);
179       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
180     }
181     if ( (0 == (eo & GNUNET_BLOCK_EO_LOCAL_SKIP_CRYPTO)) &&
182          (GNUNET_OK !=
183           GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_UBLOCK,
184                                       &ub->purpose,
185                                       &ub->signature,
186                                       &ub->verification_key)) )
187     {
188       GNUNET_break_op (0);
189       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
190     }
191     GNUNET_CRYPTO_hash (reply_block,
192                         reply_block_size,
193                         &chash);
194     if (GNUNET_YES ==
195         GNUNET_BLOCK_GROUP_bf_test_and_set (bg,
196                                             &chash))
197       return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
198     return GNUNET_BLOCK_EVALUATION_OK_MORE;
199   default:
200     return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
201   }
202 }
203
204
205 /**
206  * Function called to obtain the key for a block.
207  *
208  * @param cls closure
209  * @param type block type
210  * @param block block to get the key for
211  * @param block_size number of bytes in @a block
212  * @param key set to the key (query) for the given block
213  * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
214  *         (or if extracting a key from a block of this type does not work)
215  */
216 static int
217 block_plugin_fs_get_key (void *cls,
218                          enum GNUNET_BLOCK_Type type,
219                          const void *block,
220                          size_t block_size,
221                          struct GNUNET_HashCode *key)
222 {
223   const struct UBlock *ub;
224
225   switch (type)
226   {
227   case GNUNET_BLOCK_TYPE_FS_DBLOCK:
228   case GNUNET_BLOCK_TYPE_FS_IBLOCK:
229     GNUNET_CRYPTO_hash (block, block_size, key);
230     return GNUNET_OK;
231   case GNUNET_BLOCK_TYPE_FS_UBLOCK:
232     if (block_size < sizeof (struct UBlock))
233     {
234       GNUNET_break (0);
235       return GNUNET_SYSERR;
236     }
237     ub = block;
238     GNUNET_CRYPTO_hash (&ub->verification_key,
239                         sizeof (ub->verification_key),
240                         key);
241     return GNUNET_OK;
242   default:
243     GNUNET_break (0);
244     return GNUNET_SYSERR;
245   }
246 }
247
248
249 /**
250  * Entry point for the plugin.
251  */
252 void *
253 libgnunet_plugin_block_fs_init (void *cls)
254 {
255   static enum GNUNET_BLOCK_Type types[] =
256   {
257     GNUNET_BLOCK_TYPE_FS_DBLOCK,
258     GNUNET_BLOCK_TYPE_FS_IBLOCK,
259     GNUNET_BLOCK_TYPE_FS_UBLOCK,
260     GNUNET_BLOCK_TYPE_ANY       /* end of list */
261   };
262   struct GNUNET_BLOCK_PluginFunctions *api;
263
264   api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
265   api->evaluate = &block_plugin_fs_evaluate;
266   api->get_key = &block_plugin_fs_get_key;
267   api->create_group = &block_plugin_fs_create_group;
268   api->types = types;
269   return api;
270 }
271
272
273 /**
274  * Exit point from the plugin.
275  */
276 void *
277 libgnunet_plugin_block_fs_done (void *cls)
278 {
279   struct GNUNET_BLOCK_PluginFunctions *api = cls;
280
281   GNUNET_free (api);
282   return NULL;
283 }
284
285 /* end of plugin_block_fs.c */