fix sign api for to address #6164
[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      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 /**
21  * @file revocation/revocation_api.c
22  * @brief API to perform and access key revocations
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_revocation_service.h"
27 #include "gnunet_signatures.h"
28 #include "gnunet_protocols.h"
29 #include "revocation.h"
30 #include <gcrypt.h>
31
32
33 /**
34  * Handle for the key revocation query.
35  */
36 struct GNUNET_REVOCATION_Query
37 {
38   /**
39    * Message queue to the service.
40    */
41   struct GNUNET_MQ_Handle *mq;
42
43   /**
44    * Function to call with the result.
45    */
46   GNUNET_REVOCATION_Callback func;
47
48   /**
49    * Closure for @e func.
50    */
51   void *func_cls;
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    * Message queue to the service.
172    */
173   struct GNUNET_MQ_Handle *mq;
174
175   /**
176    * Function to call once we are done.
177    */
178   GNUNET_REVOCATION_Callback func;
179
180   /**
181    * Closure for @e func.
182    */
183   void *func_cls;
184 };
185
186
187 /**
188  * Generic error handler, called with the appropriate
189  * error code and the same closure specified at the creation of
190  * the message queue.
191  * Not every message queue implementation supports an error handler.
192  *
193  * @param cls closure with the `struct GNUNET_NSE_Handle *`
194  * @param error error code
195  */
196 static void
197 revocation_mq_error_handler (void *cls,
198                              enum GNUNET_MQ_Error error)
199 {
200   struct GNUNET_REVOCATION_Handle *h = cls;
201
202   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
203               "Revocation MQ error\n");
204   h->func (h->func_cls,
205            GNUNET_SYSERR);
206   GNUNET_REVOCATION_revoke_cancel (h);
207 }
208
209
210 /**
211  * Handle response to our revocation query.
212  *
213  * @param cls our `struct GNUNET_REVOCATION_Handle` handle
214  * @param rrm response we got
215  */
216 static void
217 handle_revocation_response (void *cls,
218                             const struct RevocationResponseMessage *rrm)
219 {
220   struct GNUNET_REVOCATION_Handle *h = cls;
221
222   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
223               "Revocation transmission result: %d\n",
224               (uint32_t) ntohl (rrm->is_valid));
225   h->func (h->func_cls,
226            ntohl (rrm->is_valid));
227   GNUNET_REVOCATION_revoke_cancel (h);
228 }
229
230
231 /**
232  * Perform key revocation.
233  *
234  * @param cfg the configuration to use
235  * @param key public key of the key to revoke
236  * @param sig signature to use on the revocation (should have been
237  *            created using #GNUNET_REVOCATION_sign_revocation).
238  * @param pow proof of work to use (should have been created by
239  *            iteratively calling #GNUNET_REVOCATION_check_pow)
240  * @param func funtion to call with the result of the check
241  *             (called with `is_valid` being #GNUNET_NO if
242  *              the revocation worked).
243  * @param func_cls closure to pass to @a func
244  * @return handle to use in #GNUNET_REVOCATION_revoke_cancel to stop REVOCATION from invoking the callback
245  */
246 struct GNUNET_REVOCATION_Handle *
247 GNUNET_REVOCATION_revoke (const struct GNUNET_CONFIGURATION_Handle *cfg,
248                           const struct GNUNET_CRYPTO_EcdsaPublicKey *key,
249                           const struct GNUNET_CRYPTO_EcdsaSignature *sig,
250                           uint64_t pow,
251                           GNUNET_REVOCATION_Callback func,
252                           void *func_cls)
253 {
254   struct GNUNET_REVOCATION_Handle *h
255     = GNUNET_new (struct GNUNET_REVOCATION_Handle);
256   struct GNUNET_MQ_MessageHandler handlers[] = {
257     GNUNET_MQ_hd_fixed_size (revocation_response,
258                              GNUNET_MESSAGE_TYPE_REVOCATION_REVOKE_RESPONSE,
259                              struct RevocationResponseMessage,
260                              h),
261     GNUNET_MQ_handler_end ()
262   };
263   unsigned long long matching_bits;
264   struct RevokeMessage *rm;
265   struct GNUNET_MQ_Envelope *env;
266
267   if ((GNUNET_OK ==
268        GNUNET_CONFIGURATION_get_value_number (cfg,
269                                               "REVOCATION",
270                                               "WORKBITS",
271                                               &matching_bits)) &&
272       (GNUNET_YES !=
273        GNUNET_REVOCATION_check_pow (key,
274                                     pow,
275                                     (unsigned int) matching_bits)))
276   {
277     GNUNET_break (0);
278     GNUNET_free (h);
279     return NULL;
280   }
281
282   h->mq = GNUNET_CLIENT_connect (cfg,
283                                  "revocation",
284                                  handlers,
285                                  &revocation_mq_error_handler,
286                                  h);
287   if (NULL == h->mq)
288   {
289     GNUNET_free (h);
290     return NULL;
291   }
292   h->func = func;
293   h->func_cls = func_cls;
294   env = GNUNET_MQ_msg (rm,
295                        GNUNET_MESSAGE_TYPE_REVOCATION_REVOKE);
296   rm->reserved = htonl (0);
297   rm->proof_of_work = pow;
298   rm->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_REVOCATION);
299   rm->purpose.size = htonl (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)
300                             + sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey));
301   rm->public_key = *key;
302   rm->signature = *sig;
303   GNUNET_MQ_send (h->mq,
304                   env);
305   return h;
306 }
307
308
309 /**
310  * Cancel key revocation.
311  *
312  * @param h operation to cancel
313  */
314 void
315 GNUNET_REVOCATION_revoke_cancel (struct GNUNET_REVOCATION_Handle *h)
316 {
317   if (NULL != h->mq)
318   {
319     GNUNET_MQ_destroy (h->mq);
320     h->mq = NULL;
321   }
322   GNUNET_free (h);
323 }
324
325
326 /**
327  * Count the leading zeroes in hash.
328  *
329  * @param hash to count leading zeros in
330  * @return the number of leading zero bits.
331  */
332 static unsigned int
333 count_leading_zeroes (const struct GNUNET_HashCode *hash)
334 {
335   unsigned int hash_count;
336
337   hash_count = 0;
338   while ((0 == GNUNET_CRYPTO_hash_get_bit (hash, hash_count)))
339     hash_count++;
340   return hash_count;
341 }
342
343
344 /**
345  * Check if the given proof-of-work value
346  * would be acceptable for revoking the given key.
347  *
348  * @param key key to check for
349  * @param pow proof of work value
350  * @param matching_bits how many bits must match (configuration)
351  * @return #GNUNET_YES if the @a pow is acceptable, #GNUNET_NO if not
352  */
353 int
354 GNUNET_REVOCATION_check_pow (const struct GNUNET_CRYPTO_EcdsaPublicKey *key,
355                              uint64_t pow,
356                              unsigned int matching_bits)
357 {
358   char buf[sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey)
359            + sizeof(pow)] GNUNET_ALIGN;
360   struct GNUNET_HashCode result;
361
362   GNUNET_memcpy (buf, &pow, sizeof(pow));
363   GNUNET_memcpy (&buf[sizeof(pow)], key,
364                  sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey));
365   GNUNET_CRYPTO_pow_hash ("gnunet-revocation-proof-of-work",
366                           buf,
367                           sizeof(buf),
368                           &result);
369   return (count_leading_zeroes (&result) >=
370           matching_bits) ? GNUNET_YES : GNUNET_NO;
371 }
372
373
374 /**
375  * Create a revocation signature.
376  *
377  * @param key private key of the key to revoke
378  * @param sig where to write the revocation signature
379  */
380 void
381 GNUNET_REVOCATION_sign_revocation (const struct
382                                    GNUNET_CRYPTO_EcdsaPrivateKey *key,
383                                    struct GNUNET_CRYPTO_EcdsaSignature *sig)
384 {
385   struct RevokeMessage rm;
386
387   rm.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_REVOCATION);
388   rm.purpose.size = htonl (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)
389                            + sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey));
390   GNUNET_CRYPTO_ecdsa_key_get_public (key, &rm.public_key);
391   GNUNET_assert (GNUNET_OK ==
392                  GNUNET_CRYPTO_ecdsa_sign_ (key,
393                                             &rm.purpose,
394                                             sig));
395 }
396
397
398 /* end of revocation_api.c */