2 This file is part of GNUnet.
3 Copyright (C) 2012-2015 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @author Martin Schanzenbach
22 * @file src/reclaim/gnunet-reclaim.c
23 * @brief Identity Provider utility
29 #include "gnunet_util_lib.h"
31 #include "gnunet_identity_service.h"
32 #include "gnunet_namestore_service.h"
33 #include "gnunet_reclaim_service.h"
34 #include "gnunet_signatures.h"
53 static char *attr_name;
58 static char *attr_value;
63 static char *issue_attrs;
68 static char *consume_ticket;
73 static char *type_str;
78 static char *revoke_ticket;
83 static int list_tickets;
88 static char *ego_name;
93 static struct GNUNET_IDENTITY_Handle *identity_handle;
98 static struct GNUNET_RECLAIM_Handle *reclaim_handle;
103 static struct GNUNET_RECLAIM_Operation *reclaim_op;
108 static struct GNUNET_RECLAIM_AttributeIterator *attr_iterator;
113 static struct GNUNET_RECLAIM_TicketIterator *ticket_iterator;
118 static struct GNUNET_CRYPTO_AbeMasterKey *abe_key;
123 static const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey;
128 static struct GNUNET_CRYPTO_EcdsaPublicKey rp_key;
133 static struct GNUNET_RECLAIM_Ticket ticket;
138 static struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attr_list;
141 * Attribute expiration interval
143 static struct GNUNET_TIME_Relative exp_interval;
148 static struct GNUNET_SCHEDULER_Task *timeout;
153 static struct GNUNET_SCHEDULER_Task *cleanup_task;
158 struct GNUNET_RECLAIM_ATTRIBUTE_Claim *claim;
163 static char *attr_delete;
166 * Claim object to delete
168 static struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr_to_delete;
171 do_cleanup (void *cls)
175 GNUNET_SCHEDULER_cancel (timeout);
176 if (NULL != reclaim_op)
177 GNUNET_RECLAIM_cancel (reclaim_op);
178 if (NULL != attr_iterator)
179 GNUNET_RECLAIM_get_attributes_stop (attr_iterator);
180 if (NULL != ticket_iterator)
181 GNUNET_RECLAIM_ticket_iteration_stop (ticket_iterator);
182 if (NULL != reclaim_handle)
183 GNUNET_RECLAIM_disconnect (reclaim_handle);
184 if (NULL != identity_handle)
185 GNUNET_IDENTITY_disconnect (identity_handle);
187 GNUNET_free (abe_key);
188 if (NULL != attr_list)
189 GNUNET_free (attr_list);
190 if (NULL != attr_to_delete)
191 GNUNET_free (attr_to_delete);
195 ticket_issue_cb (void *cls, const struct GNUNET_RECLAIM_Ticket *ticket)
199 if (NULL != ticket) {
200 ticket_str = GNUNET_STRINGS_data_to_string_alloc (
201 ticket, sizeof (struct GNUNET_RECLAIM_Ticket));
202 printf ("%s\n", ticket_str);
203 GNUNET_free (ticket_str);
205 cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
209 store_attr_cont (void *cls, int32_t success, const char *emsg)
212 if (GNUNET_SYSERR == success) {
213 fprintf (stderr, "%s\n", emsg);
215 cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
219 process_attrs (void *cls, const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
220 const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr)
224 const char *attr_type;
226 if (NULL == identity) {
228 cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
235 value_str = GNUNET_RECLAIM_ATTRIBUTE_value_to_string (attr->type, attr->data,
237 attr_type = GNUNET_RECLAIM_ATTRIBUTE_number_to_typename (attr->type);
238 id = GNUNET_STRINGS_data_to_string_alloc (&attr->id, sizeof (uint64_t));
239 fprintf (stdout, "Name: %s; Value: %s (%s); Version %u; ID: %s\n", attr->name,
240 value_str, attr_type, attr->version, id);
245 ticket_iter_err (void *cls)
247 ticket_iterator = NULL;
248 fprintf (stderr, "Failed to iterate over tickets\n");
249 cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
253 ticket_iter_fin (void *cls)
255 ticket_iterator = NULL;
256 cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
260 ticket_iter (void *cls, const struct GNUNET_RECLAIM_Ticket *ticket)
265 aud = GNUNET_STRINGS_data_to_string_alloc (
266 &ticket->audience, sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
267 ref = GNUNET_STRINGS_data_to_string_alloc (&ticket->rnd, sizeof (uint64_t));
269 fprintf (stdout, "Ticket ID: %s | Audience: %s\n", ref, aud);
270 GNUNET_RECLAIM_ticket_iteration_next (ticket_iterator);
274 iter_error (void *cls)
276 attr_iterator = NULL;
277 fprintf (stderr, "Failed to iterate over attributes\n");
278 cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
282 timeout_task (void *cls)
286 fprintf (stderr, "Timeout\n");
287 if (NULL == cleanup_task)
288 cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
292 process_rvk (void *cls, int success, const char *msg)
295 if (GNUNET_OK != success) {
296 fprintf (stderr, "Revocation failed.\n");
299 cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
304 process_delete (void *cls, int success, const char *msg)
307 if (GNUNET_OK != success) {
308 fprintf (stderr, "Deletion failed.\n");
311 cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
316 iter_finished (void *cls)
322 attr_iterator = NULL;
324 cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
329 reclaim_op = GNUNET_RECLAIM_ticket_issue (
330 reclaim_handle, pkey, &rp_key, attr_list, &ticket_issue_cb, NULL);
333 if (consume_ticket) {
334 reclaim_op = GNUNET_RECLAIM_ticket_consume (reclaim_handle, pkey, &ticket,
335 &process_attrs, NULL);
336 timeout = GNUNET_SCHEDULER_add_delayed (
337 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
338 &timeout_task, NULL);
342 reclaim_op = GNUNET_RECLAIM_ticket_revoke (reclaim_handle, pkey, &ticket,
347 if (NULL == attr_to_delete) {
348 fprintf (stdout, "No such attribute ``%s''\n", attr_delete);
351 reclaim_op = GNUNET_RECLAIM_attribute_delete (
352 reclaim_handle, pkey, attr_to_delete, &process_delete, NULL);
356 if (NULL == type_str)
357 type = GNUNET_RECLAIM_ATTRIBUTE_TYPE_STRING;
359 type = GNUNET_RECLAIM_ATTRIBUTE_typename_to_number (type_str);
361 GNUNET_assert (GNUNET_SYSERR !=
362 GNUNET_RECLAIM_ATTRIBUTE_string_to_value (
363 type, attr_value, (void **)&data, &data_size));
367 claim->data_size = data_size;
370 GNUNET_RECLAIM_ATTRIBUTE_claim_new (attr_name, type, data, data_size);
372 reclaim_op = GNUNET_RECLAIM_attribute_store (
373 reclaim_handle, pkey, claim, &exp_interval, &store_attr_cont, NULL);
378 cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
382 iter_cb (void *cls, const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
383 const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr)
385 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
390 const char *attr_type;
392 if ((NULL != attr_name) && (NULL != claim)) {
393 if (0 == strcasecmp (attr_name, attr->name)) {
394 claim = GNUNET_RECLAIM_ATTRIBUTE_claim_new (attr->name, attr->type,
395 attr->data, attr->data_size);
397 } else if (issue_attrs) {
398 attrs_tmp = GNUNET_strdup (issue_attrs);
399 attr_str = strtok (attrs_tmp, ",");
400 while (NULL != attr_str) {
401 if (0 != strcasecmp (attr_str, attr->name)) {
402 attr_str = strtok (NULL, ",");
405 le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry);
406 le->claim = GNUNET_RECLAIM_ATTRIBUTE_claim_new (
407 attr->name, attr->type, attr->data, attr->data_size);
408 le->claim->version = attr->version;
409 le->claim->id = attr->id;
410 GNUNET_CONTAINER_DLL_insert (attr_list->list_head, attr_list->list_tail,
414 GNUNET_free (attrs_tmp);
415 } else if (attr_delete && (NULL == attr_to_delete)) {
416 label = GNUNET_STRINGS_data_to_string_alloc (&attr->id, sizeof (uint64_t));
417 if (0 == strcasecmp (attr_delete, label)) {
418 attr_to_delete = GNUNET_RECLAIM_ATTRIBUTE_claim_new (
419 attr->name, attr->type, attr->data, attr->data_size);
420 attr_to_delete->id = attr->id;
424 attr_str = GNUNET_RECLAIM_ATTRIBUTE_value_to_string (attr->type, attr->data,
426 attr_type = GNUNET_RECLAIM_ATTRIBUTE_number_to_typename (attr->type);
427 id = GNUNET_STRINGS_data_to_string_alloc (&attr->id, sizeof (uint64_t));
428 fprintf (stdout, "Name: %s; Value: %s (%s); Version %u; ID: %s\n",
429 attr->name, attr_str, attr_type, attr->version, id);
432 GNUNET_RECLAIM_get_attributes_next (attr_iterator);
439 fprintf (stderr, "Ego %s not found\n", ego_name);
440 cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
445 ticket_iterator = GNUNET_RECLAIM_ticket_iteration_start (
446 reclaim_handle, pkey, &ticket_iter_err, NULL, &ticket_iter, NULL,
447 &ticket_iter_fin, NULL);
452 GNUNET_CRYPTO_ecdsa_public_key_from_string (rp, strlen (rp), &rp_key);
453 if (NULL != consume_ticket)
454 GNUNET_STRINGS_string_to_data (consume_ticket, strlen (consume_ticket),
456 sizeof (struct GNUNET_RECLAIM_Ticket));
457 if (NULL != revoke_ticket)
458 GNUNET_STRINGS_string_to_data (revoke_ticket, strlen (revoke_ticket),
460 sizeof (struct GNUNET_RECLAIM_Ticket));
462 attr_list = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList);
464 attr_iterator = GNUNET_RECLAIM_get_attributes_start (
465 reclaim_handle, pkey, &iter_error, NULL, &iter_cb, NULL, &iter_finished,
469 static int init = GNUNET_YES;
472 ego_cb (void *cls, struct GNUNET_IDENTITY_Ego *ego, void **ctx,
476 if (GNUNET_YES == init) {
482 if (0 != strcmp (name, ego_name))
484 pkey = GNUNET_IDENTITY_ego_get_private_key (ego);
489 run (void *cls, char *const *args, const char *cfgfile,
490 const struct GNUNET_CONFIGURATION_Handle *c)
493 if (NULL == ego_name) {
495 fprintf (stderr, _ ("Ego is required\n"));
499 if ((NULL == attr_value) && (NULL != attr_name)) {
501 fprintf (stderr, _ ("Attribute value missing!\n"));
505 if ((NULL == rp) && (NULL != issue_attrs)) {
507 fprintf (stderr, _ ("Requesting party key is required!\n"));
511 reclaim_handle = GNUNET_RECLAIM_connect (c);
513 identity_handle = GNUNET_IDENTITY_connect (c, &ego_cb, NULL);
518 main (int argc, char *const argv[])
520 exp_interval = GNUNET_TIME_UNIT_HOURS;
521 struct GNUNET_GETOPT_CommandLineOption options[] = {
523 GNUNET_GETOPT_option_string ('a', "add", "NAME",
524 gettext_noop ("Add an attribute NAME"),
526 GNUNET_GETOPT_option_string ('d', "delete", "ID",
527 gettext_noop ("Add an attribute with ID"),
529 GNUNET_GETOPT_option_string ('V', "value", "VALUE",
530 gettext_noop ("The attribute VALUE"),
532 GNUNET_GETOPT_option_string ('e', "ego", "EGO",
533 gettext_noop ("The EGO to use"), &ego_name),
534 GNUNET_GETOPT_option_string (
535 'r', "rp", "RP", gettext_noop ("Specify the relying party for issue"),
537 GNUNET_GETOPT_option_flag (
538 'D', "dump", gettext_noop ("List attributes for EGO"), &list),
539 GNUNET_GETOPT_option_string (
540 'i', "issue", "A1,A2,...",
542 "Issue a ticket for a set of attributes separated by comma"),
544 GNUNET_GETOPT_option_string ('C', "consume", "TICKET",
545 gettext_noop ("Consume a ticket"),
547 GNUNET_GETOPT_option_string ('R', "revoke", "TICKET",
548 gettext_noop ("Revoke a ticket"),
550 GNUNET_GETOPT_option_string (
551 't', "type", "TYPE", gettext_noop ("Type of attribute"), &type_str),
552 GNUNET_GETOPT_option_flag (
553 'T', "tickets", gettext_noop ("List tickets of ego"), &list_tickets),
554 GNUNET_GETOPT_option_relative_time (
555 'E', "expiration", "INTERVAL",
556 gettext_noop ("Expiration interval of the attribute"), &exp_interval),
558 GNUNET_GETOPT_OPTION_END};
559 if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "gnunet-reclaim",
560 _ ("re:claimID command line tool"),
561 options, &run, NULL))