2 This file is part of GNUnet.
3 Copyright (C) 2012-2013 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file gnunet-credential.c
22 * @brief command line tool to access command line Credential service
23 * @author Martin Schanzenbach
26 #include <gnunet_util_lib.h>
27 #include <gnunet_credential_service.h>
28 #include <gnunet_gnsrecord_lib.h>
29 #include "credential_misc.h"
30 #include "credential_serialization.h"
33 * Configuration we are using.
35 static const struct GNUNET_CONFIGURATION_Handle *cfg;
40 static struct GNUNET_IDENTITY_EgoLookup *el;
43 * Handle to Credential service.
45 static struct GNUNET_CREDENTIAL_Handle *credential;
48 * Desired timeout for the lookup (default is no timeout).
50 static struct GNUNET_TIME_Relative timeout;
53 * Handle to verify request
55 static struct GNUNET_CREDENTIAL_Request *verify_request;
58 * Handle to collect request
60 static struct GNUNET_CREDENTIAL_Request *collect_request;
63 * Task scheduled to handle timeout.
65 static struct GNUNET_SCHEDULER_Task *tt;
68 * Subject pubkey string
70 static char *subject_key;
73 * Subject credential string
75 static char *subject_credential;
80 static char *expiration;
85 struct GNUNET_CRYPTO_EcdsaPublicKey subject_pkey;
90 struct GNUNET_CRYPTO_EcdsaPublicKey issuer_pkey;
94 * Issuer pubkey string
96 static char *issuer_key;
101 static char *ego_name;
106 static char *issuer_attr;
116 static int create_cred;
124 * Task run on shutdown. Cleans up everything.
129 do_shutdown (void *cls)
131 if (NULL != verify_request)
133 GNUNET_CREDENTIAL_request_cancel (verify_request);
134 verify_request = NULL;
136 if (NULL != credential)
138 GNUNET_CREDENTIAL_disconnect (credential);
143 GNUNET_SCHEDULER_cancel (tt);
150 * Task run on timeout. Triggers shutdown.
155 do_timeout (void *cls)
158 GNUNET_SCHEDULER_shutdown ();
162 handle_collect_result (void *cls,
163 unsigned int d_count,
164 struct GNUNET_CREDENTIAL_Delegation *dc,
165 unsigned int c_count,
166 struct GNUNET_CREDENTIAL_Credential *cred)
171 verify_request = NULL;
174 for (i=0;i<c_count;i++)
176 line = GNUNET_CREDENTIAL_credential_to_string (&cred[i]);
184 GNUNET_SCHEDULER_shutdown ();
189 handle_verify_result (void *cls,
190 unsigned int d_count,
191 struct GNUNET_CREDENTIAL_Delegation *dc,
192 unsigned int c_count,
193 struct GNUNET_CREDENTIAL_Credential *cred)
199 verify_request = NULL;
201 printf ("Failed.\n");
204 printf("Delegation Chain:\n");
205 for (i=0;i<d_count;i++)
207 iss_key = GNUNET_CRYPTO_ecdsa_public_key_to_string (&dc[i].issuer_key);
208 sub_key = GNUNET_CRYPTO_ecdsa_public_key_to_string (&dc[i].subject_key);
209 if (0 != dc[i].subject_attribute_len)
211 printf ("(%d) %s.%s <- %s.%s\n", i,
212 iss_key, dc[i].issuer_attribute,
213 sub_key, dc[i].subject_attribute);
215 printf ("(%d) %s.%s <- %s\n", i,
216 iss_key, dc[i].issuer_attribute,
219 GNUNET_free (iss_key);
220 GNUNET_free (sub_key);
222 printf("\nCredentials:\n");
223 for (i=0;i<c_count;i++)
225 iss_key = GNUNET_CRYPTO_ecdsa_public_key_to_string (&cred[i].issuer_key);
226 sub_key = GNUNET_CRYPTO_ecdsa_public_key_to_string (&cred[i].subject_key);
227 printf ("%s.%s <- %s\n",
228 iss_key, cred[i].issuer_attribute,
230 GNUNET_free (iss_key);
231 GNUNET_free (sub_key);
234 printf ("Successful.\n");
238 GNUNET_SCHEDULER_shutdown ();
242 * Callback invoked from identity service with ego information.
243 * An @a ego of NULL means the ego was not found.
245 * @param cls closure with the configuration
246 * @param ego an ego known to identity service, or NULL
249 identity_cb (void *cls,
250 const struct GNUNET_IDENTITY_Ego *ego)
252 const struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
253 struct GNUNET_CREDENTIAL_Credential *crd;
254 struct GNUNET_TIME_Absolute etime_abs;
255 struct GNUNET_TIME_Relative etime_rel;
261 if (NULL != ego_name)
264 _("Ego `%s' not known to identity service\n"),
267 GNUNET_SCHEDULER_shutdown ();
271 if (GNUNET_YES == collect)
275 GNUNET_CRYPTO_ecdsa_public_key_from_string (issuer_key,
280 _("Issuer public key `%s' is not well-formed\n"),
282 GNUNET_SCHEDULER_shutdown ();
284 privkey = GNUNET_IDENTITY_ego_get_private_key (ego);
286 collect_request = GNUNET_CREDENTIAL_collect(credential,
288 issuer_attr, //TODO argument
290 &handle_collect_result,
297 if (NULL == expiration)
300 "Please specify a TTL\n");
301 GNUNET_SCHEDULER_shutdown ();
303 } else if (GNUNET_OK == GNUNET_STRINGS_fancy_time_to_relative (expiration,
306 etime_abs = GNUNET_TIME_relative_to_absolute (etime_rel);
307 } else if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_absolute (expiration,
311 "%s is not a valid ttl!\n",
313 GNUNET_SCHEDULER_shutdown ();
318 privkey = GNUNET_IDENTITY_ego_get_private_key (ego);
319 GNUNET_free_non_null (ego_name);
321 crd = GNUNET_CREDENTIAL_credential_issue (privkey,
326 res = GNUNET_CREDENTIAL_credential_to_string (crd);
328 printf ("%s\n", res);
329 GNUNET_SCHEDULER_shutdown ();
336 * Main function that will be run.
339 * @param args remaining command-line arguments
340 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
341 * @param c configuration
347 const struct GNUNET_CONFIGURATION_Handle *c)
353 tt = GNUNET_SCHEDULER_add_delayed (timeout,
355 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
357 if (GNUNET_YES == collect) {
358 if (NULL == issuer_key)
361 _("Issuer public key not well-formed\n"));
362 GNUNET_SCHEDULER_shutdown ();
367 credential = GNUNET_CREDENTIAL_connect (cfg);
369 if (NULL == credential)
372 _("Failed to connect to CREDENTIAL\n"));
373 GNUNET_SCHEDULER_shutdown ();
376 if (NULL == issuer_attr)
379 _("You must provide issuer the attribute\n"));
380 GNUNET_SCHEDULER_shutdown ();
384 if (NULL == ego_name)
387 _("ego required\n"));
388 GNUNET_SCHEDULER_shutdown ();
392 el = GNUNET_IDENTITY_ego_lookup (cfg,
400 if (NULL == subject_key)
403 _("Subject public key needed\n"));
404 GNUNET_SCHEDULER_shutdown ();
409 GNUNET_CRYPTO_ecdsa_public_key_from_string (subject_key,
410 strlen (subject_key),
414 _("Subject public key `%s' is not well-formed\n"),
416 GNUNET_SCHEDULER_shutdown ();
419 if (GNUNET_YES == verify) {
420 if (NULL == issuer_key)
423 _("Issuer public key not well-formed\n"));
424 GNUNET_SCHEDULER_shutdown ();
429 GNUNET_CRYPTO_ecdsa_public_key_from_string (issuer_key,
434 _("Issuer public key `%s' is not well-formed\n"),
436 GNUNET_SCHEDULER_shutdown ();
439 credential = GNUNET_CREDENTIAL_connect (cfg);
441 if (NULL == credential)
444 _("Failed to connect to CREDENTIAL\n"));
445 GNUNET_SCHEDULER_shutdown ();
448 if (NULL == issuer_attr || NULL == subject_credential)
451 _("You must provide issuer and subject attributes\n"));
452 GNUNET_SCHEDULER_shutdown ();
456 //Subject credentials are comma separated
457 char *tmp = GNUNET_strdup (subject_credential);
458 char *tok = strtok (tmp, ",");
462 "Invalid subject credentials\n");
464 GNUNET_SCHEDULER_shutdown ();
469 while (NULL != (tok = strtok(NULL, ",")))
471 struct GNUNET_CREDENTIAL_Credential credentials[count];
472 struct GNUNET_CREDENTIAL_Credential *cred;
474 tmp = GNUNET_strdup (subject_credential);
475 tok = strtok (tmp, ",");
476 for (i=0;i<count;i++)
478 cred = GNUNET_CREDENTIAL_credential_from_string (tok);
479 GNUNET_memcpy (&credentials[i],
481 sizeof (struct GNUNET_CREDENTIAL_Credential));
482 credentials[i].issuer_attribute = GNUNET_strdup (cred->issuer_attribute);
483 tok = strtok(NULL, ",");
487 verify_request = GNUNET_CREDENTIAL_verify(credential,
489 issuer_attr, //TODO argument
493 &handle_verify_result,
495 for (i=0;i<count;i++)
497 GNUNET_free ((char*)credentials[i].issuer_attribute);
500 } else if (GNUNET_YES == create_cred) {
501 if (NULL == ego_name)
504 _("Issuer ego required\n"));
505 GNUNET_SCHEDULER_shutdown ();
509 el = GNUNET_IDENTITY_ego_lookup (cfg,
516 _("Please specify name to lookup, subject key and issuer key!\n"));
517 GNUNET_SCHEDULER_shutdown ();
524 * The main function for gnunet-gns.
526 * @param argc number of arguments from the command line
527 * @param argv command line arguments
528 * @return 0 ok, 1 on error
531 main (int argc, char *const *argv)
533 struct GNUNET_GETOPT_CommandLineOption options[] = {
534 GNUNET_GETOPT_option_flag ('I',
536 gettext_noop ("create credential"),
538 GNUNET_GETOPT_option_flag ('V',
540 gettext_noop ("verify credential against attribute"),
542 GNUNET_GETOPT_option_string ('s',
545 gettext_noop ("The public key of the subject to lookup the credential for"),
547 GNUNET_GETOPT_option_string ('b',
550 gettext_noop ("The name of the credential presented by the subject"),
551 &subject_credential),
552 GNUNET_GETOPT_option_string ('i',
555 gettext_noop ("The public key of the authority to verify the credential against"),
557 GNUNET_GETOPT_option_string ('e',
560 gettext_noop ("The ego to use"),
562 GNUNET_GETOPT_option_string ('a',
565 gettext_noop ("The issuer attribute to verify against or to issue"),
567 GNUNET_GETOPT_option_string ('T',
570 gettext_noop ("The time to live for the credential"),
572 GNUNET_GETOPT_option_flag ('g',
574 gettext_noop ("collect credentials"),
576 GNUNET_GETOPT_OPTION_END
580 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
581 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
584 GNUNET_log_setup ("gnunet-credential", "WARNING", NULL);
587 GNUNET_PROGRAM_run (argc, argv, "gnunet-credential",
588 _("GNUnet credential resolver tool"),
590 &run, NULL)) ? 0 : 1;
591 GNUNET_free ((void*) argv);
595 /* end of gnunet-credential.c */