send validation challenges
[oweals/gnunet.git] / src / revocation / gnunet-revocation.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2013 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 /**
22  * @file revocation/gnunet-revocation.c
23  * @brief tool for revoking public keys
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_revocation_service.h"
29 #include "gnunet_identity_service.h"
30
31
32 /**
33  * Final status code.
34  */
35 static int ret;
36
37 /**
38  * Was "-p" specified?
39  */
40 static int perform;
41
42 /**
43  * -f option.
44  */
45 static char *filename;
46
47 /**
48  * -R option
49  */
50 static char *revoke_ego;
51
52 /**
53  * -t option.
54  */
55 static char *test_ego;
56
57 /**
58  * Handle for revocation query.
59  */
60 static struct GNUNET_REVOCATION_Query *q;
61
62 /**
63  * Handle for revocation.
64  */
65 static struct GNUNET_REVOCATION_Handle *h;
66
67 /**
68  * Handle for our ego lookup.
69  */
70 static struct GNUNET_IDENTITY_EgoLookup *el;
71
72 /**
73  * Our configuration.
74  */
75 static const struct GNUNET_CONFIGURATION_Handle *cfg;
76
77 /**
78  * Number of matching bits required for revocation.
79  */
80 static unsigned long long matching_bits;
81
82 /**
83  * Task used for proof-of-work calculation.
84  */
85 static struct GNUNET_SCHEDULER_Task *pow_task;
86
87
88 /**
89  * Function run if the user aborts with CTRL-C.
90  *
91  * @param cls closure
92  */
93 static void
94 do_shutdown (void *cls)
95 {
96   if (NULL != el)
97   {
98     GNUNET_IDENTITY_ego_lookup_cancel (el);
99     el = NULL;
100   }
101   if (NULL != q)
102   {
103     GNUNET_REVOCATION_query_cancel (q);
104     q = NULL;
105   }
106   if (NULL != h)
107   {
108     GNUNET_REVOCATION_revoke_cancel (h);
109     h = NULL;
110   }
111 }
112
113
114 /**
115  * Print the result from a revocation query.
116  *
117  * @param cls NULL
118  * @param is_valid #GNUNET_YES if the key is still valid, #GNUNET_NO if not, #GNUNET_SYSERR on error
119  */
120 static void
121 print_query_result (void *cls,
122                     int is_valid)
123 {
124   q = NULL;
125   switch (is_valid)
126   {
127   case GNUNET_YES:
128     FPRINTF (stdout,
129              _("Key `%s' is valid\n"),
130              test_ego);
131     break;
132   case GNUNET_NO:
133     FPRINTF (stdout,
134              _("Key `%s' has been revoked\n"),
135              test_ego);
136     break;
137   case GNUNET_SYSERR:
138     FPRINTF (stdout,
139              "%s",
140              _("Internal error\n"));
141     break;
142   default:
143     GNUNET_break (0);
144     break;
145   }
146   GNUNET_SCHEDULER_shutdown ();
147 }
148
149
150 /**
151  * Print the result from a revocation request.
152  *
153  * @param cls NULL
154  * @param is_valid #GNUNET_YES if the key is still valid, #GNUNET_NO if not, #GNUNET_SYSERR on error
155  */
156 static void
157 print_revocation_result (void *cls,
158                          int is_valid)
159 {
160   h = NULL;
161   switch (is_valid)
162   {
163   case GNUNET_YES:
164     if (NULL != revoke_ego)
165       FPRINTF (stdout,
166                _("Key for ego `%s' is still valid, revocation failed (!)\n"),
167                revoke_ego);
168     else
169       FPRINTF (stdout,
170                "%s",
171                _("Revocation failed (!)\n"));
172     break;
173   case GNUNET_NO:
174     if (NULL != revoke_ego)
175       FPRINTF (stdout,
176                _("Key for ego `%s' has been successfully revoked\n"),
177                revoke_ego);
178     else
179       FPRINTF (stdout,
180                "%s",
181                _("Revocation successful.\n"));
182     break;
183   case GNUNET_SYSERR:
184     FPRINTF (stdout,
185              "%s",
186              _("Internal error, key revocation might have failed\n"));
187     break;
188   default:
189     GNUNET_break (0);
190     break;
191   }
192   GNUNET_SCHEDULER_shutdown ();
193 }
194
195
196 /**
197  * Data needed to perform a revocation.
198  */
199 struct RevocationData
200 {
201   /**
202    * Public key.
203    */
204   struct GNUNET_CRYPTO_EcdsaPublicKey key;
205
206   /**
207    * Revocation signature data.
208    */
209   struct GNUNET_CRYPTO_EcdsaSignature sig;
210
211   /**
212    * Proof of work (in NBO).
213    */
214   uint64_t pow GNUNET_PACKED;
215 };
216
217
218 /**
219  * Perform the revocation.
220  */
221 static void
222 perform_revocation (const struct RevocationData *rd)
223 {
224   h = GNUNET_REVOCATION_revoke (cfg,
225                                 &rd->key,
226                                 &rd->sig,
227                                 rd->pow,
228                                 &print_revocation_result,
229                                 NULL);
230 }
231
232
233 /**
234  * Write the current state of the revocation data
235  * to disk.
236  *
237  * @param rd data to sync
238  */
239 static void
240 sync_rd (const struct RevocationData *rd)
241 {
242   if ( (NULL != filename) &&
243        (sizeof (struct RevocationData) ==
244         GNUNET_DISK_fn_write (filename,
245                               &rd,
246                               sizeof (rd),
247                               GNUNET_DISK_PERM_USER_READ |
248                               GNUNET_DISK_PERM_USER_WRITE)) )
249     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
250                               "write",
251                               filename);
252 }
253
254
255 /**
256  * Perform the proof-of-work calculation.
257  *
258  * @param cls the `struct RevocationData`
259  */
260 static void
261 calculate_pow_shutdown (void *cls)
262 {
263   struct RevocationData *rd = cls;
264
265   if (NULL != pow_task)
266   {
267     GNUNET_SCHEDULER_cancel (pow_task);
268     pow_task = NULL;
269   }
270   sync_rd (rd);
271   GNUNET_free (rd);
272 }
273
274
275 /**
276  * Perform the proof-of-work calculation.
277  *
278  * @param cls the `struct RevocationData`
279  */
280 static void
281 calculate_pow (void *cls)
282 {
283   struct RevocationData *rd = cls;
284
285   /* store temporary results */
286   pow_task = NULL;
287   if (0 == (rd->pow % 128))
288     sync_rd (rd);
289   /* display progress estimate */
290   if ( (0 == ((1 << matching_bits) / 100 / 50)) ||
291        (0 == (rd->pow % ((1 << matching_bits) / 100 / 50))) )
292     FPRINTF (stderr, "%s", ".");
293   if ( (0 != rd->pow) &&
294        ( (0 == ((1 << matching_bits) / 100)) ||
295          (0 == (rd->pow % ((1 << matching_bits) / 100))) ) )
296     FPRINTF (stderr, " - @ %3u%% (estimate)\n",
297              (unsigned int) (rd->pow * 100) / (1 << matching_bits));
298   /* actually do POW calculation */
299   rd->pow++;
300   if (GNUNET_OK ==
301       GNUNET_REVOCATION_check_pow (&rd->key,
302                                    rd->pow,
303                                    (unsigned int) matching_bits))
304   {
305     if ( (NULL != filename) &&
306          (sizeof (struct RevocationData) !=
307           GNUNET_DISK_fn_write (filename,
308                                 rd,
309                                 sizeof (struct RevocationData),
310                                 GNUNET_DISK_PERM_USER_READ |
311                                 GNUNET_DISK_PERM_USER_WRITE)) )
312       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
313                                 "write",
314                                 filename);
315     if (perform)
316     {
317       perform_revocation (rd);
318     }
319     else
320     {
321       FPRINTF (stderr, "%s", "\n");
322       FPRINTF (stderr,
323                _("Revocation certificate for `%s' stored in `%s'\n"),
324                revoke_ego,
325                filename);
326       GNUNET_SCHEDULER_shutdown ();
327     }
328     return;
329   }
330   pow_task = GNUNET_SCHEDULER_add_now (&calculate_pow,
331                                        rd);
332 }
333
334
335 /**
336  * Function called with the result from the ego lookup.
337  *
338  * @param cls closure
339  * @param ego the ego, NULL if not found
340  */
341 static void
342 ego_callback (void *cls,
343               const struct GNUNET_IDENTITY_Ego *ego)
344 {
345   struct RevocationData *rd;
346   struct GNUNET_CRYPTO_EcdsaPublicKey key;
347
348   el = NULL;
349   if (NULL == ego)
350   {
351     FPRINTF (stdout,
352              _("Ego `%s' not found.\n"),
353              revoke_ego);
354     GNUNET_SCHEDULER_shutdown ();
355     return;
356   }
357   GNUNET_IDENTITY_ego_get_public_key (ego,
358                                       &key);
359   rd = GNUNET_new (struct RevocationData);
360   if ( (NULL != filename) &&
361        (GNUNET_YES ==
362         GNUNET_DISK_file_test (filename)) &&
363        (sizeof (struct RevocationData) ==
364         GNUNET_DISK_fn_read (filename,
365                              rd,
366                              sizeof (struct RevocationData))) )
367   {
368     if (0 != memcmp (&rd->key,
369                      &key,
370                      sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
371     {
372       fprintf (stderr,
373                _("Error: revocation certificate in `%s' is not for `%s'\n"),
374                filename,
375                revoke_ego);
376       GNUNET_free (rd);
377       return;
378     }
379   }
380   else
381   {
382     GNUNET_REVOCATION_sign_revocation (GNUNET_IDENTITY_ego_get_private_key (ego),
383                                        &rd->sig);
384     rd->key = key;
385   }
386   if (GNUNET_YES ==
387       GNUNET_REVOCATION_check_pow (&key,
388                                    rd->pow,
389                                    (unsigned int) matching_bits))
390   {
391     FPRINTF (stderr,
392              "%s",
393              _("Revocation certificate ready\n"));
394     if (perform)
395       perform_revocation (rd);
396     else
397       GNUNET_SCHEDULER_shutdown ();
398     GNUNET_free (rd);
399     return;
400   }
401   FPRINTF (stderr,
402            "%s",
403            _("Revocation certificate not ready, calculating proof of work\n"));
404   pow_task = GNUNET_SCHEDULER_add_now (&calculate_pow,
405                                        rd);
406   GNUNET_SCHEDULER_add_shutdown (&calculate_pow_shutdown,
407                                  rd);
408 }
409
410
411 /**
412  * Main function that will be run by the scheduler.
413  *
414  * @param cls closure
415  * @param args remaining command-line arguments
416  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
417  * @param c configuration
418  */
419 static void
420 run (void *cls,
421      char *const *args,
422      const char *cfgfile,
423      const struct GNUNET_CONFIGURATION_Handle *c)
424 {
425   struct GNUNET_CRYPTO_EcdsaPublicKey pk;
426   struct RevocationData rd;
427
428   cfg = c;
429   if (NULL != test_ego)
430   {
431     if (GNUNET_OK !=
432         GNUNET_CRYPTO_ecdsa_public_key_from_string (test_ego,
433                                                        strlen (test_ego),
434                                                        &pk))
435     {
436       FPRINTF (stderr,
437                _("Public key `%s' malformed\n"),
438                test_ego);
439       return;
440     }
441     GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
442                                    NULL);
443     q = GNUNET_REVOCATION_query (cfg,
444                                  &pk,
445                                  &print_query_result,
446                                  NULL);
447     if (NULL != revoke_ego)
448       FPRINTF (stderr,
449                "%s",
450                _("Testing and revoking at the same time is not allowed, only executing test.\n"));
451     return;
452   }
453   if (GNUNET_OK !=
454       GNUNET_CONFIGURATION_get_value_number (cfg,
455                                              "REVOCATION",
456                                              "WORKBITS",
457                                              &matching_bits))
458   {
459     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
460                                "REVOCATION",
461                                "WORKBITS");
462     return;
463   }
464   if (NULL != revoke_ego)
465   {
466     if ( !perform && (NULL == filename) )
467     {
468         FPRINTF (stderr,
469                  "%s",
470                  _("No filename to store revocation certificate given.\n"));
471         return;
472     }
473     /* main code here */
474     el = GNUNET_IDENTITY_ego_lookup (cfg,
475                                      revoke_ego,
476                                      &ego_callback,
477                                      NULL);
478     GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
479                                    NULL);
480     return;
481   }
482   if ( (NULL != filename) &&
483        (perform) )
484   {
485     if (sizeof (rd) !=
486         GNUNET_DISK_fn_read (filename,
487                              &rd,
488                              sizeof (rd)))
489     {
490       fprintf (stderr,
491                _("Failed to read revocation certificate from `%s'\n"),
492                filename);
493       return;
494     }
495     GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
496                                    NULL);
497     if (GNUNET_YES !=
498         GNUNET_REVOCATION_check_pow (&rd.key,
499                                      rd.pow,
500                                      (unsigned int) matching_bits))
501     {
502       struct RevocationData *cp = GNUNET_new (struct RevocationData);
503
504       *cp = rd;
505       pow_task = GNUNET_SCHEDULER_add_now (&calculate_pow,
506                                            cp);
507       GNUNET_SCHEDULER_add_shutdown (&calculate_pow_shutdown,
508                                      cp);
509       return;
510     }
511     perform_revocation (&rd);
512     return;
513   }
514   FPRINTF (stderr,
515            "%s",
516            _("No action specified. Nothing to do.\n"));
517 }
518
519
520 /**
521  * The main function of gnunet-revocation.
522  *
523  * @param argc number of arguments from the command line
524  * @param argv command line arguments
525  * @return 0 ok, 1 on error
526  */
527 int
528 main (int argc, char *const *argv)
529 {
530   struct GNUNET_GETOPT_CommandLineOption options[] = {
531
532     GNUNET_GETOPT_option_string ('f',
533                                  "filename",
534                                  "NAME",
535                                  gettext_noop ("use NAME for the name of the revocation file"),
536                                  &filename),
537
538     GNUNET_GETOPT_option_string ('R',
539                                  "revoke",
540                                  "NAME",
541                                  gettext_noop ("revoke the private key associated for the the private key associated with the ego NAME "),
542                                  &revoke_ego), 
543
544     GNUNET_GETOPT_option_flag ('p',
545                                   "perform",
546                                   gettext_noop ("actually perform revocation, otherwise we just do the precomputation"),
547                                   &perform),
548
549     GNUNET_GETOPT_option_string ('t',
550                                  "test",
551                                  "KEY",
552                                  gettext_noop ("test if the public key KEY has been revoked"),
553                                  &test_ego), 
554
555     GNUNET_GETOPT_OPTION_END
556   };
557   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
558     return 2;
559
560   ret = (GNUNET_OK ==
561          GNUNET_PROGRAM_run (argc, argv, "gnunet-revocation",
562                              gettext_noop ("help text"), options, &run,
563                              NULL)) ? ret : 1;
564   GNUNET_free ((void*) argv);
565   return ret;
566 }
567
568 /* end of gnunet-revocation.c */