summaryrefslogtreecommitdiff
path: root/src/zklaim/gnunet-zklaim.c
blob: 57373ea85c270074b5ded239a43b606945686952 (plain)
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
/*
   This file is part of GNUnet.
   Copyright (C) 2012-2015 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/>.
   */
/**
 * @author Martin Schanzenbach
 * @file src/zklaim/gnunet-zklaim.c
 * @brief ZKlaim CLI
 *
 */

#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_namestore_service.h"
#include "gnunet_zklaim_service.h"
#include "gnunet_identity_service.h"
#include "gnunet_signatures.h"

/**
 * state
 */
static int init;

/**
 * return value
 */
static int ret;

/**
 * Create new ZKlaim issuer context flag
 */
static int create;

/**
 * Name of new context
 */
static char* context_name;

/**
 * Attribute names for issuer context data
 */
static char* issue_attrs;

/**
 * Attribute names for issuer context data
 */
static char* credential;

/**
 * Attribute names for issuer context data
 */
static char* prove_predicate;

/**
 * Attribute names for issuer context data
 */
static char* create_attrs;

/**
 * Ego name
 */
static char* ego_name;

/**
 * The proving key
 */
static char* pkey_fn;

/**
 * The proof to verify
 */
static char* verify_proof;


/**
 * ZKLAIM handle
 */
static struct GNUNET_ZKLAIM_Handle *zklaim_handle;

/**
 * ZKLAIM Operation
 */
static struct GNUNET_ZKLAIM_Operation *zklaim_op;

/**
 * IDENTITY handle
 */
static struct GNUNET_IDENTITY_Handle *identity_handle;

/**
 * ego private key
 */
static const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey;

/**
 * Timeout task
 */
static struct GNUNET_SCHEDULER_Task *timeout;

/**
 * Cleanup task
 */
static struct GNUNET_SCHEDULER_Task *cleanup_task;

static void
do_cleanup(void *cls)
{
  cleanup_task = NULL;
  if (NULL != timeout)
    GNUNET_SCHEDULER_cancel (timeout);
  if (NULL != zklaim_op)
    GNUNET_ZKLAIM_cancel (zklaim_op);
  if (NULL != zklaim_handle)
    GNUNET_ZKLAIM_disconnect (zklaim_handle);
  if (NULL != identity_handle)
    GNUNET_IDENTITY_disconnect (identity_handle);
}

static void
timeout_task (void *cls)
{
  timeout = NULL;
  ret = 1;
  fprintf (stderr,
           "Timeout\n");
  if (NULL == cleanup_task)
    cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
}

static void
context_create_cb (void *cls,
                   int32_t success,
                   const char* emsg)
{
  zklaim_op = NULL;
  if (GNUNET_OK == success)
    fprintf (stdout,
             "Created.\n");
  else
    fprintf(stderr,
            "Failed.\n");
  if (NULL == cleanup_task)
    cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
  return;
}

static void
issue_iter (void *cls,
            const char* attr_name,
            uint64_t *data)
{
  char *tmp;
  char *key;
  char *val;
  tmp = GNUNET_strdup (issue_attrs);
  key = strtok (tmp, "=");
  while (NULL != key)
  {
    val = strtok (NULL, ";");
    if (0 != strcmp (attr_name, key))
    {
      key = strtok (NULL, "=");
      continue;
    }
    if (1 != sscanf (val, "%lu", data))
      fprintf (stderr, 
               "Failed to fill %s with %s\n",
               key, val);
    key = strtok (NULL, "=");
  }
  GNUNET_free (tmp);
  fprintf (stdout, "Setting %s=%lu\n", attr_name, *data);
}

static void
context_cb (void *cls,
            const struct GNUNET_ZKLAIM_Context *ctx)
{
  int ret;
  size_t len;
  char* data;
  char *str;

  zklaim_op = NULL;
  if (NULL == ctx)
  {
    fprintf (stderr,
             "Context does not exist!\n");
  } 
  else
  {
    ret = GNUNET_ZKLAIM_issue_from_context ((struct GNUNET_ZKLAIM_Context*)ctx,
                                            (struct GNUNET_CRYPTO_EcdsaPrivateKey*)pkey,
                                            &issue_iter,
                                            NULL);
    if (0 != ret)
    {
      fprintf (stderr,
               "Failed (%d)\n", ret);
    }
    else 
    {
      len = GNUNET_ZKLAIM_context_serialize (ctx,
                                             &data);
      GNUNET_STRINGS_base64_encode (data,
                                    len,
                                    &str);
      fprintf (stdout,
               "%s\n", str);
      GNUNET_free (str);
    }
  }
  if (NULL == cleanup_task)
    cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
  return;

}

