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