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"
45 * Normal operation state
47 #define STATE_POST_INIT 1
50 * Minimum interval between updates
52 #define MIN_WAIT_TIME GNUNET_TIME_UNIT_MINUTES
55 * Standard token expiration time
57 #define DEFAULT_TOKEN_EXPIRATION_INTERVAL GNUNET_TIME_UNIT_HOURS
60 * Service state (to detect initial update pass)
65 * Head of ego entry DLL
67 static struct EgoEntry *ego_head;
70 * Tail of ego entry DLL
72 static struct EgoEntry *ego_tail;
77 static struct GNUNET_IDENTITY_Handle *identity_handle;
80 * Token expiration interval
82 static struct GNUNET_TIME_Relative token_expiration_interval;
87 static struct GNUNET_NAMESTORE_Handle *ns_handle;
92 static struct GNUNET_GNS_Handle *gns_handle;
97 static struct GNUNET_NAMESTORE_QueueEntry *ns_qe;
102 static struct GNUNET_NAMESTORE_ZoneIterator *ns_it;
107 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
326 * @param tc task context
329 handle_token_update (void *cls,
330 const struct GNUNET_SCHEDULER_TaskContext *tc)
332 char *token_metadata;
335 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key;
336 struct GNUNET_CRYPTO_EcdsaPublicKey pub_key;
337 struct GNUNET_CRYPTO_EcdhePrivateKey *new_ecdhe_privkey;
338 struct EgoEntry *ego_entry = cls;
339 struct GNUNET_GNSRECORD_Data token_record[2];
340 struct GNUNET_HashCode key_hash;
341 struct GNUNET_TIME_Relative token_rel_exp;
342 struct GNUNET_TIME_Relative token_ttl;
343 struct GNUNET_TIME_Absolute token_exp;
344 struct GNUNET_TIME_Absolute token_nbf;
345 struct GNUNET_TIME_Absolute new_exp;
346 struct GNUNET_TIME_Absolute new_iat;
347 struct GNUNET_TIME_Absolute new_nbf;
348 struct IdentityToken *new_token;
349 struct TokenAttr *cur_value;
350 struct TokenAttr *attr;
351 size_t token_metadata_len;
353 priv_key = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego);
354 GNUNET_IDENTITY_ego_get_public_key (ego_entry->ego,
357 //Note: We need the token expiration time here. Not the record expiration
359 //There are two types of tokens: Token that expire on GNS level with
360 //an absolute expiration time. Those are basically tokens that will
361 //be automatically revoked on (record)expiration.
362 //Tokens stored with relative expiration times will expire on the token level (token expiration)
363 //but this service will reissue new tokens that can be retrieved from GNS
366 for (attr = token->attr_head; NULL != attr; attr = attr->next)
368 if (0 == strcmp (attr->name, "exp"))
370 sscanf (attr->val_head->value,
372 &token_exp.abs_value_us);
373 } else if (0 == strcmp (attr->name, "nbf")) {
374 sscanf (attr->val_head->value,
376 &token_nbf.abs_value_us);
379 token_rel_exp = GNUNET_TIME_absolute_get_difference (token_nbf, token_exp);
381 token_ttl = GNUNET_TIME_absolute_get_remaining (token_exp);
382 if (0 != GNUNET_TIME_absolute_get_remaining (token_exp).rel_value_us)
384 //This token is not yet expired! Save and skip
385 if (min_rel_exp.rel_value_us > token_ttl.rel_value_us)
387 min_rel_exp = token_ttl;
393 GNUNET_free (scopes);
395 GNUNET_NAMESTORE_zone_iterator_next (ns_it);
398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
399 "Token is expired. Create a new one\n");
400 new_token = token_create (&pub_key,
402 new_exp = GNUNET_TIME_relative_to_absolute (token_rel_exp);
403 new_nbf = GNUNET_TIME_absolute_get ();
405 for (attr = token->attr_head; NULL != attr; attr = attr->next)
407 if (0 == strcmp (attr->name, "exp"))
409 token_add_attr_int (new_token, attr->name, new_exp.abs_value_us);
411 else if (0 == strcmp (attr->name, "nbf"))
413 token_add_attr_int (new_token, attr->name, new_nbf.abs_value_us);
415 else if (0 == strcmp (attr->name, "iat"))
417 token_add_attr_int (new_token, attr->name, new_iat.abs_value_us);
419 else if ((0 == strcmp (attr->name, "iss"))
420 || (0 == strcmp (attr->name, "aud")))
424 else if (0 == strcmp (attr->name, "sub"))
426 token_add_attr (new_token,
428 attr->val_head->value);
432 GNUNET_CRYPTO_hash (attr->name,
435 //Check if attr still exists. omit of not
437 GNUNET_CONTAINER_multihashmap_contains (ego_entry->attr_map,
440 cur_value = GNUNET_CONTAINER_multihashmap_get (ego_entry->attr_map,
442 GNUNET_CONTAINER_DLL_insert (new_token->attr_head,
443 new_token->attr_tail,
449 // reassemble and set
450 GNUNET_assert (token_serialize (new_token,
455 token_record[0].data = enc_token_str;
456 token_record[0].data_size = strlen (enc_token_str) + 1;
457 token_record[0].expiration_time = rd_exp; //Old expiration time
458 token_record[0].record_type = GNUNET_GNSRECORD_TYPE_ID_TOKEN;
459 token_record[0].flags = GNUNET_GNSRECORD_RF_NONE;
462 token_metadata_len = sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey)
463 + sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)
464 + strlen (scopes) + 1; //With 0-Terminator
465 token_metadata = GNUNET_malloc (token_metadata_len);
466 write_ptr = token_metadata;
467 memcpy (token_metadata, new_ecdhe_privkey, sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey));
468 write_ptr += sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey);
469 memcpy (write_ptr, &token->aud_key, sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
470 write_ptr += sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
471 memcpy (write_ptr, scopes, strlen (scopes) + 1); //with 0-Terminator;
473 token_record[1].data = token_metadata;
474 token_record[1].data_size = token_metadata_len;
475 token_record[1].expiration_time = rd_exp;
476 token_record[1].record_type = GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA;
477 token_record[1].flags = GNUNET_GNSRECORD_RF_PRIVATE;
479 ns_qe = GNUNET_NAMESTORE_records_store (ns_handle,
486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, ">>> Updating Token w/ %s\n", new_token);
487 token_destroy (new_token);
488 token_destroy (token);
489 GNUNET_free (new_ecdhe_privkey);
490 GNUNET_free (enc_token_str);
494 GNUNET_free (scopes);
499 update_identities(void *cls,
500 const struct GNUNET_SCHEDULER_TaskContext *tc);
508 * @param value the json_t attribute value
512 clear_ego_attrs (void *cls,
513 const struct GNUNET_HashCode *key,
516 struct TokenAttr *attr = value;
517 struct TokenAttrValue *val;
518 struct TokenAttrValue *tmp_val;
519 for (val = attr->val_head; NULL != val;)
522 GNUNET_CONTAINER_DLL_remove (attr->val_head,
525 GNUNET_free (val->value);
529 GNUNET_free (attr->name);
537 * Update all ID_TOKEN records for an identity and store them
539 * @param cls the identity entry
540 * @param zone the identity
541 * @param lbl the name of the record
542 * @param rd_count number of records
543 * @param rd record data
547 token_collect (void *cls,
548 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
550 unsigned int rd_count,
551 const struct GNUNET_GNSRECORD_Data *rd)
553 struct EgoEntry *ego_entry = cls;
554 const struct GNUNET_GNSRECORD_Data *token_record;
555 const struct GNUNET_GNSRECORD_Data *token_metadata_record;
556 struct GNUNET_CRYPTO_EcdsaPublicKey *aud_key;
561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
562 ">>> Updating Ego finished\n");
563 //Clear attribute map for ego
564 GNUNET_CONTAINER_multihashmap_iterate (ego_entry->attr_map,
567 GNUNET_CONTAINER_multihashmap_clear (ego_entry->attr_map);
568 update_task = GNUNET_SCHEDULER_add_now (&update_identities,
573 //There should be only a single record for a token under a label
576 GNUNET_NAMESTORE_zone_iterator_next (ns_it);
580 if (rd[0].record_type == GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA)
582 token_metadata_record = &rd[0];
583 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, ego_entry);
618 * Collect all ID_ATTR records for an identity and store them
620 * @param cls the identity entry
621 * @param zone the identity
622 * @param lbl the name of the record
623 * @param rd_count number of records
624 * @param rd record data
628 attribute_collect (void *cls,
629 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
631 unsigned int rd_count,
632 const struct GNUNET_GNSRECORD_Data *rd)
634 struct EgoEntry *ego_entry = cls;
635 struct GNUNET_HashCode key;
636 struct TokenAttr *attr;
637 struct TokenAttrValue *val;
644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
645 ">>> Updating Attributes finished\n");
646 ego_entry->attributes_dirty = GNUNET_NO;
647 update_task = GNUNET_SCHEDULER_add_now (&update_identities, ego_entry);
653 GNUNET_NAMESTORE_zone_iterator_next (ns_it);
656 GNUNET_CRYPTO_hash (lbl,
661 if (rd->record_type == GNUNET_GNSRECORD_TYPE_ID_ATTR)
663 val_str = GNUNET_GNSRECORD_value_to_string (rd->record_type,
666 attr = GNUNET_malloc (sizeof (struct TokenAttr));
667 attr->name = GNUNET_strdup (lbl);
668 val = GNUNET_malloc (sizeof (struct TokenAttrValue));
669 val->value = val_str;
670 GNUNET_CONTAINER_DLL_insert (attr->val_head,
673 GNUNET_CONTAINER_multihashmap_put (ego_entry->attr_map,
676 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
679 GNUNET_NAMESTORE_zone_iterator_next (ns_it);
683 attr = GNUNET_malloc (sizeof (struct TokenAttr));
684 attr->name = GNUNET_strdup (lbl);
685 for (i = 0; i < rd_count; i++)
687 if (rd[i].record_type == GNUNET_GNSRECORD_TYPE_ID_ATTR)
689 val_str = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
692 val = GNUNET_malloc (sizeof (struct TokenAttrValue));
693 val->value = val_str;
694 GNUNET_CONTAINER_DLL_insert (attr->val_head,
699 GNUNET_CONTAINER_multihashmap_put (ego_entry->attr_map,
702 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
703 GNUNET_NAMESTORE_zone_iterator_next (ns_it);
709 * Update identity information for ego. If attribute map is
710 * dirty, first update the attributes.
712 * @param cls the ego to update
713 * param tc task context
717 update_identities(void *cls,
718 const struct GNUNET_SCHEDULER_TaskContext *tc)
720 struct EgoEntry *next_ego = cls;
721 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key;
723 if (NULL == next_ego)
725 if (min_rel_exp.rel_value_us < MIN_WAIT_TIME.rel_value_us)
726 min_rel_exp = MIN_WAIT_TIME;
727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
728 ">>> Finished. Rescheduling in %d\n",
729 min_rel_exp.rel_value_us);
731 //finished -> reschedule
732 update_task = GNUNET_SCHEDULER_add_delayed (min_rel_exp,
735 min_rel_exp.rel_value_us = 0;
738 priv_key = GNUNET_IDENTITY_ego_get_private_key (next_ego->ego);
739 if (GNUNET_YES == next_ego->attributes_dirty)
741 //Starting over. We must update the Attributes for they might have changed.
742 ns_it = GNUNET_NAMESTORE_zone_iteration_start (ns_handle,
750 //Ego will be dirty next time
751 next_ego->attributes_dirty = GNUNET_YES;
752 ns_it = GNUNET_NAMESTORE_zone_iteration_start (ns_handle,
762 * Function called initially to start update task
767 GNUNET_log (GNUNET_ERROR_TYPE_INFO, ">>> Starting Service\n");
768 //Initially iterate all itenties and refresh all tokens
769 update_task = GNUNET_SCHEDULER_add_now (&update_identities, ego_head);
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,
869 const struct GNUNET_SCHEDULER_TaskContext *tc)
871 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
872 "Shutting down...\n");
877 static struct GNUNET_IDENTITY_PROVIDER_ExchangeResultMessage*
878 create_exchange_result_message (const char* token,
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 memcpy (&erm[1], token, token_len);
893 static struct GNUNET_IDENTITY_PROVIDER_IssueResultMessage*
894 create_issue_result_message (const char* label,
898 struct GNUNET_IDENTITY_PROVIDER_IssueResultMessage *irm;
901 irm = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_PROVIDER_IssueResultMessage)
903 + strlen (ticket) + 1
904 + strlen (token) + 1);
905 irm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_PROVIDER_ISSUE_RESULT);
906 irm->header.size = htons (sizeof (struct GNUNET_IDENTITY_PROVIDER_IssueResultMessage)
908 + strlen (ticket) + 1
909 + strlen (token) + 1);
910 GNUNET_asprintf (&tmp_str, "%s,%s,%s", label, ticket, token);
911 memcpy (&irm[1], tmp_str, strlen (tmp_str) + 1);
912 GNUNET_free (tmp_str);
917 cleanup_issue_handle (struct IssueHandle *handle)
919 if (NULL != handle->attr_map)
920 GNUNET_CONTAINER_multihashmap_destroy (handle->attr_map);
921 if (NULL != handle->scopes)
922 GNUNET_free (handle->scopes);
923 if (NULL != handle->token)
924 token_destroy (handle->token);
925 if (NULL != handle->ticket)
926 ticket_destroy (handle->ticket);
927 if (NULL != handle->label)
928 GNUNET_free (handle->label);
929 GNUNET_free (handle);
933 store_token_issue_cont (void *cls,
937 struct IssueHandle *handle = cls;
938 struct GNUNET_IDENTITY_PROVIDER_IssueResultMessage *irm;
941 handle->ns_qe = NULL;
942 if (GNUNET_SYSERR == success)
944 cleanup_issue_handle (handle);
945 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n",
947 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
950 if (GNUNET_OK != ticket_serialize (handle->ticket,
954 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n",
955 "Error serializing ticket\n");
956 cleanup_issue_handle (handle);
957 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
960 if (GNUNET_OK != token_to_string (handle->token,
964 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n",
965 "Error serializing token\n");
966 GNUNET_free (ticket_str);
967 cleanup_issue_handle (handle);
968 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
971 irm = create_issue_result_message (handle->label, ticket_str, token_str);
972 GNUNET_SERVER_notification_context_unicast (nc,
976 GNUNET_SERVER_client_set_user_context (handle->client, NULL);
977 cleanup_issue_handle (handle);
979 GNUNET_free (ticket_str);
980 GNUNET_free (token_str);
984 * Build a GNUid token for identity
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,
994 const struct GNUNET_SCHEDULER_TaskContext *tc)
996 struct GNUNET_CRYPTO_EcdsaPublicKey pub_key;
997 struct GNUNET_CRYPTO_EcdhePrivateKey *ecdhe_privkey;
998 struct IssueHandle *handle = cls;
999 struct GNUNET_GNSRECORD_Data token_record[2];
1001 char *enc_token_str;
1002 char *token_metadata;
1006 size_t token_metadata_len;
1010 GNUNET_asprintf (&nonce_str, "%d", handle->nonce);
1011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Request nonce: %s\n", nonce_str);
1013 GNUNET_CRYPTO_ecdsa_key_get_public (&handle->iss_key,
1015 handle->ticket = ticket_create (nonce_str,
1020 time = GNUNET_TIME_absolute_get().abs_value_us;
1021 exp_time = time + token_expiration_interval.rel_value_us;
1023 token_add_attr_int (handle->token, "nbf", time);
1024 token_add_attr_int (handle->token, "iat", time);
1025 token_add_attr_int (handle->token, "exp", exp_time);
1026 token_add_attr (handle->token, "nonce", nonce_str);
1028 //Token in a serialized encrypted format
1029 GNUNET_assert (token_serialize (handle->token,
1034 //Token record E,E_K (Token)
1035 token_record[0].data = enc_token_str;
1036 token_record[0].data_size = strlen (enc_token_str) + 1;
1037 token_record[0].expiration_time = exp_time;
1038 token_record[0].record_type = GNUNET_GNSRECORD_TYPE_ID_TOKEN;
1039 token_record[0].flags = GNUNET_GNSRECORD_RF_NONE;
1042 token_metadata_len = sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey)
1043 + sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)
1044 + strlen (handle->scopes) + 1; //With 0-Terminator
1045 token_metadata = GNUNET_malloc (token_metadata_len);
1046 write_ptr = token_metadata;
1047 memcpy (token_metadata, ecdhe_privkey, sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey));
1048 write_ptr += sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey);
1049 memcpy (write_ptr, &handle->aud_key, sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
1050 write_ptr += sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
1051 memcpy (write_ptr, handle->scopes, strlen (handle->scopes) + 1); //with 0-Terminator;
1053 token_record[1].data = token_metadata;
1054 token_record[1].data_size = token_metadata_len;
1055 token_record[1].expiration_time = exp_time;
1056 token_record[1].record_type = GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA;
1057 token_record[1].flags = GNUNET_GNSRECORD_RF_PRIVATE;
1060 handle->ns_qe = GNUNET_NAMESTORE_records_store (ns_handle,
1065 &store_token_issue_cont,
1067 GNUNET_free (ecdhe_privkey);
1068 GNUNET_free (nonce_str);
1069 GNUNET_free (enc_token_str);
1070 GNUNET_free (token_metadata);
1074 * Collect attributes for token
1077 attr_collect (void *cls,
1078 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
1080 unsigned int rd_count,
1081 const struct GNUNET_GNSRECORD_Data *rd)
1085 struct IssueHandle *handle = cls;
1086 struct GNUNET_HashCode key;
1090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding attribute END: \n");
1091 handle->ns_it = NULL;
1092 GNUNET_SCHEDULER_add_now (&sign_and_return_token, handle);
1096 GNUNET_CRYPTO_hash (label,
1100 if (0 == rd_count ||
1101 ( (NULL != handle->attr_map) &&
1102 (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains (handle->attr_map,
1107 GNUNET_NAMESTORE_zone_iterator_next (handle->ns_it);
1111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding attribute: %s\n", label);
1115 if (rd->record_type == GNUNET_GNSRECORD_TYPE_ID_ATTR)
1117 data = GNUNET_GNSRECORD_value_to_string (rd->record_type,
1120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding value: %s\n", data);
1121 token_add_attr (handle->token,
1126 GNUNET_NAMESTORE_zone_iterator_next (handle->ns_it);
1131 for (; i < rd_count; i++)
1133 if (rd->record_type == GNUNET_GNSRECORD_TYPE_ID_ATTR)
1135 data = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
1138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding value: %s\n", data);
1139 token_add_attr (handle->token, label, data);
1144 GNUNET_NAMESTORE_zone_iterator_next (handle->ns_it);
1148 cleanup_exchange_handle (struct ExchangeHandle *handle)
1150 if (NULL != handle->ticket)
1151 ticket_destroy (handle->ticket);
1152 if (NULL != handle->token)
1153 token_destroy (handle->token);
1154 GNUNET_free (handle);
1158 process_lookup_result (void *cls, uint32_t rd_count,
1159 const struct GNUNET_GNSRECORD_Data *rd)
1161 struct ExchangeHandle *handle = cls;
1162 struct GNUNET_IDENTITY_PROVIDER_ExchangeResultMessage *erm;
1166 handle->lookup_request = NULL;
1169 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1170 "Number of tokens %d != 2.",
1172 cleanup_exchange_handle (handle);
1173 GNUNET_SCHEDULER_add_now (&do_shutdown, handle);
1178 GNUNET_GNSRECORD_value_to_string (GNUNET_GNSRECORD_TYPE_ID_TOKEN,
1183 GNUNET_assert (GNUNET_OK == token_parse (record_str,
1184 &handle->aud_privkey,
1188 GNUNET_assert (GNUNET_OK == token_to_string (handle->token,
1189 &handle->aud_privkey,
1192 erm = create_exchange_result_message (token_str,
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 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1435 "Received ISSUE of `%s' from client\n",
1437 issue_handle = GNUNET_malloc (sizeof (struct IssueHandle));
1438 issue_handle->attr_map = GNUNET_CONTAINER_multihashmap_create (5,
1440 scopes_tmp = GNUNET_strdup (scopes);
1441 scope = strtok(scopes_tmp, ",");
1442 for (; NULL != scope; scope = strtok (NULL, ","))
1444 GNUNET_CRYPTO_hash (scope,
1447 GNUNET_CONTAINER_multihashmap_put (issue_handle->attr_map,
1450 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1452 GNUNET_free (scopes_tmp);
1454 issue_handle->aud_key = im->aud_key;
1455 issue_handle->iss_key = im->iss_key;
1456 GNUNET_CRYPTO_ecdsa_key_get_public (&im->iss_key,
1457 &issue_handle->iss_pkey);
1458 issue_handle->expiration = GNUNET_TIME_absolute_ntoh (im->expiration);
1459 issue_handle->nonce = ntohl (im->nonce);
1460 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1461 GNUNET_SERVER_notification_context_add (nc, client);
1462 GNUNET_SERVER_client_set_user_context (client, issue_handle);
1463 issue_handle->client = client;
1464 issue_handle->scopes = GNUNET_strdup (scopes);
1465 issue_handle->token = token_create (&issue_handle->iss_pkey,
1466 &issue_handle->aud_key);
1468 issue_handle->ns_it = GNUNET_NAMESTORE_zone_iteration_start (ns_handle,
1470 &find_existing_token,
1475 * Main function that will be run
1477 * @param cls closure
1478 * @param args remaining command-line arguments
1479 * @param cfgfile name of the configuration file used (for saving, can be NULL)
1480 * @param c configuration
1484 struct GNUNET_SERVER_Handle *server,
1485 const struct GNUNET_CONFIGURATION_Handle *c)
1487 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1488 {&handle_issue_message, NULL,
1489 GNUNET_MESSAGE_TYPE_IDENTITY_PROVIDER_ISSUE, 0},
1490 {&handle_exchange_message, NULL,
1491 GNUNET_MESSAGE_TYPE_IDENTITY_PROVIDER_EXCHANGE, 0},
1497 stats = GNUNET_STATISTICS_create ("identity-provider", cfg);
1498 GNUNET_SERVER_add_handlers (server, handlers);
1499 nc = GNUNET_SERVER_notification_context_create (server, 1);
1501 //Connect to identity and namestore services
1502 ns_handle = GNUNET_NAMESTORE_connect (cfg);
1503 if (NULL == ns_handle)
1505 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "error connecting to namestore");
1508 gns_handle = GNUNET_GNS_connect (cfg);
1509 if (NULL == gns_handle)
1511 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "error connecting to gns");
1514 identity_handle = GNUNET_IDENTITY_connect (cfg,
1519 GNUNET_CONFIGURATION_get_value_time (cfg,
1520 "identity-provider",
1521 "TOKEN_EXPIRATION_INTERVAL",
1522 &token_expiration_interval))
1524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1525 "Time window for zone iteration: %s\n",
1526 GNUNET_STRINGS_relative_time_to_string (token_expiration_interval,
1529 token_expiration_interval = DEFAULT_TOKEN_EXPIRATION_INTERVAL;
1532 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1533 &do_shutdown, NULL);
1541 * @param argc number of arguments from the cli
1542 * @param argv command line arguments
1543 * @return 0 ok, 1 on error
1547 main (int argc, char *const *argv)
1549 return (GNUNET_OK ==
1550 GNUNET_SERVICE_run (argc, argv, "identity-provider",
1551 GNUNET_SERVICE_OPTION_NONE,
1552 &run, NULL)) ? 0 : 1;
1555 /* end of gnunet-rest-server.c */