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;
113 static struct GNUNET_SCHEDULER_Task *update_task;
116 * Timeout for next update pass
118 static struct GNUNET_TIME_Relative min_rel_exp;
122 * Currently processed token
124 static struct IdentityToken *token;
127 * Label for currently processed token
132 * Scopes for processed token
137 * Expiration for processed token
139 static uint64_t rd_exp;
142 * ECDHE Privkey for processed token metadata
144 static struct GNUNET_CRYPTO_EcdhePrivateKey ecdhe_privkey;
147 * Handle to the statistics service.
149 static struct GNUNET_STATISTICS_Handle *stats;
152 * Notification context, simplifies client broadcasts.
154 static struct GNUNET_SERVER_NotificationContext *nc;
159 static const struct GNUNET_CONFIGURATION_Handle *cfg;
162 struct ExchangeHandle
168 struct GNUNET_SERVER_Client *client;
173 struct TokenTicket *ticket;
178 struct IdentityToken *token;
183 struct GNUNET_GNS_LookupRequest *lookup_request;
188 struct GNUNET_CRYPTO_EcdsaPrivateKey aud_privkey;
202 struct GNUNET_SERVER_Client *client;
207 struct GNUNET_CRYPTO_EcdsaPrivateKey iss_key;
212 struct GNUNET_CRYPTO_EcdsaPublicKey iss_pkey;
217 struct GNUNET_CRYPTO_EcdsaPublicKey aud_key;
222 struct GNUNET_TIME_Absolute expiration;
237 struct GNUNET_NAMESTORE_ZoneIterator *ns_it;
242 struct GNUNET_CONTAINER_MultiHashMap *attr_map;
247 struct IdentityToken *token;
252 struct TokenTicket *ticket;
257 struct GNUNET_NAMESTORE_QueueEntry *ns_qe;
260 * The label the token is stored under
266 * DLL for ego handles to egos containing the ID_ATTRS in a map in json_t format
274 struct EgoEntry *next;
279 struct EgoEntry *prev;
284 struct GNUNET_IDENTITY_Ego *ego;
287 * Attribute map. Contains the attributes as json_t
289 struct GNUNET_CONTAINER_MultiHashMap *attr_map;
292 * Attributes are old and should be updated if GNUNET_YES
294 int attributes_dirty;
298 * Continuation for token store call
301 * @param success error code
302 * @param emsg error message
305 store_token_cont (void *cls,
310 if (GNUNET_SYSERR == success)
312 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
313 "Failed to update token: %s\n",
317 GNUNET_NAMESTORE_zone_iterator_next (ns_it);
322 * This function updates the old token with new attributes,
323 * removes deleted attributes and expiration times.
325 * @param cls the ego entry
328 handle_token_update (void *cls)
330 char *token_metadata;
333 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key;
334 struct GNUNET_CRYPTO_EcdsaPublicKey pub_key;
335 struct GNUNET_CRYPTO_EcdhePrivateKey *new_ecdhe_privkey;
336 struct EgoEntry *ego_entry = cls;
337 struct GNUNET_GNSRECORD_Data token_record[2];
338 struct GNUNET_HashCode key_hash;
339 struct GNUNET_TIME_Relative token_rel_exp;
340 struct GNUNET_TIME_Relative token_ttl;
341 struct GNUNET_TIME_Absolute token_exp;
342 struct GNUNET_TIME_Absolute token_nbf;
343 struct GNUNET_TIME_Absolute new_exp;
344 struct GNUNET_TIME_Absolute new_iat;
345 struct GNUNET_TIME_Absolute new_nbf;
346 struct IdentityToken *new_token;
347 struct TokenAttr *cur_value;
348 struct TokenAttr *attr;
349 size_t token_metadata_len;
351 priv_key = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego);
352 GNUNET_IDENTITY_ego_get_public_key (ego_entry->ego,
355 //Note: We need the token expiration time here. Not the record expiration
357 //There are two types of tokens: Token that expire on GNS level with
358 //an absolute expiration time. Those are basically tokens that will
359 //be automatically revoked on (record)expiration.
360 //Tokens stored with relative expiration times will expire on the token level (token expiration)
361 //but this service will reissue new tokens that can be retrieved from GNS
364 for (attr = token->attr_head; NULL != attr; attr = attr->next)
366 if (0 == strcmp (attr->name, "exp"))
368 sscanf (attr->val_head->value,
370 &token_exp.abs_value_us);
371 } else if (0 == strcmp (attr->name, "nbf")) {
372 sscanf (attr->val_head->value,
374 &token_nbf.abs_value_us);
377 token_rel_exp = GNUNET_TIME_absolute_get_difference (token_nbf, token_exp);
379 token_ttl = GNUNET_TIME_absolute_get_remaining (token_exp);
380 if (0 != GNUNET_TIME_absolute_get_remaining (token_exp).rel_value_us)
382 //This token is not yet expired! Save and skip
383 if (min_rel_exp.rel_value_us > token_ttl.rel_value_us)
385 min_rel_exp = token_ttl;
391 GNUNET_free (scopes);
393 GNUNET_NAMESTORE_zone_iterator_next (ns_it);
396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
397 "Token is expired. Create a new one\n");
398 new_token = token_create (&pub_key,
400 new_exp = GNUNET_TIME_relative_to_absolute (token_rel_exp);
401 new_nbf = GNUNET_TIME_absolute_get ();
403 for (attr = token->attr_head; NULL != attr; attr = attr->next)
405 if (0 == strcmp (attr->name, "exp"))
407 token_add_attr_int (new_token, attr->name, new_exp.abs_value_us);
409 else if (0 == strcmp (attr->name, "nbf"))
411 token_add_attr_int (new_token, attr->name, new_nbf.abs_value_us);
413 else if (0 == strcmp (attr->name, "iat"))
415 token_add_attr_int (new_token, attr->name, new_iat.abs_value_us);
417 else if ((0 == strcmp (attr->name, "iss"))
418 || (0 == strcmp (attr->name, "aud")))
422 else if (0 == strcmp (attr->name, "sub"))
424 token_add_attr (new_token,
426 attr->val_head->value);
430 GNUNET_CRYPTO_hash (attr->name,
433 //Check if attr still exists. omit of not
435 GNUNET_CONTAINER_multihashmap_contains (ego_entry->attr_map,
438 cur_value = GNUNET_CONTAINER_multihashmap_get (ego_entry->attr_map,
440 GNUNET_CONTAINER_DLL_insert (new_token->attr_head,
441 new_token->attr_tail,
447 // reassemble and set
448 GNUNET_assert (token_serialize (new_token,
453 token_record[0].data = enc_token_str;
454 token_record[0].data_size = strlen (enc_token_str) + 1;
455 token_record[0].expiration_time = rd_exp; //Old expiration time
456 token_record[0].record_type = GNUNET_GNSRECORD_TYPE_ID_TOKEN;
457 token_record[0].flags = GNUNET_GNSRECORD_RF_NONE;
460 token_metadata_len = sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey)
461 + sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)
462 + strlen (scopes) + 1; //With 0-Terminator
463 token_metadata = GNUNET_malloc (token_metadata_len);
464 write_ptr = token_metadata;
465 memcpy (token_metadata, new_ecdhe_privkey, sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey));
466 write_ptr += sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey);
467 memcpy (write_ptr, &token->aud_key, sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
468 write_ptr += sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
469 memcpy (write_ptr, scopes, strlen (scopes) + 1); //with 0-Terminator;
471 token_record[1].data = token_metadata;
472 token_record[1].data_size = token_metadata_len;
473 token_record[1].expiration_time = rd_exp;
474 token_record[1].record_type = GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA;
475 token_record[1].flags = GNUNET_GNSRECORD_RF_PRIVATE;
477 ns_qe = GNUNET_NAMESTORE_records_store (ns_handle,
484 token_destroy (new_token);
485 token_destroy (token);
486 GNUNET_free (new_ecdhe_privkey);
487 GNUNET_free (enc_token_str);
491 GNUNET_free (scopes);
497 update_identities(void *cls);
506 * @param value the json_t attribute value
507 * @return #GNUNET_YES
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);
536 * Update all ID_TOKEN records for an identity and store them
538 * @param cls the identity entry
539 * @param zone the identity
540 * @param lbl the name of the record
541 * @param rd_count number of records
542 * @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];
585 token_record = &rd[0];
586 token_metadata_record = &rd[1];
588 if (token_metadata_record->record_type != GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA)
590 GNUNET_NAMESTORE_zone_iterator_next (ns_it);
593 if (token_record->record_type == GNUNET_GNSRECORD_TYPE_ID_TOKEN)
595 GNUNET_NAMESTORE_zone_iterator_next (ns_it);
599 //Get metadata and decrypt token
600 ecdhe_privkey = *((struct GNUNET_CRYPTO_EcdhePrivateKey *)token_metadata_record->data);
601 aud_key = (struct GNUNET_CRYPTO_EcdsaPublicKey *)&ecdhe_privkey+sizeof(struct GNUNET_CRYPTO_EcdhePrivateKey);
602 scopes = GNUNET_strdup ((char*) aud_key+sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
604 token_parse2 (token_record->data,
609 label = GNUNET_strdup (lbl);
610 rd_exp = token_record->expiration_time;
612 GNUNET_SCHEDULER_add_now (&handle_token_update,
619 * Collect all ID_ATTR records for an identity and store them
621 * @param cls the identity entry
622 * @param zone the identity
623 * @param lbl the name of the record
624 * @param rd_count number of records
625 * @param rd record data
629 attribute_collect (void *cls,
630 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
632 unsigned int rd_count,
633 const struct GNUNET_GNSRECORD_Data *rd)
635 struct EgoEntry *ego_entry = cls;
636 struct GNUNET_HashCode key;
637 struct TokenAttr *attr;
638 struct TokenAttrValue *val;
645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
646 ">>> Updating Attributes finished\n");
647 ego_entry->attributes_dirty = GNUNET_NO;
648 update_task = GNUNET_SCHEDULER_add_now (&update_identities,
655 GNUNET_NAMESTORE_zone_iterator_next (ns_it);
658 GNUNET_CRYPTO_hash (lbl,
663 if (rd->record_type == GNUNET_GNSRECORD_TYPE_ID_ATTR)
665 val_str = GNUNET_GNSRECORD_value_to_string (rd->record_type,
668 attr = GNUNET_malloc (sizeof (struct TokenAttr));
669 attr->name = GNUNET_strdup (lbl);
670 val = GNUNET_malloc (sizeof (struct TokenAttrValue));
671 val->value = val_str;
672 GNUNET_CONTAINER_DLL_insert (attr->val_head,
675 GNUNET_CONTAINER_multihashmap_put (ego_entry->attr_map,
678 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
681 GNUNET_NAMESTORE_zone_iterator_next (ns_it);
685 attr = GNUNET_malloc (sizeof (struct TokenAttr));
686 attr->name = GNUNET_strdup (lbl);
687 for (i = 0; i < rd_count; i++)
689 if (rd[i].record_type == GNUNET_GNSRECORD_TYPE_ID_ATTR)
691 val_str = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
694 val = GNUNET_malloc (sizeof (struct TokenAttrValue));
695 val->value = val_str;
696 GNUNET_CONTAINER_DLL_insert (attr->val_head,
701 GNUNET_CONTAINER_multihashmap_put (ego_entry->attr_map,
704 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
705 GNUNET_NAMESTORE_zone_iterator_next (ns_it);
711 * Update identity information for ego. If attribute map is
712 * dirty, first update the attributes.
714 * @param cls the ego to update
717 update_identities(void *cls)
719 struct EgoEntry *next_ego = cls;
720 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key;
722 if (NULL == next_ego)
724 if (min_rel_exp.rel_value_us < MIN_WAIT_TIME.rel_value_us)
725 min_rel_exp = MIN_WAIT_TIME;
726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
727 ">>> Finished. Rescheduling in %lu\n",
728 min_rel_exp.rel_value_us);
730 //finished -> reschedule
731 update_task = GNUNET_SCHEDULER_add_delayed (min_rel_exp,
734 min_rel_exp.rel_value_us = 0;
737 priv_key = GNUNET_IDENTITY_ego_get_private_key (next_ego->ego);
738 if (GNUNET_YES == next_ego->attributes_dirty)
740 //Starting over. We must update the Attributes for they might have changed.
741 ns_it = GNUNET_NAMESTORE_zone_iteration_start (ns_handle,
749 //Ego will be dirty next time
750 next_ego->attributes_dirty = GNUNET_YES;
751 ns_it = GNUNET_NAMESTORE_zone_iteration_start (ns_handle,
760 * Function called initially to start update task
765 GNUNET_log (GNUNET_ERROR_TYPE_INFO, ">>> Starting Service\n");
766 //Initially iterate all itenties and refresh all tokens
767 update_task = GNUNET_SCHEDULER_add_now (&update_identities,
773 * Initial ego collection function.
778 * @param identifier ego name
782 struct GNUNET_IDENTITY_Ego *ego,
784 const char *identifier)
786 struct EgoEntry *new_entry;
787 if ((NULL == ego) && (STATE_INIT == state))
789 state = STATE_POST_INIT;
793 if (STATE_INIT == state) {
794 new_entry = GNUNET_malloc (sizeof (struct EgoEntry));
795 new_entry->ego = ego;
796 new_entry->attr_map = GNUNET_CONTAINER_multihashmap_create (5,
798 new_entry->attributes_dirty = GNUNET_YES;
799 GNUNET_CONTAINER_DLL_insert_tail(ego_head, ego_tail, new_entry);
809 struct EgoEntry *ego_entry;
810 struct EgoEntry *ego_tmp;
812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
816 GNUNET_SERVER_notification_context_destroy (nc);
821 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
825 if (NULL != timeout_task)
826 GNUNET_SCHEDULER_cancel (timeout_task);
827 if (NULL != update_task)
828 GNUNET_SCHEDULER_cancel (update_task);
829 if (NULL != identity_handle)
830 GNUNET_IDENTITY_disconnect (identity_handle);
831 if (NULL != gns_handle)
832 GNUNET_GNS_disconnect (gns_handle);
834 GNUNET_NAMESTORE_zone_iteration_stop (ns_it);
836 GNUNET_NAMESTORE_cancel (ns_qe);
837 if (NULL != ns_handle)
838 GNUNET_NAMESTORE_disconnect (ns_handle);
844 for (ego_entry = ego_head;
848 if (0 != GNUNET_CONTAINER_multihashmap_size (ego_tmp->attr_map))
850 GNUNET_CONTAINER_multihashmap_iterate (ego_tmp->attr_map,
855 GNUNET_CONTAINER_multihashmap_destroy (ego_tmp->attr_map);
856 ego_entry = ego_entry->next;
857 GNUNET_free (ego_tmp);
865 * @param tc task context
868 do_shutdown (void *cls)
870 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
871 "Shutting down...\n");
876 static struct GNUNET_IDENTITY_PROVIDER_ExchangeResultMessage*
877 create_exchange_result_message (const char* token,
879 uint64_t ticket_nonce)
881 struct GNUNET_IDENTITY_PROVIDER_ExchangeResultMessage *erm;
882 uint16_t token_len = strlen (token) + 1;
883 erm = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_PROVIDER_ExchangeResultMessage)
885 erm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_PROVIDER_EXCHANGE_RESULT);
886 erm->header.size = htons (sizeof (struct GNUNET_IDENTITY_PROVIDER_ExchangeResultMessage)
888 erm->ticket_nonce = htonl (ticket_nonce);
889 memcpy (&erm[1], token, token_len);
894 static struct GNUNET_IDENTITY_PROVIDER_IssueResultMessage*
895 create_issue_result_message (const char* label,
899 struct GNUNET_IDENTITY_PROVIDER_IssueResultMessage *irm;
902 irm = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_PROVIDER_IssueResultMessage)
904 + strlen (ticket) + 1
905 + strlen (token) + 1);
906 irm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_PROVIDER_ISSUE_RESULT);
907 irm->header.size = htons (sizeof (struct GNUNET_IDENTITY_PROVIDER_IssueResultMessage)
909 + strlen (ticket) + 1
910 + strlen (token) + 1);
911 GNUNET_asprintf (&tmp_str, "%s,%s,%s", label, ticket, token);
912 memcpy (&irm[1], tmp_str, strlen (tmp_str) + 1);
913 GNUNET_free (tmp_str);
918 cleanup_issue_handle (struct IssueHandle *handle)
920 if (NULL != handle->attr_map)
921 GNUNET_CONTAINER_multihashmap_destroy (handle->attr_map);
922 if (NULL != handle->scopes)
923 GNUNET_free (handle->scopes);
924 if (NULL != handle->token)
925 token_destroy (handle->token);
926 if (NULL != handle->ticket)
927 ticket_destroy (handle->ticket);
928 if (NULL != handle->label)
929 GNUNET_free (handle->label);
930 GNUNET_free (handle);
934 store_token_issue_cont (void *cls,
938 struct IssueHandle *handle = cls;
939 struct GNUNET_IDENTITY_PROVIDER_IssueResultMessage *irm;
943 handle->ns_qe = NULL;
944 if (GNUNET_SYSERR == success)
946 cleanup_issue_handle (handle);
947 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n",
949 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
952 if (GNUNET_OK != ticket_serialize (handle->ticket,
956 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n",
957 "Error serializing ticket\n");
958 cleanup_issue_handle (handle);
959 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
962 if (GNUNET_OK != token_to_string (handle->token,
966 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n",
967 "Error serializing token\n");
968 GNUNET_free (ticket_str);
969 cleanup_issue_handle (handle);
970 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
973 irm = create_issue_result_message (handle->label,
976 GNUNET_SERVER_notification_context_unicast (nc,
980 GNUNET_SERVER_client_set_user_context (handle->client, NULL);
981 cleanup_issue_handle (handle);
983 GNUNET_free (ticket_str);
984 GNUNET_free (token_str);
989 * Build a GNUid token for identity
991 * FIXME: doxygen is very wrong here!
993 * @param handle the handle
994 * @param ego_entry the ego to build the token for
995 * @param name name of the ego
996 * @param token_aud token audience
997 * @param token the resulting gnuid token
998 * @return identifier string of token (label)
1001 sign_and_return_token (void *cls)
1003 struct GNUNET_CRYPTO_EcdsaPublicKey pub_key;
1004 struct GNUNET_CRYPTO_EcdhePrivateKey *ecdhe_privkey;
1005 struct IssueHandle *handle = cls;
1006 struct GNUNET_GNSRECORD_Data token_record[2];
1008 char *enc_token_str;
1009 char *token_metadata;
1013 size_t token_metadata_len;
1017 GNUNET_asprintf (&nonce_str, "%lu", handle->nonce);
1018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Request nonce: %s\n", nonce_str);
1020 GNUNET_CRYPTO_ecdsa_key_get_public (&handle->iss_key,
1022 handle->ticket = ticket_create (handle->nonce,
1027 time = GNUNET_TIME_absolute_get().abs_value_us;
1028 exp_time = time + token_expiration_interval.rel_value_us;
1030 token_add_attr_int (handle->token, "nbf", time);
1031 token_add_attr_int (handle->token, "iat", time);
1032 token_add_attr_int (handle->token, "exp", exp_time);
1033 token_add_attr (handle->token, "nonce", nonce_str);
1035 //Token in a serialized encrypted format
1036 GNUNET_assert (token_serialize (handle->token,
1041 //Token record E,E_K (Token)
1042 token_record[0].data = enc_token_str;
1043 token_record[0].data_size = strlen (enc_token_str) + 1;
1044 token_record[0].expiration_time = exp_time;
1045 token_record[0].record_type = GNUNET_GNSRECORD_TYPE_ID_TOKEN;
1046 token_record[0].flags = GNUNET_GNSRECORD_RF_NONE;
1049 token_metadata_len = sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey)
1050 + sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)
1051 + strlen (handle->scopes) + 1; //With 0-Terminator
1052 token_metadata = GNUNET_malloc (token_metadata_len);
1053 write_ptr = token_metadata;
1054 memcpy (token_metadata, ecdhe_privkey, sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey));
1055 write_ptr += sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey);
1056 memcpy (write_ptr, &handle->aud_key, sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
1057 write_ptr += sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
1058 memcpy (write_ptr, handle->scopes, strlen (handle->scopes) + 1); //with 0-Terminator;
1060 token_record[1].data = token_metadata;
1061 token_record[1].data_size = token_metadata_len;
1062 token_record[1].expiration_time = exp_time;
1063 token_record[1].record_type = GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA;
1064 token_record[1].flags = GNUNET_GNSRECORD_RF_PRIVATE;
1067 handle->ns_qe = GNUNET_NAMESTORE_records_store (ns_handle,
1072 &store_token_issue_cont,
1074 GNUNET_free (ecdhe_privkey);
1075 GNUNET_free (nonce_str);
1076 GNUNET_free (enc_token_str);
1077 GNUNET_free (token_metadata);
1081 * Collect attributes for token
1084 attr_collect (void *cls,
1085 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
1087 unsigned int rd_count,
1088 const struct GNUNET_GNSRECORD_Data *rd)
1092 struct IssueHandle *handle = cls;
1093 struct GNUNET_HashCode key;
1097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding attribute END: \n");
1098 handle->ns_it = NULL;
1099 GNUNET_SCHEDULER_add_now (&sign_and_return_token, handle);
1103 GNUNET_CRYPTO_hash (label,
1107 if (0 == rd_count ||
1108 ( (NULL != handle->attr_map) &&
1109 (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains (handle->attr_map,
1114 GNUNET_NAMESTORE_zone_iterator_next (handle->ns_it);
1118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding attribute: %s\n", label);
1122 if (rd->record_type == GNUNET_GNSRECORD_TYPE_ID_ATTR)
1124 data = GNUNET_GNSRECORD_value_to_string (rd->record_type,
1127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding value: %s\n", data);
1128 token_add_attr (handle->token,
1133 GNUNET_NAMESTORE_zone_iterator_next (handle->ns_it);
1138 for (; i < rd_count; i++)
1140 if (rd->record_type == GNUNET_GNSRECORD_TYPE_ID_ATTR)
1142 data = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
1145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding value: %s\n", data);
1146 token_add_attr (handle->token, label, data);
1151 GNUNET_NAMESTORE_zone_iterator_next (handle->ns_it);
1155 cleanup_exchange_handle (struct ExchangeHandle *handle)
1157 if (NULL != handle->ticket)
1158 ticket_destroy (handle->ticket);
1159 if (NULL != handle->token)
1160 token_destroy (handle->token);
1161 GNUNET_free (handle);
1165 process_lookup_result (void *cls, uint32_t rd_count,
1166 const struct GNUNET_GNSRECORD_Data *rd)
1168 struct ExchangeHandle *handle = cls;
1169 struct GNUNET_IDENTITY_PROVIDER_ExchangeResultMessage *erm;
1173 handle->lookup_request = NULL;
1176 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1177 "Number of tokens %d != 2.",
1179 cleanup_exchange_handle (handle);
1180 GNUNET_SCHEDULER_add_now (&do_shutdown, handle);
1185 GNUNET_GNSRECORD_value_to_string (GNUNET_GNSRECORD_TYPE_ID_TOKEN,
1190 GNUNET_assert (GNUNET_OK == token_parse (record_str,
1191 &handle->aud_privkey,
1195 GNUNET_assert (GNUNET_OK == token_to_string (handle->token,
1196 &handle->aud_privkey,
1199 erm = create_exchange_result_message (token_str,
1201 handle->ticket->payload->nonce);
1202 GNUNET_SERVER_notification_context_unicast (nc,
1206 GNUNET_SERVER_client_set_user_context (handle->client, NULL);
1208 cleanup_exchange_handle (handle);
1209 GNUNET_free (record_str);
1210 GNUNET_free (token_str);
1219 * Handler for exchange message
1222 * @param client who sent the message
1223 * @param message the message
1226 handle_exchange_message (void *cls,
1227 struct GNUNET_SERVER_Client *client,
1228 const struct GNUNET_MessageHeader *message)
1230 const struct GNUNET_IDENTITY_PROVIDER_ExchangeMessage *em;
1231 struct ExchangeHandle *xchange_handle;
1236 size = ntohs (message->size);
1237 if (size <= sizeof (struct GNUNET_IDENTITY_PROVIDER_ExchangeMessage))
1240 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1243 em = (const struct GNUNET_IDENTITY_PROVIDER_ExchangeMessage *) message;
1244 ticket = (const char *) &em[1];
1245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1246 "Received EXCHANGE of `%s' from client\n",
1248 xchange_handle = GNUNET_malloc (sizeof (struct ExchangeHandle));
1249 xchange_handle->aud_privkey = em->aud_privkey;
1251 if (GNUNET_SYSERR == ticket_parse (ticket,
1252 &xchange_handle->aud_privkey,
1253 &xchange_handle->ticket))
1255 GNUNET_free (xchange_handle);
1256 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Looking for token under %s\n",
1260 xchange_handle->ticket->payload->label);
1261 GNUNET_asprintf (&lookup_query,
1263 xchange_handle->ticket->payload->label);
1264 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1265 GNUNET_SERVER_notification_context_add (nc, client);
1266 GNUNET_SERVER_client_set_user_context (client, xchange_handle);
1267 xchange_handle->client = client;
1268 xchange_handle->lookup_request = GNUNET_GNS_lookup (gns_handle,
1270 &xchange_handle->ticket->payload->identity_key,
1271 GNUNET_GNSRECORD_TYPE_ID_TOKEN,
1272 GNUNET_GNS_LO_LOCAL_MASTER,
1274 &process_lookup_result,
1276 GNUNET_free (lookup_query);
1283 * Look for existing token
1285 * @param cls the identity entry
1286 * @param zone the identity
1287 * @param lbl the name of the record
1288 * @param rd_count number of records
1289 * @param rd record data
1293 find_existing_token (void *cls,
1294 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
1296 unsigned int rd_count,
1297 const struct GNUNET_GNSRECORD_Data *rd)
1299 struct IssueHandle *handle = cls;
1300 const struct GNUNET_GNSRECORD_Data *token_metadata_record;
1301 struct GNUNET_CRYPTO_EcdsaPublicKey *aud_key;
1302 struct GNUNET_HashCode key;
1303 int scope_count_token;
1311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1312 ">>> No existing token found\n");
1315 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
1317 GNUNET_STRINGS_base64_encode ((char*)&rnd_key,
1320 handle->ns_it = NULL;
1321 handle->ns_it = GNUNET_NAMESTORE_zone_iteration_start (ns_handle,
1328 //There should be only a single record for a token under a label
1331 GNUNET_NAMESTORE_zone_iterator_next (handle->ns_it);
1335 if (rd[0].record_type == GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA)
1337 token_metadata_record = &rd[0];
1339 token_metadata_record = &rd[1];
1341 if (token_metadata_record->record_type != GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA)
1343 GNUNET_NAMESTORE_zone_iterator_next (handle->ns_it);
1346 ecdhe_privkey = *((struct GNUNET_CRYPTO_EcdhePrivateKey *)token_metadata_record->data);
1348 (struct GNUNET_CRYPTO_EcdsaPublicKey *)(token_metadata_record->data+sizeof(struct GNUNET_CRYPTO_EcdhePrivateKey));
1349 tmp_scopes = GNUNET_strdup ((char*) aud_key+sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
1351 if (0 != memcmp (aud_key, &handle->aud_key,
1352 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
1354 char *tmp2 = GNUNET_STRINGS_data_to_string_alloc (aud_key,
1355 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
1356 //Audience does not match!
1357 char *tmp = GNUNET_GNSRECORD_value_to_string (GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA,
1358 token_metadata_record->data,
1359 token_metadata_record->data_size);
1360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1361 "Token does not match audience %s vs %s. Moving on\n",
1364 GNUNET_free (tmp_scopes);
1365 GNUNET_NAMESTORE_zone_iterator_next (handle->ns_it);
1369 scope = strtok (tmp_scopes, ",");
1370 scope_count_token = 0;
1371 while (NULL != scope)
1373 GNUNET_CRYPTO_hash (scope,
1377 if ((NULL != handle->attr_map) &&
1378 (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains (handle->attr_map, &key)))
1380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1381 "Issued token does not include `%s'. Moving on\n", scope);
1382 GNUNET_free (tmp_scopes);
1383 GNUNET_NAMESTORE_zone_iterator_next (handle->ns_it);
1386 scope_count_token++;
1387 scope = strtok (NULL, ",");
1389 GNUNET_free (tmp_scopes);
1390 //All scopes in token are also in request. Now
1392 if (GNUNET_CONTAINER_multihashmap_size (handle->attr_map) == scope_count_token)
1394 //We have an existing token
1395 handle->label = GNUNET_strdup (lbl);
1396 handle->ns_it = NULL;
1397 handle->ns_it = GNUNET_NAMESTORE_zone_iteration_start (ns_handle,
1404 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1405 "Nuber of attributes in token do not match request\n");
1407 GNUNET_NAMESTORE_zone_iterator_next (handle->ns_it);
1413 * Handler for issue message
1416 * @param client who sent the message
1417 * @param message the message
1420 handle_issue_message (void *cls,
1421 struct GNUNET_SERVER_Client *client,
1422 const struct GNUNET_MessageHeader *message)
1424 const struct GNUNET_IDENTITY_PROVIDER_IssueMessage *im;
1430 struct GNUNET_HashCode key;
1431 struct IssueHandle *issue_handle;
1433 size = ntohs (message->size);
1434 if (size <= sizeof (struct GNUNET_IDENTITY_PROVIDER_IssueMessage))
1437 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1440 im = (const struct GNUNET_IDENTITY_PROVIDER_IssueMessage *) message;
1441 scopes = (const char *) &im[1];
1442 issue_handle = GNUNET_malloc (sizeof (struct IssueHandle));
1443 issue_handle->attr_map = GNUNET_CONTAINER_multihashmap_create (5,
1445 /* FIXME: check that scopes is 0-termianted, Out-of-bounds access
1447 scopes_tmp = GNUNET_strdup (scopes);
1448 scope = strtok(scopes_tmp, ",");
1449 for (; NULL != scope; scope = strtok (NULL, ","))
1451 GNUNET_CRYPTO_hash (scope,
1454 GNUNET_CONTAINER_multihashmap_put (issue_handle->attr_map,
1457 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1459 GNUNET_free (scopes_tmp);
1461 issue_handle->aud_key = im->aud_key;
1462 issue_handle->iss_key = im->iss_key;
1463 GNUNET_CRYPTO_ecdsa_key_get_public (&im->iss_key,
1464 &issue_handle->iss_pkey);
1465 issue_handle->expiration = GNUNET_TIME_absolute_ntoh (im->expiration);
1466 issue_handle->nonce = ntohl (im->nonce);
1467 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1468 GNUNET_SERVER_notification_context_add (nc, client);
1469 GNUNET_SERVER_client_set_user_context (client, issue_handle);
1470 issue_handle->client = client;
1471 issue_handle->scopes = GNUNET_strdup (scopes);
1472 issue_handle->token = token_create (&issue_handle->iss_pkey,
1473 &issue_handle->aud_key);
1475 issue_handle->ns_it = GNUNET_NAMESTORE_zone_iteration_start (ns_handle,
1477 &find_existing_token,
1482 * Main function that will be run
1484 * @param cls closure
1485 * @param args remaining command-line arguments
1486 * @param cfgfile name of the configuration file used (for saving, can be NULL)
1487 * @param c configuration
1491 struct GNUNET_SERVER_Handle *server,
1492 const struct GNUNET_CONFIGURATION_Handle *c)
1494 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1495 {&handle_issue_message, NULL,
1496 GNUNET_MESSAGE_TYPE_IDENTITY_PROVIDER_ISSUE, 0},
1497 {&handle_exchange_message, NULL,
1498 GNUNET_MESSAGE_TYPE_IDENTITY_PROVIDER_EXCHANGE, 0},
1504 stats = GNUNET_STATISTICS_create ("identity-provider", cfg);
1505 GNUNET_SERVER_add_handlers (server, handlers);
1506 nc = GNUNET_SERVER_notification_context_create (server, 1);
1508 //Connect to identity and namestore services
1509 ns_handle = GNUNET_NAMESTORE_connect (cfg);
1510 if (NULL == ns_handle)
1512 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "error connecting to namestore");
1515 gns_handle = GNUNET_GNS_connect (cfg);
1516 if (NULL == gns_handle)
1518 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "error connecting to gns");
1521 identity_handle = GNUNET_IDENTITY_connect (cfg,
1526 GNUNET_CONFIGURATION_get_value_time (cfg,
1527 "identity-provider",
1528 "TOKEN_EXPIRATION_INTERVAL",
1529 &token_expiration_interval))
1531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1532 "Time window for zone iteration: %s\n",
1533 GNUNET_STRINGS_relative_time_to_string (token_expiration_interval,
1536 token_expiration_interval = DEFAULT_TOKEN_EXPIRATION_INTERVAL;
1539 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
1546 * @param argc number of arguments from the cli
1547 * @param argv command line arguments
1548 * @return 0 ok, 1 on error
1551 main (int argc, char *const *argv)
1553 return (GNUNET_OK ==
1554 GNUNET_SERVICE_run (argc, argv, "identity-provider",
1555 GNUNET_SERVICE_OPTION_NONE,
1556 &run, NULL)) ? 0 : 1;
1559 /* end of gnunet-service-identity-provider.c */