failing test
[oweals/gnunet.git] / src / util / pseudonym.c
1 /*
2      This file is part of GNUnet
3      (C) 2003, 2004, 2005, 2006, 2007, 2008, 2013 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 2, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /**
21  * @file util/pseudonym.c
22  * @brief helper functions
23  * @author Christian Grothoff
24  *
25  * TODO:
26  * - all cryptographic operations are currently NOT implemented and
27  *   provided by stubs that merely pretend to work!
28  */
29 #include "platform.h"
30 #include "gnunet_common.h"
31 #include "gnunet_container_lib.h"
32 #include "gnunet_disk_lib.h"
33 #include "gnunet_pseudonym_lib.h"
34 #include "gnunet_bio_lib.h"
35 #include <gcrypt.h>
36
37 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
38
39 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
40
41 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
42
43 /**
44  * Log an error message at log-level 'level' that indicates
45  * a failure of the command 'cmd' with the message given
46  * by gcry_strerror(rc).
47  */
48 #define LOG_GCRY(level, cmd, rc) do { LOG(level, _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, gcry_strerror(rc)); } while(0);
49
50 /**
51  * Name of the directory which stores meta data for pseudonym
52  */
53 #define PS_METADATA_DIR DIR_SEPARATOR_STR "data" DIR_SEPARATOR_STR "pseudonym" DIR_SEPARATOR_STR "metadata" DIR_SEPARATOR_STR
54
55 /**
56  * Name of the directory which stores names for pseudonyms
57  */
58 #define PS_NAMES_DIR    DIR_SEPARATOR_STR "data" DIR_SEPARATOR_STR "pseudonym" DIR_SEPARATOR_STR "names"    DIR_SEPARATOR_STR
59
60
61 /**
62  * Configuration section we use.
63  */
64 #define GNUNET_CLIENT_SERVICE_NAME "client"
65
66
67 /* ************************* Disk operations (pseudonym data mgmt) **************** */
68
69 /**
70  * Registered callbacks for discovery of pseudonyms.
71  */
72 struct GNUNET_PSEUDONYM_DiscoveryHandle
73 {
74   /**
75    * This is a doubly linked list.
76    */
77   struct GNUNET_PSEUDONYM_DiscoveryHandle *next;
78
79   /**
80    * This is a doubly linked list.
81    */
82   struct GNUNET_PSEUDONYM_DiscoveryHandle *prev;
83
84   /**
85    * Function to call each time a pseudonym is discovered.
86    */
87   GNUNET_PSEUDONYM_Iterator callback;
88
89   /**
90    * Closure for callback.
91    */
92   void *callback_cls;
93 };
94
95
96 /**
97  * Head of the linked list of functions to call when
98  * new pseudonyms are added.
99  */
100 static struct GNUNET_PSEUDONYM_DiscoveryHandle *disco_head;
101
102 /**
103  * Tail of the linked list of functions to call when
104  * new pseudonyms are added.
105  */
106 static struct GNUNET_PSEUDONYM_DiscoveryHandle *disco_tail;
107
108
109 /**
110  * Internal notification about new tracked URI.
111  *
112  * @param pseudonym public key of the pseudonym
113  * @param md meta data to be written
114  * @param rating rating of pseudonym
115  */
116 static void
117 internal_notify (const struct GNUNET_PseudonymIdentifier *pseudonym,
118                  const struct GNUNET_CONTAINER_MetaData *md, int rating)
119 {
120   struct GNUNET_PSEUDONYM_DiscoveryHandle *pos;
121
122   for (pos = disco_head; NULL != pos; pos = pos->next)
123     pos->callback (pos->callback_cls, pseudonym, NULL, NULL, md, rating);
124 }
125
126
127 /**
128  * Register callback to be invoked whenever we discover
129  * a new pseudonym.
130  * Will immediately call provided iterator callback for all
131  * already discovered pseudonyms.
132  *
133  * @param cfg configuration to use
134  * @param iterator iterator over pseudonym
135  * @param iterator_cls point to a closure
136  * @return registration handle
137  */
138 struct GNUNET_PSEUDONYM_DiscoveryHandle *
139 GNUNET_PSEUDONYM_discovery_callback_register (const struct
140                                               GNUNET_CONFIGURATION_Handle *cfg,
141                                               GNUNET_PSEUDONYM_Iterator iterator, 
142                                               void *iterator_cls)
143 {
144   struct GNUNET_PSEUDONYM_DiscoveryHandle *dh;
145
146   dh = GNUNET_malloc (sizeof (struct GNUNET_PSEUDONYM_DiscoveryHandle));
147   dh->callback = iterator;
148   dh->callback_cls = iterator_cls;
149   GNUNET_CONTAINER_DLL_insert (disco_head, disco_tail, dh);
150   GNUNET_PSEUDONYM_list_all (cfg, iterator, iterator_cls);
151   return dh;
152 }
153
154
155 /**
156  * Unregister pseudonym discovery callback.
157  *
158  * @param dh registration to unregister
159  */
160 void
161 GNUNET_PSEUDONYM_discovery_callback_unregister (struct GNUNET_PSEUDONYM_DiscoveryHandle *dh)
162 {
163   GNUNET_CONTAINER_DLL_remove (disco_head, disco_tail, dh);
164   GNUNET_free (dh);
165 }
166
167
168 /**
169  * Get the filename (or directory name) for the given
170  * pseudonym identifier and directory prefix.
171  *
172  * @param cfg configuration to use
173  * @param prefix path components to append to the private directory name
174  * @param pseudonym the pseudonym, can be NULL
175  * @return filename of the pseudonym (if pseudonym != NULL) or directory with the data (if pseudonym == NULL)
176  */
177 static char *
178 get_data_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
179                    const char *prefix, 
180                    const struct GNUNET_PseudonymIdentifier *pseudonym)
181 {
182   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
183   struct GNUNET_HashCode psid;
184
185   if (NULL != pseudonym)
186   {
187     GNUNET_CRYPTO_hash (pseudonym,
188                         sizeof (struct GNUNET_PseudonymIdentifier),
189                         &psid);
190     GNUNET_CRYPTO_hash_to_enc (&psid, &enc);
191   }
192   return GNUNET_DISK_get_home_filename (cfg, 
193                                         GNUNET_CLIENT_SERVICE_NAME, prefix,
194                                         (NULL == pseudonym) 
195                                         ? NULL 
196                                         : (const char *) &enc,
197                                         NULL);
198 }
199
200
201 /**
202  * Get the filename (or directory name) for the given
203  * hash code and directory prefix.
204  *
205  * @param cfg configuration to use
206  * @param prefix path components to append to the private directory name
207  * @param hc some hash code
208  * @return filename of the pseudonym (if hc != NULL) or directory with the data (if hc == NULL)
209  */
210 static char *
211 get_data_filename_hash (const struct GNUNET_CONFIGURATION_Handle *cfg,
212                         const char *prefix, 
213                         const struct GNUNET_HashCode *hc)
214 {
215   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
216
217   if (NULL != hc)
218     GNUNET_CRYPTO_hash_to_enc (hc, &enc);
219   return GNUNET_DISK_get_home_filename (cfg, 
220                                         GNUNET_CLIENT_SERVICE_NAME, prefix,
221                                         (NULL == hc) 
222                                         ? NULL 
223                                         : (const char *) &enc,
224                                         NULL);
225 }
226
227
228 /**
229  * Set the pseudonym metadata, rank and name.
230  * Writes the pseudonym infomation into a file
231  *
232  * @param cfg overall configuration
233  * @param pseudonym id of the pseudonym
234  * @param name name to set. Must be the non-unique version of it.
235  *        May be NULL, in which case it erases pseudonym's name!
236  * @param md metadata to set
237  *        May be NULL, in which case it erases pseudonym's metadata!
238  * @param rank rank to assign
239  * @return GNUNET_OK on success, GNUNET_SYSERR on failure
240  */
241 int
242 GNUNET_PSEUDONYM_set_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
243                            const struct GNUNET_PseudonymIdentifier *pseudonym,
244                            const char *name,
245                            const struct GNUNET_CONTAINER_MetaData *md, 
246                            int32_t rank)
247 {
248   char *fn;
249   struct GNUNET_BIO_WriteHandle *fileW;
250
251   fn = get_data_filename (cfg, PS_METADATA_DIR, pseudonym);
252   if (NULL == (fileW = GNUNET_BIO_write_open (fn)))
253   {
254     GNUNET_free (fn);
255     return GNUNET_SYSERR;
256   }
257   if ((GNUNET_OK != GNUNET_BIO_write (fileW, pseudonym, 
258                                       sizeof (struct GNUNET_PseudonymIdentifier))) ||
259       (GNUNET_OK != GNUNET_BIO_write_int32 (fileW, rank)) ||
260       (GNUNET_OK != GNUNET_BIO_write_string (fileW, name)) ||
261       (GNUNET_OK != GNUNET_BIO_write_meta_data (fileW, md)))
262   {
263     (void) GNUNET_BIO_write_close (fileW);
264     GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
265     GNUNET_free (fn);
266     return GNUNET_SYSERR;
267   }
268   if (GNUNET_OK != GNUNET_BIO_write_close (fileW))
269   {
270     GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
271     GNUNET_free (fn);
272     return GNUNET_SYSERR;
273   } 
274   GNUNET_free (fn);
275   /* create entry for pseudonym name in names */
276   if (NULL != name)
277     GNUNET_free_non_null (GNUNET_PSEUDONYM_name_uniquify (cfg, pseudonym, 
278                                                           name, NULL));
279   return GNUNET_OK;
280 }
281
282
283 /**
284  * Read pseudonym infomation from a file
285  *
286  * @param cfg configuration to use
287  * @param pseudonym hash code of a pseudonym
288  * @param meta meta data to be read from a file
289  * @param rank rank of a pseudonym
290  * @param ns_name name of a pseudonym
291  * @return GNUNET_OK on success, GNUNET_SYSERR on error
292  */
293 static int
294 read_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
295            const struct GNUNET_PseudonymIdentifier *pseudonym,
296            struct GNUNET_CONTAINER_MetaData **meta,
297            int32_t *rank,
298            char **ns_name)
299 {
300   struct GNUNET_PseudonymIdentifier pd;
301   char *fn;
302   char *emsg;
303   struct GNUNET_BIO_ReadHandle *fileR;
304
305   fn = get_data_filename (cfg, PS_METADATA_DIR, pseudonym);
306   if (GNUNET_YES !=
307       GNUNET_DISK_file_test (fn))
308   {
309     GNUNET_free (fn);
310     return GNUNET_SYSERR;
311   }
312   if (NULL == (fileR = GNUNET_BIO_read_open (fn)))
313   {
314     GNUNET_free (fn);
315     return GNUNET_SYSERR;
316   }
317   emsg = NULL;
318   *ns_name = NULL;
319   if ( (GNUNET_OK != GNUNET_BIO_read (fileR, "pseudonym", &pd, sizeof (pd))) ||
320        (0 != memcmp (&pd, pseudonym, sizeof (pd))) ||
321        (GNUNET_OK != GNUNET_BIO_read_int32 (fileR, rank)) ||
322        (GNUNET_OK !=
323         GNUNET_BIO_read_string (fileR, "Read string error!", ns_name, 200)) ||
324        (GNUNET_OK !=
325        GNUNET_BIO_read_meta_data (fileR, "Read meta data error!", meta)) )
326   {
327     (void) GNUNET_BIO_read_close (fileR, &emsg);
328     GNUNET_free_non_null (emsg);
329     GNUNET_free_non_null (*ns_name);
330     *ns_name = NULL;
331     GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
332     GNUNET_free (fn);
333     return GNUNET_SYSERR;
334   }
335   if (GNUNET_OK != GNUNET_BIO_read_close (fileR, &emsg))
336   {
337     LOG (GNUNET_ERROR_TYPE_WARNING,
338          _("Failed to parse metadata about pseudonym from file `%s': %s\n"), fn,
339          emsg);
340     GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
341     GNUNET_CONTAINER_meta_data_destroy (*meta);
342     *meta = NULL;
343     GNUNET_free_non_null (*ns_name);
344     *ns_name = NULL;
345     GNUNET_free_non_null (emsg);
346     GNUNET_free (fn);
347     return GNUNET_SYSERR;
348   }
349   GNUNET_free (fn);
350   return GNUNET_OK;
351 }
352
353
354 /**
355  * Return unique variant of the namespace name.  Use it after
356  * GNUNET_PSEUDONYM_get_info() to make sure that name is unique.
357  *
358  * @param cfg configuration
359  * @param pseudonym public key of the pseudonym
360  * @param name name to uniquify
361  * @param suffix if not NULL, filled with the suffix value
362  * @return NULL on failure (should never happen), name on success.
363  *         Free the name with GNUNET_free().
364  */
365 char *
366 GNUNET_PSEUDONYM_name_uniquify (const struct GNUNET_CONFIGURATION_Handle *cfg,
367                                 const struct GNUNET_PseudonymIdentifier *pseudonym,
368                                 const char *name,
369                                 unsigned int *suffix)
370 {
371   struct GNUNET_HashCode nh;
372   struct GNUNET_PseudonymIdentifier pi;
373   uint64_t len;
374   char *fn;
375   struct GNUNET_DISK_FileHandle *fh;
376   unsigned int i;
377   unsigned int idx;
378   char *ret;
379   struct stat sbuf;
380
381   GNUNET_CRYPTO_hash (name, strlen (name), &nh);
382   fn = get_data_filename_hash (cfg, PS_NAMES_DIR, &nh);
383   len = 0;
384   if (0 == STAT (fn, &sbuf))
385     GNUNET_break (GNUNET_OK == GNUNET_DISK_file_size (fn, &len, GNUNET_YES, GNUNET_YES));
386   fh = GNUNET_DISK_file_open (fn,
387                               GNUNET_DISK_OPEN_CREATE |
388                               GNUNET_DISK_OPEN_READWRITE,
389                               GNUNET_DISK_PERM_USER_READ |
390                               GNUNET_DISK_PERM_USER_WRITE);
391   i = 0;
392   idx = -1;
393   while ((len >= sizeof (struct GNUNET_PseudonymIdentifier)) &&
394          (sizeof (struct GNUNET_PseudonymIdentifier) ==
395           GNUNET_DISK_file_read (fh, &pi, sizeof (struct GNUNET_PseudonymIdentifier))))
396   {
397     if (0 == memcmp (&pi, pseudonym, sizeof (struct GNUNET_PseudonymIdentifier)))
398     {
399       idx = i;
400       break;
401     }
402     i++;
403     len -= sizeof (struct GNUNET_HashCode);
404   }
405   if (-1 == idx)
406   {
407     idx = i;
408     if (sizeof (struct GNUNET_PseudonymIdentifier) !=
409         GNUNET_DISK_file_write (fh, pseudonym, sizeof (struct GNUNET_PseudonymIdentifier)))
410       LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "write", fn);
411   }
412   GNUNET_DISK_file_close (fh);
413   ret = GNUNET_malloc (strlen (name) + 32);
414   GNUNET_snprintf (ret, strlen (name) + 32, "%s-%u", name, idx);
415   if (suffix != NULL)
416     *suffix = idx;
417   GNUNET_free (fn);
418   return ret;
419 }
420
421
422 /**
423  * Get namespace name, metadata and rank
424  * This is a wrapper around internal read_info() call, and ensures that
425  * returned data is not invalid (not NULL).
426  *
427  * @param cfg configuration
428  * @param pseudonym public key of the pseudonym
429  * @param ret_meta a location to store metadata pointer. NULL, if metadata
430  *        is not needed. Destroy with GNUNET_CONTAINER_meta_data_destroy().
431  * @param ret_rank a location to store rank. NULL, if rank not needed.
432  * @param ret_name a location to store human-readable name. Name is not unique.
433  *        NULL, if name is not needed. Free with GNUNET_free().
434  * @param name_is_a_dup is set to GNUNET_YES, if ret_name was filled with
435  *        a duplicate of a "no-name" placeholder
436  * @return GNUNET_OK on success. GNUENT_SYSERR if the data was
437  *         unobtainable (in that case ret_* are filled with placeholders - 
438  *         empty metadata container, rank -1 and a "no-name" name).
439  */
440 int
441 GNUNET_PSEUDONYM_get_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
442                            const struct GNUNET_PseudonymIdentifier *pseudonym, 
443                            struct GNUNET_CONTAINER_MetaData **ret_meta,
444                            int32_t *ret_rank, 
445                            char **ret_name, 
446                            int *name_is_a_dup)
447 {
448   struct GNUNET_CONTAINER_MetaData *meta;
449   char *name;
450   int32_t rank = -1;
451
452   meta = NULL;
453   name = NULL;
454   if (GNUNET_OK == read_info (cfg, pseudonym, &meta, &rank, &name))
455   {
456     if ((meta != NULL) && (name == NULL))
457       name =
458           GNUNET_CONTAINER_meta_data_get_first_by_types (meta,
459                                                          EXTRACTOR_METATYPE_TITLE,
460                                                          EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME,
461                                                          EXTRACTOR_METATYPE_FILENAME,
462                                                          EXTRACTOR_METATYPE_DESCRIPTION,
463                                                          EXTRACTOR_METATYPE_SUBJECT,
464                                                          EXTRACTOR_METATYPE_PUBLISHER,
465                                                          EXTRACTOR_METATYPE_AUTHOR_NAME,
466                                                          EXTRACTOR_METATYPE_COMMENT,
467                                                          EXTRACTOR_METATYPE_SUMMARY,
468                                                          -1);
469     if (ret_name != NULL)
470     {
471       if (name == NULL)
472       {
473         name = GNUNET_strdup (_("no-name"));
474         if (name_is_a_dup != NULL)
475           *name_is_a_dup = GNUNET_YES;
476       }
477       else if (name_is_a_dup != NULL)
478         *name_is_a_dup = GNUNET_NO;
479       *ret_name = name;
480     }
481     else if (name != NULL)
482       GNUNET_free (name);
483
484     if (ret_meta != NULL)
485     {
486       if (meta == NULL)
487         meta = GNUNET_CONTAINER_meta_data_create ();
488       *ret_meta = meta;
489     }
490     else if (meta != NULL)
491       GNUNET_CONTAINER_meta_data_destroy (meta);
492
493     if (ret_rank != NULL)
494       *ret_rank = rank;
495
496     return GNUNET_OK;
497   }
498   if (ret_name != NULL)
499     *ret_name = GNUNET_strdup (_("no-name"));
500   if (ret_meta != NULL)
501     *ret_meta = GNUNET_CONTAINER_meta_data_create ();
502   if (ret_rank != NULL)
503     *ret_rank = -1;
504   if (name_is_a_dup != NULL)
505     *name_is_a_dup = GNUNET_YES;
506   return GNUNET_SYSERR;
507 }
508
509
510 /**
511  * Get the namespace ID belonging to the given namespace name.
512  *
513  * @param cfg configuration to use
514  * @param ns_uname unique (!) human-readable name for the namespace
515  * @param pseudonym set to public key of pseudonym based on 'ns_uname'
516  * @return GNUNET_OK on success, GNUNET_SYSERR on failure
517  */
518 int
519 GNUNET_PSEUDONYM_name_to_id (const struct GNUNET_CONFIGURATION_Handle *cfg,
520                              const char *ns_uname, 
521                              struct GNUNET_PseudonymIdentifier *pseudonym)
522 {
523   size_t slen;
524   uint64_t len;
525   unsigned int idx;
526   char *name;
527   struct GNUNET_HashCode nh;
528   char *fn;
529   struct GNUNET_DISK_FileHandle *fh;
530
531   idx = -1;
532   slen = strlen (ns_uname);
533   while ((slen > 0) && (1 != SSCANF (&ns_uname[slen - 1], "-%u", &idx)))
534     slen--;
535   if (0 == slen)
536     return GNUNET_SYSERR;
537   name = GNUNET_strdup (ns_uname);
538   name[slen - 1] = '\0';
539
540   GNUNET_CRYPTO_hash (name, strlen (name), &nh);
541   GNUNET_free (name);
542   fn = get_data_filename_hash (cfg, PS_NAMES_DIR, &nh);
543
544   if ((GNUNET_OK != GNUNET_DISK_file_test (fn) ||
545        (GNUNET_OK != GNUNET_DISK_file_size (fn, &len, GNUNET_YES, GNUNET_YES))) ||
546       ((idx + 1) * sizeof (struct GNUNET_PseudonymIdentifier) > len))
547   {
548     GNUNET_free (fn);
549     return GNUNET_SYSERR;
550   }
551   fh = GNUNET_DISK_file_open (fn,
552                               GNUNET_DISK_OPEN_CREATE |
553                               GNUNET_DISK_OPEN_READWRITE,
554                               GNUNET_DISK_PERM_USER_READ |
555                               GNUNET_DISK_PERM_USER_WRITE);
556   GNUNET_free (fn);
557   if (GNUNET_SYSERR ==
558       GNUNET_DISK_file_seek (fh, idx * sizeof (struct GNUNET_PseudonymIdentifier),
559                              GNUNET_DISK_SEEK_SET))
560   {
561     GNUNET_DISK_file_close (fh);
562     return GNUNET_SYSERR;
563   }
564   if (sizeof (struct GNUNET_PseudonymIdentifier) !=
565       GNUNET_DISK_file_read (fh, pseudonym, sizeof (struct GNUNET_PseudonymIdentifier)))
566   {
567     GNUNET_DISK_file_close (fh);
568     return GNUNET_SYSERR;
569   }
570   GNUNET_DISK_file_close (fh);
571   return GNUNET_OK;
572 }
573
574
575
576 /**
577  * struct used to list the pseudonym
578  */
579 struct ListPseudonymClosure
580 {
581
582   /**
583    * iterator over pseudonym
584    */
585   GNUNET_PSEUDONYM_Iterator iterator;
586
587   /**
588    * Closure for iterator.
589    */
590   void *iterator_cls;
591
592   /**
593    * Configuration to use.
594    */
595   const struct GNUNET_CONFIGURATION_Handle *cfg;
596 };
597
598
599
600 /**
601  * Helper function to list all available pseudonyms
602  *
603  * @param cls point to a struct ListPseudonymClosure
604  * @param fullname name of pseudonym
605  */
606 static int
607 list_pseudonym_helper (void *cls, const char *fullname)
608 {
609   struct ListPseudonymClosure *lpc = cls;
610   struct GNUNET_PseudonymIdentifier pd;
611   char *emsg;
612   struct GNUNET_BIO_ReadHandle *fileR;
613   int32_t rank;
614   char *ns_name;
615   struct GNUNET_CONTAINER_MetaData *meta;
616   int ret; 
617   char *name_unique;
618
619   if (NULL == (fileR = GNUNET_BIO_read_open (fullname)))
620     return GNUNET_SYSERR;
621   emsg = NULL;
622   ns_name = NULL;
623   if ( (GNUNET_OK != GNUNET_BIO_read (fileR, "pseudonym", &pd, sizeof (pd))) ||
624        (GNUNET_OK != GNUNET_BIO_read_int32 (fileR, &rank)) ||
625        (GNUNET_OK !=
626         GNUNET_BIO_read_string (fileR, "Read string error!", &ns_name, 200)) ||
627        (GNUNET_OK !=
628        GNUNET_BIO_read_meta_data (fileR, "Read meta data error!", &meta)) )
629   {
630     (void) GNUNET_BIO_read_close (fileR, &emsg);
631     GNUNET_free_non_null (emsg);
632     GNUNET_free_non_null (ns_name);
633     GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fullname));
634     return GNUNET_SYSERR;
635   }
636   if (NULL == ns_name)
637     ns_name = GNUNET_strdup (_("no-name"));
638   if (GNUNET_OK != GNUNET_BIO_read_close (fileR, &emsg))
639   {
640     LOG (GNUNET_ERROR_TYPE_WARNING,
641          _("Failed to parse metadata about pseudonym from file `%s': %s\n"), fullname,
642          emsg);
643     GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fullname));
644     GNUNET_CONTAINER_meta_data_destroy (meta);
645     GNUNET_free (ns_name);
646     GNUNET_free_non_null (emsg);
647     return GNUNET_SYSERR;
648   }
649   ret = GNUNET_OK;
650   name_unique = GNUNET_PSEUDONYM_name_uniquify (lpc->cfg, &pd, ns_name, NULL);
651   if (NULL != lpc->iterator)
652     ret = lpc->iterator (lpc->iterator_cls, &pd, ns_name, name_unique, meta, rank);
653   GNUNET_free (ns_name);
654   GNUNET_free_non_null (name_unique);
655   GNUNET_CONTAINER_meta_data_destroy (meta);
656   return ret;
657 }
658
659
660 /**
661  * List all available pseudonyms.
662  *
663  * @param cfg overall configuration
664  * @param iterator function to call for each pseudonym
665  * @param iterator_cls closure for iterator
666  * @return number of pseudonyms found
667  */
668 int
669 GNUNET_PSEUDONYM_list_all (const struct GNUNET_CONFIGURATION_Handle *cfg,
670                            GNUNET_PSEUDONYM_Iterator iterator, 
671                            void *iterator_cls)
672 {
673   struct ListPseudonymClosure cls;
674   char *fn;
675   int ret;
676
677   cls.iterator = iterator;
678   cls.iterator_cls = iterator_cls;
679   cls.cfg = cfg;
680   fn = get_data_filename (cfg, PS_METADATA_DIR, NULL);
681   GNUNET_assert (fn != NULL);
682   GNUNET_DISK_directory_create (fn);
683   ret = GNUNET_DISK_directory_scan (fn, &list_pseudonym_helper, &cls);
684   GNUNET_free (fn);
685   return ret;
686 }
687
688
689 /**
690  * Change the rank of a pseudonym.
691  *
692  * @param cfg overall configuration
693  * @param pseudonym the pseudonym
694  * @param delta by how much should the rating be changed?
695  * @return new rating of the pseudonym
696  */
697 int
698 GNUNET_PSEUDONYM_rank (const struct GNUNET_CONFIGURATION_Handle *cfg,
699                        const struct GNUNET_PseudonymIdentifier *pseudonym, 
700                        int32_t delta)
701 {
702   struct GNUNET_CONTAINER_MetaData *meta;
703   int ret;
704   int32_t rank;
705   char *name;
706
707   name = NULL;
708   ret = read_info (cfg, pseudonym, &meta, &rank, &name);
709   if (ret == GNUNET_SYSERR)
710   {
711     rank = 0;
712     meta = GNUNET_CONTAINER_meta_data_create ();
713   }
714   rank += delta;
715   GNUNET_PSEUDONYM_set_info (cfg, pseudonym, name, meta, rank);
716   GNUNET_CONTAINER_meta_data_destroy (meta);
717   GNUNET_free_non_null (name);
718   return rank;
719 }
720
721
722 /**
723  * Add a pseudonym to the set of known pseudonyms.
724  * For all pseudonym advertisements that we discover
725  * FS should automatically call this function.
726  *
727  * @param cfg overall configuration
728  * @param pseudonym the pseudonym to add
729  * @param meta metadata for the pseudonym
730  * @return GNUNET_OK on success, GNUNET_SYSERR on failure
731  */
732 int
733 GNUNET_PSEUDONYM_add (const struct GNUNET_CONFIGURATION_Handle *cfg,
734                       const struct GNUNET_PseudonymIdentifier *pseudonym,
735                       const struct GNUNET_CONTAINER_MetaData *meta)
736 {
737   char *name;
738   int32_t rank;
739   struct GNUNET_CONTAINER_MetaData *old;
740   char *fn;
741   struct stat sbuf;
742   int ret;
743
744   rank = 0;
745   fn = get_data_filename (cfg, PS_METADATA_DIR, pseudonym);
746   GNUNET_assert (fn != NULL);
747
748   if ((0 == STAT (fn, &sbuf)) &&
749       (GNUNET_OK == read_info (cfg, pseudonym, &old, &rank, &name)))
750   {
751     GNUNET_CONTAINER_meta_data_merge (old, meta);
752     ret = GNUNET_PSEUDONYM_set_info (cfg, pseudonym, name, old, rank);
753     GNUNET_CONTAINER_meta_data_destroy (old);
754     GNUNET_free_non_null (name);
755   }
756   else
757   {
758     ret = GNUNET_PSEUDONYM_set_info (cfg, pseudonym, NULL, meta, rank);
759   }
760   GNUNET_free (fn);
761   internal_notify (pseudonym, meta, rank);
762   return ret;
763 }
764
765
766 /* ***************************** cryptographic operations ************************* */
767
768 /**
769  * Handle for a pseudonym (private key).
770  */
771 struct GNUNET_PseudonymHandle
772 {
773   /**
774    * 256-bit 'd' secret value (mod 'n', where n is 256-bit for NIST P-256).
775    */
776   unsigned char d[256 / 8];
777
778   /**
779    * Public key corresponding to the private key.
780    */
781   struct GNUNET_PseudonymIdentifier public_key;
782 };
783
784
785 /**
786  * If target != size, move target bytes to the end of the size-sized
787  * buffer and zero out the first target-size bytes.
788  *
789  * @param buf original buffer
790  * @param size number of bytes in the buffer
791  * @param target target size of the buffer
792  */
793 static void
794 adjust (unsigned char *buf, size_t size, size_t target)
795 {
796   if (size < target)
797   {
798     memmove (&buf[target - size], buf, size);
799     memset (buf, 0, target - size);
800   }
801 }
802
803
804 /**
805  * Extract values from an S-expression.
806  *
807  * @param array where to store the result(s)
808  * @param sexp S-expression to parse
809  * @param topname top-level name in the S-expression that is of interest
810  * @param elems names of the elements to extract
811  * @return 0 on success
812  */
813 static int
814 key_from_sexp (gcry_mpi_t * array, gcry_sexp_t sexp, const char *topname,
815                const char *elems)
816 {
817   gcry_sexp_t list;
818   gcry_sexp_t l2;
819   const char *s;
820   unsigned int i;
821   unsigned int idx;
822
823   if (! (list = gcry_sexp_find_token (sexp, topname, 0)))
824     return 1;  
825   l2 = gcry_sexp_cadr (list);
826   gcry_sexp_release (list);
827   list = l2;
828   if (! list)  
829     return 2;
830   idx = 0;
831   for (s = elems; *s; s++, idx++)
832   {
833     if (! (l2 = gcry_sexp_find_token (list, s, 1)))
834     {
835       for (i = 0; i < idx; i++)
836       {
837         gcry_free (array[i]);
838         array[i] = NULL;
839       }
840       gcry_sexp_release (list);
841       return 3;                 /* required parameter not found */
842     }
843     array[idx] = gcry_sexp_nth_mpi (l2, 1, GCRYMPI_FMT_USG);
844     gcry_sexp_release (l2);
845     if (! array[idx])
846     {
847       for (i = 0; i < idx; i++)
848       {
849         gcry_free (array[i]);
850         array[i] = NULL;
851       }
852       gcry_sexp_release (list);
853       return 4;                 /* required parameter is invalid */
854     }
855   }
856   gcry_sexp_release (list);
857   return 0;
858 }
859
860
861 /**
862  * Create a pseudonym.
863  *
864  * @param filename name of the file to use for storage, NULL for in-memory only
865  * @return handle to the private key of the pseudonym
866  */
867 struct GNUNET_PseudonymHandle *
868 GNUNET_PSEUDONYM_create (const char *filename)
869 {
870   struct GNUNET_PseudonymHandle *ph;
871   ssize_t ret;
872   gcry_sexp_t r_key;
873   gcry_sexp_t params;
874   gcry_ctx_t ctx;
875   gcry_mpi_point_t q;
876   gcry_mpi_t q_x;
877   gcry_mpi_t q_y;
878   gcry_error_t rc;
879   gcry_mpi_t d;
880   size_t size;
881
882   ph = GNUNET_malloc (sizeof (struct GNUNET_PseudonymHandle));
883   if ( (NULL != filename) &&
884        (GNUNET_YES == GNUNET_DISK_file_test (filename)) )
885   {
886     ret = GNUNET_DISK_fn_read (filename, ph, 
887                                sizeof (struct GNUNET_PseudonymHandle));
888     /* Note: we don't do any validation here, maybe we should? */
889     if (sizeof (struct GNUNET_PseudonymHandle) == ret)
890       return ph;
891   }  
892   if (0 != (rc = gcry_sexp_build (&params, NULL,
893                                   "(genkey(ecdsa(curve \"NIST P-256\")))")))
894   {
895     LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
896     return NULL;
897   }
898   if (0 != (rc = gcry_pk_genkey (&r_key, params)))
899   {
900     LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_genkey", rc);
901     gcry_sexp_release (r_key);
902     return NULL;
903   }
904   /* extract "d" (secret key) from r_key */
905   rc = key_from_sexp (&d, r_key, "private-key", "d");
906   if (0 != rc)
907     rc = key_from_sexp (&d, r_key, "private-key", "d");
908   if (0 != rc)
909     rc = key_from_sexp (&d, r_key, "ecc", "d");
910   if (0 != rc)
911   {
912     gcry_sexp_release (r_key);
913     LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "key_from_sexp", rc);
914     return NULL;
915   }
916   size = sizeof (ph->d);
917   GNUNET_assert (0 ==
918                  gcry_mpi_print (GCRYMPI_FMT_USG, ph->d, size, &size,
919                                  d));
920   gcry_mpi_release (d);
921   adjust (ph->d, size, sizeof (ph->d));
922
923   /* extract 'q' (public key) from r_key */
924   if (0 != (rc = gcry_mpi_ec_new (&ctx, r_key, NULL)))
925   {
926     LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_ec_new", rc);  /* erroff gives more info */
927     gcry_sexp_release (r_key);
928     return NULL;
929   }  
930   gcry_sexp_release (r_key);
931   q = gcry_mpi_ec_get_point ("q", ctx, 0);
932   q_x = gcry_mpi_new (256);
933   q_y = gcry_mpi_new (256);
934   gcry_mpi_ec_get_affine (q_x, q_y, q, ctx);
935   gcry_mpi_point_release (q);
936
937   /* store q_x/q_y in public key */
938   size = sizeof (ph->public_key.q_x);  
939   if (0 !=
940       gcry_mpi_print (GCRYMPI_FMT_USG, ph->public_key.q_x, size, &size,
941                       q_x))
942   {
943     LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_print", rc);
944     gcry_mpi_release (q_x);
945     gcry_mpi_release (q_y);
946     return NULL;
947
948   }
949   adjust (ph->public_key.q_x, size, sizeof (ph->public_key.q_x));
950   gcry_mpi_release (q_x);
951
952   size = sizeof (ph->public_key.q_y);  
953   if (0 !=
954       gcry_mpi_print (GCRYMPI_FMT_USG, ph->public_key.q_y, size, &size,
955                       q_y))
956   {
957     LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_print", rc);
958     gcry_mpi_release (q_y);
959     return NULL;
960   }
961   adjust (ph->public_key.q_y, size, sizeof (ph->public_key.q_y));
962   gcry_mpi_release (q_y);
963
964   /* write to disk */
965   if (NULL != filename)
966   {
967     ret = GNUNET_DISK_fn_write (filename, ph, sizeof (struct GNUNET_PseudonymHandle),
968                                 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
969     if (sizeof (struct GNUNET_PseudonymHandle) != ret)
970     {
971       GNUNET_free (ph);
972       return NULL;
973     }
974   }
975   return ph;
976 }
977
978
979 /**
980  * Create a pseudonym, from a file that must already exist.
981  *
982  * @param filename name of the file to use for storage, NULL for in-memory only
983  * @return handle to the private key of the pseudonym
984  */
985 struct GNUNET_PseudonymHandle *
986 GNUNET_PSEUDONYM_create_from_existing_file (const char *filename)
987 {
988   struct GNUNET_PseudonymHandle *ph;
989   ssize_t ret;
990
991   ph = GNUNET_malloc (sizeof (struct GNUNET_PseudonymHandle));
992   ret = GNUNET_DISK_fn_read (filename, ph, 
993                              sizeof (struct GNUNET_PseudonymHandle));
994   if (sizeof (struct GNUNET_PseudonymHandle) != ret)
995   {
996     GNUNET_free (ph);
997     return NULL;
998   }
999   /* Note: we don't do any validation here; maybe we should? */
1000   return ph;
1001 }
1002
1003
1004 /**
1005  * Get the handle for the 'anonymous' pseudonym shared by all users.
1006  * That pseudonym uses a fixed 'secret' for the private key; this
1007  * construction is useful to make anonymous and pseudonymous APIs
1008  * (and packets) indistinguishable on the network.  See #2564.
1009  *
1010  * @return handle to the (non-secret) private key of the 'anonymous' pseudonym
1011  */
1012 struct GNUNET_PseudonymHandle *
1013 GNUNET_PSEUDONYM_get_anonymous_pseudonym_handle ()
1014 {
1015   struct GNUNET_PseudonymHandle *ph;
1016
1017   ph = GNUNET_malloc (sizeof (struct GNUNET_PseudonymHandle));
1018   /* Note if we use 'd=0' for the anonymous handle (as per#2564),
1019      then I believe the public key should be also zero, as Q=0P=0;
1020      so setting everything to all-zeros (as per GNUNET_malloc)
1021      should be all that is needed here).
1022   */
1023   return ph;
1024 }
1025
1026
1027 /**
1028  * Destroy a pseudonym handle.  Does NOT remove the private key from
1029  * the disk.
1030  *
1031  * @param ph pseudonym handle to destroy
1032  */
1033 void
1034 GNUNET_PSEUDONYM_destroy (struct GNUNET_PseudonymHandle *ph)
1035 {
1036   GNUNET_free (ph);
1037 }
1038
1039
1040 /**
1041  * Convert the data specified in the given purpose argument to an
1042  * S-expression suitable for signature operations.
1043  *
1044  * @param purpose data to convert
1045  * @return converted s-expression
1046  */
1047 static gcry_sexp_t
1048 data_to_pkcs1 (const struct GNUNET_PseudonymSignaturePurpose *purpose)
1049 {
1050   struct GNUNET_CRYPTO_ShortHashCode hc;
1051   size_t bufSize;
1052   gcry_sexp_t data;
1053
1054   GNUNET_CRYPTO_short_hash (purpose, ntohl (purpose->size), &hc);
1055 #define FORMATSTRING "(4:data(5:flags3:raw)(5:value32:01234567890123456789012345678901))"
1056   bufSize = strlen (FORMATSTRING) + 1;
1057   {
1058     char buff[bufSize];
1059
1060     memcpy (buff, FORMATSTRING, bufSize);
1061     memcpy (&buff
1062             [bufSize -
1063              strlen
1064              ("01234567890123456789012345678901))")
1065              - 1], &hc, sizeof (struct GNUNET_CRYPTO_ShortHashCode));
1066     GNUNET_assert (0 == gcry_sexp_new (&data, buff, bufSize, 0));
1067   }
1068 #undef FORMATSTRING
1069   return data;
1070 }
1071
1072
1073 /**
1074  * Cryptographically sign some data with the pseudonym.
1075  *
1076  * @param ph private key used for signing (corresponds to 'x' in #2564)
1077  * @param purpose data to sign
1078  * @param seed hash of the plaintext of the data that we are signing, 
1079  *             used for deterministic PRNG for anonymous signing;
1080  *             corresponds to 'k' in section 2.7 of #2564
1081  * @param signing_key modifier to apply to the private key for signing;
1082  *                    corresponds to 'h' in section 2.3 of #2564.
1083  * @param signature where to store the signature
1084  * @return GNUNET_SYSERR on failure
1085  */
1086 int 
1087 GNUNET_PSEUDONYM_sign (struct GNUNET_PseudonymHandle *ph,
1088                        const struct GNUNET_PseudonymSignaturePurpose *purpose,
1089                        const struct GNUNET_HashCode *seed,
1090                        const struct GNUNET_HashCode *signing_key,
1091                        struct GNUNET_PseudonymSignature *signature)
1092 {
1093   size_t size;
1094   size_t erroff;
1095   gcry_mpi_t x;
1096   gcry_mpi_t k;
1097   gcry_mpi_t h;
1098   gcry_mpi_t d;
1099   gcry_mpi_t n; /* n from P-256 */
1100   gcry_sexp_t spriv;
1101   gcry_sexp_t data;
1102   gcry_sexp_t result;
1103   gcry_mpi_t rs[2];
1104   int rc;
1105
1106   /* get private key 'x' from pseudonym */
1107   size = sizeof (ph->d);
1108   if (0 != (rc = gcry_mpi_scan (&x, GCRYMPI_FMT_USG,
1109                                 &ph->d,
1110                                 size, &size)))
1111   {
1112     LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
1113     return GNUNET_SYSERR;
1114   }
1115   /* get 'h' value from signing key */
1116   size = sizeof (struct GNUNET_HashCode);
1117   if (0 != (rc = gcry_mpi_scan (&h, GCRYMPI_FMT_USG,
1118                                 signing_key,
1119                                 size, &size)))
1120   {
1121     LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
1122     gcry_mpi_release (x);
1123     return GNUNET_SYSERR;
1124   } 
1125   
1126   /* initialize 'n' from P-256; hex copied from libgcrypt code */
1127   if (0 != (rc = gcry_mpi_scan (&n, GCRYMPI_FMT_HEX, 
1128                                 "0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551", 0, NULL)))
1129   {
1130     LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
1131     gcry_mpi_release (x);
1132     gcry_mpi_release (h);
1133     return GNUNET_SYSERR;
1134   }
1135
1136   /* calculate d = x + h mod n */
1137   d = gcry_mpi_new (256);
1138   gcry_mpi_addm (d, x, h, n);
1139   gcry_mpi_release (x);
1140   gcry_mpi_release (h);
1141   gcry_mpi_release (n);
1142   
1143   /* now build sexpression with the signing key;
1144      NOTE: libgcrypt docs say that we should specify 'Q', but hopefully soon
1145      libgcrypt will derive it from 'd' for us... */
1146   if (0 != (rc = gcry_sexp_build (&spriv, &erroff,
1147                                   "(private-key(ecc(curve \"NIST P-256\")(d %m)))",
1148                                   d)))
1149   {
1150     LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
1151     gcry_mpi_release (d);
1152     return GNUNET_SYSERR;
1153   }
1154   gcry_mpi_release (d);
1155   /* prepare data for signing */
1156   data = data_to_pkcs1 (purpose);
1157   
1158   /* get 'k' value from seed, if available */
1159   if (NULL != seed)
1160   {
1161     size = sizeof (struct GNUNET_HashCode);
1162     if (0 != (rc = gcry_mpi_scan (&k, GCRYMPI_FMT_USG,
1163                                   seed,
1164                                   size, &size)))
1165     {
1166       LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
1167       gcry_mpi_release (x);
1168       return GNUNET_SYSERR;
1169     }
1170   }
1171
1172   /* actually create signature */
1173   /* FIXME: need API to pass 'k' if 'seed' was non-NULL! */
1174   if (0 != (rc = gcry_pk_sign (&result, data, spriv)))
1175   {
1176     LOG (GNUNET_ERROR_TYPE_WARNING,
1177          _("ECC signing failed at %s:%d: %s\n"), __FILE__,
1178          __LINE__, gcry_strerror (rc));
1179     gcry_sexp_release (data);
1180     gcry_sexp_release (spriv);
1181     if (NULL != seed)
1182       gcry_mpi_release (k);
1183     memset (signature, 0, sizeof (struct GNUNET_PseudonymSignature));
1184     return GNUNET_SYSERR;
1185   }
1186   if (NULL != seed)
1187     gcry_mpi_release (k);
1188   gcry_sexp_release (data);
1189   gcry_sexp_release (spriv);
1190
1191   /* extract 'r' and 's' values from sexpression 'result' and store in 'signature' */
1192   if (0 != (rc = key_from_sexp (rs, result, "ecdsa", "rs")))
1193   {
1194     GNUNET_break (0);
1195     gcry_sexp_release (result);
1196     return GNUNET_SYSERR;
1197   }
1198   gcry_sexp_release (result);
1199   size = sizeof (signature->sig_r);
1200   if (0 != (rc = gcry_mpi_print (GCRYMPI_FMT_USG, signature->sig_r, size,
1201                                  &size, rs[0])))
1202   {
1203     LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_print", rc);
1204     gcry_mpi_release (rs[0]);
1205     gcry_mpi_release (rs[1]);
1206     return GNUNET_SYSERR;
1207   }
1208   gcry_mpi_release (rs[0]);
1209   size = sizeof (signature->sig_s);
1210   if (0 != (rc = gcry_mpi_print (GCRYMPI_FMT_USG, signature->sig_s, size,
1211                                  &size, rs[1])))
1212   {
1213     LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_print", rc);
1214     gcry_mpi_release (rs[1]);
1215     return GNUNET_SYSERR;
1216   }
1217   gcry_mpi_release (rs[1]);
1218   return GNUNET_OK;
1219 }
1220
1221
1222 /**
1223  * Get an ECC context (with Q set to the respective public key) from
1224  * a pseudonym.
1225  *
1226  * @param pseudonym with information on 'q'
1227  * @return curve context
1228  */
1229 static gcry_ctx_t 
1230 get_context_from_pseudonym (struct GNUNET_PseudonymIdentifier *pseudonym)
1231 {
1232   gcry_ctx_t ctx;
1233   gcry_mpi_t ONE;
1234   gcry_mpi_t q_x;
1235   gcry_mpi_t q_y;
1236   gcry_mpi_point_t q;
1237   size_t size;
1238   int rc;
1239
1240   /* extract 'q' from pseudonym */
1241   size = sizeof (pseudonym->q_x);
1242   if (0 != (rc = gcry_mpi_scan (&q_x, GCRYMPI_FMT_USG, pseudonym->q_x, size, &size)))
1243   {
1244     LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
1245     return NULL;
1246   }
1247   size = sizeof (pseudonym->q_y);
1248   if (0 != (rc = gcry_mpi_scan (&q_y, GCRYMPI_FMT_USG, pseudonym->q_y, size, &size)))
1249   {
1250     LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
1251     gcry_mpi_release (q_x);
1252     return NULL;
1253   }
1254   q = gcry_mpi_point_new (256);
1255   ONE = gcry_mpi_new (1);
1256   gcry_mpi_set_ui (ONE, 1);
1257   gcry_mpi_point_set (q, q_x, q_y, ONE); /* FIXME: convenience function 'set_affine'? */
1258   gcry_mpi_release (ONE);
1259   gcry_mpi_release (q_x);
1260   gcry_mpi_release (q_y);
1261
1262   /* create basic ECC context */
1263   if (0 != (rc = gcry_mpi_ec_new (&ctx, NULL, "NIST P-256")))
1264   {
1265     LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_ec_new", rc);  /* erroff gives more info */
1266     gcry_mpi_point_release (q);
1267     return NULL;
1268   }  
1269   /* initialize 'ctx' with 'q' */
1270   gcry_mpi_ec_set_point ("q", q, ctx);
1271   gcry_mpi_point_release (q);
1272   return ctx;
1273 }
1274
1275
1276 /**
1277  * Given a pseudonym and a signing key, derive the corresponding public
1278  * key that would be used to verify the resulting signature.
1279  *
1280  * @param pseudonym the public key (g^x in DSA, dQ in ECDSA)
1281  * @param signing_key input to derive 'h' (see section 2.4 of #2564)
1282  * @param verification_key resulting public key to verify the signature
1283  *        created from the 'ph' of 'pseudonym' and the 'signing_key';
1284  *        the value stored here can then be given to GNUNET_PSEUDONYM_verify.
1285  * @return GNUNET_OK on success, GNUNET_SYSERR on error
1286  */
1287 int
1288 GNUNET_PSEUDONYM_derive_verification_key (struct GNUNET_PseudonymIdentifier *pseudonym,
1289                                           const struct GNUNET_HashCode *signing_key,
1290                                           struct GNUNET_PseudonymIdentifier *verification_key)
1291 {
1292   gcry_mpi_t h;  
1293   size_t size;
1294   int rc;
1295   gcry_ctx_t ctx;
1296   gcry_mpi_point_t g;
1297   gcry_mpi_point_t q;
1298   gcry_mpi_point_t hg;
1299   gcry_mpi_point_t v;
1300   gcry_mpi_t v_x;
1301   gcry_mpi_t v_y;
1302
1303   /* get 'h' value from signing key */
1304   size = sizeof (struct GNUNET_HashCode);
1305   if (0 != (rc = gcry_mpi_scan (&h, GCRYMPI_FMT_USG,
1306                                 signing_key,
1307                                 size, &size)))
1308   {
1309     LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
1310     return GNUNET_SYSERR;
1311   }
1312   /* create ECC context based on Q from pseudonym */
1313   if (NULL == (ctx = get_context_from_pseudonym (pseudonym)))
1314   {
1315     gcry_mpi_release (h);
1316     return GNUNET_SYSERR;
1317   }
1318   /* get G */  
1319   g = gcry_mpi_ec_get_point ("g", ctx, 0);
1320
1321   /* then call the 'multiply' function, to compute the product hG */
1322   hg = gcry_mpi_point_new (0);
1323   gcry_mpi_ec_mul (hg, h, g, ctx);
1324   gcry_mpi_release (h);
1325
1326   /* get Q = dG from 'pseudonym' */
1327   q = gcry_mpi_ec_get_point ("q", ctx, 0);
1328
1329   /* calculate V = q + hG = dG + hG */
1330   v = gcry_mpi_point_new (0);
1331   gcry_mpi_ec_add (v, q, hg, ctx);
1332   
1333   /* store 'v' point in "verification_key" */
1334   v_x = gcry_mpi_new (256);
1335   v_y = gcry_mpi_new (256);
1336   gcry_mpi_ec_get_affine (v_x, v_y, v, ctx);
1337   gcry_mpi_point_release (v);
1338   gcry_ctx_release (ctx);
1339
1340   size = sizeof (verification_key->q_x);
1341   if (0 != (rc = gcry_mpi_print (GCRYMPI_FMT_USG, verification_key->q_x, size,
1342                                  &size, v_x)))
1343   {
1344     LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_print", rc);
1345     gcry_mpi_release (v_x);
1346     gcry_mpi_release (v_y);
1347     return GNUNET_SYSERR;
1348   }
1349   gcry_mpi_release (v_x);
1350   size = sizeof (verification_key->q_y);
1351   if (0 != (rc = gcry_mpi_print (GCRYMPI_FMT_USG, verification_key->q_y, size,
1352                                  &size, v_y)))
1353   {
1354     LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_print", rc);
1355     gcry_mpi_release (v_y);
1356     return GNUNET_SYSERR;
1357   }
1358   gcry_mpi_release (v_y);
1359   return GNUNET_OK;
1360 }
1361
1362
1363 /**
1364  * Verify a signature made with a pseudonym.
1365  *
1366  * @param purpose data that was signed
1367  * @param signature signature to verify
1368  * @param verification_key public key to use for checking the signature;
1369  *                    corresponds to 'g^(x+h)' in section 2.4 of #2564.
1370  * @return GNUNET_OK on success (signature valid, 'pseudonym' set),
1371  *         GNUNET_SYSERR if the signature is invalid
1372  */
1373 int
1374 GNUNET_PSEUDONYM_verify (const struct GNUNET_PseudonymSignaturePurpose *purpose,
1375                          const struct GNUNET_PseudonymSignature *signature,
1376                          const struct GNUNET_PseudonymIdentifier *verification_key)
1377 {
1378 #if FUTURE 
1379   gcry_sexp_t data;
1380   gcry_sexp_t sig_sexpr;
1381   gcry_sexp_t pk_sexpr;
1382   size_t size;
1383   gcry_ctx_t ctx;
1384   gcry_mpi_t ONE;
1385   gcry_mpi_t r;
1386   gcry_mpi_t s;
1387   gcry_mpi_point_t q;
1388   gcry_mpi_t q_x;
1389   gcry_mpi_t q_y;
1390   size_t erroff;
1391   int rc;
1392
1393   /* build s-expression for signature */
1394   size = sizeof (signature->sig_r);
1395   if (0 != (rc = gcry_mpi_scan (&r, GCRYMPI_FMT_USG,
1396                                 signature->sig_r, size, &size)))
1397   {
1398     LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
1399     return GNUNET_SYSERR;
1400   }
1401   size = sizeof (signature->sig_s);
1402   if (0 != (rc = gcry_mpi_scan (&s, GCRYMPI_FMT_USG,
1403                                 signature->sig_s, size, &size)))
1404   {
1405     LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
1406     gcry_mpi_release (r);
1407     return GNUNET_SYSERR;
1408   }
1409   if (0 != (rc = gcry_sexp_build (&sig_sexpr, &erroff, "(sig-val(ecdsa(r %m)(s %m)))",
1410                                   r, s)))
1411   {
1412     LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
1413     gcry_mpi_release (r);
1414     gcry_mpi_release (s);
1415     return GNUNET_SYSERR;
1416   }
1417   gcry_mpi_release (r);
1418   gcry_mpi_release (s);
1419
1420   /* build s-expression for data that was signed */
1421   data = data_to_pkcs1 (purpose);
1422
1423   /* create context of public key and initialize Q */
1424   size = sizeof (verification_key->q_x);
1425   if (0 != (rc = gcry_mpi_scan (&q_x, GCRYMPI_FMT_USG,
1426                                 verification_key->q_x, size, &size)))
1427   {
1428     LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
1429     gcry_sexp_release (data);
1430     gcry_sexp_release (sig_sexpr);
1431     return GNUNET_SYSERR;
1432   }
1433   size = sizeof (verification_key->q_y);
1434   if (0 != (rc = gcry_mpi_scan (&q_y, GCRYMPI_FMT_USG,
1435                                 verification_key->q_y, size, &size)))
1436   {
1437     LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
1438     gcry_sexp_release (data);
1439     gcry_sexp_release (sig_sexpr);
1440     gcry_mpi_release (q_x);
1441     return GNUNET_SYSERR;
1442   }
1443   q = gcry_mpi_point_new (256);
1444   ONE = gcry_mpi_new (1);
1445   gcry_mpi_set_ui (ONE, 1);
1446   gcry_mpi_point_set (q, q_x, q_y, ONE); /* FIXME: convenience function 'set_affine'? */
1447   gcry_mpi_release (ONE);
1448   gcry_mpi_release (q_x);
1449   gcry_mpi_release (q_y);
1450
1451   /* create basic ECC context */
1452   if (0 != (rc = gcry_mpi_ec_new (&ctx, NULL, "NIST P-256")))
1453   {
1454     LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_ec_new", rc);  /* erroff gives more info */
1455     gcry_sexp_release (data);
1456     gcry_sexp_release (sig_sexpr);
1457     gcry_mpi_point_release (q);
1458     return GNUNET_SYSERR;
1459   }  
1460   /* initialize 'ctx' with 'q' */
1461   gcry_mpi_ec_set_point ("q", q, ctx);
1462   gcry_mpi_point_release (q);
1463
1464   /* convert 'ctx' to 'sexp' (this hurts) */
1465   if (0 != (rc = gcry_sexp_from_context (&pk_sexpr, ctx)))
1466   {
1467     LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_from_context", rc);
1468     gcry_ctx_release (ctx);
1469     gcry_sexp_release (data);
1470     gcry_sexp_release (sig_sexpr);
1471     return GNUNET_SYSERR;
1472   }
1473   gcry_ctx_release (ctx);
1474
1475   /* finally, verify the signature */
1476   rc = gcry_pk_verify (sig_sexpr, data, pk_sexpr);
1477   gcry_sexp_release (sig_sexpr);
1478   gcry_sexp_release (data);
1479   gcry_sexp_release (pk_sexpr);
1480   if (rc)
1481   {
1482     LOG (GNUNET_ERROR_TYPE_WARNING,
1483          _("ECDSA signature verification failed at %s:%d: %s\n"), __FILE__,
1484          __LINE__, gcry_strerror (rc));
1485     return GNUNET_SYSERR;
1486   }
1487 #else
1488   GNUNET_break (0);
1489 #endif
1490   return GNUNET_OK;
1491 }
1492
1493
1494 /**
1495  * Get the identifier (public key) of a pseudonym.
1496  *
1497  * @param ph pseudonym handle with the private key
1498  * @param pseudonym pseudonym identifier (set based on 'ph')
1499  */
1500 void
1501 GNUNET_PSEUDONYM_get_identifier (struct GNUNET_PseudonymHandle *ph,
1502                                  struct GNUNET_PseudonymIdentifier *pseudonym)
1503 {
1504   memcpy (pseudonym, &ph->public_key,
1505           sizeof (struct GNUNET_PseudonymIdentifier));
1506 }
1507
1508
1509 /**
1510  * Remove pseudonym from the set of known pseudonyms.
1511  *
1512  * @param cfg overall configuration
1513  * @param id the pseudonym identifier
1514  * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1515  */
1516 int
1517 GNUNET_PSEUDONYM_remove (const struct GNUNET_CONFIGURATION_Handle *cfg,
1518                          const struct GNUNET_PseudonymIdentifier *id)
1519 {
1520   char *fn;
1521   int result;
1522
1523   fn = get_data_filename (cfg, PS_METADATA_DIR, id);
1524   if (NULL == fn)
1525     return GNUNET_SYSERR;
1526   result = UNLINK (fn);
1527   GNUNET_free (fn);  
1528   return (0 == result) ? GNUNET_OK : GNUNET_SYSERR;
1529 }
1530
1531 /* end of pseudonym.c */