2 This file is part of GNUnet.
3 Copyright (C) 2011-2013 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 * @file credential/gnunet-service-credential.c
22 * @brief GNUnet Credential Service (main service)
23 * @author Martin Schanzenbach
26 #include "gnunet_util_lib.h"
27 #include "gnunet_credential_service.h"
28 #include "gnunet_statistics_service.h"
29 #include "credential.h"
30 #include "credential_serialization.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_signatures.h"
34 #include <gnunet_dnsparser_lib.h>
35 #include <gnunet_identity_service.h>
36 #include <gnunet_gnsrecord_lib.h>
37 #include <gnunet_namestore_service.h>
38 #include <gnunet_gns_service.h>
41 #define GNUNET_CREDENTIAL_MAX_LENGTH 255
43 struct VerifyRequestHandle;
45 struct DelegationSetQueueEntry;
48 struct DelegationChainEntry
53 struct DelegationChainEntry *next;
58 struct DelegationChainEntry *prev;
63 struct GNUNET_CRYPTO_EcdsaPublicKey issuer_key;
68 struct GNUNET_CRYPTO_EcdsaPublicKey subject_key;
71 * The issued attribute
73 char *issuer_attribute;
76 * The delegated attribute
78 char *subject_attribute;
84 struct CredentialRecordEntry
89 struct CredentialRecordEntry *next;
94 struct CredentialRecordEntry *prev;
97 * Number of references in delegation chains
104 struct GNUNET_CREDENTIAL_Credential *credential;
108 * DLL used for delegations
109 * Used for OR delegations
111 struct DelegationQueueEntry
116 struct DelegationQueueEntry *next;
121 struct DelegationQueueEntry *prev;
124 * Sets under this Queue
126 struct DelegationSetQueueEntry *set_entries_head;
129 * Sets under this Queue
131 struct DelegationSetQueueEntry *set_entries_tail;
136 struct DelegationSetQueueEntry *parent_set;
141 uint32_t required_solutions;
145 * DLL for delegation sets
146 * Used for AND delegation set
148 struct DelegationSetQueueEntry
153 struct DelegationSetQueueEntry *next;
158 struct DelegationSetQueueEntry *prev;
163 struct GNUNET_GNS_LookupRequest *lookup_request;
168 struct VerifyRequestHandle *handle;
171 * Parent attribute delegation
173 struct DelegationQueueEntry *parent;
178 struct GNUNET_CRYPTO_EcdsaPublicKey *issuer_key;
181 * Queue entries of this set
183 struct DelegationQueueEntry *queue_entries_head;
186 * Queue entries of this set
188 struct DelegationQueueEntry *queue_entries_tail;
193 struct DelegationQueueEntry *parent_queue_entry;
196 * Issuer attribute delegated to
198 char *issuer_attribute;
201 * The current attribute to look up
203 char *lookup_attribute;
206 * Trailing attribute context
211 * Still to resolve delegation as string
213 char *unresolved_attribute_delegation;
216 * The delegation chain entry
218 struct DelegationChainEntry *delegation_chain_entry;
224 * Handle to a lookup operation from api
226 struct VerifyRequestHandle
230 * We keep these in a DLL.
232 struct VerifyRequestHandle *next;
235 * We keep these in a DLL.
237 struct VerifyRequestHandle *prev;
240 * Handle to the requesting client
242 struct GNUNET_SERVICE_Client *client;
247 struct GNUNET_GNS_LookupRequest *lookup_request;
250 * Size of delegation tree
252 uint32_t delegation_chain_size;
255 * Children of this attribute
257 struct DelegationChainEntry *delegation_chain_head;
260 * Children of this attribute
262 struct DelegationChainEntry *delegation_chain_tail;
267 struct GNUNET_CRYPTO_EcdsaPublicKey issuer_key;
272 char *issuer_attribute;
277 struct GNUNET_CRYPTO_EcdsaPublicKey subject_key;
282 struct CredentialRecordEntry *cred_chain_head;
287 struct CredentialRecordEntry *cred_chain_tail;
290 * Credential DLL size
292 uint32_t cred_chain_size;
295 * Root Delegation Set
297 struct DelegationSetQueueEntry *root_set;
300 * Current Delegation Pointer
302 struct DelegationQueueEntry *current_delegation;
312 uint64_t pending_lookups;
315 * Credential iterator
317 struct GNUNET_NAMESTORE_ZoneIterator *cred_collection_iter;
322 struct GNUNET_SCHEDULER_Task *collect_next_task;
330 static struct VerifyRequestHandle *vrh_head;
335 static struct VerifyRequestHandle *vrh_tail;
338 * Handle to the statistics service
340 static struct GNUNET_STATISTICS_Handle *statistics;
343 * Handle to GNS service.
345 static struct GNUNET_GNS_Handle *gns;
349 * Handle to namestore service
351 static struct GNUNET_NAMESTORE_Handle *namestore;
354 cleanup_delegation_set (struct DelegationSetQueueEntry *ds_entry)
356 struct DelegationQueueEntry *dq_entry;
357 struct DelegationSetQueueEntry *child;
359 if (NULL == ds_entry)
362 for (dq_entry = ds_entry->queue_entries_head;
364 dq_entry = ds_entry->queue_entries_head)
366 GNUNET_CONTAINER_DLL_remove (ds_entry->queue_entries_head,
367 ds_entry->queue_entries_tail,
369 for (child = dq_entry->set_entries_head;
371 child = dq_entry->set_entries_head)
373 GNUNET_CONTAINER_DLL_remove (dq_entry->set_entries_head,
374 dq_entry->set_entries_tail,
376 cleanup_delegation_set (child);
378 GNUNET_free (dq_entry);
380 GNUNET_free_non_null (ds_entry->issuer_key);
381 GNUNET_free_non_null (ds_entry->lookup_attribute);
382 GNUNET_free_non_null (ds_entry->issuer_attribute);
383 GNUNET_free_non_null (ds_entry->unresolved_attribute_delegation);
384 GNUNET_free_non_null (ds_entry->attr_trailer);
385 if (NULL != ds_entry->lookup_request)
387 GNUNET_GNS_lookup_cancel (ds_entry->lookup_request);
388 ds_entry->lookup_request = NULL;
390 if (NULL != ds_entry->delegation_chain_entry)
392 GNUNET_free_non_null (ds_entry->delegation_chain_entry->subject_attribute);
393 GNUNET_free_non_null (ds_entry->delegation_chain_entry->issuer_attribute);
394 GNUNET_free (ds_entry->delegation_chain_entry);
396 GNUNET_free (ds_entry);
400 cleanup_handle (struct VerifyRequestHandle *vrh)
402 struct CredentialRecordEntry *cr_entry;
403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
405 if (NULL != vrh->lookup_request)
407 GNUNET_GNS_lookup_cancel (vrh->lookup_request);
408 vrh->lookup_request = NULL;
410 cleanup_delegation_set (vrh->root_set);
411 GNUNET_free_non_null (vrh->issuer_attribute);
412 for (cr_entry = vrh->cred_chain_head;
413 NULL != vrh->cred_chain_head;
414 cr_entry = vrh->cred_chain_head)
416 GNUNET_CONTAINER_DLL_remove (vrh->cred_chain_head,
417 vrh->cred_chain_tail,
419 GNUNET_free_non_null (cr_entry->credential);
420 GNUNET_free (cr_entry);
426 shutdown_task (void *cls)
428 struct VerifyRequestHandle *vrh;
430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
433 while (NULL != (vrh = vrh_head))
435 //CREDENTIAL_resolver_lookup_cancel (clh->lookup);
436 GNUNET_CONTAINER_DLL_remove (vrh_head,
439 cleanup_handle (vrh);
444 GNUNET_GNS_disconnect (gns);
447 if (NULL != namestore)
449 GNUNET_NAMESTORE_disconnect (namestore);
452 if (NULL != statistics)
454 GNUNET_STATISTICS_destroy (statistics,
464 send_lookup_response (struct VerifyRequestHandle *vrh)
466 struct GNUNET_MQ_Envelope *env;
467 struct DelegationChainResultMessage *rmsg;
468 struct DelegationChainEntry *dce;
469 struct GNUNET_CREDENTIAL_Delegation dd[vrh->delegation_chain_size];
470 struct GNUNET_CREDENTIAL_Credential cred[vrh->cred_chain_size];
471 struct CredentialRecordEntry *cd;
472 struct CredentialRecordEntry *tmp;
475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
476 "Sending response\n");
477 dce = vrh->delegation_chain_head;
478 for (uint32_t i=0;i<vrh->delegation_chain_size;i++)
480 dd[i].issuer_key = dce->issuer_key;
481 dd[i].subject_key = dce->subject_key;
482 dd[i].issuer_attribute = dce->issuer_attribute;
483 dd[i].issuer_attribute_len = strlen (dce->issuer_attribute)+1;
484 dd[i].subject_attribute_len = 0;
485 dd[i].subject_attribute = NULL;
486 if (NULL != dce->subject_attribute)
488 dd[i].subject_attribute = dce->subject_attribute;
489 dd[i].subject_attribute_len = strlen(dce->subject_attribute)+1;
495 * Remove all credentials not needed
497 for (cd = vrh->cred_chain_head; NULL != cd;)
499 if (cd->refcount > 0)
506 GNUNET_CONTAINER_DLL_remove (vrh->cred_chain_head,
507 vrh->cred_chain_tail,
509 GNUNET_free (tmp->credential);
511 vrh->cred_chain_size--;
515 * Get serialized record data
516 * Append at the end of rmsg
518 cd = vrh->cred_chain_head;
519 for (uint32_t i=0;i<vrh->cred_chain_size;i++)
521 cred[i].issuer_key = cd->credential->issuer_key;
522 cred[i].subject_key = cd->credential->subject_key;
523 cred[i].issuer_attribute_len = strlen(cd->credential->issuer_attribute)+1;
524 cred[i].issuer_attribute = cd->credential->issuer_attribute;
525 cred[i].expiration = cd->credential->expiration;
526 cred[i].signature = cd->credential->signature;
529 size = GNUNET_CREDENTIAL_delegation_chain_get_size (vrh->delegation_chain_size,
531 vrh->cred_chain_size,
533 env = GNUNET_MQ_msg_extra (rmsg,
535 GNUNET_MESSAGE_TYPE_CREDENTIAL_VERIFY_RESULT);
536 //Assign id so that client can find associated request
537 rmsg->id = vrh->request_id;
538 rmsg->d_count = htonl (vrh->delegation_chain_size);
539 rmsg->c_count = htonl (vrh->cred_chain_size);
541 if (0 < vrh->cred_chain_size)
542 rmsg->cred_found = htonl (GNUNET_YES);
544 rmsg->cred_found = htonl (GNUNET_NO);
547 GNUNET_CREDENTIAL_delegation_chain_serialize (vrh->delegation_chain_size,
549 vrh->cred_chain_size,
554 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq(vrh->client),
556 GNUNET_CONTAINER_DLL_remove (vrh_head, vrh_tail, vrh);
559 GNUNET_STATISTICS_update (statistics,
560 "Completed verifications", 1,
566 backward_resolution (void* cls,
568 const struct GNUNET_GNSRECORD_Data *rd)
571 struct VerifyRequestHandle *vrh;
572 const struct GNUNET_CREDENTIAL_DelegationRecord *sets;
573 struct CredentialRecordEntry *cred_pointer;
574 struct DelegationSetQueueEntry *current_set;
575 struct DelegationSetQueueEntry *ds_entry;
576 struct DelegationSetQueueEntry *tmp_set;
577 struct DelegationQueueEntry *dq_entry;
579 char *lookup_attribute;
583 current_set->lookup_request = NULL;
584 vrh = current_set->handle;
585 vrh->pending_lookups--;
586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
587 "Got %d attrs\n", rd_count);
590 for (uint32_t i=0; i < rd_count; i++)
592 if (GNUNET_GNSRECORD_TYPE_ATTRIBUTE != rd[i].record_type)
596 struct GNUNET_CREDENTIAL_DelegationSet set[ntohl(sets->set_count)];
597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
598 "Found new attribute delegation with %d sets. Creating new Job...\n",
599 ntohl (sets->set_count));
601 if (GNUNET_OK !=GNUNET_CREDENTIAL_delegation_set_deserialize (GNUNET_ntohll(sets->data_size),
602 (const char*)&sets[1],
603 ntohl(sets->set_count),
606 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
607 "Failed to deserialize!\n");
610 dq_entry = GNUNET_new (struct DelegationQueueEntry);
611 dq_entry->required_solutions = ntohl(sets->set_count);
612 dq_entry->parent_set = current_set;
613 GNUNET_CONTAINER_DLL_insert (current_set->queue_entries_head,
614 current_set->queue_entries_tail,
617 for (uint32_t j=0; j<ntohl(sets->set_count); j++)
619 ds_entry = GNUNET_new (struct DelegationSetQueueEntry);
620 if (NULL != current_set->attr_trailer)
622 if (0 == set[j].subject_attribute_len)
624 GNUNET_asprintf (&expanded_attr,
626 current_set->attr_trailer);
629 GNUNET_asprintf (&expanded_attr,
631 set[j].subject_attribute,
632 current_set->attr_trailer);
634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
635 "Expanded to %s\n", expanded_attr);
636 ds_entry->unresolved_attribute_delegation = expanded_attr;
638 if (0 != set[j].subject_attribute_len)
640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
641 "Not Expanding %s\n", set[j].subject_attribute);
642 ds_entry->unresolved_attribute_delegation = GNUNET_strdup (set[j].subject_attribute);
646 //Add a credential chain entry
647 ds_entry->delegation_chain_entry = GNUNET_new (struct DelegationChainEntry);
648 ds_entry->delegation_chain_entry->subject_key = set[j].subject_key;
649 ds_entry->issuer_key = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPublicKey);
650 GNUNET_memcpy (ds_entry->issuer_key,
652 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
653 if (0 < set[j].subject_attribute_len)
654 ds_entry->delegation_chain_entry->subject_attribute = GNUNET_strdup (set[j].subject_attribute);
655 ds_entry->delegation_chain_entry->issuer_key = *current_set->issuer_key;
656 ds_entry->delegation_chain_entry->issuer_attribute = GNUNET_strdup (current_set->lookup_attribute);
658 ds_entry->parent_queue_entry = dq_entry; //current_delegation;
659 GNUNET_CONTAINER_DLL_insert (dq_entry->set_entries_head,
660 dq_entry->set_entries_tail,
663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
664 "Checking for cred match\n");
666 * Check if this delegation already matches one of our credentials
668 for(cred_pointer = vrh->cred_chain_head; cred_pointer != NULL;
669 cred_pointer = cred_pointer->next)
671 if(0 != memcmp (&set->subject_key,
672 &cred_pointer->credential->issuer_key,
673 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey)))
675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
676 "Checking if %s matches %s\n",
677 ds_entry->unresolved_attribute_delegation,
678 cred_pointer->credential->issuer_attribute);
680 if (0 != strcmp (ds_entry->unresolved_attribute_delegation,
681 cred_pointer->credential->issuer_attribute))
684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
686 cred_pointer->refcount++;
688 for (tmp_set = ds_entry;
689 NULL != tmp_set->parent_queue_entry;
690 tmp_set = tmp_set->parent_queue_entry->parent_set)
692 tmp_set->parent_queue_entry->required_solutions--;
693 if (NULL != tmp_set->delegation_chain_entry)
695 vrh->delegation_chain_size++;
696 GNUNET_CONTAINER_DLL_insert (vrh->delegation_chain_head,
697 vrh->delegation_chain_tail,
698 tmp_set->delegation_chain_entry);
700 if (0 < tmp_set->parent_queue_entry->required_solutions)
704 if (NULL == tmp_set->parent_queue_entry)
706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
707 "All solutions found\n");
709 send_lookup_response (vrh);
712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
713 "Not all solutions found yet.\n");
717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
718 "Building new lookup request from %s\n",
719 ds_entry->unresolved_attribute_delegation);
720 //Continue with backward resolution
721 char issuer_attribute_name[strlen (ds_entry->unresolved_attribute_delegation)+1];
722 strcpy (issuer_attribute_name,
723 ds_entry->unresolved_attribute_delegation);
724 char *next_attr = strtok (issuer_attribute_name, ".");
725 GNUNET_asprintf (&lookup_attribute,
728 GNUNET_asprintf (&ds_entry->lookup_attribute,
731 if (strlen (next_attr) == strlen (ds_entry->unresolved_attribute_delegation))
733 ds_entry->attr_trailer = NULL;
735 next_attr += strlen (next_attr) + 1;
736 ds_entry->attr_trailer = GNUNET_strdup (next_attr);
739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
740 "Looking up %s\n", ds_entry->lookup_attribute);
741 if (NULL != ds_entry->attr_trailer)
742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
743 "%s still to go...\n", ds_entry->attr_trailer);
745 vrh->pending_lookups++;
746 ds_entry->handle = vrh;
747 ds_entry->lookup_request = GNUNET_GNS_lookup (gns,
749 ds_entry->issuer_key, //issuer_key,
750 GNUNET_GNSRECORD_TYPE_ATTRIBUTE,
751 GNUNET_GNS_LO_DEFAULT,
752 &backward_resolution,
754 GNUNET_free (lookup_attribute);
758 if(0 == vrh->pending_lookups)
760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
761 "We are all out of attributes...\n");
762 send_lookup_response (vrh);
770 * Result from GNS lookup.
772 * @param cls the closure (our client lookup handle)
775 delegation_chain_resolution_start (void* cls)
777 struct VerifyRequestHandle *vrh = cls;
778 struct DelegationSetQueueEntry *ds_entry;
779 struct CredentialRecordEntry *cr_entry;
780 vrh->lookup_request = NULL;
782 if (0 == vrh->cred_chain_size)
784 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
785 "No credentials found\n");
786 send_lookup_response (vrh);
790 for (cr_entry = vrh->cred_chain_head; cr_entry != NULL; cr_entry = cr_entry->next)
792 if (0 != memcmp (&cr_entry->credential->issuer_key,
794 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
796 if (0 != strcmp (cr_entry->credential->issuer_attribute, vrh->issuer_attribute))
798 cr_entry->refcount++;
799 //Found match prematurely
800 send_lookup_response (vrh);
806 * Check for attributes from the issuer and follow the chain
807 * till you get the required subject's attributes
809 char issuer_attribute_name[strlen (vrh->issuer_attribute)];
810 strcpy (issuer_attribute_name,
811 vrh->issuer_attribute);
812 strcpy (issuer_attribute_name + strlen (vrh->issuer_attribute),
814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
815 "Looking up %s\n", issuer_attribute_name);
816 ds_entry = GNUNET_new (struct DelegationSetQueueEntry);
817 ds_entry->issuer_key = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPublicKey);
818 memcpy (ds_entry->issuer_key,
820 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
821 ds_entry->issuer_attribute = GNUNET_strdup (vrh->issuer_attribute);
822 ds_entry->handle = vrh;
823 ds_entry->lookup_attribute = GNUNET_strdup (vrh->issuer_attribute);
824 vrh->root_set = ds_entry;
825 vrh->pending_lookups = 1;
826 //Start with backward resolution
827 ds_entry->lookup_request = GNUNET_GNS_lookup (gns,
828 issuer_attribute_name,
829 &vrh->issuer_key, //issuer_key,
830 GNUNET_GNSRECORD_TYPE_ATTRIBUTE,
831 GNUNET_GNS_LO_DEFAULT,
832 &backward_resolution,
837 check_verify (void *cls,
838 const struct VerifyMessage *v_msg)
843 msg_size = ntohs (v_msg->header.size);
844 if (msg_size < sizeof (struct VerifyMessage))
847 return GNUNET_SYSERR;
849 if (ntohs (v_msg->issuer_attribute_len) > GNUNET_CREDENTIAL_MAX_LENGTH)
852 return GNUNET_SYSERR;
854 attr = (const char *) &v_msg[1];
856 if ( strlen (attr) > GNUNET_CREDENTIAL_MAX_LENGTH)
859 return GNUNET_SYSERR;
865 handle_verify (void *cls,
866 const struct VerifyMessage *v_msg)
868 struct VerifyRequestHandle *vrh;
869 struct GNUNET_SERVICE_Client *client = cls;
870 struct CredentialRecordEntry *cr_entry;
871 uint32_t credentials_count;
872 uint32_t credential_data_size;
873 char attr[GNUNET_CREDENTIAL_MAX_LENGTH + 1];
874 char issuer_attribute[GNUNET_CREDENTIAL_MAX_LENGTH + 1];
875 char *attrptr = attr;
876 char *credential_data;
879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
880 "Received VERIFY message\n");
881 utf_in = (const char *) &v_msg[1];
882 GNUNET_STRINGS_utf8_tolower (utf_in, attrptr);
883 GNUNET_memcpy (issuer_attribute, attr, ntohs (v_msg->issuer_attribute_len));
884 issuer_attribute[ntohs (v_msg->issuer_attribute_len)] = '\0';
885 vrh = GNUNET_new (struct VerifyRequestHandle);
886 GNUNET_CONTAINER_DLL_insert (vrh_head, vrh_tail, vrh);
887 vrh->client = client;
888 vrh->request_id = v_msg->id;
889 vrh->issuer_key = v_msg->issuer_key;
890 vrh->subject_key = v_msg->subject_key;
891 vrh->issuer_attribute = GNUNET_strdup (issuer_attribute);
892 if (NULL == issuer_attribute)
894 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
895 "No issuer attribute provided!\n");
896 send_lookup_response (vrh);
900 * First, collect credentials
903 credentials_count = ntohl(v_msg->c_count);
904 credential_data_size = ntohs (v_msg->header.size)
905 - sizeof (struct VerifyMessage)
906 - ntohs (v_msg->issuer_attribute_len)
908 struct GNUNET_CREDENTIAL_Credential credentials[credentials_count];
909 credential_data = (char*)&v_msg[1] + ntohs (v_msg->issuer_attribute_len) + 1;
910 if (GNUNET_OK != GNUNET_CREDENTIAL_credentials_deserialize (credential_data_size,
915 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
916 "Cannot deserialize credentials!\n");
917 send_lookup_response (vrh);
921 for (uint32_t i=0;i<credentials_count;i++) {
922 cr_entry = GNUNET_new (struct CredentialRecordEntry);
923 cr_entry->credential = GNUNET_malloc (sizeof (struct GNUNET_CREDENTIAL_Credential) +
924 credentials[i].issuer_attribute_len);
925 GNUNET_memcpy (cr_entry->credential,
927 sizeof (struct GNUNET_CREDENTIAL_Credential));
928 GNUNET_memcpy (&cr_entry->credential[1],
929 credentials[i].issuer_attribute,
930 credentials[i].issuer_attribute_len);
931 cr_entry->credential->issuer_attribute = (char*)&cr_entry->credential[1];
932 GNUNET_CONTAINER_DLL_insert_tail (vrh->cred_chain_head,
933 vrh->cred_chain_tail,
935 vrh->cred_chain_size++;
938 delegation_chain_resolution_start (vrh);
943 handle_cred_collection_error_cb (void *cls)
945 struct VerifyRequestHandle *vrh = cls;
946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
947 "Got disconnected from namestore database.\n");
948 vrh->cred_collection_iter = NULL;
949 send_lookup_response (vrh);
953 collect_next (void *cls)
955 struct VerifyRequestHandle *vrh = cls;
956 vrh->collect_next_task = NULL;
957 GNUNET_assert (NULL != vrh->cred_collection_iter);
958 GNUNET_NAMESTORE_zone_iterator_next (vrh->cred_collection_iter);
962 handle_cred_collection_cb (void *cls,
963 const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
965 unsigned int rd_count,
966 const struct GNUNET_GNSRECORD_Data *rd)
968 struct VerifyRequestHandle *vrh = cls;
969 struct GNUNET_CREDENTIAL_Credential *crd;
970 struct CredentialRecordEntry *cr_entry;
971 int cred_record_count;
973 cred_record_count = 0;
974 for (uint32_t i=0; i < rd_count; i++)
976 if (GNUNET_GNSRECORD_TYPE_CREDENTIAL != rd[i].record_type)
979 crd = GNUNET_CREDENTIAL_credential_deserialize (rd[i].data,
983 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
984 "Invalid credential found\n");
987 cr_entry = GNUNET_new (struct CredentialRecordEntry);
988 cr_entry->credential = crd;
989 GNUNET_CONTAINER_DLL_insert_tail (vrh->cred_chain_head,
990 vrh->cred_chain_tail,
992 vrh->cred_chain_size++;
994 vrh->collect_next_task = GNUNET_SCHEDULER_add_now (&collect_next,
999 handle_cred_collection_finished_cb (void *cls)
1001 struct VerifyRequestHandle *vrh = cls;
1002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1003 "Done collecting credentials.\n");
1004 vrh->cred_collection_iter = NULL;
1005 delegation_chain_resolution_start (vrh);
1009 handle_collect (void *cls,
1010 const struct CollectMessage *c_msg)
1012 char attr[GNUNET_CREDENTIAL_MAX_LENGTH + 1];
1013 char issuer_attribute[GNUNET_CREDENTIAL_MAX_LENGTH + 1];
1014 struct VerifyRequestHandle *vrh;
1015 struct GNUNET_SERVICE_Client *client = cls;
1016 char *attrptr = attr;
1019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1020 "Received COLLECT message\n");
1022 utf_in = (const char *) &c_msg[1];
1023 GNUNET_STRINGS_utf8_tolower (utf_in, attrptr);
1025 GNUNET_memcpy (issuer_attribute, attr, ntohs (c_msg->issuer_attribute_len));
1026 issuer_attribute[ntohs (c_msg->issuer_attribute_len)] = '\0';
1027 vrh = GNUNET_new (struct VerifyRequestHandle);
1028 GNUNET_CONTAINER_DLL_insert (vrh_head, vrh_tail, vrh);
1029 vrh->client = client;
1030 vrh->request_id = c_msg->id;
1031 vrh->issuer_key = c_msg->issuer_key;
1032 GNUNET_CRYPTO_ecdsa_key_get_public (&c_msg->subject_key,
1034 vrh->issuer_attribute = GNUNET_strdup (issuer_attribute);
1036 if (NULL == issuer_attribute)
1038 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1039 "No issuer attribute provided!\n");
1040 send_lookup_response (vrh);
1043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1044 "Getting credentials for subject\n");
1046 * First, get attribute from subject
1048 vrh->cred_collection_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore,
1049 &c_msg->subject_key,
1050 &handle_cred_collection_error_cb,
1052 &handle_cred_collection_cb,
1054 &handle_cred_collection_finished_cb,
1060 check_collect (void *cls,
1061 const struct CollectMessage *c_msg)
1066 msg_size = ntohs (c_msg->header.size);
1067 if (msg_size < sizeof (struct CollectMessage))
1070 return GNUNET_SYSERR;
1072 if (ntohs (c_msg->issuer_attribute_len) > GNUNET_CREDENTIAL_MAX_LENGTH)
1075 return GNUNET_SYSERR;
1077 attr = (const char *) &c_msg[1];
1079 if ( ('\0' != attr[ntohs(c_msg->header.size) - sizeof (struct CollectMessage) - 1]) ||
1080 (strlen (attr) > GNUNET_CREDENTIAL_MAX_LENGTH) )
1083 return GNUNET_SYSERR;
1089 client_disconnect_cb (void *cls,
1090 struct GNUNET_SERVICE_Client *client,
1093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1094 "Client %p disconnected\n",
1099 client_connect_cb (void *cls,
1100 struct GNUNET_SERVICE_Client *client,
1101 struct GNUNET_MQ_Handle *mq)
1103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1104 "Client %p connected\n",
1110 * Process Credential requests.
1112 * @param cls closure
1113 * @param c configuration to use
1114 * @param handle service handle
1118 const struct GNUNET_CONFIGURATION_Handle *c,
1119 struct GNUNET_SERVICE_Handle *handle)
1122 gns = GNUNET_GNS_connect (c);
1126 _("Failed to connect to GNS\n"));
1128 namestore = GNUNET_NAMESTORE_connect (c);
1129 if (NULL == namestore)
1132 _("Failed to connect to namestore\n"));
1135 statistics = GNUNET_STATISTICS_create ("credential", c);
1136 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
1141 * Define "main" method using service macro
1145 GNUNET_SERVICE_OPTION_NONE,
1148 &client_disconnect_cb,
1150 GNUNET_MQ_hd_var_size (verify,
1151 GNUNET_MESSAGE_TYPE_CREDENTIAL_VERIFY,
1152 struct VerifyMessage,
1154 GNUNET_MQ_hd_var_size (collect,
1155 GNUNET_MESSAGE_TYPE_CREDENTIAL_COLLECT,
1156 struct CollectMessage,
1158 GNUNET_MQ_handler_end());
1160 /* end of gnunet-service-credential.c */