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 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.
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.
17 * @file revocation/gnunet-revocation.c
18 * @brief tool for revoking public keys
19 * @author Christian Grothoff
22 #include "gnunet_util_lib.h"
23 #include "gnunet_revocation_service.h"
24 #include "gnunet_identity_service.h"
40 static char *filename;
45 static char *revoke_ego;
50 static char *test_ego;
53 * Handle for revocation query.
55 static struct GNUNET_REVOCATION_Query *q;
58 * Handle for revocation.
60 static struct GNUNET_REVOCATION_Handle *h;
63 * Handle for our ego lookup.
65 static struct GNUNET_IDENTITY_EgoLookup *el;
70 static const struct GNUNET_CONFIGURATION_Handle *cfg;
73 * Number of matching bits required for revocation.
75 static unsigned long long matching_bits;
78 * Task used for proof-of-work calculation.
80 static struct GNUNET_SCHEDULER_Task *pow_task;
84 * Function run if the user aborts with CTRL-C.
89 do_shutdown (void *cls)
93 GNUNET_IDENTITY_ego_lookup_cancel (el);
98 GNUNET_REVOCATION_query_cancel (q);
103 GNUNET_REVOCATION_revoke_cancel (h);
110 * Print the result from a revocation query.
113 * @param is_valid #GNUNET_YES if the key is still valid, #GNUNET_NO if not, #GNUNET_SYSERR on error
116 print_query_result (void *cls,
124 _("Key `%s' is valid\n"),
129 _("Key `%s' has been revoked\n"),
135 _("Internal error\n"));
141 GNUNET_SCHEDULER_shutdown ();
146 * Print the result from a revocation request.
149 * @param is_valid #GNUNET_YES if the key is still valid, #GNUNET_NO if not, #GNUNET_SYSERR on error
152 print_revocation_result (void *cls,
159 if (NULL != revoke_ego)
161 _("Key for ego `%s' is still valid, revocation failed (!)\n"),
166 _("Revocation failed (!)\n"));
169 if (NULL != revoke_ego)
171 _("Key for ego `%s' has been successfully revoked\n"),
176 _("Revocation successful.\n"));
181 _("Internal error, key revocation might have failed\n"));
187 GNUNET_SCHEDULER_shutdown ();
192 * Data needed to perform a revocation.
194 struct RevocationData
199 struct GNUNET_CRYPTO_EcdsaPublicKey key;
202 * Revocation signature data.
204 struct GNUNET_CRYPTO_EcdsaSignature sig;
207 * Proof of work (in NBO).
209 uint64_t pow GNUNET_PACKED;
214 * Perform the revocation.
217 perform_revocation (const struct RevocationData *rd)
219 h = GNUNET_REVOCATION_revoke (cfg,
223 &print_revocation_result,
229 * Write the current state of the revocation data
232 * @param rd data to sync
235 sync_rd (const struct RevocationData *rd)
237 if ( (NULL != filename) &&
238 (sizeof (struct RevocationData) ==
239 GNUNET_DISK_fn_write (filename,
242 GNUNET_DISK_PERM_USER_READ |
243 GNUNET_DISK_PERM_USER_WRITE)) )
244 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
251 * Perform the proof-of-work calculation.
253 * @param cls the `struct RevocationData`
256 calculate_pow_shutdown (void *cls)
258 struct RevocationData *rd = cls;
260 if (NULL != pow_task)
262 GNUNET_SCHEDULER_cancel (pow_task);
271 * Perform the proof-of-work calculation.
273 * @param cls the `struct RevocationData`
276 calculate_pow (void *cls)
278 struct RevocationData *rd = cls;
280 /* store temporary results */
282 if (0 == (rd->pow % 128))
284 /* display progress estimate */
285 if ( (0 == ((1 << matching_bits) / 100 / 50)) ||
286 (0 == (rd->pow % ((1 << matching_bits) / 100 / 50))) )
287 FPRINTF (stderr, "%s", ".");
288 if ( (0 != rd->pow) &&
289 ( (0 == ((1 << matching_bits) / 100)) ||
290 (0 == (rd->pow % ((1 << matching_bits) / 100))) ) )
291 FPRINTF (stderr, " - @ %3u%% (estimate)\n",
292 (unsigned int) (rd->pow * 100) / (1 << matching_bits));
293 /* actually do POW calculation */
296 GNUNET_REVOCATION_check_pow (&rd->key,
298 (unsigned int) matching_bits))
300 if ( (NULL != filename) &&
301 (sizeof (struct RevocationData) !=
302 GNUNET_DISK_fn_write (filename,
304 sizeof (struct RevocationData),
305 GNUNET_DISK_PERM_USER_READ |
306 GNUNET_DISK_PERM_USER_WRITE)) )
307 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
312 perform_revocation (rd);
316 FPRINTF (stderr, "%s", "\n");
318 _("Revocation certificate for `%s' stored in `%s'\n"),
321 GNUNET_SCHEDULER_shutdown ();
325 pow_task = GNUNET_SCHEDULER_add_now (&calculate_pow,
331 * Function called with the result from the ego lookup.
334 * @param ego the ego, NULL if not found
337 ego_callback (void *cls,
338 const struct GNUNET_IDENTITY_Ego *ego)
340 struct RevocationData *rd;
341 struct GNUNET_CRYPTO_EcdsaPublicKey key;
347 _("Ego `%s' not found.\n"),
349 GNUNET_SCHEDULER_shutdown ();
352 GNUNET_IDENTITY_ego_get_public_key (ego,
354 rd = GNUNET_new (struct RevocationData);
355 if ( (NULL != filename) &&
357 GNUNET_DISK_file_test (filename)) &&
358 (sizeof (struct RevocationData) ==
359 GNUNET_DISK_fn_read (filename,
361 sizeof (struct RevocationData))) )
363 if (0 != memcmp (&rd->key,
365 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
368 _("Error: revocation certificate in `%s' is not for `%s'\n"),
377 GNUNET_REVOCATION_sign_revocation (GNUNET_IDENTITY_ego_get_private_key (ego),
382 GNUNET_REVOCATION_check_pow (&key,
384 (unsigned int) matching_bits))
388 _("Revocation certificate ready\n"));
390 perform_revocation (rd);
392 GNUNET_SCHEDULER_shutdown ();
398 _("Revocation certificate not ready, calculating proof of work\n"));
399 pow_task = GNUNET_SCHEDULER_add_now (&calculate_pow,
401 GNUNET_SCHEDULER_add_shutdown (&calculate_pow_shutdown,
407 * Main function that will be run by the scheduler.
410 * @param args remaining command-line arguments
411 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
412 * @param c configuration
418 const struct GNUNET_CONFIGURATION_Handle *c)
420 struct GNUNET_CRYPTO_EcdsaPublicKey pk;
421 struct RevocationData rd;
424 if (NULL != test_ego)
427 GNUNET_CRYPTO_ecdsa_public_key_from_string (test_ego,
432 _("Public key `%s' malformed\n"),
436 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
438 q = GNUNET_REVOCATION_query (cfg,
442 if (NULL != revoke_ego)
445 _("Testing and revoking at the same time is not allowed, only executing test.\n"));
449 GNUNET_CONFIGURATION_get_value_number (cfg,
454 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
459 if (NULL != revoke_ego)
461 if ( !perform && (NULL == filename) )
465 _("No filename to store revocation certificate given.\n"));
469 el = GNUNET_IDENTITY_ego_lookup (cfg,
473 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
477 if ( (NULL != filename) &&
481 GNUNET_DISK_fn_read (filename,
486 _("Failed to read revocation certificate from `%s'\n"),
490 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
493 GNUNET_REVOCATION_check_pow (&rd.key,
495 (unsigned int) matching_bits))
497 struct RevocationData *cp = GNUNET_new (struct RevocationData);
500 pow_task = GNUNET_SCHEDULER_add_now (&calculate_pow,
502 GNUNET_SCHEDULER_add_shutdown (&calculate_pow_shutdown,
506 perform_revocation (&rd);
511 _("No action specified. Nothing to do.\n"));
516 * The main function of gnunet-revocation.
518 * @param argc number of arguments from the command line
519 * @param argv command line arguments
520 * @return 0 ok, 1 on error
523 main (int argc, char *const *argv)
525 struct GNUNET_GETOPT_CommandLineOption options[] = {
527 GNUNET_GETOPT_option_string ('f',
530 gettext_noop ("use NAME for the name of the revocation file"),
533 GNUNET_GETOPT_option_string ('R',
536 gettext_noop ("revoke the private key associated for the the private key associated with the ego NAME "),
539 GNUNET_GETOPT_option_flag ('p',
541 gettext_noop ("actually perform revocation, otherwise we just do the precomputation"),
544 GNUNET_GETOPT_option_string ('t',
547 gettext_noop ("test if the public key KEY has been revoked"),
550 GNUNET_GETOPT_OPTION_END
552 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
556 GNUNET_PROGRAM_run (argc, argv, "gnunet-revocation",
557 gettext_noop ("help text"), options, &run,
559 GNUNET_free ((void*) argv);
563 /* end of gnunet-revocation.c */