summaryrefslogtreecommitdiff
path: root/src/reclaim/pabc_helper.c
blob: 1b1dbea0f1a85d06b7e138493d458a5bbaf22d66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// maximilian.kaul@aisec.fraunhofer.de

// WIP implementation of
// https://github.com/ontio/ontology-crypto/wiki/Anonymous-Credential
// using the relic library https://github.com/relic-toolkit/relic/

#include "pabc_helper.h"
#include <pwd.h>
#include <stdlib.h>
#include <unistd.h>

static char pabc_dir[PATH_MAX + 1];

static const char *
get_homedir ()
{
  const char *homedir;
  if ((homedir = getenv ("HOME")) == NULL)
  {
    homedir = getpwuid (getuid ())->pw_dir;
  }
  return homedir;
}


static enum GNUNET_GenericReturnValue
write_file (char const *const filename, const char *buffer)
{
  struct GNUNET_DISK_FileHandle *fh;
  fh = GNUNET_DISK_file_open (filename,
                              GNUNET_DISK_OPEN_WRITE
                              | GNUNET_DISK_OPEN_TRUNCATE
                              | GNUNET_DISK_OPEN_CREATE,
                              GNUNET_DISK_PERM_USER_WRITE
                              | GNUNET_DISK_PERM_USER_READ);
  if (fh == NULL)
    return GNUNET_SYSERR;
  if (GNUNET_SYSERR == GNUNET_DISK_file_write (fh,
                                               buffer, strlen (buffer) + 1))
    goto fail;
  GNUNET_DISK_file_close (fh);
  return GNUNET_OK;

fail:
  GNUNET_DISK_file_close (fh);
  return GNUNET_SYSERR;
}


static enum GNUNET_GenericReturnValue
init_pabc_dir ()
{
  size_t filename_size = strlen (get_homedir ()) + 1 + strlen (".local") + 1
                         + strlen ("pabc-reclaim") + 1;
  snprintf (pabc_dir, filename_size, "%s/%s/%s",
            get_homedir (), ".local", "pabc-reclaim");
  return GNUNET_DISK_directory_create (pabc_dir);
}


static const char *
get_pabcdir ()
{
  init_pabc_dir ();
  return pabc_dir;
}


enum GNUNET_GenericReturnValue
read_file (char const *const filename, char **buffer)
{
  struct GNUNET_DISK_FileHandle *fh;
  if (GNUNET_YES != GNUNET_DISK_file_test (filename))
    return GNUNET_SYSERR;

  fh = GNUNET_DISK_file_open (filename,
                              GNUNET_DISK_OPEN_READ,
                              GNUNET_DISK_PERM_USER_READ);
  if (fh == NULL)
    return GNUNET_SYSERR;
  long lSize = GNUNET_DISK_file_seek (fh, 0, GNUNET_DISK_SEEK_END);
  if (lSize < 0)
    goto fail;
  GNUNET_DISK_file_seek (fh, 0, GNUNET_DISK_SEEK_SET);
  *buffer = calloc ((size_t) lSize + 1, sizeof(char));
  if (*buffer == NULL)
    goto fail;

  // copy the file into the buffer:
  size_t r = GNUNET_DISK_file_read (fh, *buffer, (size_t) lSize);
  if (r != (size_t) lSize)
    goto fail;

  GNUNET_DISK_file_close (fh);
  return GNUNET_OK;

fail:
  GNUNET_DISK_file_close (fh);
  return GNUNET_SYSERR;
}


struct pabc_public_parameters *
PABC_read_issuer_ppfile (const char *f, struct pabc_context *const ctx)
{
  if (NULL == ctx)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No global context provided\n");
    return NULL;
  }
  struct pabc_public_parameters *pp;
  char *buffer;
  int r;
  r = read_file (f, &buffer);
  if (GNUNET_OK != r)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error reading file\n");
    return NULL;
  }
  if (PABC_OK != pabc_decode_and_new_public_parameters (ctx, &pp, buffer))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Failed to decode public parameters\n");
    PABC_FREE_NULL (buffer);
    return NULL;
  }
  PABC_FREE_NULL (buffer);
  return pp;
}


enum GNUNET_GenericReturnValue
PABC_load_public_parameters (struct pabc_context *const ctx,
                             char const *const pp_name,
                             struct pabc_public_parameters **pp)
{
  char fname[PATH_MAX];
  char *pp_filename;
  const char *pdir = get_pabcdir ();

  if (ctx == NULL)
    return GNUNET_SYSERR;
  if (pp_name == NULL)
    return GNUNET_SYSERR;

  GNUNET_STRINGS_urlencode (pp_name, strlen (pp_name), &pp_filename);
  if (GNUNET_YES != GNUNET_DISK_directory_test (pdir, GNUNET_YES))
  {
    GNUNET_free (pp_filename);
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error reading %s\n", pdir);
    return GNUNET_SYSERR;
  }
  snprintf (fname, PATH_MAX, "%s/%s%s", pdir, pp_filename, PABC_PP_EXT);
  if (GNUNET_YES != GNUNET_DISK_file_test (fname))
  {
    GNUNET_free (pp_filename);
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error testing %s\n", fname);
    return GNUNET_SYSERR;
  }
  *pp = PABC_read_issuer_ppfile (fname, ctx);
  if (*pp)
    return GNUNET_OK;
  else
    return GNUNET_SYSERR;
}


enum GNUNET_GenericReturnValue
PABC_write_public_parameters (char const *const pp_name,
                              struct pabc_public_parameters *const pp)
{
  char *json;
  char *filename;
  char *pp_filename;
  enum pabc_status status;
  struct pabc_context *ctx = NULL;

