plugin_reclaim_credential_jwt.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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_jwt.c
  18. * @brief reclaim-credential-plugin-jwt attribute plugin to provide the API for
  19. * JWT 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. /**
  29. * Convert the 'value' of an credential to a string.
  30. *
  31. * @param cls closure, unused
  32. * @param type type of the credential
  33. * @param data value in binary encoding
  34. * @param data_size number of bytes in @a data
  35. * @return NULL on error, otherwise human-readable representation of the value
  36. */
  37. static char *
  38. jwt_value_to_string (void *cls,
  39. uint32_t type,
  40. const void *data,
  41. size_t data_size)
  42. {
  43. switch (type)
  44. {
  45. case GNUNET_RECLAIM_CREDENTIAL_TYPE_JWT:
  46. return GNUNET_strndup (data, data_size);
  47. default:
  48. return NULL;
  49. }
  50. }
  51. /**
  52. * Convert human-readable version of a 'value' of an credential to the binary
  53. * representation.
  54. *
  55. * @param cls closure, unused
  56. * @param type type of the credential
  57. * @param s human-readable string
  58. * @param data set to value in binary encoding (will be allocated)
  59. * @param data_size set to number of bytes in @a data
  60. * @return #GNUNET_OK on success
  61. */
  62. static int
  63. jwt_string_to_value (void *cls,
  64. uint32_t type,
  65. const char *s,
  66. void **data,
  67. size_t *data_size)
  68. {
  69. if (NULL == s)
  70. return GNUNET_SYSERR;
  71. switch (type)
  72. {
  73. case GNUNET_RECLAIM_CREDENTIAL_TYPE_JWT:
  74. *data = GNUNET_strdup (s);
  75. *data_size = strlen (s) + 1;
  76. return GNUNET_OK;
  77. default:
  78. return GNUNET_SYSERR;
  79. }
  80. }
  81. /**
  82. * Mapping of credential type numbers to human-readable
  83. * credential type names.
  84. */
  85. static struct
  86. {
  87. const char *name;
  88. uint32_t number;
  89. } jwt_cred_name_map[] = { { "JWT", GNUNET_RECLAIM_CREDENTIAL_TYPE_JWT },
  90. { NULL, UINT32_MAX } };
  91. /**
  92. * Convert a type name to the corresponding number.
  93. *
  94. * @param cls closure, unused
  95. * @param jwt_typename name to convert
  96. * @return corresponding number, UINT32_MAX on error
  97. */
  98. static uint32_t
  99. jwt_typename_to_number (void *cls, const char *jwt_typename)
  100. {
  101. unsigned int i;
  102. i = 0;
  103. while ((NULL != jwt_cred_name_map[i].name) &&
  104. (0 != strcasecmp (jwt_typename, jwt_cred_name_map[i].name)))
  105. i++;
  106. return jwt_cred_name_map[i].number;
  107. }
  108. /**
  109. * Convert a type number to the corresponding type string (e.g. 1 to "A")
  110. *
  111. * @param cls closure, unused
  112. * @param type number of a type to convert
  113. * @return corresponding typestring, NULL on error
  114. */
  115. static const char *
  116. jwt_number_to_typename (void *cls, uint32_t type)
  117. {
  118. unsigned int i;
  119. i = 0;
  120. while ((NULL != jwt_cred_name_map[i].name) && (type !=
  121. jwt_cred_name_map[i].
  122. number))
  123. i++;
  124. return jwt_cred_name_map[i].name;
  125. }
  126. /**
  127. * Parse a JWT and return the respective claim value as Attribute
  128. *
  129. * @param cls the plugin
  130. * @param cred the jwt credential
  131. * @return a GNUNET_RECLAIM_Attribute, containing the new value
  132. */
  133. struct GNUNET_RECLAIM_AttributeList *
  134. jwt_parse_attributes (void *cls,
  135. const char *data,
  136. size_t data_size)
  137. {
  138. char *jwt_string;
  139. struct GNUNET_RECLAIM_AttributeList *attrs;
  140. char delim[] = ".";
  141. char *val_str = NULL;
  142. char *decoded_jwt;
  143. char *tmp;
  144. json_t *json_val;
  145. json_error_t json_err;
  146. attrs = GNUNET_new (struct GNUNET_RECLAIM_AttributeList);
  147. jwt_string = GNUNET_strndup (data, data_size);
  148. const char *jwt_body = strtok (jwt_string, delim);
  149. jwt_body = strtok (NULL, delim);
  150. GNUNET_STRINGS_base64url_decode (jwt_body, strlen (jwt_body),
  151. (void **) &decoded_jwt);
  152. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Decoded JWT: %s\n", decoded_jwt);
  153. GNUNET_assert (NULL != decoded_jwt);
  154. json_val = json_loads (decoded_jwt, JSON_DECODE_ANY, &json_err);
  155. GNUNET_free (decoded_jwt);
  156. const char *key;
  157. const char *addr_key;
  158. json_t *value;
  159. json_t *addr_value;
  160. json_object_foreach (json_val, key, value) {
  161. if (0 == strcmp ("iss", key))
  162. continue;
  163. if (0 == strcmp ("jti", key))
  164. continue;
  165. if (0 == strcmp ("exp", key))
  166. continue;
  167. if (0 == strcmp ("iat", key))
  168. continue;
  169. if (0 == strcmp ("nbf", key))
  170. continue;
  171. if (0 == strcmp ("aud", key))
  172. continue;
  173. if (0 == strcmp ("address", key))
  174. {
  175. if (! json_is_object (value))
  176. {
  177. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  178. "address claim in wrong format!");
  179. continue;
  180. }
  181. json_object_foreach (value, addr_key, addr_value) {
  182. val_str = json_dumps (addr_value, JSON_ENCODE_ANY);
  183. tmp = val_str;
  184. // Remove leading " from jasson conversion
  185. if (tmp[0] == '"')
  186. tmp++;
  187. // Remove trailing " from jansson conversion
  188. if (tmp[strlen (tmp) - 1] == '"')
  189. tmp[strlen (tmp) - 1] = '\0';
  190. GNUNET_RECLAIM_attribute_list_add (attrs,
  191. addr_key,
  192. NULL,
  193. GNUNET_RECLAIM_ATTRIBUTE_TYPE_STRING,
  194. tmp,
  195. strlen (val_str));
  196. GNUNET_free (val_str);
  197. }
  198. continue;
  199. }
  200. val_str = json_dumps (value, JSON_ENCODE_ANY);
  201. tmp = val_str;
  202. // Remove leading " from jasson conversion
  203. if (tmp[0] == '"')
  204. tmp++;
  205. // Remove trailing " from jansson conversion
  206. if (tmp[strlen (tmp) - 1] == '"')
  207. tmp[strlen (tmp) - 1] = '\0';
  208. GNUNET_RECLAIM_attribute_list_add (attrs,
  209. key,
  210. NULL,
  211. GNUNET_RECLAIM_ATTRIBUTE_TYPE_STRING,// FIXME
  212. tmp,
  213. strlen (val_str));
  214. GNUNET_free (val_str);
  215. }
  216. json_decref (json_val);
  217. GNUNET_free (jwt_string);
  218. return attrs;
  219. }
  220. /**
  221. * Parse a JWT and return the respective claim value as Attribute
  222. *
  223. * @param cls the plugin
  224. * @param cred the jwt credential
  225. * @return a GNUNET_RECLAIM_Attribute, containing the new value
  226. */
  227. struct GNUNET_RECLAIM_AttributeList *
  228. jwt_parse_attributes_c (void *cls,
  229. const struct GNUNET_RECLAIM_Credential *cred)
  230. {
  231. if (cred->type != GNUNET_RECLAIM_CREDENTIAL_TYPE_JWT)
  232. return NULL;
  233. return jwt_parse_attributes (cls, cred->data, cred->data_size);
  234. }
  235. /**
  236. * Parse a JWT and return the respective claim value as Attribute
  237. *
  238. * @param cls the plugin
  239. * @param cred the jwt credential
  240. * @return a GNUNET_RECLAIM_Attribute, containing the new value
  241. */
  242. struct GNUNET_RECLAIM_AttributeList *
  243. jwt_parse_attributes_p (void *cls,
  244. const struct GNUNET_RECLAIM_Presentation *cred)
  245. {
  246. if (cred->type != GNUNET_RECLAIM_CREDENTIAL_TYPE_JWT)
  247. return NULL;
  248. return jwt_parse_attributes (cls, cred->data, cred->data_size);
  249. }
  250. /**
  251. * Parse a JWT and return the issuer
  252. *
  253. * @param cls the plugin
  254. * @param cred the jwt credential
  255. * @return a string, containing the isser
  256. */
  257. char *
  258. jwt_get_issuer (void *cls,
  259. const char *data,
  260. size_t data_size)
  261. {
  262. const char *jwt_body;
  263. char *jwt_string;
  264. char delim[] = ".";
  265. char *issuer = NULL;
  266. char *decoded_jwt;
  267. json_t *issuer_json;
  268. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Parsing JWT attributes.\n");
  269. json_t *json_val;
  270. json_error_t json_err;
  271. jwt_string = GNUNET_strndup (data, data_size);
  272. jwt_body = strtok (jwt_string, delim);
  273. jwt_body = strtok (NULL, delim);
  274. GNUNET_STRINGS_base64url_decode (jwt_body, strlen (jwt_body),
  275. (void **) &decoded_jwt);
  276. json_val = json_loads (decoded_jwt, JSON_DECODE_ANY, &json_err);
  277. GNUNET_free (decoded_jwt);
  278. GNUNET_free (jwt_string);
  279. if (NULL == json_val)
  280. return NULL;
  281. issuer_json = json_object_get (json_val, "iss");
  282. if ((NULL == issuer_json) || (! json_is_string (issuer_json)))
  283. {
  284. json_decref (json_val);
  285. return NULL;
  286. }
  287. issuer = GNUNET_strdup (json_string_value (issuer_json));
  288. json_decref (json_val);
  289. return issuer;
  290. }
  291. /**
  292. * Parse a JWT and return the issuer
  293. *
  294. * @param cls the plugin
  295. * @param cred the jwt credential
  296. * @return a string, containing the isser
  297. */
  298. char *
  299. jwt_get_issuer_c (void *cls,
  300. const struct GNUNET_RECLAIM_Credential *cred)
  301. {
  302. if (GNUNET_RECLAIM_CREDENTIAL_TYPE_JWT != cred->type)
  303. return NULL;
  304. return jwt_get_issuer (cls, cred->data, cred->data_size);
  305. }
  306. /**
  307. * Parse a JWT and return the issuer
  308. *
  309. * @param cls the plugin
  310. * @param cred the jwt credential
  311. * @return a string, containing the isser
  312. */
  313. char *
  314. jwt_get_issuer_p (void *cls,
  315. const struct GNUNET_RECLAIM_Presentation *cred)
  316. {
  317. if (GNUNET_RECLAIM_CREDENTIAL_TYPE_JWT != cred->type)
  318. return NULL;
  319. return jwt_get_issuer (cls, cred->data, cred->data_size);
  320. }
  321. /**
  322. * Parse a JWT and return the expiration
  323. *
  324. * @param cls the plugin
  325. * @param cred the jwt credential
  326. * @return a string, containing the isser
  327. */
  328. enum GNUNET_GenericReturnValue
  329. jwt_get_expiration (void *cls,
  330. const char *data,
  331. size_t data_size,
  332. struct GNUNET_TIME_Absolute *exp)
  333. {
  334. const char *jwt_body;
  335. char *jwt_string;
  336. char delim[] = ".";
  337. char *decoded_jwt;
  338. json_t *exp_json;
  339. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Parsing JWT attributes.\n");
  340. json_t *json_val;
  341. json_error_t json_err;
  342. jwt_string = GNUNET_strndup (data, data_size);
  343. jwt_body = strtok (jwt_string, delim);
  344. jwt_body = strtok (NULL, delim);
  345. GNUNET_STRINGS_base64url_decode (jwt_body, strlen (jwt_body),
  346. (void **) &decoded_jwt);
  347. json_val = json_loads (decoded_jwt, JSON_DECODE_ANY, &json_err);
  348. GNUNET_free (decoded_jwt);
  349. GNUNET_free (jwt_string);
  350. if (NULL == json_val)
  351. return GNUNET_SYSERR;
  352. exp_json = json_object_get (json_val, "exp");
  353. if ((NULL == exp_json) || (! json_is_integer (exp_json)))
  354. {
  355. json_decref (json_val);
  356. return GNUNET_SYSERR;
  357. }
  358. exp->abs_value_us = json_integer_value (exp_json) * 1000 * 1000;
  359. json_decref (json_val);
  360. return GNUNET_OK;
  361. }
  362. /**
  363. * Parse a JWT and return the expiration
  364. *
  365. * @param cls the plugin
  366. * @param cred the jwt credential
  367. * @return the expirati
  368. */
  369. enum GNUNET_GenericReturnValue
  370. jwt_get_expiration_c (void *cls,
  371. const struct GNUNET_RECLAIM_Credential *cred,
  372. struct GNUNET_TIME_Absolute *exp)
  373. {
  374. if (GNUNET_RECLAIM_CREDENTIAL_TYPE_JWT != cred->type)
  375. return GNUNET_NO;
  376. return jwt_get_expiration (cls, cred->data, cred->data_size, exp);
  377. }
  378. /**
  379. * Parse a JWT and return the expiration
  380. *
  381. * @param cls the plugin
  382. * @param cred the jwt credential
  383. * @return a string, containing the isser
  384. */
  385. enum GNUNET_GenericReturnValue
  386. jwt_get_expiration_p (void *cls,
  387. const struct GNUNET_RECLAIM_Presentation *cred,
  388. struct GNUNET_TIME_Absolute *exp)
  389. {
  390. if (GNUNET_RECLAIM_CREDENTIAL_TYPE_JWT != cred->type)
  391. return GNUNET_NO;
  392. return jwt_get_expiration (cls, cred->data, cred->data_size, exp);
  393. }
  394. enum GNUNET_GenericReturnValue
  395. jwt_create_presentation (void *cls,
  396. const struct GNUNET_RECLAIM_Credential *cred,
  397. const struct GNUNET_RECLAIM_AttributeList *attrs,
  398. struct GNUNET_RECLAIM_Presentation **presentation)
  399. {
  400. if (GNUNET_RECLAIM_CREDENTIAL_TYPE_JWT != cred->type)
  401. return GNUNET_NO;
  402. *presentation = GNUNET_RECLAIM_presentation_new (
  403. GNUNET_RECLAIM_CREDENTIAL_TYPE_JWT,
  404. cred->data,
  405. cred->data_size);
  406. return GNUNET_OK;
  407. }
  408. /**
  409. * Entry point for the plugin.
  410. *
  411. * @param cls NULL
  412. * @return the exported block API
  413. */
  414. void *
  415. libgnunet_plugin_reclaim_credential_jwt_init (void *cls)
  416. {
  417. struct GNUNET_RECLAIM_CredentialPluginFunctions *api;
  418. api = GNUNET_new (struct GNUNET_RECLAIM_CredentialPluginFunctions);
  419. api->value_to_string = &jwt_value_to_string;
  420. api->string_to_value = &jwt_string_to_value;
  421. api->typename_to_number = &jwt_typename_to_number;
  422. api->number_to_typename = &jwt_number_to_typename;
  423. api->get_attributes = &jwt_parse_attributes_c;
  424. api->get_issuer = &jwt_get_issuer_c;
  425. api->get_expiration = &jwt_get_expiration_c;
  426. api->value_to_string_p = &jwt_value_to_string;
  427. api->string_to_value_p = &jwt_string_to_value;
  428. api->typename_to_number_p = &jwt_typename_to_number;
  429. api->number_to_typename_p = &jwt_number_to_typename;
  430. api->get_attributes_p = &jwt_parse_attributes_p;
  431. api->get_issuer_p = &jwt_get_issuer_p;
  432. api->get_expiration_p = &jwt_get_expiration_p;
  433. api->create_presentation = &jwt_create_presentation;
  434. return api;
  435. }
  436. /**
  437. * Exit point from the plugin.
  438. *
  439. * @param cls the return value from #libgnunet_plugin_block_test_init()
  440. * @return NULL
  441. */
  442. void *
  443. libgnunet_plugin_reclaim_credential_jwt_done (void *cls)
  444. {
  445. struct GNUNET_RECLAIM_CredentialPluginFunctions *api = cls;
  446. GNUNET_free (api);
  447. return NULL;
  448. }
  449. /* end of plugin_reclaim_credential_type_jwt.c */