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 struct GNUNET_CREDENTIAL_CredentialRecordData *cred,
161 uint32_t delegation_count,
162 struct GNUNET_CREDENTIAL_AttributeRecordData *deleg)
166 verify_request = NULL;
168 printf ("Verify failed.\n");
170 printf ("Successful.\n");
173 GNUNET_SCHEDULER_shutdown ();
177 * Callback invoked from identity service with ego information.
178 * An @a ego of NULL means the ego was not found.
180 * @param cls closure with the configuration
181 * @param ego an ego known to identity service, or NULL
184 identity_cb (void *cls,
185 const struct GNUNET_IDENTITY_Ego *ego)
187 const struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
188 struct GNUNET_CREDENTIAL_CredentialRecordData *crd;
189 struct GNUNET_TIME_Absolute etime_abs;
190 struct GNUNET_TIME_Relative etime_rel;
196 if (NULL != issuer_ego_name)
199 _("Ego `%s' not known to identity service\n"),
202 GNUNET_SCHEDULER_shutdown ();
205 if (NULL == expiration)
208 "Please specify a TTL\n");
209 GNUNET_SCHEDULER_shutdown ();
211 } else if (GNUNET_OK == GNUNET_STRINGS_fancy_time_to_relative (expiration,
214 etime_abs = GNUNET_TIME_relative_to_absolute (etime_rel);
215 } else if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_absolute (expiration,
219 "%s is not a valid ttl!\n",
221 GNUNET_SCHEDULER_shutdown ();
226 privkey = GNUNET_IDENTITY_ego_get_private_key (ego);
227 GNUNET_free_non_null (issuer_ego_name);
228 issuer_ego_name = NULL;
229 crd = GNUNET_CREDENTIAL_issue (credential,
234 res = GNUNET_GNSRECORD_value_to_string (GNUNET_GNSRECORD_TYPE_CREDENTIAL,
236 sizeof (struct GNUNET_CREDENTIAL_CredentialRecordData) + strlen (issuer_attr) + 1);
237 printf ("%s\n", res);
238 GNUNET_SCHEDULER_shutdown ();
245 * Main function that will be run.
248 * @param args remaining command-line arguments
249 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
250 * @param c configuration
256 const struct GNUNET_CONFIGURATION_Handle *c)
262 tt = GNUNET_SCHEDULER_add_delayed (timeout,
264 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
268 if (NULL == subject_key)
271 _("Subject public key needed\n"));
272 GNUNET_SCHEDULER_shutdown ();
277 GNUNET_CRYPTO_ecdsa_public_key_from_string (subject_key,
278 strlen (subject_key),
282 _("Subject public key `%s' is not well-formed\n"),
284 GNUNET_SCHEDULER_shutdown ();
288 if (GNUNET_YES == verify) {
289 if (NULL == issuer_key)
292 _("Issuer public key not well-formed\n"));
293 GNUNET_SCHEDULER_shutdown ();
298 GNUNET_CRYPTO_ecdsa_public_key_from_string (issuer_key,
303 _("Issuer public key `%s' is not well-formed\n"),
305 GNUNET_SCHEDULER_shutdown ();
307 credential = GNUNET_CREDENTIAL_connect (cfg);
309 if (NULL == credential)
312 _("Failed to connect to CREDENTIAL\n"));
313 GNUNET_SCHEDULER_shutdown ();
316 if (NULL == issuer_attr || NULL == subject_credential)
319 _("You must provide issuer and subject attributes\n"));
320 GNUNET_SCHEDULER_shutdown ();
324 verify_request = GNUNET_CREDENTIAL_verify(credential,
326 issuer_attr, //TODO argument
329 &handle_verify_result,
331 } else if (GNUNET_YES == create_cred) {
332 if (NULL == issuer_ego_name)
335 _("Issuer ego required\n"));
336 GNUNET_SCHEDULER_shutdown ();
340 el = GNUNET_IDENTITY_ego_lookup (cfg,
347 _("Please specify name to lookup, subject key and issuer key!\n"));
348 GNUNET_SCHEDULER_shutdown ();
355 * The main function for gnunet-gns.
357 * @param argc number of arguments from the command line
358 * @param argv command line arguments
359 * @return 0 ok, 1 on error
362 main (int argc, char *const *argv)
364 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
366 gettext_noop ("create credential"), 0,
367 &GNUNET_GETOPT_set_one, &create_cred},
368 {'V', "verify", NULL,
369 gettext_noop ("verify credential against attribute"), 0,
370 &GNUNET_GETOPT_set_one, &verify},
371 {'s', "subject", "PKEY",
372 gettext_noop ("The public key of the subject to lookup the credential for"), 1,
373 &GNUNET_GETOPT_set_string, &subject_key},
374 {'b', "credential", "CRED",
375 gettext_noop ("The name of the credential presented by the subject"), 1,
376 &GNUNET_GETOPT_set_string, &subject_credential},
377 {'i', "issuer", "PKEY",
378 gettext_noop ("The public key of the authority to verify the credential against"), 1,
379 &GNUNET_GETOPT_set_string, &issuer_key},
381 gettext_noop ("The ego to use to issue"), 1,
382 &GNUNET_GETOPT_set_string, &issuer_ego_name},
383 {'a', "attribute", "ATTR",
384 gettext_noop ("The issuer attribute to verify against or to issue"), 1,
385 &GNUNET_GETOPT_set_string, &issuer_attr},
387 gettext_noop ("The time to live for the credential"), 1,
388 &GNUNET_GETOPT_set_string, &expiration},
389 GNUNET_GETOPT_OPTION_END
393 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
394 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
397 GNUNET_log_setup ("gnunet-credential", "WARNING", NULL);
400 GNUNET_PROGRAM_run (argc, argv, "gnunet-credential",
401 _("GNUnet credential resolver tool"),
403 &run, NULL)) ? 0 : 1;
404 GNUNET_free ((void*) argv);
408 /* end of gnunet-credential.c */