aboutsummaryrefslogtreecommitdiff
path: root/src/identity-token/identity-token.c
blob: dc0154c21543da7ba99cafc05f2280e55471ab81 (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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
/*
      This file is part of GNUnet
      Copyright (C) 2010-2015 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 License 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 License for more details.

      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
      Boston, MA 02110-1301, USA.
 */

/**
 * @file identity-token/identity-token.c
 * @brief helper library to manage identity tokens
 * @author Martin Schanzenbach
 */
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_signatures.h"
#include "identity-token.h"
#include <jansson.h>


/**
 * Crypto helper functions
 */

static int
create_sym_key_from_ecdh(const struct GNUNET_HashCode *new_key_hash,
                         struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
                         struct GNUNET_CRYPTO_SymmetricInitializationVector *iv)
{
  struct GNUNET_CRYPTO_HashAsciiEncoded new_key_hash_str;

  GNUNET_CRYPTO_hash_to_enc (new_key_hash,
                             &new_key_hash_str);
  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Creating symmetric rsa key from %s\n", (char*)&new_key_hash_str);
  static const char ctx_key[] = "gnuid-aes-ctx-key";
  GNUNET_CRYPTO_kdf (skey, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
                     new_key_hash, sizeof (struct GNUNET_HashCode),
                     ctx_key, strlen (ctx_key),
                     NULL, 0);
  static const char ctx_iv[] = "gnuid-aes-ctx-iv";
  GNUNET_CRYPTO_kdf (iv, sizeof (struct GNUNET_CRYPTO_SymmetricInitializationVector),
                     new_key_hash, sizeof (struct GNUNET_HashCode),
                     ctx_iv, strlen (ctx_iv),
                     NULL, 0);
  return GNUNET_OK;
}



/**
 * Decrypts metainfo part from a token code
 */
static int
decrypt_str_ecdhe (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key,
                   const struct GNUNET_CRYPTO_EcdhePublicKey *ecdh_key,
                   const char *cyphertext,
                   size_t cyphertext_len,
                   char **result_str)
{
  struct GNUNET_HashCode new_key_hash;
  struct GNUNET_CRYPTO_SymmetricSessionKey enc_key;
  struct GNUNET_CRYPTO_SymmetricInitializationVector enc_iv;

  char *str_buf = GNUNET_malloc (cyphertext_len);
  size_t str_size;

  //Calculate symmetric key from ecdh parameters
  GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_ecdsa_ecdh (priv_key,
                                                        ecdh_key,
                                                        &new_key_hash));

  create_sym_key_from_ecdh (&new_key_hash,
                            &enc_key,
                            &enc_iv);

  str_size = GNUNET_CRYPTO_symmetric_decrypt (cyphertext,
                                              cyphertext_len,
                                              &enc_key,
                                              &enc_iv,
                                              str_buf);
  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Decrypted bytes: %d Expected bytes: %d\n", str_size, cyphertext_len);
  if (-1 == str_size)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ECDH invalid\n");
    GNUNET_free (str_buf);
    return GNUNET_SYSERR;
  }
  *result_str = GNUNET_malloc (str_size+1);
  memcpy (*result_str, str_buf, str_size);
  (*result_str)[str_size] = '\0';
  GNUNET_free (str_buf);
  return GNUNET_OK;

}

/**
 * Decrypt string using pubkey and ECDHE
*/
static int
decrypt_str_ecdhe2 (const struct GNUNET_CRYPTO_EcdhePrivateKey *ecdh_privkey,
                    const struct GNUNET_CRYPTO_EcdsaPublicKey *aud_key,
                    const char *ciphertext,
                    size_t ciphertext_len,
                    char **plaintext)
{
  struct GNUNET_CRYPTO_SymmetricSessionKey skey;
  struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
  struct GNUNET_HashCode new_key_hash;

  //This is true see documentation for GNUNET_CRYPTO_symmetric_encrypt
  *plaintext = GNUNET_malloc (ciphertext_len);

  // Derived key K = H(eB)
  GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_ecdh_ecdsa (ecdh_privkey,
                                                        aud_key,
                                                        &new_key_hash));
  create_sym_key_from_ecdh(&new_key_hash, &skey, &iv);
  GNUNET_CRYPTO_symmetric_decrypt (ciphertext,
                                   ciphertext_len,
                                   &skey, &iv,
                                   *plaintext);
  return GNUNET_OK;
}


