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