ba1fd56e176fd19809b865008195174ae30d0726
[oweals/gnunet.git] / src / gns / plugin_block_gns.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 gns/plugin_block_gns.c
23  * @brief blocks used for GNS records
24  * @author Martin Schanzenbach
25  */
26
27 #include "platform.h"
28 #include "gnunet_block_plugin.h"
29 #include "gnunet_namestore_service.h"
30 #include "block_gns.h"
31 #include "gnunet_signatures.h"
32
33 /**
34  * Number of bits we set per entry in the bloomfilter.
35  * Do not change! -from fs
36  */
37 #define BLOOMFILTER_K 16
38
39 /**
40  * Function called to validate a reply or a request.  For
41  * request evaluation, simply pass "NULL" for the reply_block.
42  * Note that it is assumed that the reply has already been
43  * matched to the key (and signatures checked) as it would
44  * be done with the "get_key" function.
45  *
46  * @param cls closure
47  * @param type block type
48  * @param query original query (hash)
49  * @param bf pointer to bloom filter associated with query; possibly updated (!)
50  * @param bf_mutator mutation value for bf
51  * @param xquery extrended query data (can be NULL, depending on type)
52  * @param xquery_size number of bytes in xquery
53  * @param reply_block response to validate
54  * @param reply_block_size number of bytes in reply block
55  * @return characterization of result
56  */
57 static enum GNUNET_BLOCK_EvaluationResult
58 block_plugin_gns_evaluate (void *cls, enum GNUNET_BLOCK_Type type,
59                           const struct GNUNET_HashCode * query,
60                           struct GNUNET_CONTAINER_BloomFilter **bf,
61                           int32_t bf_mutator, const void *xquery,
62                           size_t xquery_size, const void *reply_block,
63                           size_t reply_block_size)
64 {
65   char* name;
66   struct GNUNET_HashCode pkey_hash_double;
67   struct GNUNET_HashCode query_key;
68   struct GNUNET_HashCode name_hash_double;
69   struct GNUNET_HashCode mhash;
70   struct GNUNET_HashCode chash;
71   struct GNUNET_CRYPTO_ShortHashCode pkey_hash;
72   struct GNUNET_CRYPTO_ShortHashCode name_hash;
73   struct GNSNameRecordBlock *nrb;
74   uint32_t rd_count;
75   char* rd_data = NULL;
76   int rd_len;
77   uint32_t record_xquery;
78   unsigned int record_match;
79   
80   //GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "RB SIZE %d\n", reply_block_size);
81
82   if (type != GNUNET_BLOCK_TYPE_GNS_NAMERECORD)
83     return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
84   if (reply_block == NULL)
85   {
86     /**
87      *  check if request is valid
88      *  FIXME we could check for the record types here
89      **/
90     if (xquery_size < sizeof(uint32_t))
91     {
92       GNUNET_break_op (0);
93       return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
94     }
95     return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
96   }
97   
98   /* this is a reply */
99
100   nrb = (struct GNSNameRecordBlock *)reply_block;
101   name = (char*)&nrb[1];
102   GNUNET_CRYPTO_short_hash(&nrb->public_key,
103                      sizeof(nrb->public_key),
104                      &pkey_hash);
105
106   GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
107   
108   GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
109   GNUNET_CRYPTO_short_hash_double(&pkey_hash, &pkey_hash_double);
110
111   GNUNET_CRYPTO_hash_xor(&pkey_hash_double, &name_hash_double, &query_key);
112   
113   struct GNUNET_CRYPTO_HashAsciiEncoded xor_exp;
114   struct GNUNET_CRYPTO_HashAsciiEncoded xor_got;
115   GNUNET_CRYPTO_hash_to_enc (&query_key, &xor_exp);
116   GNUNET_CRYPTO_hash_to_enc (query, &xor_got);
117
118   //GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
119   //           "BLOCK_TEST for %s got %s expected %s\n",
120   //           name, (char*) &xor_got, (char*) &xor_exp);
121
122   /* Check query key against public key */
123   if (0 != GNUNET_CRYPTO_hash_cmp(query, &query_key))
124   {
125     GNUNET_break_op (0);
126     return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
127   }
128   
129   record_match = 0;
130   rd_count = ntohl(nrb->rd_count);
131   rd_data = (char*)&nrb[1];
132   rd_data += strlen(name) + 1;
133   rd_len = reply_block_size - (strlen(name) + 1
134                                + sizeof(struct GNSNameRecordBlock));
135   {
136     struct GNUNET_NAMESTORE_RecordData rd[rd_count];
137     unsigned int i;
138     struct GNUNET_TIME_Absolute exp = GNUNET_TIME_UNIT_FOREVER_ABS;
139     
140     if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_len,
141                                                                rd_data,
142                                                                rd_count,
143                                                                rd))
144     {
145       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
146                  "Data invalid (%d bytes, %d records)\n", rd_len, rd_count);
147       GNUNET_break_op (0);
148       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
149     }
150
151     if (xquery_size < sizeof(uint32_t))
152       record_xquery = 0;
153     else
154       record_xquery = ntohl(*((uint32_t*)xquery));
155     
156     for (i=0; i<rd_count; i++)
157     {
158       
159       exp = GNUNET_TIME_absolute_min (exp, rd[i].expiration);
160       
161       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
162                  "Got record of size %d\n", rd[i].data_size);
163
164       if ((record_xquery != 0)
165           && (rd[i].record_type == record_xquery))
166       {
167         record_match++;
168       }
169     }
170     
171     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
172                "Verifying signature of %d records for name %s\n",
173                rd_count, name);
174
175     if (GNUNET_OK != GNUNET_NAMESTORE_verify_signature (&nrb->public_key,
176                                                         exp,
177                                                         name,
178                                                         rd_count,
179                                                         rd,
180                                                         &nrb->signature))
181     {
182       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Signature invalid for name %s\n");
183       GNUNET_break_op (0);
184       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
185     }
186   }
187   
188   if (NULL != bf)
189   {
190     GNUNET_CRYPTO_hash(reply_block, reply_block_size, &chash);
191     GNUNET_BLOCK_mingle_hash(&chash, bf_mutator, &mhash);
192     if (NULL != *bf)
193     {
194       if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test(*bf, &mhash))
195         return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
196     }
197     else
198     {
199       *bf = GNUNET_CONTAINER_bloomfilter_init(NULL, 8, BLOOMFILTER_K);
200     }
201     GNUNET_CONTAINER_bloomfilter_add(*bf, &mhash);
202   }
203   return GNUNET_BLOCK_EVALUATION_OK_MORE;
204 }
205
206
207 /**
208  * Function called to obtain the key for a block.
209  *
210  * @param cls closure
211  * @param type block type
212  * @param block block to get the key for
213  * @param block_size number of bytes in block
214  * @param key set to the key (query) for the given block
215  * @return GNUNET_OK on success, GNUNET_SYSERR if type not supported
216  *         (or if extracting a key from a block of this type does not work)
217  */
218 static int
219 block_plugin_gns_get_key (void *cls, enum GNUNET_BLOCK_Type type,
220                          const void *block, size_t block_size,
221                          struct GNUNET_HashCode * key)
222 {
223   if (type != GNUNET_BLOCK_TYPE_GNS_NAMERECORD)
224     return GNUNET_SYSERR;
225   struct GNUNET_CRYPTO_ShortHashCode name_hash;
226   struct GNUNET_CRYPTO_ShortHashCode pkey_hash;
227   struct GNUNET_HashCode name_hash_double;
228   struct GNUNET_HashCode pkey_hash_double;
229
230   struct GNSNameRecordBlock *nrb = (struct GNSNameRecordBlock *)block;
231
232   GNUNET_CRYPTO_short_hash(&nrb[1], strlen((char*)&nrb[1]), &name_hash);
233   GNUNET_CRYPTO_short_hash(&nrb->public_key,
234                      sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
235                      &pkey_hash);
236   
237   GNUNET_CRYPTO_short_hash_double(&name_hash, &name_hash_double);
238   GNUNET_CRYPTO_short_hash_double(&pkey_hash, &pkey_hash_double);
239
240   GNUNET_CRYPTO_hash_xor(&name_hash_double, &pkey_hash_double, key);
241   
242   return GNUNET_OK;
243 }
244
245
246 /**
247  * Entry point for the plugin.
248  */
249 void *
250 libgnunet_plugin_block_gns_init (void *cls)
251 {
252   static enum GNUNET_BLOCK_Type types[] =
253   {
254     GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
255     GNUNET_BLOCK_TYPE_ANY       /* end of list */
256   };
257   struct GNUNET_BLOCK_PluginFunctions *api;
258
259   api = GNUNET_malloc (sizeof (struct GNUNET_BLOCK_PluginFunctions));
260   api->evaluate = &block_plugin_gns_evaluate;
261   api->get_key = &block_plugin_gns_get_key;
262   api->types = types;
263   return api;
264 }
265
266
267 /**
268  * Exit point from the plugin.
269  */
270 void *
271 libgnunet_plugin_block_gns_done (void *cls)
272 {
273   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
274
275   GNUNET_free (api);
276   return NULL;
277 }
278
279 /* end of plugin_block_gns.c */