enum zklaim_op
op_str_to_enum (const char* op_str)
{

  if (0 == strcmp ("<", op_str))
    return zklaim_less;
  else if (0 == strcmp ("<=", op_str))
    return zklaim_less_or_eq;
  else if (0 == strcmp ("==", op_str))
    return zklaim_eq;
  else if (0 == strcmp ("<=", op_str))
    return zklaim_greater_or_eq;
  else if (0 == strcmp (">", op_str))
    return zklaim_greater;
  else if (0 == strcmp ("!=", op_str))
    return zklaim_not_eq;
  return zklaim_noop;
}

void
prove_iter (void *cls,
            const char* name,
            enum zklaim_op *zop,
            uint64_t *ref)
{
  char *tmp;
  char *attr;
  char *val;
  char *op;
  tmp = GNUNET_strdup (prove_predicate);
  attr = strtok (tmp, " ");
  while (NULL != attr)
  {
    op = strtok (NULL, " ");
    if (NULL == op)
      break;
    val = strtok (NULL, ";");
    if (NULL == val)
      break;
    if (0 != strcmp (name, attr))
    {
      attr = strtok (NULL, " ");
      continue;
    }
    *zop = op_str_to_enum (op);
    if (1 != sscanf (val, "%lu", ref))
      fprintf (stderr, 
               "Failed parse %s %s %s\n",
               attr, op, val);
    attr = strtok (NULL, " ");
  }
  GNUNET_free (tmp);

}

const char* zklaim_parse_op (enum zklaim_op e) {
  switch (e) {
    case zklaim_noop:
      return "noop";
    case zklaim_less:
      return "<";
    case zklaim_less_or_eq:
      return "<=";
    case zklaim_eq:
      return "=";
    case zklaim_greater_or_eq:
      return ">=";
    case zklaim_greater:
      return ">";
    case zklaim_not_eq:
      return "!=";
    default:
      return "enum zklaim_op: no valid value";
  }
}

void
verify_iter (void *cls,
             const char* name,
             enum zklaim_op *zop,
             uint64_t *ref)
{
  const char *op = zklaim_parse_op (*zop);
  fprintf (stdout,
           "%s %s %lu\n", name, op, *ref);
}

static void
handle_arguments ()
{
  struct GNUNET_ZKLAIM_Context *ctx;
  size_t len;
  char *data;
  char *proof_str;
  char *proof_data;
  int ret;
  size_t proof_size;

  timeout = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 60),
                                          &timeout_task,
                                          NULL);
  if (create)
  {
    fprintf (stdout,
             "Creating context...\n");
    zklaim_op = GNUNET_ZKLAIM_context_create (zklaim_handle,
                                              pkey,
                                              context_name,
                                              create_attrs,
                                              &context_create_cb,
                                              NULL);
    return;
  }
  else if (issue_attrs)
  {
    zklaim_op = GNUNET_ZKLAIM_lookup_context (zklaim_handle,
                                              context_name,
                                              pkey,
                                              &context_cb,
                                              NULL);
    return;
  }
  else if (prove_predicate)
  {
    len = GNUNET_STRINGS_base64_decode (credential,
                                        strlen (credential),
                                        (void**)&data);

    ctx = GNUNET_ZKLAIM_context_deserialize (data,
                                             len);
    fprintf (stdout, "Proving %s...", prove_predicate);
    ret = GNUNET_ZKLAIM_context_prove_with_keyfile (ctx,
                                                    pkey_fn,
                                                    &prove_iter,
                                                    NULL);
    fprintf (stdout,
             "%s\n", ret ? "failed." : "success.");
    proof_size = GNUNET_ZKLAIM_context_serialize (ctx,
                                                  &proof_data);
    GNUNET_STRINGS_base64_encode (proof_data,
                                  proof_size,
                                  &proof_str);
    fprintf (stdout,
             "Here is your proof:\n%s\n", proof_str);
    GNUNET_free (proof_str);
    GNUNET_free (proof_data);
    GNUNET_ZKLAIM_context_destroy (ctx);
  }
  else if (verify_proof)
  {
    proof_size = GNUNET_STRINGS_base64_decode (verify_proof,
                                               strlen (verify_proof),
                                               (void**)&proof_data);
    ctx = GNUNET_ZKLAIM_context_deserialize (proof_data,
                                             proof_size);
    ret = GNUNET_ZKLAIM_context_verify (ctx,
                                        &verify_iter,
                                        NULL);
    fprintf (stdout,
             "Proof is %s (%d)\n", ret ? "INVALID" : "VALID", ret);
    GNUNET_free (proof_data);
    GNUNET_ZKLAIM_context_destroy (ctx);
  }
  cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
}