/**
 * Encrypt string using pubkey and ECDHE
 * Returns ECDHE pubkey to be used for decryption
 */
static int
encrypt_str_ecdhe (const char *plaintext,
                   const struct GNUNET_CRYPTO_EcdsaPublicKey *pub_key,
                   char **cyphertext,
                   struct GNUNET_CRYPTO_EcdhePrivateKey **ecdh_privkey,
                   struct GNUNET_CRYPTO_EcdhePublicKey *ecdh_pubkey)
{
  struct GNUNET_CRYPTO_SymmetricSessionKey skey;
  struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
  struct GNUNET_HashCode new_key_hash;
  ssize_t enc_size;

  // ECDH keypair E = eG
  *ecdh_privkey = GNUNET_CRYPTO_ecdhe_key_create();
  GNUNET_CRYPTO_ecdhe_key_get_public (*ecdh_privkey,
                                      ecdh_pubkey);

  //This is true see documentation for GNUNET_CRYPTO_symmetric_encrypt
  *cyphertext = GNUNET_malloc (strlen (plaintext));

  // Derived key K = H(eB)
  GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_ecdh_ecdsa (*ecdh_privkey,
                                                        pub_key,
                                                        &new_key_hash));
  create_sym_key_from_ecdh(&new_key_hash, &skey, &iv);
  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Encrypting string %s\n (len=%d)",
              plaintext,
              strlen (plaintext));
  enc_size = GNUNET_CRYPTO_symmetric_encrypt (plaintext, strlen (plaintext),
                                              &skey, &iv,
                                              *cyphertext);
  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Encrypted (len=%d)", enc_size);
  return GNUNET_OK;
}




/**
 * Identity Token API
 */


/**
 * Create an Identity Token
 *
 * @param type the JSON API resource type
 * @param id the JSON API resource id
 * @return a new JSON API resource or NULL on error.
 */
struct IdentityToken*
identity_token_create (const char* issuer,
                       const char* audience)
{
  struct IdentityToken *token;

  token = GNUNET_malloc (sizeof (struct IdentityToken));

  token->header = json_object();
  token->payload = json_object();

  json_object_set_new (token->header, "alg", json_string ("ED512"));
  json_object_set_new (token->header, "typ", json_string ("JWT"));

  json_object_set_new (token->payload, "iss", json_string (issuer));
  json_object_set_new (token->payload, "aud", json_string (audience));

  GNUNET_STRINGS_string_to_data  (audience,
                                  strlen (audience),
                                  &token->aud_key,
                                  sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));

  return token;
}

void
identity_token_destroy (struct IdentityToken *token)
{
  json_decref (token->header);
  json_decref (token->payload);
  GNUNET_free (token);
}

void
identity_token_add_attr (const struct IdentityToken *token,
                         const char* key,
                         const char* value)
{
  GNUNET_assert (NULL != token);
  GNUNET_assert (NULL != token->payload);

  json_object_set_new (token->payload, key, json_string (value));
}

void
identity_token_add_json (const struct IdentityToken *token,
                         const char* key,
                         json_t* value)
{
  GNUNET_assert (NULL != token);
  GNUNET_assert (NULL != token->payload);

  json_object_set_new (token->payload, key, value);
}


int
identity_token_parse2 (const char* raw_data,
                       const struct GNUNET_CRYPTO_EcdhePrivateKey *priv_key,
                       const struct GNUNET_CRYPTO_EcdsaPublicKey *aud_key,
                       struct IdentityToken **result)
{
  char *enc_token_str;
  char *tmp_buf;
  char *token_str;
  char *enc_token;
  char *header;
  char *header_base64;
  char *payload;
  char *payload_base64;
  size_t enc_token_len;
  json_error_t err_json;

