d9e01c1d35a72d738b4562521f7366d19d5225a4
[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   uint32_t c_count = ntohl (vr_msg->c_count);
220   struct GNUNET_CREDENTIAL_Delegation d_chain[d_count];
221   struct GNUNET_CREDENTIAL_Credential creds[c_count];
222   GNUNET_CREDENTIAL_VerifyResultProcessor proc;
223   void *proc_cls;
224
225   LOG (GNUNET_ERROR_TYPE_DEBUG,
226        "Received verify reply from CREDENTIAL service\n");
227   for (vr = handle->verify_head; NULL != vr; vr = vr->next)
228     if (vr->r_id == r_id)
229       break;
230   if (NULL == vr)
231     return;
232   proc = vr->verify_proc;
233   proc_cls = vr->proc_cls;
234   GNUNET_CONTAINER_DLL_remove (handle->verify_head,
235                                handle->verify_tail,
236                                vr);
237   GNUNET_MQ_discard (vr->env);
238   GNUNET_free (vr);
239   GNUNET_assert (GNUNET_OK ==
240                  GNUNET_CREDENTIAL_delegation_chain_deserialize (mlen,
241                                                        (const char*) &vr_msg[1],
242                                                        d_count,
243                                                        d_chain,
244                                                        c_count,
245                                                        creds));
246   if (GNUNET_NO == ntohl (vr_msg->cred_found))
247   {
248     proc (proc_cls,
249           0,
250           NULL,
251           0,
252           NULL); // TODO
253   } else {
254     proc (proc_cls,
255           d_count,
256           d_chain,
257           c_count,
258           creds);
259   }
260 }
261
262
263 /**
264  * Reconnect to CREDENTIAL service.
265  *
266  * @param handle the handle to the CREDENTIAL service
267  */
268 static void
269 reconnect (struct GNUNET_CREDENTIAL_Handle *handle)
270 {
271   struct GNUNET_MQ_MessageHandler handlers[] = {
272     GNUNET_MQ_hd_var_size (result,
273                            GNUNET_MESSAGE_TYPE_CREDENTIAL_VERIFY_RESULT,
274                            struct VerifyResultMessage,
275                            handle),
276     GNUNET_MQ_handler_end ()
277   };
278   struct GNUNET_CREDENTIAL_Request *vr;
279
280   GNUNET_assert (NULL == handle->mq);
281   LOG (GNUNET_ERROR_TYPE_DEBUG,
282        "Trying to connect to CREDENTIAL\n");
283   handle->mq = GNUNET_CLIENT_connecT (handle->cfg,
284                                       "credential",
285                                       handlers,
286                                       &mq_error_handler,
287                                       handle);
288   if (NULL == handle->mq)
289     return;
290   for (vr = handle->verify_head; NULL != vr; vr = vr->next)
291     GNUNET_MQ_send_copy (handle->mq,
292                          vr->env);
293 }
294
295
296 /**
297  * Initialize the connection with the CREDENTIAL service.
298  *
299  * @param cfg configuration to use
300  * @return handle to the CREDENTIAL service, or NULL on error
301  */
302 struct GNUNET_CREDENTIAL_Handle *
303 GNUNET_CREDENTIAL_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
304 {
305   struct GNUNET_CREDENTIAL_Handle *handle;
306
307   handle = GNUNET_new (struct GNUNET_CREDENTIAL_Handle);
308   handle->cfg = cfg;
309   reconnect (handle);
310   if (NULL == handle->mq)
311   {
312     GNUNET_free (handle);
313     return NULL;
314   }
315   return handle;
316 }
317
318
319 /**
320  * Shutdown connection with the CREDENTIAL service.
321  *
322  * @param handle handle of the CREDENTIAL connection to stop
323  */
324 void
325 GNUNET_CREDENTIAL_disconnect (struct GNUNET_CREDENTIAL_Handle *handle)
326 {
327   if (NULL != handle->mq)
328   {
329     GNUNET_MQ_destroy (handle->mq);
330     handle->mq = NULL;
331   }
332   if (NULL != handle->reconnect_task)
333   {
334     GNUNET_SCHEDULER_cancel (handle->reconnect_task);
335     handle->reconnect_task = NULL;
336   }
337   GNUNET_assert (NULL == handle->verify_head);
338   GNUNET_free (handle);
339 }
340
341
342 /**
343  * Cancel pending verify request
344  *
345  * @param lr the verify request to cancel
346  */
347 void
348 GNUNET_CREDENTIAL_verify_cancel (struct GNUNET_CREDENTIAL_Request *vr)
349 {
350   struct GNUNET_CREDENTIAL_Handle *handle = vr->credential_handle;
351
352   GNUNET_CONTAINER_DLL_remove (handle->verify_head,
353                                handle->verify_tail,
354                                vr);
355   GNUNET_MQ_discard (vr->env);
356   GNUNET_free (vr);
357 }
358
359 /**
360  * Performs attribute verification.
361  * Checks if there is a delegation chain from
362  * attribute ``issuer_attribute'' issued by the issuer
363  * with public key ``issuer_key'' maps to the attribute
364  * ``subject_attribute'' claimed by the subject with key
365  * ``subject_key''
366  *
367  * @param handle handle to the Credential service
368  * @param issuer_key the issuer public key
369  * @param issuer_attribute the issuer attribute
370  * @param subject_key the subject public key
371  * @param subject_attribute the attribute claimed by the subject
372  * @param proc function to call on result
373  * @param proc_cls closure for processor
374  * @return handle to the queued request
375  */
376 struct GNUNET_CREDENTIAL_Request*
377 GNUNET_CREDENTIAL_verify (struct GNUNET_CREDENTIAL_Handle *handle,
378                           const struct GNUNET_CRYPTO_EcdsaPublicKey *issuer_key,
379                           const char *issuer_attribute,
380                           const struct GNUNET_CRYPTO_EcdsaPublicKey *subject_key,
381                           const char *subject_attribute,
382                           GNUNET_CREDENTIAL_VerifyResultProcessor proc,
383                           void *proc_cls)
384 {
385   /* IPC to shorten credential names, return shorten_handle */
386   struct VerifyMessage *v_msg;
387   struct GNUNET_CREDENTIAL_Request *vr;
388   size_t nlen;
389
390   if (NULL == issuer_attribute || NULL == subject_attribute)
391   {
392     GNUNET_break (0);
393     return NULL;
394   }
395   //DEBUG LOG
396   LOG (GNUNET_ERROR_TYPE_DEBUG,
397        "Trying to verify `%s' in CREDENTIAL\n",
398        issuer_attribute);
399   nlen = strlen (issuer_attribute) + strlen (subject_attribute) + 1;
400   if (nlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (*vr))
401   {
402     GNUNET_break (0);
403     return NULL;
404   }
405   vr = GNUNET_new (struct GNUNET_CREDENTIAL_Request);
406   vr->credential_handle = handle;
407   vr->verify_proc = proc;
408   vr->proc_cls = proc_cls;
409   vr->r_id = handle->r_id_gen++;
410   vr->env = GNUNET_MQ_msg_extra (v_msg,
411                                  nlen,
412                                  GNUNET_MESSAGE_TYPE_CREDENTIAL_VERIFY);
413   v_msg->id = htonl (vr->r_id);
414   v_msg->subject_key = *subject_key;
415   v_msg->issuer_key =  *issuer_key;
416   v_msg->issuer_attribute_len = htons(strlen(issuer_attribute));
417   v_msg->subject_attribute_len = htons(strlen(subject_attribute));
418   GNUNET_memcpy (&v_msg[1],
419                  issuer_attribute,
420                  strlen (issuer_attribute));
421   GNUNET_memcpy (((char*)&v_msg[1]) + strlen (issuer_attribute),
422                  subject_attribute,
423                  strlen (subject_attribute));
424   GNUNET_CONTAINER_DLL_insert (handle->verify_head,
425                                handle->verify_tail,
426                                vr);
427   if (NULL != handle->mq)
428     GNUNET_MQ_send_copy (handle->mq,
429                          vr->env);
430   return vr;
431 }
432
433 /**
434  * Issue an attribute to a subject
435  *
436  * @param handle handle to the Credential service
437  * @param issuer the ego that should be used to issue the attribute
438  * @param subject the subject of the attribute
439  * @param attribute the name of the attribute
440  * @return handle to the queued request
441  */
442 struct GNUNET_CREDENTIAL_CredentialRecordData *
443 GNUNET_CREDENTIAL_issue (struct GNUNET_CREDENTIAL_Handle *handle,
444                          const struct GNUNET_CRYPTO_EcdsaPrivateKey *issuer,
445                          struct GNUNET_CRYPTO_EcdsaPublicKey *subject,
446                          const char *attribute,
447                          struct GNUNET_TIME_Absolute *expiration)
448 {
449   struct GNUNET_CREDENTIAL_CredentialRecordData *crd;
450
451   crd = GNUNET_malloc (sizeof (struct GNUNET_CREDENTIAL_CredentialRecordData) + strlen (attribute) + 1);
452
453   crd->purpose.size = htonl (strlen (attribute) + 1 +
454                       sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
455                                         sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
456                       sizeof (uint64_t));
457   
458   crd->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_CREDENTIAL);
459   GNUNET_CRYPTO_ecdsa_key_get_public (issuer,
460                                       &crd->issuer_key);
461   crd->subject_key = *subject;
462   crd->expiration = GNUNET_htonll (expiration->abs_value_us);
463   GNUNET_memcpy (&crd[1],
464                  attribute,
465                  strlen (attribute));
466   if (GNUNET_OK !=
467       GNUNET_CRYPTO_ecdsa_sign (issuer,
468                                 &crd->purpose,
469                                 &crd->signature))
470   {
471     GNUNET_break (0);
472     GNUNET_free (crd);
473     return NULL;
474   }
475   return crd;
476 }
477
478
479
480
481 /* end of credential_api.c */