aboutsummaryrefslogtreecommitdiff
path: root/src/reclaim/plugin_reclaim_credential_pabc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/reclaim/plugin_reclaim_credential_pabc.c')
-rw-r--r--src/reclaim/plugin_reclaim_credential_pabc.c498
1 files changed, 498 insertions, 0 deletions
diff --git a/src/reclaim/plugin_reclaim_credential_pabc.c b/src/reclaim/plugin_reclaim_credential_pabc.c
new file mode 100644
index 000000000..765652355
--- /dev/null
+++ b/src/reclaim/plugin_reclaim_credential_pabc.c
@@ -0,0 +1,498 @@
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
21/**
22 * @file reclaim/plugin_reclaim_credential_pabc.c
23 * @brief reclaim-credential-plugin-pabc attribute plugin to provide the API for
24 * pabc credentials.
25 *
26 * @author Martin Schanzenbach
27 */
28#include "platform.h"
29#include "gnunet_util_lib.h"
30#include "gnunet_reclaim_plugin.h"
31#include <inttypes.h>
32#include <jansson.h>
33#include <libpabc/libpabc.h>
34
35/**
36 * Convert the 'value' of an credential to a string.
37 *
38 * @param cls closure, unused
39 * @param type type of the credential
40 * @param data value in binary encoding
41 * @param data_size number of bytes in @a data
42 * @return NULL on error, otherwise human-readable representation of the value
43 */
44static char *
45pabc_value_to_string (void *cls,
46 uint32_t type,
47 const void *data,
48 size_t data_size)
49{
50 switch (type)
51 {
52 case GNUNET_RECLAIM_CREDENTIAL_TYPE_PABC:
53 return GNUNET_strndup (data, data_size);
54
55 default:
56 return NULL;
57 }
58}
59
60
61/**
62 * Convert human-readable version of a 'value' of an credential to the binary
63 * representation.
64 *
65 * @param cls closure, unused
66 * @param type type of the credential
67 * @param s human-readable string
68 * @param data set to value in binary encoding (will be allocated)
69 * @param data_size set to number of bytes in @a data
70 * @return #GNUNET_OK on success
71 */
72static int
73pabc_string_to_value (void *cls,
74 uint32_t type,
75 const char *s,
76 void **data,
77 size_t *data_size)
78{
79 if (NULL == s)
80 return GNUNET_SYSERR;
81 switch (type)
82 {
83 case GNUNET_RECLAIM_CREDENTIAL_TYPE_PABC:
84 *data = GNUNET_strdup (s);
85 *data_size = strlen (s) + 1;
86 return GNUNET_OK;
87
88 default:
89 return GNUNET_SYSERR;
90 }
91}
92
93
94/**
95 * Mapping of credential type numbers to human-readable
96 * credential type names.
97 */
98static struct
99{
100 const char *name;
101 uint32_t number;
102} pabc_cred_name_map[] = { { "pabc", GNUNET_RECLAIM_CREDENTIAL_TYPE_PABC },
103 { NULL, UINT32_MAX } };
104
105/**
106 * Convert a type name to the corresponding number.
107 *
108 * @param cls closure, unused
109 * @param pabc_typename name to convert
110 * @return corresponding number, UINT32_MAX on error
111 */
112static uint32_t
113pabc_typename_to_number (void *cls, const char *pabc_typename)
114{
115 unsigned int i;
116
117 i = 0;
118 while ((NULL != pabc_cred_name_map[i].name) &&
119 (0 != strcasecmp (pabc_typename, pabc_cred_name_map[i].name)))
120 i++;
121 return pabc_cred_name_map[i].number;
122}
123
124
125/**
126 * Convert a type number (i.e. 1) to the corresponding type string
127 *
128 * @param cls closure, unused
129 * @param type number of a type to convert
130 * @return corresponding typestring, NULL on error
131 */
132static const char *
133pabc_number_to_typename (void *cls, uint32_t type)
134{
135 unsigned int i;
136
137 i = 0;
138 while ((NULL != pabc_cred_name_map[i].name) && (type !=
139 pabc_cred_name_map[i].
140 number))
141 i++;
142 return pabc_cred_name_map[i].name;
143}
144
145
146/**
147 * Parse a pabc and return the respective claim value as Attribute
148 *
149 * @param cls the plugin
150 * @param cred the pabc credential
151 * @return a GNUNET_RECLAIM_Attribute, containing the new value
152 */
153struct GNUNET_RECLAIM_AttributeList *
154pabc_parse_attributes (void *cls,
155 const char *data,
156 size_t data_size)
157{
158 char *pabc_string;
159 struct GNUNET_RECLAIM_AttributeList *attrs;
160 char delim[] = ".";
161 char *val_str = NULL;
162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Parsing pabc attributes.\n");
163 char *decoded_pabc;
164 char *tmp;
165 json_t *json_val;
166 json_error_t *json_err = NULL;
167
168 attrs = GNUNET_new (struct GNUNET_RECLAIM_AttributeList);
169
170 pabc_string = GNUNET_strndup (data, data_size);
171 const char *pabc_body = strtok (pabc_string, delim);
172 pabc_body = strtok (NULL, delim);
173 GNUNET_STRINGS_base64url_decode (pabc_body, strlen (pabc_body),
174 (void **) &decoded_pabc);
175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Decoded pabc: %s\n", decoded_pabc);
176 GNUNET_assert (NULL != decoded_pabc);
177 json_val = json_loads (decoded_pabc, JSON_DECODE_ANY, json_err);
178 GNUNET_free (decoded_pabc);
179 const char *key;
180 const char *addr_key;
181 json_t *value;
182 json_t *addr_value;
183
184 json_object_foreach (json_val, key, value) {
185 if (0 == strcmp ("iss", key))
186 continue;
187 if (0 == strcmp ("jti", key))
188 continue;
189 if (0 == strcmp ("exp", key))
190 continue;
191 if (0 == strcmp ("iat", key))
192 continue;
193 if (0 == strcmp ("nbf", key))
194 continue;
195 if (0 == strcmp ("aud", key))
196 continue;
197 if (0 == strcmp ("address", key))
198 {
199 if (!json_is_object(value)) {
200 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
201 "address claim in wrong format!");
202 continue;
203 }
204 json_object_foreach (value, addr_key, addr_value) {
205 val_str = json_dumps (addr_value, JSON_ENCODE_ANY);
206 tmp = val_str;
207 //Remove leading " from jasson conversion
208 if (tmp[0] == '"')
209 tmp++;
210 //Remove trailing " from jansson conversion
211 if (tmp[strlen(tmp)-1] == '"')
212 tmp[strlen(tmp)-1] = '\0';
213 GNUNET_RECLAIM_attribute_list_add (attrs,
214 addr_key,
215 NULL,
216 GNUNET_RECLAIM_ATTRIBUTE_TYPE_STRING,
217 tmp,
218 strlen (val_str));
219 GNUNET_free (val_str);
220 }
221 continue;
222 }
223 val_str = json_dumps (value, JSON_ENCODE_ANY);
224 tmp = val_str;
225 //Remove leading " from jasson conversion
226 if (tmp[0] == '"')
227 tmp++;
228 //Remove trailing " from jansson conversion
229 if (tmp[strlen(tmp)-1] == '"')
230 tmp[strlen(tmp)-1] = '\0';
231 GNUNET_RECLAIM_attribute_list_add (attrs,
232 key,
233 NULL,
234 GNUNET_RECLAIM_ATTRIBUTE_TYPE_STRING,// FIXME
235 tmp,
236 strlen (val_str));
237 GNUNET_free (val_str);
238 }
239 json_decref (json_val);
240 GNUNET_free (pabc_string);
241 return attrs;
242}
243
244
245/**
246 * Parse a pabc and return the respective claim value as Attribute
247 *
248 * @param cls the plugin
249 * @param cred the pabc credential
250 * @return a GNUNET_RECLAIM_Attribute, containing the new value
251 */
252struct GNUNET_RECLAIM_AttributeList *
253pabc_parse_attributes_c (void *cls,
254 const struct GNUNET_RECLAIM_Credential *cred)
255{
256 return pabc_parse_attributes (cls, cred->data, cred->data_size);
257}
258
259
260/**
261 * Parse a pabc and return the respective claim value as Attribute
262 *
263 * @param cls the plugin
264 * @param cred the pabc credential
265 * @return a GNUNET_RECLAIM_Attribute, containing the new value
266 */
267struct GNUNET_RECLAIM_AttributeList *
268pabc_parse_attributes_p (void *cls,
269 const struct GNUNET_RECLAIM_Presentation *cred)
270{
271 return pabc_parse_attributes (cls, cred->data, cred->data_size);
272}
273
274
275/**
276 * Parse a pabc and return the issuer
277 *
278 * @param cls the plugin
279 * @param cred the pabc credential
280 * @return a string, containing the isser
281 */
282char *
283pabc_get_issuer (void *cls,
284 const char *data,
285 size_t data_size)
286{
287 const char *pabc_body;
288 char *pabc_string;
289 char delim[] = ".";
290 char *issuer = NULL;
291 char *decoded_pabc;
292 json_t *issuer_json;
293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Parsing pabc attributes.\n");
294 json_t *json_val;
295 json_error_t *json_err = NULL;
296
297 pabc_string = GNUNET_strndup (data, data_size);
298 pabc_body = strtok (pabc_string, delim);
299 pabc_body = strtok (NULL, delim);
300 GNUNET_STRINGS_base64url_decode (pabc_body, strlen (pabc_body),
301 (void **) &decoded_pabc);
302 json_val = json_loads (decoded_pabc, JSON_DECODE_ANY, json_err);
303 GNUNET_free (decoded_pabc);
304 GNUNET_free (pabc_string);
305 if (NULL == json_val)
306 return NULL;
307 issuer_json = json_object_get (json_val, "iss");
308 if ((NULL == issuer_json) || (! json_is_string (issuer_json))) {
309 json_decref (json_val);
310 return NULL;
311 }
312 issuer = GNUNET_strdup (json_string_value (issuer_json));
313 json_decref (json_val);
314 return issuer;
315}
316
317
318/**
319 * Parse a pabc and return the issuer
320 *
321 * @param cls the plugin
322 * @param cred the pabc credential
323 * @return a string, containing the isser
324 */
325char *
326pabc_get_issuer_c (void *cls,
327 const struct GNUNET_RECLAIM_Credential *cred)
328{
329 if (GNUNET_RECLAIM_CREDENTIAL_TYPE_PABC != cred->type)
330 return NULL;
331 return pabc_get_issuer (cls, cred->data, cred->data_size);
332}
333
334
335/**
336 * Parse a pabc and return the issuer
337 *
338 * @param cls the plugin
339 * @param cred the pabc credential
340 * @return a string, containing the isser
341 */
342char *
343pabc_get_issuer_p (void *cls,
344 const struct GNUNET_RECLAIM_Presentation *cred)
345{
346 if (GNUNET_RECLAIM_CREDENTIAL_TYPE_PABC != cred->type)
347 return NULL;
348 return pabc_get_issuer (cls, cred->data, cred->data_size);
349}
350
351
352/**
353 * Parse a pabc and return the expiration
354 *
355 * @param cls the plugin
356 * @param cred the pabc credential
357 * @return a string, containing the isser
358 */
359int
360pabc_get_expiration (void *cls,
361 const char *data,
362 size_t data_size,
363 struct GNUNET_TIME_Absolute *exp)
364{
365 const char *pabc_body;
366 char *pabc_string;
367 char delim[] = ".";
368 char *decoded_pabc;
369 json_t *exp_json;
370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Parsing pabc attributes.\n");
371 json_t *json_val;
372 json_error_t *json_err = NULL;
373
374 pabc_string = GNUNET_strndup (data, data_size);
375 pabc_body = strtok (pabc_string, delim);
376 pabc_body = strtok (NULL, delim);
377 GNUNET_STRINGS_base64url_decode (pabc_body, strlen (pabc_body),
378 (void **) &decoded_pabc);
379 json_val = json_loads (decoded_pabc, JSON_DECODE_ANY, json_err);
380 GNUNET_free (decoded_pabc);
381 GNUNET_free (pabc_string);
382 if (NULL == json_val)
383 return GNUNET_SYSERR;
384 exp_json = json_object_get (json_val, "exp");
385 if ((NULL == exp_json) || (! json_is_integer (exp_json))) {
386 json_decref (json_val);
387 return GNUNET_SYSERR;
388 }
389 exp->abs_value_us = json_integer_value (exp_json) * 1000 * 1000;
390 json_decref (json_val);
391 return GNUNET_OK;
392}
393
394
395/**
396 * Parse a pabc and return the expiration
397 *
398 * @param cls the plugin
399 * @param cred the pabc credential
400 * @return a string, containing the isser
401 */
402int
403pabc_get_expiration_c (void *cls,
404 const struct GNUNET_RECLAIM_Credential *cred,
405 struct GNUNET_TIME_Absolute *exp)
406{
407 return pabc_get_expiration (cls, cred->data, cred->data_size, exp);
408}
409
410
411/**
412 * Parse a pabc and return the expiration
413 *
414 * @param cls the plugin
415 * @param cred the pabc credential
416 * @return a string, containing the isser
417 */
418int
419pabc_get_expiration_p (void *cls,
420 const struct GNUNET_RECLAIM_Presentation *cred,
421 struct GNUNET_TIME_Absolute *exp)
422{
423 return pabc_get_expiration (cls, cred->data, cred->data_size, exp);
424}
425
426
427int
428pabc_create_presentation (void *cls,
429 const struct GNUNET_RECLAIM_Credential *cred,
430 const struct GNUNET_RECLAIM_AttributeList *attrs,
431 struct GNUNET_RECLAIM_Presentation **pres)
432{
433 // FIXME sanity checks??
434 if (GNUNET_RECLAIM_CREDENTIAL_TYPE_PABC != cred->type)
435 return GNUNET_NO;
436 *pres = GNUNET_RECLAIM_presentation_new (GNUNET_RECLAIM_CREDENTIAL_TYPE_PABC,
437 cred->data,
438 cred->data_size);
439 return GNUNET_OK;
440}
441
442
443/**
444 * Entry point for the plugin.
445 *
446 * @param cls NULL
447 * @return the exported block API
448 */
449void *
450libgnunet_plugin_reclaim_credential_pabc_init (void *cls)
451{
452 struct GNUNET_RECLAIM_CredentialPluginFunctions *api;
453 struct pabc_context *ctx;
454
455 if (PABC_OK != pabc_new_ctx (&ctx))
456 {
457 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
458 "Unable to initialize pabc context\n");
459 return NULL;
460 }
461
462 api = GNUNET_new (struct GNUNET_RECLAIM_CredentialPluginFunctions);
463 api->value_to_string = &pabc_value_to_string;
464 api->string_to_value = &pabc_string_to_value;
465 api->typename_to_number = &pabc_typename_to_number;
466 api->number_to_typename = &pabc_number_to_typename;
467 api->get_attributes = &pabc_parse_attributes_c;
468 api->get_issuer = &pabc_get_issuer_c;
469 api->get_expiration = &pabc_get_expiration_c;
470 api->value_to_string_p = &pabc_value_to_string;
471 api->string_to_value_p = &pabc_string_to_value;
472 api->typename_to_number_p = &pabc_typename_to_number;
473 api->number_to_typename_p = &pabc_number_to_typename;
474 api->get_attributes_p = &pabc_parse_attributes_p;
475 api->get_issuer_p = &pabc_get_issuer_p;
476 api->get_expiration_p = &pabc_get_expiration_p;
477 api->create_presentation = &pabc_create_presentation;
478 return api;
479}
480
481
482/**
483 * Exit point from the plugin.
484 *
485 * @param cls the return value from #libgnunet_plugin_block_test_init()
486 * @return NULL
487 */
488void *
489libgnunet_plugin_reclaim_credential_pabc_done (void *cls)
490{
491 struct GNUNET_RECLAIM_CredentialPluginFunctions *api = cls;
492
493 GNUNET_free (api);
494 return NULL;
495}
496
497
498/* end of plugin_reclaim_credential_type_pabc.c */