Merge branch 'master' of gnunet.org:gnunet
[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 it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 /**
19  * @file gnunet-credential.c
20  * @brief command line tool to access command line Credential service
21  * @author Martin Schanzenbach
22  */
23 #include "platform.h"
24 #include <gnunet_util_lib.h>
25 #include <gnunet_credential_service.h>
26 #include <gnunet_gnsrecord_lib.h>
27 #include "credential_misc.h"
28 #include "credential_serialization.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  * Handle to collect request
57  */
58 static struct GNUNET_CREDENTIAL_Request *collect_request;
59
60 /**
61  * Task scheduled to handle timeout.
62  */
63 static struct GNUNET_SCHEDULER_Task *tt;
64
65 /**
66  * Subject pubkey string
67  */
68 static char *subject_key;
69
70 /**
71  * Subject credential string
72  */
73 static char *subject_credential;
74
75 /**
76  * Credential TTL
77  */
78 static char *expiration;
79
80 /**
81  * Subject key
82  */
83 struct GNUNET_CRYPTO_EcdsaPublicKey subject_pkey;
84
85 /**
86  * Issuer key
87  */
88 struct GNUNET_CRYPTO_EcdsaPublicKey issuer_pkey;
89
90
91 /**
92  * Issuer pubkey string
93  */
94 static char *issuer_key;
95
96 /**
97  * ego
98  */
99 static char *ego_name;
100
101 /**
102  * Issuer attribute
103  */
104 static char *issuer_attr;
105
106 /**
107  * Verify mode
108  */
109 static int verify;
110
111 /**
112  * Issue mode
113  */
114 static int create_cred;
115
116 /**
117  * Collect mode
118  */
119 static int collect;
120
121 /**
122  * Task run on shutdown.  Cleans up everything.
123  *
124  * @param cls unused
125  */
126 static void
127 do_shutdown (void *cls)
128 {
129   if (NULL != verify_request)
130   {
131     GNUNET_CREDENTIAL_request_cancel (verify_request);
132     verify_request = NULL;
133   }
134   if (NULL != credential)
135   {
136     GNUNET_CREDENTIAL_disconnect (credential);
137     credential = NULL;
138   }
139   if (NULL != tt)
140   {
141     GNUNET_SCHEDULER_cancel (tt);
142     tt = NULL;
143   }
144 }
145
146
147 /**
148  * Task run on timeout. Triggers shutdown.
149  *
150  * @param cls unused
151  */
152 static void
153 do_timeout (void *cls)
154 {
155   tt = NULL;
156   GNUNET_SCHEDULER_shutdown ();
157 }
158
159 static void
160 handle_collect_result (void *cls,
161                       unsigned int d_count,
162                       struct GNUNET_CREDENTIAL_Delegation *dc,
163                       unsigned int c_count,
164                       struct GNUNET_CREDENTIAL_Credential *cred)
165 {
166   int i;
167   char* line;
168
169   verify_request = NULL;
170   if (NULL != cred)
171   {
172     for (i=0;i<c_count;i++)
173     {
174       line = GNUNET_CREDENTIAL_credential_to_string (&cred[i]);
175       printf ("%s\n",
176               line);
177       GNUNET_free (line);
178     }
179   }
180
181
182   GNUNET_SCHEDULER_shutdown ();
183 }
184
185
186 static void
187 handle_verify_result (void *cls,
188                       unsigned int d_count,
189                       struct GNUNET_CREDENTIAL_Delegation *dc,
190                       unsigned int c_count,
191                       struct GNUNET_CREDENTIAL_Credential *cred)
192 {
193   int i;
194   char* iss_key;
195   char* sub_key;
196
197   verify_request = NULL;
198   if (NULL == cred)
199     printf ("Failed.\n");
200   else
201   {
202     printf("Delegation Chain:\n");
203     for (i=0;i<d_count;i++)
204     {
205       iss_key = GNUNET_CRYPTO_ecdsa_public_key_to_string (&dc[i].issuer_key);
206       sub_key = GNUNET_CRYPTO_ecdsa_public_key_to_string (&dc[i].subject_key);
207       if (0 != dc[i].subject_attribute_len)
208       {
209         printf ("(%d) %s.%s <- %s.%s\n", i,
210                 iss_key, dc[i].issuer_attribute,
211                 sub_key, dc[i].subject_attribute);
212       } else {
213         printf ("(%d) %s.%s <- %s\n", i,
214                 iss_key, dc[i].issuer_attribute,
215                 sub_key);
216       }
217       GNUNET_free (iss_key);
218       GNUNET_free (sub_key);
219     }
220     printf("\nCredentials:\n");
221     for (i=0;i<c_count;i++)
222     {
223       iss_key = GNUNET_CRYPTO_ecdsa_public_key_to_string (&cred[i].issuer_key);
224       sub_key = GNUNET_CRYPTO_ecdsa_public_key_to_string (&cred[i].subject_key);
225       printf ("%s.%s <- %s\n",
226               iss_key, cred[i].issuer_attribute,
227               sub_key);
228       GNUNET_free (iss_key);
229       GNUNET_free (sub_key);
230
231     }
232     printf ("Successful.\n");
233   }
234
235
236   GNUNET_SCHEDULER_shutdown ();
237 }
238
239 /**
240  * Callback invoked from identity service with ego information.
241  * An @a ego of NULL means the ego was not found.
242  *
243  * @param cls closure with the configuration
244  * @param ego an ego known to identity service, or NULL
245  */
246 static void
247 identity_cb (void *cls,
248              const struct GNUNET_IDENTITY_Ego *ego)
249 {
250   const struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
251   struct GNUNET_CREDENTIAL_Credential *crd;
252   struct GNUNET_TIME_Absolute etime_abs;
253   struct GNUNET_TIME_Relative etime_rel;
254   char *res;
255
256   el = NULL;
257   if (NULL == ego)
258   {
259     if (NULL != ego_name)
260     {
261       fprintf (stderr,
262                _("Ego `%s' not known to identity service\n"),
263                ego_name);
264     }
265     GNUNET_SCHEDULER_shutdown ();
266     return;
267   }
268
269   if (GNUNET_YES == collect)
270   {
271     
272     if (GNUNET_OK !=
273         GNUNET_CRYPTO_ecdsa_public_key_from_string (issuer_key,
274                                                     strlen (issuer_key),
275                                                     &issuer_pkey))
276     {
277       fprintf (stderr,
278                _("Issuer public key `%s' is not well-formed\n"),
279                issuer_key);
280       GNUNET_SCHEDULER_shutdown ();
281     }
282     privkey = GNUNET_IDENTITY_ego_get_private_key (ego);
283
284     collect_request = GNUNET_CREDENTIAL_collect(credential,
285                                                 &issuer_pkey,
286                                                 issuer_attr, //TODO argument
287                                                 privkey,
288                                                 &handle_collect_result,
289                                                 NULL);
290     return;
291   }
292
293   //Else issue
294
295   if (NULL == expiration)
296   {
297     fprintf (stderr,
298              "Please specify a TTL\n");
299     GNUNET_SCHEDULER_shutdown ();
300     return;
301   } else if (GNUNET_OK == GNUNET_STRINGS_fancy_time_to_relative (expiration,
302                                                                  &etime_rel))
303   {
304     etime_abs = GNUNET_TIME_relative_to_absolute (etime_rel);
305   } else if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_absolute (expiration,
306                                                                  &etime_abs))
307   {
308     fprintf (stderr,
309              "%s is not a valid ttl!\n",
310              expiration);
311     GNUNET_SCHEDULER_shutdown ();
312     return;
313   }
314
315
316   privkey = GNUNET_IDENTITY_ego_get_private_key (ego);
317   GNUNET_free_non_null (ego_name);
318   ego_name = NULL;
319   crd = GNUNET_CREDENTIAL_credential_issue (privkey,
320                                             &subject_pkey,
321                                             issuer_attr,
322                                             &etime_abs);
323
324   res = GNUNET_CREDENTIAL_credential_to_string (crd);
325   GNUNET_free (crd);
326   printf ("%s\n", res);
327   GNUNET_SCHEDULER_shutdown ();
328 }
329
330
331
332
333 /**
334  * Main function that will be run.
335  *
336  * @param cls closure
337  * @param args remaining command-line arguments
338  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
339  * @param c configuration
340  */
341 static void
342 run (void *cls,
343      char *const *args,
344      const char *cfgfile,
345      const struct GNUNET_CONFIGURATION_Handle *c)
346 {
347
348   cfg = c;
349
350
351   tt = GNUNET_SCHEDULER_add_delayed (timeout,
352                                      &do_timeout, NULL);
353   GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
354
355   if (GNUNET_YES == collect) {
356     if (NULL == issuer_key)
357     {
358       fprintf (stderr,
359                _("Issuer public key not well-formed\n"));
360       GNUNET_SCHEDULER_shutdown ();
361       return;
362
363     }
364
365     credential = GNUNET_CREDENTIAL_connect (cfg);
366
367     if (NULL == credential)
368     {
369       fprintf (stderr,
370                _("Failed to connect to CREDENTIAL\n"));
371       GNUNET_SCHEDULER_shutdown ();
372       return;
373     }
374     if (NULL == issuer_attr)
375     {
376       fprintf (stderr,
377                _("You must provide issuer the attribute\n"));
378       GNUNET_SCHEDULER_shutdown ();
379       return;
380     }
381
382     if (NULL == ego_name)
383     {
384       fprintf (stderr,
385                _("ego required\n"));
386       GNUNET_SCHEDULER_shutdown ();
387       return;
388
389     }
390     el = GNUNET_IDENTITY_ego_lookup (cfg,
391                                      ego_name,
392                                      &identity_cb,
393                                      (void *) cfg);
394     return;
395
396   } 
397
398   if (NULL == subject_key)
399   {
400     fprintf (stderr,
401              _("Subject public key needed\n"));
402     GNUNET_SCHEDULER_shutdown ();
403     return;
404
405   }
406   if (GNUNET_OK !=
407       GNUNET_CRYPTO_ecdsa_public_key_from_string (subject_key,
408                                                   strlen (subject_key),
409                                                   &subject_pkey))
410   {
411     fprintf (stderr,
412              _("Subject public key `%s' is not well-formed\n"),
413              subject_key);
414     GNUNET_SCHEDULER_shutdown ();
415     return;
416   }
417   if (GNUNET_YES == verify) {
418     if (NULL == issuer_key)
419     {
420       fprintf (stderr,
421                _("Issuer public key not well-formed\n"));
422       GNUNET_SCHEDULER_shutdown ();
423       return;
424
425     }
426     if (GNUNET_OK !=
427         GNUNET_CRYPTO_ecdsa_public_key_from_string (issuer_key,
428                                                     strlen (issuer_key),
429                                                     &issuer_pkey))
430     {
431       fprintf (stderr,
432                _("Issuer public key `%s' is not well-formed\n"),
433                issuer_key);
434       GNUNET_SCHEDULER_shutdown ();
435       return;
436     }
437     credential = GNUNET_CREDENTIAL_connect (cfg);
438
439     if (NULL == credential)
440     {
441       fprintf (stderr,
442                _("Failed to connect to CREDENTIAL\n"));
443       GNUNET_SCHEDULER_shutdown ();
444       return;
445     }
446     if (NULL == issuer_attr || NULL == subject_credential)
447     {
448       fprintf (stderr,
449                _("You must provide issuer and subject attributes\n"));
450       GNUNET_SCHEDULER_shutdown ();
451       return;
452     }
453
454     //Subject credentials are comma separated
455     char *tmp = GNUNET_strdup (subject_credential);
456     char *tok = strtok (tmp, ",");
457     if (NULL == tok)
458     {
459       fprintf (stderr,
460                "Invalid subject credentials\n");
461       GNUNET_free (tmp);
462       GNUNET_SCHEDULER_shutdown ();
463       return;
464     }
465     int count = 1;
466     int i;
467     while (NULL != (tok = strtok(NULL, ",")))
468       count++;
469     struct GNUNET_CREDENTIAL_Credential credentials[count];
470     struct GNUNET_CREDENTIAL_Credential *cred;
471     GNUNET_free (tmp);
472     tmp = GNUNET_strdup (subject_credential);
473     tok = strtok (tmp, ",");
474     for (i=0;i<count;i++)
475     {
476       cred = GNUNET_CREDENTIAL_credential_from_string (tok);
477       GNUNET_memcpy (&credentials[i],
478                      cred,
479                      sizeof (struct GNUNET_CREDENTIAL_Credential));
480       credentials[i].issuer_attribute = GNUNET_strdup (cred->issuer_attribute);
481       tok = strtok(NULL, ",");
482       GNUNET_free (cred);
483     }
484
485     verify_request = GNUNET_CREDENTIAL_verify(credential,
486                                               &issuer_pkey,
487                                               issuer_attr, //TODO argument
488                                               &subject_pkey,
489                                               count,
490                                               credentials,
491                                               &handle_verify_result,
492                                               NULL);
493     for (i=0;i<count;i++)
494     {
495       GNUNET_free ((char*)credentials[i].issuer_attribute);
496     }
497     GNUNET_free (tmp);
498   } else if (GNUNET_YES == create_cred) {
499     if (NULL == ego_name)
500     {
501       fprintf (stderr,
502                _("Issuer ego required\n"));
503       GNUNET_SCHEDULER_shutdown ();
504       return;
505
506     }
507     el = GNUNET_IDENTITY_ego_lookup (cfg,
508                                      ego_name,
509                                      &identity_cb,
510                                      (void *) cfg);
511     return;
512   } else {
513     fprintf (stderr,
514              _("Please specify name to lookup, subject key and issuer key!\n"));
515     GNUNET_SCHEDULER_shutdown ();
516   }
517   return;
518 }
519
520
521 /**
522  * The main function for gnunet-gns.
523  *
524  * @param argc number of arguments from the command line
525  * @param argv command line arguments
526  * @return 0 ok, 1 on error
527  */
528 int
529 main (int argc, char *const *argv)
530 {
531   struct GNUNET_GETOPT_CommandLineOption options[] = {
532     GNUNET_GETOPT_option_flag ('I',
533                                "issue",
534                                gettext_noop ("create credential"),
535                                &create_cred),
536     GNUNET_GETOPT_option_flag ('V',
537                                "verify",
538                                gettext_noop ("verify credential against attribute"),
539                                &verify),
540     GNUNET_GETOPT_option_string ('s',
541                                  "subject",
542                                  "PKEY",
543                                  gettext_noop ("The public key of the subject to lookup the credential for"),
544                                  &subject_key),
545     GNUNET_GETOPT_option_string ('b',
546                                  "credential",
547                                  "CRED",
548                                  gettext_noop ("The name of the credential presented by the subject"),
549                                  &subject_credential),
550     GNUNET_GETOPT_option_string ('i',
551                                  "issuer",
552                                  "PKEY",
553                                  gettext_noop ("The public key of the authority to verify the credential against"),
554                                  &issuer_key),
555     GNUNET_GETOPT_option_string ('e',
556                                  "ego",
557                                  "EGO",
558                                  gettext_noop ("The ego to use"),
559                                  &ego_name),
560     GNUNET_GETOPT_option_string ('a',
561                                  "attribute",
562                                  "ATTR",
563                                  gettext_noop ("The issuer attribute to verify against or to issue"),
564                                  &issuer_attr),
565     GNUNET_GETOPT_option_string ('T',
566                                  "ttl",
567                                  "EXP",
568                                  gettext_noop ("The time to live for the credential"),
569                                  &expiration),
570     GNUNET_GETOPT_option_flag ('g',
571                                "collect",
572                                gettext_noop ("collect credentials"),
573                                &collect),
574     GNUNET_GETOPT_OPTION_END
575   };
576   int ret;
577
578   timeout = GNUNET_TIME_UNIT_FOREVER_REL;
579   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
580     return 2;
581
582   GNUNET_log_setup ("gnunet-credential", "WARNING", NULL);
583   ret =
584     (GNUNET_OK ==
585      GNUNET_PROGRAM_run (argc, argv, "gnunet-credential",
586                          _("GNUnet credential resolver tool"),
587                          options,
588                          &run, NULL)) ? 0 : 1;
589   GNUNET_free ((void*) argv);
590   return ret;
591 }
592
593 /* end of gnunet-credential.c */