4f17919b9597d4406b8535de615bbf044ea942fa
[oweals/gnunet.git] / src / identity-provider / gnunet-idp.c
1 /*
2    This file is part of GNUnet.
3    Copyright (C) 2012-2015 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  * @author Martin Schanzenbach
22  * @file src/identity-provider/gnunet-idp.c
23  * @brief Identity Provider utility
24  *
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_namestore_service.h"
30 #include "gnunet_identity_provider_service.h"
31 #include "gnunet_identity_service.h"
32 #include "gnunet_signatures.h"
33
34 /**
35  * return value
36  */
37 static int ret;
38
39 /**
40  * List attribute flag
41  */
42 static int list;
43
44 /**
45  * Relying party
46  */
47 static char* rp;
48
49 /**
50  * The attribute
51  */
52 static char* attr_name;
53
54 /**
55  * Attribute value
56  */
57 static char* attr_value;
58
59 /**
60  * Attributes to issue
61  */
62 static char* issue_attrs;
63
64 /**
65  * Ticket to consume
66  */
67 static char* consume_ticket;
68
69 /**
70  * Attribute type
71  */
72 static char* type_str;
73
74 /**
75  * Ticket to revoke
76  */
77 static char* revoke_ticket;
78
79 /**
80  * Ego name
81  */
82 static char* ego_name;
83
84 /**
85  * Identity handle
86  */
87 static struct GNUNET_IDENTITY_Handle *identity_handle;
88
89 /**
90  * IdP handle
91  */
92 static struct GNUNET_IDENTITY_PROVIDER_Handle *idp_handle;
93
94 /**
95  * IdP operation
96  */
97 static struct GNUNET_IDENTITY_PROVIDER_Operation *idp_op;
98
99 /**
100  * Attribute iterator
101  */
102 static struct GNUNET_IDENTITY_PROVIDER_AttributeIterator *attr_iterator;
103
104 /**
105  * Master ABE key
106  */
107 static struct GNUNET_CRYPTO_AbeMasterKey *abe_key;
108
109 /**
110  * ego private key
111  */
112 static const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey;
113
114 /**
115  * rp public key
116  */
117 static struct GNUNET_CRYPTO_EcdsaPublicKey rp_key;
118
119 /**
120  * Ticket to consume
121  */
122 static struct GNUNET_IDENTITY_PROVIDER_Ticket ticket;
123
124 /**
125  * Attribute list
126  */
127 static struct GNUNET_IDENTITY_ATTRIBUTE_ClaimList *attr_list;
128
129 /**
130  * Attribute expiration interval
131  */
132 static struct GNUNET_TIME_Relative exp_interval;
133
134 /**
135  * Timeout task
136  */
137 static struct GNUNET_SCHEDULER_Task *timeout;
138
139 static void
140 do_cleanup(void *cls)
141 {
142   if (NULL != timeout)
143     GNUNET_SCHEDULER_cancel (timeout);
144   if (NULL != idp_op)
145     GNUNET_IDENTITY_PROVIDER_cancel (idp_op);
146   if (NULL != attr_iterator)
147     GNUNET_IDENTITY_PROVIDER_get_attributes_stop (attr_iterator);
148   if (NULL != idp_handle)
149     GNUNET_IDENTITY_PROVIDER_disconnect (idp_handle);
150   if (NULL != identity_handle)
151     GNUNET_IDENTITY_disconnect (identity_handle);
152   if (NULL != abe_key)
153     GNUNET_free (abe_key);
154   if (NULL != attr_list)
155     GNUNET_free (attr_list);
156 }
157
158 static void
159 ticket_issue_cb (void* cls,
160                  const struct GNUNET_IDENTITY_PROVIDER_Ticket *ticket)
161 {
162   char* ticket_str;
163   idp_op = NULL;
164   if (NULL != ticket) {
165     ticket_str = GNUNET_STRINGS_data_to_string_alloc (ticket,
166                                                       sizeof (struct GNUNET_IDENTITY_PROVIDER_Ticket));
167     printf("%s\n",
168            ticket_str);
169     GNUNET_free (ticket_str);
170   }
171   GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
172 }
173
174 static void
175 store_attr_cont (void *cls,
176                  int32_t success,
177                  const char*emsg)
178 {
179   idp_op = NULL;
180   if (GNUNET_SYSERR == success) {
181     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
182                 "%s\n", emsg);
183   }
184   GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
185 }
186
187 static void
188 process_attrs (void *cls,
189          const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
190          const struct GNUNET_IDENTITY_ATTRIBUTE_Claim *attr)
191 {
192   char *value_str;
193   if (NULL == identity)
194   {
195     idp_op = NULL;
196     GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
197     return;
198   }
199   if (NULL == attr)
200   {
201     ret = 1;
202     return;
203   }
204   value_str = GNUNET_IDENTITY_ATTRIBUTE_value_to_string (attr->type,
205                                                      attr->data,
206                                                      attr->data_size);
207   GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
208               "%s: %s\n", attr->name, value_str);
209 }
210
211
212 static void
213 iter_error (void *cls)
214 {
215   attr_iterator = NULL;
216   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
217               "Failed to iterate over attributes\n");
218   GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
219 }
220
221 static void
222 timeout_task (void *cls)
223 {
224   timeout = NULL;
225   ret = 1;
226   GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
227               "Timeout\n");
228   GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
229 }
230
231 static void
232 process_rvk (void *cls, int success, const char* msg)
233 {
234   idp_op = NULL;
235   if (GNUNET_OK != success)
236   {
237     GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
238                 "Revocation failed.\n");
239     ret = 1;
240   }
241   GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
242 }
243
244 static void
245 iter_finished (void *cls)
246 {
247   struct GNUNET_IDENTITY_ATTRIBUTE_Claim *claim;
248   char *data;
249   size_t data_size;
250   int type;
251
252   attr_iterator = NULL;
253   if (list)
254   {
255     GNUNET_SCHEDULER_add_now (&do_cleanup, NULL);
256     return;
257   }
258
259   if (issue_attrs)
260   {
261     idp_op = GNUNET_IDENTITY_PROVIDER_ticket_issue (idp_handle,
262                                                     pkey,
263                                                     &rp_key,
264                                                     attr_list,
265                                                     &ticket_issue_cb,
266                                                     NULL);
267     return;
268   }
269   if (consume_ticket)
270   {
271     idp_op = GNUNET_IDENTITY_PROVIDER_ticket_consume (idp_handle,
272                                                       pkey,
273                                                       &ticket,
274                                                       &process_attrs,
275                                                       NULL);
276     timeout = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 10),
277                                             &timeout_task,
278                                             NULL);
279     return;
280   }
281   if (revoke_ticket)
282   {
283     idp_op = GNUNET_IDENTITY_PROVIDER_ticket_revoke (idp_handle,
284                                                      pkey,
285                                                      &ticket,
286                                                      &process_rvk,
287                                                      NULL);
288     return;
289   }
290   if (NULL == type_str)
291     type = GNUNET_IDENTITY_ATTRIBUTE_TYPE_STRING;
292   else
293     type = GNUNET_IDENTITY_ATTRIBUTE_typename_to_number (type_str);
294
295   GNUNET_assert (GNUNET_SYSERR != GNUNET_IDENTITY_ATTRIBUTE_string_to_value (type,
296                                              attr_value,
297                                              (void**)&data,
298                                              &data_size));
299   claim = GNUNET_IDENTITY_ATTRIBUTE_claim_new (attr_name,
300                                                type,
301                                                data,
302                                                data_size);
303   idp_op = GNUNET_IDENTITY_PROVIDER_attribute_store (idp_handle,
304                                                      pkey,
305                                                      claim,
306                                                      &exp_interval,
307                                                      &store_attr_cont,
308                                                      NULL);
309
310
311 }
312
313 static void
314 iter_cb (void *cls,
315          const struct GNUNET_CRYPTO_EcdsaPublicKey *identity,
316          const struct GNUNET_IDENTITY_ATTRIBUTE_Claim *attr)
317 {
318   struct GNUNET_IDENTITY_ATTRIBUTE_ClaimListEntry *le;
319   char *attrs_tmp;
320   char *attr_str;
321
322   if (issue_attrs)
323   {
324     attrs_tmp = GNUNET_strdup (issue_attrs);
325     attr_str = strtok (attrs_tmp, ",");
326     while (NULL != attr_str) {
327       if (0 != strcmp (attr_str, attr->name)) {
328         attr_str = strtok (NULL, ",");
329         continue;
330       }
331       le = GNUNET_new (struct GNUNET_IDENTITY_ATTRIBUTE_ClaimListEntry);
332       le->claim = GNUNET_IDENTITY_ATTRIBUTE_claim_new (attr->name,
333                                                        attr->type,
334                                                        attr->data,
335                                                        attr->data_size);
336       GNUNET_CONTAINER_DLL_insert (attr_list->list_head,
337                                    attr_list->list_tail,
338                                    le);
339       break;
340     }
341     GNUNET_free (attrs_tmp);
342   } else if (list) {
343     GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
344                 "%s: %s\n", attr->name, (char*)attr->data);
345   }
346   GNUNET_IDENTITY_PROVIDER_get_attributes_next (attr_iterator);
347 }
348
349 static void
350 ego_cb (void *cls,
351         struct GNUNET_IDENTITY_Ego *ego,
352         void **ctx,
353         const char *name)
354 {
355   if (NULL == name)
356     return;
357   if (0 != strcmp (name, ego_name))
358     return;
359   pkey = GNUNET_IDENTITY_ego_get_private_key (ego);
360
361   if (NULL != rp)
362     GNUNET_CRYPTO_ecdsa_public_key_from_string (rp,
363                                                 strlen (rp),
364                                                 &rp_key);
365   if (NULL != consume_ticket)
366     GNUNET_STRINGS_string_to_data (consume_ticket,
367                                    strlen (consume_ticket),
368                                    &ticket,
369                                    sizeof (struct GNUNET_IDENTITY_PROVIDER_Ticket));
370   if (NULL != revoke_ticket)
371     GNUNET_STRINGS_string_to_data (revoke_ticket,
372                                    strlen (revoke_ticket),
373                                    &ticket,
374                                    sizeof (struct GNUNET_IDENTITY_PROVIDER_Ticket));
375
376
377   attr_list = GNUNET_new (struct GNUNET_IDENTITY_ATTRIBUTE_ClaimList);
378
379   attr_iterator = GNUNET_IDENTITY_PROVIDER_get_attributes_start (idp_handle,
380                                                                  pkey,
381                                                                  &iter_error,
382                                                                  NULL,
383                                                                  &iter_cb,
384                                                                  NULL,
385                                                                  &iter_finished,
386                                                                  NULL);
387
388
389 }
390
391 static void
392 run (void *cls,
393      char *const *args,
394      const char *cfgfile,
395      const struct GNUNET_CONFIGURATION_Handle *c)
396 {
397   ret = 0;
398   if (NULL == ego_name)
399   {
400     GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
401                 _("Ego is required\n"));
402     return;
403   } 
404
405   idp_handle = GNUNET_IDENTITY_PROVIDER_connect (c);
406   //Get Ego
407   identity_handle = GNUNET_IDENTITY_connect (c,
408                                              &ego_cb,
409                                              NULL);
410
411
412 }
413
414
415 int
416 main(int argc, char *const argv[])
417 {
418   exp_interval = GNUNET_TIME_UNIT_HOURS;
419   struct GNUNET_GETOPT_CommandLineOption options[] = {
420
421     GNUNET_GETOPT_option_string ('a',
422                                  "add",
423                                  NULL,
424                                  gettext_noop ("Add attribute"),
425                                  &attr_name),
426
427     GNUNET_GETOPT_option_string ('V',
428                                  "value",
429                                  NULL,
430                                  gettext_noop ("Attribute value"),
431                                  &attr_value),
432     GNUNET_GETOPT_option_string ('e',
433                                  "ego",
434                                  NULL,
435                                  gettext_noop ("Ego"),
436                                  &ego_name),
437     GNUNET_GETOPT_option_string ('r',
438                                  "rp",
439                                  NULL,
440                                  gettext_noop ("Audience (relying party)"),
441                                  &rp),
442     GNUNET_GETOPT_option_flag ('D',
443                                "dump",
444                                gettext_noop ("List attributes for Ego"),
445                                &list),
446     GNUNET_GETOPT_option_string ('i',
447                                  "issue",
448                                  NULL,
449                                  gettext_noop ("Issue a ticket"),
450                                  &issue_attrs),
451     GNUNET_GETOPT_option_string ('C',
452                                  "consume",
453                                  NULL,
454                                  gettext_noop ("Consume a ticket"),
455                                  &consume_ticket),
456     GNUNET_GETOPT_option_string ('R',
457                                  "revoke",
458                                  NULL,
459                                  gettext_noop ("Revoke a ticket"),
460                                  &revoke_ticket),
461     GNUNET_GETOPT_option_string ('t',
462                                  "type",
463                                  NULL,
464                                  gettext_noop ("Type of attribute"),
465                                  &type_str),
466     GNUNET_GETOPT_option_relative_time ('E',
467                                         "expiration",
468                                         NULL,
469                                         gettext_noop ("Expiration interval of the attribute"),
470                                         &exp_interval),
471
472     GNUNET_GETOPT_OPTION_END
473   };
474   if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "ct",
475                       "ct", options,
476                       &run, NULL))
477     return 1;
478   else
479     return ret;
480 }