  GNUNET_asprintf (&tmp_buf, "%s", raw_data);
  strtok (tmp_buf, ",");
  enc_token_str = strtok (NULL, ",");

  enc_token_len = GNUNET_STRINGS_base64_decode (enc_token_str,
                                                strlen (enc_token_str),
                                                &enc_token);
  if (GNUNET_OK != decrypt_str_ecdhe2 (priv_key,
                                       aud_key,
                                       enc_token,
                                       enc_token_len,
                                       &token_str))
  {
    GNUNET_free (tmp_buf);
    GNUNET_free (enc_token);
    return GNUNET_SYSERR;
  }

  header_base64 = strtok (token_str, ".");
  payload_base64 = strtok (NULL, ".");

  GNUNET_STRINGS_base64_decode (header_base64,
                                strlen (header_base64),
                                &header);
  GNUNET_STRINGS_base64_decode (payload_base64,
                                strlen (payload_base64),
                                &payload);
  //TODO signature


  *result = GNUNET_malloc (sizeof (struct IdentityToken));
  (*result)->aud_key =  *aud_key;
  (*result)->header = json_loads (header, JSON_DECODE_ANY, &err_json);
  (*result)->payload = json_loads (payload, JSON_DECODE_ANY, &err_json);
  GNUNET_free (enc_token);
  GNUNET_free (token_str);
  GNUNET_free (tmp_buf);
  GNUNET_free (payload);
  GNUNET_free (header);
  return GNUNET_OK;
}

int
identity_token_parse (const char* raw_data,
                      const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key,
                      struct IdentityToken **result)
{
  char *ecdh_pubkey_str;
  char *enc_token_str;
  char *tmp_buf;
  char *token_str;
  char *enc_token;
  char *header;
  char *header_base64;
  char *payload;
  char *payload_base64;
  size_t enc_token_len;
  json_error_t err_json;
  struct GNUNET_CRYPTO_EcdhePublicKey ecdh_pubkey;

  GNUNET_asprintf (&tmp_buf, "%s", raw_data);
  ecdh_pubkey_str = strtok (tmp_buf, ",");
  enc_token_str = strtok (NULL, ",");

  GNUNET_STRINGS_string_to_data (ecdh_pubkey_str,
                                 strlen (ecdh_pubkey_str),
                                 &ecdh_pubkey,
                                 sizeof (struct GNUNET_CRYPTO_EcdhePublicKey));
  enc_token_len = GNUNET_STRINGS_base64_decode (enc_token_str,
                                                strlen (enc_token_str),
                                                &enc_token);
  if (GNUNET_OK != decrypt_str_ecdhe (priv_key,
                                      &ecdh_pubkey,
                                      enc_token,
                                      enc_token_len,
                                      &token_str))
  {
    GNUNET_free (tmp_buf);
    GNUNET_free (enc_token);
    return GNUNET_SYSERR;
  }

  header_base64 = strtok (token_str, ".");
  payload_base64 = strtok (NULL, ".");

  GNUNET_STRINGS_base64_decode (header_base64,
                                strlen (header_base64),
                                &header);
  GNUNET_STRINGS_base64_decode (payload_base64,
                                strlen (payload_base64),
                                &payload);
  //TODO signature and aud key


  *result = GNUNET_malloc (sizeof (struct IdentityToken));
  (*result)->header = json_loads (header, JSON_DECODE_ANY, &err_json);
  (*result)->payload = json_loads (payload, JSON_DECODE_ANY, &err_json);
  GNUNET_free (enc_token);
  GNUNET_free (token_str);
  GNUNET_free (tmp_buf);
  GNUNET_free (payload);
  GNUNET_free (header);
  return GNUNET_OK;
}

