2 This file is part of GNUnet.
3 Copyright (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_IDENTITY_ego_lookup_cancel (el);
100 GNUNET_REVOCATION_query_cancel (q);
105 GNUNET_REVOCATION_revoke_cancel (h);
112 * Print the result from a revocation query.
115 * @param is_valid #GNUNET_YES if the key is still valid, #GNUNET_NO if not, #GNUNET_SYSERR on error
118 print_query_result (void *cls,
126 _("Key `%s' is valid\n"),
131 _("Key `%s' has been revoked\n"),
137 _("Internal error\n"));
143 GNUNET_SCHEDULER_shutdown ();
148 * Print the result from a revocation request.
151 * @param is_valid #GNUNET_YES if the key is still valid, #GNUNET_NO if not, #GNUNET_SYSERR on error
154 print_revocation_result (void *cls,
161 if (NULL != revoke_ego)
163 _("Key for ego `%s' is still valid, revocation failed (!)\n"),
168 _("Revocation failed (!)\n"));
171 if (NULL != revoke_ego)
173 _("Key for ego `%s' has been successfully revoked\n"),
178 _("Revocation successful.\n"));
183 _("Internal error, key revocation might have failed\n"));
189 GNUNET_SCHEDULER_shutdown ();
194 * Data needed to perform a revocation.
196 struct RevocationData
201 struct GNUNET_CRYPTO_EcdsaPublicKey key;
204 * Revocation signature data.
206 struct GNUNET_CRYPTO_EcdsaSignature sig;
209 * Proof of work (in NBO).
211 uint64_t pow GNUNET_PACKED;
216 * Perform the revocation.
219 perform_revocation (const struct RevocationData *rd)
221 h = GNUNET_REVOCATION_revoke (cfg,
225 &print_revocation_result,
231 * Perform the proof-of-work calculation.
233 * @param cls the `struct RevocationData`
234 * @param tc scheduler context
237 calculate_pow (void *cls,
238 const struct GNUNET_SCHEDULER_TaskContext *tc)
240 struct RevocationData *rd = cls;
242 /* store temporary results */
243 if ( (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason)) ||
244 (0 == (rd->pow % 128) ) )
246 if ( (NULL != filename) &&
247 (sizeof (struct RevocationData) ==
248 GNUNET_DISK_fn_write (filename,
251 GNUNET_DISK_PERM_USER_READ |
252 GNUNET_DISK_PERM_USER_WRITE)) )
253 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
257 /* display progress estimate */
258 if ( (0 == ((1 << matching_bits) / 100 / 50)) ||
259 (0 == (rd->pow % ((1 << matching_bits) / 100 / 50))) )
260 FPRINTF (stderr, "%s", ".");
261 if ( (0 != rd->pow) &&
262 ( (0 == ((1 << matching_bits) / 100)) ||
263 (0 == (rd->pow % ((1 << matching_bits) / 100))) ) )
264 FPRINTF (stderr, " - @ %3u%% (estimate)\n",
265 (unsigned int) (rd->pow * 100) / (1 << matching_bits));
266 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
271 /* actually do POW calculation */
274 GNUNET_REVOCATION_check_pow (&rd->key,
276 (unsigned int) matching_bits))
278 if ( (NULL != filename) &&
279 (sizeof (struct RevocationData) !=
280 GNUNET_DISK_fn_write (filename,
282 sizeof (struct RevocationData),
283 GNUNET_DISK_PERM_USER_READ |
284 GNUNET_DISK_PERM_USER_WRITE)) )
285 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
289 perform_revocation (rd);
292 FPRINTF (stderr, "%s", "\n");
294 _("Revocation certificate for `%s' stored in `%s'\n"),
297 GNUNET_SCHEDULER_shutdown ();
302 GNUNET_SCHEDULER_add_now (&calculate_pow,
308 * Function called with the result from the ego lookup.
311 * @param ego the ego, NULL if not found
314 ego_callback (void *cls,
315 const struct GNUNET_IDENTITY_Ego *ego)
317 struct RevocationData *rd;
318 struct GNUNET_CRYPTO_EcdsaPublicKey key;
324 _("Ego `%s' not found.\n"),
326 GNUNET_SCHEDULER_shutdown ();
329 GNUNET_IDENTITY_ego_get_public_key (ego,
331 rd = GNUNET_new (struct RevocationData);
332 if ( (NULL != filename) &&
334 GNUNET_DISK_file_test (filename)) &&
335 (sizeof (struct RevocationData) ==
336 GNUNET_DISK_fn_read (filename,
338 sizeof (struct RevocationData))) )
340 if (0 != memcmp (&rd->key,
342 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
345 _("Error: revocation certificate in `%s' is not for `%s'\n"),
354 GNUNET_REVOCATION_sign_revocation (GNUNET_IDENTITY_ego_get_private_key (ego),
359 GNUNET_REVOCATION_check_pow (&key,
361 (unsigned int) matching_bits))
365 _("Revocation certificate ready\n"));
367 perform_revocation (rd);
369 GNUNET_SCHEDULER_shutdown ();
375 _("Revocation certificate not ready, calculating proof of work\n"));
376 GNUNET_SCHEDULER_add_now (&calculate_pow,
382 * Main function that will be run by the scheduler.
385 * @param args remaining command-line arguments
386 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
387 * @param c configuration
393 const struct GNUNET_CONFIGURATION_Handle *c)
395 struct GNUNET_CRYPTO_EcdsaPublicKey pk;
396 struct RevocationData rd;
399 if (NULL != test_ego)
402 GNUNET_CRYPTO_ecdsa_public_key_from_string (test_ego,
407 _("Public key `%s' malformed\n"),
411 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
414 q = GNUNET_REVOCATION_query (cfg,
418 if (NULL != revoke_ego)
421 _("Testing and revoking at the same time is not allowed, only executing test.\n"));
425 GNUNET_CONFIGURATION_get_value_number (cfg,
430 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
435 if (NULL != revoke_ego)
437 if ( !perform && (NULL == filename) )
441 _("No filename to store revocation certificate given.\n"));
445 el = GNUNET_IDENTITY_ego_lookup (cfg,
449 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
454 if ( (NULL != filename) &&
458 GNUNET_DISK_fn_read (filename,
463 _("Failed to read revocation certificate from `%s'\n"),
467 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
471 GNUNET_REVOCATION_check_pow (&rd.key,
473 (unsigned int) matching_bits))
475 struct RevocationData *cp = GNUNET_new (struct RevocationData);
478 GNUNET_SCHEDULER_add_now (&calculate_pow,
483 perform_revocation (&rd);
488 _("No action specified. Nothing to do.\n"));
493 * The main function of gnunet-revocation.
495 * @param argc number of arguments from the command line
496 * @param argv command line arguments
497 * @return 0 ok, 1 on error
500 main (int argc, char *const *argv)
502 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
503 {'f', "filename", "NAME",
504 gettext_noop ("use NAME for the name of the revocation file"),
505 1, &GNUNET_GETOPT_set_string, &filename},
506 {'R', "revoke", "NAME",
507 gettext_noop ("revoke the private key associated for the the private key associated with the ego NAME "),
508 1, &GNUNET_GETOPT_set_string, &revoke_ego},
509 {'p', "perform", NULL,
510 gettext_noop ("actually perform revocation, otherwise we just do the precomputation"),
511 0, &GNUNET_GETOPT_set_one, &perform},
513 gettext_noop ("test if the public key KEY has been revoked"),
514 1, &GNUNET_GETOPT_set_string, &test_ego},
515 GNUNET_GETOPT_OPTION_END
517 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
521 GNUNET_PROGRAM_run (argc, argv, "gnunet-revocation",
522 gettext_noop ("help text"), options, &run,
524 GNUNET_free ((void*) argv);
528 /* end of gnunet-revocation.c */