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 if (NULL == next_attr)
727 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
728 "Failed to parse next attribute\n");
731 GNUNET_asprintf (&lookup_attribute,
734 GNUNET_asprintf (&ds_entry->lookup_attribute,
737 if (strlen (next_attr) == strlen (ds_entry->unresolved_attribute_delegation))
739 ds_entry->attr_trailer = NULL;
741 next_attr += strlen (next_attr) + 1;
742 ds_entry->attr_trailer = GNUNET_strdup (next_attr);
745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
746 "Looking up %s\n", ds_entry->lookup_attribute);
747 if (NULL != ds_entry->attr_trailer)
748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
749 "%s still to go...\n", ds_entry->attr_trailer);
751 vrh->pending_lookups++;
752 ds_entry->handle = vrh;
753 ds_entry->lookup_request = GNUNET_GNS_lookup (gns,
755 ds_entry->issuer_key, //issuer_key,
756 GNUNET_GNSRECORD_TYPE_ATTRIBUTE,
757 GNUNET_GNS_LO_DEFAULT,
758 &backward_resolution,
760 GNUNET_free (lookup_attribute);
764 if(0 == vrh->pending_lookups)
766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
767 "We are all out of attributes...\n");
768 send_lookup_response (vrh);
776 * Result from GNS lookup.
778 * @param cls the closure (our client lookup handle)
781 delegation_chain_resolution_start (void* cls)
783 struct VerifyRequestHandle *vrh = cls;
784 struct DelegationSetQueueEntry *ds_entry;
785 struct CredentialRecordEntry *cr_entry;
786 vrh->lookup_request = NULL;
788 if (0 == vrh->cred_chain_size)
790 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
791 "No credentials found\n");
792 send_lookup_response (vrh);
796 for (cr_entry = vrh->cred_chain_head; cr_entry != NULL; cr_entry = cr_entry->next)
798 if (0 != memcmp (&cr_entry->credential->issuer_key,
800 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
802 if (0 != strcmp (cr_entry->credential->issuer_attribute, vrh->issuer_attribute))
804 cr_entry->refcount++;
805 //Found match prematurely
806 send_lookup_response (vrh);
812 * Check for attributes from the issuer and follow the chain
813 * till you get the required subject's attributes
815 char issuer_attribute_name[strlen (vrh->issuer_attribute) + strlen (".gnu") + 1];
816 strcpy (issuer_attribute_name,
817 vrh->issuer_attribute);
818 strcpy (issuer_attribute_name + strlen (vrh->issuer_attribute),
820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
821 "Looking up %s\n", issuer_attribute_name);
822 ds_entry = GNUNET_new (struct DelegationSetQueueEntry);
823 ds_entry->issuer_key = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPublicKey);
824 GNUNET_memcpy (ds_entry->issuer_key,
826 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
827 ds_entry->issuer_attribute = GNUNET_strdup (vrh->issuer_attribute);
828 ds_entry->handle = vrh;
829 ds_entry->lookup_attribute = GNUNET_strdup (vrh->issuer_attribute);
830 vrh->root_set = ds_entry;
831 vrh->pending_lookups = 1;
832 //Start with backward resolution
833 ds_entry->lookup_request = GNUNET_GNS_lookup (gns,
834 issuer_attribute_name,
835 &vrh->issuer_key, //issuer_key,
836 GNUNET_GNSRECORD_TYPE_ATTRIBUTE,
837 GNUNET_GNS_LO_DEFAULT,
838 &backward_resolution,
843 check_verify (void *cls,
844 const struct VerifyMessage *v_msg)
849 msg_size = ntohs (v_msg->header.size);
850 if (msg_size < sizeof (struct VerifyMessage))
853 return GNUNET_SYSERR;
855 if (ntohs (v_msg->issuer_attribute_len) > GNUNET_CREDENTIAL_MAX_LENGTH)
858 return GNUNET_SYSERR;
860 attr = (const char *) &v_msg[1];
862 if ( strlen (attr) > GNUNET_CREDENTIAL_MAX_LENGTH)
865 return GNUNET_SYSERR;
871 handle_verify (void *cls,
872 const struct VerifyMessage *v_msg)
874 struct VerifyRequestHandle *vrh;
875 struct GNUNET_SERVICE_Client *client = cls;
876 struct CredentialRecordEntry *cr_entry;
877 uint32_t credentials_count;
878 uint32_t credential_data_size;
879 char attr[GNUNET_CREDENTIAL_MAX_LENGTH + 1];
880 char issuer_attribute[GNUNET_CREDENTIAL_MAX_LENGTH + 1];
881 char *attrptr = attr;
882 char *credential_data;
885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
886 "Received VERIFY message\n");
887 utf_in = (const char *) &v_msg[1];
888 GNUNET_STRINGS_utf8_tolower (utf_in, attrptr);
889 GNUNET_memcpy (issuer_attribute,
891 ntohs (v_msg->issuer_attribute_len));
892 issuer_attribute[ntohs (v_msg->issuer_attribute_len)] = '\0';
893 vrh = GNUNET_new (struct VerifyRequestHandle);
894 GNUNET_CONTAINER_DLL_insert (vrh_head, vrh_tail, vrh);
895 vrh->client = client;
896 vrh->request_id = v_msg->id;
897 vrh->issuer_key = v_msg->issuer_key;
898 vrh->subject_key = v_msg->subject_key;
899 vrh->issuer_attribute = GNUNET_strdup (issuer_attribute);
900 if (0 == strlen (issuer_attribute))
902 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
903 "No issuer attribute provided!\n");
904 send_lookup_response (vrh);
908 * First, collect credentials
911 credentials_count = ntohl(v_msg->c_count);
912 credential_data_size = ntohs (v_msg->header.size)
913 - sizeof (struct VerifyMessage)
914 - ntohs (v_msg->issuer_attribute_len)
916 struct GNUNET_CREDENTIAL_Credential credentials[credentials_count];
917 credential_data = (char*)&v_msg[1] + ntohs (v_msg->issuer_attribute_len) + 1;
918 if (GNUNET_OK != GNUNET_CREDENTIAL_credentials_deserialize (credential_data_size,
923 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
924 "Cannot deserialize credentials!\n");
925 send_lookup_response (vrh);
929 for (uint32_t i=0;i<credentials_count;i++) {
930 cr_entry = GNUNET_new (struct CredentialRecordEntry);
931 cr_entry->credential = GNUNET_malloc (sizeof (struct GNUNET_CREDENTIAL_Credential) +
932 credentials[i].issuer_attribute_len);
933 GNUNET_memcpy (cr_entry->credential,
935 sizeof (struct GNUNET_CREDENTIAL_Credential));
936 GNUNET_memcpy (&cr_entry->credential[1],
937 credentials[i].issuer_attribute,
938 credentials[i].issuer_attribute_len);
939 cr_entry->credential->issuer_attribute = (char*)&cr_entry->credential[1];
940 GNUNET_CONTAINER_DLL_insert_tail (vrh->cred_chain_head,
941 vrh->cred_chain_tail,
943 vrh->cred_chain_size++;
946 delegation_chain_resolution_start (vrh);
951 handle_cred_collection_error_cb (void *cls)
953 struct VerifyRequestHandle *vrh = cls;
954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
955 "Got disconnected from namestore database.\n");
956 vrh->cred_collection_iter = NULL;
957 send_lookup_response (vrh);
961 collect_next (void *cls)
963 struct VerifyRequestHandle *vrh = cls;
964 vrh->collect_next_task = NULL;
965 GNUNET_assert (NULL != vrh->cred_collection_iter);
966 GNUNET_NAMESTORE_zone_iterator_next (vrh->cred_collection_iter,
972 handle_cred_collection_cb (void *cls,
973 const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
975 unsigned int rd_count,
976 const struct GNUNET_GNSRECORD_Data *rd)
978 struct VerifyRequestHandle *vrh = cls;
979 struct GNUNET_CREDENTIAL_Credential *crd;
980 struct CredentialRecordEntry *cr_entry;
981 int cred_record_count;
983 cred_record_count = 0;
984 for (uint32_t i=0; i < rd_count; i++)
986 if (GNUNET_GNSRECORD_TYPE_CREDENTIAL != rd[i].record_type)
989 crd = GNUNET_CREDENTIAL_credential_deserialize (rd[i].data,
993 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
994 "Invalid credential found\n");
997 cr_entry = GNUNET_new (struct CredentialRecordEntry);
998 cr_entry->credential = crd;
999 GNUNET_CONTAINER_DLL_insert_tail (vrh->cred_chain_head,
1000 vrh->cred_chain_tail,
1002 vrh->cred_chain_size++;
1004 vrh->collect_next_task = GNUNET_SCHEDULER_add_now (&collect_next,
1009 handle_cred_collection_finished_cb (void *cls)
1011 struct VerifyRequestHandle *vrh = cls;
1012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1013 "Done collecting credentials.\n");
1014 vrh->cred_collection_iter = NULL;
1015 delegation_chain_resolution_start (vrh);
1019 handle_collect (void *cls,
1020 const struct CollectMessage *c_msg)
1022 char attr[GNUNET_CREDENTIAL_MAX_LENGTH + 1];
1023 char issuer_attribute[GNUNET_CREDENTIAL_MAX_LENGTH + 1];
1024 struct VerifyRequestHandle *vrh;
1025 struct GNUNET_SERVICE_Client *client = cls;
1026 char *attrptr = attr;
1029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1030 "Received COLLECT message\n");
1032 utf_in = (const char *) &c_msg[1];
1033 GNUNET_STRINGS_utf8_tolower (utf_in, attrptr);
1035 GNUNET_memcpy (issuer_attribute,
1037 ntohs (c_msg->issuer_attribute_len));
1038 issuer_attribute[ntohs (c_msg->issuer_attribute_len)] = '\0';
1039 vrh = GNUNET_new (struct VerifyRequestHandle);
1040 GNUNET_CONTAINER_DLL_insert (vrh_head, vrh_tail, vrh);
1041 vrh->client = client;
1042 vrh->request_id = c_msg->id;
1043 vrh->issuer_key = c_msg->issuer_key;
1044 GNUNET_CRYPTO_ecdsa_key_get_public (&c_msg->subject_key,
1046 vrh->issuer_attribute = GNUNET_strdup (issuer_attribute);
1048 if (0 == strlen (issuer_attribute))
1050 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1051 "No issuer attribute provided!\n");
1052 send_lookup_response (vrh);
1055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1056 "Getting credentials for subject\n");
1058 * First, get attribute from subject
1060 vrh->cred_collection_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore,
1061 &c_msg->subject_key,
1062 &handle_cred_collection_error_cb,
1064 &handle_cred_collection_cb,
1066 &handle_cred_collection_finished_cb,
1072 check_collect (void *cls,
1073 const struct CollectMessage *c_msg)
1078 msg_size = ntohs (c_msg->header.size);
1079 if (msg_size < sizeof (struct CollectMessage))
1082 return GNUNET_SYSERR;
1084 if (ntohs (c_msg->issuer_attribute_len) > GNUNET_CREDENTIAL_MAX_LENGTH)
1087 return GNUNET_SYSERR;
1089 attr = (const char *) &c_msg[1];
1091 if ( ('\0' != attr[msg_size - sizeof (struct CollectMessage) - 1]) ||
1092 (strlen (attr) > GNUNET_CREDENTIAL_MAX_LENGTH) )
1095 return GNUNET_SYSERR;
1101 client_disconnect_cb (void *cls,
1102 struct GNUNET_SERVICE_Client *client,
1105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1106 "Client %p disconnected\n",
1111 client_connect_cb (void *cls,
1112 struct GNUNET_SERVICE_Client *client,
1113 struct GNUNET_MQ_Handle *mq)
1115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1116 "Client %p connected\n",
1122 * Process Credential requests.
1124 * @param cls closure
1125 * @param c configuration to use
1126 * @param handle service handle
1130 const struct GNUNET_CONFIGURATION_Handle *c,
1131 struct GNUNET_SERVICE_Handle *handle)
1134 gns = GNUNET_GNS_connect (c);
1138 _("Failed to connect to GNS\n"));
1140 namestore = GNUNET_NAMESTORE_connect (c);
1141 if (NULL == namestore)
1144 _("Failed to connect to namestore\n"));
1147 statistics = GNUNET_STATISTICS_create ("credential", c);
1148 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
1153 * Define "main" method using service macro
1157 GNUNET_SERVICE_OPTION_NONE,
1160 &client_disconnect_cb,
1162 GNUNET_MQ_hd_var_size (verify,
1163 GNUNET_MESSAGE_TYPE_CREDENTIAL_VERIFY,
1164 struct VerifyMessage,
1166 GNUNET_MQ_hd_var_size (collect,
1167 GNUNET_MESSAGE_TYPE_CREDENTIAL_COLLECT,
1168 struct CollectMessage,
1170 GNUNET_MQ_handler_end());
1172 /* end of gnunet-service-credential.c */