GNUnet  0.19.2
pabc_helper.c
Go to the documentation of this file.
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 "platform.h"
8 #include "pabc_helper.h"
9 #include <pwd.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 
13 static char pabc_dir[PATH_MAX + 1];
14 
15 static const char *
17 {
18  const char *homedir;
19  if ((homedir = getenv ("HOME")) == NULL)
20  {
21  homedir = getpwuid (getuid ())->pw_dir;
22  }
23  return homedir;
24 }
25 
26 
27 static enum GNUNET_GenericReturnValue
28 write_file (char const *const filename, const char *buffer)
29 {
30  struct GNUNET_DISK_FileHandle *fh;
37  if (fh == NULL)
38  return GNUNET_SYSERR;
40  buffer, strlen (buffer) + 1))
41  goto fail;
43  return GNUNET_OK;
44 
45 fail:
47  return GNUNET_SYSERR;
48 }
49 
50 
51 static enum GNUNET_GenericReturnValue
53 {
54  size_t filename_size = strlen (get_homedir ()) + 1 + strlen (".local") + 1
55  + strlen ("pabc-reclaim") + 1;
56  snprintf (pabc_dir, filename_size, "%s/%s/%s",
57  get_homedir (), ".local", "pabc-reclaim");
59 }
60 
61 
62 static const char *
64 {
65  init_pabc_dir ();
66  return pabc_dir;
67 }
68 
69 
71 read_file (char const *const filename, char **buffer)
72 {
73  struct GNUNET_DISK_FileHandle *fh;
75  return GNUNET_SYSERR;
76 
80  if (fh == NULL)
81  return GNUNET_SYSERR;
83  if (lSize < 0)
84  goto fail;
86  *buffer = calloc ((size_t) lSize + 1, sizeof(char));
87  if (*buffer == NULL)
88  goto fail;
89 
90  // copy the file into the buffer:
91  size_t r = GNUNET_DISK_file_read (fh, *buffer, (size_t) lSize);
92  if (r != (size_t) lSize)
93  goto fail;
94 
96  return GNUNET_OK;
97 
98 fail:
100  GNUNET_free (*buffer);
101  return GNUNET_SYSERR;
102 }
103 
104 
105 struct pabc_public_parameters *
106 PABC_read_issuer_ppfile (const char *f, struct pabc_context *const ctx)
107 {
108  if (NULL == ctx)
109  {
110  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No global context provided\n");
111  return NULL;
112  }
113  struct pabc_public_parameters *pp;
114  char *buffer;
115  int r;
116  r = read_file (f, &buffer);
117  if (GNUNET_OK != r)
118  {
119  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error reading file\n");
120  return NULL;
121  }
122  if (PABC_OK != pabc_decode_and_new_public_parameters (ctx, &pp, buffer))
123  {
125  "Failed to decode public parameters\n");
126  PABC_FREE_NULL (buffer);
127  return NULL;
128  }
129  PABC_FREE_NULL (buffer);
130  return pp;
131 }
132 
133 
135 PABC_load_public_parameters (struct pabc_context *const ctx,
136  char const *const pp_name,
137  struct pabc_public_parameters **pp)
138 {
139  char fname[PATH_MAX];
140  char *pp_filename;
141  const char *pdir = get_pabcdir ();
142 
143  if (ctx == NULL)
144  return GNUNET_SYSERR;
145  if (pp_name == NULL)
146  return GNUNET_SYSERR;
147 
148  GNUNET_STRINGS_urlencode (pp_name, strlen (pp_name), &pp_filename);
150  {
151  GNUNET_free (pp_filename);
152  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error reading %s\n", pdir);
153  return GNUNET_SYSERR;
154  }
155  snprintf (fname, PATH_MAX, "%s/%s%s", pdir, pp_filename, PABC_PP_EXT);
156  if (GNUNET_YES != GNUNET_DISK_file_test (fname))
157  {
158  GNUNET_free (pp_filename);
159  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error testing %s\n", fname);
160  return GNUNET_SYSERR;
161  }
162  *pp = PABC_read_issuer_ppfile (fname, ctx);
163  if (*pp)
164  return GNUNET_OK;
165  else
166  return GNUNET_SYSERR;
167 }
168 
169 
171 PABC_write_public_parameters (char const *const pp_name,
172  struct pabc_public_parameters *const pp)
173 {
174  char *json;
175  char *filename;
176  char *pp_filename;
177  enum pabc_status status;
178  struct pabc_context *ctx = NULL;
179 
180  GNUNET_STRINGS_urlencode (pp_name, strlen (pp_name), &pp_filename);
181  PABC_ASSERT (pabc_new_ctx (&ctx));
182  // store in json file
183  status = pabc_encode_public_parameters (ctx, pp, &json);
184  if (status != PABC_OK)
185  {
186  GNUNET_free (pp_filename);
188  "Failed to encode public parameters.\n");
189  pabc_free_ctx (&ctx);
190  return GNUNET_SYSERR;
191  }
192 
193  size_t filename_size =
194  strlen (get_pabcdir ()) + 1 + strlen (pp_filename) + strlen (PABC_PP_EXT)
195  + 1;
196  filename = GNUNET_malloc (filename_size);
197  if (! filename)
198  {
199  GNUNET_free (pp_filename);
200  PABC_FREE_NULL (json);
201  pabc_free_ctx (&ctx);
202  return GNUNET_SYSERR;
203  }
204  snprintf (filename, filename_size, "%s/%s%s", get_pabcdir (), pp_filename,
205  PABC_PP_EXT);
206 
207  GNUNET_free (pp_filename);
208  if (GNUNET_OK != write_file (filename, json))
209  {
210  PABC_FREE_NULL (filename);
211  PABC_FREE_NULL (json);
212  pabc_free_ctx (&ctx);
213  return GNUNET_SYSERR;
214  }
215  PABC_FREE_NULL (filename);
216  PABC_FREE_NULL (json);
217  pabc_free_ctx (&ctx);
218  return GNUNET_OK;
219 }
220 
221 
223 PABC_write_usr_ctx (char const *const usr_name,
224  char const *const pp_name,
225  struct pabc_context const *const ctx,
226  struct pabc_public_parameters const *const pp,
227  struct pabc_user_context *const usr_ctx)
228 {
229 
230  char *pp_filename;
231  char *json = NULL;
232  enum pabc_status status;
233  char *fname = NULL;
234 
235  if (NULL == usr_name)
236  {
237  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No issuer given.\n");
238  return GNUNET_SYSERR;
239  }
240  if (NULL == pp_name)
241  {
242  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No user given.\n");
243  return GNUNET_SYSERR;
244  }
245  if (NULL == ctx)
246  {
247  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No context given.\n");
248  return GNUNET_SYSERR;
249  }
250  if (NULL == pp)
251  {
252  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No public parameters given.\n");
253  return GNUNET_SYSERR;
254  }
255  if (NULL == usr_ctx)
256  {
257  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No user context given.\n");
258  return GNUNET_SYSERR;
259  }
260 
261  GNUNET_STRINGS_urlencode (pp_name, strlen (pp_name), &pp_filename);
262  status = pabc_encode_user_ctx (ctx, pp, usr_ctx, &json);
263  if (PABC_OK != status)
264  {
265  GNUNET_free (pp_filename);
266  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to encode user context.\n");
267  return status;
268  }
269 
270  size_t fname_size = strlen (get_pabcdir ()) + 1 + strlen (usr_name) + 1
271  + strlen (pp_filename) + strlen (PABC_USR_EXT) + 1;
272  fname = GNUNET_malloc (fname_size);
273 
274  snprintf (fname, fname_size, "%s/%s_%s%s", get_pabcdir (), usr_name,
275  pp_filename,
276  PABC_USR_EXT);
277 
278  GNUNET_free (pp_filename);
279  if (GNUNET_OK == write_file (fname, json))
280  {
281  GNUNET_free (fname);
282  GNUNET_free (json);
283  return GNUNET_OK;
284  }
285  else
286  {
287  GNUNET_free (fname);
288  GNUNET_free (json);
289  return GNUNET_SYSERR;
290  }
291 }
292 
293 
295 PABC_read_usr_ctx (char const *const usr_name,
296  char const *const pp_name,
297  struct pabc_context const *const ctx,
298  struct pabc_public_parameters const *const pp,
299  struct pabc_user_context **usr_ctx)
300 {
301  char *json = NULL;
302  char *pp_filename;
303  enum pabc_status status;
304 
305  char *fname = NULL;
306 
307  if (NULL == usr_name)
308  {
309  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No issuer given.\n");
310  return GNUNET_SYSERR;
311  }
312  if (NULL == pp_name)
313  {
314  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No user given.\n");
315  return GNUNET_SYSERR;
316  }
317  if (NULL == ctx)
318  {
319  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No context given.\n");
320  return GNUNET_SYSERR;
321  }
322  if (NULL == pp)
323  {
324  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No public parameters given.\n");
325  return GNUNET_SYSERR;
326  }
327  if (NULL == usr_ctx)
328  {
329  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No user context given.\n");
330  return GNUNET_SYSERR;
331  }
332  GNUNET_STRINGS_urlencode (pp_name, strlen (pp_name), &pp_filename);
333 
334  size_t fname_size = strlen (get_pabcdir ()) + 1 + strlen (usr_name) + 1
335  + strlen (pp_filename) + strlen (PABC_USR_EXT) + 1;
336  fname = GNUNET_malloc (fname_size);
337  snprintf (fname, fname_size, "%s/%s_%s%s", get_pabcdir (), usr_name,
338  pp_filename,
339  PABC_USR_EXT);
340  GNUNET_free (pp_filename);
341  if (GNUNET_OK != read_file (fname, &json))
342  {
344  "Failed to read `%s'\n", fname);
345  PABC_FREE_NULL (fname);
346  return GNUNET_SYSERR;
347  }
348  GNUNET_free (fname);
349 
350  status = pabc_new_user_context (ctx, pp, usr_ctx);
351  if (PABC_OK != status)
352  {
353  GNUNET_free (json);
354  return GNUNET_SYSERR;
355  }
356  status = pabc_decode_user_ctx (ctx, pp, *usr_ctx, json);
357  GNUNET_free (json);
358  if (PABC_OK != status)
359  {
360  pabc_free_user_context (ctx, pp, usr_ctx);
361  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to encode user context.\n");
362  return GNUNET_SYSERR;
363  }
364 
365  return GNUNET_OK;
366 }
char * getenv()
static char * filename
uint16_t status
See PRISM_STATUS_*-constants.
static struct GNUNET_DISK_FileHandle * fh
File handle to STDIN, for reading restart/quit commands.
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
struct GNUNET_DISK_FileHandle * GNUNET_DISK_file_open(const char *fn, enum GNUNET_DISK_OpenFlags flags, enum GNUNET_DISK_AccessPermissions perm)
Open a file.
Definition: disk.c:1234
enum GNUNET_GenericReturnValue GNUNET_DISK_file_test(const char *fil)
Check that fil corresponds to a filename (of a file that exists and that is not a directory).
Definition: disk.c:481
ssize_t GNUNET_DISK_file_write(const struct GNUNET_DISK_FileHandle *h, const void *buffer, size_t n)
Write a buffer to a file.
Definition: disk.c:685
off_t GNUNET_DISK_file_seek(const struct GNUNET_DISK_FileHandle *h, off_t offset, enum GNUNET_DISK_Seek whence)
Move the read/write pointer in a file.
Definition: disk.c:205
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_test(const char *fil, int is_readable)
Test if fil is a directory and listable.
Definition: disk.c:403
enum GNUNET_GenericReturnValue GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1305
ssize_t GNUNET_DISK_file_read(const struct GNUNET_DISK_FileHandle *h, void *result, size_t len)
Read the contents of a binary file into a buffer.
Definition: disk.c:621
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_create(const char *dir)
Implementation of "mkdir -p".
Definition: disk.c:495
@ GNUNET_DISK_OPEN_READ
Open the file for reading.
@ GNUNET_DISK_OPEN_WRITE
Open the file for writing.
@ GNUNET_DISK_OPEN_TRUNCATE
Truncate file if it exists.
@ GNUNET_DISK_OPEN_CREATE
Create file if it doesn't exist.
@ GNUNET_DISK_PERM_USER_READ
Owner can read.
@ GNUNET_DISK_PERM_USER_WRITE
Owner can write.
@ GNUNET_DISK_SEEK_SET
Seek an absolute position (from the start of the file).
@ GNUNET_DISK_SEEK_END
Seek an absolute position from the end of the file.
#define GNUNET_log(kind,...)
GNUNET_GenericReturnValue
Named constants for return values.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_SYSERR
@ GNUNET_ERROR_TYPE_ERROR
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
size_t GNUNET_STRINGS_urlencode(const char *data, size_t len, char **out)
url/percent encode (RFC3986).
Definition: strings.c:1850
enum GNUNET_GenericReturnValue read_file(char const *const filename, char **buffer)
Definition: pabc_helper.c:71
static char pabc_dir[4096+1]
Definition: pabc_helper.c:13
enum GNUNET_GenericReturnValue PABC_write_public_parameters(char const *const pp_name, struct pabc_public_parameters *const pp)
Definition: pabc_helper.c:171
enum GNUNET_GenericReturnValue PABC_load_public_parameters(struct pabc_context *const ctx, char const *const pp_name, struct pabc_public_parameters **pp)
Definition: pabc_helper.c:135
struct pabc_public_parameters * PABC_read_issuer_ppfile(const char *f, struct pabc_context *const ctx)
Definition: pabc_helper.c:106
static const char * get_pabcdir()
Definition: pabc_helper.c:63
static const char * get_homedir()
Definition: pabc_helper.c:16
static enum GNUNET_GenericReturnValue init_pabc_dir()
Definition: pabc_helper.c:52
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)
Definition: pabc_helper.c:223
static enum GNUNET_GenericReturnValue write_file(char const *const filename, const char *buffer)
Definition: pabc_helper.c:28
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)
Definition: pabc_helper.c:295
#define PABC_USR_EXT
Definition: pabc_helper.h:13
#define PABC_PP_EXT
Definition: pabc_helper.h:11
#define PATH_MAX
Assumed maximum path length.
Definition: platform.h:240
Handle used to access files (and pipes).