GNUnet 0.22.2
plugin_reclaim_credential_pabc.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet
3 Copyright (C) 2013, 2014, 2016 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
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 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
28#include "gnunet_common.h"
29#include "platform.h"
30#include "gnunet_util_lib.h"
32#include <inttypes.h>
33#include <jansson.h>
34#include <pabc/pabc.h>
35#include "pabc_helper.h"
36
46static char *
48 uint32_t type,
49 const void *data,
50 size_t data_size)
51{
52 switch (type)
53 {
56
57 default:
58 return NULL;
59 }
60}
61
62
74static int
76 uint32_t type,
77 const char *s,
78 void **data,
79 size_t *data_size)
80{
81 if (NULL == s)
82 return GNUNET_SYSERR;
83 switch (type)
84 {
86 *data = GNUNET_strdup (s);
87 *data_size = strlen (s) + 1;
88 return GNUNET_OK;
89
90 default:
91 return GNUNET_SYSERR;
92 }
93}
94
95
100static struct
101{
102 const char *name;
103 uint32_t number;
105 { NULL, UINT32_MAX } };
106
114static uint32_t
115pabc_typename_to_number (void *cls, const char *pabc_typename)
116{
117 unsigned int i;
118
119 i = 0;
120 while ((NULL != pabc_cred_name_map[i].name) &&
121 (0 != strcasecmp (pabc_typename, pabc_cred_name_map[i].name)))
122 i++;
123 return pabc_cred_name_map[i].number;
124}
125
126
134static const char *
135pabc_number_to_typename (void *cls, uint32_t type)
136{
137 unsigned int i;
138
139 i = 0;
140 while ((NULL != pabc_cred_name_map[i].name) && (type !=
142 number))
143 i++;
144 return pabc_cred_name_map[i].name;
145}
146
147
148static void
149inspect_attrs (char const *const key,
150 char const *const value,
151 void *ctx)
152{
153 struct GNUNET_RECLAIM_AttributeList *attrs = ctx;
154
155 if (NULL == value)
156 return;
158 "Found attribute in PABC credential: `%s': `%s'\n",
159 key, value);
160 if (0 == strcmp (key, "expiration"))
161 return;
162 if (0 == strcmp (key, "issuer"))
163 return;
164 if (0 == strcmp (key, "subject"))
165 return;
167 key,
168 NULL,
170 value,
171 strlen (value));
172}
173
174
182static struct GNUNET_RECLAIM_AttributeList *
184 const char *data,
185 size_t data_size)
186{
187 struct GNUNET_RECLAIM_AttributeList *attrs;
188
190 "Collecting PABC attributes...\n");
192 GNUNET_assert (PABC_OK ==
193 pabc_cred_inspect_credential (data,
194 &inspect_attrs, attrs));
195 return attrs;
196}
197
198
206static struct GNUNET_RECLAIM_AttributeList *
208 const struct GNUNET_RECLAIM_Credential *cred)
209{
211 return NULL;
212 return pabc_parse_attributes (cls, cred->data, cred->data_size);
213}
214
215
223static struct GNUNET_RECLAIM_AttributeList *
225 const struct GNUNET_RECLAIM_Presentation *cred)
226{
228 return NULL;
229 return pabc_parse_attributes (cls, cred->data, cred->data_size);
230}
231
232
240static char*
242 const char *data,
243 size_t data_size)
244{
245 char *res;
246 if (PABC_OK != pabc_cred_get_attr_by_name_from_cred (data,
247 "issuer",
248 &res))
249 return NULL;
250 return res;
251}
252
253
261static char *
263 const struct GNUNET_RECLAIM_Credential *cred)
264{
266 return NULL;
267 return pabc_get_issuer (cls, cred->data, cred->data_size);
268}
269
270
278static char *
280 const struct GNUNET_RECLAIM_Presentation *cred)
281{
283 return NULL;
284 return pabc_get_issuer (cls, cred->data, cred->data_size);
285}
286
287
297 const char *data,
298 size_t data_size,
299 struct GNUNET_TIME_Absolute *exp)
300{
301 char *exp_str;
302 uint64_t exp_i;
303
304 if (PABC_OK != pabc_cred_get_attr_by_name_from_cred (data,
305 "expiration",
306 &exp_str))
307 return GNUNET_SYSERR;
308
309 if (1 != sscanf (exp_str, "%llu", &exp_i))
310 {
312 "Invalid expiration `%s'\n", exp_str);
313 GNUNET_free (exp_str);
314 return GNUNET_SYSERR;
315 }
317 "Converted expiration string `%s' to %llu",
318 exp_str, exp_i);
319
320 GNUNET_free (exp_str);
321 exp->abs_value_us = exp_i * 1000 * 1000;
322 return GNUNET_OK;
323}
324
325
335 const struct GNUNET_RECLAIM_Credential *cred,
336 struct GNUNET_TIME_Absolute *exp)
337{
339 return GNUNET_NO;
340 return pabc_get_expiration (cls, cred->data, cred->data_size, exp);
341}
342
343
353 const struct GNUNET_RECLAIM_Presentation *cred,
354 struct GNUNET_TIME_Absolute *exp)
355{
357 return GNUNET_NO;
358 return pabc_get_expiration (cls, cred->data, cred->data_size, exp);
359}
360
361
365 const struct GNUNET_RECLAIM_AttributeList *attrs,
366 struct GNUNET_RECLAIM_Presentation **presentation)
367{
368 struct pabc_context *ctx = NULL;
369 struct pabc_user_context *usr_ctx = NULL;
370 struct pabc_public_parameters *pp = NULL;
371 struct pabc_credential *cred = NULL;
372 struct pabc_blinded_proof *proof = NULL;
374 char *issuer;
375 char *subject;
376 enum pabc_status status;
377
379 return GNUNET_NO;
380
381
382 PABC_ASSERT (pabc_new_ctx (&ctx));
383 issuer = pabc_get_issuer_c (cls, credential);
384 if (NULL == issuer)
385 {
387 "No issuer found in credential\n");
388 pabc_free_ctx (&ctx);
389 return GNUNET_SYSERR;
390 }
392 "Got issuer for credential: %s\n", issuer);
393 status = PABC_load_public_parameters (ctx, issuer, &pp);
394 if (status != PABC_OK)
395 {
397 "Failed to read public parameters.\n");
398 pabc_free_ctx (&ctx);
399 GNUNET_free (issuer);
400 return GNUNET_SYSERR;
401 }
402 if (PABC_OK != pabc_cred_get_attr_by_name_from_cred (credential->data,
403 "subject",
404 &subject))
405 {
407 "Failed to get subject.\n");
408 pabc_free_ctx (&ctx);
409 GNUNET_free (issuer);
410 return GNUNET_SYSERR;
411 }
412 status = PABC_read_usr_ctx (subject, issuer, ctx, pp, &usr_ctx);
413 GNUNET_free (issuer);
415 if (PABC_OK != status)
416 {
418 "Failed to read user context.\n");
419 pabc_free_public_parameters (ctx, &pp);
420 return GNUNET_SYSERR;
421 }
422
423 status = pabc_new_credential (ctx, pp, &cred);
424 if (status != PABC_OK)
425 {
427 "Failed to allocate credential.\n");
428 pabc_free_user_context (ctx, pp, &usr_ctx);
429 pabc_free_public_parameters (ctx, &pp);
430 return GNUNET_SYSERR;
431 }
432
433 status = pabc_decode_credential (ctx, pp, cred, credential->data);
434 if (status != PABC_OK)
435 {
437 "Failed to decode credential.\n");
438 pabc_free_credential (ctx, pp, &cred);
439 pabc_free_user_context (ctx, pp, &usr_ctx);
440 pabc_free_public_parameters (ctx, &pp);
441 return GNUNET_SYSERR;
442 }
443
444 status = pabc_new_proof (ctx, pp, &proof);
445 if (status != PABC_OK)
446 {
448 "Failed to allocate proof.\n");
449 pabc_free_credential (ctx, pp, &cred);
450 pabc_free_user_context (ctx, pp, &usr_ctx);
451 pabc_free_public_parameters (ctx, &pp);
452 return GNUNET_SYSERR;
453 }
454
455 // now we can parse the attributes to disclose and configure the proof
456 for (ale = attrs->list_head; NULL != ale; ale = ale->next)
457 {
458 status = pabc_set_disclosure_by_attribute_name (ctx, pp, proof,
459 ale->attribute->name,
460 PABC_DISCLOSED, cred);
461 if (status != PABC_OK)
462 {
464 "Failed to configure proof.\n");
465 pabc_free_credential (ctx, pp, &cred);
466 pabc_free_user_context (ctx, pp, &usr_ctx);
467 pabc_free_public_parameters (ctx, &pp);
468 return GNUNET_SYSERR;
469 }
470 }
471
472 // and finally -> sign the proof
473 status = pabc_gen_proof (ctx, usr_ctx, pp, proof, cred);
474 if (status != PABC_OK)
475 {
477 "Failed to sign proof.\n");
478 pabc_free_proof (ctx, pp, &proof);
479 pabc_free_credential (ctx, pp, &cred);
480 pabc_free_user_context (ctx, pp, &usr_ctx);
481 pabc_free_public_parameters (ctx, &pp);
482 return GNUNET_SYSERR;
483 }
484 // print the result
485 char *json = NULL;
486 char *ppid = NULL;
487 char *userid = NULL;
488 GNUNET_assert (PABC_OK == pabc_cred_get_userid_from_cred (credential->data,
489 &userid));
490 GNUNET_assert (PABC_OK == pabc_cred_get_ppid_from_cred (credential->data,
491 &ppid));
492 pabc_cred_encode_proof (ctx, pp, proof, userid, ppid, &json);
493 GNUNET_free (ppid);
494 GNUNET_free (userid);
495 if (PABC_OK != status)
496 {
498 "Failed to serialize proof.\n");
499 pabc_free_proof (ctx, pp, &proof);
500 pabc_free_credential (ctx, pp, &cred);
501 pabc_free_user_context (ctx, pp, &usr_ctx);
502 pabc_free_public_parameters (ctx, &pp);
503 return GNUNET_SYSERR;
504 }
505 char *json_enc;
507 strlen (json) + 1,
508 &json_enc);
510 "Presentation: %s\n", json_enc);
511 // clean up
512 *presentation = GNUNET_RECLAIM_presentation_new (
514 json_enc,
515 strlen (json_enc) + 1);
516 GNUNET_free (json_enc);
517 PABC_FREE_NULL (json);
518 pabc_free_proof (ctx, pp, &proof);
519 pabc_free_credential (ctx, pp, &cred);
520 pabc_free_user_context (ctx, pp, &usr_ctx);
521 pabc_free_public_parameters (ctx, &pp);
522 return GNUNET_OK;
523}
524
525void *
527
534void *
536{
538
555 return api;
556}
557
558void *
560
567void *
569{
571
572 GNUNET_free (api);
573 return NULL;
574}
575
576
577/* end of plugin_reclaim_credential_type_pabc.c */
static char * subject
Subject pubkey string.
Definition: gnunet-abd.c:87
static gnutls_certificate_credentials_t cred
The credential.
static char * data
The data to insert into the dht.
struct GNUNET_HashCode key
The key used in the DHT.
static struct GNUNET_FS_Handle * ctx
static char * res
Currently read line or NULL on EOF.
static char * value
Value of the record to add/remove.
static uint32_t type
Type string converted to DNS type value.
static size_t data_size
Number of bytes in data.
static int status
The program status; 0 for success.
Definition: gnunet-nse.c:39
static struct GNUNET_RECLAIM_Identifier credential
Credential ID.
static uint64_t proof
Definition: gnunet-scrypt.c:49
commonly used definitions; globals in this file are exempt from the rule that the module name ("commo...
Plugin API for reclaim attribute types.
#define GNUNET_log(kind,...)
GNUNET_GenericReturnValue
Named constants for return values.
@ GNUNET_OK
@ GNUNET_NO
@ GNUNET_SYSERR
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_strndup(a, length)
Wrapper around GNUNET_xstrndup_.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_RECLAIM_Presentation * GNUNET_RECLAIM_presentation_new(uint32_t type, const void *data, size_t data_size)
void GNUNET_RECLAIM_attribute_list_add(struct GNUNET_RECLAIM_AttributeList *attrs, const char *attr_name, const struct GNUNET_RECLAIM_Identifier *credential, uint32_t type, const void *data, size_t data_size)
Add a new attribute to a claim list.
@ GNUNET_RECLAIM_CREDENTIAL_TYPE_PABC
libpabc credential
@ GNUNET_RECLAIM_ATTRIBUTE_TYPE_STRING
String attribute.
size_t GNUNET_STRINGS_base64_encode(const void *in, size_t len, char **output)
Encode into Base64.
Definition: strings.c:1599
enum GNUNET_GenericReturnValue PABC_load_public_parameters(struct pabc_context *const ctx, char const *const pp_name, struct pabc_public_parameters **pp)
Definition: pabc_helper.c:135
enum GNUNET_GenericReturnValue PABC_read_usr_ctx(char const *const usr_name, char const *const pp_name, struct pabc_context const *const ctx, struct pabc_public_parameters const *const pp, struct pabc_user_context **usr_ctx)
Definition: pabc_helper.c:301
static char * pabc_get_issuer(void *cls, const char *data, size_t data_size)
Parse a pabc and return the issuer.
static char * pabc_value_to_string(void *cls, uint32_t type, const void *data, size_t data_size)
Convert the 'value' of an credential to a string.
static struct GNUNET_RECLAIM_AttributeList * pabc_parse_attributes_c(void *cls, const struct GNUNET_RECLAIM_Credential *cred)
Parse a pabc and return the respective claim value as Attribute.
static const char * pabc_number_to_typename(void *cls, uint32_t type)
Convert a type number (i.e.
static struct GNUNET_RECLAIM_AttributeList * pabc_parse_attributes(void *cls, const char *data, size_t data_size)
Parse a pabc and return the respective claim value as Attribute.
static enum GNUNET_GenericReturnValue pabc_get_expiration(void *cls, const char *data, size_t data_size, struct GNUNET_TIME_Absolute *exp)
Parse a pabc and return the expiration.
void * libgnunet_plugin_reclaim_credential_pabc_done(void *cls)
Exit point from the plugin.
static uint32_t pabc_typename_to_number(void *cls, const char *pabc_typename)
Convert a type name to the corresponding number.
static char * pabc_get_issuer_p(void *cls, const struct GNUNET_RECLAIM_Presentation *cred)
Parse a pabc and return the issuer.
static void inspect_attrs(char const *const key, char const *const value, void *ctx)
const char * name
static enum GNUNET_GenericReturnValue pabc_get_expiration_p(void *cls, const struct GNUNET_RECLAIM_Presentation *cred, struct GNUNET_TIME_Absolute *exp)
Parse a pabc and return the expiration.
static struct @43 pabc_cred_name_map[]
Mapping of credential type numbers to human-readable credential type names.
void * libgnunet_plugin_reclaim_credential_pabc_init(void *cls)
Entry point for the plugin.
static struct GNUNET_RECLAIM_AttributeList * pabc_parse_attributes_p(void *cls, const struct GNUNET_RECLAIM_Presentation *cred)
Parse a pabc and return the respective claim value as Attribute.
static int pabc_string_to_value(void *cls, uint32_t type, const char *s, void **data, size_t *data_size)
Convert human-readable version of a 'value' of an credential to the binary representation.
static enum GNUNET_GenericReturnValue pabc_create_presentation(void *cls, const struct GNUNET_RECLAIM_Credential *credential, const struct GNUNET_RECLAIM_AttributeList *attrs, struct GNUNET_RECLAIM_Presentation **presentation)
static enum GNUNET_GenericReturnValue pabc_get_expiration_c(void *cls, const struct GNUNET_RECLAIM_Credential *cred, struct GNUNET_TIME_Absolute *exp)
Parse a pabc and return the expiration.
static char * pabc_get_issuer_c(void *cls, const struct GNUNET_RECLAIM_Credential *cred)
Parse a pabc and return the issuer.
struct GNUNET_RECLAIM_Attribute * attribute
The attribute claim.
struct GNUNET_RECLAIM_AttributeListEntry * next
DLL.
A list of GNUNET_RECLAIM_Attribute structures.
struct GNUNET_RECLAIM_AttributeListEntry * list_head
List head.
const char * name
The name of the attribute.
Each plugin is required to return a pointer to a struct of this type as the return value from its ent...
GNUNET_RECLAIM_CredentialValueToStringFunction value_to_string
Conversion to string.
GNUNET_RECLAIM_CredentialTypenameToNumberFunction typename_to_number
Typename to number.
GNUNET_RECLAIM_PresentationGetIssuerFunction get_issuer_p
Attesation issuer.
GNUNET_RECLAIM_CredentialNumberToTypenameFunction number_to_typename
Number to typename.
GNUNET_RECLAIM_CredentialGetIssuerFunction get_issuer
Attesation issuer.
GNUNET_RECLAIM_PresentationGetExpirationFunction get_expiration_p
Expiration.
GNUNET_RECLAIM_PresentationValueToStringFunction value_to_string_p
Conversion to string.
GNUNET_RECLAIM_CredentialStringToValueFunction string_to_value
Conversion to binary.
GNUNET_RECLAIM_CredentialGetExpirationFunction get_expiration
Expiration.
void * cls
Closure for all of the callbacks.
GNUNET_RECLAIM_CredentialGetAttributesFunction get_attributes
Attesation attributes.
GNUNET_RECLAIM_PresentationStringToValueFunction string_to_value_p
Conversion to binary.
GNUNET_RECLAIM_PresentationTypenameToNumberFunction typename_to_number_p
Typename to number.
GNUNET_RECLAIM_PresentationNumberToTypenameFunction number_to_typename_p
Number to typename.
GNUNET_RECLAIM_CredentialToPresentation create_presentation
Get presentation.
GNUNET_RECLAIM_PresentationGetAttributesFunction get_attributes_p
Attesation attributes.
A credential presentation.
Time for absolute times used by GNUnet, in microseconds.
uint64_t abs_value_us
The actual value.