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 record_data;
69 * DLL for attributes - Used as a queue
70 * Insert tail - Pop head
72 struct AttributeRecordEntry
77 struct AttributeRecordEntry *next;
82 struct AttributeRecordEntry *prev;
87 struct GNUNET_CREDENTIAL_AttributeRecordData record_data;
92 * Handle to a lookup operation from api
94 struct VerifyRequestHandle
98 * We keep these in a DLL.
100 struct VerifyRequestHandle *next;
103 * We keep these in a DLL.
105 struct VerifyRequestHandle *prev;
108 * Handle to the requesting client
110 struct GNUNET_SERVICE_Client *client;
113 * Handle to GNS lookup
115 struct GNUNET_GNS_LookupRequest *lookup_request;
120 struct GNUNET_CRYPTO_EcdsaPublicKey issuer_key;
125 char *issuer_attribute;
130 struct GNUNET_CRYPTO_EcdsaPublicKey subject_key;
135 struct CredentialRecordEntry *cred_chain_head;
140 struct CredentialRecordEntry *cred_chain_tail;
145 struct AttributeRecordEntry *attr_queue_head;
150 struct AttributeRecordEntry *attr_queue_tail;
153 * Current Attribute Pointer
155 struct AttributeRecordEntry* attr_pointer;
168 static struct VerifyRequestHandle *vrh_head;
173 static struct VerifyRequestHandle *vrh_tail;
176 * Handle to the statistics service
178 static struct GNUNET_STATISTICS_Handle *statistics;
183 * Handle to GNS service.
185 static struct GNUNET_GNS_Handle *gns;
188 * Task run during shutdown.
194 shutdown_task (void *cls)
196 struct VerifyRequestHandle *vrh;
198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
200 while (NULL != (vrh = vrh_head))
202 //CREDENTIAL_resolver_lookup_cancel (clh->lookup);
203 GNUNET_CONTAINER_DLL_remove (vrh_head,
210 if (NULL != statistics)
212 GNUNET_STATISTICS_destroy (statistics,
220 * Checks a #GNUNET_MESSAGE_TYPE_CREDENTIAL_VERIFY message
222 * @param cls client sending the message
223 * @param v_msg message of type `struct VerifyMessage`
224 * @return #GNUNET_OK if @a v_msg is well-formed
227 check_verify (void *cls,
228 const struct VerifyMessage *v_msg)
235 msg_size = ntohs (v_msg->header.size);
236 if (msg_size < sizeof (struct VerifyMessage))
239 return GNUNET_SYSERR;
241 i_attr = (const char *) &v_msg[1];
242 if ( ('\0' != i_attr[v_msg->header.size - sizeof (struct VerifyMessage) - 1]) ||
243 (strlen (i_attr) > GNUNET_CREDENTIAL_MAX_LENGTH) )
246 return GNUNET_SYSERR;
248 attr_len = strlen (i_attr);
249 s_attr = ((const char *) &v_msg[1]) + attr_len + 1;
250 if ( ('\0' != s_attr[v_msg->header.size - sizeof (struct VerifyMessage) - 1]) ||
251 (strlen (s_attr) > GNUNET_CREDENTIAL_MAX_LENGTH) )
254 return GNUNET_SYSERR;
260 start_backward_resolution (void* cls,
262 const struct GNUNET_GNSRECORD_Data *rd)
264 struct VerifyRequestHandle *vrh = cls;
266 struct GNUNET_CREDENTIAL_CredentialRecordData *cred;
267 struct GNUNET_CREDENTIAL_AttributeRecordData *attr;
268 struct CredentialRecordEntry *cred_pointer;
269 const char *attribute;
270 const char *cred_attribute;
272 char *cred_issuer_key;
273 const struct GNUNET_CRYPTO_EcdsaPublicKey *issuer_key_ecdsa;
274 const struct GNUNET_CRYPTO_EcdsaPublicKey *cred_issuer_key_ecdsa;
276 for(cred_pointer = vrh->cred_chain_head; cred_pointer != NULL;
277 cred_pointer = cred_pointer->next){
278 cred = &cred_pointer->record_data;
279 issuer_key_ecdsa = &vrh->attr_pointer->record_data.subject_key;
280 cred_issuer_key_ecdsa = &cred_pointer->record_data.issuer_key;
282 issuer_key = GNUNET_CRYPTO_ecdsa_public_key_to_string(issuer_key_ecdsa);
283 cred_issuer_key = GNUNET_CRYPTO_ecdsa_public_key_to_string(cred_issuer_key_ecdsa);
284 if(0 == strcmp(issuer_key,cred_issuer_key))
286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
294 //Start from next to head
295 for(vrh->attr_pointer = vrh->attr_queue_head->next ; vrh->attr_pointer->next != NULL ;
296 vrh->attr_pointer = vrh->attr_pointer->next ){
298 //Start with backward resolution
299 GNUNET_GNS_lookup (gns,
300 vrh->issuer_attribute,
301 &vrh->issuer_key, //issuer_key,
302 GNUNET_GNSRECORD_TYPE_ATTRIBUTE,
303 GNUNET_GNS_LO_DEFAULT,
304 NULL, //shorten_key, always NULL
305 &start_backward_resolution,
313 * Result from GNS lookup.
315 * @param cls the closure (our client lookup handle)
316 * @param rd_count the number of records in @a rd
317 * @param rd the record data
320 send_lookup_response (void* cls,
322 const struct GNUNET_GNSRECORD_Data *rd)
324 struct VerifyRequestHandle *vrh = cls;
327 int cred_record_count;
328 struct GNUNET_MQ_Envelope *env;
329 struct VerifyResultMessage *rmsg;
330 const struct GNUNET_CREDENTIAL_CredentialRecordData *crd;
331 struct CredentialRecordEntry *cr_entry;
334 cred_record_count = 0;
335 struct AttributeRecordEntry *attr_entry;
337 struct GNUNET_CREDENTIAL_AttributeRecordData *ard =
338 GNUNET_new(struct GNUNET_CREDENTIAL_AttributeRecordData);
340 attr_entry->record_data = *ard;
341 ard->subject_key = vrh->issuer_key;
342 GNUNET_CONTAINER_DLL_insert_tail (vrh->attr_queue_head,
343 vrh->attr_queue_tail,
345 for (i=0; i < rd_count; i++)
347 if (GNUNET_GNSRECORD_TYPE_CREDENTIAL != rd[i].record_type)
353 * Check if we have already found our credential here
354 * If so return success
356 * Save all found attributes/issues and prepare forward
357 * resolution of issuer attribute
359 cr_entry = GNUNET_new (struct CredentialRecordEntry);
360 cr_entry->record_data = *crd;
361 GNUNET_CONTAINER_DLL_insert_tail (vrh->cred_chain_head,
362 vrh->cred_chain_tail,
365 if(GNUNET_OK == GNUNET_CRYPTO_ecdsa_verify(GNUNET_SIGNATURE_PURPOSE_CREDENTIAL,
367 &crd->sig, &crd->issuer_key))
369 cred_verified = GNUNET_YES;
377 * Check for attributes from the issuer and follow the chain
378 * till you get the required subject's attributes
380 if(cred_verified != GNUNET_YES){
383 vrh->attr_pointer = vrh->attr_queue_head;
385 //Start with backward resolution
386 GNUNET_GNS_lookup (gns,
387 vrh->issuer_attribute,
388 &vrh->issuer_key, //issuer_key,
389 GNUNET_GNSRECORD_TYPE_ATTRIBUTE,
390 GNUNET_GNS_LO_DEFAULT,
391 NULL, //shorten_key, always NULL
392 &start_backward_resolution,
400 * Start resolution of Attribute delegations from issuer
402 * - Build adequate data structures for attribute(s) to lookup
403 * - Use GNUNET_GNSRECORD_TYPE_XXX
404 * - recursively try to find match(es) with results found top
405 * - return one found credential chain
410 * Get serialized record data size
412 len = cred_record_count * sizeof (struct GNUNET_CREDENTIAL_CredentialRecordData);
415 * Prepare a lookup result response message for the client
417 env = GNUNET_MQ_msg_extra (rmsg,
419 GNUNET_MESSAGE_TYPE_CREDENTIAL_VERIFY_RESULT);
420 //Assign id so that client can find associated request
421 rmsg->id = vrh->request_id;
422 rmsg->ad_count = htonl (cred_record_count);
425 * Get serialized record data
426 * Append at the end of rmsg
429 struct GNUNET_CREDENTIAL_CredentialRecordData *tmp_record = (struct GNUNET_CREDENTIAL_CredentialRecordData*) &rmsg[1];
430 for (cr_entry = vrh->cred_chain_head; NULL != cr_entry; cr_entry = cr_entry->next)
433 &cr_entry->record_data,
434 sizeof (struct GNUNET_CREDENTIAL_CredentialRecordData));
437 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq(vrh->client),
440 GNUNET_CONTAINER_DLL_remove (vrh_head, vrh_tail, vrh);
445 * - Refactor into cleanup_handle() function for this
449 GNUNET_STATISTICS_update (statistics,
450 "Completed verifications", 1,
452 GNUNET_STATISTICS_update (statistics,
453 "Credentials resolved",
459 * Handle Credential verification requests from client
461 * @param cls the closure
462 * @param client the client
463 * @param message the message
466 handle_verify (void *cls,
467 const struct VerifyMessage *v_msg)
469 char issuer_attribute[GNUNET_CREDENTIAL_MAX_LENGTH + 1];
470 char subject_attribute[GNUNET_CREDENTIAL_MAX_LENGTH + 1];
471 size_t issuer_attribute_len;
472 struct VerifyRequestHandle *vrh;
473 struct GNUNET_SERVICE_Client *client = cls;
474 char *attrptr = issuer_attribute;
477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
478 "Received VERIFY message\n");
480 utf_in = (const char *) &v_msg[1];
481 GNUNET_STRINGS_utf8_tolower (utf_in, attrptr);
482 issuer_attribute_len = strlen (utf_in);
483 utf_in = (const char *) (&v_msg[1] + issuer_attribute_len + 1);
484 attrptr = subject_attribute;
485 GNUNET_STRINGS_utf8_tolower (utf_in, attrptr);
486 vrh = GNUNET_new (struct VerifyRequestHandle);
487 GNUNET_CONTAINER_DLL_insert (vrh_head, vrh_tail, vrh);
488 vrh->client = client;
489 vrh->request_id = v_msg->id;
490 vrh->issuer_key = v_msg->issuer_key;
491 vrh->subject_key = v_msg->subject_key;
492 vrh->issuer_attribute = issuer_attribute;
494 if (NULL == subject_attribute)
496 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
497 "No subject attribute provided!\n");
498 send_lookup_response (vrh, 0, NULL);
501 if (NULL == issuer_attribute)
503 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
504 "No issuer attribute provided!\n");
505 send_lookup_response (vrh, 0, NULL);
509 * First, get attribute from subject
511 vrh->lookup_request = GNUNET_GNS_lookup (gns,
513 &v_msg->subject_key, //subject_pkey,
514 GNUNET_GNSRECORD_TYPE_CREDENTIAL,
515 GNUNET_GNS_LO_DEFAULT,
516 NULL, //shorten_key, always NULL
517 &send_lookup_response,
523 * One of our clients disconnected, clean up after it.
526 * @param client the client that disconnected
529 client_disconnect_cb (void *cls,
530 struct GNUNET_SERVICE_Client *client,
533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
534 "Client %p disconnected\n",
539 * Add a client to our list of active clients.
542 * @param client client to add
543 * @param mq message queue for @a client
544 * @return this client
547 client_connect_cb (void *cls,
548 struct GNUNET_SERVICE_Client *client,
549 struct GNUNET_MQ_Handle *mq)
551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
552 "Client %p connected\n",
558 * Process Credential requests.
561 * @param server the initialized server
562 * @param c configuration to use
566 const struct GNUNET_CONFIGURATION_Handle *c,
567 struct GNUNET_SERVICE_Handle *handle)
570 gns = GNUNET_GNS_connect (c);
574 _("Failed to connect to GNS\n"));
577 statistics = GNUNET_STATISTICS_create ("credential", c);
578 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
583 * Define "main" method using service macro
587 GNUNET_SERVICE_OPTION_NONE,
590 &client_disconnect_cb,
592 GNUNET_MQ_hd_var_size (verify,
593 GNUNET_MESSAGE_TYPE_CREDENTIAL_VERIFY,
594 struct VerifyMessage,
596 GNUNET_MQ_handler_end());
598 /* end of gnunet-service-credential.c */