aboutsummaryrefslogtreecommitdiff
path: root/src/revocation/revocation_api.c
blob: 82e905e0912d28b827b050fffcd8e4a67153b276 (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
/*
      This file is part of GNUnet
      (C) 2013 Christian Grothoff (and other contributing authors)

      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public Licerevocation as published
      by the Free Software Foundation; either version 3, 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
      General Public Licerevocation for more details.

      You should have received a copy of the GNU General Public Licerevocation
      along with GNUnet; see the file COPYING.  If not, write to the
      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
      Boston, MA 02111-1307, USA.
 */
/**
 * @file revocation/revocation_api.c
 * @brief API to perform and access key revocations
 * @author Christian Grothoff
 */
#include "platform.h"
#include "gnunet_revocation_service.h"
#include "gnunet_signatures.h"
#include "gnunet_protocols.h"
#include "revocation.h"
#include <gcrypt.h>


/**
 * Handle for the key revocation query.
 */
struct GNUNET_REVOCATION_Query
{

  /**
   * Connection to the service.
   */
  struct GNUNET_CLIENT_Connection *client;

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

  /**
   * Key to check.
   */
  struct GNUNET_CRYPTO_EcdsaPublicKey key;

  /**
   * Function to call with the result.
   */
  GNUNET_REVOCATION_Callback func;

  /**
   * Closure for @e func.
   */
  void *func_cls;

  /**
   * Transmission handle to the service.
   */
  struct GNUNET_CLIENT_TransmitHandle *th;

};


/**
 * Handle response to our revocation query.
 *
 * @param cls our `struct GNUNET_REVOCATION_Query` handle
 * @param msg response we got, NULL on disconnect
 */
static void
handle_revocation_query_response (void *cls,
                                  const struct GNUNET_MessageHeader *msg)
{
  struct GNUNET_REVOCATION_Query *q = cls;
  const struct QueryResponseMessage *qrm;

  if ( (NULL == msg) ||
       (sizeof (struct QueryResponseMessage) != ntohs (msg->size)) ||
       (GNUNET_MESSAGE_TYPE_REVOCATION_QUERY_RESPONSE != ntohs (msg->type)) )
  {
    GNUNET_break (NULL == msg);
    q->func (q->func_cls, GNUNET_SYSERR);
    GNUNET_REVOCATION_query_cancel (q);
    return;
  }
  qrm = (const struct QueryResponseMessage *) msg;
  q->func (q->func_cls, ntohl (qrm->is_valid));
  GNUNET_REVOCATION_query_cancel (q);
}


/**
 * Transmit our revocation query to the service.
 *
 * @param cls our `struct GNUNET_REVOCATION_Query` handle
 * @param size number of bytes available in @a buf
 * @param buf where to copy the query
 * @return number of bytes copied to @a buf
 */
static size_t
send_revocation_query (void *cls,
                       size_t size,
                       void *buf)
{
  struct GNUNET_REVOCATION_Query *q = cls;
  struct QueryMessage qm;

  q->th = NULL;
  if ( (NULL == buf) ||
       (sizeof (struct QueryMessage) > size) )
  {
    GNUNET_break (0);
    q->func (q->func_cls, GNUNET_SYSERR);
    GNUNET_REVOCATION_query_cancel (q);
    return 0;
  }
  qm.header.size = htons (sizeof (struct QueryMessage));
  qm.header.type = htons (GNUNET_MESSAGE_TYPE_REVOCATION_QUERY);
  qm.reserved = htonl (0);
  qm.key = q->key;
  memcpy (buf, &qm, sizeof (struct QueryMessage));
  GNUNET_CLIENT_receive (q->client,
                         &handle_revocation_query_response,
                         q,
                         GNUNET_TIME_UNIT_FOREVER_REL);
  return sizeof (struct QueryMessage);
}


/**
 * Check if a key was revoked.
 *
 * @param cfg the configuration to use
 * @param key key to check for revocation
 * @param func funtion to call with the result of the check
 * @param func_cls closure to pass to @a func
 * @return handle to use in #GNUNET_REVOCATION_query_cancel to stop REVOCATION from invoking the callback
 */
struct GNUNET_REVOCATION_Query *
GNUNET_REVOCATION_query (const struct GNUNET_CONFIGURATION_Handle *cfg,
			 const struct GNUNET_CRYPTO_EcdsaPublicKey *key,
			 GNUNET_REVOCATION_Callback func, void *func_cls)
{
  struct GNUNET_REVOCATION_Query *q;

  q = GNUNET_new (struct GNUNET_REVOCATION_Query);
  q->client = GNUNET_CLIENT_connect ("revocation", cfg);
  if (NULL == q->client)
  {
    GNUNET_break (0);
    GNUNET_free (q);
    return NULL;
  }
  q->cfg = cfg;
  q->key = *key;
  q->func = func;
  q->func_cls = func_cls;
  q->th = GNUNET_CLIENT_notify_transmit_ready (q->client,
                                               sizeof (struct QueryMessage),
                                               GNUNET_TIME_UNIT_FOREVER_REL,
                                               GNUNET_YES,
                                               &send_revocation_query,
                                               q);
  return q;
}


/**
 * Cancel key revocation check.
 *
 * @param q query to cancel
 */
void
GNUNET_REVOCATION_query_cancel (struct GNUNET_REVOCATION_Query *q)
{
  if (NULL != q->th)
  {
    GNUNET_CLIENT_notify_transmit_ready_cancel (q->th);
    q->th = NULL;
  }
  GNUNET_CLIENT_disconnect (q->client);
  GNUNET_free (q);
}


/**
 * Handle for the key revocation operation.
 */
struct GNUNET_REVOCATION_Handle
{

  /**
   * Connection to the service.
   */
  struct GNUNET_CLIENT_Connection *client;

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

  /**
   * Key to revoke.
   */
  struct GNUNET_CRYPTO_EcdsaPublicKey key;

  /**
   * Signature showing that we have the right to revoke.
   */
  struct GNUNET_CRYPTO_EcdsaSignature sig;

  /**
   * Proof of work showing that we spent enough resources to broadcast revocation.
   */
  uint64_t pow;

  /**
   * Function to call once we are done.
   */
  GNUNET_REVOCATION_Callback func;

  /**
   * Closure for @e func.
   */
  void *func_cls;

  /**
   * Transmission handle to the service.
   */
  struct GNUNET_CLIENT_TransmitHandle *th;

};


/**
 * Handle response to our revocation query.
 *
 * @param cls our `struct GNUNET_REVOCATION_Handle` handle
 * @param msg response we got, NULL on disconnect
 */
static void
handle_revocation_response (void *cls,
                            const struct GNUNET_MessageHeader *msg)
{
  struct GNUNET_REVOCATION_Handle *h = cls;
  const struct RevocationResponseMessage *rrm;

  if ( (NULL == msg) ||
       (sizeof (struct RevocationResponseMessage) != ntohs (msg->size)) ||
       (GNUNET_MESSAGE_TYPE_REVOCATION_REVOKE_RESPONSE != ntohs (msg->type)) )
  {
    GNUNET_break (NULL == msg);
    h->func (h->func_cls, GNUNET_SYSERR);
    GNUNET_REVOCATION_revoke_cancel (h);
    return;
  }
  rrm = (const struct RevocationResponseMessage *) msg;
  h->func (h->func_cls, ntohl (rrm->is_valid));
  GNUNET_REVOCATION_revoke_cancel (h);

}


/**
 * Transmit our revocation to the service.
 *
 * @param cls our `struct GNUNET_REVOCATION_Handle` handle
 * @param size number of bytes available in @a buf
 * @param buf where to copy the query
 * @return number of bytes copied to @a buf
 */
static size_t
send_revoke (void *cls,
             size_t size,
             void *buf)
{
  struct GNUNET_REVOCATION_Handle *h = cls;
  struct RevokeMessage rm;

  h->th = NULL;
  if ( (NULL == buf) ||
       (sizeof (struct RevokeMessage) > size) )
  {
    GNUNET_break (0);
    h->func (h->func_cls, GNUNET_SYSERR);
    GNUNET_REVOCATION_revoke_cancel (h);
    return 0;
  }
  rm.header.size = htons (sizeof (struct RevokeMessage));
  rm.header.type = htons (GNUNET_MESSAGE_TYPE_REVOCATION_REVOKE);
  rm.reserved = htonl (0);
  rm.proof_of_work = h->pow;
  rm.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_REVOCATION);
  rm.purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
                           sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
  rm.public_key = h->key;
  rm.signature = h->sig;
  memcpy (buf, &rm, sizeof (struct RevokeMessage));
  GNUNET_CLIENT_receive (h->client,
                         &handle_revocation_response,
                         h,
                         GNUNET_TIME_UNIT_FOREVER_REL);
  return sizeof (struct RevokeMessage);
}