int
identity_token_to_string (const struct IdentityToken *token,
                          const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key,
                          char **result)
{
  char *payload_str;
  char *header_str;
  char *payload_base64;
  char *header_base64;
  char *padding;
  char *signature_target;
  char *signature_str;
  struct GNUNET_CRYPTO_EccSignaturePurpose *purpose;
  header_str = json_dumps (token->header, JSON_COMPACT);
  GNUNET_STRINGS_base64_encode (header_str,
                                strlen (header_str),
                                &header_base64);
  //Remove GNUNET padding of base64
  padding = strtok(header_base64, "=");
  while (NULL != padding)
    padding = strtok(NULL, "=");

  payload_str = json_dumps (token->payload, JSON_COMPACT);
  GNUNET_STRINGS_base64_encode (payload_str,
                                strlen (payload_str),
                                &payload_base64);

  //Remove GNUNET padding of base64
  padding = strtok(payload_base64, "=");
  while (NULL != padding)
    padding = strtok(NULL, "=");

  GNUNET_asprintf (&signature_target, "%s,%s", header_base64, payload_base64);
  purpose =
    GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
                   strlen (signature_target));
  purpose->size =
    htonl (strlen (signature_target) + sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose));
  purpose->purpose = htonl(GNUNET_SIGNATURE_PURPOSE_GNUID_TOKEN);
  memcpy (&purpose[1], signature_target, strlen (signature_target));
  if (GNUNET_OK != GNUNET_CRYPTO_ecdsa_sign (priv_key,
                                             purpose,
                                             (struct GNUNET_CRYPTO_EcdsaSignature *)&token->signature))
  {
    GNUNET_free (signature_target);
    GNUNET_free (payload_str);
    GNUNET_free (header_str);
    GNUNET_free (payload_base64);
    GNUNET_free (header_base64);
    GNUNET_free (purpose);
    return GNUNET_SYSERR;
  }

  GNUNET_STRINGS_base64_encode ((const char*)&token->signature,
                                sizeof (struct GNUNET_CRYPTO_EcdsaSignature),
                                &signature_str);
  GNUNET_asprintf (result, "%s.%s.%s",
                   header_base64, payload_base64, signature_str);
  GNUNET_free (signature_target);
  GNUNET_free (payload_str);
  GNUNET_free (header_str);
  GNUNET_free (signature_str);
  GNUNET_free (payload_base64);
  GNUNET_free (header_base64);
  GNUNET_free (purpose);
  return GNUNET_OK;
}

int
identity_token_serialize (const struct IdentityToken *token,
                          const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key,
                          struct GNUNET_CRYPTO_EcdhePrivateKey **ecdh_privkey,
                          char **result)
{
  char *token_str;
  char *enc_token;
  char *dh_key_str;
  char *enc_token_base64;
  struct GNUNET_CRYPTO_EcdhePublicKey ecdh_pubkey;

  GNUNET_assert (GNUNET_OK == identity_token_to_string (token,
                                                        priv_key,
                                                        &token_str));

  GNUNET_assert (GNUNET_OK == encrypt_str_ecdhe (token_str,
                                                 &token->aud_key,
                                                 &enc_token,
                                                 ecdh_privkey,
                                                 &ecdh_pubkey));
  GNUNET_STRINGS_base64_encode (enc_token,
                                strlen (token_str),
                                &enc_token_base64);
  dh_key_str = GNUNET_STRINGS_data_to_string_alloc (&ecdh_pubkey,
                                                    sizeof (struct GNUNET_CRYPTO_EcdhePublicKey));
  GNUNET_asprintf (result, "%s,%s", dh_key_str, enc_token_base64);
  GNUNET_free (dh_key_str);
  GNUNET_free (enc_token_base64);
  GNUNET_free (enc_token);
  GNUNET_free (token_str);
  return GNUNET_OK;
}

struct IdentityTokenCodePayload*
identity_token_code_payload_create (const char* nonce,
                                    const struct GNUNET_CRYPTO_EcdsaPublicKey* identity_pkey,
                                    const char* lbl_str)
{
  struct IdentityTokenCodePayload* payload;

  payload = GNUNET_malloc (sizeof (struct IdentityTokenCodePayload));
  GNUNET_asprintf (&payload->nonce, nonce, strlen (nonce));
  payload->identity_key = *identity_pkey;
  GNUNET_asprintf (&payload->label, lbl_str, strlen (lbl_str));
  return payload;
}