  GNUNET_STRINGS_urlencode (pp_name, strlen (pp_name), &pp_filename);
  PABC_ASSERT (pabc_new_ctx (&ctx));
  // store in json file
  status = pabc_encode_public_parameters (ctx, pp, &json);
  if (status != PABC_OK)
  {
    GNUNET_free (pp_filename);
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Failed to encode public parameters.\n");
    pabc_free_ctx (&ctx);
    return GNUNET_SYSERR;
  }

  size_t filename_size =
    strlen (get_pabcdir ()) + 1 + strlen (pp_filename) + strlen (PABC_PP_EXT)
    + 1;
  filename = GNUNET_malloc (filename_size);
  if (! filename)
  {
    GNUNET_free (pp_filename);
    PABC_FREE_NULL (json);
    pabc_free_ctx (&ctx);
    return GNUNET_SYSERR;
  }
  snprintf (filename, filename_size, "%s/%s%s", get_pabcdir (), pp_filename,
            PABC_PP_EXT);

  GNUNET_free (pp_filename);
  if (GNUNET_OK != write_file (filename, json))
  {
    PABC_FREE_NULL (filename);
    PABC_FREE_NULL (json);
    pabc_free_ctx (&ctx);
    return GNUNET_SYSERR;
  }
  PABC_FREE_NULL (filename);
  PABC_FREE_NULL (json);
  pabc_free_ctx (&ctx);
  return GNUNET_OK;
}


enum GNUNET_GenericReturnValue
PABC_write_usr_ctx (char const *const usr_name,
                    char const *const pp_name,
                    struct pabc_context const *const ctx,
                    struct pabc_public_parameters const *const pp,
                    struct pabc_user_context *const usr_ctx)
{

  char *pp_filename;
  char *json = NULL;
  enum pabc_status status;
  char *fname = NULL;

  if (NULL == usr_name)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No issuer given.\n");
    return GNUNET_SYSERR;
  }
  if (NULL == pp_name)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No user given.\n");
    return GNUNET_SYSERR;
  }
  if (NULL == ctx)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No context given.\n");
    return GNUNET_SYSERR;
  }
  if (NULL == pp)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No public parameters given.\n");
    return GNUNET_SYSERR;
  }
  if (NULL == usr_ctx)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No user context given.\n");
    return GNUNET_SYSERR;
  }

  GNUNET_STRINGS_urlencode (pp_name, strlen (pp_name), &pp_filename);
  status = pabc_encode_user_ctx (ctx, pp, usr_ctx, &json);
  if (PABC_OK != status)
  {
    GNUNET_free (pp_filename);
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to encode user context.\n");
    return status;
  }

  size_t fname_size = strlen (get_pabcdir ()) + 1 + strlen (usr_name) + 1
                      + strlen (pp_filename) + strlen (PABC_USR_EXT) + 1;
  fname = GNUNET_malloc (fname_size);

  snprintf (fname, fname_size, "%s/%s_%s%s", get_pabcdir (), usr_name,
            pp_filename,
            PABC_USR_EXT);

  GNUNET_free (pp_filename);
  if (GNUNET_OK == write_file (fname, json))
  {
    GNUNET_free (fname);
    GNUNET_free (json);
    return GNUNET_OK;
  }
  else
  {
    GNUNET_free (fname);
    GNUNET_free (json);
    return GNUNET_SYSERR;
  }
}


enum GNUNET_GenericReturnValue
PABC_read_usr_ctx (char const *const usr_name,
                   char const *const pp_name,
                   struct pabc_context const *const ctx,
                   struct pabc_public_parameters const *const pp,
                   struct pabc_user_context **usr_ctx)
{
  char *json = NULL;
  char *pp_filename;
  enum pabc_status status;

  char *fname = NULL;

  if (NULL == usr_name)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No issuer given.\n");
    return GNUNET_SYSERR;
  }
  if (NULL == pp_name)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No user given.\n");
    return GNUNET_SYSERR;
  }
  if (NULL == ctx)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No context given.\n");
    return GNUNET_SYSERR;
  }
  if (NULL == pp)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No public parameters given.\n");
    return GNUNET_SYSERR;
  }
  if (NULL == usr_ctx)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No user context given.\n");
    return GNUNET_SYSERR;
  }
  GNUNET_STRINGS_urlencode (pp_name, strlen (pp_name), &pp_filename);

  size_t fname_size = strlen (get_pabcdir ()) + 1 + strlen (usr_name) + 1
                      + strlen (pp_filename) + strlen (PABC_USR_EXT) + 1;
  fname = GNUNET_malloc (fname_size);
  snprintf (fname, fname_size, "%s/%s_%s%s", get_pabcdir (), usr_name,
            pp_filename,
            PABC_USR_EXT);
  GNUNET_free (pp_filename);
  if (GNUNET_OK != read_file (fname, &json))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Failed to read `%s'\n", fname);
    PABC_FREE_NULL (fname);
    return GNUNET_SYSERR;
  }
  GNUNET_free (fname);

  status = pabc_new_user_context (ctx, pp, usr_ctx);
  if (PABC_OK != status)
  {
    GNUNET_free (json);
    return GNUNET_SYSERR;
  }
  status = pabc_decode_user_ctx (ctx, pp, *usr_ctx, json);
  GNUNET_free (json);
  if (PABC_OK != status)
  {
    pabc_free_user_context (ctx, pp, usr_ctx);
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to encode user context.\n");
    return GNUNET_SYSERR;
  }

  return GNUNET_OK;
}