/**
 * Perform key revocation.
 *
 * @param cfg the configuration to use
 * @param key public key of the key to revoke
 * @param sig signature to use on the revocation (should have been
 *            created using #GNUNET_REVOCATION_sign_revocation).
 * @param pow proof of work to use (should have been created by
 *            iteratively calling #GNUNET_REVOCATION_check_pow)
 * @param func funtion to call with the result of the check
 *             (called with `is_valid` being #GNUNET_NO if
 *              the revocation worked).
 * @param func_cls closure to pass to @a func
 * @return handle to use in #GNUNET_REVOCATION_revoke_cancel to stop REVOCATION from invoking the callback
 */
struct GNUNET_REVOCATION_Handle *
GNUNET_REVOCATION_revoke (const struct GNUNET_CONFIGURATION_Handle *cfg,
			  const struct GNUNET_CRYPTO_EcdsaPublicKey *key,
			  const struct GNUNET_CRYPTO_EcdsaSignature *sig,
			  uint64_t pow,
			  GNUNET_REVOCATION_Callback func, void *func_cls)
{
  struct GNUNET_REVOCATION_Handle *h;
  unsigned long long matching_bits;

  if ( (GNUNET_OK ==
        GNUNET_CONFIGURATION_get_value_number (cfg,
                                               "REVOCATION",
                                               "WORKBITS",
                                               &matching_bits)) &&
       (GNUNET_YES !=
        GNUNET_REVOCATION_check_pow (key, pow,
                                     (unsigned int) matching_bits)) )
  {
    GNUNET_break (0);
    return NULL;
  }
  h = GNUNET_new (struct GNUNET_REVOCATION_Handle);
  h->client = GNUNET_CLIENT_connect ("revocation", cfg);
  h->cfg = cfg;
  h->key = *key;
  h->sig = *sig;
  h->pow = pow;
  h->func = func;
  h->func_cls = func_cls;
  h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
                                               sizeof (struct RevokeMessage),
                                               GNUNET_TIME_UNIT_FOREVER_REL,
                                               GNUNET_YES,
                                               &send_revoke,
                                               h);
  return h;
}


