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 Adnan Husain
26 #include <gnunet_util_lib.h>
27 #include <gnunet_credential_service.h>
28 #include <gnunet_gnsrecord_lib.h>
31 * Configuration we are using.
33 static const struct GNUNET_CONFIGURATION_Handle *cfg;
38 static struct GNUNET_IDENTITY_EgoLookup *el;
41 * Handle to Credential service.
43 static struct GNUNET_CREDENTIAL_Handle *credential;
46 * Desired timeout for the lookup (default is no timeout).
48 static struct GNUNET_TIME_Relative timeout;
51 * Handle to verify request
53 static struct GNUNET_CREDENTIAL_Request *verify_request;
56 * Task scheduled to handle timeout.
58 static struct GNUNET_SCHEDULER_Task *tt;
61 * Subject pubkey string
63 static char *subject_key;
66 * Subject credential string
68 static char *subject_credential;
73 static char *expiration;
78 struct GNUNET_CRYPTO_EcdsaPublicKey subject_pkey;
83 struct GNUNET_CRYPTO_EcdsaPublicKey issuer_pkey;
87 * Issuer pubkey string
89 static char *issuer_key;
94 static char *issuer_ego_name;
99 static char *issuer_attr;
104 static uint32_t verify;
109 static uint32_t create_cred;
113 * Task run on shutdown. Cleans up everything.
118 do_shutdown (void *cls)
120 if (NULL != verify_request)
122 GNUNET_CREDENTIAL_verify_cancel (verify_request);
123 verify_request = NULL;
125 if (NULL != credential)
127 GNUNET_CREDENTIAL_disconnect (credential);
132 GNUNET_SCHEDULER_cancel (tt);
139 * Task run on timeout. Triggers shutdown.
144 do_timeout (void *cls)
147 GNUNET_SCHEDULER_shutdown ();
152 * Function called with the result of a Credential lookup.
154 * @param cls the 'const char *' name that was resolved
155 * @param cd_count number of records returned
156 * @param cd array of @a cd_count records with the results
159 handle_verify_result (void *cls,
160 unsigned int d_count,
161 struct GNUNET_CREDENTIAL_Delegation *dc,
162 struct GNUNET_CREDENTIAL_Credential *cred)
166 verify_request = NULL;
168 printf ("Failed.\n");
171 for (i=0;i<d_count;i++)
173 char iss_attr[dc[i].issuer_attribute_len];
174 char* iss_key = GNUNET_CRYPTO_ecdsa_public_key_to_string (&dc[i].issuer_key);
175 char* sub_key = GNUNET_CRYPTO_ecdsa_public_key_to_string (&dc[i].subject_key);
177 char sub_attr[dc[i].subject_attribute_len];
179 dc[i].issuer_attribute,
180 dc[i].issuer_attribute_len);
181 iss_attr[dc[i].issuer_attribute_len] = '\0';
182 printf ("%s.%s <- ",iss_key, iss_attr);
183 printf ("%s",sub_key);
184 if (0 != dc[i].subject_attribute_len)
187 dc[i].subject_attribute,
188 dc[i].subject_attribute_len);
189 sub_attr[dc[i].subject_attribute_len] = '\0';
191 printf (".%s",sub_attr);
195 printf ("Successful.\n");
199 GNUNET_SCHEDULER_shutdown ();
203 * Callback invoked from identity service with ego information.
204 * An @a ego of NULL means the ego was not found.
206 * @param cls closure with the configuration
207 * @param ego an ego known to identity service, or NULL
210 identity_cb (void *cls,
211 const struct GNUNET_IDENTITY_Ego *ego)
213 const struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
214 struct GNUNET_CREDENTIAL_CredentialRecordData *crd;
215 struct GNUNET_TIME_Absolute etime_abs;
216 struct GNUNET_TIME_Relative etime_rel;
222 if (NULL != issuer_ego_name)
225 _("Ego `%s' not known to identity service\n"),
228 GNUNET_SCHEDULER_shutdown ();
231 if (NULL == expiration)
234 "Please specify a TTL\n");
235 GNUNET_SCHEDULER_shutdown ();
237 } else if (GNUNET_OK == GNUNET_STRINGS_fancy_time_to_relative (expiration,
240 etime_abs = GNUNET_TIME_relative_to_absolute (etime_rel);
241 } else if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_absolute (expiration,
245 "%s is not a valid ttl!\n",
247 GNUNET_SCHEDULER_shutdown ();
252 privkey = GNUNET_IDENTITY_ego_get_private_key (ego);
253 GNUNET_free_non_null (issuer_ego_name);
254 issuer_ego_name = NULL;
255 crd = GNUNET_CREDENTIAL_issue (credential,
260 res = GNUNET_GNSRECORD_value_to_string (GNUNET_GNSRECORD_TYPE_CREDENTIAL,
262 sizeof (struct GNUNET_CREDENTIAL_CredentialRecordData) + strlen (issuer_attr) + 1);
263 printf ("%s\n", res);
264 GNUNET_SCHEDULER_shutdown ();
271 * Main function that will be run.
274 * @param args remaining command-line arguments
275 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
276 * @param c configuration
282 const struct GNUNET_CONFIGURATION_Handle *c)
288 tt = GNUNET_SCHEDULER_add_delayed (timeout,
290 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
294 if (NULL == subject_key)
297 _("Subject public key needed\n"));
298 GNUNET_SCHEDULER_shutdown ();
303 GNUNET_CRYPTO_ecdsa_public_key_from_string (subject_key,
304 strlen (subject_key),
308 _("Subject public key `%s' is not well-formed\n"),
310 GNUNET_SCHEDULER_shutdown ();
314 if (GNUNET_YES == verify) {
315 if (NULL == issuer_key)
318 _("Issuer public key not well-formed\n"));
319 GNUNET_SCHEDULER_shutdown ();
324 GNUNET_CRYPTO_ecdsa_public_key_from_string (issuer_key,
329 _("Issuer public key `%s' is not well-formed\n"),
331 GNUNET_SCHEDULER_shutdown ();
333 credential = GNUNET_CREDENTIAL_connect (cfg);
335 if (NULL == credential)
338 _("Failed to connect to CREDENTIAL\n"));
339 GNUNET_SCHEDULER_shutdown ();
342 if (NULL == issuer_attr || NULL == subject_credential)
345 _("You must provide issuer and subject attributes\n"));
346 GNUNET_SCHEDULER_shutdown ();
350 verify_request = GNUNET_CREDENTIAL_verify(credential,
352 issuer_attr, //TODO argument
355 &handle_verify_result,
357 } else if (GNUNET_YES == create_cred) {
358 if (NULL == issuer_ego_name)
361 _("Issuer ego required\n"));
362 GNUNET_SCHEDULER_shutdown ();
366 el = GNUNET_IDENTITY_ego_lookup (cfg,
373 _("Please specify name to lookup, subject key and issuer key!\n"));
374 GNUNET_SCHEDULER_shutdown ();
381 * The main function for gnunet-gns.
383 * @param argc number of arguments from the command line
384 * @param argv command line arguments
385 * @return 0 ok, 1 on error
388 main (int argc, char *const *argv)
390 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
392 gettext_noop ("create credential"), 0,
393 &GNUNET_GETOPT_set_one, &create_cred},
394 {'V', "verify", NULL,
395 gettext_noop ("verify credential against attribute"), 0,
396 &GNUNET_GETOPT_set_one, &verify},
397 {'s', "subject", "PKEY",
398 gettext_noop ("The public key of the subject to lookup the credential for"), 1,
399 &GNUNET_GETOPT_set_string, &subject_key},
400 {'b', "credential", "CRED",
401 gettext_noop ("The name of the credential presented by the subject"), 1,
402 &GNUNET_GETOPT_set_string, &subject_credential},
403 {'i', "issuer", "PKEY",
404 gettext_noop ("The public key of the authority to verify the credential against"), 1,
405 &GNUNET_GETOPT_set_string, &issuer_key},
407 gettext_noop ("The ego to use to issue"), 1,
408 &GNUNET_GETOPT_set_string, &issuer_ego_name},
409 {'a', "attribute", "ATTR",
410 gettext_noop ("The issuer attribute to verify against or to issue"), 1,
411 &GNUNET_GETOPT_set_string, &issuer_attr},
413 gettext_noop ("The time to live for the credential"), 1,
414 &GNUNET_GETOPT_set_string, &expiration},
415 GNUNET_GETOPT_OPTION_END
419 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
420 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
423 GNUNET_log_setup ("gnunet-credential", "WARNING", NULL);
426 GNUNET_PROGRAM_run (argc, argv, "gnunet-credential",
427 _("GNUnet credential resolver tool"),
429 &run, NULL)) ? 0 : 1;
430 GNUNET_free ((void*) argv);
434 /* end of gnunet-credential.c */