Verify that GCD(m,n) != 1 when n is an RSA modulus
[oweals/gnunet.git] / src / identity / identity_api.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2013 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 Liceidentity 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 Liceidentity for more details.
14
15      You should have received a copy of the GNU General Public Liceidentity
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 /**
22  * @file identity/identity_api.c
23  * @brief api to interact with the identity service
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_constants.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_identity_service.h"
31 #include "identity.h"
32
33 #define LOG(kind,...) GNUNET_log_from (kind, "identity-api",__VA_ARGS__)
34
35 /**
36  * Handle for an ego.
37  */
38 struct GNUNET_IDENTITY_Ego
39 {
40   /**
41    * Private key associated with this ego.
42    */
43   struct GNUNET_CRYPTO_EcdsaPrivateKey *pk;
44
45   /**
46    * Current name associated with this ego.
47    */
48   char *name;
49
50   /**
51    * Client context associated with this ego.
52    */
53   void *ctx;
54
55   /**
56    * Hash of the public key of this ego.
57    */
58   struct GNUNET_HashCode id;
59 };
60
61
62 /**
63  * Handle for an operation with the identity service.
64  */
65 struct GNUNET_IDENTITY_Operation
66 {
67
68   /**
69    * Main identity handle.
70    */
71   struct GNUNET_IDENTITY_Handle *h;
72
73   /**
74    * We keep operations in a DLL.
75    */
76   struct GNUNET_IDENTITY_Operation *next;
77
78   /**
79    * We keep operations in a DLL.
80    */
81   struct GNUNET_IDENTITY_Operation *prev;
82
83   /**
84    * Message to send to the identity service.
85    * Allocated at the end of this struct.
86    */
87   const struct GNUNET_MessageHeader *msg;
88
89   /**
90    * Continuation to invoke with the result of the transmission; @e cb
91    * will be NULL in this case.
92    */
93   GNUNET_IDENTITY_Continuation cont;
94
95   /**
96    * Continuation to invoke with the result of the transmission for
97    * 'get' operations (@e cont will be NULL in this case).
98    */
99   GNUNET_IDENTITY_Callback cb;
100
101   /**
102    * Closure for @e cont or @e cb.
103    */
104   void *cls;
105
106 };
107
108
109 /**
110  * Handle for the service.
111  */
112 struct GNUNET_IDENTITY_Handle
113 {
114   /**
115    * Configuration to use.
116    */
117   const struct GNUNET_CONFIGURATION_Handle *cfg;
118
119   /**
120    * Socket (if available).
121    */
122   struct GNUNET_CLIENT_Connection *client;
123
124   /**
125    * Hash map from the hash of the public key to the
126    * respective 'GNUNET_IDENTITY_Ego' handle.
127    */
128   struct GNUNET_CONTAINER_MultiHashMap *egos;
129
130   /**
131    * Function to call when we receive updates.
132    */
133   GNUNET_IDENTITY_Callback cb;
134
135   /**
136    * Closure for 'cb'.
137    */
138   void *cb_cls;
139
140   /**
141    * Head of active operations.
142    */
143   struct GNUNET_IDENTITY_Operation *op_head;
144
145   /**
146    * Tail of active operations.
147    */
148   struct GNUNET_IDENTITY_Operation *op_tail;
149
150   /**
151    * Currently pending transmission request, or NULL for none.
152    */
153   struct GNUNET_CLIENT_TransmitHandle *th;
154
155   /**
156    * Task doing exponential back-off trying to reconnect.
157    */
158   struct GNUNET_SCHEDULER_Task * reconnect_task;
159
160   /**
161    * Time for next connect retry.
162    */
163   struct GNUNET_TIME_Relative reconnect_delay;
164
165   /**
166    * Are we polling for incoming messages right now?
167    */
168   int in_receive;
169
170 };
171
172
173 /**
174  * Obtain the ego representing 'anonymous' users.
175  *
176  * @return handle for the anonymous user, must not be freed
177  */
178 const struct GNUNET_IDENTITY_Ego *
179 GNUNET_IDENTITY_ego_get_anonymous ()
180 {
181   static struct GNUNET_IDENTITY_Ego anon;
182   struct GNUNET_CRYPTO_EcdsaPublicKey pub;
183
184   if (NULL != anon.pk)
185     return &anon;
186   anon.pk = (struct GNUNET_CRYPTO_EcdsaPrivateKey *) GNUNET_CRYPTO_ecdsa_key_get_anonymous ();
187   GNUNET_CRYPTO_ecdsa_key_get_public (anon.pk,
188                                     &pub);
189   GNUNET_CRYPTO_hash (&pub, sizeof (pub), &anon.id);
190   return &anon;
191 }
192
193
194 /**
195  * Try again to connect to the identity service.
196  *
197  * @param cls handle to the identity service.
198  */
199 static void
200 reconnect (void *cls);
201
202
203 /**
204  * Reschedule a connect attempt to the service.
205  *
206  * @param h transport service to reconnect
207  */
208 static void
209 reschedule_connect (struct GNUNET_IDENTITY_Handle *h)
210 {
211   GNUNET_assert (h->reconnect_task == NULL);
212
213   if (NULL != h->th)
214   {
215     GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
216     h->th = NULL;
217   }
218   if (NULL != h->client)
219   {
220     GNUNET_CLIENT_disconnect (h->client);
221     h->client = NULL;
222   }
223   h->in_receive = GNUNET_NO;
224   LOG (GNUNET_ERROR_TYPE_DEBUG,
225        "Scheduling task to reconnect to identity service in %s.\n",
226        GNUNET_STRINGS_relative_time_to_string (h->reconnect_delay, GNUNET_YES));
227   h->reconnect_task =
228       GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h);
229   h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
230 }
231
232
233 /**
234  * Type of a function to call when we receive a message
235  * from the service.
236  *
237  * @param cls closure
238  * @param msg message received, NULL on timeout or fatal error
239  */
240 static void
241 message_handler (void *cls,
242                  const struct GNUNET_MessageHeader *msg)
243 {
244   struct GNUNET_IDENTITY_Handle *h = cls;
245   struct GNUNET_IDENTITY_Operation *op;
246   struct GNUNET_IDENTITY_Ego *ego;
247   const struct GNUNET_IDENTITY_ResultCodeMessage *rcm;
248   const struct GNUNET_IDENTITY_UpdateMessage *um;
249   const struct GNUNET_IDENTITY_SetDefaultMessage *sdm;
250   struct GNUNET_CRYPTO_EcdsaPublicKey pub;
251   struct GNUNET_HashCode id;
252   const char *str;
253   uint16_t size;
254   uint16_t name_len;
255
256   if (NULL == msg)
257   {
258     reschedule_connect (h);
259     return;
260   }
261   LOG (GNUNET_ERROR_TYPE_DEBUG,
262        "Received message of type %d from identity service\n",
263        ntohs (msg->type));
264   size = ntohs (msg->size);
265   switch (ntohs (msg->type))
266   {
267   case GNUNET_MESSAGE_TYPE_IDENTITY_RESULT_CODE:
268     if (size < sizeof (struct GNUNET_IDENTITY_ResultCodeMessage))
269     {
270       GNUNET_break (0);
271       reschedule_connect (h);
272       return;
273     }
274     rcm = (const struct GNUNET_IDENTITY_ResultCodeMessage *) msg;
275     str = (const char *) &rcm[1];
276     if ( (size > sizeof (struct GNUNET_IDENTITY_ResultCodeMessage)) &&
277          ('\0' != str[size - sizeof (struct GNUNET_IDENTITY_ResultCodeMessage) - 1]) )
278     {
279       GNUNET_break (0);
280       reschedule_connect (h);
281       return;
282     }
283     if (size == sizeof (struct GNUNET_IDENTITY_ResultCodeMessage))
284       str = NULL;
285
286     op = h->op_head;
287     GNUNET_CONTAINER_DLL_remove (h->op_head,
288                                  h->op_tail,
289                                  op);
290     GNUNET_CLIENT_receive (h->client, &message_handler, h,
291                            GNUNET_TIME_UNIT_FOREVER_REL);
292     if (NULL != op->cont)
293       op->cont (op->cls,
294                 str);
295     else if (NULL != op->cb)
296       op->cb (op->cls, NULL, NULL, NULL);
297     GNUNET_free (op);
298     break;
299   case GNUNET_MESSAGE_TYPE_IDENTITY_UPDATE:
300     if (size < sizeof (struct GNUNET_IDENTITY_UpdateMessage))
301     {
302       GNUNET_break (0);
303       reschedule_connect (h);
304       return;
305     }
306     um = (const struct GNUNET_IDENTITY_UpdateMessage *) msg;
307     name_len = ntohs (um->name_len);
308
309     str = (const char *) &um[1];
310     if ( (size != name_len + sizeof (struct GNUNET_IDENTITY_UpdateMessage)) ||
311          ( (0 != name_len) &&
312            ('\0' != str[name_len - 1])) )
313     {
314       GNUNET_break (0);
315       reschedule_connect (h);
316       return;
317     }
318     if (GNUNET_YES == ntohs (um->end_of_list))
319     {
320       /* end of initial list of data */
321       GNUNET_CLIENT_receive (h->client, &message_handler, h,
322                              GNUNET_TIME_UNIT_FOREVER_REL);
323       if (NULL != h->cb)
324         h->cb (h->cb_cls, NULL, NULL, NULL);
325       break;
326     }
327     GNUNET_CRYPTO_ecdsa_key_get_public (&um->private_key,
328                                       &pub);
329     GNUNET_CRYPTO_hash (&pub, sizeof (pub), &id);
330     if (0 == name_len)
331       str = NULL;
332     else
333       str = (const char *) &um[1];
334     ego = GNUNET_CONTAINER_multihashmap_get (h->egos,
335                                              &id);
336     if (NULL == ego)
337     {
338       /* ego was created */
339       if (NULL == str)
340       {
341         /* deletion of unknown ego? not allowed */
342         GNUNET_break (0);
343         reschedule_connect (h);
344         return;
345       }
346       ego = GNUNET_new (struct GNUNET_IDENTITY_Ego);
347       ego->pk = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
348       *ego->pk = um->private_key;
349       ego->name = GNUNET_strdup (str);
350       ego->id = id;
351       GNUNET_assert (GNUNET_YES ==
352                      GNUNET_CONTAINER_multihashmap_put (h->egos,
353                                                         &ego->id,
354                                                         ego,
355                                                         GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
356     }
357     if (NULL == str)
358     {
359       /* ego was deleted */
360       GNUNET_assert (GNUNET_YES ==
361                      GNUNET_CONTAINER_multihashmap_remove (h->egos,
362                                                            &ego->id,
363                                                            ego));
364     }
365     else
366     {
367       /* ego changed name */
368       GNUNET_free (ego->name);
369       ego->name = GNUNET_strdup (str);
370     }
371     GNUNET_CLIENT_receive (h->client, &message_handler, h,
372                            GNUNET_TIME_UNIT_FOREVER_REL);
373     /* inform application about change */
374     if (NULL != h->cb)
375       h->cb (h->cb_cls,
376              ego,
377              &ego->ctx,
378              str);
379     if (NULL == str)
380     {
381       GNUNET_free (ego->pk);
382       GNUNET_free (ego->name);
383       GNUNET_free (ego);
384     }
385     break;
386   case GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT:
387     if (size < sizeof (struct GNUNET_IDENTITY_SetDefaultMessage))
388     {
389       GNUNET_break (0);
390       reschedule_connect (h);
391       return;
392     }
393     sdm = (const struct GNUNET_IDENTITY_SetDefaultMessage *) msg;
394     GNUNET_break (0 == ntohs (sdm->reserved));
395     name_len = ntohs (sdm->name_len);
396     str = (const char *) &sdm[1];
397     if ( (size != name_len + sizeof (struct GNUNET_IDENTITY_SetDefaultMessage)) ||
398          ( (0 != name_len) &&
399            ('\0' != str[name_len - 1]) ) )
400     {
401       GNUNET_break (0);
402       reschedule_connect (h);
403       return;
404     }
405     /* Note: we know which service this should be for, so we're not
406        really using 'str' henceforth */
407     GNUNET_CRYPTO_ecdsa_key_get_public (&sdm->private_key,
408                                       &pub);
409     GNUNET_CRYPTO_hash (&pub, sizeof (pub), &id);
410     ego = GNUNET_CONTAINER_multihashmap_get (h->egos,
411                                              &id);
412     if (NULL == ego)
413     {
414       GNUNET_break (0);
415       reschedule_connect (h);
416       return;
417     }
418     op = h->op_head;
419     if (NULL == op)
420     {
421       GNUNET_break (0);
422       reschedule_connect (h);
423       return;
424     }
425     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
426                 "Received SET_DEFAULT message from identity service\n");
427     GNUNET_CONTAINER_DLL_remove (h->op_head,
428                                  h->op_tail,
429                                  op);
430     GNUNET_CLIENT_receive (h->client, &message_handler, h,
431                            GNUNET_TIME_UNIT_FOREVER_REL);
432     if (NULL != op->cb)
433       op->cb (op->cls,
434               ego,
435               &ego->ctx,
436               ego->name);
437     GNUNET_free (op);
438     break;
439   default:
440     GNUNET_break (0);
441     reschedule_connect (h);
442     return;
443   }
444 }
445
446
447 /**
448  * Schedule transmission of the next message from our queue.
449  *
450  * @param h identity handle
451  */
452 static void
453 transmit_next (struct GNUNET_IDENTITY_Handle *h);
454
455
456 /**
457  * Transmit next message to service.
458  *
459  * @param cls the `struct GNUNET_IDENTITY_Handle`.
460  * @param size number of bytes available in @a buf
461  * @param buf where to copy the message
462  * @return number of bytes copied to buf
463  */
464 static size_t
465 send_next_message (void *cls,
466                    size_t size,
467                    void *buf)
468 {
469   struct GNUNET_IDENTITY_Handle *h = cls;
470   struct GNUNET_IDENTITY_Operation *op = h->op_head;
471   size_t ret;
472
473   h->th = NULL;
474   if (NULL == op)
475     return 0;
476   ret = ntohs (op->msg->size);
477   if (ret > size)
478   {
479     reschedule_connect (h);
480     return 0;
481   }
482   LOG (GNUNET_ERROR_TYPE_DEBUG,
483        "Sending message of type %d to identity service\n",
484        ntohs (op->msg->type));
485   memcpy (buf, op->msg, ret);
486   if ( (NULL == op->cont) &&
487        (NULL == op->cb) )
488   {
489     GNUNET_CONTAINER_DLL_remove (h->op_head,
490                                  h->op_tail,
491                                  op);
492     GNUNET_free (op);
493     transmit_next (h);
494   }
495   if (GNUNET_NO == h->in_receive)
496   {
497     h->in_receive = GNUNET_YES;
498     GNUNET_CLIENT_receive (h->client,
499                            &message_handler, h,
500                            GNUNET_TIME_UNIT_FOREVER_REL);
501   }
502   return ret;
503 }
504
505
506 /**
507  * Schedule transmission of the next message from our queue.
508  *
509  * @param h identity handle
510  */
511 static void
512 transmit_next (struct GNUNET_IDENTITY_Handle *h)
513 {
514   struct GNUNET_IDENTITY_Operation *op = h->op_head;
515
516   GNUNET_assert (NULL == h->th);
517   if (NULL == op)
518     return;
519   if (NULL == h->client)
520     return;
521   h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
522                                                ntohs (op->msg->size),
523                                                GNUNET_TIME_UNIT_FOREVER_REL,
524                                                GNUNET_NO,
525                                                &send_next_message,
526                                                h);
527 }
528
529
530 /**
531  * Try again to connect to the identity service.
532  *
533  * @param cls handle to the identity service.
534  */
535 static void
536 reconnect (void *cls)
537 {
538   struct GNUNET_IDENTITY_Handle *h = cls;
539   struct GNUNET_IDENTITY_Operation *op;
540   struct GNUNET_MessageHeader msg;
541
542   h->reconnect_task = NULL;
543   LOG (GNUNET_ERROR_TYPE_DEBUG,
544        "Connecting to identity service.\n");
545   GNUNET_assert (NULL == h->client);
546   h->client = GNUNET_CLIENT_connect ("identity", h->cfg);
547   GNUNET_assert (NULL != h->client);
548   if ( (NULL == h->op_head) ||
549        (GNUNET_MESSAGE_TYPE_IDENTITY_START != ntohs (h->op_head->msg->type)) )
550   {
551     op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
552                         sizeof (struct GNUNET_MessageHeader));
553     op->h = h;
554     op->msg = (const struct GNUNET_MessageHeader *) &op[1];
555     msg.size = htons (sizeof (msg));
556     msg.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_START);
557     memcpy (&op[1], &msg, sizeof (msg));
558     GNUNET_CONTAINER_DLL_insert (h->op_head,
559                                  h->op_tail,
560                                  op);
561   }
562   transmit_next (h);
563   GNUNET_assert (NULL != h->th);
564 }
565
566
567 /**
568  * Connect to the identity service.
569  *
570  * @param cfg the configuration to use
571  * @param cb function to call on all identity events, can be NULL
572  * @param cb_cls closure for @a cb
573  * @return handle to use
574  */
575 struct GNUNET_IDENTITY_Handle *
576 GNUNET_IDENTITY_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
577                          GNUNET_IDENTITY_Callback cb,
578                          void *cb_cls)
579 {
580   struct GNUNET_IDENTITY_Handle *h;
581
582   h = GNUNET_new (struct GNUNET_IDENTITY_Handle);
583   h->cfg = cfg;
584   h->cb = cb;
585   h->cb_cls = cb_cls;
586   h->egos = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_YES);
587   h->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
588   h->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, h);
589   return h;
590 }
591
592
593 /**
594  * Obtain the ECC key associated with a ego.
595  *
596  * @param ego the ego
597  * @return associated ECC key, valid as long as the ego is valid
598  */
599 const struct GNUNET_CRYPTO_EcdsaPrivateKey *
600 GNUNET_IDENTITY_ego_get_private_key (const struct GNUNET_IDENTITY_Ego *ego)
601 {
602   return ego->pk;
603 }
604
605
606 /**
607  * Get the identifier (public key) of an ego.
608  *
609  * @param ego identity handle with the private key
610  * @param pk set to ego's public key
611  */
612 void
613 GNUNET_IDENTITY_ego_get_public_key (const struct GNUNET_IDENTITY_Ego *ego,
614                                     struct GNUNET_CRYPTO_EcdsaPublicKey *pk)
615 {
616   GNUNET_CRYPTO_ecdsa_key_get_public (ego->pk,
617                                     pk);
618 }
619
620
621 /**
622  * Obtain the identity that is currently preferred/default
623  * for a service.
624  *
625  * @param id identity service to query
626  * @param service_name for which service is an identity wanted
627  * @param cb function to call with the result (will only be called once)
628  * @param cb_cls closure for @a cb
629  * @return handle to abort the operation
630  */
631 struct GNUNET_IDENTITY_Operation *
632 GNUNET_IDENTITY_get (struct GNUNET_IDENTITY_Handle *id,
633                      const char *service_name,
634                      GNUNET_IDENTITY_Callback cb,
635                      void *cb_cls)
636 {
637   struct GNUNET_IDENTITY_Operation *op;
638   struct GNUNET_IDENTITY_GetDefaultMessage *gdm;
639   size_t slen;
640
641   slen = strlen (service_name) + 1;
642   if (slen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_GetDefaultMessage))
643   {
644     GNUNET_break (0);
645     return NULL;
646   }
647   op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
648                       sizeof (struct GNUNET_IDENTITY_GetDefaultMessage) +
649                       slen);
650   op->h = id;
651   op->cb = cb;
652   op->cls = cb_cls;
653   gdm = (struct GNUNET_IDENTITY_GetDefaultMessage *) &op[1];
654   gdm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_GET_DEFAULT);
655   gdm->header.size = htons (sizeof (struct GNUNET_IDENTITY_GetDefaultMessage) +
656                             slen);
657   gdm->name_len = htons (slen);
658   gdm->reserved = htons (0);
659   memcpy (&gdm[1], service_name, slen);
660   op->msg = &gdm->header;
661   GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
662                                     id->op_tail,
663                                     op);
664   if (NULL == id->th)
665     transmit_next (id);
666   return op;
667 }
668
669
670 /**
671  * Set the preferred/default identity for a service.
672  *
673  * @param id identity service to inform
674  * @param service_name for which service is an identity set
675  * @param ego new default identity to be set for this service
676  * @param cont function to call once the operation finished
677  * @param cont_cls closure for @a cont
678  * @return handle to abort the operation
679  */
680 struct GNUNET_IDENTITY_Operation *
681 GNUNET_IDENTITY_set (struct GNUNET_IDENTITY_Handle *id,
682                      const char *service_name,
683                      struct GNUNET_IDENTITY_Ego *ego,
684                      GNUNET_IDENTITY_Continuation cont,
685                      void *cont_cls)
686 {
687   struct GNUNET_IDENTITY_Operation *op;
688   struct GNUNET_IDENTITY_SetDefaultMessage *sdm;
689   size_t slen;
690
691   slen = strlen (service_name) + 1;
692   if (slen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_SetDefaultMessage))
693   {
694     GNUNET_break (0);
695     return NULL;
696   }
697   op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
698                       sizeof (struct GNUNET_IDENTITY_SetDefaultMessage) +
699                       slen);
700   op->h = id;
701   op->cont = cont;
702   op->cls = cont_cls;
703   sdm = (struct GNUNET_IDENTITY_SetDefaultMessage *) &op[1];
704   sdm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_SET_DEFAULT);
705   sdm->header.size = htons (sizeof (struct GNUNET_IDENTITY_SetDefaultMessage) +
706                             slen);
707   sdm->name_len = htons (slen);
708   sdm->reserved = htons (0);
709   sdm->private_key = *ego->pk;
710   memcpy (&sdm[1], service_name, slen);
711   op->msg = &sdm->header;
712   GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
713                                     id->op_tail,
714                                     op);
715   if (NULL == id->th)
716     transmit_next (id);
717   return op;
718 }
719
720
721 /**
722  * Create a new identity with the given name.
723  *
724  * @param id identity service to use
725  * @param name desired name
726  * @param cont function to call with the result (will only be called once)
727  * @param cont_cls closure for @a cont
728  * @return handle to abort the operation
729  */
730 struct GNUNET_IDENTITY_Operation *
731 GNUNET_IDENTITY_create (struct GNUNET_IDENTITY_Handle *id,
732                         const char *name,
733                         GNUNET_IDENTITY_Continuation cont,
734                         void *cont_cls)
735 {
736   struct GNUNET_IDENTITY_Operation *op;
737   struct GNUNET_IDENTITY_CreateRequestMessage *crm;
738   struct GNUNET_CRYPTO_EcdsaPrivateKey *pk;
739   size_t slen;
740
741   slen = strlen (name) + 1;
742   pk = GNUNET_CRYPTO_ecdsa_key_create ();
743
744   if (slen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_CreateRequestMessage))
745   {
746     GNUNET_break (0);
747     GNUNET_free (pk);
748     return NULL;
749   }
750   op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
751                       sizeof (struct GNUNET_IDENTITY_CreateRequestMessage) +
752                       slen);
753   op->h = id;
754   op->cont = cont;
755   op->cls = cont_cls;
756   crm = (struct GNUNET_IDENTITY_CreateRequestMessage *) &op[1];
757   crm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_CREATE);
758   crm->header.size = htons (sizeof (struct GNUNET_IDENTITY_CreateRequestMessage) +
759                             slen);
760   crm->name_len = htons (slen);
761   crm->reserved = htons (0);
762   crm->private_key = *pk;
763   memcpy (&crm[1], name, slen);
764   op->msg = &crm->header;
765   GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
766                                     id->op_tail,
767                                     op);
768   if (NULL == id->th)
769     transmit_next (id);
770   GNUNET_free (pk);
771   return op;
772 }
773
774
775 /**
776  * Renames an existing identity.
777  *
778  * @param id identity service to use
779  * @param old_name old name
780  * @param new_name desired new name
781  * @param cb function to call with the result (will only be called once)
782  * @param cb_cls closure for @a cb
783  * @return handle to abort the operation
784  */
785 struct GNUNET_IDENTITY_Operation *
786 GNUNET_IDENTITY_rename (struct GNUNET_IDENTITY_Handle *id,
787                         const char *old_name,
788                         const char *new_name,
789                         GNUNET_IDENTITY_Continuation cb,
790                         void *cb_cls)
791 {
792   struct GNUNET_IDENTITY_Operation *op;
793   struct GNUNET_IDENTITY_RenameMessage *grm;
794   size_t slen_old;
795   size_t slen_new;
796   char *dst;
797
798   slen_old = strlen (old_name) + 1;
799   slen_new = strlen (new_name) + 1;
800   if ( (slen_old >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
801        (slen_new >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
802        (slen_old + slen_new >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_RenameMessage)) )
803   {
804     GNUNET_break (0);
805     return NULL;
806   }
807   op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
808                       sizeof (struct GNUNET_IDENTITY_RenameMessage) +
809                       slen_old + slen_new);
810   op->h = id;
811   op->cont = cb;
812   op->cls = cb_cls;
813   grm = (struct GNUNET_IDENTITY_RenameMessage *) &op[1];
814   grm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_RENAME);
815   grm->header.size = htons (sizeof (struct GNUNET_IDENTITY_RenameMessage) +
816                             slen_old + slen_new);
817   grm->old_name_len = htons (slen_old);
818   grm->new_name_len = htons (slen_new);
819   dst = (char *) &grm[1];
820   memcpy (dst, old_name, slen_old);
821   memcpy (&dst[slen_old], new_name, slen_new);
822   op->msg = &grm->header;
823   GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
824                                     id->op_tail,
825                                     op);
826   if (NULL == id->th)
827     transmit_next (id);
828   return op;
829 }
830
831
832 /**
833  * Delete an existing identity.
834  *
835  * @param id identity service to use
836  * @param name name of the identity to delete
837  * @param cb function to call with the result (will only be called once)
838  * @param cb_cls closure for @a cb
839  * @return handle to abort the operation
840  */
841 struct GNUNET_IDENTITY_Operation *
842 GNUNET_IDENTITY_delete (struct GNUNET_IDENTITY_Handle *id,
843                         const char *name,
844                         GNUNET_IDENTITY_Continuation cb,
845                         void *cb_cls)
846 {
847   struct GNUNET_IDENTITY_Operation *op;
848   struct GNUNET_IDENTITY_DeleteMessage *gdm;
849   size_t slen;
850
851   slen = strlen (name) + 1;
852   if (slen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_IDENTITY_DeleteMessage))
853   {
854     GNUNET_break (0);
855     return NULL;
856   }
857   op = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Operation) +
858                       sizeof (struct GNUNET_IDENTITY_DeleteMessage) +
859                       slen);
860   op->h = id;
861   op->cont = cb;
862   op->cls = cb_cls;
863   gdm = (struct GNUNET_IDENTITY_DeleteMessage *) &op[1];
864   gdm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_DELETE);
865   gdm->header.size = htons (sizeof (struct GNUNET_IDENTITY_DeleteMessage) +
866                             slen);
867   gdm->name_len = htons (slen);
868   gdm->reserved = htons (0);
869   memcpy (&gdm[1], name, slen);
870   op->msg = &gdm->header;
871   GNUNET_CONTAINER_DLL_insert_tail (id->op_head,
872                                     id->op_tail,
873                                     op);
874   if (NULL == id->th)
875     transmit_next (id);
876   return op;
877 }
878
879
880 /**
881  * Cancel an identity operation. Note that the operation MAY still
882  * be executed; this merely cancels the continuation; if the request
883  * was already transmitted, the service may still choose to complete
884  * the operation.
885  *
886  * @param op operation to cancel
887  */
888 void
889 GNUNET_IDENTITY_cancel (struct GNUNET_IDENTITY_Operation *op)
890 {
891   struct GNUNET_IDENTITY_Handle *h = op->h;
892
893   if ( (h->op_head != op) ||
894        (NULL == h->client) )
895   {
896     /* request not active, can simply remove */
897     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
898                 "Client aborted non-head operation, simply removing it\n");
899     GNUNET_CONTAINER_DLL_remove (h->op_head,
900                                  h->op_tail,
901                                  op);
902     GNUNET_free (op);
903     return;
904   }
905   if (NULL != h->th)
906   {
907     /* request active but not yet with service, can still abort */
908     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
909                 "Client aborted head operation prior to transmission, aborting it\n");
910     GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
911     h->th = NULL;
912     GNUNET_CONTAINER_DLL_remove (h->op_head,
913                                  h->op_tail,
914                                  op);
915     GNUNET_free (op);
916     transmit_next (h);
917     return;
918   }
919   /* request active with service, simply ensure continuations are not called */
920   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
921               "Client aborted active request, NULLing continuation\n");
922   op->cont = NULL;
923   op->cb = NULL;
924 }
925
926
927 /**
928  * Free ego from hash map.
929  *
930  * @param cls identity service handle
931  * @param key unused
932  * @param value ego to free
933  * @return #GNUNET_OK (continue to iterate)
934  */
935 static int
936 free_ego (void *cls,
937           const struct GNUNET_HashCode *key,
938           void *value)
939 {
940   struct GNUNET_IDENTITY_Handle *h = cls;
941   struct GNUNET_IDENTITY_Ego *ego = value;
942
943   if (NULL != h->cb)
944     h->cb (h->cb_cls,
945            ego,
946            &ego->ctx,
947            NULL);
948   GNUNET_free (ego->pk);
949   GNUNET_free (ego->name);
950   GNUNET_free (ego);
951   return GNUNET_OK;
952 }
953
954
955 /**
956  * Disconnect from identity service
957  *
958  * @param h handle to destroy
959  */
960 void
961 GNUNET_IDENTITY_disconnect (struct GNUNET_IDENTITY_Handle *h)
962 {
963   struct GNUNET_IDENTITY_Operation *op;
964
965   GNUNET_assert (NULL != h);
966   if (h->reconnect_task != NULL)
967   {
968     GNUNET_SCHEDULER_cancel (h->reconnect_task);
969     h->reconnect_task = NULL;
970   }
971   if (NULL != h->th)
972   {
973     GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
974     h->th = NULL;
975   }
976   if (NULL != h->egos)
977   {
978     GNUNET_CONTAINER_multihashmap_iterate (h->egos,
979                                            &free_ego,
980                                            h);
981     GNUNET_CONTAINER_multihashmap_destroy (h->egos);
982     h->egos = NULL;
983   }
984   while (NULL != (op = h->op_head))
985   {
986     GNUNET_break (NULL == op->cont);
987     GNUNET_CONTAINER_DLL_remove (h->op_head,
988                                  h->op_tail,
989                                  op);
990     GNUNET_free (op);
991   }
992   if (NULL != h->client)
993   {
994     GNUNET_CLIENT_disconnect (h->client);
995     h->client = NULL;
996   }
997   GNUNET_free (h);
998 }
999
1000 /* end of identity_api.c */