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 ();
375 if (NULL == issuer_attr)
378 _("You must provide issuer the attribute\n"));
379 GNUNET_SCHEDULER_shutdown ();
382 if (NULL == ego_name)
385 _("ego required\n"));
386 GNUNET_SCHEDULER_shutdown ();
390 el = GNUNET_IDENTITY_ego_lookup (cfg,
398 if (NULL == subject_key)
401 _("Subject public key needed\n"));
402 GNUNET_SCHEDULER_shutdown ();
407 GNUNET_CRYPTO_ecdsa_public_key_from_string (subject_key,
408 strlen (subject_key),
412 _("Subject public key `%s' is not well-formed\n"),
414 GNUNET_SCHEDULER_shutdown ();
417 if (GNUNET_YES == verify) {
418 if (NULL == issuer_key)
421 _("Issuer public key not well-formed\n"));
422 GNUNET_SCHEDULER_shutdown ();
427 GNUNET_CRYPTO_ecdsa_public_key_from_string (issuer_key,
432 _("Issuer public key `%s' is not well-formed\n"),
434 GNUNET_SCHEDULER_shutdown ();
436 credential = GNUNET_CREDENTIAL_connect (cfg);
438 if (NULL == credential)
441 _("Failed to connect to CREDENTIAL\n"));
442 GNUNET_SCHEDULER_shutdown ();
444 if (NULL == issuer_attr || NULL == subject_credential)
447 _("You must provide issuer and subject attributes\n"));
448 GNUNET_SCHEDULER_shutdown ();
451 //Subject credentials are comma separated
452 char *tmp = GNUNET_strdup (subject_credential);
453 char *tok = strtok (tmp, ",");
457 "Invalid subject credentials\n");
459 GNUNET_SCHEDULER_shutdown ();
463 while (NULL != (tok = strtok(NULL, ",")))
465 struct GNUNET_CREDENTIAL_Credential credentials[count];
466 struct GNUNET_CREDENTIAL_Credential *cred;
468 tmp = GNUNET_strdup (subject_credential);
469 tok = strtok (tmp, ",");
470 for (i=0;i<count;i++)
472 cred = GNUNET_CREDENTIAL_credential_from_string (tok);
473 GNUNET_memcpy (&credentials[i],
475 sizeof (struct GNUNET_CREDENTIAL_Credential));
476 credentials[i].issuer_attribute = GNUNET_strdup (cred->issuer_attribute);
477 tok = strtok(NULL, ",");
481 verify_request = GNUNET_CREDENTIAL_verify(credential,
483 issuer_attr, //TODO argument
487 &handle_verify_result,
489 for (i=0;i<count;i++)
491 GNUNET_free ((char*)credentials[i].issuer_attribute);
493 } else if (GNUNET_YES == create_cred) {
494 if (NULL == ego_name)
497 _("Issuer ego required\n"));
498 GNUNET_SCHEDULER_shutdown ();
502 el = GNUNET_IDENTITY_ego_lookup (cfg,
509 _("Please specify name to lookup, subject key and issuer key!\n"));
510 GNUNET_SCHEDULER_shutdown ();
517 * The main function for gnunet-gns.
519 * @param argc number of arguments from the command line
520 * @param argv command line arguments
521 * @return 0 ok, 1 on error
524 main (int argc, char *const *argv)
526 struct GNUNET_GETOPT_CommandLineOption options[] = {
527 GNUNET_GETOPT_option_flag ('I',
529 gettext_noop ("create credential"),
531 GNUNET_GETOPT_option_flag ('V',
533 gettext_noop ("verify credential against attribute"),
535 GNUNET_GETOPT_option_string ('s',
538 gettext_noop ("The public key of the subject to lookup the credential for"),
540 GNUNET_GETOPT_option_string ('b',
543 gettext_noop ("The name of the credential presented by the subject"),
544 &subject_credential),
545 GNUNET_GETOPT_option_string ('i',
548 gettext_noop ("The public key of the authority to verify the credential against"),
550 GNUNET_GETOPT_option_string ('e',
553 gettext_noop ("The ego to use"),
555 GNUNET_GETOPT_option_string ('a',
558 gettext_noop ("The issuer attribute to verify against or to issue"),
560 GNUNET_GETOPT_option_string ('T',
563 gettext_noop ("The time to live for the credential"),
565 GNUNET_GETOPT_option_flag ('g',
567 gettext_noop ("collect credentials"),
569 GNUNET_GETOPT_OPTION_END
573 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
574 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
577 GNUNET_log_setup ("gnunet-credential", "WARNING", NULL);
580 GNUNET_PROGRAM_run (argc, argv, "gnunet-credential",
581 _("GNUnet credential resolver tool"),
583 &run, NULL)) ? 0 : 1;
584 GNUNET_free ((void*) argv);
588 /* end of gnunet-credential.c */