plugin_reclaim_credential_pabc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2013, 2014, 2016 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file reclaim/plugin_reclaim_credential_pabc.c
  18. * @brief reclaim-credential-plugin-pabc attribute plugin to provide the API for
  19. * pabc credentials.
  20. *
  21. * @author Martin Schanzenbach
  22. */
  23. #include "platform.h"
  24. #include "gnunet_util_lib.h"
  25. #include "gnunet_reclaim_plugin.h"
  26. #include <inttypes.h>
  27. #include <jansson.h>
  28. #include <pabc/pabc.h>
  29. #include "pabc_helper.h"
  30. /**
  31. * Convert the 'value' of an credential to a string.
  32. *
  33. * @param cls closure, unused
  34. * @param type type of the credential
  35. * @param data value in binary encoding
  36. * @param data_size number of bytes in @a data
  37. * @return NULL on error, otherwise human-readable representation of the value
  38. */
  39. static char *
  40. pabc_value_to_string (void *cls,
  41. uint32_t type,
  42. const void *data,
  43. size_t data_size)
  44. {
  45. switch (type)
  46. {
  47. case GNUNET_RECLAIM_CREDENTIAL_TYPE_PABC:
  48. return GNUNET_strndup (data, data_size);
  49. default:
  50. return NULL;
  51. }
  52. }
  53. /**
  54. * Convert human-readable version of a 'value' of an credential to the binary
  55. * representation.
  56. *
  57. * @param cls closure, unused
  58. * @param type type of the credential
  59. * @param s human-readable string
  60. * @param data set to value in binary encoding (will be allocated)
  61. * @param data_size set to number of bytes in @a data
  62. * @return #GNUNET_OK on success
  63. */
  64. static int
  65. pabc_string_to_value (void *cls,
  66. uint32_t type,
  67. const char *s,
  68. void **data,
  69. size_t *data_size)
  70. {
  71. if (NULL == s)
  72. return GNUNET_SYSERR;
  73. switch (type)
  74. {
  75. case GNUNET_RECLAIM_CREDENTIAL_TYPE_PABC:
  76. *data = GNUNET_strdup (s);
  77. *data_size = strlen (s) + 1;
  78. return GNUNET_OK;
  79. default:
  80. return GNUNET_SYSERR;
  81. }
  82. }
  83. /**
  84. * Mapping of credential type numbers to human-readable
  85. * credential type names.
  86. */
  87. static struct
  88. {
  89. const char *name;
  90. uint32_t number;
  91. } pabc_cred_name_map[] = { { "PABC", GNUNET_RECLAIM_CREDENTIAL_TYPE_PABC },
  92. { NULL, UINT32_MAX } };
  93. /**
  94. * Convert a type name to the corresponding number.
  95. *
  96. * @param cls closure, unused
  97. * @param pabc_typename name to convert
  98. * @return corresponding number, UINT32_MAX on error
  99. */
  100. static uint32_t
  101. pabc_typename_to_number (void *cls, const char *pabc_typename)
  102. {
  103. unsigned int i;
  104. i = 0;
  105. while ((NULL != pabc_cred_name_map[i].name) &&
  106. (0 != strcasecmp (pabc_typename, pabc_cred_name_map[i].name)))
  107. i++;
  108. return pabc_cred_name_map[i].number;
  109. }
  110. /**
  111. * Convert a type number (i.e. 1) to the corresponding type string
  112. *
  113. * @param cls closure, unused
  114. * @param type number of a type to convert
  115. * @return corresponding typestring, NULL on error
  116. */
  117. static const char *
  118. pabc_number_to_typename (void *cls, uint32_t type)
  119. {
  120. unsigned int i;
  121. i = 0;
  122. while ((NULL != pabc_cred_name_map[i].name) && (type !=
  123. pabc_cred_name_map[i].
  124. number))
  125. i++;
  126. return pabc_cred_name_map[i].name;
  127. }
  128. static void
  129. inspect_attrs (char const *const key,
  130. char const *const value,
  131. void *ctx)
  132. {
  133. struct GNUNET_RECLAIM_AttributeList *attrs = ctx;
  134. if (NULL == value)
  135. return;
  136. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  137. "Found attribue in PABC credential: `%s': `%s'\n",
  138. key, value);
  139. if (0 == strcmp (key, "expiration"))
  140. return;
  141. if (0 == strcmp (key, "issuer"))
  142. return;
  143. if (0 == strcmp (key, "subject"))
  144. return;
  145. GNUNET_RECLAIM_attribute_list_add (attrs,
  146. key,
  147. NULL,
  148. GNUNET_RECLAIM_ATTRIBUTE_TYPE_STRING,
  149. value,
  150. strlen (value));
  151. }
  152. /**
  153. * Parse a pabc and return the respective claim value as Attribute
  154. *
  155. * @param cls the plugin
  156. * @param cred the pabc credential
  157. * @return a GNUNET_RECLAIM_Attribute, containing the new value
  158. */
  159. struct GNUNET_RECLAIM_AttributeList *
  160. pabc_parse_attributes (void *cls,
  161. const char *data,
  162. size_t data_size)
  163. {
  164. struct GNUNET_RECLAIM_AttributeList *attrs;
  165. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  166. "Collecting PABC attributes...\n");
  167. attrs = GNUNET_new (struct GNUNET_RECLAIM_AttributeList);
  168. GNUNET_assert (PABC_OK ==
  169. pabc_cred_inspect_credential (data,
  170. &inspect_attrs, attrs));
  171. return attrs;
  172. }
  173. /**
  174. * Parse a pabc and return the respective claim value as Attribute
  175. *
  176. * @param cls the plugin
  177. * @param cred the pabc credential
  178. * @return a GNUNET_RECLAIM_Attribute, containing the new value
  179. */
  180. struct GNUNET_RECLAIM_AttributeList *
  181. pabc_parse_attributes_c (void *cls,
  182. const struct GNUNET_RECLAIM_Credential *cred)
  183. {
  184. if (cred->type != GNUNET_RECLAIM_CREDENTIAL_TYPE_PABC)
  185. return NULL;
  186. return pabc_parse_attributes (cls, cred->data, cred->data_size);
  187. }
  188. /**
  189. * Parse a pabc and return the respective claim value as Attribute
  190. *
  191. * @param cls the plugin
  192. * @param cred the pabc credential
  193. * @return a GNUNET_RECLAIM_Attribute, containing the new value
  194. */
  195. struct GNUNET_RECLAIM_AttributeList *
  196. pabc_parse_attributes_p (void *cls,
  197. const struct GNUNET_RECLAIM_Presentation *cred)
  198. {
  199. if (cred->type != GNUNET_RECLAIM_CREDENTIAL_TYPE_PABC)
  200. return NULL;
  201. return pabc_parse_attributes (cls, cred->data, cred->data_size);
  202. }
  203. /**
  204. * Parse a pabc and return the issuer
  205. *
  206. * @param cls the plugin
  207. * @param cred the pabc credential
  208. * @return a string, containing the isser
  209. */
  210. char*
  211. pabc_get_issuer (void *cls,
  212. const char *data,
  213. size_t data_size)
  214. {
  215. char *res;
  216. if (PABC_OK != pabc_cred_get_attr_by_name_from_cred (data,
  217. "issuer",
  218. &res))
  219. return NULL;
  220. return res;
  221. }
  222. /**
  223. * Parse a pabc and return the issuer
  224. *
  225. * @param cls the plugin
  226. * @param cred the pabc credential
  227. * @return a string, containing the isser
  228. */
  229. char *
  230. pabc_get_issuer_c (void *cls,
  231. const struct GNUNET_RECLAIM_Credential *cred)
  232. {
  233. if (GNUNET_RECLAIM_CREDENTIAL_TYPE_PABC != cred->type)
  234. return NULL;
  235. return pabc_get_issuer (cls, cred->data, cred->data_size);
  236. }
  237. /**
  238. * Parse a pabc and return the issuer
  239. *
  240. * @param cls the plugin
  241. * @param cred the pabc credential
  242. * @return a string, containing the isser
  243. */
  244. char *
  245. pabc_get_issuer_p (void *cls,
  246. const struct GNUNET_RECLAIM_Presentation *cred)
  247. {
  248. if (GNUNET_RECLAIM_CREDENTIAL_TYPE_PABC != cred->type)
  249. return NULL;
  250. return pabc_get_issuer (cls, cred->data, cred->data_size);
  251. }
  252. /**
  253. * Parse a pabc and return the expiration
  254. *
  255. * @param cls the plugin
  256. * @param cred the pabc credential
  257. * @return a string, containing the isser
  258. */
  259. int
  260. pabc_get_expiration (void *cls,
  261. const char *data,
  262. size_t data_size,
  263. struct GNUNET_TIME_Absolute *exp)
  264. {
  265. uint64_t exp_i;
  266. char *exp_str;
  267. if (PABC_OK != pabc_cred_get_attr_by_name_from_cred (data,
  268. "expiration",
  269. &exp_str))
  270. {
  271. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  272. "Unable to retrive expiration from credential\n");
  273. return GNUNET_SYSERR;
  274. }
  275. if (1 != sscanf (exp_str, "%llu", &exp_i))
  276. {
  277. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  278. "Invalid expiration `%s'\n", exp_str);
  279. GNUNET_free (exp_str);
  280. return GNUNET_SYSERR;
  281. }
  282. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  283. "Converted expiration string `%s' to %llu",
  284. exp_str, exp_i);
  285. GNUNET_free (exp_str);
  286. exp->abs_value_us = exp_i * 1000 * 1000;
  287. return GNUNET_OK;
  288. }
  289. /**
  290. * Parse a pabc and return the expiration
  291. *
  292. * @param cls the plugin
  293. * @param cred the pabc credential
  294. * @return a string, containing the isser
  295. */
  296. enum GNUNET_GenericReturnValue
  297. pabc_get_expiration_c (void *cls,
  298. const struct GNUNET_RECLAIM_Credential *cred,
  299. struct GNUNET_TIME_Absolute *exp)
  300. {
  301. if (cred->type != GNUNET_RECLAIM_CREDENTIAL_TYPE_PABC)
  302. return GNUNET_NO;
  303. return pabc_get_expiration (cls, cred->data, cred->data_size, exp);
  304. }
  305. /**
  306. * Parse a pabc and return the expiration
  307. *
  308. * @param cls the plugin
  309. * @param cred the pabc credential
  310. * @return a string, containing the isser
  311. */
  312. enum GNUNET_GenericReturnValue
  313. pabc_get_expiration_p (void *cls,
  314. const struct GNUNET_RECLAIM_Presentation *cred,
  315. struct GNUNET_TIME_Absolute *exp)
  316. {
  317. if (cred->type != GNUNET_RECLAIM_CREDENTIAL_TYPE_PABC)
  318. return GNUNET_NO;
  319. return pabc_get_expiration (cls, cred->data, cred->data_size, exp);
  320. }
  321. int
  322. pabc_create_presentation (void *cls,
  323. const struct GNUNET_RECLAIM_Credential *credential,
  324. const struct GNUNET_RECLAIM_AttributeList *attrs,
  325. struct GNUNET_RECLAIM_Presentation **pres)
  326. {
  327. struct pabc_context *ctx = NULL;
  328. struct pabc_user_context *usr_ctx = NULL;
  329. struct pabc_public_parameters *pp = NULL;
  330. struct pabc_credential *cred = NULL;
  331. struct pabc_blinded_proof *proof = NULL;
  332. struct GNUNET_RECLAIM_AttributeListEntry *ale;
  333. char *issuer;
  334. char *subject;
  335. enum pabc_status status;
  336. if (GNUNET_RECLAIM_CREDENTIAL_TYPE_PABC != credential->type)
  337. return GNUNET_NO;
  338. PABC_ASSERT (pabc_new_ctx (&ctx));
  339. issuer = pabc_get_issuer_c (cls, credential);
  340. if (NULL == issuer)
  341. {
  342. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  343. "No issuer found in credential\n");
  344. pabc_free_ctx (&ctx);
  345. return GNUNET_SYSERR;
  346. }
  347. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  348. "Got issuer for credential: %s\n", issuer);
  349. status = PABC_load_public_parameters (ctx, issuer, &pp);
  350. if (status != PABC_OK)
  351. {
  352. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  353. "Failed to read public parameters.\n");
  354. pabc_free_ctx (&ctx);
  355. GNUNET_free (issuer);
  356. return GNUNET_SYSERR;
  357. }
  358. if (PABC_OK != pabc_cred_get_attr_by_name_from_cred (credential->data,
  359. "subject",
  360. &subject))
  361. {
  362. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  363. "Failed to get subject.\n");
  364. pabc_free_ctx (&ctx);
  365. GNUNET_free (issuer);
  366. return GNUNET_SYSERR;
  367. }
  368. status = PABC_read_usr_ctx (subject, issuer, ctx, pp, &usr_ctx);
  369. GNUNET_free (issuer);
  370. GNUNET_free (subject);
  371. if (PABC_OK != status)
  372. {
  373. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  374. "Failed to read user context.\n");
  375. pabc_free_public_parameters (ctx, &pp);
  376. return GNUNET_SYSERR;
  377. }
  378. status = pabc_new_credential (ctx, pp, &cred);
  379. if (status != PABC_OK)
  380. {
  381. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  382. "Failed to allocate credential.\n");
  383. pabc_free_user_context (ctx, pp, &usr_ctx);
  384. pabc_free_public_parameters (ctx, &pp);
  385. return GNUNET_SYSERR;
  386. }
  387. status = pabc_decode_credential (ctx, pp, cred, credential->data);
  388. if (status != PABC_OK)
  389. {
  390. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  391. "Failed to decode credential.\n");
  392. pabc_free_credential (ctx, pp, &cred);
  393. pabc_free_user_context (ctx, pp, &usr_ctx);
  394. pabc_free_public_parameters (ctx, &pp);
  395. return GNUNET_SYSERR;
  396. }
  397. status = pabc_new_proof (ctx, pp, &proof);
  398. if (status != PABC_OK)
  399. {
  400. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  401. "Failed to allocate proof.\n");
  402. pabc_free_credential (ctx, pp, &cred);
  403. pabc_free_user_context (ctx, pp, &usr_ctx);
  404. pabc_free_public_parameters (ctx, &pp);
  405. return GNUNET_SYSERR;
  406. }
  407. // now we can parse the attributes to disclose and configure the proof
  408. for (ale = attrs->list_head; NULL != ale; ale = ale->next)
  409. {
  410. status = pabc_set_disclosure_by_attribute_name (ctx, pp, proof,
  411. ale->attribute->name,
  412. PABC_DISCLOSED, cred);
  413. if (status != PABC_OK)
  414. {
  415. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  416. "Failed to configure proof.\n");
  417. pabc_free_credential (ctx, pp, &cred);
  418. pabc_free_user_context (ctx, pp, &usr_ctx);
  419. pabc_free_public_parameters (ctx, &pp);
  420. return GNUNET_SYSERR;
  421. }
  422. }
  423. // and finally -> sign the proof
  424. status = pabc_gen_proof (ctx, usr_ctx, pp, proof, cred);
  425. if (status != PABC_OK)
  426. {
  427. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  428. "Failed to sign proof.\n");
  429. pabc_free_proof (ctx, pp, &proof);
  430. pabc_free_credential (ctx, pp, &cred);
  431. pabc_free_user_context (ctx, pp, &usr_ctx);
  432. pabc_free_public_parameters (ctx, &pp);
  433. return GNUNET_SYSERR;
  434. }
  435. // print the result
  436. char *json = NULL;
  437. char *ppid = NULL;
  438. char *userid = NULL;
  439. GNUNET_assert (PABC_OK == pabc_cred_get_userid_from_cred (credential->data,
  440. &userid));
  441. GNUNET_assert (PABC_OK == pabc_cred_get_ppid_from_cred (credential->data,
  442. &ppid));
  443. pabc_cred_encode_proof (ctx, pp, proof, userid, ppid, &json);
  444. GNUNET_free (ppid);
  445. GNUNET_free (userid);
  446. if (PABC_OK != status)
  447. {
  448. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  449. "Failed to serialize proof.\n");
  450. pabc_free_proof (ctx, pp, &proof);
  451. pabc_free_credential (ctx, pp, &cred);
  452. pabc_free_user_context (ctx, pp, &usr_ctx);
  453. pabc_free_public_parameters (ctx, &pp);
  454. return GNUNET_SYSERR;
  455. }
  456. char *json_enc;
  457. GNUNET_STRINGS_base64_encode (json,
  458. strlen (json) + 1,
  459. &json_enc);
  460. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  461. "Presentation: %s\n", json_enc);
  462. // clean up
  463. *pres = GNUNET_RECLAIM_presentation_new (GNUNET_RECLAIM_CREDENTIAL_TYPE_PABC,
  464. json_enc,
  465. strlen (json_enc) + 1);
  466. GNUNET_free (json_enc);
  467. PABC_FREE_NULL (json);
  468. pabc_free_proof (ctx, pp, &proof);
  469. pabc_free_credential (ctx, pp, &cred);
  470. pabc_free_user_context (ctx, pp, &usr_ctx);
  471. pabc_free_public_parameters (ctx, &pp);
  472. return GNUNET_OK;
  473. }
  474. /**
  475. * Entry point for the plugin.
  476. *
  477. * @param cls NULL
  478. * @return the exported block API
  479. */
  480. void *
  481. libgnunet_plugin_reclaim_credential_pabc_init (void *cls)
  482. {
  483. struct GNUNET_RECLAIM_CredentialPluginFunctions *api;
  484. api = GNUNET_new (struct GNUNET_RECLAIM_CredentialPluginFunctions);
  485. api->value_to_string = &pabc_value_to_string;
  486. api->string_to_value = &pabc_string_to_value;
  487. api->typename_to_number = &pabc_typename_to_number;
  488. api->number_to_typename = &pabc_number_to_typename;
  489. api->get_attributes = &pabc_parse_attributes_c;
  490. api->get_issuer = &pabc_get_issuer_c;
  491. api->get_expiration = &pabc_get_expiration_c;
  492. api->value_to_string_p = &pabc_value_to_string;
  493. api->string_to_value_p = &pabc_string_to_value;
  494. api->typename_to_number_p = &pabc_typename_to_number;
  495. api->number_to_typename_p = &pabc_number_to_typename;
  496. api->get_attributes_p = &pabc_parse_attributes_p;
  497. api->get_issuer_p = &pabc_get_issuer_p;
  498. api->get_expiration_p = &pabc_get_expiration_p;
  499. api->create_presentation = &pabc_create_presentation;
  500. return api;
  501. }
  502. /**
  503. * Exit point from the plugin.
  504. *
  505. * @param cls the return value from #libgnunet_plugin_block_test_init()
  506. * @return NULL
  507. */
  508. void *
  509. libgnunet_plugin_reclaim_credential_pabc_done (void *cls)
  510. {
  511. struct GNUNET_RECLAIM_CredentialPluginFunctions *api = cls;
  512. GNUNET_free (api);
  513. return NULL;
  514. }
  515. /* end of plugin_reclaim_credential_type_pabc.c */