fix sign api for to address #6164
[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 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 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
70   case GNUNET_BLOCK_TYPE_FS_IBLOCK:
71     GNUNET_break (NULL == va_arg (va, const char *));
72     return NULL;
73
74   case GNUNET_BLOCK_TYPE_FS_UBLOCK:
75     guard = va_arg (va, const char *);
76     if (0 == strcmp (guard,
77                      "seen-set-size"))
78     {
79       size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size (va_arg (va, unsigned
80                                                                   int),
81                                                           BLOOMFILTER_K);
82     }
83     else if (0 == strcmp (guard,
84                           "filter-size"))
85     {
86       size = va_arg (va, unsigned int);
87     }
88     else
89     {
90       /* va-args invalid! bad bug, complain! */
91       GNUNET_break (0);
92       size = 8;
93     }
94     if (0 == size)
95       size = raw_data_size;   /* not for us to determine, use what we got! */
96     GNUNET_break (NULL == va_arg (va, const char *));
97     return GNUNET_BLOCK_GROUP_bf_create (cls,
98                                          size,
99                                          BLOOMFILTER_K,
100                                          type,
101                                          nonce,
102                                          raw_data,
103                                          raw_data_size);
104
105   default:
106     GNUNET_break (NULL == va_arg (va, const char *));
107     GNUNET_break (0);
108     return NULL;
109   }
110 }
111
112
113 /**
114  * Function called to validate a reply or a request.  For
115  * request evaluation, simply pass "NULL" for the reply_block.
116  * Note that it is assumed that the reply has already been
117  * matched to the key (and signatures checked) as it would
118  * be done with the #GNUNET_BLOCK_get_key() function.
119  *
120  * @param cls closure
121  * @param ctx block context
122  * @param type block type
123  * @param bg group to use for evaluation
124  * @param eo control flags
125  * @param query original query (hash)
126  * @param xquery extrended query data (can be NULL, depending on type)
127  * @param xquery_size number of bytes in @a xquery
128  * @param reply_block response to validate
129  * @param reply_block_size number of bytes in @a reply_block
130  * @return characterization of result
131  */
132 static enum GNUNET_BLOCK_EvaluationResult
133 block_plugin_fs_evaluate (void *cls,
134                           struct GNUNET_BLOCK_Context *ctx,
135                           enum GNUNET_BLOCK_Type type,
136                           struct GNUNET_BLOCK_Group *bg,
137                           enum GNUNET_BLOCK_EvaluationOptions eo,
138                           const struct GNUNET_HashCode *query,
139                           const void *xquery,
140                           size_t xquery_size,
141                           const void *reply_block,
142                           size_t reply_block_size)
143 {
144   const struct UBlock *ub;
145   struct GNUNET_HashCode hc;
146   struct GNUNET_HashCode chash;
147
148   switch (type)
149   {
150   case GNUNET_BLOCK_TYPE_FS_DBLOCK:
151   case GNUNET_BLOCK_TYPE_FS_IBLOCK:
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     return GNUNET_BLOCK_EVALUATION_OK_LAST;
160
161   case GNUNET_BLOCK_TYPE_FS_UBLOCK:
162     if (0 != xquery_size)
163     {
164       GNUNET_break_op (0);
165       return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
166     }
167     if (NULL == reply_block)
168       return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
169
170     if (reply_block_size < sizeof(struct UBlock))
171     {
172       GNUNET_break_op (0);
173       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
174     }
175     ub = reply_block;
176     GNUNET_CRYPTO_hash (&ub->verification_key,
177                         sizeof(ub->verification_key),
178                         &hc);
179     if (0 != memcmp (&hc,
180                      query,
181                      sizeof(struct GNUNET_HashCode)))
182     {
183       GNUNET_break_op (0);
184       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
185     }
186     if (reply_block_size != ntohl (ub->purpose.size) + sizeof(struct
187                                                               GNUNET_CRYPTO_EcdsaSignature))
188     {
189       GNUNET_break_op (0);
190       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
191     }
192     if ((0 == (eo & GNUNET_BLOCK_EO_LOCAL_SKIP_CRYPTO)) &&
193         (GNUNET_OK !=
194          GNUNET_CRYPTO_ecdsa_verify_ (GNUNET_SIGNATURE_PURPOSE_FS_UBLOCK,
195                                       &ub->purpose,
196                                       &ub->signature,
197                                       &ub->verification_key)))
198     {
199       GNUNET_break_op (0);
200       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
201     }
202     GNUNET_CRYPTO_hash (reply_block,
203                         reply_block_size,
204                         &chash);
205     if (GNUNET_YES ==
206         GNUNET_BLOCK_GROUP_bf_test_and_set (bg,
207                                             &chash))
208       return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
209     return GNUNET_BLOCK_EVALUATION_OK_MORE;
210
211   default:
212     return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
213   }
214 }
215
216
217 /**
218  * Function called to obtain the key for a block.
219  *
220  * @param cls closure
221  * @param type block type
222  * @param block block to get the key for
223  * @param block_size number of bytes in @a block
224  * @param key set to the key (query) for the given block
225  * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
226  *         (or if extracting a key from a block of this type does not work)
227  */
228 static int
229 block_plugin_fs_get_key (void *cls,
230                          enum GNUNET_BLOCK_Type type,
231                          const void *block,
232                          size_t block_size,
233                          struct GNUNET_HashCode *key)
234 {
235   const struct UBlock *ub;
236
237   switch (type)
238   {
239   case GNUNET_BLOCK_TYPE_FS_DBLOCK:
240   case GNUNET_BLOCK_TYPE_FS_IBLOCK:
241     GNUNET_CRYPTO_hash (block, block_size, key);
242     return GNUNET_OK;
243
244   case GNUNET_BLOCK_TYPE_FS_UBLOCK:
245     if (block_size < sizeof(struct UBlock))
246     {
247       GNUNET_break (0);
248       return GNUNET_SYSERR;
249     }
250     ub = block;
251     GNUNET_CRYPTO_hash (&ub->verification_key,
252                         sizeof(ub->verification_key),
253                         key);
254     return GNUNET_OK;
255
256   default:
257     GNUNET_break (0);
258     return GNUNET_SYSERR;
259   }
260 }
261
262
263 /**
264  * Entry point for the plugin.
265  */
266 void *
267 libgnunet_plugin_block_fs_init (void *cls)
268 {
269   static enum GNUNET_BLOCK_Type types[] = {
270     GNUNET_BLOCK_TYPE_FS_DBLOCK,
271     GNUNET_BLOCK_TYPE_FS_IBLOCK,
272     GNUNET_BLOCK_TYPE_FS_UBLOCK,
273     GNUNET_BLOCK_TYPE_ANY       /* end of list */
274   };
275   struct GNUNET_BLOCK_PluginFunctions *api;
276
277   api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
278   api->evaluate = &block_plugin_fs_evaluate;
279   api->get_key = &block_plugin_fs_get_key;
280   api->create_group = &block_plugin_fs_create_group;
281   api->types = types;
282   return api;
283 }
284
285
286 /**
287  * Exit point from the plugin.
288  */
289 void *
290 libgnunet_plugin_block_fs_done (void *cls)
291 {
292   struct GNUNET_BLOCK_PluginFunctions *api = cls;
293
294   GNUNET_free (api);
295   return NULL;
296 }
297
298
299 /* end of plugin_block_fs.c */