aboutsummaryrefslogtreecommitdiff
path: root/src/revocation/gnunet-revocation.c
blob: f5aa2d17ead811c6390935c2675618779a6f7172 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/*
     This file is part of GNUnet.
     Copyright (C) 2013 GNUnet e.V.

     GNUnet is free software: you can redistribute it and/or modify it
     under the terms of the GNU Affero General Public License as published
     by the Free Software Foundation, either version 3 of the License,
     or (at your option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     Affero General Public License for more details.

     You should have received a copy of the GNU Affero General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.

     SPDX-License-Identifier: AGPL3.0-or-later
 */

/**
 * @file revocation/gnunet-revocation.c
 * @brief tool for revoking public keys
 * @author Christian Grothoff
 */
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_revocation_service.h"
#include "gnunet_identity_service.h"


/**
 * Final status code.
 */
static int ret;

/**
 * Was "-p" specified?
 */
static int perform;

/**
 * -f option.
 */
static char *filename;

/**
 * -R option
 */
static char *revoke_ego;

/**
 * -t option.
 */
static char *test_ego;

/**
 * Handle for revocation query.
 */
static struct GNUNET_REVOCATION_Query *q;

/**
 * Handle for revocation.
 */
static struct GNUNET_REVOCATION_Handle *h;

/**
 * Handle for our ego lookup.
 */
static struct GNUNET_IDENTITY_EgoLookup *el;

/**
 * Our configuration.
 */
static const struct GNUNET_CONFIGURATION_Handle *cfg;

/**
 * Number of matching bits required for revocation.
 */
static unsigned long long matching_bits;

/**
 * Task used for proof-of-work calculation.
 */
static struct GNUNET_SCHEDULER_Task *pow_task;


/**
 * Function run if the user aborts with CTRL-C.
 *
 * @param cls closure
 */
static void
do_shutdown (void *cls)
{
  if (NULL != el)
  {
    GNUNET_IDENTITY_ego_lookup_cancel (el);
    el = NULL;
  }
  if (NULL != q)
  {
    GNUNET_REVOCATION_query_cancel (q);
    q = NULL;
  }
  if (NULL != h)
  {
    GNUNET_REVOCATION_revoke_cancel (h);
    h = NULL;
  }
}


/**
 * Print the result from a revocation query.
 *
 * @param cls NULL
 * @param is_valid #GNUNET_YES if the key is still valid, #GNUNET_NO if not, #GNUNET_SYSERR on error
 */
static void
print_query_result (void *cls, int is_valid)
{
  q = NULL;
  switch (is_valid)
  {
  case GNUNET_YES:
    fprintf (stdout, _ ("Key `%s' is valid\n"), test_ego);
    break;

  case GNUNET_NO:
    fprintf (stdout, _ ("Key `%s' has been revoked\n"), test_ego);
    break;

  case GNUNET_SYSERR:
    fprintf (stdout, "%s", _ ("Internal error\n"));
    break;

  default:
    GNUNET_break (0);
    break;
  }
  GNUNET_SCHEDULER_shutdown ();
}


/**
 * Print the result from a revocation request.
 *
 * @param cls NULL
 * @param is_valid #GNUNET_YES if the key is still valid, #GNUNET_NO if not, #GNUNET_SYSERR on error
 */
static void
print_revocation_result (void *cls, int is_valid)
{
  h = NULL;
  switch (is_valid)
  {
  case GNUNET_YES:
    if (NULL != revoke_ego)
      fprintf (stdout,
               _ ("Key for ego `%s' is still valid, revocation failed (!)\n"),
               revoke_ego);
    else
      fprintf (stdout, "%s", _ ("Revocation failed (!)\n"));
    break;

  case GNUNET_NO:
    if (NULL != revoke_ego)
      fprintf (stdout,
               _ ("Key for ego `%s' has been successfully revoked\n"),
               revoke_ego);
    else
      fprintf (stdout, "%s", _ ("Revocation successful.\n"));
    break;

  case GNUNET_SYSERR:
    fprintf (stdout,
             "%s",
             _ ("Internal error, key revocation might have failed\n"));
    break;

  default:
    GNUNET_break (0);
    break;
  }
  GNUNET_SCHEDULER_shutdown ();
}


/**
 * Data needed to perform a revocation.
 */
struct RevocationData
{
  /**
   * Public key.
   */
  struct GNUNET_CRYPTO_EcdsaPublicKey key;

  /**
   * Revocation signature data.
   */
  struct GNUNET_CRYPTO_EcdsaSignature sig;

  /**
   * Proof of work (in NBO).
   */
  uint64_t pow GNUNET_PACKED;
};


