2 This file is part of GNUnet.
3 (C) 2013 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file revocation/gnunet-revocation.c
23 * @brief tool for revoking public keys
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_revocation_service.h"
29 #include "gnunet_identity_service.h"
45 static char *filename;
50 static char *revoke_ego;
55 static char *test_ego;
58 * Handle for revocation query.
60 static struct GNUNET_REVOCATION_Query *q;
63 * Handle for revocation.
65 static struct GNUNET_REVOCATION_Handle *h;
68 * Handle for our ego lookup.
70 static struct GNUNET_IDENTITY_EgoLookup *el;
75 static const struct GNUNET_CONFIGURATION_Handle *cfg;
78 * Number of matching bits required for revocation.
80 static unsigned long long matching_bits;
84 * Function run if the user aborts with CTRL-C.
87 * @param tc scheduler context
90 do_shutdown (void *cls,
91 const struct GNUNET_SCHEDULER_TaskContext *tc)
95 GNUNET_REVOCATION_query_cancel (q);
100 GNUNET_REVOCATION_revoke_cancel (h);
107 * Print the result from a revocation query.
110 * @param is_valid #GNUNET_YES if the key is still valid, #GNUNET_NO if not, #GNUNET_SYSERR on error
113 print_query_result (void *cls,
121 _("Key `%s' is valid\n"),
126 _("Key `%s' has been revoked\n"),
132 _("Internal error\n"));
138 GNUNET_SCHEDULER_shutdown ();
143 * Print the result from a revocation request.
146 * @param is_valid #GNUNET_YES if the key is still valid, #GNUNET_NO if not, #GNUNET_SYSERR on error
149 print_revocation_result (void *cls,
156 if (NULL != revoke_ego)
158 _("Key for ego `%s' is still valid, revocation failed (!)\n"),
163 _("Revocation failed (!)\n"));
166 if (NULL != revoke_ego)
168 _("Key for ego `%s' has been successfully revoked\n"),
173 _("Revocation successful.\n"));
178 _("Internal error, key revocation might have failed\n"));
184 GNUNET_SCHEDULER_shutdown ();
189 * Data needed to perform a revocation.
191 struct RevocationData
196 struct GNUNET_CRYPTO_EcdsaPublicKey key;
199 * Revocation signature data.
201 struct GNUNET_CRYPTO_EcdsaSignature sig;
204 * Proof of work (in NBO).
206 uint64_t pow GNUNET_PACKED;
211 * Perform the revocation.
214 perform_revocation (const struct RevocationData *rd)
216 h = GNUNET_REVOCATION_revoke (cfg,
220 &print_revocation_result,
226 * Perform the proof-of-work calculation.
228 * @param cls the `struct RevocationData`
229 * @param tc scheduler context
232 calculate_pow (void *cls,
233 const struct GNUNET_SCHEDULER_TaskContext *tc)
235 struct RevocationData *rd = cls;
237 /* store temporary results */
238 if ( (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason)) ||
239 (0 == (rd->pow % 128) ) )
241 if ( (NULL != filename) &&
242 (sizeof (struct RevocationData) ==
243 GNUNET_DISK_fn_write (filename,
246 GNUNET_DISK_PERM_USER_READ |
247 GNUNET_DISK_PERM_USER_WRITE)) )
248 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
252 /* display progress estimate */
253 if ( (0 == ((1 << matching_bits) / 100 / 50)) ||
254 (0 == (rd->pow % ((1 << matching_bits) / 100 / 50))) )
255 FPRINTF (stderr, "%s", ".");
256 if ( (0 != rd->pow) &&
257 ( (0 == ((1 << matching_bits) / 100)) ||
258 (0 == (rd->pow % ((1 << matching_bits) / 100))) ) )
259 FPRINTF (stderr, " - @ %3u%% (estimate)\n",
260 (unsigned int) (rd->pow * 100) / (1 << matching_bits));
261 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
266 /* actually do POW calculation */
269 GNUNET_REVOCATION_check_pow (&rd->key,
271 (unsigned int) matching_bits))
273 if ( (NULL != filename) &&
274 (sizeof (struct RevocationData) !=
275 GNUNET_DISK_fn_write (filename,
277 sizeof (struct RevocationData),
278 GNUNET_DISK_PERM_USER_READ |
279 GNUNET_DISK_PERM_USER_WRITE)) )
280 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
284 perform_revocation (rd);
287 FPRINTF (stderr, "%s", "\n");
289 _("Revocation certificate for `%s' stored in `%s'\n"),
292 GNUNET_SCHEDULER_shutdown ();
297 GNUNET_SCHEDULER_add_now (&calculate_pow,
303 * Function called with the result from the ego lookup.
306 * @param ego the ego, NULL if not found
309 ego_callback (void *cls,
310 const struct GNUNET_IDENTITY_Ego *ego)
312 struct RevocationData *rd;
313 struct GNUNET_CRYPTO_EcdsaPublicKey key;
319 _("Ego `%s' not found.\n"),
321 GNUNET_SCHEDULER_shutdown ();
324 GNUNET_IDENTITY_ego_get_public_key (ego,
326 rd = GNUNET_new (struct RevocationData);
327 if ( (NULL != filename) &&
329 GNUNET_DISK_file_test (filename)) &&
330 (sizeof (struct RevocationData) ==
331 GNUNET_DISK_fn_read (filename,
333 sizeof (struct RevocationData))) )
335 if (0 != memcmp (&rd->key,
337 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
340 _("Error: revocation certificate in `%s' is not for `%s'\n"),
349 GNUNET_REVOCATION_sign_revocation (GNUNET_IDENTITY_ego_get_private_key (ego),
354 GNUNET_REVOCATION_check_pow (&key,
356 (unsigned int) matching_bits))
360 _("Revocation certificate ready\n"));
362 perform_revocation (rd);
364 GNUNET_SCHEDULER_shutdown ();
370 _("Revocation certificate not ready, calculating proof of work\n"));
371 GNUNET_SCHEDULER_add_now (&calculate_pow,
377 * Main function that will be run by the scheduler.
380 * @param args remaining command-line arguments
381 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
382 * @param c configuration
388 const struct GNUNET_CONFIGURATION_Handle *c)
390 struct GNUNET_CRYPTO_EcdsaPublicKey pk;
391 struct RevocationData rd;
394 if (NULL != test_ego)
397 GNUNET_CRYPTO_ecdsa_public_key_from_string (test_ego,
402 _("Public key `%s' malformed\n"),
406 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
409 q = GNUNET_REVOCATION_query (cfg,
413 if (NULL != revoke_ego)
416 _("Testing and revoking at the same time is not allowed, only executing test.\n"));
420 GNUNET_CONFIGURATION_get_value_number (cfg,
425 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
430 if (NULL != revoke_ego)
433 el = GNUNET_IDENTITY_ego_lookup (cfg,
437 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
442 if ( (NULL != filename) &&
446 GNUNET_DISK_fn_read (filename,
451 _("Failed to read revocation certificate from `%s'\n"),
455 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
459 GNUNET_REVOCATION_check_pow (&rd.key,
461 (unsigned int) matching_bits))
463 struct RevocationData *cp = GNUNET_new (struct RevocationData);
466 GNUNET_SCHEDULER_add_now (&calculate_pow,
471 perform_revocation (&rd);
476 _("No action specified. Nothing to do.\n"));
481 * The main function of gnunet-revocation.
483 * @param argc number of arguments from the command line
484 * @param argv command line arguments
485 * @return 0 ok, 1 on error
488 main (int argc, char *const *argv)
490 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
491 {'f', "filename", "NAME",
492 gettext_noop ("use NAME for the name of the revocation file"),
493 1, &GNUNET_GETOPT_set_string, &filename},
494 {'R', "revoke", "NAME",
495 gettext_noop ("revoke the private key associated with the ego NAME "),
496 1, &GNUNET_GETOPT_set_string, &revoke_ego},
497 {'p', "perform", NULL,
498 gettext_noop ("actually perform the revocation revocation file, otherwise we just do the precomputation"),
499 0, &GNUNET_GETOPT_set_one, &perform},
501 gettext_noop ("test if the public key KEY has been revoked"),
502 1, &GNUNET_GETOPT_set_string, &test_ego},
503 GNUNET_GETOPT_OPTION_END
505 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
509 GNUNET_PROGRAM_run (argc, argv, "gnunet-revocation",
510 gettext_noop ("help text"), options, &run,
512 GNUNET_free ((void*) argv);
516 /* end of gnunet-revocation.c */