aboutsummaryrefslogtreecommitdiff
path: root/src/did/gnunet-did.c
blob: ad88bcaf659004d0c5bd00df6c090e719a1f25e6 (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
/*
   This file is part of GNUnet.
   Copyright (C) 2012-2021 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
 */

// TODO: Public Key in DID Docuement
// TODO: Correct Key type
// TODO: valid time when setting DID Docuement - replace - create
// TODO: uncrustify
// TODO: Unit Tests


/**
 * @author Tristan Schwieren
 * @file src/did/gnunet-did.c
 * @brief DID Method Wrapper
 *
 */
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_namestore_service.h"
#include "gnunet_gns_service.h"
#include "gnunet_gnsrecord_lib.h"
#include "jansson.h"

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

/**
 * Attribute replace
 */
static int attr_replace;

/**
 * Attribute remove
 */
static int attr_remove;

/**
 *  Attibute get
 */
static int attr_get;

/**
 * Attribute create
 */
static int attr_create;

/**
 * Attribute show
 */
static int attr_show;

/**
 * Attribute did
 */
static char *attr_did;

/**
 * Attribute did document
 */
static char *attr_didd;

/**
 * Attribute ego
 */
static char *attr_ego;

/**
 * Attribute name
 */
static char *attr_name;

/**
 * Attribute expire
 */
static char *attr_expire;

static struct GNUNET_GNS_Handle * gns_handle;
static struct GNUNET_NAMESTORE_Handle * namestore_handle;
static struct GNUNET_IDENTITY_Handle * identity_handle;
const static struct GNUNET_CONFIGURATION_Handle * my_cfg;

// TODO
// eddsa only
// safe delete the didd record - look for other with same sub
// Add a data DID Document type
// Set Record flag when storing did

/**
 * @brief Disconnect and shutdown
 * @param cls closure
 */
static void
cleanup(void * cls)
{
	GNUNET_GNS_disconnect(gns_handle);
	GNUNET_NAMESTORE_disconnect(namestore_handle);
	GNUNET_IDENTITY_disconnect(identity_handle);
	GNUNET_SCHEDULER_shutdown();
}

/**
 * @brief Callback for ego loockup of get_did_for_ego()
 * 
 * @param cls closure
 * @param ego the returned ego
 */
static void
get_did_for_ego_lookup_cb(void *cls, struct GNUNET_IDENTITY_Ego * ego)
{
  struct GNUNET_IDENTITY_PublicKey pkey; // Get Public key
	const char * pkey_str;
  char did_str[71]; // 58 + 12 + 1= 71

  if(ego == NULL) {
    printf("EGO not found\n");
    GNUNET_SCHEDULER_add_now(&cleanup, NULL);
    ret = 1;
    return;
  }

  GNUNET_IDENTITY_ego_get_public_key(ego, &pkey);

  pkey_str = GNUNET_IDENTITY_public_key_to_string(&pkey); // Convert public key to string
  sprintf(did_str, "did:reclaim:%s", pkey_str); // Convert the public key to a DID str

  printf("%s\n", did_str);
  GNUNET_SCHEDULER_add_now(&cleanup, NULL);
  ret = 0;
  return;
}

/**
 * @brief Get the DID for a given EGO
 * 
 */
static void
get_did_for_ego()
{
  if(attr_ego != NULL){
		GNUNET_IDENTITY_ego_lookup(my_cfg,
		                           attr_ego,
		                           &get_did_for_ego_lookup_cb,
		                           NULL);
  } else {
    printf("Set the EGO argument to get the DID for a given EGO\n");
    GNUNET_SCHEDULER_add_now(&cleanup, NULL);
    ret = 1;
    return;
  }
}


/**
 * @brief Get the public key from did attribute given by the user
 * 
 * @param pkey place to write the public key to
 */
static void
get_pkey_from_attr_did(struct GNUNET_IDENTITY_PublicKey * pkey)
{
	char id_str[59];

	if ((1 != (sscanf (attr_did, "did:reclaim:%58s", id_str))) ||
	    (GNUNET_OK != GNUNET_IDENTITY_public_key_from_string (id_str, pkey)))
	{
		fprintf (stderr, _ ("Invalid DID `%s'\n"), id_str);
		GNUNET_SCHEDULER_add_now(cleanup, NULL);
		ret = 1;
		return;
	}
}

/**
 * @brief GNS lookup callback. Prints the DID Document to standard out. 
 * Fails if there is more than one DID record.
 * 
 * @param cls closure 
 * @param rd_count number of records in @a rd
 * @param rd the records in the reply
 */
static void
print_did_document(
	void *cls,
	uint32_t rd_count,
	const struct GNUNET_GNSRECORD_Data *rd)
{
	if (rd_count != 1)
	{
		printf("An ego should only have one DID Document");
		GNUNET_SCHEDULER_add_now(cleanup, NULL);
		ret = 1;
		return;
	}

	printf("%s\n", (char *) rd[0].data);

	GNUNET_SCHEDULER_add_now(cleanup, NULL);
	ret = 0;
	return;
}

/**
 * @brief Resolve a DID given by the user. 
 */
static void
resolve_did_document()
{
	struct GNUNET_IDENTITY_PublicKey pkey;

	if (attr_did == NULL) {
		printf("Set DID option to resolve DID\n");
		GNUNET_SCHEDULER_add_now(cleanup, NULL);
		ret = 1;
		return;
	}

	get_pkey_from_attr_did(&pkey);

	// TODO: Check the type of returned records
	GNUNET_GNS_lookup(gns_handle, "didd", &pkey, GNUNET_DNSPARSER_TYPE_TXT, GNUNET_GNS_LO_DEFAULT, &print_did_document, NULL);
}


typedef void
(*remove_did_document_callback) (void * cls);

struct event {
	remove_did_document_callback cont;
	void * cls;
};

/**
 * @brief Implements the GNUNET_NAMESTORE_ContinuationWithStatus
 * Calls the callback function and cls in the event struct 
 * 
 * @param cls closure containing the event struct 
 * @param success
 * @param emgs 
 */
static void
remove_did_document_namestore_cb(void * cls, int32_t success, const char *emgs){
	struct event * blob;

	if(success == GNUNET_YES){
		printf("DID Document has been removed\n");

		blob = (struct event *) cls;

		if(blob->cont != NULL)
		{
			blob->cont(blob->cls);
			free(blob);
		} else {
			free(blob);
			GNUNET_SCHEDULER_add_now(cleanup, NULL);
			ret = 0;
			return;
		}
	} else {
		printf("Something went wrong when deleting the DID Document\n");

		if(emgs != NULL) {
			printf("%s\n", emgs);
		}

		GNUNET_SCHEDULER_add_now(cleanup, NULL);
		ret = 0;
		return;
	}
}

/**
 * @brief Callback called after the ego has been locked up
 * 
 * @param cls closure
 * @param ego the ego returned by the identity service 
 */
static void
remove_did_document_ego_lookup_cb(void * cls, struct GNUNET_IDENTITY_Ego * ego){
	const struct GNUNET_IDENTITY_PrivateKey * skey = GNUNET_IDENTITY_ego_get_private_key(ego);
	const int emp[0];
	struct GNUNET_GNSRECORD_Data rd = {
		.data = &emp,
		.expiration_time = 0,
		.data_size = 0,
		.record_type = 0,
		.flags = GNUNET_GNSRECORD_RF_NONE
	};

	GNUNET_NAMESTORE_records_store (namestore_handle,
	                                skey,
	                                "didd",
	                                0,
	                                &rd,
	                                &remove_did_document_namestore_cb,
	                                cls);
}

/**
 * @brief Remove a DID Document
 */
static void
remove_did_document(remove_did_document_callback cont, void * cls)
{
	struct event * blob;

	if(attr_ego == NULL) {
		printf("Remove requieres an ego option\n");
		GNUNET_SCHEDULER_add_now(cleanup, NULL);
		ret = 1;
		return;
	} else {
		blob = malloc(sizeof(* blob));
		blob->cont = cont;
		blob->cls = cls;

		GNUNET_IDENTITY_ego_lookup(my_cfg,
		                           attr_ego,
		                           &remove_did_document_ego_lookup_cb,
		                           (void *) blob);
	} 
}


/**
 * @brief Create a did generate did object
 * 
 * @param pkey 
 * @return void* Return pointer to the DID Document
 */
char *
create_did_generate(struct GNUNET_IDENTITY_PublicKey pkey)
{
  char * pkey_str; // Convert public key to string
  char did_str[71]; // 58 + 12 + 1= 71
  char * didd_str;
  char pkey_multibase_str[60]; // 58 + 1 + 1 = 60

  json_t * did_json;
  json_t * pkey_multibase_json;
  json_t * context_1_json;
  json_t * context_2_json;
  json_t * auth_type_json;
  json_t * context_json;
  json_t * auth_json;
  json_t * auth_1_json;
  json_t * didd;

  pkey_str = GNUNET_IDENTITY_public_key_to_string(&pkey); // Convert public key to string
  sprintf(did_str, "did:reclaim:%s", pkey_str); // Convert the public key to a DID str
  sprintf(pkey_multibase_str, "V%s", pkey_str); // Convert the public key to MultiBase data format

  // Create DID Document 
  did_json = json_string(did_str);
  pkey_multibase_json = json_string(pkey_multibase_str);
  context_1_json = json_string("https://www.w3.org/ns/did/v1");
  context_2_json = json_string("https://w3id.org/security/suites/ed25519-2020/v1");
  auth_type_json = json_string("Ed25519VerificationKey2020");

  context_json = json_array();
  json_array_append(context_json, context_1_json);
  json_array_append(context_json, context_2_json);

  auth_json = json_array();
  auth_1_json = json_object();
  json_object_set(auth_1_json, "id", did_json);
  json_object_set(auth_1_json, "type", auth_type_json);
  json_object_set(auth_1_json, "controller", did_json);
  json_object_set(auth_1_json, "publicKeyMultiBase", pkey_multibase_json);
  json_array_append(auth_json, auth_1_json);

  didd = json_object();
  json_object_set(didd, "@context", context_json);
  json_object_set(didd, "id", did_json);
  json_object_set(didd, "authentication", auth_json);

  // Encode DID Document as JSON string
  didd_str = json_dumps(didd, JSON_INDENT(2));
  if(didd_str == NULL)
  {
    printf("DID Document could not be encoded");
    GNUNET_SCHEDULER_add_now(&cleanup, NULL);
    ret = 1;
    return NULL;
  }

	free(did_json);
	free(pkey_multibase_json);
	free(context_1_json);
	free(context_2_json);
	free(auth_type_json);
	free(auth_json);
	free(auth_1_json);
	free(didd);

	return didd_str;
}

/**
 * @brief Create a DID. Store DID in Namestore cb 
 * 
 */
static void
create_did_store_cb(void *cls, int32_t success, const char *emsg){
  printf("DID Document has been stored to namestore\n");
  GNUNET_SCHEDULER_add_now(&cleanup, NULL);
  ret = 0;
  return;
}

/**
 * @brief Create a did. Store DID in Namestore 
 * 
 * @param didd_str String endoced DID Docuement 
 * @param ego Identity whos DID Document is stored
 */
static void
create_did_store(char * didd_str, struct GNUNET_IDENTITY_Ego * ego)
{

	struct GNUNET_TIME_Relative expire_time;
  struct GNUNET_GNSRECORD_Data record_data;
	const struct GNUNET_IDENTITY_PrivateKey * skey;

	if(GNUNET_STRINGS_fancy_time_to_relative(attr_expire, &expire_time) != GNUNET_OK)
	{
		record_data.data = (void *) didd_str;
		record_data.expiration_time = expire_time.rel_value_us;
		record_data.data_size = strlen(didd_str);
		record_data.record_type = GNUNET_GNSRECORD_typename_to_number("TXT"),
		record_data.flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;

		skey = GNUNET_IDENTITY_ego_get_private_key(ego);

  	GNUNET_NAMESTORE_records_store( namestore_handle,
  	                                skey,
  	                                "didd",
  	                                1,
  	                                &record_data,
  	                                &create_did_store_cb,
  	                                NULL);
	} else {
		printf("Failed to read given expiration time\n");
  	GNUNET_SCHEDULER_add_now(&cleanup, NULL);
  	ret = 1;
  	return;
	}
}

/**
 * @brief Create a did ego lockup cb
 * 
 * @param cls 
 * @param ego 
 */
static void 
create_did_ego_lockup_cb(void *cls, struct GNUNET_IDENTITY_Ego * ego)
{
  struct GNUNET_IDENTITY_PublicKey pkey;
	char * didd_str;

  if(ego == NULL) 
  {
    printf("EGO not found\n");
    GNUNET_SCHEDULER_add_now(&cleanup, NULL);
    ret = 1;
    return;
  }

  GNUNET_IDENTITY_ego_get_public_key(ego, &pkey);

	printf("DEBUG: Key type: %ld\n", (unsigned long) pkey.type);

	// if (false)
	if (pkey.type != GNUNET_GNSRECORD_TYPE_EDKEY)
	{
		printf("The EGO has to have an EDDSA key pair\n");
		GNUNET_SCHEDULER_add_now(&cleanup, NULL);
		ret = 1;
		return;
	}

	if(attr_didd != NULL)
	{
		// TODO: Check if given DIDD is valid
		printf("DID Docuement is read from \"did-document\" argument (EXPERIMENTAL)\n");
		didd_str = attr_didd;
	} else {
		// Generate DID Docuement from public key
		didd_str = create_did_generate(pkey);
	}

  // Print DID Docuement to stdout
  printf("%s\n", didd_str);

	// Store the DID Docuement
	create_did_store(didd_str, ego);

  // Save DID Document String to GNS
  free(didd_str);
}

/**
 * @brief Create a did document - Create a new identity first
 */
static void 
create_did_document_ego_create_cb(void *cls,
  								  const struct GNUNET_IDENTITY_PrivateKey *pk,
  								  const char *emsg)
{
	const char * ego_name;

	if (emsg != NULL){
		printf("Something went wrong during the creation of a new identity\n");
		printf("%s\n", emsg);
    GNUNET_SCHEDULER_add_now(&cleanup, NULL);
    ret = 1;
    return;
	}

	ego_name = (char *) cls;

	GNUNET_IDENTITY_ego_lookup(my_cfg,
	                           ego_name,
	                           &create_did_ego_lockup_cb,
	                           NULL);
}

/**
 * @brief Create a did document
 * 
 */
static void 
create_did_document()
{
	if(attr_name != NULL && attr_expire != NULL){
		GNUNET_IDENTITY_create(identity_handle,
													 attr_name,
													 NULL,
													 GNUNET_IDENTITY_TYPE_EDDSA,
													 &create_did_document_ego_create_cb,
													 (void *) attr_name);
	} else if (attr_ego != NULL && attr_expire != NULL) {
		GNUNET_IDENTITY_ego_lookup(my_cfg,
		                           attr_ego,
		                           &create_did_ego_lockup_cb,
		                           NULL);
  } else {
    printf("Set the NAME or the EGO and the Expiration-time argument to create a new DID(-Document)\n");
    GNUNET_SCHEDULER_add_now(&cleanup, NULL);
    ret = 1;
    return;
  }
}


/**
 * @brief Replace a DID Docuemnt. Callback function after ego lockup
 * 
 * @param cls 
 * @param ego 
 */
static void 
replace_did_document_ego_lookup_cb(void *cls, struct GNUNET_IDENTITY_Ego * ego)
{
	create_did_store(attr_didd, ego);
}

/**
 * @brief Replace a DID Document. Callback functiona after remove
 * 
 * @param cls 
 */
static void 
replace_did_document_remove_cb(void * cls)
{
	GNUNET_IDENTITY_ego_lookup(my_cfg,
	                           attr_ego,
	                           &replace_did_document_ego_lookup_cb,
	                           NULL);
}

/**
 * @brief Replace a DID Docuemnt 
 * 
 */
static void 
replace_did_document()
{
	if (attr_didd != NULL || attr_expire != NULL)
	{
		remove_did_document(&replace_did_document_remove_cb, NULL);
	} else {
		printf("Set the DID Document and expiration time argument to repalce the DID Document\n");
  	GNUNET_SCHEDULER_add_now(&cleanup, NULL);
  	ret = 1;
  	return;
	}
}


static void
run (void *cls,
     char *const *args,
     const char *cfgfile,
     const struct GNUNET_CONFIGURATION_Handle *c)
{
	gns_handle = GNUNET_GNS_connect(c);
	namestore_handle = GNUNET_NAMESTORE_connect(c);
	identity_handle = GNUNET_IDENTITY_connect(c, NULL, NULL);
	my_cfg = c;

	// check if GNS_handle could connect
	if(gns_handle == NULL) {
		ret = 1;
		return;
	}

	// check if NAMESTORE_handle could connect
	if(namestore_handle == NULL) {
		ret = 1;
		return;
	}

	// check if IDENTITY_handle could connect
	if(identity_handle == NULL) {
		ret = 1;
		return;
	}

	if (1 == attr_replace) {
		replace_did_document();
	} else if (1 == attr_get) {
		resolve_did_document();
	} else if (1 == attr_remove) {
		remove_did_document(NULL, NULL);
	} else if (1 == attr_create) {
    create_did_document();
	} else if (1 == attr_show) {
    get_did_for_ego();
  } else {
		// No Argument found
		printf("No correct argument combination found. Use gnunet-did -h for help");
		ret = 1;
		GNUNET_SCHEDULER_add_now(cleanup, NULL);
		return;
	}
}

int
main (int argc, char *const argv[])
{
	struct GNUNET_GETOPT_CommandLineOption options[] = {
		GNUNET_GETOPT_option_flag ('C',
		                           "create",
		                           gettext_noop ("Create a DID Document and display its DID"),
		                           &attr_create),
		GNUNET_GETOPT_option_flag ('g',
		                           "get",
		                           gettext_noop ("Get the DID Document associated with the given DID"),
		                           &attr_get),
		GNUNET_GETOPT_option_flag ('s',
		                           "show",
		                           gettext_noop ("Show the DID for a given ego"),
		                           &attr_show),
		GNUNET_GETOPT_option_flag ('r',
		                           "remove",
		                           gettext_noop ("Remove the DID Document with DID from GNUNET"),
		                           &attr_remove),
		GNUNET_GETOPT_option_flag ('R',
		                           "replace",
		                           gettext_noop ("Replace the DID Document."),
		                           &attr_replace),
		GNUNET_GETOPT_option_string ('d',
		                             "did",
		                             "DID",
		                             gettext_noop ("The DID to work with"),
		                             &attr_did),
		GNUNET_GETOPT_option_string ('D',
		                             "did-docuement",
		                             "JSON",
		                             gettext_noop ("The DID Document to store in GNUNET"),
		                             &attr_didd),
		GNUNET_GETOPT_option_string ('e',
		                             "ego",
		                             "EGO",
		                             gettext_noop ("The EGO to work with"),
		                             &attr_ego),
		GNUNET_GETOPT_option_string ('n',
		                             "name",
		                             "NAME",
		                             gettext_noop ("The name of the created EGO"),
		                             &attr_name),
		GNUNET_GETOPT_option_string ('t',
		                             "expiration-time",
		                             "TIME",
		                             gettext_noop ("The time until the DID Document is going to expire (e.g. 5d)"),
		                             &attr_expire),
		GNUNET_GETOPT_OPTION_END
	};

	if (GNUNET_OK != GNUNET_PROGRAM_run (argc,
	                                     argv,
	                                     "gnunet-did",
	                                     ("did command line tool"),
	                                     options,
	                                     &run,
	                                     NULL))
		return 1;
	else
		return ret;
}