void
identity_token_code_payload_destroy (struct IdentityTokenCodePayload* payload)
{
  GNUNET_free (payload->nonce);
  GNUNET_free (payload->label);
  GNUNET_free (payload);
}

void
identity_token_code_payload_serialize (struct IdentityTokenCodePayload *payload,
                                       char **result)
{
  char* identity_key_str;

  identity_key_str = GNUNET_STRINGS_data_to_string_alloc (&payload->identity_key,
                                                          sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));

  GNUNET_asprintf (result, 
                   "{\"nonce\": \"%u\",\"identity\": \"%s\",\"label\": \"%s\"}",
                   payload->nonce, identity_key_str, payload->label);
  GNUNET_free (identity_key_str);

}


/**
 * Create the token code
 * The metadata is encrypted with a share ECDH derived secret using B (aud_key)
 * and e (ecdh_privkey)
 * The token_code also contains E (ecdh_pubkey) and a signature over the
 * metadata and E
 */
struct IdentityTokenCode*
identity_token_code_create (const char* nonce_str,
                            const struct GNUNET_CRYPTO_EcdsaPublicKey* identity_pkey,
                            const char* lbl_str,
                            const struct GNUNET_CRYPTO_EcdsaPublicKey *aud_key)
{
  struct IdentityTokenCode *token_code;
  struct IdentityTokenCodePayload *code_payload;

  token_code = GNUNET_malloc (sizeof (struct IdentityTokenCode));
  code_payload = identity_token_code_payload_create (nonce_str,
                                                     identity_pkey,
                                                     lbl_str);
  token_code->aud_key = *aud_key;
  token_code->payload = code_payload;


  return token_code;
}

void
identity_token_code_destroy (struct IdentityTokenCode *token_code)
{
  identity_token_code_payload_destroy (token_code->payload);
  GNUNET_free (token_code);
}

int
identity_token_code_serialize (struct IdentityTokenCode *identity_token_code,
                               const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key,
                               char **result)
{
  char *code_payload_str;
  char *enc_token_code_payload;
  char *token_code_payload_str;
  char *token_code_sig_str;
  char *token_code_str;
  char *dh_key_str;
  char *write_ptr;
  struct GNUNET_CRYPTO_EcdhePrivateKey *ecdhe_privkey;

  struct GNUNET_CRYPTO_EccSignaturePurpose *purpose;

  identity_token_code_payload_serialize (identity_token_code->payload,
                                         &code_payload_str);

  GNUNET_assert (GNUNET_OK == encrypt_str_ecdhe (code_payload_str,
                                                 &identity_token_code->aud_key,
                                                 &enc_token_code_payload,
                                                 &ecdhe_privkey,
                                                 &identity_token_code->ecdh_pubkey));

  GNUNET_free (ecdhe_privkey);

  purpose = 
    GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) + 
                   sizeof (struct GNUNET_CRYPTO_EcdhePublicKey) + //E
                   strlen (code_payload_str)); // E_K (code_str)
  purpose->size = 
    htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
           sizeof (struct GNUNET_CRYPTO_EcdhePublicKey) +
           strlen (code_payload_str));
  purpose->purpose = htonl(GNUNET_SIGNATURE_PURPOSE_GNUID_TOKEN_CODE);
  write_ptr = (char*) &purpose[1];
  memcpy (write_ptr,
          &identity_token_code->ecdh_pubkey,
          sizeof (struct GNUNET_CRYPTO_EcdhePublicKey));
  write_ptr += sizeof (struct GNUNET_CRYPTO_EcdhePublicKey);
  memcpy (write_ptr, enc_token_code_payload, strlen (code_payload_str));
  GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_ecdsa_sign (priv_key,
                                                        purpose,
                                                        &identity_token_code->signature));
  GNUNET_STRINGS_base64_encode (enc_token_code_payload,
                                strlen (code_payload_str),
                                &token_code_payload_str);
  token_code_sig_str = GNUNET_STRINGS_data_to_string_alloc (&identity_token_code->signature,
                                                            sizeof (struct GNUNET_CRYPTO_EcdsaSignature));

  dh_key_str = GNUNET_STRINGS_data_to_string_alloc (&identity_token_code->ecdh_pubkey,
                                                    sizeof (struct GNUNET_CRYPTO_EcdhePublicKey));
  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Using ECDH pubkey %s to encrypt\n", dh_key_str);
  GNUNET_asprintf (&token_code_str, "{\"meta\": \"%s\", \"ecdh\": \"%s\", \"signature\": \"%s\"}",
                   token_code_payload_str, dh_key_str, token_code_sig_str);
  GNUNET_STRINGS_base64_encode (token_code_str, strlen (token_code_str), result);
  GNUNET_free (dh_key_str);
  GNUNET_free (purpose);
  GNUNET_free (token_code_str);
  GNUNET_free (token_code_sig_str);
  GNUNET_free (code_payload_str);
  GNUNET_free (enc_token_code_payload);
  GNUNET_free (token_code_payload_str);
  return GNUNET_OK;
}

