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;
161 do_cleanup (void *cls)
165 GNUNET_SCHEDULER_cancel (timeout);
166 if (NULL != reclaim_op)
167 GNUNET_RECLAIM_cancel (reclaim_op);
168 if (NULL != attr_iterator)
169 GNUNET_RECLAIM_get_attributes_stop (attr_iterator);
170 if (NULL != ticket_iterator)
171 GNUNET_RECLAIM_ticket_iteration_stop (ticket_iterator);
172 if (NULL != reclaim_handle)
173 GNUNET_RECLAIM_disconnect (reclaim_handle);
174 if (NULL != identity_handle)
175 GNUNET_IDENTITY_disconnect (identity_handle);
177 GNUNET_free (abe_key);
178 if (NULL != attr_list)
179 GNUNET_free (attr_list);
183 ticket_issue_cb (void *cls, const struct GNUNET_RECLAIM_Ticket *ticket)
187 if (NULL != ticket) {
188 ticket_str = GNUNET_STRINGS_data_to_string_alloc (
189 ticket, sizeof (struct GNUNET_RECLAIM_Ticket));
190 printf ("%s\n", ticket_str);
191 GNUNET_free (ticket_str);
193 cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
197 store_attr_cont (void *cls, int32_t success, const char *emsg)
200 if (GNUNET_SYSERR == success) {
201 fprintf (stderr, "%s\n", emsg);
203 cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
207 process_attrs (void *cls, const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
208 const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr)
211 const char *attr_type;
213 if (NULL == identity) {
215 cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
222 value_str = GNUNET_RECLAIM_ATTRIBUTE_value_to_string (attr->type, attr->data,
224 attr_type = GNUNET_RECLAIM_ATTRIBUTE_number_to_typename (attr->type);
225 fprintf (stdout, "%s: %s [%s,v%u,id=%" PRIu64 "]\n", attr->name, value_str,
226 attr_type, attr->version, attr->id);
230 ticket_iter_err (void *cls)
232 ticket_iterator = NULL;
233 fprintf (stderr, "Failed to iterate over tickets\n");
234 cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
238 ticket_iter_fin (void *cls)
240 ticket_iterator = NULL;
241 cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
245 ticket_iter (void *cls, const struct GNUNET_RECLAIM_Ticket *ticket)
250 aud = GNUNET_STRINGS_data_to_string_alloc (
251 &ticket->audience, sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
252 ref = GNUNET_STRINGS_data_to_string_alloc (&ticket->rnd, sizeof (uint64_t));
254 fprintf (stdout, "Ticket ID: %s | Audience: %s\n", ref, aud);
255 GNUNET_RECLAIM_ticket_iteration_next (ticket_iterator);
259 iter_error (void *cls)
261 attr_iterator = NULL;
262 fprintf (stderr, "Failed to iterate over attributes\n");
263 cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
267 timeout_task (void *cls)
271 fprintf (stderr, "Timeout\n");
272 if (NULL == cleanup_task)
273 cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
277 process_rvk (void *cls, int success, const char *msg)
280 if (GNUNET_OK != success) {
281 fprintf (stderr, "Revocation failed.\n");
284 cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
288 iter_finished (void *cls)
294 attr_iterator = NULL;
296 cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
301 reclaim_op = GNUNET_RECLAIM_ticket_issue (
302 reclaim_handle, pkey, &rp_key, attr_list, &ticket_issue_cb, NULL);
305 if (consume_ticket) {
306 reclaim_op = GNUNET_RECLAIM_ticket_consume (reclaim_handle, pkey, &ticket,
307 &process_attrs, NULL);
308 timeout = GNUNET_SCHEDULER_add_delayed (
309 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
310 &timeout_task, NULL);
314 reclaim_op = GNUNET_RECLAIM_ticket_revoke (reclaim_handle, pkey, &ticket,
319 if (NULL == type_str)
320 type = GNUNET_RECLAIM_ATTRIBUTE_TYPE_STRING;
322 type = GNUNET_RECLAIM_ATTRIBUTE_typename_to_number (type_str);
324 GNUNET_assert (GNUNET_SYSERR !=
325 GNUNET_RECLAIM_ATTRIBUTE_string_to_value (
326 type, attr_value, (void **)&data, &data_size));
330 claim->data_size = data_size;
333 GNUNET_RECLAIM_ATTRIBUTE_claim_new (attr_name, type, data, data_size);
335 reclaim_op = GNUNET_RECLAIM_attribute_store (
336 reclaim_handle, pkey, claim, &exp_interval, &store_attr_cont, NULL);
341 cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
345 iter_cb (void *cls, const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
346 const struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr)
348 struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
351 const char *attr_type;
353 if ((NULL != attr_name) && (NULL != claim)) {
354 if (0 == strcasecmp (attr_name, attr->name)) {
355 claim = GNUNET_RECLAIM_ATTRIBUTE_claim_new (attr->name, attr->type,
356 attr->data, attr->data_size);
358 } else if (issue_attrs) {
359 attrs_tmp = GNUNET_strdup (issue_attrs);
360 attr_str = strtok (attrs_tmp, ",");
361 while (NULL != attr_str) {
362 if (0 != strcasecmp (attr_str, attr->name)) {
363 attr_str = strtok (NULL, ",");
366 le = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry);
367 le->claim = GNUNET_RECLAIM_ATTRIBUTE_claim_new (
368 attr->name, attr->type, attr->data, attr->data_size);
369 le->claim->version = attr->version;
370 le->claim->id = attr->id;
371 GNUNET_CONTAINER_DLL_insert (attr_list->list_head, attr_list->list_tail,
375 GNUNET_free (attrs_tmp);
377 attr_str = GNUNET_RECLAIM_ATTRIBUTE_value_to_string (attr->type, attr->data,
379 attr_type = GNUNET_RECLAIM_ATTRIBUTE_number_to_typename (attr->type);
380 fprintf (stdout, "%s: %s [%s,v%u,id=%" PRIu64 "]\n", attr->name, attr_str,
381 attr_type, attr->version, attr->id);
383 GNUNET_RECLAIM_get_attributes_next (attr_iterator);
390 fprintf (stderr, "Ego %s not found\n", ego_name);
391 cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
396 ticket_iterator = GNUNET_RECLAIM_ticket_iteration_start (
397 reclaim_handle, pkey, &ticket_iter_err, NULL, &ticket_iter, NULL,
398 &ticket_iter_fin, NULL);
403 GNUNET_CRYPTO_ecdsa_public_key_from_string (rp, strlen (rp), &rp_key);
404 if (NULL != consume_ticket)
405 GNUNET_STRINGS_string_to_data (consume_ticket, strlen (consume_ticket),
407 sizeof (struct GNUNET_RECLAIM_Ticket));
408 if (NULL != revoke_ticket)
409 GNUNET_STRINGS_string_to_data (revoke_ticket, strlen (revoke_ticket),
411 sizeof (struct GNUNET_RECLAIM_Ticket));
413 attr_list = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList);
415 attr_iterator = GNUNET_RECLAIM_get_attributes_start (
416 reclaim_handle, pkey, &iter_error, NULL, &iter_cb, NULL, &iter_finished,
420 static int init = GNUNET_YES;
423 ego_cb (void *cls, struct GNUNET_IDENTITY_Ego *ego, void **ctx,
427 if (GNUNET_YES == init) {
433 if (0 != strcmp (name, ego_name))
435 pkey = GNUNET_IDENTITY_ego_get_private_key (ego);
440 run (void *cls, char *const *args, const char *cfgfile,
441 const struct GNUNET_CONFIGURATION_Handle *c)
444 if (NULL == ego_name) {
446 fprintf (stderr, _ ("Ego is required\n"));
450 if ((NULL == attr_value) && (NULL != attr_name)) {
452 fprintf (stderr, _ ("Attribute value missing!\n"));
456 if ((NULL == rp) && (NULL != issue_attrs)) {
458 fprintf (stderr, _ ("Requesting party key is required!\n"));
462 reclaim_handle = GNUNET_RECLAIM_connect (c);
464 identity_handle = GNUNET_IDENTITY_connect (c, &ego_cb, NULL);
469 main (int argc, char *const argv[])
471 exp_interval = GNUNET_TIME_UNIT_HOURS;
472 struct GNUNET_GETOPT_CommandLineOption options[] = {
474 GNUNET_GETOPT_option_string ('a', "add", "NAME",
475 gettext_noop ("Add an attribute NAME"),
478 GNUNET_GETOPT_option_string ('V', "value", "VALUE",
479 gettext_noop ("The attribute VALUE"),
481 GNUNET_GETOPT_option_string ('e', "ego", "EGO",
482 gettext_noop ("The EGO to use"), &ego_name),
483 GNUNET_GETOPT_option_string (
484 'r', "rp", "RP", gettext_noop ("Specify the relying party for issue"),
486 GNUNET_GETOPT_option_flag (
487 'D', "dump", gettext_noop ("List attributes for EGO"), &list),
488 GNUNET_GETOPT_option_string (
489 'i', "issue", "A1,A2,...",
491 "Issue a ticket for a set of attributes separated by comma"),
493 GNUNET_GETOPT_option_string ('C', "consume", "TICKET",
494 gettext_noop ("Consume a ticket"),
496 GNUNET_GETOPT_option_string ('R', "revoke", "TICKET",
497 gettext_noop ("Revoke a ticket"),
499 GNUNET_GETOPT_option_string (
500 't', "type", "TYPE", gettext_noop ("Type of attribute"), &type_str),
501 GNUNET_GETOPT_option_flag (
502 'T', "tickets", gettext_noop ("List tickets of ego"), &list_tickets),
503 GNUNET_GETOPT_option_relative_time (
504 'E', "expiration", "INTERVAL",
505 gettext_noop ("Expiration interval of the attribute"), &exp_interval),
507 GNUNET_GETOPT_OPTION_END};
508 if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "gnunet-reclaim",
509 _ ("re:claimID command line tool"),
510 options, &run, NULL))