/**
 * Perform the revocation.
 */
static void
perform_revocation (const struct RevocationData *rd)
{
  h = GNUNET_REVOCATION_revoke (cfg,
                                &rd->key,
                                &rd->sig,
                                rd->pow,
                                &print_revocation_result,
                                NULL);
}


/**
 * Write the current state of the revocation data
 * to disk.
 *
 * @param rd data to sync
 */
static void
sync_rd (const struct RevocationData *rd)
{
  if ((NULL != filename) &&
      (sizeof(struct RevocationData) ==
       GNUNET_DISK_fn_write (filename,
                             &rd,
                             sizeof(rd),
                             GNUNET_DISK_PERM_USER_READ
                             | GNUNET_DISK_PERM_USER_WRITE)))
    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "write", filename);
}


/**
 * Perform the proof-of-work calculation.
 *
 * @param cls the `struct RevocationData`
 */
static void
calculate_pow_shutdown (void *cls)
{
  struct RevocationData *rd = cls;

  if (NULL != pow_task)
  {
    GNUNET_SCHEDULER_cancel (pow_task);
    pow_task = NULL;
  }
  sync_rd (rd);
  GNUNET_free (rd);
}


/**
 * Perform the proof-of-work calculation.
 *
 * @param cls the `struct RevocationData`
 */
static void
calculate_pow (void *cls)
{
  struct RevocationData *rd = cls;

  /* store temporary results */
  pow_task = NULL;
  if (0 == (rd->pow % 128))
    sync_rd (rd);
  /* display progress estimate */
  if ((0 == ((1 << matching_bits) / 100 / 50)) ||
      (0 == (rd->pow % ((1 << matching_bits) / 100 / 50))))
    fprintf (stderr, "%s", ".");
  if ((0 != rd->pow) && ((0 == ((1 << matching_bits) / 100)) ||
                         (0 == (rd->pow % ((1 << matching_bits) / 100)))))
    fprintf (stderr,
             " - @ %3u%% (estimate)\n",
             (unsigned int) (rd->pow * 100) / (1 << matching_bits));
  /* actually do POW calculation */
  rd->pow++;
  if (GNUNET_OK == GNUNET_REVOCATION_check_pow (&rd->key,
                                                rd->pow,
                                                (unsigned int) matching_bits))
  {
    if ((NULL != filename) &&
        (sizeof(struct RevocationData) !=
         GNUNET_DISK_fn_write (filename,
                               rd,
                               sizeof(struct RevocationData),
                               GNUNET_DISK_PERM_USER_READ
                               | GNUNET_DISK_PERM_USER_WRITE)))
      GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "write", filename);
    if (perform)
    {
      perform_revocation (rd);
    }
    else
    {
      fprintf (stderr, "%s", "\n");
      fprintf (stderr,
               _ ("Revocation certificate for `%s' stored in `%s'\n"),
               revoke_ego,
               filename);
      GNUNET_SCHEDULER_shutdown ();
    }
    return;
  }
  pow_task = GNUNET_SCHEDULER_add_now (&calculate_pow, rd);
}


/**
 * Function called with the result from the ego lookup.
 *
 * @param cls closure
 * @param ego the ego, NULL if not found
 */
static void
ego_callback (void *cls, const struct GNUNET_IDENTITY_Ego *ego)
{
  struct RevocationData *rd;
  struct GNUNET_CRYPTO_EcdsaPublicKey key;

  el = NULL;
  if (NULL == ego)
  {
    fprintf (stdout, _ ("Ego `%s' not found.\n"), revoke_ego);
    GNUNET_SCHEDULER_shutdown ();
    return;
  }
  GNUNET_IDENTITY_ego_get_public_key (ego, &key);
  rd = GNUNET_new (struct RevocationData);
  if ((NULL != filename) && (GNUNET_YES == GNUNET_DISK_file_test (filename)) &&
      (sizeof(struct RevocationData) ==
       GNUNET_DISK_fn_read (filename, rd, sizeof(struct RevocationData))))
  {
    if (0 != GNUNET_memcmp (&rd->key, &key))
    {
      fprintf (stderr,
               _ ("Error: revocation certificate in `%s' is not for `%s'\n"),
               filename,
               revoke_ego);
      GNUNET_free (rd);
      return;
    }
  }
  else
  {
    GNUNET_REVOCATION_sign_revocation (GNUNET_IDENTITY_ego_get_private_key (
                                         ego),
                                       &rd->sig);
    rd->key = key;
  }
  if (GNUNET_YES ==
      GNUNET_REVOCATION_check_pow (&key, rd->pow, (unsigned int) matching_bits))
  {
    fprintf (stderr, "%s", _ ("Revocation certificate ready\n"));
    if (perform)
      perform_revocation (rd);
    else
      GNUNET_SCHEDULER_shutdown ();
    GNUNET_free (rd);
    return;
  }
  fprintf (stderr,
           "%s",
           _ ("Revocation certificate not ready, calculating proof of work\n"));
  pow_task = GNUNET_SCHEDULER_add_now (&calculate_pow, rd);
  GNUNET_SCHEDULER_add_shutdown (&calculate_pow_shutdown, rd);
}


