move mingle function
[oweals/gnunet.git] / src / block / plugin_block_fs.c
1 /*
2      This file is part of GNUnet
3      (C) 2010 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file block/plugin_block_fs.c
23  * @brief blocks used for file-sharing
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
28 #include "plugin_block.h"
29 #include "block_fs.h"
30 #include "gnunet_signatures.h"
31
32 #define DEBUG_FS_BLOCK GNUNET_NO
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  * Function called to validate a reply or a request.  For
42  * request evaluation, simply pass "NULL" for the reply_block.
43  * Note that it is assumed that the reply has already been 
44  * matched to the key (and signatures checked) as it would
45  * be done with the "get_key" function.
46  *
47  * @param cls closure
48  * @param type block type
49  * @param query original query (hash)
50  * @param bf pointer to bloom filter associated with query; possibly updated (!)
51  * @param bf_mutator mutation value for bf
52  * @param xquery extrended query data (can be NULL, depending on type)
53  * @param xquery_size number of bytes in xquery
54  * @param reply_block response to validate
55  * @param reply_block_size number of bytes in reply block
56  * @return characterization of result
57  */
58 static enum GNUNET_BLOCK_EvaluationResult
59 block_plugin_fs_evaluate (void *cls,
60                           enum GNUNET_BLOCK_Type type,
61                           const GNUNET_HashCode *query,
62                           struct GNUNET_CONTAINER_BloomFilter **bf,
63                           int32_t bf_mutator,
64                           const void *xquery,
65                           size_t xquery_size,
66                           const void *reply_block,
67                           size_t reply_block_size)
68 {
69   const struct SBlock *sb;
70   GNUNET_HashCode chash;
71   GNUNET_HashCode mhash;
72   const GNUNET_HashCode *nsid;
73   GNUNET_HashCode sh;
74
75   switch (type)
76     {
77     case GNUNET_BLOCK_TYPE_DBLOCK:
78     case GNUNET_BLOCK_TYPE_IBLOCK:
79       if (xquery_size != 0) 
80         {
81           GNUNET_break_op (0);
82           return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
83         }
84       if (reply_block == NULL)
85         return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
86       return GNUNET_BLOCK_EVALUATION_OK_LAST;
87     case GNUNET_BLOCK_TYPE_KBLOCK:
88     case GNUNET_BLOCK_TYPE_NBLOCK:
89       if (xquery_size != 0) 
90         {
91           GNUNET_break_op (0);
92           return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
93         }
94       if (reply_block == NULL)
95         return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
96       GNUNET_CRYPTO_hash (reply_block,
97                           reply_block_size,
98                           &chash);
99       GNUNET_BLOCK_mingle_hash (&chash, bf_mutator, &mhash);
100       if (NULL != *bf)
101         {
102           if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf,
103                                                                &mhash))
104             return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
105         }
106       else
107         {
108           *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 
109                                                    8,
110                                                    BLOOMFILTER_K);
111         }
112       GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
113       return GNUNET_BLOCK_EVALUATION_OK_MORE;
114     case GNUNET_BLOCK_TYPE_SBLOCK:
115       if (xquery_size != sizeof (GNUNET_HashCode)) 
116         {
117           GNUNET_break_op (0);
118           return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
119         }
120       if (reply_block == NULL)
121         return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
122       nsid = xquery;
123       if (reply_block_size < sizeof (struct NBlock))
124         return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
125       sb = reply_block;
126       GNUNET_CRYPTO_hash (&sb->subspace,
127                           sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
128                           &sh);
129       if (0 != memcmp (nsid,
130                        &sh,
131                        sizeof (GNUNET_HashCode)))
132         {
133           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
134                       _("Reply mismatched in terms of namespace.  Discarded.\n"));
135           return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
136         }
137       GNUNET_CRYPTO_hash (reply_block,
138                           reply_block_size,
139                           &chash);
140       GNUNET_BLOCK_mingle_hash (&chash, bf_mutator, &mhash);
141       if (NULL != *bf)
142         {
143           if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf,
144                                                                &mhash))
145             return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
146         }
147       else
148         {
149           *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 
150                                                    8,
151                                                    BLOOMFILTER_K);
152         }
153       GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
154       return GNUNET_BLOCK_EVALUATION_OK_MORE;
155     default:
156       return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
157     }
158 }
159
160
161 /**
162  * Function called to obtain the key for a block.
163  *
164  * @param cls closure
165  * @param type block type
166  * @param block block to get the key for
167  * @param block_size number of bytes in block
168  * @param key set to the key (query) for the given block
169  * @return GNUNET_OK on success, GNUNET_SYSERR if type not supported
170  *         (or if extracting a key from a block of this type does not work)
171  */
172 static int
173 block_plugin_fs_get_key (void *cls,
174                          enum GNUNET_BLOCK_Type type,
175                          const void *block,
176                          size_t block_size,
177                          GNUNET_HashCode *key)
178 {
179   const struct KBlock *kb;
180   const struct SBlock *sb;
181   const struct NBlock *nb;
182
183   switch (type)
184     {
185     case GNUNET_BLOCK_TYPE_DBLOCK:
186     case GNUNET_BLOCK_TYPE_IBLOCK:
187       GNUNET_CRYPTO_hash (block, block_size, key);
188       return GNUNET_OK;
189     case GNUNET_BLOCK_TYPE_KBLOCK:
190       if (block_size < sizeof (struct KBlock))
191         {
192           GNUNET_break_op (0);
193           return GNUNET_SYSERR;
194         }
195       kb = block;
196       if (block_size - sizeof (struct KBlock) !=
197           ntohl (kb->purpose.size) 
198           - sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) 
199           - sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) ) 
200         {
201           GNUNET_break_op (0);
202           return GNUNET_SYSERR;
203         }
204       if (GNUNET_OK !=
205           GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK,
206                                     &kb->purpose,
207                                     &kb->signature,
208                                     &kb->keyspace)) 
209         {
210           GNUNET_break_op (0);
211           return GNUNET_SYSERR;
212         }
213       if (key != NULL)
214         GNUNET_CRYPTO_hash (&kb->keyspace,
215                             sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
216                             key);
217       return GNUNET_OK;
218     case GNUNET_BLOCK_TYPE_SBLOCK:
219       if (block_size < sizeof (struct SBlock))
220         {
221           GNUNET_break_op (0);
222           return GNUNET_SYSERR;
223         }
224       sb = block;
225       if (block_size !=
226           ntohl (sb->purpose.size) + sizeof (struct GNUNET_CRYPTO_RsaSignature))
227         {
228           GNUNET_break_op (0);
229           return GNUNET_SYSERR;
230         }
231       if (GNUNET_OK !=
232           GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK,
233                                     &sb->purpose,
234                                     &sb->signature,
235                                     &sb->subspace)) 
236         {
237           GNUNET_break_op (0);
238           return GNUNET_SYSERR;
239         }
240       if (key != NULL)
241         *key = sb->identifier;
242       return GNUNET_OK;
243     case GNUNET_BLOCK_TYPE_NBLOCK:
244       if (block_size < sizeof (struct NBlock))
245         {
246           GNUNET_break_op (0);
247           return GNUNET_SYSERR;
248         }
249       nb = block;
250       if (block_size - sizeof (struct NBlock) !=
251           ntohl (nb->ns_purpose.size) 
252           - sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) 
253           - sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) ) 
254         {
255           GNUNET_break_op (0);
256           return GNUNET_SYSERR;
257         }
258       if (block_size !=
259           ntohl (nb->ksk_purpose.size) + sizeof (struct GNUNET_CRYPTO_RsaSignature))
260         {
261           GNUNET_break_op (0);
262           return GNUNET_SYSERR;
263         }
264       if (GNUNET_OK !=
265           GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK_KSIG,
266                                     &nb->ksk_purpose,
267                                     &nb->ksk_signature,
268                                     &nb->keyspace)) 
269         {
270           GNUNET_break_op (0);
271           return GNUNET_SYSERR;
272         }
273       if (GNUNET_OK !=
274           GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK,
275                                     &nb->ns_purpose,
276                                     &nb->ns_signature,
277                                     &nb->subspace)) 
278         {
279           GNUNET_break_op (0);
280           return GNUNET_SYSERR;
281         }
282       /* FIXME: we used to xor ID with NSID,
283          why not here? */
284       if (key != NULL)
285         GNUNET_CRYPTO_hash (&nb->keyspace,
286                             sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
287                             key);
288       return GNUNET_OK;
289     default:
290       return GNUNET_SYSERR;
291     }
292 }
293                                   
294
295 /**
296  * Entry point for the plugin.
297  */
298 void *
299 libgnunet_plugin_block_fs_init (void *cls)
300 {
301   static enum GNUNET_BLOCK_Type types[] = 
302     {
303       GNUNET_BLOCK_TYPE_DBLOCK,
304       GNUNET_BLOCK_TYPE_IBLOCK,
305       GNUNET_BLOCK_TYPE_KBLOCK,
306       GNUNET_BLOCK_TYPE_SBLOCK,
307       GNUNET_BLOCK_TYPE_NBLOCK,
308       GNUNET_BLOCK_TYPE_ANY /* end of list */
309     };
310   struct GNUNET_BLOCK_PluginFunctions *api;
311
312   api = GNUNET_malloc (sizeof (struct GNUNET_BLOCK_PluginFunctions));
313   api->evaluate = &block_plugin_fs_evaluate;
314   api->get_key = &block_plugin_fs_get_key;
315   api->types = types;
316   return api;
317 }
318
319
320 /**
321  * Exit point from the plugin.
322  */
323 void *
324 libgnunet_plugin_block_fs_done (void *cls)
325 {
326   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
327
328   GNUNET_free (api);
329   return NULL;
330 }
331
332 /* end of plugin_block_fs.c */