-add serializer
[oweals/gnunet.git] / src / credential / gnunet-credential.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012-2013 GNUnet e.V.
4
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.
9
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.
14
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.
19 */
20 /**
21  * @file gnunet-credential.c
22  * @brief command line tool to access command line Credential service
23  * @author Adnan Husain
24  */
25 #include "platform.h"
26 #include <gnunet_util_lib.h>
27 #include <gnunet_credential_service.h>
28 #include <gnunet_gnsrecord_lib.h>
29
30 /**
31  * Configuration we are using.
32  */
33 static const struct GNUNET_CONFIGURATION_Handle *cfg;
34
35 /**
36  * EgoLookup
37  */
38 static struct GNUNET_IDENTITY_EgoLookup *el;
39
40 /**
41  * Handle to Credential service.
42  */
43 static struct GNUNET_CREDENTIAL_Handle *credential;
44
45 /**
46  * Desired timeout for the lookup (default is no timeout).
47  */
48 static struct GNUNET_TIME_Relative timeout;
49
50 /**
51  * Handle to verify request
52  */
53 static struct GNUNET_CREDENTIAL_Request *verify_request;
54
55 /**
56  * Task scheduled to handle timeout.
57  */
58 static struct GNUNET_SCHEDULER_Task *tt;
59
60 /**
61  * Subject pubkey string
62  */
63 static char *subject_key;
64
65 /**
66  * Subject credential string
67  */
68 static char *subject_credential;
69
70 /**
71  * Credential TTL
72  */
73 static char *expiration;
74
75 /**
76  * Subject key
77  */
78 struct GNUNET_CRYPTO_EcdsaPublicKey subject_pkey;
79
80 /**
81  * Issuer key
82  */
83 struct GNUNET_CRYPTO_EcdsaPublicKey issuer_pkey;
84
85
86 /**
87  * Issuer pubkey string
88  */
89 static char *issuer_key;
90
91 /**
92  * Issuer ego
93  */
94 static char *issuer_ego_name;
95
96 /**
97  * Issuer attribute
98  */
99 static char *issuer_attr;
100
101 /**
102  * Verify mode
103  */
104 static uint32_t verify;
105
106 /**
107  * Issue mode
108  */
109 static uint32_t create_cred;
110
111
112 /**
113  * Task run on shutdown.  Cleans up everything.
114  *
115  * @param cls unused
116  */
117 static void
118 do_shutdown (void *cls)
119 {
120   if (NULL != verify_request)
121   {
122     GNUNET_CREDENTIAL_verify_cancel (verify_request);
123     verify_request = NULL;
124   }
125   if (NULL != credential)
126   {
127     GNUNET_CREDENTIAL_disconnect (credential);
128     credential = NULL;
129   }
130   if (NULL != tt)
131   {
132     GNUNET_SCHEDULER_cancel (tt);
133     tt = NULL;
134   }
135 }
136
137
138 /**
139  * Task run on timeout. Triggers shutdown.
140  *
141  * @param cls unused
142  */
143 static void
144 do_timeout (void *cls)
145 {
146   tt = NULL;
147   GNUNET_SCHEDULER_shutdown ();
148 }
149
150
151 /**
152  * Function called with the result of a Credential lookup.
153  *
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
157  */
158 static void
159 handle_verify_result (void *cls,
160                       unsigned int d_count,
161                       struct GNUNET_CREDENTIAL_Delegation *dc,
162                       struct GNUNET_CREDENTIAL_Credential *cred)
163 {
164   int i;
165
166   verify_request = NULL;
167   if (NULL == cred)
168     printf ("Failed.\n");
169   else
170   {
171     for (i=0;i<d_count;i++)
172     {
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);
176
177       char sub_attr[dc[i].subject_attribute_len];
178       memcpy (iss_attr,
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)
185       {
186         memcpy (sub_attr,
187                 dc[i].subject_attribute,
188                 dc[i].subject_attribute_len);
189         sub_attr[dc[i].subject_attribute_len] = '\0';
190
191         printf (".%s",sub_attr);
192       }
193       printf ("\n");
194     }
195     printf ("Successful.\n");
196   }
197
198
199   GNUNET_SCHEDULER_shutdown ();
200 }
201
202 /**
203  * Callback invoked from identity service with ego information.
204  * An @a ego of NULL means the ego was not found.
205  *
206  * @param cls closure with the configuration
207  * @param ego an ego known to identity service, or NULL
208  */
209 static void
210 identity_cb (void *cls,
211              const struct GNUNET_IDENTITY_Ego *ego)
212 {
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;
217   char *res;
218
219   el = NULL;
220   if (NULL == ego)
221   {
222     if (NULL != issuer_ego_name)
223     {
224       fprintf (stderr,
225                _("Ego `%s' not known to identity service\n"),
226                issuer_ego_name);
227     }
228     GNUNET_SCHEDULER_shutdown ();
229     return;
230   }
231   if (NULL == expiration)
232   {
233     fprintf (stderr,
234              "Please specify a TTL\n");
235     GNUNET_SCHEDULER_shutdown ();
236     return;
237   } else if (GNUNET_OK == GNUNET_STRINGS_fancy_time_to_relative (expiration,
238                                                                  &etime_rel))
239   {
240     etime_abs = GNUNET_TIME_relative_to_absolute (etime_rel);
241   } else if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_absolute (expiration,
242                                                                  &etime_abs))
243   {
244     fprintf (stderr,
245              "%s is not a valid ttl!\n",
246              expiration);
247     GNUNET_SCHEDULER_shutdown ();
248     return;
249   }
250
251
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,
256                                  privkey,
257                                  &subject_pkey,
258                                  issuer_attr,
259                                  &etime_abs);
260   res =  GNUNET_GNSRECORD_value_to_string (GNUNET_GNSRECORD_TYPE_CREDENTIAL,
261                                            crd,
262                                            sizeof (struct GNUNET_CREDENTIAL_CredentialRecordData) + strlen (issuer_attr) + 1);
263   printf ("%s\n", res);
264   GNUNET_SCHEDULER_shutdown ();
265 }
266
267
268
269
270 /**
271  * Main function that will be run.
272  *
273  * @param cls closure
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
277  */
278 static void
279 run (void *cls,
280      char *const *args,
281      const char *cfgfile,
282      const struct GNUNET_CONFIGURATION_Handle *c)
283 {
284
285   cfg = c;
286
287
288   tt = GNUNET_SCHEDULER_add_delayed (timeout,
289                                      &do_timeout, NULL);
290   GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
291
292
293
294   if (NULL == subject_key)
295   {
296     fprintf (stderr,
297              _("Subject public key needed\n"));
298     GNUNET_SCHEDULER_shutdown ();
299     return;
300
301   }
302   if (GNUNET_OK !=
303       GNUNET_CRYPTO_ecdsa_public_key_from_string (subject_key,
304                                                   strlen (subject_key),
305                                                   &subject_pkey))
306   {
307     fprintf (stderr,
308              _("Subject public key `%s' is not well-formed\n"),
309              subject_key);
310     GNUNET_SCHEDULER_shutdown ();
311     return;
312   }
313
314   if (GNUNET_YES == verify) {
315     if (NULL == issuer_key)
316     {
317       fprintf (stderr,
318                _("Issuer public key not well-formed\n"));
319       GNUNET_SCHEDULER_shutdown ();
320       return;
321
322     }
323     if (GNUNET_OK !=
324         GNUNET_CRYPTO_ecdsa_public_key_from_string (issuer_key,
325                                                     strlen (issuer_key),
326                                                     &issuer_pkey))
327     {
328       fprintf (stderr,
329                _("Issuer public key `%s' is not well-formed\n"),
330                issuer_key);
331       GNUNET_SCHEDULER_shutdown ();
332     }
333     credential = GNUNET_CREDENTIAL_connect (cfg);
334
335     if (NULL == credential)
336     {
337       fprintf (stderr,
338                _("Failed to connect to CREDENTIAL\n"));
339       GNUNET_SCHEDULER_shutdown ();
340     }
341
342     if (NULL == issuer_attr || NULL == subject_credential)
343     {
344       fprintf (stderr,
345                _("You must provide issuer and subject attributes\n"));
346       GNUNET_SCHEDULER_shutdown ();
347     }
348
349
350     verify_request = GNUNET_CREDENTIAL_verify(credential,
351                                               &issuer_pkey,
352                                               issuer_attr, //TODO argument
353                                               &subject_pkey,
354                                               subject_credential,
355                                               &handle_verify_result,
356                                               NULL);
357   } else if (GNUNET_YES == create_cred) {
358     if (NULL == issuer_ego_name)
359     {
360       fprintf (stderr,
361                _("Issuer ego required\n"));
362       GNUNET_SCHEDULER_shutdown ();
363       return;
364
365     }
366     el = GNUNET_IDENTITY_ego_lookup (cfg,
367                                      issuer_ego_name,
368                                      &identity_cb,
369                                      (void *) cfg);
370     return;
371   } else {
372     fprintf (stderr,
373              _("Please specify name to lookup, subject key and issuer key!\n"));
374     GNUNET_SCHEDULER_shutdown ();
375   }
376   return;
377 }
378
379
380 /**
381  * The main function for gnunet-gns.
382  *
383  * @param argc number of arguments from the command line
384  * @param argv command line arguments
385  * @return 0 ok, 1 on error
386  */
387 int
388 main (int argc, char *const *argv)
389 {
390   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
391     {'I', "issue", NULL,
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},
406     {'e', "ego", "EGO",
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},
412     {'T', "ttl", "EXP",
413       gettext_noop ("The time to live for the credential"), 1,
414       &GNUNET_GETOPT_set_string, &expiration},
415     GNUNET_GETOPT_OPTION_END
416   };
417   int ret;
418
419   timeout = GNUNET_TIME_UNIT_FOREVER_REL;
420   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
421     return 2;
422
423   GNUNET_log_setup ("gnunet-credential", "WARNING", NULL);
424   ret =
425     (GNUNET_OK ==
426      GNUNET_PROGRAM_run (argc, argv, "gnunet-credential",
427                          _("GNUnet credential resolver tool"),
428                          options,
429                          &run, NULL)) ? 0 : 1;
430   GNUNET_free ((void*) argv);
431   return ret;
432 }
433
434 /* end of gnunet-credential.c */