pabc_helper.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // maximilian.kaul@aisec.fraunhofer.de
  2. // WIP implementation of
  3. // https://github.com/ontio/ontology-crypto/wiki/Anonymous-Credential
  4. // using the relic library https://github.com/relic-toolkit/relic/
  5. #include "pabc_helper.h"
  6. #include <pwd.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. static char pabc_dir[PATH_MAX + 1];
  10. static const char *
  11. get_homedir ()
  12. {
  13. const char *homedir;
  14. if ((homedir = getenv ("HOME")) == NULL)
  15. {
  16. homedir = getpwuid (getuid ())->pw_dir;
  17. }
  18. return homedir;
  19. }
  20. static enum GNUNET_GenericReturnValue
  21. write_file (char const *const filename, const char *buffer)
  22. {
  23. struct GNUNET_DISK_FileHandle *fh;
  24. fh = GNUNET_DISK_file_open (filename,
  25. GNUNET_DISK_OPEN_WRITE
  26. | GNUNET_DISK_OPEN_TRUNCATE
  27. | GNUNET_DISK_OPEN_CREATE,
  28. GNUNET_DISK_PERM_USER_WRITE
  29. | GNUNET_DISK_PERM_USER_READ);
  30. if (fh == NULL)
  31. return GNUNET_SYSERR;
  32. if (GNUNET_SYSERR == GNUNET_DISK_file_write (fh,
  33. buffer, strlen (buffer) + 1))
  34. goto fail;
  35. GNUNET_DISK_file_close (fh);
  36. return GNUNET_OK;
  37. fail:
  38. GNUNET_DISK_file_close (fh);
  39. return GNUNET_SYSERR;
  40. }
  41. static enum GNUNET_GenericReturnValue
  42. init_pabc_dir ()
  43. {
  44. size_t filename_size = strlen (get_homedir ()) + 1 + strlen (".local") + 1
  45. + strlen ("pabc-reclaim") + 1;
  46. snprintf (pabc_dir, filename_size, "%s/%s/%s",
  47. get_homedir (), ".local", "pabc-reclaim");
  48. return GNUNET_DISK_directory_create (pabc_dir);
  49. }
  50. static const char *
  51. get_pabcdir ()
  52. {
  53. init_pabc_dir ();
  54. return pabc_dir;
  55. }
  56. enum GNUNET_GenericReturnValue
  57. read_file (char const *const filename, char **buffer)
  58. {
  59. struct GNUNET_DISK_FileHandle *fh;
  60. if (GNUNET_YES != GNUNET_DISK_file_test (filename))
  61. return GNUNET_SYSERR;
  62. fh = GNUNET_DISK_file_open (filename,
  63. GNUNET_DISK_OPEN_READ,
  64. GNUNET_DISK_PERM_USER_READ);
  65. if (fh == NULL)
  66. return GNUNET_SYSERR;
  67. long lSize = GNUNET_DISK_file_seek (fh, 0, GNUNET_DISK_SEEK_END);
  68. if (lSize < 0)
  69. goto fail;
  70. GNUNET_DISK_file_seek (fh, 0, GNUNET_DISK_SEEK_SET);
  71. *buffer = calloc ((size_t) lSize + 1, sizeof(char));
  72. if (*buffer == NULL)
  73. goto fail;
  74. // copy the file into the buffer:
  75. size_t r = GNUNET_DISK_file_read (fh, *buffer, (size_t) lSize);
  76. if (r != (size_t) lSize)
  77. goto fail;
  78. GNUNET_DISK_file_close (fh);
  79. return GNUNET_OK;
  80. fail:
  81. GNUNET_DISK_file_close (fh);
  82. return GNUNET_SYSERR;
  83. }
  84. struct pabc_public_parameters *
  85. PABC_read_issuer_ppfile (const char *f, struct pabc_context *const ctx)
  86. {
  87. if (NULL == ctx)
  88. {
  89. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No global context provided\n");
  90. return NULL;
  91. }
  92. struct pabc_public_parameters *pp;
  93. char *buffer;
  94. int r;
  95. r = read_file (f, &buffer);
  96. if (GNUNET_OK != r)
  97. {
  98. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error reading file\n");
  99. return NULL;
  100. }
  101. if (PABC_OK != pabc_decode_and_new_public_parameters (ctx, &pp, buffer))
  102. {
  103. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  104. "Failed to decode public parameters\n");
  105. PABC_FREE_NULL (buffer);
  106. return NULL;
  107. }
  108. PABC_FREE_NULL (buffer);
  109. return pp;
  110. }
  111. enum GNUNET_GenericReturnValue
  112. PABC_load_public_parameters (struct pabc_context *const ctx,
  113. char const *const pp_name,
  114. struct pabc_public_parameters **pp)
  115. {
  116. char fname[PATH_MAX];
  117. char *pp_filename;
  118. const char *pdir = get_pabcdir ();
  119. if (ctx == NULL)
  120. return GNUNET_SYSERR;
  121. if (pp_name == NULL)
  122. return GNUNET_SYSERR;
  123. GNUNET_STRINGS_urlencode (pp_name, strlen (pp_name), &pp_filename);
  124. if (GNUNET_YES != GNUNET_DISK_directory_test (pdir, GNUNET_YES))
  125. {
  126. GNUNET_free (pp_filename);
  127. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error reading %s\n", pdir);
  128. return GNUNET_SYSERR;
  129. }
  130. snprintf (fname, PATH_MAX, "%s/%s%s", pdir, pp_filename, PABC_PP_EXT);
  131. if (GNUNET_YES != GNUNET_DISK_file_test (fname))
  132. {
  133. GNUNET_free (pp_filename);
  134. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error testing %s\n", fname);
  135. return GNUNET_SYSERR;
  136. }
  137. *pp = PABC_read_issuer_ppfile (fname, ctx);
  138. if (*pp)
  139. return GNUNET_OK;
  140. else
  141. return GNUNET_SYSERR;
  142. }
  143. enum GNUNET_GenericReturnValue
  144. PABC_write_public_parameters (char const *const pp_name,
  145. struct pabc_public_parameters *const pp)
  146. {
  147. char *json;
  148. char *filename;
  149. char *pp_filename;
  150. enum pabc_status status;
  151. struct pabc_context *ctx = NULL;
  152. GNUNET_STRINGS_urlencode (pp_name, strlen (pp_name), &pp_filename);
  153. PABC_ASSERT (pabc_new_ctx (&ctx));
  154. // store in json file
  155. status = pabc_encode_public_parameters (ctx, pp, &json);
  156. if (status != PABC_OK)
  157. {
  158. GNUNET_free (pp_filename);
  159. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  160. "Failed to encode public parameters.\n");
  161. pabc_free_ctx (&ctx);
  162. return GNUNET_SYSERR;
  163. }
  164. size_t filename_size =
  165. strlen (get_pabcdir ()) + 1 + strlen (pp_filename) + strlen (PABC_PP_EXT)
  166. + 1;
  167. filename = GNUNET_malloc (filename_size);
  168. if (! filename)
  169. {
  170. GNUNET_free (pp_filename);
  171. PABC_FREE_NULL (json);
  172. pabc_free_ctx (&ctx);
  173. return GNUNET_SYSERR;
  174. }
  175. snprintf (filename, filename_size, "%s/%s%s", get_pabcdir (), pp_filename,
  176. PABC_PP_EXT);
  177. GNUNET_free (pp_filename);
  178. if (GNUNET_OK != write_file (filename, json))
  179. {
  180. PABC_FREE_NULL (filename);
  181. PABC_FREE_NULL (json);
  182. pabc_free_ctx (&ctx);
  183. return GNUNET_SYSERR;
  184. }
  185. PABC_FREE_NULL (filename);
  186. PABC_FREE_NULL (json);
  187. pabc_free_ctx (&ctx);
  188. return GNUNET_OK;
  189. }
  190. enum GNUNET_GenericReturnValue
  191. PABC_write_usr_ctx (char const *const usr_name,
  192. char const *const pp_name,
  193. struct pabc_context const *const ctx,
  194. struct pabc_public_parameters const *const pp,
  195. struct pabc_user_context *const usr_ctx)
  196. {
  197. char *pp_filename;
  198. char *json = NULL;
  199. enum pabc_status status;
  200. char *fname = NULL;
  201. if (NULL == usr_name)
  202. {
  203. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No issuer given.\n");
  204. return GNUNET_SYSERR;
  205. }
  206. if (NULL == pp_name)
  207. {
  208. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No user given.\n");
  209. return GNUNET_SYSERR;
  210. }
  211. if (NULL == ctx)
  212. {
  213. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No context given.\n");
  214. return GNUNET_SYSERR;
  215. }
  216. if (NULL == pp)
  217. {
  218. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No public parameters given.\n");
  219. return GNUNET_SYSERR;
  220. }
  221. if (NULL == usr_ctx)
  222. {
  223. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No user context given.\n");
  224. return GNUNET_SYSERR;
  225. }
  226. GNUNET_STRINGS_urlencode (pp_name, strlen (pp_name), &pp_filename);
  227. status = pabc_encode_user_ctx (ctx, pp, usr_ctx, &json);
  228. if (PABC_OK != status)
  229. {
  230. GNUNET_free (pp_filename);
  231. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to encode user context.\n");
  232. return status;
  233. }
  234. size_t fname_size = strlen (get_pabcdir ()) + 1 + strlen (usr_name) + 1
  235. + strlen (pp_filename) + strlen (PABC_USR_EXT) + 1;
  236. fname = GNUNET_malloc (fname_size);
  237. snprintf (fname, fname_size, "%s/%s_%s%s", get_pabcdir (), usr_name,
  238. pp_filename,
  239. PABC_USR_EXT);
  240. GNUNET_free (pp_filename);
  241. if (GNUNET_OK == write_file (fname, json))
  242. {
  243. GNUNET_free (fname);
  244. GNUNET_free (json);
  245. return GNUNET_OK;
  246. }
  247. else
  248. {
  249. GNUNET_free (fname);
  250. GNUNET_free (json);
  251. return GNUNET_SYSERR;
  252. }
  253. }
  254. enum GNUNET_GenericReturnValue
  255. PABC_read_usr_ctx (char const *const usr_name,
  256. char const *const pp_name,
  257. struct pabc_context const *const ctx,
  258. struct pabc_public_parameters const *const pp,
  259. struct pabc_user_context **usr_ctx)
  260. {
  261. char *json = NULL;
  262. char *pp_filename;
  263. enum pabc_status status;
  264. char *fname = NULL;
  265. if (NULL == usr_name)
  266. {
  267. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No issuer given.\n");
  268. return GNUNET_SYSERR;
  269. }
  270. if (NULL == pp_name)
  271. {
  272. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No user given.\n");
  273. return GNUNET_SYSERR;
  274. }
  275. if (NULL == ctx)
  276. {
  277. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No context given.\n");
  278. return GNUNET_SYSERR;
  279. }
  280. if (NULL == pp)
  281. {
  282. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No public parameters given.\n");
  283. return GNUNET_SYSERR;
  284. }
  285. if (NULL == usr_ctx)
  286. {
  287. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No user context given.\n");
  288. return GNUNET_SYSERR;
  289. }
  290. GNUNET_STRINGS_urlencode (pp_name, strlen (pp_name), &pp_filename);
  291. size_t fname_size = strlen (get_pabcdir ()) + 1 + strlen (usr_name) + 1
  292. + strlen (pp_filename) + strlen (PABC_USR_EXT) + 1;
  293. fname = GNUNET_malloc (fname_size);
  294. snprintf (fname, fname_size, "%s/%s_%s%s", get_pabcdir (), usr_name,
  295. pp_filename,
  296. PABC_USR_EXT);
  297. GNUNET_free (pp_filename);
  298. if (GNUNET_OK != read_file (fname, &json))
  299. {
  300. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  301. "Failed to read `%s'\n", fname);
  302. PABC_FREE_NULL (fname);
  303. return GNUNET_SYSERR;
  304. }
  305. GNUNET_free (fname);
  306. status = pabc_new_user_context (ctx, pp, usr_ctx);
  307. if (PABC_OK != status)
  308. {
  309. GNUNET_free (json);
  310. return GNUNET_SYSERR;
  311. }
  312. status = pabc_decode_user_ctx (ctx, pp, *usr_ctx, json);
  313. GNUNET_free (json);
  314. if (PABC_OK != status)
  315. {
  316. pabc_free_user_context (ctx, pp, usr_ctx);
  317. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to encode user context.\n");
  318. return GNUNET_SYSERR;
  319. }
  320. return GNUNET_OK;
  321. }