2 This file is part of GNUnet.
3 Copyright (C) 2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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;
83 * Task used for proof-of-work calculation.
85 static struct GNUNET_SCHEDULER_Task *pow_task;
89 * Function run if the user aborts with CTRL-C.
94 do_shutdown (void *cls)
98 GNUNET_IDENTITY_ego_lookup_cancel (el);
103 GNUNET_REVOCATION_query_cancel (q);
108 GNUNET_REVOCATION_revoke_cancel (h);
115 * Print the result from a revocation query.
118 * @param is_valid #GNUNET_YES if the key is still valid, #GNUNET_NO if not, #GNUNET_SYSERR on error
121 print_query_result (void *cls,
129 _("Key `%s' is valid\n"),
134 _("Key `%s' has been revoked\n"),
140 _("Internal error\n"));
146 GNUNET_SCHEDULER_shutdown ();
151 * Print the result from a revocation request.
154 * @param is_valid #GNUNET_YES if the key is still valid, #GNUNET_NO if not, #GNUNET_SYSERR on error
157 print_revocation_result (void *cls,
164 if (NULL != revoke_ego)
166 _("Key for ego `%s' is still valid, revocation failed (!)\n"),
171 _("Revocation failed (!)\n"));
174 if (NULL != revoke_ego)
176 _("Key for ego `%s' has been successfully revoked\n"),
181 _("Revocation successful.\n"));
186 _("Internal error, key revocation might have failed\n"));
192 GNUNET_SCHEDULER_shutdown ();
197 * Data needed to perform a revocation.
199 struct RevocationData
204 struct GNUNET_CRYPTO_EcdsaPublicKey key;
207 * Revocation signature data.
209 struct GNUNET_CRYPTO_EcdsaSignature sig;
212 * Proof of work (in NBO).
214 uint64_t pow GNUNET_PACKED;
219 * Perform the revocation.
222 perform_revocation (const struct RevocationData *rd)
224 h = GNUNET_REVOCATION_revoke (cfg,
228 &print_revocation_result,
234 * Write the current state of the revocation data
237 * @param rd data to sync
240 sync_rd (const struct RevocationData *rd)
242 if ( (NULL != filename) &&
243 (sizeof (struct RevocationData) ==
244 GNUNET_DISK_fn_write (filename,
247 GNUNET_DISK_PERM_USER_READ |
248 GNUNET_DISK_PERM_USER_WRITE)) )
249 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
256 * Perform the proof-of-work calculation.
258 * @param cls the `struct RevocationData`
261 calculate_pow_shutdown (void *cls)
263 struct RevocationData *rd = cls;
265 if (NULL != pow_task)
267 GNUNET_SCHEDULER_cancel (pow_task);
276 * Perform the proof-of-work calculation.
278 * @param cls the `struct RevocationData`
281 calculate_pow (void *cls)
283 struct RevocationData *rd = cls;
285 /* store temporary results */
287 if (0 == (rd->pow % 128))
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 */
301 GNUNET_REVOCATION_check_pow (&rd->key,
303 (unsigned int) matching_bits))
305 if ( (NULL != filename) &&
306 (sizeof (struct RevocationData) !=
307 GNUNET_DISK_fn_write (filename,
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,
317 perform_revocation (rd);
321 FPRINTF (stderr, "%s", "\n");
323 _("Revocation certificate for `%s' stored in `%s'\n"),
326 GNUNET_SCHEDULER_shutdown ();
330 pow_task = GNUNET_SCHEDULER_add_now (&calculate_pow,
336 * Function called with the result from the ego lookup.
339 * @param ego the ego, NULL if not found
342 ego_callback (void *cls,
343 const struct GNUNET_IDENTITY_Ego *ego)
345 struct RevocationData *rd;
346 struct GNUNET_CRYPTO_EcdsaPublicKey key;
352 _("Ego `%s' not found.\n"),
354 GNUNET_SCHEDULER_shutdown ();
357 GNUNET_IDENTITY_ego_get_public_key (ego,
359 rd = GNUNET_new (struct RevocationData);
360 if ( (NULL != filename) &&
362 GNUNET_DISK_file_test (filename)) &&
363 (sizeof (struct RevocationData) ==
364 GNUNET_DISK_fn_read (filename,
366 sizeof (struct RevocationData))) )
368 if (0 != memcmp (&rd->key,
370 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
373 _("Error: revocation certificate in `%s' is not for `%s'\n"),
382 GNUNET_REVOCATION_sign_revocation (GNUNET_IDENTITY_ego_get_private_key (ego),
387 GNUNET_REVOCATION_check_pow (&key,
389 (unsigned int) matching_bits))
393 _("Revocation certificate ready\n"));
395 perform_revocation (rd);
397 GNUNET_SCHEDULER_shutdown ();
403 _("Revocation certificate not ready, calculating proof of work\n"));
404 pow_task = GNUNET_SCHEDULER_add_now (&calculate_pow,
406 GNUNET_SCHEDULER_add_shutdown (&calculate_pow_shutdown,
412 * Main function that will be run by the scheduler.
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
423 const struct GNUNET_CONFIGURATION_Handle *c)
425 struct GNUNET_CRYPTO_EcdsaPublicKey pk;
426 struct RevocationData rd;
429 if (NULL != test_ego)
432 GNUNET_CRYPTO_ecdsa_public_key_from_string (test_ego,
437 _("Public key `%s' malformed\n"),
441 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
443 q = GNUNET_REVOCATION_query (cfg,
447 if (NULL != revoke_ego)
450 _("Testing and revoking at the same time is not allowed, only executing test.\n"));
454 GNUNET_CONFIGURATION_get_value_number (cfg,
459 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
464 if (NULL != revoke_ego)
466 if ( !perform && (NULL == filename) )
470 _("No filename to store revocation certificate given.\n"));
474 el = GNUNET_IDENTITY_ego_lookup (cfg,
478 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
482 if ( (NULL != filename) &&
486 GNUNET_DISK_fn_read (filename,
491 _("Failed to read revocation certificate from `%s'\n"),
495 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
498 GNUNET_REVOCATION_check_pow (&rd.key,
500 (unsigned int) matching_bits))
502 struct RevocationData *cp = GNUNET_new (struct RevocationData);
505 pow_task = GNUNET_SCHEDULER_add_now (&calculate_pow,
507 GNUNET_SCHEDULER_add_shutdown (&calculate_pow_shutdown,
511 perform_revocation (&rd);
516 _("No action specified. Nothing to do.\n"));
521 * The main function of gnunet-revocation.
523 * @param argc number of arguments from the command line
524 * @param argv command line arguments
525 * @return 0 ok, 1 on error
528 main (int argc, char *const *argv)
530 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
531 {'f', "filename", "NAME",
532 gettext_noop ("use NAME for the name of the revocation file"),
533 1, &GNUNET_GETOPT_set_string, &filename},
534 {'R', "revoke", "NAME",
535 gettext_noop ("revoke the private key associated for the the private key associated with the ego NAME "),
536 1, &GNUNET_GETOPT_set_string, &revoke_ego},
537 {'p', "perform", NULL,
538 gettext_noop ("actually perform revocation, otherwise we just do the precomputation"),
539 0, &GNUNET_GETOPT_set_one, &perform},
541 gettext_noop ("test if the public key KEY has been revoked"),
542 1, &GNUNET_GETOPT_set_string, &test_ego},
543 GNUNET_GETOPT_OPTION_END
545 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
549 GNUNET_PROGRAM_run (argc, argv, "gnunet-revocation",
550 gettext_noop ("help text"), options, &run,
552 GNUNET_free ((void*) argv);
556 /* end of gnunet-revocation.c */