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