2 This file is part of GNUnet.
3 Copyright (C) 2009-2013, 2016 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/credential_api.c
22 * @brief library to access the CREDENTIAL service
23 * @author Adnan Husain
26 #include "gnunet_util_lib.h"
27 #include "gnunet_constants.h"
28 #include "gnunet_arm_service.h"
29 #include "gnunet_hello_lib.h"
30 #include "gnunet_protocols.h"
31 #include "gnunet_signatures.h"
32 #include "credential.h"
33 #include "credential_serialization.h"
34 #include "gnunet_credential_service.h"
35 #include "gnunet_identity_service.h"
38 #define LOG(kind,...) GNUNET_log_from (kind, "credential-api",__VA_ARGS__)
41 * Handle to a verify request
43 struct GNUNET_CREDENTIAL_Request
49 struct GNUNET_CREDENTIAL_Request *next;
54 struct GNUNET_CREDENTIAL_Request *prev;
57 * handle to credential service
59 struct GNUNET_CREDENTIAL_Handle *credential_handle;
62 * processor to call on verify result
64 GNUNET_CREDENTIAL_VerifyResultProcessor verify_proc;
67 * @e verify_proc closure
72 * Envelope with the message for this queue entry.
74 struct GNUNET_MQ_Envelope *env;
85 * Connection to the CREDENTIAL service.
87 struct GNUNET_CREDENTIAL_Handle
91 * Configuration to use.
93 const struct GNUNET_CONFIGURATION_Handle *cfg;
96 * Connection to service (if available).
98 struct GNUNET_MQ_Handle *mq;
101 * Head of linked list of active verify requests.
103 struct GNUNET_CREDENTIAL_Request *verify_head;
106 * Tail of linked list of active verify requests.
108 struct GNUNET_CREDENTIAL_Request *verify_tail;
113 struct GNUNET_SCHEDULER_Task *reconnect_task;
116 * How long do we wait until we try to reconnect?
118 struct GNUNET_TIME_Relative reconnect_backoff;
121 * Request Id generator. Incremented by one for each request.
129 * Reconnect to CREDENTIAL service.
131 * @param handle the handle to the CREDENTIAL service
134 reconnect (struct GNUNET_CREDENTIAL_Handle *handle);
138 * Reconnect to CREDENTIAL
140 * @param cls the handle
143 reconnect_task (void *cls)
145 struct GNUNET_CREDENTIAL_Handle *handle = cls;
147 handle->reconnect_task = NULL;
153 * Disconnect from service and then reconnect.
155 * @param handle our handle
158 force_reconnect (struct GNUNET_CREDENTIAL_Handle *handle)
160 GNUNET_MQ_destroy (handle->mq);
162 handle->reconnect_backoff
163 = GNUNET_TIME_STD_BACKOFF (handle->reconnect_backoff);
164 handle->reconnect_task
165 = GNUNET_SCHEDULER_add_delayed (handle->reconnect_backoff,
172 * Generic error handler, called with the appropriate error code and
173 * the same closure specified at the creation of the message queue.
174 * Not every message queue implementation supports an error handler.
176 * @param cls closure with the `struct GNUNET_CREDENTIAL_Handle *`
177 * @param error error code
180 mq_error_handler (void *cls,
181 enum GNUNET_MQ_Error error)
183 struct GNUNET_CREDENTIAL_Handle *handle = cls;
185 force_reconnect (handle);
190 * Check validity of message received from the CREDENTIAL service
192 * @param cls the `struct GNUNET_CREDENTIAL_Handle *`
193 * @param loookup_msg the incoming message
196 check_result (void *cls,
197 const struct VerifyResultMessage *vr_msg)
205 * Handler for messages received from the CREDENTIAL service
207 * @param cls the `struct GNUNET_CREDENTIAL_Handle *`
208 * @param loookup_msg the incoming message
211 handle_result (void *cls,
212 const struct VerifyResultMessage *vr_msg)
214 struct GNUNET_CREDENTIAL_Handle *handle = cls;
215 uint32_t r_id = ntohl (vr_msg->id);
216 struct GNUNET_CREDENTIAL_Request *vr;
217 size_t mlen = ntohs (vr_msg->header.size) - sizeof (*vr_msg);
218 uint32_t d_count = ntohl (vr_msg->d_count);
219 uint32_t c_count = ntohl (vr_msg->c_count);
220 struct GNUNET_CREDENTIAL_Delegation d_chain[d_count];
221 struct GNUNET_CREDENTIAL_Credential creds[c_count];
222 GNUNET_CREDENTIAL_VerifyResultProcessor proc;
225 LOG (GNUNET_ERROR_TYPE_DEBUG,
226 "Received verify reply from CREDENTIAL service\n");
227 for (vr = handle->verify_head; NULL != vr; vr = vr->next)
228 if (vr->r_id == r_id)
232 proc = vr->verify_proc;
233 proc_cls = vr->proc_cls;
234 GNUNET_CONTAINER_DLL_remove (handle->verify_head,
237 GNUNET_MQ_discard (vr->env);
239 GNUNET_assert (GNUNET_OK ==
240 GNUNET_CREDENTIAL_delegation_chain_deserialize (mlen,
241 (const char*) &vr_msg[1],
246 if (GNUNET_NO == ntohl (vr_msg->cred_found))
264 * Reconnect to CREDENTIAL service.
266 * @param handle the handle to the CREDENTIAL service
269 reconnect (struct GNUNET_CREDENTIAL_Handle *handle)
271 struct GNUNET_MQ_MessageHandler handlers[] = {
272 GNUNET_MQ_hd_var_size (result,
273 GNUNET_MESSAGE_TYPE_CREDENTIAL_VERIFY_RESULT,
274 struct VerifyResultMessage,
276 GNUNET_MQ_handler_end ()
278 struct GNUNET_CREDENTIAL_Request *vr;
280 GNUNET_assert (NULL == handle->mq);
281 LOG (GNUNET_ERROR_TYPE_DEBUG,
282 "Trying to connect to CREDENTIAL\n");
283 handle->mq = GNUNET_CLIENT_connecT (handle->cfg,
288 if (NULL == handle->mq)
290 for (vr = handle->verify_head; NULL != vr; vr = vr->next)
291 GNUNET_MQ_send_copy (handle->mq,
297 * Initialize the connection with the CREDENTIAL service.
299 * @param cfg configuration to use
300 * @return handle to the CREDENTIAL service, or NULL on error
302 struct GNUNET_CREDENTIAL_Handle *
303 GNUNET_CREDENTIAL_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
305 struct GNUNET_CREDENTIAL_Handle *handle;
307 handle = GNUNET_new (struct GNUNET_CREDENTIAL_Handle);
310 if (NULL == handle->mq)
312 GNUNET_free (handle);
320 * Shutdown connection with the CREDENTIAL service.
322 * @param handle handle of the CREDENTIAL connection to stop
325 GNUNET_CREDENTIAL_disconnect (struct GNUNET_CREDENTIAL_Handle *handle)
327 if (NULL != handle->mq)
329 GNUNET_MQ_destroy (handle->mq);
332 if (NULL != handle->reconnect_task)
334 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
335 handle->reconnect_task = NULL;
337 GNUNET_assert (NULL == handle->verify_head);
338 GNUNET_free (handle);
343 * Cancel pending verify request
345 * @param lr the verify request to cancel
348 GNUNET_CREDENTIAL_verify_cancel (struct GNUNET_CREDENTIAL_Request *vr)
350 struct GNUNET_CREDENTIAL_Handle *handle = vr->credential_handle;
352 GNUNET_CONTAINER_DLL_remove (handle->verify_head,
355 GNUNET_MQ_discard (vr->env);
360 * Performs attribute verification.
361 * Checks if there is a delegation chain from
362 * attribute ``issuer_attribute'' issued by the issuer
363 * with public key ``issuer_key'' maps to the attribute
364 * ``subject_attribute'' claimed by the subject with key
367 * @param handle handle to the Credential service
368 * @param issuer_key the issuer public key
369 * @param issuer_attribute the issuer attribute
370 * @param subject_key the subject public key
371 * @param subject_attribute the attribute claimed by the subject
372 * @param proc function to call on result
373 * @param proc_cls closure for processor
374 * @return handle to the queued request
376 struct GNUNET_CREDENTIAL_Request*
377 GNUNET_CREDENTIAL_verify (struct GNUNET_CREDENTIAL_Handle *handle,
378 const struct GNUNET_CRYPTO_EcdsaPublicKey *issuer_key,
379 const char *issuer_attribute,
380 const struct GNUNET_CRYPTO_EcdsaPublicKey *subject_key,
381 const char *subject_attribute,
382 GNUNET_CREDENTIAL_VerifyResultProcessor proc,
385 /* IPC to shorten credential names, return shorten_handle */
386 struct VerifyMessage *v_msg;
387 struct GNUNET_CREDENTIAL_Request *vr;
390 if (NULL == issuer_attribute || NULL == subject_attribute)
396 LOG (GNUNET_ERROR_TYPE_DEBUG,
397 "Trying to verify `%s' in CREDENTIAL\n",
399 nlen = strlen (issuer_attribute) + strlen (subject_attribute) + 1;
400 if (nlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (*vr))
405 vr = GNUNET_new (struct GNUNET_CREDENTIAL_Request);
406 vr->credential_handle = handle;
407 vr->verify_proc = proc;
408 vr->proc_cls = proc_cls;
409 vr->r_id = handle->r_id_gen++;
410 vr->env = GNUNET_MQ_msg_extra (v_msg,
412 GNUNET_MESSAGE_TYPE_CREDENTIAL_VERIFY);
413 v_msg->id = htonl (vr->r_id);
414 v_msg->subject_key = *subject_key;
415 v_msg->issuer_key = *issuer_key;
416 v_msg->issuer_attribute_len = htons(strlen(issuer_attribute));
417 v_msg->subject_attribute_len = htons(strlen(subject_attribute));
418 GNUNET_memcpy (&v_msg[1],
420 strlen (issuer_attribute));
421 GNUNET_memcpy (((char*)&v_msg[1]) + strlen (issuer_attribute),
423 strlen (subject_attribute));
424 GNUNET_CONTAINER_DLL_insert (handle->verify_head,
427 if (NULL != handle->mq)
428 GNUNET_MQ_send_copy (handle->mq,
433 /* end of credential_api.c */