2 This file is part of GNUnet.
3 Copyright (C) 2012-2015 GNUnet e.V.
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.
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.
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.
21 * @author Martin Schanzenbach
22 * @file src/identity-provider/gnunet-service-identity-provider.c
23 * @brief Identity Token Service
27 #include "gnunet_util_lib.h"
28 #include "gnunet_constants.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_identity_service.h"
31 #include "gnunet_gnsrecord_lib.h"
32 #include "gnunet_namestore_service.h"
33 #include "gnunet_statistics_service.h"
34 #include "gnunet_gns_service.h"
35 #include "gnunet_signatures.h"
36 #include "identity_provider.h"
37 #include "identity_token.h"
46 * Normal operation state
48 #define STATE_POST_INIT 1
51 * Minimum interval between updates
53 #define MIN_WAIT_TIME GNUNET_TIME_UNIT_MINUTES
56 * Standard token expiration time
58 #define DEFAULT_TOKEN_EXPIRATION_INTERVAL GNUNET_TIME_UNIT_HOURS
61 * Service state (to detect initial update pass)
66 * Head of ego entry DLL
68 static struct EgoEntry *ego_head;
71 * Tail of ego entry DLL
73 static struct EgoEntry *ego_tail;
78 static struct GNUNET_IDENTITY_Handle *identity_handle;
81 * Token expiration interval
83 static struct GNUNET_TIME_Relative token_expiration_interval;
88 static struct GNUNET_NAMESTORE_Handle *ns_handle;
93 static struct GNUNET_GNS_Handle *gns_handle;
98 static struct GNUNET_NAMESTORE_QueueEntry *ns_qe;
103 static struct GNUNET_NAMESTORE_ZoneIterator *ns_it;
108 static struct GNUNET_SCHEDULER_Task * timeout_task;
114 static struct GNUNET_SCHEDULER_Task * update_task;
117 * Timeout for next update pass
119 static struct GNUNET_TIME_Relative min_rel_exp;
123 * Currently processed token
125 static struct IdentityToken *token;
128 * Label for currently processed token
133 * Scopes for processed token
138 * Expiration for processed token
140 static uint64_t rd_exp;
143 * ECDHE Privkey for processed token metadata
145 static struct GNUNET_CRYPTO_EcdhePrivateKey ecdhe_privkey;
148 * Handle to the statistics service.
150 static struct GNUNET_STATISTICS_Handle *stats;
153 * Notification context, simplifies client broadcasts.
155 static struct GNUNET_SERVER_NotificationContext *nc;
160 static const struct GNUNET_CONFIGURATION_Handle *cfg;
163 struct ExchangeHandle
169 struct GNUNET_SERVER_Client *client;
174 struct TokenTicket *ticket;
179 struct IdentityToken *token;
184 struct GNUNET_GNS_LookupRequest *lookup_request;
189 struct GNUNET_CRYPTO_EcdsaPrivateKey aud_privkey;
203 struct GNUNET_SERVER_Client *client;
208 struct GNUNET_CRYPTO_EcdsaPrivateKey iss_key;
213 struct GNUNET_CRYPTO_EcdsaPublicKey iss_pkey;
218 struct GNUNET_CRYPTO_EcdsaPublicKey aud_key;
223 struct GNUNET_TIME_Absolute expiration;
238 struct GNUNET_NAMESTORE_ZoneIterator *ns_it;
243 struct GNUNET_CONTAINER_MultiHashMap *attr_map;
248 struct IdentityToken *token;
253 struct TokenTicket *ticket;
258 struct GNUNET_NAMESTORE_QueueEntry *ns_qe;
261 * The label the token is stored under
267 * DLL for ego handles to egos containing the ID_ATTRS in a map in json_t format
275 struct EgoEntry *next;
280 struct EgoEntry *prev;
285 struct GNUNET_IDENTITY_Ego *ego;
288 * Attribute map. Contains the attributes as json_t
290 struct GNUNET_CONTAINER_MultiHashMap *attr_map;
293 * Attributes are old and should be updated if GNUNET_YES
295 int attributes_dirty;
299 * Continuation for token store call
302 * @param success error code
303 * @param emsg error message
306 store_token_cont (void *cls,
311 if (GNUNET_SYSERR == success)
313 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
314 "Failed to update token: %s\n",
318 GNUNET_NAMESTORE_zone_iterator_next (ns_it);
323 * This function updates the old token with new attributes,
324 * removes deleted attributes and expiration times.
326 * @param cls the ego entry
329 handle_token_update (void *cls)
331 char *token_metadata;
334 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key;
335 struct GNUNET_CRYPTO_EcdsaPublicKey pub_key;
336 struct GNUNET_CRYPTO_EcdhePrivateKey *new_ecdhe_privkey;
337 struct EgoEntry *ego_entry = cls;
338 struct GNUNET_GNSRECORD_Data token_record[2];
339 struct GNUNET_HashCode key_hash;
340 struct GNUNET_TIME_Relative token_rel_exp;
341 struct GNUNET_TIME_Relative token_ttl;
342 struct GNUNET_TIME_Absolute token_exp;
343 struct GNUNET_TIME_Absolute token_nbf;
344 struct GNUNET_TIME_Absolute new_exp;
345 struct GNUNET_TIME_Absolute new_iat;
346 struct GNUNET_TIME_Absolute new_nbf;
347 struct IdentityToken *new_token;
348 struct TokenAttr *cur_value;
349 struct TokenAttr *attr;
350 size_t token_metadata_len;
352 priv_key = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego);
353 GNUNET_IDENTITY_ego_get_public_key (ego_entry->ego,
356 //Note: We need the token expiration time here. Not the record expiration
358 //There are two types of tokens: Token that expire on GNS level with
359 //an absolute expiration time. Those are basically tokens that will
360 //be automatically revoked on (record)expiration.
361 //Tokens stored with relative expiration times will expire on the token level (token expiration)
362 //but this service will reissue new tokens that can be retrieved from GNS
365 for (attr = token->attr_head; NULL != attr; attr = attr->next)
367 if (0 == strcmp (attr->name, "exp"))
369 sscanf (attr->val_head->value,
371 &token_exp.abs_value_us);
372 } else if (0 == strcmp (attr->name, "nbf")) {
373 sscanf (attr->val_head->value,
375 &token_nbf.abs_value_us);
378 token_rel_exp = GNUNET_TIME_absolute_get_difference (token_nbf, token_exp);
380 token_ttl = GNUNET_TIME_absolute_get_remaining (token_exp);
381 if (0 != GNUNET_TIME_absolute_get_remaining (token_exp).rel_value_us)
383 //This token is not yet expired! Save and skip
384 if (min_rel_exp.rel_value_us > token_ttl.rel_value_us)
386 min_rel_exp = token_ttl;
392 GNUNET_free (scopes);
394 GNUNET_NAMESTORE_zone_iterator_next (ns_it);
397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
398 "Token is expired. Create a new one\n");
399 new_token = token_create (&pub_key,
401 new_exp = GNUNET_TIME_relative_to_absolute (token_rel_exp);
402 new_nbf = GNUNET_TIME_absolute_get ();
404 for (attr = token->attr_head; NULL != attr; attr = attr->next)
406 if (0 == strcmp (attr->name, "exp"))
408 token_add_attr_int (new_token, attr->name, new_exp.abs_value_us);
410 else if (0 == strcmp (attr->name, "nbf"))
412 token_add_attr_int (new_token, attr->name, new_nbf.abs_value_us);
414 else if (0 == strcmp (attr->name, "iat"))
416 token_add_attr_int (new_token, attr->name, new_iat.abs_value_us);
418 else if ((0 == strcmp (attr->name, "iss"))
419 || (0 == strcmp (attr->name, "aud")))
423 else if (0 == strcmp (attr->name, "sub"))
425 token_add_attr (new_token,
427 attr->val_head->value);
431 GNUNET_CRYPTO_hash (attr->name,
434 //Check if attr still exists. omit of not
436 GNUNET_CONTAINER_multihashmap_contains (ego_entry->attr_map,
439 cur_value = GNUNET_CONTAINER_multihashmap_get (ego_entry->attr_map,
441 GNUNET_CONTAINER_DLL_insert (new_token->attr_head,
442 new_token->attr_tail,
448 // reassemble and set
449 GNUNET_assert (token_serialize (new_token,
454 token_record[0].data = enc_token_str;
455 token_record[0].data_size = strlen (enc_token_str) + 1;
456 token_record[0].expiration_time = rd_exp; //Old expiration time
457 token_record[0].record_type = GNUNET_GNSRECORD_TYPE_ID_TOKEN;
458 token_record[0].flags = GNUNET_GNSRECORD_RF_NONE;
461 token_metadata_len = sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey)
462 + sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)
463 + strlen (scopes) + 1; //With 0-Terminator
464 token_metadata = GNUNET_malloc (token_metadata_len);
465 write_ptr = token_metadata;
466 memcpy (token_metadata, new_ecdhe_privkey, sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey));
467 write_ptr += sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey);
468 memcpy (write_ptr, &token->aud_key, sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
469 write_ptr += sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
470 memcpy (write_ptr, scopes, strlen (scopes) + 1); //with 0-Terminator;
472 token_record[1].data = token_metadata;
473 token_record[1].data_size = token_metadata_len;
474 token_record[1].expiration_time = rd_exp;
475 token_record[1].record_type = GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA;
476 token_record[1].flags = GNUNET_GNSRECORD_RF_PRIVATE;
478 ns_qe = GNUNET_NAMESTORE_records_store (ns_handle,
485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, ">>> Updating Token w/ %s\n", new_token);
486 token_destroy (new_token);
487 token_destroy (token);
488 GNUNET_free (new_ecdhe_privkey);
489 GNUNET_free (enc_token_str);
493 GNUNET_free (scopes);
498 update_identities(void *cls);
506 * @param value the json_t attribute value
510 clear_ego_attrs (void *cls,
511 const struct GNUNET_HashCode *key,
514 struct TokenAttr *attr = value;
515 struct TokenAttrValue *val;
516 struct TokenAttrValue *tmp_val;
517 for (val = attr->val_head; NULL != val;)
520 GNUNET_CONTAINER_DLL_remove (attr->val_head,
523 GNUNET_free (val->value);
527 GNUNET_free (attr->name);
535 * Update all ID_TOKEN records for an identity and store them
537 * @param cls the identity entry
538 * @param zone the identity
539 * @param lbl the name of the record
540 * @param rd_count number of records
541 * @param rd record data
545 token_collect (void *cls,
546 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
548 unsigned int rd_count,
549 const struct GNUNET_GNSRECORD_Data *rd)
551 struct EgoEntry *ego_entry = cls;
552 const struct GNUNET_GNSRECORD_Data *token_record;
553 const struct GNUNET_GNSRECORD_Data *token_metadata_record;
554 struct GNUNET_CRYPTO_EcdsaPublicKey *aud_key;
559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
560 ">>> Updating Ego finished\n");
561 //Clear attribute map for ego
562 GNUNET_CONTAINER_multihashmap_iterate (ego_entry->attr_map,
565 GNUNET_CONTAINER_multihashmap_clear (ego_entry->attr_map);
566 update_task = GNUNET_SCHEDULER_add_now (&update_identities,
571 //There should be only a single record for a token under a label
574 GNUNET_NAMESTORE_zone_iterator_next (ns_it);
578 if (rd[0].record_type == GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA)
580 token_metadata_record = &rd[0];
581 token_record = &rd[1];
583 token_record = &rd[0];
584 token_metadata_record = &rd[1];
586 if (token_metadata_record->record_type != GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA)
588 GNUNET_NAMESTORE_zone_iterator_next (ns_it);
591 if (token_record->record_type == GNUNET_GNSRECORD_TYPE_ID_TOKEN)
593 GNUNET_NAMESTORE_zone_iterator_next (ns_it);
597 //Get metadata and decrypt token
598 ecdhe_privkey = *((struct GNUNET_CRYPTO_EcdhePrivateKey *)token_metadata_record->data);
599 aud_key = (struct GNUNET_CRYPTO_EcdsaPublicKey *)&ecdhe_privkey+sizeof(struct GNUNET_CRYPTO_EcdhePrivateKey);
600 scopes = GNUNET_strdup ((char*) aud_key+sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
602 token_parse2 (token_record->data,
607 label = GNUNET_strdup (lbl);
608 rd_exp = token_record->expiration_time;
610 GNUNET_SCHEDULER_add_now (&handle_token_update, ego_entry);
616 * Collect all ID_ATTR records for an identity and store them
618 * @param cls the identity entry
619 * @param zone the identity
620 * @param lbl the name of the record
621 * @param rd_count number of records
622 * @param rd record data
626 attribute_collect (void *cls,
627 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
629 unsigned int rd_count,
630 const struct GNUNET_GNSRECORD_Data *rd)
632 struct EgoEntry *ego_entry = cls;
633 struct GNUNET_HashCode key;
634 struct TokenAttr *attr;
635 struct TokenAttrValue *val;
642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
643 ">>> Updating Attributes finished\n");
644 ego_entry->attributes_dirty = GNUNET_NO;
645 update_task = GNUNET_SCHEDULER_add_now (&update_identities, ego_entry);
651 GNUNET_NAMESTORE_zone_iterator_next (ns_it);
654 GNUNET_CRYPTO_hash (lbl,
659 if (rd->record_type == GNUNET_GNSRECORD_TYPE_ID_ATTR)
661 val_str = GNUNET_GNSRECORD_value_to_string (rd->record_type,
664 attr = GNUNET_malloc (sizeof (struct TokenAttr));
665 attr->name = GNUNET_strdup (lbl);
666 val = GNUNET_malloc (sizeof (struct TokenAttrValue));
667 val->value = val_str;
668 GNUNET_CONTAINER_DLL_insert (attr->val_head,
671 GNUNET_CONTAINER_multihashmap_put (ego_entry->attr_map,
674 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
677 GNUNET_NAMESTORE_zone_iterator_next (ns_it);
681 attr = GNUNET_malloc (sizeof (struct TokenAttr));
682 attr->name = GNUNET_strdup (lbl);
683 for (i = 0; i < rd_count; i++)
685 if (rd[i].record_type == GNUNET_GNSRECORD_TYPE_ID_ATTR)
687 val_str = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
690 val = GNUNET_malloc (sizeof (struct TokenAttrValue));
691 val->value = val_str;
692 GNUNET_CONTAINER_DLL_insert (attr->val_head,
697 GNUNET_CONTAINER_multihashmap_put (ego_entry->attr_map,
700 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
701 GNUNET_NAMESTORE_zone_iterator_next (ns_it);
707 * Update identity information for ego. If attribute map is
708 * dirty, first update the attributes.
710 * @param cls the ego to update
713 update_identities(void *cls)
715 struct EgoEntry *next_ego = cls;
716 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key;
718 if (NULL == next_ego)
720 if (min_rel_exp.rel_value_us < MIN_WAIT_TIME.rel_value_us)
721 min_rel_exp = MIN_WAIT_TIME;
722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
723 ">>> Finished. Rescheduling in %d\n",
724 min_rel_exp.rel_value_us);
726 //finished -> reschedule
727 update_task = GNUNET_SCHEDULER_add_delayed (min_rel_exp,
730 min_rel_exp.rel_value_us = 0;
733 priv_key = GNUNET_IDENTITY_ego_get_private_key (next_ego->ego);
734 if (GNUNET_YES == next_ego->attributes_dirty)
736 //Starting over. We must update the Attributes for they might have changed.
737 ns_it = GNUNET_NAMESTORE_zone_iteration_start (ns_handle,
745 //Ego will be dirty next time
746 next_ego->attributes_dirty = GNUNET_YES;
747 ns_it = GNUNET_NAMESTORE_zone_iteration_start (ns_handle,
757 * Function called initially to start update task
762 GNUNET_log (GNUNET_ERROR_TYPE_INFO, ">>> Starting Service\n");
763 //Initially iterate all itenties and refresh all tokens
764 update_task = GNUNET_SCHEDULER_add_now (&update_identities, ego_head);
768 * Initial ego collection function.
773 * @param identifier ego name
777 struct GNUNET_IDENTITY_Ego *ego,
779 const char *identifier)
781 struct EgoEntry *new_entry;
782 if ((NULL == ego) && (STATE_INIT == state))
784 state = STATE_POST_INIT;
788 if (STATE_INIT == state) {
789 new_entry = GNUNET_malloc (sizeof (struct EgoEntry));
790 new_entry->ego = ego;
791 new_entry->attr_map = GNUNET_CONTAINER_multihashmap_create (5,
793 new_entry->attributes_dirty = GNUNET_YES;
794 GNUNET_CONTAINER_DLL_insert_tail(ego_head, ego_tail, new_entry);
804 struct EgoEntry *ego_entry;
805 struct EgoEntry *ego_tmp;
807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
811 GNUNET_SERVER_notification_context_destroy (nc);
816 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
820 if (NULL != timeout_task)
821 GNUNET_SCHEDULER_cancel (timeout_task);
822 if (NULL != update_task)
823 GNUNET_SCHEDULER_cancel (update_task);
824 if (NULL != identity_handle)
825 GNUNET_IDENTITY_disconnect (identity_handle);
826 if (NULL != gns_handle)
827 GNUNET_GNS_disconnect (gns_handle);
829 GNUNET_NAMESTORE_zone_iteration_stop (ns_it);
831 GNUNET_NAMESTORE_cancel (ns_qe);
832 if (NULL != ns_handle)
833 GNUNET_NAMESTORE_disconnect (ns_handle);
839 for (ego_entry = ego_head;
843 if (0 != GNUNET_CONTAINER_multihashmap_size (ego_tmp->attr_map))
845 GNUNET_CONTAINER_multihashmap_iterate (ego_tmp->attr_map,
850 GNUNET_CONTAINER_multihashmap_destroy (ego_tmp->attr_map);
851 ego_entry = ego_entry->next;
852 GNUNET_free (ego_tmp);
860 * @param tc task context
863 do_shutdown (void *cls)
865 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
866 "Shutting down...\n");
871 static struct GNUNET_IDENTITY_PROVIDER_ExchangeResultMessage*
872 create_exchange_result_message (const char* token,
874 uint64_t ticket_nonce)
876 struct GNUNET_IDENTITY_PROVIDER_ExchangeResultMessage *erm;
877 uint16_t token_len = strlen (token) + 1;
878 erm = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_PROVIDER_ExchangeResultMessage)
880 erm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_PROVIDER_EXCHANGE_RESULT);
881 erm->header.size = htons (sizeof (struct GNUNET_IDENTITY_PROVIDER_ExchangeResultMessage)
883 erm->ticket_nonce = htonl (ticket_nonce);
884 memcpy (&erm[1], token, token_len);
889 static struct GNUNET_IDENTITY_PROVIDER_IssueResultMessage*
890 create_issue_result_message (const char* label,
894 struct GNUNET_IDENTITY_PROVIDER_IssueResultMessage *irm;
897 irm = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_PROVIDER_IssueResultMessage)
899 + strlen (ticket) + 1
900 + strlen (token) + 1);
901 irm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_PROVIDER_ISSUE_RESULT);
902 irm->header.size = htons (sizeof (struct GNUNET_IDENTITY_PROVIDER_IssueResultMessage)
904 + strlen (ticket) + 1
905 + strlen (token) + 1);
906 GNUNET_asprintf (&tmp_str, "%s,%s,%s", label, ticket, token);
907 memcpy (&irm[1], tmp_str, strlen (tmp_str) + 1);
908 GNUNET_free (tmp_str);
913 cleanup_issue_handle (struct IssueHandle *handle)
915 if (NULL != handle->attr_map)
916 GNUNET_CONTAINER_multihashmap_destroy (handle->attr_map);
917 if (NULL != handle->scopes)
918 GNUNET_free (handle->scopes);
919 if (NULL != handle->token)
920 token_destroy (handle->token);
921 if (NULL != handle->ticket)
922 ticket_destroy (handle->ticket);
923 if (NULL != handle->label)
924 GNUNET_free (handle->label);
925 GNUNET_free (handle);
929 store_token_issue_cont (void *cls,
933 struct IssueHandle *handle = cls;
934 struct GNUNET_IDENTITY_PROVIDER_IssueResultMessage *irm;
937 handle->ns_qe = NULL;
938 if (GNUNET_SYSERR == success)
940 cleanup_issue_handle (handle);
941 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n",
943 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
946 if (GNUNET_OK != ticket_serialize (handle->ticket,
950 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n",
951 "Error serializing ticket\n");
952 cleanup_issue_handle (handle);
953 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
956 if (GNUNET_OK != token_to_string (handle->token,
960 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n",
961 "Error serializing token\n");
962 GNUNET_free (ticket_str);
963 cleanup_issue_handle (handle);
964 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
967 irm = create_issue_result_message (handle->label, ticket_str, token_str);
968 GNUNET_SERVER_notification_context_unicast (nc,
972 GNUNET_SERVER_client_set_user_context (handle->client, NULL);
973 cleanup_issue_handle (handle);
975 GNUNET_free (ticket_str);
976 GNUNET_free (token_str);
981 * Build a GNUid token for identity
983 * FIXME: doxygen is very wrong here!
985 * @param handle the handle
986 * @param ego_entry the ego to build the token for
987 * @param name name of the ego
988 * @param token_aud token audience
989 * @param token the resulting gnuid token
990 * @return identifier string of token (label)
993 sign_and_return_token (void *cls)
995 struct GNUNET_CRYPTO_EcdsaPublicKey pub_key;
996 struct GNUNET_CRYPTO_EcdhePrivateKey *ecdhe_privkey;
997 struct IssueHandle *handle = cls;
998 struct GNUNET_GNSRECORD_Data token_record[2];
1000 char *enc_token_str;
1001 char *token_metadata;
1005 size_t token_metadata_len;
1009 GNUNET_asprintf (&nonce_str, "%lu", handle->nonce);
1010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Request nonce: %s\n", nonce_str);
1012 GNUNET_CRYPTO_ecdsa_key_get_public (&handle->iss_key,
1014 handle->ticket = ticket_create (handle->nonce,
1019 time = GNUNET_TIME_absolute_get().abs_value_us;
1020 exp_time = time + token_expiration_interval.rel_value_us;
1022 token_add_attr_int (handle->token, "nbf", time);
1023 token_add_attr_int (handle->token, "iat", time);
1024 token_add_attr_int (handle->token, "exp", exp_time);
1025 token_add_attr (handle->token, "nonce", nonce_str);
1027 //Token in a serialized encrypted format
1028 GNUNET_assert (token_serialize (handle->token,
1033 //Token record E,E_K (Token)
1034 token_record[0].data = enc_token_str;
1035 token_record[0].data_size = strlen (enc_token_str) + 1;
1036 token_record[0].expiration_time = exp_time;
1037 token_record[0].record_type = GNUNET_GNSRECORD_TYPE_ID_TOKEN;
1038 token_record[0].flags = GNUNET_GNSRECORD_RF_NONE;
1041 token_metadata_len = sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey)
1042 + sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)
1043 + strlen (handle->scopes) + 1; //With 0-Terminator
1044 token_metadata = GNUNET_malloc (token_metadata_len);
1045 write_ptr = token_metadata;
1046 memcpy (token_metadata, ecdhe_privkey, sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey));
1047 write_ptr += sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey);
1048 memcpy (write_ptr, &handle->aud_key, sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
1049 write_ptr += sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
1050 memcpy (write_ptr, handle->scopes, strlen (handle->scopes) + 1); //with 0-Terminator;
1052 token_record[1].data = token_metadata;
1053 token_record[1].data_size = token_metadata_len;
1054 token_record[1].expiration_time = exp_time;
1055 token_record[1].record_type = GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA;
1056 token_record[1].flags = GNUNET_GNSRECORD_RF_PRIVATE;
1059 handle->ns_qe = GNUNET_NAMESTORE_records_store (ns_handle,
1064 &store_token_issue_cont,
1066 GNUNET_free (ecdhe_privkey);
1067 GNUNET_free (nonce_str);
1068 GNUNET_free (enc_token_str);
1069 GNUNET_free (token_metadata);
1073 * Collect attributes for token
1076 attr_collect (void *cls,
1077 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
1079 unsigned int rd_count,
1080 const struct GNUNET_GNSRECORD_Data *rd)
1084 struct IssueHandle *handle = cls;
1085 struct GNUNET_HashCode key;
1089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding attribute END: \n");
1090 handle->ns_it = NULL;
1091 GNUNET_SCHEDULER_add_now (&sign_and_return_token, handle);
1095 GNUNET_CRYPTO_hash (label,
1099 if (0 == rd_count ||
1100 ( (NULL != handle->attr_map) &&
1101 (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains (handle->attr_map,
1106 GNUNET_NAMESTORE_zone_iterator_next (handle->ns_it);
1110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding attribute: %s\n", label);
1114 if (rd->record_type == GNUNET_GNSRECORD_TYPE_ID_ATTR)
1116 data = GNUNET_GNSRECORD_value_to_string (rd->record_type,
1119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding value: %s\n", data);
1120 token_add_attr (handle->token,
1125 GNUNET_NAMESTORE_zone_iterator_next (handle->ns_it);
1130 for (; i < rd_count; i++)
1132 if (rd->record_type == GNUNET_GNSRECORD_TYPE_ID_ATTR)
1134 data = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
1137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding value: %s\n", data);
1138 token_add_attr (handle->token, label, data);
1143 GNUNET_NAMESTORE_zone_iterator_next (handle->ns_it);
1147 cleanup_exchange_handle (struct ExchangeHandle *handle)
1149 if (NULL != handle->ticket)
1150 ticket_destroy (handle->ticket);
1151 if (NULL != handle->token)
1152 token_destroy (handle->token);
1153 GNUNET_free (handle);
1157 process_lookup_result (void *cls, uint32_t rd_count,
1158 const struct GNUNET_GNSRECORD_Data *rd)
1160 struct ExchangeHandle *handle = cls;
1161 struct GNUNET_IDENTITY_PROVIDER_ExchangeResultMessage *erm;
1165 handle->lookup_request = NULL;
1168 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1169 "Number of tokens %d != 2.",
1171 cleanup_exchange_handle (handle);
1172 GNUNET_SCHEDULER_add_now (&do_shutdown, handle);
1177 GNUNET_GNSRECORD_value_to_string (GNUNET_GNSRECORD_TYPE_ID_TOKEN,
1182 GNUNET_assert (GNUNET_OK == token_parse (record_str,
1183 &handle->aud_privkey,
1187 GNUNET_assert (GNUNET_OK == token_to_string (handle->token,
1188 &handle->aud_privkey,
1191 erm = create_exchange_result_message (token_str,
1193 handle->ticket->payload->nonce);
1194 GNUNET_SERVER_notification_context_unicast (nc,
1198 GNUNET_SERVER_client_set_user_context (handle->client, NULL);
1200 cleanup_exchange_handle (handle);
1201 GNUNET_free (record_str);
1202 GNUNET_free (token_str);
1211 * Handler for exchange message
1214 * @param client who sent the message
1215 * @param message the message
1218 handle_exchange_message (void *cls,
1219 struct GNUNET_SERVER_Client *client,
1220 const struct GNUNET_MessageHeader *message)
1222 const struct GNUNET_IDENTITY_PROVIDER_ExchangeMessage *em;
1223 struct ExchangeHandle *xchange_handle;
1228 size = ntohs (message->size);
1229 if (size <= sizeof (struct GNUNET_IDENTITY_PROVIDER_ExchangeMessage))
1232 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1235 em = (const struct GNUNET_IDENTITY_PROVIDER_ExchangeMessage *) message;
1236 ticket = (const char *) &em[1];
1237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1238 "Received EXCHANGE of `%s' from client\n",
1240 xchange_handle = GNUNET_malloc (sizeof (struct ExchangeHandle));
1241 xchange_handle->aud_privkey = em->aud_privkey;
1243 if (GNUNET_SYSERR == ticket_parse (ticket,
1244 &xchange_handle->aud_privkey,
1245 &xchange_handle->ticket))
1247 GNUNET_free (xchange_handle);
1248 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Looking for token under %s\n",
1252 xchange_handle->ticket->payload->label);
1253 GNUNET_asprintf (&lookup_query,
1255 xchange_handle->ticket->payload->label);
1256 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1257 GNUNET_SERVER_notification_context_add (nc, client);
1258 GNUNET_SERVER_client_set_user_context (client, xchange_handle);
1259 xchange_handle->client = client;
1260 xchange_handle->lookup_request = GNUNET_GNS_lookup (gns_handle,
1262 &xchange_handle->ticket->payload->identity_key,
1263 GNUNET_GNSRECORD_TYPE_ID_TOKEN,
1264 GNUNET_GNS_LO_LOCAL_MASTER,
1266 &process_lookup_result,
1268 GNUNET_free (lookup_query);
1275 * Look for existing token
1277 * @param cls the identity entry
1278 * @param zone the identity
1279 * @param lbl the name of the record
1280 * @param rd_count number of records
1281 * @param rd record data
1285 find_existing_token (void *cls,
1286 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
1288 unsigned int rd_count,
1289 const struct GNUNET_GNSRECORD_Data *rd)
1291 struct IssueHandle *handle = cls;
1292 const struct GNUNET_GNSRECORD_Data *token_metadata_record;
1293 struct GNUNET_CRYPTO_EcdsaPublicKey *aud_key;
1294 struct GNUNET_HashCode key;
1295 int scope_count_token;
1303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1304 ">>> No existing token found\n");
1307 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
1309 GNUNET_STRINGS_base64_encode ((char*)&rnd_key,
1312 handle->ns_it = NULL;
1313 handle->ns_it = GNUNET_NAMESTORE_zone_iteration_start (ns_handle,
1320 //There should be only a single record for a token under a label
1323 GNUNET_NAMESTORE_zone_iterator_next (handle->ns_it);
1327 if (rd[0].record_type == GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA)
1329 token_metadata_record = &rd[0];
1331 token_metadata_record = &rd[1];
1333 if (token_metadata_record->record_type != GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA)
1335 GNUNET_NAMESTORE_zone_iterator_next (handle->ns_it);
1338 ecdhe_privkey = *((struct GNUNET_CRYPTO_EcdhePrivateKey *)token_metadata_record->data);
1340 (struct GNUNET_CRYPTO_EcdsaPublicKey *)(token_metadata_record->data+sizeof(struct GNUNET_CRYPTO_EcdhePrivateKey));
1341 tmp_scopes = GNUNET_strdup ((char*) aud_key+sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
1343 if (0 != memcmp (aud_key, &handle->aud_key,
1344 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
1346 char *tmp2 = GNUNET_STRINGS_data_to_string_alloc (aud_key,
1347 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
1348 //Audience does not match!
1349 char *tmp = GNUNET_GNSRECORD_value_to_string (GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA,
1350 token_metadata_record->data,
1351 token_metadata_record->data_size);
1352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1353 "Token does not match audience %s vs %s. Moving on\n",
1356 GNUNET_free (tmp_scopes);
1357 GNUNET_NAMESTORE_zone_iterator_next (handle->ns_it);
1361 scope = strtok (tmp_scopes, ",");
1362 scope_count_token = 0;
1363 while (NULL != scope)
1365 GNUNET_CRYPTO_hash (scope,
1369 if ((NULL != handle->attr_map) &&
1370 (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains (handle->attr_map, &key)))
1372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1373 "Issued token does not include `%s'. Moving on\n", scope);
1374 GNUNET_free (tmp_scopes);
1375 GNUNET_NAMESTORE_zone_iterator_next (handle->ns_it);
1378 scope_count_token++;
1379 scope = strtok (NULL, ",");
1381 GNUNET_free (tmp_scopes);
1382 //All scopes in token are also in request. Now
1384 if (GNUNET_CONTAINER_multihashmap_size (handle->attr_map) == scope_count_token)
1386 //We have an existing token
1387 handle->label = GNUNET_strdup (lbl);
1388 handle->ns_it = NULL;
1389 handle->ns_it = GNUNET_NAMESTORE_zone_iteration_start (ns_handle,
1396 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1397 "Nuber of attributes in token do not match request\n");
1399 GNUNET_NAMESTORE_zone_iterator_next (handle->ns_it);
1405 * Handler for issue message
1408 * @param client who sent the message
1409 * @param message the message
1412 handle_issue_message (void *cls,
1413 struct GNUNET_SERVER_Client *client,
1414 const struct GNUNET_MessageHeader *message)
1416 const struct GNUNET_IDENTITY_PROVIDER_IssueMessage *im;
1422 struct GNUNET_HashCode key;
1423 struct IssueHandle *issue_handle;
1425 size = ntohs (message->size);
1426 if (size <= sizeof (struct GNUNET_IDENTITY_PROVIDER_IssueMessage))
1429 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1432 im = (const struct GNUNET_IDENTITY_PROVIDER_IssueMessage *) message;
1433 scopes = (const char *) &im[1];
1434 issue_handle = GNUNET_malloc (sizeof (struct IssueHandle));
1435 issue_handle->attr_map = GNUNET_CONTAINER_multihashmap_create (5,
1437 /* FIXME: check that scopes is 0-termianted, Out-of-bounds access
1439 scopes_tmp = GNUNET_strdup (scopes);
1440 scope = strtok(scopes_tmp, ",");
1441 for (; NULL != scope; scope = strtok (NULL, ","))
1443 GNUNET_CRYPTO_hash (scope,
1446 GNUNET_CONTAINER_multihashmap_put (issue_handle->attr_map,
1449 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1451 GNUNET_free (scopes_tmp);
1453 issue_handle->aud_key = im->aud_key;
1454 issue_handle->iss_key = im->iss_key;
1455 GNUNET_CRYPTO_ecdsa_key_get_public (&im->iss_key,
1456 &issue_handle->iss_pkey);
1457 issue_handle->expiration = GNUNET_TIME_absolute_ntoh (im->expiration);
1458 issue_handle->nonce = ntohl (im->nonce);
1459 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1460 GNUNET_SERVER_notification_context_add (nc, client);
1461 GNUNET_SERVER_client_set_user_context (client, issue_handle);
1462 issue_handle->client = client;
1463 issue_handle->scopes = GNUNET_strdup (scopes);
1464 issue_handle->token = token_create (&issue_handle->iss_pkey,
1465 &issue_handle->aud_key);
1467 issue_handle->ns_it = GNUNET_NAMESTORE_zone_iteration_start (ns_handle,
1469 &find_existing_token,
1474 * Main function that will be run
1476 * @param cls closure
1477 * @param args remaining command-line arguments
1478 * @param cfgfile name of the configuration file used (for saving, can be NULL)
1479 * @param c configuration
1483 struct GNUNET_SERVER_Handle *server,
1484 const struct GNUNET_CONFIGURATION_Handle *c)
1486 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1487 {&handle_issue_message, NULL,
1488 GNUNET_MESSAGE_TYPE_IDENTITY_PROVIDER_ISSUE, 0},
1489 {&handle_exchange_message, NULL,
1490 GNUNET_MESSAGE_TYPE_IDENTITY_PROVIDER_EXCHANGE, 0},
1496 stats = GNUNET_STATISTICS_create ("identity-provider", cfg);
1497 GNUNET_SERVER_add_handlers (server, handlers);
1498 nc = GNUNET_SERVER_notification_context_create (server, 1);
1500 //Connect to identity and namestore services
1501 ns_handle = GNUNET_NAMESTORE_connect (cfg);
1502 if (NULL == ns_handle)
1504 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "error connecting to namestore");
1507 gns_handle = GNUNET_GNS_connect (cfg);
1508 if (NULL == gns_handle)
1510 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "error connecting to gns");
1513 identity_handle = GNUNET_IDENTITY_connect (cfg,
1518 GNUNET_CONFIGURATION_get_value_time (cfg,
1519 "identity-provider",
1520 "TOKEN_EXPIRATION_INTERVAL",
1521 &token_expiration_interval))
1523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1524 "Time window for zone iteration: %s\n",
1525 GNUNET_STRINGS_relative_time_to_string (token_expiration_interval,
1528 token_expiration_interval = DEFAULT_TOKEN_EXPIRATION_INTERVAL;
1531 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1532 &do_shutdown, NULL);
1540 * @param argc number of arguments from the cli
1541 * @param argv command line arguments
1542 * @return 0 ok, 1 on error
1546 main (int argc, char *const *argv)
1548 return (GNUNET_OK ==
1549 GNUNET_SERVICE_run (argc, argv, "identity-provider",
1550 GNUNET_SERVICE_OPTION_NONE,
1551 &run, NULL)) ? 0 : 1;
1554 /* end of gnunet-rest-server.c */