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