- add simple verification
[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                       struct GNUNET_CREDENTIAL_CredentialRecordData *cred,
161                       uint32_t delegation_count,
162                       struct GNUNET_CREDENTIAL_AttributeRecordData *deleg)
163 {
164
165
166   verify_request = NULL;
167   if (NULL == cred)
168     printf ("Verify failed.\n");
169   else
170     printf ("Successful.\n");
171
172
173   GNUNET_SCHEDULER_shutdown ();
174 }
175
176 /**
177  * Callback invoked from identity service with ego information.
178  * An @a ego of NULL means the ego was not found.
179  *
180  * @param cls closure with the configuration
181  * @param ego an ego known to identity service, or NULL
182  */
183 static void
184 identity_cb (void *cls,
185              const struct GNUNET_IDENTITY_Ego *ego)
186 {
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;
191   char *res;
192
193   el = NULL;
194   if (NULL == ego)
195   {
196     if (NULL != issuer_ego_name)
197     {
198       fprintf (stderr,
199                _("Ego `%s' not known to identity service\n"),
200                issuer_ego_name);
201     }
202     GNUNET_SCHEDULER_shutdown ();
203     return;
204   }
205   if (NULL == expiration)
206   {
207     fprintf (stderr,
208              "Please specify a TTL\n");
209     GNUNET_SCHEDULER_shutdown ();
210     return;
211   } else if (GNUNET_OK == GNUNET_STRINGS_fancy_time_to_relative (expiration,
212                                                           &etime_rel))
213   {
214     etime_abs = GNUNET_TIME_relative_to_absolute (etime_rel);
215   } else if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_absolute (expiration,
216                                                                  &etime_abs))
217   {
218     fprintf (stderr,
219              "%s is not a valid ttl!\n",
220              expiration);
221     GNUNET_SCHEDULER_shutdown ();
222     return;
223   }
224
225
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,
230                                  privkey,
231                                  &subject_pkey,
232                                  issuer_attr,
233                                  &etime_abs);
234   res =  GNUNET_GNSRECORD_value_to_string (GNUNET_GNSRECORD_TYPE_CREDENTIAL,
235                                            crd,
236                                            sizeof (struct GNUNET_CREDENTIAL_CredentialRecordData) + strlen (issuer_attr) + 1);
237   printf ("%s\n", res);
238   GNUNET_SCHEDULER_shutdown ();
239 }
240
241
242
243
244 /**
245  * Main function that will be run.
246  *
247  * @param cls closure
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
251  */
252 static void
253 run (void *cls,
254      char *const *args,
255      const char *cfgfile,
256      const struct GNUNET_CONFIGURATION_Handle *c)
257 {
258
259   cfg = c;
260
261
262   tt = GNUNET_SCHEDULER_add_delayed (timeout,
263                                      &do_timeout, NULL);
264   GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
265
266
267
268   if (NULL == subject_key)
269   {
270     fprintf (stderr,
271              _("Subject public key needed\n"));
272     GNUNET_SCHEDULER_shutdown ();
273     return;
274
275   }
276   if (GNUNET_OK !=
277       GNUNET_CRYPTO_ecdsa_public_key_from_string (subject_key,
278                                                   strlen (subject_key),
279                                                   &subject_pkey))
280   {
281     fprintf (stderr,
282              _("Subject public key `%s' is not well-formed\n"),
283              subject_key);
284     GNUNET_SCHEDULER_shutdown ();
285     return;
286   }
287
288   if (GNUNET_YES == verify) {
289     if (NULL == issuer_key)
290     {
291       fprintf (stderr,
292                _("Issuer public key not well-formed\n"));
293       GNUNET_SCHEDULER_shutdown ();
294       return;
295
296     }
297     if (GNUNET_OK !=
298         GNUNET_CRYPTO_ecdsa_public_key_from_string (issuer_key,
299                                                     strlen (issuer_key),
300                                                     &issuer_pkey))
301     {
302       fprintf (stderr,
303                _("Issuer public key `%s' is not well-formed\n"),
304                issuer_key);
305       GNUNET_SCHEDULER_shutdown ();
306     }
307     credential = GNUNET_CREDENTIAL_connect (cfg);
308
309     if (NULL == credential)
310     {
311       fprintf (stderr,
312                _("Failed to connect to CREDENTIAL\n"));
313       GNUNET_SCHEDULER_shutdown ();
314     }
315
316     if (NULL == issuer_attr || NULL == subject_credential)
317     {
318       fprintf (stderr,
319                _("You must provide issuer and subject attributes\n"));
320       GNUNET_SCHEDULER_shutdown ();
321     }
322
323
324     verify_request = GNUNET_CREDENTIAL_verify(credential,
325                                               &issuer_pkey,
326                                               issuer_attr, //TODO argument
327                                               &subject_pkey,
328                                               subject_credential,
329                                               &handle_verify_result,
330                                               NULL);
331   } else if (GNUNET_YES == create_cred) {
332     if (NULL == issuer_ego_name)
333     {
334       fprintf (stderr,
335                _("Issuer ego required\n"));
336       GNUNET_SCHEDULER_shutdown ();
337       return;
338
339     }
340     el = GNUNET_IDENTITY_ego_lookup (cfg,
341                                      issuer_ego_name,
342                                      &identity_cb,
343                                      (void *) cfg);
344     return;
345   } else {
346     fprintf (stderr,
347              _("Please specify name to lookup, subject key and issuer key!\n"));
348     GNUNET_SCHEDULER_shutdown ();
349   }
350   return;
351 }
352
353
354 /**
355  * The main function for gnunet-gns.
356  *
357  * @param argc number of arguments from the command line
358  * @param argv command line arguments
359  * @return 0 ok, 1 on error
360  */
361 int
362 main (int argc, char *const *argv)
363 {
364   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
365     {'I', "issue", NULL,
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},
380     {'e', "ego", "EGO",
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},
386     {'T', "ttl", "EXP",
387       gettext_noop ("The time to live for the credential"), 1,
388       &GNUNET_GETOPT_set_string, &expiration},
389     GNUNET_GETOPT_OPTION_END
390   };
391   int ret;
392
393   timeout = GNUNET_TIME_UNIT_FOREVER_REL;
394   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
395     return 2;
396
397   GNUNET_log_setup ("gnunet-credential", "WARNING", NULL);
398   ret =
399     (GNUNET_OK ==
400      GNUNET_PROGRAM_run (argc, argv, "gnunet-credential",
401                          _("GNUnet credential resolver tool"),
402                          options,
403                          &run, NULL)) ? 0 : 1;
404   GNUNET_free ((void*) argv);
405   return ret;
406 }
407
408 /* end of gnunet-credential.c */