2 This file is part of GNUnet.
3 Copyright (C) 2012-2015 Christian Grothoff (and other contributing authors)
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/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"
36 #include "gnunet_signatures.h"
37 #include "identity_provider.h"
38 #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 * Service state (to detect initial update pass)
61 * Head of ego entry DLL
63 static struct EgoEntry *ego_head;
66 * Tail of ego entry DLL
68 static struct EgoEntry *ego_tail;
73 static struct GNUNET_IDENTITY_Handle *identity_handle;
78 static struct GNUNET_NAMESTORE_Handle *ns_handle;
83 static struct GNUNET_GNS_Handle *gns_handle;
88 static struct GNUNET_NAMESTORE_QueueEntry *ns_qe;
93 static struct GNUNET_NAMESTORE_ZoneIterator *ns_it;
98 static struct GNUNET_SCHEDULER_Task * timeout_task;
104 static struct GNUNET_SCHEDULER_Task * update_task;
107 * Timeout for next update pass
109 static struct GNUNET_TIME_Relative min_rel_exp;
113 * Currently processed token
115 static struct IdentityToken *token;
118 * Label for currently processed token
123 * Scopes for processed token
128 * Expiration for processed token
130 static uint64_t rd_exp;
133 * ECDHE Privkey for processed token metadata
135 static struct GNUNET_CRYPTO_EcdhePrivateKey ecdhe_privkey;
138 * Handle to the statistics service.
140 static struct GNUNET_STATISTICS_Handle *stats;
143 * Notification context, simplifies client broadcasts.
145 static struct GNUNET_SERVER_NotificationContext *nc;
147 struct ExchangeHandle
153 struct GNUNET_SERVER_Client *client;
158 struct TokenTicket *ticket;
163 struct IdentityToken *token;
168 struct GNUNET_GNS_LookupRequest *lookup_request;
173 struct GNUNET_CRYPTO_EcdsaPrivateKey aud_privkey;
187 struct GNUNET_SERVER_Client *client;
192 struct GNUNET_CRYPTO_EcdsaPrivateKey iss_key;
197 struct GNUNET_CRYPTO_EcdsaPublicKey iss_pkey;
202 struct GNUNET_CRYPTO_EcdsaPublicKey aud_key;
207 struct GNUNET_TIME_Absolute expiration;
222 struct GNUNET_NAMESTORE_ZoneIterator *ns_it;
227 struct GNUNET_CONTAINER_MultiHashMap *attr_map;
232 struct IdentityToken *token;
237 struct TokenTicket *ticket;
242 struct GNUNET_NAMESTORE_QueueEntry *ns_qe;
246 * DLL for ego handles to egos containing the ID_ATTRS in a map in json_t format
254 struct EgoEntry *next;
259 struct EgoEntry *prev;
264 struct GNUNET_IDENTITY_Ego *ego;
267 * Attribute map. Contains the attributes as json_t
269 struct GNUNET_CONTAINER_MultiHashMap *attr_map;
272 * Attributes are old and should be updated if GNUNET_YES
274 int attributes_dirty;
280 static const struct GNUNET_CONFIGURATION_Handle *cfg;
284 * Continuation for token store call
287 * @param success error code
288 * @param emsg error message
291 store_token_cont (void *cls,
296 if (GNUNET_SYSERR == success)
298 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
299 "Failed to update token: %s\n",
303 GNUNET_NAMESTORE_zone_iterator_next (ns_it);
308 * This function updates the old token with new attributes,
309 * removes deleted attributes and expiration times.
311 * @param cls the ego entry
312 * @param tc task context
315 handle_token_update (void *cls,
316 const struct GNUNET_SCHEDULER_TaskContext *tc)
318 char *token_metadata;
322 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key;
323 struct GNUNET_CRYPTO_EcdsaPublicKey pub_key;
324 struct GNUNET_CRYPTO_EcdhePrivateKey *new_ecdhe_privkey;
325 struct EgoEntry *ego_entry = cls;
326 struct GNUNET_GNSRECORD_Data token_record[2];
327 struct GNUNET_HashCode key_hash;
328 struct GNUNET_TIME_Relative token_rel_exp;
329 struct GNUNET_TIME_Relative token_ttl;
330 struct GNUNET_TIME_Absolute token_exp;
331 struct GNUNET_TIME_Absolute token_nbf;
332 struct GNUNET_TIME_Absolute new_exp;
333 struct GNUNET_TIME_Absolute new_iat;
334 struct GNUNET_TIME_Absolute new_nbf;
335 struct IdentityToken *new_token;
336 json_t *payload_json;
339 json_t *token_nbf_json;
340 json_t *token_exp_json;
341 size_t token_metadata_len;
343 priv_key = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego);
344 GNUNET_IDENTITY_ego_get_public_key (ego_entry->ego,
347 //Note: We need the token expiration time here. Not the record expiration
349 //There are two types of tokens: Token that expire on GNS level with
350 //an absolute expiration time. Those are basically tokens that will
351 //be automatically revoked on (record)expiration.
352 //Tokens stored with relative expiration times will expire on the token level (token expiration)
353 //but this service will reissue new tokens that can be retrieved from GNS
356 payload_json = token->payload;
358 token_exp_json = json_object_get (payload_json, "exp");
359 token_nbf_json = json_object_get (payload_json, "nbf");
360 token_exp.abs_value_us = json_integer_value(token_exp_json);
361 token_nbf.abs_value_us = json_integer_value(token_nbf_json);
362 token_rel_exp = GNUNET_TIME_absolute_get_difference (token_nbf, token_exp);
364 token_ttl = GNUNET_TIME_absolute_get_remaining (token_exp);
365 if (0 != GNUNET_TIME_absolute_get_remaining (token_exp).rel_value_us)
367 //This token is not yet expired! Save and skip
368 if (min_rel_exp.rel_value_us > token_ttl.rel_value_us)
370 min_rel_exp = token_ttl;
372 json_decref (payload_json);
377 GNUNET_free (scopes);
379 GNUNET_NAMESTORE_zone_iterator_next (ns_it);
382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
383 "Token is expired. Create a new one\n");
384 new_token = token_create (&pub_key,
386 new_exp = GNUNET_TIME_relative_to_absolute (token_rel_exp);
387 new_nbf = GNUNET_TIME_absolute_get ();
390 json_object_foreach(payload_json, key, value) {
391 if (0 == strcmp (key, "exp"))
393 token_add_json (new_token, key, json_integer (new_exp.abs_value_us));
395 else if (0 == strcmp (key, "nbf"))
397 token_add_json (new_token, key, json_integer (new_nbf.abs_value_us));
399 else if (0 == strcmp (key, "iat"))
401 token_add_json (new_token, key, json_integer (new_iat.abs_value_us));
403 else if ((0 == strcmp (key, "iss"))
404 || (0 == strcmp (key, "aud")))
408 else if ((0 == strcmp (key, "sub"))
409 || (0 == strcmp (key, "rnl")))
411 token_add_json (new_token, key, value);
414 GNUNET_CRYPTO_hash (key,
417 //Check if attr still exists. omit of not
418 if (GNUNET_NO != GNUNET_CONTAINER_multihashmap_contains (ego_entry->attr_map,
421 cur_value = GNUNET_CONTAINER_multihashmap_get (ego_entry->attr_map,
423 token_add_json (new_token, key, cur_value);
428 // reassemble and set
429 GNUNET_assert (token_serialize (new_token,
434 json_decref (payload_json);
436 token_record[0].data = enc_token_str;
437 token_record[0].data_size = strlen (enc_token_str) + 1;
438 token_record[0].expiration_time = rd_exp; //Old expiration time
439 token_record[0].record_type = GNUNET_GNSRECORD_TYPE_ID_TOKEN;
440 token_record[0].flags = GNUNET_GNSRECORD_RF_NONE;
443 token_metadata_len = sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey)
444 + sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)
445 + strlen (scopes) + 1; //With 0-Terminator
446 token_metadata = GNUNET_malloc (token_metadata_len);
447 write_ptr = token_metadata;
448 memcpy (token_metadata, new_ecdhe_privkey, sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey));
449 write_ptr += sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey);
450 memcpy (write_ptr, &token->aud_key, sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
451 write_ptr += sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
452 memcpy (write_ptr, scopes, strlen (scopes) + 1); //with 0-Terminator;
454 token_record[1].data = token_metadata;
455 token_record[1].data_size = token_metadata_len;
456 token_record[1].expiration_time = rd_exp;
457 token_record[1].record_type = GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA;
458 token_record[1].flags = GNUNET_GNSRECORD_RF_PRIVATE;
460 ns_qe = GNUNET_NAMESTORE_records_store (ns_handle,
467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, ">>> Updating Token w/ %s\n", new_token);
468 token_destroy (new_token);
469 token_destroy (token);
470 GNUNET_free (new_ecdhe_privkey);
471 GNUNET_free (enc_token_str);
475 GNUNET_free (scopes);
480 update_identities(void *cls,
481 const struct GNUNET_SCHEDULER_TaskContext *tc);
489 * @param value the json_t attribute value
493 clear_ego_attrs (void *cls,
494 const struct GNUNET_HashCode *key,
497 json_t *attr_value = value;
499 json_decref (attr_value);
507 * Update all ID_TOKEN records for an identity and store them
509 * @param cls the identity entry
510 * @param zone the identity
511 * @param lbl the name of the record
512 * @param rd_count number of records
513 * @param rd record data
517 token_collect (void *cls,
518 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
520 unsigned int rd_count,
521 const struct GNUNET_GNSRECORD_Data *rd)
523 struct EgoEntry *ego_entry = cls;
524 const struct GNUNET_GNSRECORD_Data *token_record;
525 const struct GNUNET_GNSRECORD_Data *token_metadata_record;
526 struct GNUNET_CRYPTO_EcdsaPublicKey *aud_key;
531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
532 ">>> Updating Ego finished\n");
533 //Clear attribute map for ego
534 GNUNET_CONTAINER_multihashmap_iterate (ego_entry->attr_map,
537 GNUNET_CONTAINER_multihashmap_clear (ego_entry->attr_map);
538 GNUNET_SCHEDULER_add_now (&update_identities, ego_entry->next);
542 //There should be only a single record for a token under a label
545 GNUNET_NAMESTORE_zone_iterator_next (ns_it);
549 if (rd[0].record_type == GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA)
551 token_metadata_record = &rd[0];
552 token_record = &rd[1];
554 token_record = &rd[0];
555 token_metadata_record = &rd[1];
557 GNUNET_assert (token_metadata_record->record_type == GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA);
558 GNUNET_assert (token_record->record_type == GNUNET_GNSRECORD_TYPE_ID_TOKEN);
560 //Get metadata and decrypt token
561 ecdhe_privkey = *((struct GNUNET_CRYPTO_EcdhePrivateKey *)token_metadata_record->data);
562 aud_key = (struct GNUNET_CRYPTO_EcdsaPublicKey *)&ecdhe_privkey+sizeof(struct GNUNET_CRYPTO_EcdhePrivateKey);
563 scopes = GNUNET_strdup ((char*) aud_key+sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
565 token_parse2 (token_record->data,
570 //token = GNUNET_GNSRECORD_value_to_string (rd->record_type,
573 label = GNUNET_strdup (lbl);
574 rd_exp = token_record->expiration_time;
576 GNUNET_SCHEDULER_add_now (&handle_token_update, ego_entry);
582 * Collect all ID_ATTR records for an identity and store them
584 * @param cls the identity entry
585 * @param zone the identity
586 * @param lbl the name of the record
587 * @param rd_count number of records
588 * @param rd record data
592 attribute_collect (void *cls,
593 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
595 unsigned int rd_count,
596 const struct GNUNET_GNSRECORD_Data *rd)
598 struct EgoEntry *ego_entry = cls;
600 struct GNUNET_HashCode key;
607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
608 ">>> Updating Attributes finished\n");
609 ego_entry->attributes_dirty = GNUNET_NO;
610 GNUNET_SCHEDULER_add_now (&update_identities, ego_entry);
616 GNUNET_NAMESTORE_zone_iterator_next (ns_it);
619 GNUNET_CRYPTO_hash (lbl,
624 if (rd->record_type == GNUNET_GNSRECORD_TYPE_ID_ATTR)
626 attr = GNUNET_GNSRECORD_value_to_string (rd->record_type,
629 attr_value = json_string (attr);
630 GNUNET_CONTAINER_multihashmap_put (ego_entry->attr_map,
633 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
637 GNUNET_NAMESTORE_zone_iterator_next (ns_it);
641 attr_value = json_array();
642 for (i = 0; i < rd_count; i++)
644 if (rd[i].record_type == GNUNET_GNSRECORD_TYPE_ID_ATTR)
646 attr = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
649 json_array_append_new (attr_value, json_string (attr));
654 GNUNET_CONTAINER_multihashmap_put (ego_entry->attr_map,
657 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
658 GNUNET_NAMESTORE_zone_iterator_next (ns_it);
664 * Update identity information for ego. If attribute map is
665 * dirty, first update the attributes.
667 * @param cls the ego to update
668 * param tc task context
672 update_identities(void *cls,
673 const struct GNUNET_SCHEDULER_TaskContext *tc)
675 struct EgoEntry *next_ego = cls;
676 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key;
677 if (NULL == next_ego)
679 if (min_rel_exp.rel_value_us < MIN_WAIT_TIME.rel_value_us)
680 min_rel_exp = MIN_WAIT_TIME;
681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
682 ">>> Finished. Rescheduling in %d\n",
683 min_rel_exp.rel_value_us);
685 //finished -> TODO reschedule
686 update_task = GNUNET_SCHEDULER_add_delayed (min_rel_exp,
689 min_rel_exp.rel_value_us = 0;
692 priv_key = GNUNET_IDENTITY_ego_get_private_key (next_ego->ego);
693 if (GNUNET_YES == next_ego->attributes_dirty)
695 //Starting over. We must update the Attributes for they might have changed.
696 ns_it = GNUNET_NAMESTORE_zone_iteration_start (ns_handle,
704 //Ego will be dirty next time
705 next_ego->attributes_dirty = GNUNET_YES;
706 ns_it = GNUNET_NAMESTORE_zone_iteration_start (ns_handle,
716 * Function called initially to start update task
721 GNUNET_log (GNUNET_ERROR_TYPE_INFO, ">>> Starting Service\n");
722 //Initially iterate all itenties and refresh all tokens
723 update_task = GNUNET_SCHEDULER_add_now (&update_identities, ego_head);
727 * Initial ego collection function.
732 * @param identifier ego name
736 struct GNUNET_IDENTITY_Ego *ego,
738 const char *identifier)
740 struct EgoEntry *new_entry;
741 if ((NULL == ego) && (STATE_INIT == state))
743 state = STATE_POST_INIT;
747 if (STATE_INIT == state) {
748 new_entry = GNUNET_malloc (sizeof (struct EgoEntry));
749 new_entry->ego = ego;
750 new_entry->attr_map = GNUNET_CONTAINER_multihashmap_create (5,
752 new_entry->attributes_dirty = GNUNET_YES;
753 GNUNET_CONTAINER_DLL_insert_tail(ego_head, ego_tail, new_entry);
763 struct EgoEntry *ego_entry;
764 struct EgoEntry *ego_tmp;
766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
770 GNUNET_SERVER_notification_context_destroy (nc);
775 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
779 if (NULL != timeout_task)
780 GNUNET_SCHEDULER_cancel (timeout_task);
781 if (NULL != update_task)
782 GNUNET_SCHEDULER_cancel (update_task);
783 if (NULL != identity_handle)
784 GNUNET_IDENTITY_disconnect (identity_handle);
785 if (NULL != gns_handle)
786 GNUNET_GNS_disconnect (gns_handle);
788 GNUNET_NAMESTORE_zone_iteration_stop (ns_it);
790 GNUNET_NAMESTORE_cancel (ns_qe);
791 if (NULL != ns_handle)
792 GNUNET_NAMESTORE_disconnect (ns_handle);
798 for (ego_entry = ego_head;
802 if (0 != GNUNET_CONTAINER_multihashmap_size (ego_tmp->attr_map))
804 GNUNET_CONTAINER_multihashmap_iterate (ego_tmp->attr_map,
809 GNUNET_CONTAINER_multihashmap_destroy (ego_tmp->attr_map);
810 ego_entry = ego_entry->next;
811 GNUNET_free (ego_tmp);
819 * @param tc task context
822 do_shutdown (void *cls,
823 const struct GNUNET_SCHEDULER_TaskContext *tc)
825 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
826 "Shutting down...\n");
831 static struct GNUNET_IDENTITY_PROVIDER_ExchangeResultMessage*
832 create_exchange_result_message (const char* token,
835 struct GNUNET_IDENTITY_PROVIDER_ExchangeResultMessage *erm;
836 uint16_t token_len = strlen (token) + 1;
837 erm = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_PROVIDER_ExchangeResultMessage)
839 erm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_PROVIDER_EXCHANGE_RESULT);
840 erm->header.size = htons (sizeof (struct GNUNET_IDENTITY_PROVIDER_ExchangeResultMessage)
842 memcpy (&erm[1], token, token_len);
847 static struct GNUNET_IDENTITY_PROVIDER_IssueResultMessage*
848 create_issue_result_message (const char* ticket)
850 struct GNUNET_IDENTITY_PROVIDER_IssueResultMessage *irm;
852 irm = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_PROVIDER_IssueResultMessage) + strlen(ticket) + 1);
853 irm->header.type = htons (GNUNET_MESSAGE_TYPE_IDENTITY_PROVIDER_ISSUE_RESULT);
854 irm->header.size = htons (sizeof (struct GNUNET_IDENTITY_PROVIDER_IssueResultMessage) + strlen (ticket) + 1);
855 memcpy (&irm[1], ticket, strlen (ticket) + 1);
860 store_token_issue_cont (void *cls,
864 struct IssueHandle *handle = cls;
865 struct GNUNET_IDENTITY_PROVIDER_IssueResultMessage *irm;
866 char* token_ticket_str;
867 handle->ns_qe = NULL;
868 if (GNUNET_SYSERR == success)
873 if (GNUNET_OK != ticket_serialize (handle->ticket,
877 GNUNET_CONTAINER_multihashmap_destroy (handle->attr_map);
878 ticket_destroy (handle->ticket);
879 GNUNET_free (handle);
880 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
883 irm = create_issue_result_message (token_ticket_str);
884 GNUNET_SERVER_notification_context_unicast (nc,
889 GNUNET_free (token_ticket_str);
890 GNUNET_SERVER_receive_done (handle->client, GNUNET_OK);
894 * Build a GNUid token for identity
895 * @param handle the handle
896 * @param ego_entry the ego to build the token for
897 * @param name name of the ego
898 * @param token_aud token audience
899 * @param token the resulting gnuid token
900 * @return identifier string of token (label)
903 sign_and_return_token (void *cls,
904 const struct GNUNET_SCHEDULER_TaskContext *tc)
906 const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key;
907 struct GNUNET_CRYPTO_EcdsaPublicKey pub_key;
908 struct GNUNET_CRYPTO_EcdsaPublicKey aud_pkey;
909 struct GNUNET_CRYPTO_EcdhePrivateKey *ecdhe_privkey;
910 struct IssueHandle *handle = cls;
911 struct GNUNET_GNSRECORD_Data token_record[2];
912 struct GNUNET_TIME_Relative etime_rel;
916 char *token_metadata;
922 size_t token_metadata_len;
926 GNUNET_asprintf (&nonce_str, "%d", handle->nonce);
927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Request nonce: %s\n", nonce_str);
930 rnd_key = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG, UINT64_MAX);
931 GNUNET_STRINGS_base64_encode ((char*)&rnd_key, sizeof (uint64_t), &lbl_str);
932 GNUNET_CRYPTO_ecdsa_key_get_public (&handle->iss_key,
935 handle->ticket = ticket_create (nonce_str,
943 GNUNET_STRINGS_fancy_time_to_relative ("1d", //TODO
946 ticket_destroy (handle->ticket);
947 GNUNET_free (handle);
948 GNUNET_SCHEDULER_add_now (&do_shutdown, handle);
951 time = GNUNET_TIME_absolute_get().abs_value_us;
952 exp_time = time + etime_rel.rel_value_us;
954 token_add_json (handle->token, "nbf", json_integer (time));
955 token_add_json (handle->token, "iat", json_integer (time));
956 token_add_json (handle->token, "exp", json_integer (exp_time));
957 token_add_attr (handle->token, "nonce", nonce_str);
960 //Token in a serialized encrypted format
961 GNUNET_assert (token_serialize (handle->token,
966 //Token record E,E_K (Token)
967 token_record[0].data = enc_token_str;
968 token_record[0].data_size = strlen (enc_token_str) + 1;
969 token_record[0].expiration_time = exp_time;
970 token_record[0].record_type = GNUNET_GNSRECORD_TYPE_ID_TOKEN;
971 token_record[0].flags = GNUNET_GNSRECORD_RF_NONE;
974 token_metadata_len = sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey)
975 + sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)
976 + strlen (handle->scopes) + 1; //With 0-Terminator
977 token_metadata = GNUNET_malloc (token_metadata_len);
978 write_ptr = token_metadata;
979 memcpy (token_metadata, ecdhe_privkey, sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey));
980 write_ptr += sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey);
981 memcpy (write_ptr, &handle->aud_key, sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
982 write_ptr += sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
983 memcpy (write_ptr, scopes, strlen (scopes) + 1); //with 0-Terminator;
985 GNUNET_free (ecdhe_privkey);
987 token_record[1].data = token_metadata;
988 token_record[1].data_size = token_metadata_len;
989 token_record[1].expiration_time = exp_time;
990 token_record[1].record_type = GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA;
991 token_record[1].flags = GNUNET_GNSRECORD_RF_PRIVATE;
994 handle->ns_qe = GNUNET_NAMESTORE_records_store (ns_handle,
999 &store_token_issue_cont,
1001 GNUNET_free (lbl_str);
1002 GNUNET_free (enc_token_str);
1006 * Collect attributes for token
1009 attr_collect (void *cls,
1010 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
1012 unsigned int rd_count,
1013 const struct GNUNET_GNSRECORD_Data *rd)
1018 struct IssueHandle *handle = cls;
1019 struct GNUNET_HashCode key;
1023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding attribute END: \n");
1024 handle->ns_it = NULL;
1025 GNUNET_SCHEDULER_add_now (&sign_and_return_token, handle);
1029 GNUNET_CRYPTO_hash (label,
1033 if (0 == rd_count ||
1034 ( (NULL != handle->attr_map) &&
1035 (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains (handle->attr_map,
1040 GNUNET_NAMESTORE_zone_iterator_next (handle->ns_it);
1044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding attribute: %s\n", label);
1048 if (rd->record_type == GNUNET_GNSRECORD_TYPE_ID_ATTR)
1050 data = GNUNET_GNSRECORD_value_to_string (rd->record_type,
1053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding value: %s\n", data);
1054 token_add_json (handle->token,
1056 json_string (data));
1059 GNUNET_NAMESTORE_zone_iterator_next (handle->ns_it);
1064 attr_arr = json_array();
1065 for (; i < rd_count; i++)
1067 if (rd->record_type == GNUNET_GNSRECORD_TYPE_ID_ATTR)
1069 data = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
1072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding value: %s\n", data);
1073 json_array_append_new (attr_arr, json_string (data));
1078 if (0 < json_array_size (attr_arr))
1080 token_add_json (handle->token, label, attr_arr);
1082 json_decref (attr_arr);
1083 GNUNET_NAMESTORE_zone_iterator_next (handle->ns_it);
1087 process_lookup_result (void *cls, uint32_t rd_count,
1088 const struct GNUNET_GNSRECORD_Data *rd)
1090 struct ExchangeHandle *handle = cls;
1091 struct GNUNET_IDENTITY_PROVIDER_ExchangeResultMessage *erm;
1095 handle->lookup_request = NULL;
1098 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1099 "Number of tokens %d != 2.",
1101 GNUNET_free (handle->label);
1102 GNUNET_free (handle);
1103 GNUNET_SCHEDULER_add_now (&do_shutdown, handle);
1108 GNUNET_GNSRECORD_value_to_string (GNUNET_GNSRECORD_TYPE_ID_TOKEN,
1113 GNUNET_assert (GNUNET_OK == token_parse (record_str,
1114 &handle->aud_privkey,
1118 GNUNET_assert (GNUNET_OK == token_to_string (handle->token,
1119 &handle->aud_privkey,
1122 erm = create_exchange_result_message (token_str,
1124 GNUNET_SERVER_notification_context_unicast (nc,
1129 GNUNET_SERVER_receive_done (handle->client, GNUNET_OK);
1135 * Handler for exchange message
1138 * @param client who sent the message
1139 * @param message the message
1142 handle_exchange_message (void *cls,
1143 struct GNUNET_SERVER_Client *client,
1144 const struct GNUNET_MessageHeader *message)
1146 const struct GNUNET_IDENTITY_PROVIDER_ExchangeMessage *em;
1147 struct ExchangeHandle *xchange_handle;
1152 size = ntohs (message->size);
1153 if (size <= sizeof (struct GNUNET_IDENTITY_PROVIDER_ExchangeMessage))
1156 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1159 em = (const struct GNUNET_IDENTITY_PROVIDER_ExchangeMessage *) message;
1160 ticket = (const char *) &em[1];
1161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1162 "Received EXCHANGE of `%s' from client\n",
1164 xchange_handle = GNUNET_malloc (sizeof (struct ExchangeHandle));
1165 xchange_handle->aud_privkey = em->aud_privkey;
1166 if (GNUNET_SYSERR == ticket_parse (ticket,
1167 &xchange_handle->aud_privkey,
1168 &xchange_handle->ticket))
1170 GNUNET_free (xchange_handle);
1172 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1175 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Looking for token under %s\n",
1176 xchange_handle->ticket->payload->label);
1177 GNUNET_asprintf (&lookup_query,
1179 xchange_handle->ticket->payload->label);
1180 xchange_handle->lookup_request = GNUNET_GNS_lookup (gns_handle,
1182 &xchange_handle->ticket->payload->identity_key,
1183 GNUNET_GNSRECORD_TYPE_ID_TOKEN,
1184 GNUNET_GNS_LO_LOCAL_MASTER,
1186 &process_lookup_result,
1188 GNUNET_free (lookup_query);
1194 * Handler for issue message
1197 * @param client who sent the message
1198 * @param message the message
1201 handle_issue_message (void *cls,
1202 struct GNUNET_SERVER_Client *client,
1203 const struct GNUNET_MessageHeader *message)
1205 const struct GNUNET_IDENTITY_PROVIDER_IssueMessage *im;
1210 char *attr_list_tmp;
1211 struct GNUNET_HashCode key;
1212 struct IssueHandle *issue_handle;
1214 size = ntohs (message->size);
1215 if (size <= sizeof (struct GNUNET_IDENTITY_PROVIDER_IssueMessage))
1218 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1221 im = (const struct GNUNET_IDENTITY_PROVIDER_IssueMessage *) message;
1222 scopes = (const char *) &im[1];
1223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1224 "Received ISSUE of `%s' from client\n",
1226 issue_handle = GNUNET_malloc (sizeof (struct IssueHandle));
1227 issue_handle->attr_map = GNUNET_CONTAINER_multihashmap_create (5,
1229 scopes_tmp = GNUNET_strdup (scopes);
1230 scope = strtok(attr_list_tmp, ",");
1231 for (; NULL != scope; scope = strtok (NULL, ","))
1233 GNUNET_CRYPTO_hash (scope,
1236 GNUNET_CONTAINER_multihashmap_put (issue_handle->attr_map,
1239 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1241 GNUNET_free (scopes_tmp);
1243 issue_handle->aud_key = im->aud_key;
1244 issue_handle->iss_key = im->iss_key;
1245 issue_handle->expiration = GNUNET_TIME_absolute_ntoh (im->expiration);
1246 issue_handle->nonce = im->nonce;
1247 GNUNET_CRYPTO_ecdsa_key_get_public (&im->iss_key,
1248 &issue_handle->iss_pkey);
1249 issue_handle->token = token_create (&issue_handle->iss_pkey,
1252 issue_handle->ns_it = GNUNET_NAMESTORE_zone_iteration_start (ns_handle,
1256 GNUNET_SERVER_receive_done (client, GNUNET_OK); //TODO here?
1262 * Main function that will be run
1264 * @param cls closure
1265 * @param args remaining command-line arguments
1266 * @param cfgfile name of the configuration file used (for saving, can be NULL)
1267 * @param c configuration
1271 struct GNUNET_SERVER_Handle *server,
1272 const struct GNUNET_CONFIGURATION_Handle *c)
1274 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1275 {&handle_issue_message, NULL,
1276 GNUNET_MESSAGE_TYPE_IDENTITY_PROVIDER_ISSUE, 0},
1277 {&handle_exchange_message, NULL,
1278 GNUNET_MESSAGE_TYPE_IDENTITY_PROVIDER_EXCHANGE, 0},
1284 stats = GNUNET_STATISTICS_create ("identity-provider", cfg);
1285 GNUNET_SERVER_add_handlers (server, handlers);
1286 nc = GNUNET_SERVER_notification_context_create (server, 1);
1288 //Connect to identity and namestore services
1289 ns_handle = GNUNET_NAMESTORE_connect (cfg);
1290 if (NULL == ns_handle)
1292 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "error connecting to namestore");
1295 gns_handle = GNUNET_GNS_connect (cfg);
1296 if (NULL == gns_handle)
1298 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "error connecting to gns");
1301 identity_handle = GNUNET_IDENTITY_connect (cfg,
1305 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1306 &do_shutdown, NULL);
1314 * @param argc number of arguments from the cli
1315 * @param argv command line arguments
1316 * @return 0 ok, 1 on error
1320 main (int argc, char *const *argv)
1322 return (GNUNET_OK ==
1323 GNUNET_SERVICE_run (argc, argv, "identity-provider",
1324 GNUNET_SERVICE_OPTION_NONE,
1325 &run, NULL)) ? 0 : 1;
1328 /* end of gnunet-rest-server.c */