/**
 * Main function that will be run by the scheduler.
 *
 * @param cls closure
 * @param args remaining command-line arguments
 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
 * @param c configuration
 */
static void
run (void *cls,
     char *const *args,
     const char *cfgfile,
     const struct GNUNET_CONFIGURATION_Handle *c)
{
  struct GNUNET_CRYPTO_EcdsaPublicKey pk;
  struct RevocationData rd;

  cfg = c;
  if (NULL != test_ego)
  {
    if (GNUNET_OK !=
        GNUNET_CRYPTO_ecdsa_public_key_from_string (test_ego,
                                                    strlen (test_ego),
                                                    &pk))
    {
      fprintf (stderr, _ ("Public key `%s' malformed\n"), test_ego);
      return;
    }
    GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
    q = GNUNET_REVOCATION_query (cfg, &pk, &print_query_result, NULL);
    if (NULL != revoke_ego)
      fprintf (
        stderr,
        "%s",
        _ (
          "Testing and revoking at the same time is not allowed, only executing test.\n"));
    return;
  }
  if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg,
                                                          "REVOCATION",
                                                          "WORKBITS",
                                                          &matching_bits))
  {
    GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
                               "REVOCATION",
                               "WORKBITS");
    return;
  }
  if (NULL != revoke_ego)
  {
    if (! perform && (NULL == filename))
    {
      fprintf (stderr,
               "%s",
               _ ("No filename to store revocation certificate given.\n"));
      return;
    }
    /* main code here */
    el = GNUNET_IDENTITY_ego_lookup (cfg, revoke_ego, &ego_callback, NULL);
    GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
    return;
  }
  if ((NULL != filename) && (perform))
  {
    if (sizeof(rd) != GNUNET_DISK_fn_read (filename, &rd, sizeof(rd)))
    {
      fprintf (stderr,
               _ ("Failed to read revocation certificate from `%s'\n"),
               filename);
      return;
    }
    GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
    if (GNUNET_YES !=
        GNUNET_REVOCATION_check_pow (&rd.key,
                                     rd.pow,
                                     (unsigned int) matching_bits))
    {
      struct RevocationData *cp = GNUNET_new (struct RevocationData);

      *cp = rd;
      pow_task = GNUNET_SCHEDULER_add_now (&calculate_pow, cp);
      GNUNET_SCHEDULER_add_shutdown (&calculate_pow_shutdown, cp);
      return;
    }
    perform_revocation (&rd);
    return;
  }
  fprintf (stderr, "%s", _ ("No action specified. Nothing to do.\n"));
}


/**
 * The main function of gnunet-revocation.
 *
 * @param argc number of arguments from the command line
 * @param argv command line arguments
 * @return 0 ok, 1 on error
 */
int
main (int argc, char *const *argv)
{
  struct GNUNET_GETOPT_CommandLineOption options[] = {
    GNUNET_GETOPT_option_string ('f',
                                 "filename",
                                 "NAME",
                                 gettext_noop (
                                   "use NAME for the name of the revocation file"),
                                 &filename),

    GNUNET_GETOPT_option_string (
      'R',
      "revoke",
      "NAME",
      gettext_noop (
        "revoke the private key associated for the the private key associated with the ego NAME "),
      &revoke_ego),

    GNUNET_GETOPT_option_flag (
      'p',
      "perform",
      gettext_noop (
        "actually perform revocation, otherwise we just do the precomputation"),
      &perform),

    GNUNET_GETOPT_option_string ('t',
                                 "test",
                                 "KEY",
                                 gettext_noop (
                                   "test if the public key KEY has been revoked"),
                                 &test_ego),

    GNUNET_GETOPT_OPTION_END
  };

  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
    return 2;

  ret = (GNUNET_OK == GNUNET_PROGRAM_run (argc,
                                          argv,
                                          "gnunet-revocation",
                                          gettext_noop ("help text"),
                                          options,
                                          &run,
                                          NULL))
        ? ret
        : 1;
  GNUNET_free ((void *) argv);
  return ret;
}


/* end of gnunet-revocation.c */