54a02484df59a3c37918bde2ce31a75bc39d83cc
[oweals/gnunet.git] / src / credential / credential_api.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009-2013, 2016 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 credential/credential_api.c
22  * @brief library to access the CREDENTIAL service
23  * @author Adnan Husain
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_constants.h"
28 #include "gnunet_arm_service.h"
29 #include "gnunet_hello_lib.h"
30 #include "gnunet_protocols.h"
31 #include "gnunet_signatures.h"
32 #include "credential.h"
33 #include "gnunet_credential_service.h"
34 #include "gnunet_identity_service.h"
35
36
37 #define LOG(kind,...) GNUNET_log_from (kind, "credential-api",__VA_ARGS__)
38
39 /**
40  * Handle to a verify request
41  */
42 struct GNUNET_CREDENTIAL_Request
43 {
44
45   /**
46    * DLL
47    */
48   struct GNUNET_CREDENTIAL_Request *next;
49
50   /**
51    * DLL
52    */
53   struct GNUNET_CREDENTIAL_Request *prev;
54
55   /**
56    * handle to credential service
57    */
58   struct GNUNET_CREDENTIAL_Handle *credential_handle;
59
60   /**
61    * processor to call on verify result
62    */
63   GNUNET_CREDENTIAL_VerifyResultProcessor verify_proc;
64
65   /**
66    * @e verify_proc closure
67    */
68   void *proc_cls;
69
70   /**
71    * Envelope with the message for this queue entry.
72    */
73   struct GNUNET_MQ_Envelope *env;
74
75   /**
76    * request id
77    */
78   uint32_t r_id;
79
80 };
81
82
83 /**
84  * Connection to the CREDENTIAL service.
85  */
86 struct GNUNET_CREDENTIAL_Handle
87 {
88
89   /**
90    * Configuration to use.
91    */
92   const struct GNUNET_CONFIGURATION_Handle *cfg;
93
94   /**
95    * Connection to service (if available).
96    */
97   struct GNUNET_MQ_Handle *mq;
98
99   /**
100    * Head of linked list of active verify requests.
101    */
102   struct GNUNET_CREDENTIAL_Request *verify_head;
103
104   /**
105    * Tail of linked list of active verify requests.
106    */
107   struct GNUNET_CREDENTIAL_Request *verify_tail;
108
109   /**
110    * Reconnect task
111    */
112   struct GNUNET_SCHEDULER_Task *reconnect_task;
113
114   /**
115    * How long do we wait until we try to reconnect?
116    */
117   struct GNUNET_TIME_Relative reconnect_backoff;
118
119   /**
120    * Request Id generator.  Incremented by one for each request.
121    */
122   uint32_t r_id_gen;
123
124 };
125
126
127 /**
128  * Reconnect to CREDENTIAL service.
129  *
130  * @param handle the handle to the CREDENTIAL service
131  */
132 static void
133 reconnect (struct GNUNET_CREDENTIAL_Handle *handle);
134
135
136 /**
137  * Reconnect to CREDENTIAL
138  *
139  * @param cls the handle
140  */
141 static void
142 reconnect_task (void *cls)
143 {
144   struct GNUNET_CREDENTIAL_Handle *handle = cls;
145
146   handle->reconnect_task = NULL;
147   reconnect (handle);
148 }
149
150
151 /**
152  * Disconnect from service and then reconnect.
153  *
154  * @param handle our handle
155  */
156 static void
157 force_reconnect (struct GNUNET_CREDENTIAL_Handle *handle)
158 {
159   GNUNET_MQ_destroy (handle->mq);
160   handle->mq = NULL;
161   handle->reconnect_backoff
162     = GNUNET_TIME_STD_BACKOFF (handle->reconnect_backoff);
163   handle->reconnect_task
164     = GNUNET_SCHEDULER_add_delayed (handle->reconnect_backoff,
165                         &reconnect_task,
166                         handle);
167 }
168
169
170 /**
171  * Generic error handler, called with the appropriate error code and
172  * the same closure specified at the creation of the message queue.
173  * Not every message queue implementation supports an error handler.
174  *
175  * @param cls closure with the `struct GNUNET_CREDENTIAL_Handle *`
176  * @param error error code
177  */
178 static void
179 mq_error_handler (void *cls,
180                   enum GNUNET_MQ_Error error)
181 {
182   struct GNUNET_CREDENTIAL_Handle *handle = cls;
183
184   force_reconnect (handle);
185 }
186
187
188 /**
189  * Check validity of message received from the CREDENTIAL service
190  *
191  * @param cls the `struct GNUNET_CREDENTIAL_Handle *`
192  * @param loookup_msg the incoming message
193  */
194 static int
195 check_result (void *cls,
196               const struct VerifyResultMessage *vr_msg)
197 {
198   //TODO
199   return GNUNET_OK;
200 }
201
202
203 /**
204  * Handler for messages received from the CREDENTIAL service
205  *
206  * @param cls the `struct GNUNET_CREDENTIAL_Handle *`
207  * @param loookup_msg the incoming message
208  */
209 static void
210 handle_result (void *cls,
211                const struct VerifyResultMessage *vr_msg)
212 {
213   struct GNUNET_CREDENTIAL_Handle *handle = cls;
214   uint32_t r_id = ntohl (vr_msg->id);
215   struct GNUNET_CREDENTIAL_Request *vr;
216   GNUNET_CREDENTIAL_VerifyResultProcessor proc;
217   void *proc_cls;
218
219   LOG (GNUNET_ERROR_TYPE_DEBUG,
220        "Received verify reply from CREDENTIAL service\n");
221   for (vr = handle->verify_head; NULL != vr; vr = vr->next)
222     if (vr->r_id == r_id)
223       break;
224   if (NULL == vr)
225     return;
226   proc = vr->verify_proc;
227   proc_cls = vr->proc_cls;
228   GNUNET_CONTAINER_DLL_remove (handle->verify_head,
229                                handle->verify_tail,
230                                vr);
231   GNUNET_free (vr);
232   /**
233   GNUNET_assert (GNUNET_OK ==
234                  GNUNET_CREDENTIAL_records_deserialize (mlen,
235                                                        (const char*) &lookup_msg[1],
236                                                        rd_count,
237                                                          rd));
238                                                          */
239   if (GNUNET_NO == ntohl (vr_msg->cred_found))
240   {
241     proc (proc_cls,
242           NULL,
243           0,
244           NULL); // TODO
245   } else {
246     proc (proc_cls,
247           (struct GNUNET_CREDENTIAL_CredentialRecordData*) &vr_msg[1],
248           0,
249           NULL);
250   }
251 }
252
253
254 /**
255  * Reconnect to CREDENTIAL service.
256  *
257  * @param handle the handle to the CREDENTIAL service
258  */
259 static void
260 reconnect (struct GNUNET_CREDENTIAL_Handle *handle)
261 {
262   struct GNUNET_MQ_MessageHandler handlers[] = {
263     GNUNET_MQ_hd_var_size (result,
264                            GNUNET_MESSAGE_TYPE_CREDENTIAL_VERIFY_RESULT,
265                            struct VerifyResultMessage,
266                            handle),
267     GNUNET_MQ_handler_end ()
268   };
269   struct GNUNET_CREDENTIAL_Request *vr;
270
271   GNUNET_assert (NULL == handle->mq);
272   LOG (GNUNET_ERROR_TYPE_DEBUG,
273        "Trying to connect to CREDENTIAL\n");
274   handle->mq = GNUNET_CLIENT_connecT (handle->cfg,
275                                       "credential",
276                                       handlers,
277                                       &mq_error_handler,
278                                       handle);
279   if (NULL == handle->mq)
280     return;
281   for (vr = handle->verify_head; NULL != vr; vr = vr->next)
282     GNUNET_MQ_send_copy (handle->mq,
283                          vr->env);
284 }
285
286
287 /**
288  * Initialize the connection with the CREDENTIAL service.
289  *
290  * @param cfg configuration to use
291  * @return handle to the CREDENTIAL service, or NULL on error
292  */
293 struct GNUNET_CREDENTIAL_Handle *
294 GNUNET_CREDENTIAL_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
295 {
296   struct GNUNET_CREDENTIAL_Handle *handle;
297
298   handle = GNUNET_new (struct GNUNET_CREDENTIAL_Handle);
299   handle->cfg = cfg;
300   reconnect (handle);
301   if (NULL == handle->mq)
302   {
303     GNUNET_free (handle);
304     return NULL;
305   }
306   return handle;
307 }
308
309
310 /**
311  * Shutdown connection with the CREDENTIAL service.
312  *
313  * @param handle handle of the CREDENTIAL connection to stop
314  */
315 void
316 GNUNET_CREDENTIAL_disconnect (struct GNUNET_CREDENTIAL_Handle *handle)
317 {
318   if (NULL != handle->mq)
319   {
320     GNUNET_MQ_destroy (handle->mq);
321     handle->mq = NULL;
322   }
323   if (NULL != handle->reconnect_task)
324   {
325     GNUNET_SCHEDULER_cancel (handle->reconnect_task);
326     handle->reconnect_task = NULL;
327   }
328   GNUNET_assert (NULL == handle->verify_head);
329   GNUNET_free (handle);
330 }
331
332
333 /**
334  * Cancel pending verify request
335  *
336  * @param lr the verify request to cancel
337  */
338 void
339 GNUNET_CREDENTIAL_verify_cancel (struct GNUNET_CREDENTIAL_Request *vr)
340 {
341   struct GNUNET_CREDENTIAL_Handle *handle = vr->credential_handle;
342
343   GNUNET_CONTAINER_DLL_remove (handle->verify_head,
344                                handle->verify_tail,
345                                vr);
346   GNUNET_MQ_discard (vr->env);
347   GNUNET_free (vr);
348 }
349
350 /**
351  * Performs attribute verification.
352  * Checks if there is a delegation chain from
353  * attribute ``issuer_attribute'' issued by the issuer
354  * with public key ``issuer_key'' maps to the attribute
355  * ``subject_attribute'' claimed by the subject with key
356  * ``subject_key''
357  *
358  * @param handle handle to the Credential service
359  * @param issuer_key the issuer public key
360  * @param issuer_attribute the issuer attribute
361  * @param subject_key the subject public key
362  * @param subject_attribute the attribute claimed by the subject
363  * @param proc function to call on result
364  * @param proc_cls closure for processor
365  * @return handle to the queued request
366  */
367 struct GNUNET_CREDENTIAL_Request*
368 GNUNET_CREDENTIAL_verify (struct GNUNET_CREDENTIAL_Handle *handle,
369                           const struct GNUNET_CRYPTO_EcdsaPublicKey *issuer_key,
370                           const char *issuer_attribute,
371                           const struct GNUNET_CRYPTO_EcdsaPublicKey *subject_key,
372                           const char *subject_attribute,
373                           GNUNET_CREDENTIAL_VerifyResultProcessor proc,
374                           void *proc_cls)
375 {
376   /* IPC to shorten credential names, return shorten_handle */
377   struct VerifyMessage *v_msg;
378   struct GNUNET_CREDENTIAL_Request *vr;
379   size_t nlen;
380
381   if (NULL == issuer_attribute || NULL == subject_attribute)
382   {
383     GNUNET_break (0);
384     return NULL;
385   }
386   //DEBUG LOG
387   LOG (GNUNET_ERROR_TYPE_DEBUG,
388        "Trying to verify `%s' in CREDENTIAL\n",
389        issuer_attribute);
390   nlen = strlen (issuer_attribute) + strlen (subject_attribute) + 1;
391   if (nlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (*vr))
392   {
393     GNUNET_break (0);
394     return NULL;
395   }
396   vr = GNUNET_new (struct GNUNET_CREDENTIAL_Request);
397   vr->credential_handle = handle;
398   vr->verify_proc = proc;
399   vr->proc_cls = proc_cls;
400   vr->r_id = handle->r_id_gen++;
401   vr->env = GNUNET_MQ_msg_extra (v_msg,
402                                  nlen,
403                                  GNUNET_MESSAGE_TYPE_CREDENTIAL_VERIFY);
404   v_msg->id = htonl (vr->r_id);
405   v_msg->subject_key = *subject_key;
406   v_msg->issuer_key =  *issuer_key;
407   v_msg->issuer_attribute_len = htons(strlen(issuer_attribute));
408   v_msg->subject_attribute_len = htons(strlen(subject_attribute));
409   GNUNET_memcpy (&v_msg[1],
410                  issuer_attribute,
411                  strlen (issuer_attribute));
412   GNUNET_memcpy (((char*)&v_msg[1]) + strlen (issuer_attribute),
413                  subject_attribute,
414                  strlen (subject_attribute));
415   GNUNET_CONTAINER_DLL_insert (handle->verify_head,
416                                handle->verify_tail,
417                                vr);
418   if (NULL != handle->mq)
419     GNUNET_MQ_send_copy (handle->mq,
420                          vr->env);
421   return vr;
422 }
423
424 /**
425  * Issue an attribute to a subject
426  *
427  * @param handle handle to the Credential service
428  * @param issuer the ego that should be used to issue the attribute
429  * @param subject the subject of the attribute
430  * @param attribute the name of the attribute
431  * @return handle to the queued request
432  */
433 struct GNUNET_CREDENTIAL_CredentialRecordData *
434 GNUNET_CREDENTIAL_issue (struct GNUNET_CREDENTIAL_Handle *handle,
435                          const struct GNUNET_CRYPTO_EcdsaPrivateKey *issuer,
436                          struct GNUNET_CRYPTO_EcdsaPublicKey *subject,
437                          const char *attribute,
438                          struct GNUNET_TIME_Absolute *expiration)
439 {
440   struct GNUNET_CREDENTIAL_CredentialRecordData *crd;
441
442   crd = GNUNET_malloc (sizeof (struct GNUNET_CREDENTIAL_CredentialRecordData) + strlen (attribute) + 1);
443
444   crd->purpose.size = htonl (strlen (attribute) + 1 +
445                       sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
446                                         sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
447                       sizeof (uint64_t));
448   
449   crd->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_CREDENTIAL);
450   GNUNET_CRYPTO_ecdsa_key_get_public (issuer,
451                                       &crd->issuer_key);
452   crd->subject_key = *subject;
453   crd->expiration = GNUNET_htonll (expiration->abs_value_us);
454   GNUNET_memcpy (&crd[1],
455                  attribute,
456                  strlen (attribute));
457   if (GNUNET_OK !=
458       GNUNET_CRYPTO_ecdsa_sign (issuer,
459                                 &crd->purpose,
460                                 &crd->signature))
461   {
462     GNUNET_break (0);
463     GNUNET_free (crd);
464     return NULL;
465   }
466   return crd;
467 }
468
469
470
471
472 /* end of credential_api.c */