-more rsa refactoring
[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 #include "gns_common.h"
33
34 /**
35  * Number of bits we set per entry in the bloomfilter.
36  * Do not change! -from fs
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_gns_evaluate (void *cls, enum GNUNET_BLOCK_Type type,
60                           const struct GNUNET_HashCode * query,
61                           struct GNUNET_CONTAINER_BloomFilter **bf,
62                           int32_t bf_mutator, const void *xquery,
63                           size_t xquery_size, const void *reply_block,
64                           size_t reply_block_size)
65 {
66   const char* name;
67   struct GNUNET_HashCode query_key;
68   struct GNUNET_HashCode mhash;
69   struct GNUNET_HashCode chash;
70   struct GNUNET_CRYPTO_ShortHashCode pkey_hash;
71   struct GNSNameRecordBlock *nrb;
72   struct GNUNET_CRYPTO_HashAsciiEncoded xor_exp;
73   struct GNUNET_CRYPTO_HashAsciiEncoded xor_got;
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 = (const char*)&nrb[1];
102   
103   GNUNET_CRYPTO_short_hash (&nrb->public_key,
104                             sizeof(nrb->public_key),
105                             &pkey_hash);
106   GNUNET_GNS_get_key_for_record (name, &pkey_hash, &query_key);
107   
108   GNUNET_CRYPTO_hash_to_enc (&query_key, &xor_exp);
109   GNUNET_CRYPTO_hash_to_enc (query, &xor_got);
110
111   //GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
112   //           "BLOCK_TEST for %s got %s expected %s\n",
113   //           name, (char*) &xor_got, (char*) &xor_exp);
114
115   /* Check query key against public key */
116   if (0 != GNUNET_CRYPTO_hash_cmp(query, &query_key))
117   {
118     GNUNET_break_op (0);
119     return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
120   }
121   
122   record_match = 0;
123   rd_count = ntohl(nrb->rd_count);
124   rd_data = (char*)&nrb[1];
125   rd_data += strlen(name) + 1;
126   rd_len = reply_block_size - (strlen(name) + 1
127                                + sizeof(struct GNSNameRecordBlock));
128   {
129     struct GNUNET_NAMESTORE_RecordData rd[rd_count];
130     unsigned int i;
131     uint64_t exp = UINT64_MAX;
132     struct GNUNET_TIME_Absolute et = GNUNET_TIME_UNIT_FOREVER_ABS;
133     
134     if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_len,
135                                                                rd_data,
136                                                                rd_count,
137                                                                rd))
138     {
139       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
140                  "Data invalid (%d bytes, %d records)\n", rd_len, rd_count);
141       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
142     }
143
144     if (xquery_size < sizeof(uint32_t))
145       record_xquery = 0;
146     else
147       record_xquery = ntohl(*((uint32_t*)xquery));
148     
149     for (i=0; i<rd_count; i++)
150     {     
151       GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
152       exp = GNUNET_MIN (exp, rd[i].expiration_time);
153       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
154                  "Got record of size %d expiration %u\n",
155      rd[i].data_size, rd[i].expiration_time);
156       if ((record_xquery != 0)
157           && (rd[i].record_type == record_xquery))
158       {
159         record_match++;
160       }
161     }
162     et.abs_value = exp;
163     
164     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
165         "Verifying signature of %d records for name %s with expiration of %u\n",
166                rd_count, name, et.abs_value);
167
168     if (GNUNET_OK != GNUNET_NAMESTORE_verify_signature (&nrb->public_key,
169                                                         et,
170                                                         name,
171                                                         rd_count,
172                                                         rd,
173                                                         &nrb->signature))
174     {
175       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Signature invalid for %s\n", name);
176       GNUNET_break_op (0);
177       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
178     }
179   }
180   
181   if (NULL != bf)
182   {
183     GNUNET_CRYPTO_hash(reply_block, reply_block_size, &chash);
184     GNUNET_BLOCK_mingle_hash(&chash, bf_mutator, &mhash);
185     if (NULL != *bf)
186     {
187       if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test(*bf, &mhash))
188         return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
189     }
190     else
191     {
192       *bf = GNUNET_CONTAINER_bloomfilter_init(NULL, 8, BLOOMFILTER_K);
193     }
194     GNUNET_CONTAINER_bloomfilter_add(*bf, &mhash);
195   }
196   return GNUNET_BLOCK_EVALUATION_OK_MORE;
197 }
198
199
200 /**
201  * Function called to obtain the key for a block.
202  *
203  * @param cls closure
204  * @param type block type
205  * @param block block to get the key for
206  * @param block_size number of bytes in block
207  * @param key set to the key (query) for the given block
208  * @return GNUNET_OK on success, GNUNET_SYSERR if type not supported
209  *         (or if extracting a key from a block of this type does not work)
210  */
211 static int
212 block_plugin_gns_get_key (void *cls, enum GNUNET_BLOCK_Type type,
213                          const void *block, size_t block_size,
214                          struct GNUNET_HashCode * key)
215 {
216   struct GNUNET_CRYPTO_ShortHashCode pkey_hash;
217   const struct GNSNameRecordBlock *nrb = (const struct GNSNameRecordBlock *)block;
218   const char *name;
219
220   if (type != GNUNET_BLOCK_TYPE_GNS_NAMERECORD)
221     return GNUNET_SYSERR;
222   name = (const char *) &nrb[1];
223   if (NULL == memchr (name, '\0', block_size - sizeof (struct GNSNameRecordBlock)))
224   {
225     /* malformed, no 0-termination in name */
226     GNUNET_break_op (0);
227     return GNUNET_SYSERR; 
228   }
229   GNUNET_CRYPTO_short_hash (&nrb->public_key,
230                             sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
231                             &pkey_hash);
232   GNUNET_GNS_get_key_for_record (name, &pkey_hash, key);
233   return GNUNET_OK;
234 }
235
236
237 /**
238  * Entry point for the plugin.
239  */
240 void *
241 libgnunet_plugin_block_gns_init (void *cls)
242 {
243   static enum GNUNET_BLOCK_Type types[] =
244   {
245     GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
246     GNUNET_BLOCK_TYPE_ANY       /* end of list */
247   };
248   struct GNUNET_BLOCK_PluginFunctions *api;
249
250   api = GNUNET_malloc (sizeof (struct GNUNET_BLOCK_PluginFunctions));
251   api->evaluate = &block_plugin_gns_evaluate;
252   api->get_key = &block_plugin_gns_get_key;
253   api->types = types;
254   return api;
255 }
256
257
258 /**
259  * Exit point from the plugin.
260  */
261 void *
262 libgnunet_plugin_block_gns_done (void *cls)
263 {
264   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
265
266   GNUNET_free (api);
267   return NULL;
268 }
269
270 /* end of plugin_block_gns.c */