GNUnet  0.11.x
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 "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 *
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,
36  if (fh == NULL)
37  return GNUNET_SYSERR;
39  buffer, strlen (buffer) + 1))
40  goto fail;
42  return GNUNET_OK;
43 
44 fail:
46  return GNUNET_SYSERR;
47 }
48 
49 
50 static enum GNUNET_GenericReturnValue
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");
58 }
59 
60 
61 static const char *
63 {
64  init_pabc_dir ();
65  return pabc_dir;
66 }
67 
68 
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,
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;
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 
95  return GNUNET_OK;
96 
97 fail:
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  {
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 
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);
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 
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);
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 
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 
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  {
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 }
Open the file for reading.
static const char * get_pabcdir()
Definition: pabc_helper.c:62
Create file if it doesn&#39;t exist.
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:134
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:622
enum GNUNET_GenericReturnValue GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1305
GNUNET_GenericReturnValue
Named constants for return values.
Definition: gnunet_common.h:83
#define PABC_USR_EXT
Definition: pabc_helper.h:13
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:206
static const char * get_homedir()
Definition: pabc_helper.c:15
struct pabc_public_parameters * PABC_read_issuer_ppfile(const char *f, struct pabc_context *const ctx)
Definition: pabc_helper.c:105
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
static char pabc_dir[4096+1]
Definition: pabc_helper.c:12
static enum GNUNET_GenericReturnValue init_pabc_dir()
Definition: pabc_helper.c:51
#define PABC_PP_EXT
Definition: pabc_helper.h:11
Truncate file if it exists.
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:686
uint16_t status
See PRISM_STATUS_*-constants.
static char * filename
#define PATH_MAX
Assumed maximum path length.
Definition: platform.h:246
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_create(const char *dir)
Implementation of "mkdir -p".
Definition: disk.c:496
static int fh
Handle to the unique file.
enum GNUNET_GenericReturnValue read_file(char const *const filename, char **buffer)
Definition: pabc_helper.c:70
enum GNUNET_GenericReturnValue PABC_write_public_parameters(char const *const pp_name, struct pabc_public_parameters *const pp)
Definition: pabc_helper.c:170
size_t GNUNET_STRINGS_urlencode(const char *data, size_t len, char **out)
url/percent encode (RFC3986).
Definition: strings.c:1830
Seek an absolute position from the end of the file.
Open the file for writing.
char * getenv()
static enum GNUNET_GenericReturnValue write_file(char const *const filename, const char *buffer)
Definition: pabc_helper.c:27
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:222
Seek an absolute position (from the start of the file).
#define GNUNET_log(kind,...)
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:294
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
Handle used to access files (and pipes).
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_test(const char *fil, int is_readable)
Test if fil is a directory and listable.
Definition: disk.c:404
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:482
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.