/**
 * Cancel key revocation.
 *
 * @param h operation to cancel
 */
void
GNUNET_REVOCATION_revoke_cancel (struct GNUNET_REVOCATION_Handle *h)
{
  if (NULL != h->th)
  {
    GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
    h->th = NULL;
  }
  GNUNET_CLIENT_disconnect (h->client);
  GNUNET_free (h);
}



/**
 * Calculate the 'proof-of-work' hash (an expensive hash).
 *
 * @param buf data to hash
 * @param buf_len number of bytes in @a buf
 * @param result where to write the resulting hash
 */
static void
pow_hash (const void *buf,
	  size_t buf_len,
	  struct GNUNET_HashCode *result)
{
  GNUNET_break (0 ==
		gcry_kdf_derive (buf, buf_len,
				 GCRY_KDF_SCRYPT,
				 1 /* subalgo */,
				 "gnunet-revocation-proof-of-work",
				 strlen ("gnunet-revocation-proof-of-work"),
				 2 /* iterations; keep cost of individual op small */,
				 sizeof (struct GNUNET_HashCode), result));
}


/**
 * Count the leading zeroes in hash.
 *
 * @param hash to count leading zeros in
 * @return the number of leading zero bits.
 */
static unsigned int
count_leading_zeroes (const struct GNUNET_HashCode *hash)
{
  unsigned int hash_count;

  hash_count = 0;
  while ((0 == GNUNET_CRYPTO_hash_get_bit (hash, hash_count)))
    hash_count++;
  return hash_count;
}


/**
 * Check if the given proof-of-work value
 * would be acceptable for revoking the given key.
 *
 * @param key key to check for
 * @param pow proof of work value
 * @param matching_bits how many bits must match (configuration)
 * @return #GNUNET_YES if the @a pow is acceptable, #GNUNET_NO if not
 */
int
GNUNET_REVOCATION_check_pow (const struct GNUNET_CRYPTO_EcdsaPublicKey *key,
			     uint64_t pow,
			     unsigned int matching_bits)
{
  char buf[sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
           sizeof (pow)] GNUNET_ALIGN;
  struct GNUNET_HashCode result;

  memcpy (buf, &pow, sizeof (pow));
  memcpy (&buf[sizeof (pow)], key,
          sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
  pow_hash (buf, sizeof (buf), &result);
  return (count_leading_zeroes (&result) >=
          matching_bits) ? GNUNET_YES : GNUNET_NO;
}


/**
 * Create a revocation signature.
 *
 * @param key private key of the key to revoke
 * @param sig where to write the revocation signature
 */
void
GNUNET_REVOCATION_sign_revocation (const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
				   struct GNUNET_CRYPTO_EcdsaSignature *sig)
{
  struct RevokeMessage rm;

  rm.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_REVOCATION);
  rm.purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
			   sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
  GNUNET_CRYPTO_ecdsa_key_get_public (key, &rm.public_key);
  GNUNET_assert (GNUNET_OK ==
		 GNUNET_CRYPTO_ecdsa_sign (key,
					 &rm.purpose,
					 sig));
}


/* end of revocation_api.c */