static void
ego_cb (void *cls,
        struct GNUNET_IDENTITY_Ego *ego,
        void **ctx,
        const char *name)
{
  if (NULL == name) {
    if (GNUNET_YES == init) {
      init = GNUNET_NO;
      handle_arguments();
    }
    return;
  }
  if (0 != strcmp (name, ego_name))
    return;
  pkey = GNUNET_IDENTITY_ego_get_private_key (ego);
}


static void
run (void *cls,
     char *const *args,
     const char *cfgfile,
     const struct GNUNET_CONFIGURATION_Handle *c)
{
  ret = 0;
  if (NULL == ego_name)
  {
    ret = 1;
    fprintf (stderr,
             _("Ego is required\n"));
    return;
  }

  if ( (create) && (NULL == context_name) )
  {
    ret = 1;
    fprintf (stderr,
             _("Context name missing!\n"));
    return;
  }
  if ( (create) && (NULL == create_attrs) )
  {
    ret = 1;
    fprintf (stderr,
             _("Context attributes missing!\n"));
    return;
  }

  zklaim_handle = GNUNET_ZKLAIM_connect (c);
  //Get Ego
  init = GNUNET_YES;
  identity_handle = GNUNET_IDENTITY_connect (c,
                                             &ego_cb,
                                             NULL);


}


int
main(int argc, char *const argv[])
{
  struct GNUNET_GETOPT_CommandLineOption options[] = {

    GNUNET_GETOPT_option_string ('n',
                                 "name",
                                 NULL,
                                 gettext_noop ("Context name"),
                                 &context_name),
    GNUNET_GETOPT_option_string ('A',
                                 "attributes",
                                 NULL,
                                 gettext_noop ("Context attributes (comma separated)"),
                                 &create_attrs),
    GNUNET_GETOPT_option_string ('e',
                                 "ego",
                                 NULL,
                                 gettext_noop ("Ego"),
                                 &ego_name),
    GNUNET_GETOPT_option_flag ('C',
                               "create",
                               gettext_noop ("Create new issuer context"),
                               &create),
    GNUNET_GETOPT_option_string ('I',
                                 "issue",
                                 NULL,
                                 gettext_noop ("Issue a credential with the given attributes and given zklaim context"),
                                 &issue_attrs),
    GNUNET_GETOPT_option_string ('P',
                                 "predicate",
                                 NULL,
                                 gettext_noop ("Predicate to prove"),
                                 &prove_predicate),
    GNUNET_GETOPT_option_string ('R',
                                 "credential",
                                 NULL,
                                 gettext_noop ("A credential"),
                                 &credential),
    GNUNET_GETOPT_option_filename ('K',
                                   "provingkey",
                                   NULL,
                                   gettext_noop ("The proving key to use"),
                                   &pkey_fn),
    GNUNET_GETOPT_option_string ('V',
                                 "verify",
                                 NULL,
                                 gettext_noop ("Proof to verify"),
                                 &verify_proof),

    GNUNET_GETOPT_OPTION_END
  };
  if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "ct",
                                       "ct", options,
                                       &run, NULL))
    return 1;
  else
    return ret;
}