bc970081786c71f360d3651e8f82ceff785ec7bd
[oweals/gnunet.git] / src / revocation / revocation_api.c
1 /*
2       This file is part of GNUnet
3       Copyright (C) 2013, 2016 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 /**
19  * @file revocation/revocation_api.c
20  * @brief API to perform and access key revocations
21  * @author Christian Grothoff
22  */
23 #include "platform.h"
24 #include "gnunet_revocation_service.h"
25 #include "gnunet_signatures.h"
26 #include "gnunet_protocols.h"
27 #include "revocation.h"
28 #include <gcrypt.h>
29
30
31 /**
32  * Handle for the key revocation query.
33  */
34 struct GNUNET_REVOCATION_Query
35 {
36
37   /**
38    * Message queue to the service.
39    */
40   struct GNUNET_MQ_Handle *mq;
41
42   /**
43    * Function to call with the result.
44    */
45   GNUNET_REVOCATION_Callback func;
46
47   /**
48    * Closure for @e func.
49    */
50   void *func_cls;
51
52 };
53
54
55 /**
56  * Generic error handler, called with the appropriate
57  * error code and the same closure specified at the creation of
58  * the message queue.
59  * Not every message queue implementation supports an error handler.
60  *
61  * @param cls closure with the `struct GNUNET_NSE_Handle *`
62  * @param error error code
63  */
64 static void
65 query_mq_error_handler (void *cls,
66                         enum GNUNET_MQ_Error error)
67 {
68   struct GNUNET_REVOCATION_Query *q = cls;
69
70   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
71               "Revocation query MQ error\n");
72   q->func (q->func_cls,
73            GNUNET_SYSERR);
74   GNUNET_REVOCATION_query_cancel (q);
75 }
76
77
78 /**
79  * Handle response to our revocation query.
80  *
81  * @param cls our `struct GNUNET_REVOCATION_Query` handle
82  * @param qrm response we got
83  */
84 static void
85 handle_revocation_query_response (void *cls,
86                                   const struct QueryResponseMessage *qrm)
87 {
88   struct GNUNET_REVOCATION_Query *q = cls;
89
90   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
91               "Revocation query result: %d\n",
92               (uint32_t) ntohl (qrm->is_valid));
93   q->func (q->func_cls,
94            ntohl (qrm->is_valid));
95   GNUNET_REVOCATION_query_cancel (q);
96 }
97
98
99 /**
100  * Check if a key was revoked.
101  *
102  * @param cfg the configuration to use
103  * @param key key to check for revocation
104  * @param func funtion to call with the result of the check
105  * @param func_cls closure to pass to @a func
106  * @return handle to use in #GNUNET_REVOCATION_query_cancel to stop REVOCATION from invoking the callback
107  */
108 struct GNUNET_REVOCATION_Query *
109 GNUNET_REVOCATION_query (const struct GNUNET_CONFIGURATION_Handle *cfg,
110                          const struct GNUNET_CRYPTO_EcdsaPublicKey *key,
111                          GNUNET_REVOCATION_Callback func,
112                          void *func_cls)
113 {
114   struct GNUNET_REVOCATION_Query *q
115     = GNUNET_new (struct GNUNET_REVOCATION_Query);
116   struct GNUNET_MQ_MessageHandler handlers[] = {
117     GNUNET_MQ_hd_fixed_size (revocation_query_response,
118                              GNUNET_MESSAGE_TYPE_REVOCATION_QUERY_RESPONSE,
119                              struct QueryResponseMessage,
120                              q),
121     GNUNET_MQ_handler_end ()
122   };
123   struct QueryMessage *qm;
124   struct GNUNET_MQ_Envelope *env;
125
126   q->mq = GNUNET_CLIENT_connect (cfg,
127                                  "revocation",
128                                  handlers,
129                                  &query_mq_error_handler,
130                                  q);
131   if (NULL == q->mq)
132   {
133     GNUNET_free (q);
134     return NULL;
135   }
136   q->func = func;
137   q->func_cls = func_cls;
138   env = GNUNET_MQ_msg (qm,
139                        GNUNET_MESSAGE_TYPE_REVOCATION_QUERY);
140   qm->reserved = htonl (0);
141   qm->key = *key;
142   GNUNET_MQ_send (q->mq,
143                   env);
144   return q;
145 }
146
147
148 /**
149  * Cancel key revocation check.
150  *
151  * @param q query to cancel
152  */
153 void
154 GNUNET_REVOCATION_query_cancel (struct GNUNET_REVOCATION_Query *q)
155 {
156   if (NULL != q->mq)
157   {
158     GNUNET_MQ_destroy (q->mq);
159     q->mq = NULL;
160   }
161   GNUNET_free (q);
162 }
163
164
165 /**
166  * Handle for the key revocation operation.
167  */
168 struct GNUNET_REVOCATION_Handle
169 {
170
171   /**
172    * Message queue to the service.
173    */
174   struct GNUNET_MQ_Handle *mq;
175
176   /**
177    * Function to call once we are done.
178    */
179   GNUNET_REVOCATION_Callback func;
180
181   /**
182    * Closure for @e func.
183    */
184   void *func_cls;
185
186 };
187
188
189 /**
190  * Generic error handler, called with the appropriate
191  * error code and the same closure specified at the creation of
192  * the message queue.
193  * Not every message queue implementation supports an error handler.
194  *
195  * @param cls closure with the `struct GNUNET_NSE_Handle *`
196  * @param error error code
197  */
198 static void
199 revocation_mq_error_handler (void *cls,
200                              enum GNUNET_MQ_Error error)
201 {
202   struct GNUNET_REVOCATION_Handle *h = cls;
203
204   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
205               "Revocation MQ error\n");
206   h->func (h->func_cls,
207            GNUNET_SYSERR);
208   GNUNET_REVOCATION_revoke_cancel (h);
209 }
210
211
212 /**
213  * Handle response to our revocation query.
214  *
215  * @param cls our `struct GNUNET_REVOCATION_Handle` handle
216  * @param rrm response we got
217  */
218 static void
219 handle_revocation_response (void *cls,
220                             const struct RevocationResponseMessage *rrm)
221 {
222   struct GNUNET_REVOCATION_Handle *h = cls;
223
224   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
225               "Revocation transmission result: %d\n",
226               (uint32_t) ntohl (rrm->is_valid));
227   h->func (h->func_cls,
228            ntohl (rrm->is_valid));
229   GNUNET_REVOCATION_revoke_cancel (h);
230 }
231
232
233 /**
234  * Perform key revocation.
235  *
236  * @param cfg the configuration to use
237  * @param key public key of the key to revoke
238  * @param sig signature to use on the revocation (should have been
239  *            created using #GNUNET_REVOCATION_sign_revocation).
240  * @param pow proof of work to use (should have been created by
241  *            iteratively calling #GNUNET_REVOCATION_check_pow)
242  * @param func funtion to call with the result of the check
243  *             (called with `is_valid` being #GNUNET_NO if
244  *              the revocation worked).
245  * @param func_cls closure to pass to @a func
246  * @return handle to use in #GNUNET_REVOCATION_revoke_cancel to stop REVOCATION from invoking the callback
247  */
248 struct GNUNET_REVOCATION_Handle *
249 GNUNET_REVOCATION_revoke (const struct GNUNET_CONFIGURATION_Handle *cfg,
250                           const struct GNUNET_CRYPTO_EcdsaPublicKey *key,
251                           const struct GNUNET_CRYPTO_EcdsaSignature *sig,
252                           uint64_t pow,
253                           GNUNET_REVOCATION_Callback func,
254                           void *func_cls)
255 {
256   struct GNUNET_REVOCATION_Handle *h
257     = GNUNET_new (struct GNUNET_REVOCATION_Handle);
258   struct GNUNET_MQ_MessageHandler handlers[] = {
259     GNUNET_MQ_hd_fixed_size (revocation_response,
260                              GNUNET_MESSAGE_TYPE_REVOCATION_REVOKE_RESPONSE,
261                              struct RevocationResponseMessage,
262                              h),
263     GNUNET_MQ_handler_end ()
264   };
265   unsigned long long matching_bits;
266   struct RevokeMessage *rm;
267   struct GNUNET_MQ_Envelope *env;
268
269   if ( (GNUNET_OK ==
270         GNUNET_CONFIGURATION_get_value_number (cfg,
271                                                "REVOCATION",
272                                                "WORKBITS",
273                                                &matching_bits)) &&
274        (GNUNET_YES !=
275         GNUNET_REVOCATION_check_pow (key,
276                                      pow,
277                                      (unsigned int) matching_bits)) )
278   {
279     GNUNET_break (0);
280     GNUNET_free (h);
281     return NULL;
282   }
283
284   h->mq = GNUNET_CLIENT_connect (cfg,
285                                  "revocation",
286                                  handlers,
287                                  &revocation_mq_error_handler,
288                                  h);
289   if (NULL == h->mq)
290   {
291     GNUNET_free (h);
292     return NULL;
293   }
294   h->func = func;
295   h->func_cls = func_cls;
296   env = GNUNET_MQ_msg (rm,
297                        GNUNET_MESSAGE_TYPE_REVOCATION_REVOKE);
298   rm->reserved = htonl (0);
299   rm->proof_of_work = pow;
300   rm->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_REVOCATION);
301   rm->purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
302                             sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
303   rm->public_key = *key;
304   rm->signature = *sig;
305   GNUNET_MQ_send (h->mq,
306                   env);
307   return h;
308 }
309
310
311 /**
312  * Cancel key revocation.
313  *
314  * @param h operation to cancel
315  */
316 void
317 GNUNET_REVOCATION_revoke_cancel (struct GNUNET_REVOCATION_Handle *h)
318 {
319   if (NULL != h->mq)
320   {
321     GNUNET_MQ_destroy (h->mq);
322     h->mq = NULL;
323   }
324   GNUNET_free (h);
325 }
326
327
328 /**
329  * Calculate the 'proof-of-work' hash (an expensive hash).
330  *
331  * @param buf data to hash
332  * @param buf_len number of bytes in @a buf
333  * @param result where to write the resulting hash
334  */
335 static void
336 pow_hash (const void *buf,
337           size_t buf_len,
338           struct GNUNET_HashCode *result)
339 {
340   GNUNET_break (0 ==
341                 gcry_kdf_derive (buf, buf_len,
342                                  GCRY_KDF_SCRYPT,
343                                  1 /* subalgo */,
344                                  "gnunet-revocation-proof-of-work",
345                                  strlen ("gnunet-revocation-proof-of-work"),
346                                  2 /* iterations; keep cost of individual op small */,
347                                  sizeof (struct GNUNET_HashCode), result));
348 }
349
350
351 /**
352  * Count the leading zeroes in hash.
353  *
354  * @param hash to count leading zeros in
355  * @return the number of leading zero bits.
356  */
357 static unsigned int
358 count_leading_zeroes (const struct GNUNET_HashCode *hash)
359 {
360   unsigned int hash_count;
361
362   hash_count = 0;
363   while ((0 == GNUNET_CRYPTO_hash_get_bit (hash, hash_count)))
364     hash_count++;
365   return hash_count;
366 }
367
368
369 /**
370  * Check if the given proof-of-work value
371  * would be acceptable for revoking the given key.
372  *
373  * @param key key to check for
374  * @param pow proof of work value
375  * @param matching_bits how many bits must match (configuration)
376  * @return #GNUNET_YES if the @a pow is acceptable, #GNUNET_NO if not
377  */
378 int
379 GNUNET_REVOCATION_check_pow (const struct GNUNET_CRYPTO_EcdsaPublicKey *key,
380                              uint64_t pow,
381                              unsigned int matching_bits)
382 {
383   char buf[sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
384            sizeof (pow)] GNUNET_ALIGN;
385   struct GNUNET_HashCode result;
386
387   GNUNET_memcpy (buf, &pow, sizeof (pow));
388   GNUNET_memcpy (&buf[sizeof (pow)], key,
389           sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
390   pow_hash (buf, sizeof (buf), &result);
391   return (count_leading_zeroes (&result) >=
392           matching_bits) ? GNUNET_YES : GNUNET_NO;
393 }
394
395
396 /**
397  * Create a revocation signature.
398  *
399  * @param key private key of the key to revoke
400  * @param sig where to write the revocation signature
401  */
402 void
403 GNUNET_REVOCATION_sign_revocation (const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
404                                    struct GNUNET_CRYPTO_EcdsaSignature *sig)
405 {
406   struct RevokeMessage rm;
407
408   rm.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_REVOCATION);
409   rm.purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
410                            sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
411   GNUNET_CRYPTO_ecdsa_key_get_public (key, &rm.public_key);
412   GNUNET_assert (GNUNET_OK ==
413                  GNUNET_CRYPTO_ecdsa_sign (key,
414                                          &rm.purpose,
415                                          sig));
416 }
417
418
419 /* end of revocation_api.c */