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 gns/gnunet-service-credential.c
22 * @brief GNU Credential Service (main service)
23 * @author Adnan Husain
26 #include "gnunet_util_lib.h"
27 #include "gnunet_credential_service.h"
28 #include "gnunet_statistics_service.h"
29 #include "credential.h"
30 #include "gnunet_protocols.h"
31 #include "gnunet_signatures.h"
33 // For Looking up GNS request
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>
39 #include "gnunet_gns_service.h"
44 #define GNUNET_CREDENTIAL_MAX_LENGTH 255
49 struct CredentialRecordEntry
54 struct CredentialRecordEntry *next;
59 struct CredentialRecordEntry *prev;
65 struct GNUNET_CREDENTIAL_CredentialRecordData *data;
74 * DLL for attributes - Used as a queue
75 * Insert tail - Pop head
77 struct AttributeQueueEntry
82 struct AttributeQueueEntry *next;
87 struct AttributeQueueEntry *prev;
92 struct GNUNET_CREDENTIAL_AttributeRecordData *data;
100 * Parent attribute delegation
102 struct AttributeQueueEntry *parent;
107 * Handle to a lookup operation from api
109 struct VerifyRequestHandle
113 * We keep these in a DLL.
115 struct VerifyRequestHandle *next;
118 * We keep these in a DLL.
120 struct VerifyRequestHandle *prev;
123 * Handle to the requesting client
125 struct GNUNET_SERVICE_Client *client;
128 * Handle to GNS lookup
130 struct GNUNET_GNS_LookupRequest *lookup_request;
135 struct GNUNET_CRYPTO_EcdsaPublicKey issuer_key;
140 char *issuer_attribute;
145 struct GNUNET_CRYPTO_EcdsaPublicKey subject_key;
150 struct CredentialRecordEntry *cred_chain_head;
155 struct CredentialRecordEntry *cred_chain_tail;
158 * Number of chain entries
160 uint32_t cred_chain_entries;
165 struct AttributeQueueEntry *attr_queue_head;
170 struct AttributeQueueEntry *attr_queue_tail;
173 * Current Attribute Pointer
175 struct AttributeQueueEntry *current_attribute;
178 * The found credential
180 struct GNUNET_CREDENTIAL_CredentialRecordData *credential;
183 * Length of the credential
185 uint32_t credential_size;
198 static struct VerifyRequestHandle *vrh_head;
203 static struct VerifyRequestHandle *vrh_tail;
206 * Handle to the statistics service
208 static struct GNUNET_STATISTICS_Handle *statistics;
213 * Handle to GNS service.
215 static struct GNUNET_GNS_Handle *gns;
218 * Task run during shutdown.
224 shutdown_task (void *cls)
226 struct VerifyRequestHandle *vrh;
228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
230 while (NULL != (vrh = vrh_head))
232 //CREDENTIAL_resolver_lookup_cancel (clh->lookup);
233 GNUNET_CONTAINER_DLL_remove (vrh_head,
241 GNUNET_GNS_disconnect (gns);
244 if (NULL != statistics)
246 GNUNET_STATISTICS_destroy (statistics,
254 * Checks a #GNUNET_MESSAGE_TYPE_CREDENTIAL_VERIFY message
256 * @param cls client sending the message
257 * @param v_msg message of type `struct VerifyMessage`
258 * @return #GNUNET_OK if @a v_msg is well-formed
261 check_verify (void *cls,
262 const struct VerifyMessage *v_msg)
267 msg_size = ntohs (v_msg->header.size);
268 if (msg_size < sizeof (struct VerifyMessage))
271 return GNUNET_SYSERR;
273 if ((ntohs (v_msg->issuer_attribute_len) > GNUNET_CREDENTIAL_MAX_LENGTH) ||
274 (ntohs (v_msg->subject_attribute_len) > GNUNET_CREDENTIAL_MAX_LENGTH))
277 return GNUNET_SYSERR;
279 attrs = (const char *) &v_msg[1];
281 if ( ('\0' != attrs[ntohs(v_msg->header.size) - sizeof (struct VerifyMessage) - 1]) ||
282 (strlen (attrs) > GNUNET_CREDENTIAL_MAX_LENGTH * 2) )
285 return GNUNET_SYSERR;
293 * @param handle the handle to the request
296 send_lookup_response (struct VerifyRequestHandle *vrh)
299 struct GNUNET_MQ_Envelope *env;
300 struct VerifyResultMessage *rmsg;
303 * Get serialized record data size
305 len = vrh->credential_size; //TODO max length of attr
307 //TODO add attr chain
309 * Prepare a lookup result response message for the client
311 env = GNUNET_MQ_msg_extra (rmsg,
313 GNUNET_MESSAGE_TYPE_CREDENTIAL_VERIFY_RESULT);
314 //Assign id so that client can find associated request
315 rmsg->id = vrh->request_id;
316 rmsg->cd_count = htonl (vrh->cred_chain_entries);
319 * Get serialized record data
320 * Append at the end of rmsg
322 rmsg->cred_found = htonl (GNUNET_NO);
323 if (NULL != vrh->credential)
327 vrh->credential_size);
328 rmsg->cred_found = htonl (GNUNET_YES);
331 /*char* tmp_entry = (char*)&rmsg[1];
332 for (cr_entry = vrh->cred_chain_head; NULL != cr_entry; cr_entry = cr_entry->next)
335 &cr_entry->record_data,
336 cr_entry->record_data_size);
337 tmp_entry += cr_entry->record_data_size;
339 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq(vrh->client),
342 GNUNET_CONTAINER_DLL_remove (vrh_head, vrh_tail, vrh);
347 * - Refactor into cleanup_handle() function for this
351 GNUNET_STATISTICS_update (statistics,
352 "Completed verifications", 1,
358 start_backward_resolution (void* cls,
360 const struct GNUNET_GNSRECORD_Data *rd)
362 struct VerifyRequestHandle *vrh = cls;
363 struct GNUNET_CREDENTIAL_CredentialRecordData *cred;
364 const struct GNUNET_CREDENTIAL_AttributeRecordData *attr;
365 struct CredentialRecordEntry *cred_pointer;
366 struct AttributeQueueEntry *attr_entry;
369 for (i=0; i < rd_count; i++)
371 if (GNUNET_GNSRECORD_TYPE_ATTRIBUTE != rd[i].record_type)
374 for(cred_pointer = vrh->cred_chain_head; cred_pointer != NULL;
375 cred_pointer = cred_pointer->next){
376 cred = cred_pointer->data;
378 attr_entry = GNUNET_new (struct AttributeQueueEntry);
380 attr_entry->data = GNUNET_malloc (rd[i].data_size);
381 memcpy (attr_entry->data,
384 attr_entry->data_size = rd[i].data_size;
386 attr_entry->parent = vrh->current_attribute;
388 GNUNET_CONTAINER_DLL_insert (vrh->attr_queue_head,
389 vrh->attr_queue_tail,
392 if(0 != memcmp (&attr->subject_key,
393 &cred_pointer->data->issuer_key,
394 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey)))
397 if (0 != strcmp ((char*)&attr[1], (char*)&cred[1]))
401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
403 vrh->credential = GNUNET_malloc (rd[i].data_size);
404 memcpy (vrh->credential,
407 vrh->credential_size = rd[i].data_size;
409 send_lookup_response (vrh);
417 //Start from next to head
418 vrh->current_attribute = vrh->attr_queue_head;
420 if(vrh->current_attribute != NULL)
422 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
423 "We are all out of attributes...\n");
424 send_lookup_response (vrh);
428 GNUNET_CONTAINER_DLL_remove (vrh->attr_queue_head,
429 vrh->attr_queue_tail,
430 vrh->current_attribute);
434 //Start with backward resolution
435 vrh->lookup_request = GNUNET_GNS_lookup (gns,
436 (char*)&vrh->current_attribute->data[1],
437 &vrh->current_attribute->data->subject_key, //issuer_key,
438 GNUNET_GNSRECORD_TYPE_ATTRIBUTE,
439 GNUNET_GNS_LO_DEFAULT,
440 NULL, //shorten_key, always NULL
441 &start_backward_resolution,
448 * Result from GNS lookup.
450 * @param cls the closure (our client lookup handle)
451 * @param rd_count the number of records in @a rd
452 * @param rd the record data
455 handle_credential_query (void* cls,
457 const struct GNUNET_GNSRECORD_Data *rd)
459 struct VerifyRequestHandle *vrh = cls;
460 int cred_record_count;
462 const struct GNUNET_CREDENTIAL_CredentialRecordData *crd;
463 struct CredentialRecordEntry *cr_entry;
465 cred_record_count = 0;
466 for (i=0; i < rd_count; i++)
468 if (GNUNET_GNSRECORD_TYPE_CREDENTIAL != rd[i].record_type)
474 * Check if we have already found our credential here
475 * If so return success
477 * Save all found attributes/issues and prepare forward
478 * resolution of issuer attribute
480 if(GNUNET_OK != GNUNET_CRYPTO_ecdsa_verify(GNUNET_SIGNATURE_PURPOSE_CREDENTIAL,
485 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
486 "Invalid credential found\n");
489 cr_entry = GNUNET_new (struct CredentialRecordEntry);
490 cr_entry->data = GNUNET_malloc (rd[i].data_size);
491 memcpy (cr_entry->data,
494 cr_entry->data_size = rd[i].data_size;
495 GNUNET_CONTAINER_DLL_insert_tail (vrh->cred_chain_head,
496 vrh->cred_chain_tail,
499 if (0 != memcmp (&crd->issuer_key,
501 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
503 if (0 != strcmp ((char*)&crd[1], vrh->issuer_attribute))
505 vrh->credential = GNUNET_malloc (rd[i].data_size);
506 memcpy (vrh->credential,
509 vrh->credential_size = rd[i].data_size;
510 //Found match prematurely
511 send_lookup_response (vrh);
516 GNUNET_break (0); //TODO remove when implemented
519 * Check for attributes from the issuer and follow the chain
520 * till you get the required subject's attributes
522 char issuer_attribute_name[strlen (vrh->issuer_attribute)];
523 strcpy (issuer_attribute_name,
524 vrh->issuer_attribute);
525 strcpy (issuer_attribute_name + strlen (vrh->issuer_attribute),
527 //Start with backward resolution
528 GNUNET_GNS_lookup (gns,
529 issuer_attribute_name,
530 &vrh->issuer_key, //issuer_key,
531 GNUNET_GNSRECORD_TYPE_ATTRIBUTE,
532 GNUNET_GNS_LO_DEFAULT,
533 NULL, //shorten_key, always NULL
534 &start_backward_resolution,
540 * Handle Credential verification requests from client
542 * @param cls the closure
543 * @param client the client
544 * @param message the message
547 handle_verify (void *cls,
548 const struct VerifyMessage *v_msg)
550 char attrs[GNUNET_CREDENTIAL_MAX_LENGTH*2 + 1];
551 char issuer_attribute[GNUNET_CREDENTIAL_MAX_LENGTH + 1];
552 char subject_attribute[GNUNET_CREDENTIAL_MAX_LENGTH + 1 + 4];
553 struct VerifyRequestHandle *vrh;
554 struct GNUNET_SERVICE_Client *client = cls;
555 char *attrptr = attrs;
558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
559 "Received VERIFY message\n");
561 utf_in = (const char *) &v_msg[1];
562 GNUNET_STRINGS_utf8_tolower (utf_in, attrptr);
564 GNUNET_memcpy (issuer_attribute, attrs, ntohs (v_msg->issuer_attribute_len));
565 issuer_attribute[ntohs (v_msg->issuer_attribute_len)] = '\0';
566 GNUNET_memcpy (subject_attribute, attrs+strlen(issuer_attribute), ntohs (v_msg->subject_attribute_len));
567 strcpy (subject_attribute+ntohs (v_msg->subject_attribute_len),
569 subject_attribute[ntohs (v_msg->subject_attribute_len)+4] = '\0';
570 vrh = GNUNET_new (struct VerifyRequestHandle);
571 GNUNET_CONTAINER_DLL_insert (vrh_head, vrh_tail, vrh);
572 vrh->client = client;
573 vrh->request_id = v_msg->id;
574 vrh->issuer_key = v_msg->issuer_key;
575 vrh->subject_key = v_msg->subject_key;
576 vrh->issuer_attribute = GNUNET_strdup (issuer_attribute);
578 if (NULL == subject_attribute)
580 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
581 "No subject attribute provided!\n");
582 send_lookup_response (vrh);
585 if (NULL == issuer_attribute)
587 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
588 "No issuer attribute provided!\n");
589 send_lookup_response (vrh);
593 * First, get attribute from subject
595 vrh->lookup_request = GNUNET_GNS_lookup (gns,
597 &v_msg->subject_key, //subject_pkey,
598 GNUNET_GNSRECORD_TYPE_CREDENTIAL,
599 GNUNET_GNS_LO_DEFAULT,
600 NULL, //shorten_key, always NULL
601 &handle_credential_query,
607 * One of our clients disconnected, clean up after it.
610 * @param client the client that disconnected
613 client_disconnect_cb (void *cls,
614 struct GNUNET_SERVICE_Client *client,
617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
618 "Client %p disconnected\n",
623 * Add a client to our list of active clients.
626 * @param client client to add
627 * @param mq message queue for @a client
628 * @return this client
631 client_connect_cb (void *cls,
632 struct GNUNET_SERVICE_Client *client,
633 struct GNUNET_MQ_Handle *mq)
635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
636 "Client %p connected\n",
642 * Process Credential requests.
645 * @param server the initialized server
646 * @param c configuration to use
650 const struct GNUNET_CONFIGURATION_Handle *c,
651 struct GNUNET_SERVICE_Handle *handle)
654 gns = GNUNET_GNS_connect (c);
658 _("Failed to connect to GNS\n"));
661 statistics = GNUNET_STATISTICS_create ("credential", c);
662 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
667 * Define "main" method using service macro
671 GNUNET_SERVICE_OPTION_NONE,
674 &client_disconnect_cb,
676 GNUNET_MQ_hd_var_size (verify,
677 GNUNET_MESSAGE_TYPE_CREDENTIAL_VERIFY,
678 struct VerifyMessage,
680 GNUNET_MQ_handler_end());
682 /* end of gnunet-service-credential.c */