int
identity_token_code_payload_parse(const char *raw_data,
                                  ssize_t data_len,
                                  const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key,
                                  const struct GNUNET_CRYPTO_EcdhePublicKey *ecdhe_pkey,
                                  struct IdentityTokenCodePayload **result)
{
  const char* label_str;
  const char* nonce_str;
  const char* identity_key_str;

  json_t *root;
  json_t *label_json;
  json_t *identity_json;
  json_t *nonce_json;
  json_error_t err_json;
  char* meta_str;
  struct GNUNET_CRYPTO_EcdsaPublicKey id_pkey;

  if (GNUNET_OK != decrypt_str_ecdhe (priv_key,
                                      ecdhe_pkey,
                                      raw_data,
                                      data_len,
                                      &meta_str))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Metadata decryption failed\n");
    return GNUNET_SYSERR;
  }

  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Metadata: %s\n", meta_str);
  root = json_loads (meta_str, JSON_DECODE_ANY, &err_json);
  if (!root)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Error parsing metadata: %s\n", err_json.text);
    GNUNET_free (meta_str);
    return GNUNET_SYSERR;
  }

  identity_json = json_object_get (root, "identity");
  if (!json_is_string (identity_json))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Error parsing metadata: %s\n", err_json.text);
    json_decref (root);
    GNUNET_free (meta_str);
    return GNUNET_SYSERR;
  }
  identity_key_str = json_string_value (identity_json);
  GNUNET_STRINGS_string_to_data (identity_key_str,
                                 strlen (identity_key_str),
                                 &id_pkey,
                                 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));


  label_json = json_object_get (root, "label");
  if (!json_is_string (label_json))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Error parsing metadata: %s\n", err_json.text);
    json_decref (root);
    GNUNET_free (meta_str);
    return GNUNET_SYSERR;
  }

  label_str = json_string_value (label_json);
  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Found label: %s\n", label_str);

  nonce_json = json_object_get (root, "nonce");
  if (!json_is_string (label_json))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Error parsing metadata: %s\n", err_json.text);
    json_decref (root);
    GNUNET_free (meta_str);
    return GNUNET_SYSERR;
  }

  nonce_str = json_string_value (nonce_json);
  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Found nonce: %s\n", nonce_str);

  *result = identity_token_code_payload_create (nonce_str,
                                                (const struct GNUNET_CRYPTO_EcdsaPublicKey*)&id_pkey,
                                                label_str);
  GNUNET_free (meta_str);
  json_decref (root);
  return GNUNET_OK;

}

int
identity_token_code_parse (const char *raw_data,
                           const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key,
                           struct IdentityTokenCode **result)
{
  const char* enc_meta_str;
  const char* ecdh_enc_str;
  const char* signature_enc_str;

  json_t *root;
  json_t *signature_json;
  json_t *ecdh_json;
  json_t *enc_meta_json;
  json_error_t err_json;
  char* enc_meta;
  char* token_code_decoded;
  char* write_ptr;
  size_t enc_meta_len;
  struct GNUNET_CRYPTO_EccSignaturePurpose *purpose;
  struct IdentityTokenCode *token_code;
  struct IdentityTokenCodePayload *token_code_payload;

  token_code_decoded = NULL;
  GNUNET_STRINGS_base64_decode (raw_data, strlen (raw_data), &token_code_decoded);
  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Token Code: %s\n", token_code_decoded);
  root = json_loads (token_code_decoded, JSON_DECODE_ANY, &err_json);
  if (!root)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "%s\n", err_json.text);
    return GNUNET_SYSERR;
  }

  signature_json = json_object_get (root, "signature");
  ecdh_json = json_object_get (root, "ecdh");
  enc_meta_json = json_object_get (root, "meta");

  signature_enc_str = json_string_value (signature_json);
  ecdh_enc_str = json_string_value (ecdh_json);
  enc_meta_str = json_string_value (enc_meta_json);

  token_code = GNUNET_malloc (sizeof (struct IdentityTokenCode));

  if (GNUNET_OK != GNUNET_STRINGS_string_to_data (ecdh_enc_str,
                                                  strlen (ecdh_enc_str),
                                                  &token_code->ecdh_pubkey,
                                                  sizeof  (struct GNUNET_CRYPTO_EcdhePublicKey)))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ECDH PKEY %s invalid in metadata\n", ecdh_enc_str);
    json_decref (root);
    GNUNET_free (token_code);
    return GNUNET_SYSERR;
  }
  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Using ECDH pubkey %s for metadata decryption\n", ecdh_enc_str);
  if (GNUNET_OK != GNUNET_STRINGS_string_to_data (signature_enc_str,
                                                  strlen (signature_enc_str),
                                                  &token_code->signature,
                                                  sizeof (struct GNUNET_CRYPTO_EcdsaSignature)))
  {
    json_decref (root);
    GNUNET_free (token_code_decoded);
    GNUNET_free (token_code);
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ECDH signature invalid in metadata\n");
    return GNUNET_SYSERR;
  }

  enc_meta_len = GNUNET_STRINGS_base64_decode (enc_meta_str,
                                               strlen (enc_meta_str),
                                               &enc_meta);


  identity_token_code_payload_parse (enc_meta,
                                     enc_meta_len,
                                     priv_key,
                                     (const struct GNUNET_CRYPTO_EcdhePublicKey*)&token_code->ecdh_pubkey,
                                     &token_code_payload);

  token_code->payload = token_code_payload;
  //TODO: check signature here
  purpose = 
    GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) + 
                   sizeof (struct GNUNET_CRYPTO_EcdhePublicKey) + //E
                   enc_meta_len); // E_K (code_str)
  purpose->size = 
    htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
           sizeof (struct GNUNET_CRYPTO_EcdhePublicKey) +
           enc_meta_len);
  purpose->purpose = htonl(GNUNET_SIGNATURE_PURPOSE_GNUID_TOKEN_CODE);
  write_ptr = (char*) &purpose[1];
  memcpy (write_ptr, &token_code->ecdh_pubkey, sizeof (struct GNUNET_CRYPTO_EcdhePublicKey));
  write_ptr += sizeof (struct GNUNET_CRYPTO_EcdhePublicKey);
  memcpy (write_ptr, enc_meta, enc_meta_len);

  if (GNUNET_OK != GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_GNUID_TOKEN_CODE,
                                               purpose,
                                               &token_code->signature,
                                               &token_code_payload->identity_key))
  {
    identity_token_code_destroy (token_code);
    GNUNET_free (token_code_decoded);
    json_decref (root);
    GNUNET_free (purpose);
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Error verifying signature for token code\n");
    return GNUNET_SYSERR;
  }
  *result = token_code;
  GNUNET_free (purpose);

  GNUNET_free (enc_meta);
  GNUNET_free (token_code_decoded);
  json_decref (root);
  return GNUNET_OK;

}



/* end of identity-token.c */