2 This file is part of GNUnet
3 (C) 2003, 2004, 2005, 2006, 2007, 2008, 2013 Christian Grothoff (and other contributing authors)
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.
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.
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.
21 * @file fs/fs_pseudonym.c
22 * @brief pseudonym functions
23 * @author Christian Grothoff
26 * - all cryptographic operations are currently NOT implemented and
27 * provided by stubs that merely pretend to work!
30 #include "gnunet_util_lib.h"
31 #include "gnunet_fs_service.h"
35 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
37 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
39 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
42 * Log an error message at log-level 'level' that indicates
43 * a failure of the command 'cmd' with the message given
44 * by gcry_strerror(rc).
46 #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 * Name of the directory which stores meta data for pseudonym
51 #define PS_METADATA_DIR DIR_SEPARATOR_STR "data" DIR_SEPARATOR_STR "pseudonym" DIR_SEPARATOR_STR "metadata" DIR_SEPARATOR_STR
54 * Name of the directory which stores names for pseudonyms
56 #define PS_NAMES_DIR DIR_SEPARATOR_STR "data" DIR_SEPARATOR_STR "pseudonym" DIR_SEPARATOR_STR "names" DIR_SEPARATOR_STR
60 * Configuration section we use.
62 #define GNUNET_CLIENT_SERVICE_NAME "fs"
65 /* ************************* Disk operations (pseudonym data mgmt) **************** */
68 * Registered callbacks for discovery of pseudonyms.
70 struct GNUNET_FS_pseudonym_DiscoveryHandle
73 * This is a doubly linked list.
75 struct GNUNET_FS_pseudonym_DiscoveryHandle *next;
78 * This is a doubly linked list.
80 struct GNUNET_FS_pseudonym_DiscoveryHandle *prev;
83 * Function to call each time a pseudonym is discovered.
85 GNUNET_FS_PseudonymIterator callback;
88 * Closure for callback.
95 * Head of the linked list of functions to call when
96 * new pseudonyms are added.
98 static struct GNUNET_FS_pseudonym_DiscoveryHandle *disco_head;
101 * Tail of the linked list of functions to call when
102 * new pseudonyms are added.
104 static struct GNUNET_FS_pseudonym_DiscoveryHandle *disco_tail;
108 * Internal notification about new tracked URI.
110 * @param pseudonym public key of the pseudonym
111 * @param md meta data to be written
112 * @param rating rating of pseudonym
115 internal_notify (const struct GNUNET_FS_PseudonymIdentifier *pseudonym,
116 const struct GNUNET_CONTAINER_MetaData *md, int rating)
118 struct GNUNET_FS_pseudonym_DiscoveryHandle *pos;
120 for (pos = disco_head; NULL != pos; pos = pos->next)
121 pos->callback (pos->callback_cls, pseudonym, NULL, NULL, md, rating);
126 * Register callback to be invoked whenever we discover
128 * Will immediately call provided iterator callback for all
129 * already discovered pseudonyms.
131 * @param cfg configuration to use
132 * @param iterator iterator over pseudonym
133 * @param iterator_cls point to a closure
134 * @return registration handle
136 struct GNUNET_FS_pseudonym_DiscoveryHandle *
137 GNUNET_FS_pseudonym_discovery_callback_register (const struct
138 GNUNET_CONFIGURATION_Handle *cfg,
139 GNUNET_FS_PseudonymIterator iterator,
142 struct GNUNET_FS_pseudonym_DiscoveryHandle *dh;
144 dh = GNUNET_malloc (sizeof (struct GNUNET_FS_pseudonym_DiscoveryHandle));
145 dh->callback = iterator;
146 dh->callback_cls = iterator_cls;
147 GNUNET_CONTAINER_DLL_insert (disco_head, disco_tail, dh);
148 GNUNET_FS_pseudonym_list_all (cfg, iterator, iterator_cls);
154 * Unregister pseudonym discovery callback.
156 * @param dh registration to unregister
159 GNUNET_FS_pseudonym_discovery_callback_unregister (struct GNUNET_FS_pseudonym_DiscoveryHandle *dh)
161 GNUNET_CONTAINER_DLL_remove (disco_head, disco_tail, dh);
167 * Get the filename (or directory name) for the given
168 * pseudonym identifier and directory prefix.
170 * @param cfg configuration to use
171 * @param prefix path components to append to the private directory name
172 * @param pseudonym the pseudonym, can be NULL
173 * @return filename of the pseudonym (if pseudonym != NULL) or directory with the data (if pseudonym == NULL)
176 get_data_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
178 const struct GNUNET_FS_PseudonymIdentifier *pseudonym)
180 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
181 struct GNUNET_HashCode psid;
183 if (NULL != pseudonym)
185 GNUNET_CRYPTO_hash (pseudonym,
186 sizeof (struct GNUNET_FS_PseudonymIdentifier),
188 GNUNET_CRYPTO_hash_to_enc (&psid, &enc);
190 return GNUNET_DISK_get_home_filename (cfg,
191 GNUNET_CLIENT_SERVICE_NAME, prefix,
194 : (const char *) &enc,
200 * Get the filename (or directory name) for the given
201 * hash code and directory prefix.
203 * @param cfg configuration to use
204 * @param prefix path components to append to the private directory name
205 * @param hc some hash code
206 * @return filename of the pseudonym (if hc != NULL) or directory with the data (if hc == NULL)
209 get_data_filename_hash (const struct GNUNET_CONFIGURATION_Handle *cfg,
211 const struct GNUNET_HashCode *hc)
213 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
216 GNUNET_CRYPTO_hash_to_enc (hc, &enc);
217 return GNUNET_DISK_get_home_filename (cfg,
218 GNUNET_CLIENT_SERVICE_NAME, prefix,
221 : (const char *) &enc,
227 * Set the pseudonym metadata, rank and name.
228 * Writes the pseudonym infomation into a file
230 * @param cfg overall configuration
231 * @param pseudonym id of the pseudonym
232 * @param name name to set. Must be the non-unique version of it.
233 * May be NULL, in which case it erases pseudonym's name!
234 * @param md metadata to set
235 * May be NULL, in which case it erases pseudonym's metadata!
236 * @param rank rank to assign
237 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
240 GNUNET_FS_pseudonym_set_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
241 const struct GNUNET_FS_PseudonymIdentifier *pseudonym,
243 const struct GNUNET_CONTAINER_MetaData *md,
247 struct GNUNET_BIO_WriteHandle *fileW;
249 fn = get_data_filename (cfg, PS_METADATA_DIR, pseudonym);
250 if (NULL == (fileW = GNUNET_BIO_write_open (fn)))
253 return GNUNET_SYSERR;
255 if ((GNUNET_OK != GNUNET_BIO_write (fileW, pseudonym,
256 sizeof (struct GNUNET_FS_PseudonymIdentifier))) ||
257 (GNUNET_OK != GNUNET_BIO_write_int32 (fileW, rank)) ||
258 (GNUNET_OK != GNUNET_BIO_write_string (fileW, name)) ||
259 (GNUNET_OK != GNUNET_BIO_write_meta_data (fileW, md)))
261 (void) GNUNET_BIO_write_close (fileW);
262 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
264 return GNUNET_SYSERR;
266 if (GNUNET_OK != GNUNET_BIO_write_close (fileW))
268 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
270 return GNUNET_SYSERR;
273 /* create entry for pseudonym name in names */
275 GNUNET_free_non_null (GNUNET_FS_pseudonym_name_uniquify (cfg, pseudonym,
282 * Read pseudonym infomation from a file
284 * @param cfg configuration to use
285 * @param pseudonym hash code of a pseudonym
286 * @param meta meta data to be read from a file
287 * @param rank rank of a pseudonym
288 * @param ns_name name of a pseudonym
289 * @return GNUNET_OK on success, GNUNET_SYSERR on error
292 read_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
293 const struct GNUNET_FS_PseudonymIdentifier *pseudonym,
294 struct GNUNET_CONTAINER_MetaData **meta,
298 struct GNUNET_FS_PseudonymIdentifier pd;
301 struct GNUNET_BIO_ReadHandle *fileR;
303 fn = get_data_filename (cfg, PS_METADATA_DIR, pseudonym);
305 GNUNET_DISK_file_test (fn))
308 return GNUNET_SYSERR;
310 if (NULL == (fileR = GNUNET_BIO_read_open (fn)))
313 return GNUNET_SYSERR;
317 if ( (GNUNET_OK != GNUNET_BIO_read (fileR, "pseudonym", &pd, sizeof (pd))) ||
318 (0 != memcmp (&pd, pseudonym, sizeof (pd))) ||
319 (GNUNET_OK != GNUNET_BIO_read_int32 (fileR, rank)) ||
321 GNUNET_BIO_read_string (fileR, "Read string error!", ns_name, 200)) ||
323 GNUNET_BIO_read_meta_data (fileR, "Read meta data error!", meta)) )
325 (void) GNUNET_BIO_read_close (fileR, &emsg);
326 GNUNET_free_non_null (emsg);
327 GNUNET_free_non_null (*ns_name);
329 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
331 return GNUNET_SYSERR;
333 if (GNUNET_OK != GNUNET_BIO_read_close (fileR, &emsg))
335 LOG (GNUNET_ERROR_TYPE_WARNING,
336 _("Failed to parse metadata about pseudonym from file `%s': %s\n"), fn,
338 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
339 GNUNET_CONTAINER_meta_data_destroy (*meta);
341 GNUNET_free_non_null (*ns_name);
343 GNUNET_free_non_null (emsg);
345 return GNUNET_SYSERR;
353 * Return unique variant of the namespace name. Use it after
354 * GNUNET_FS_pseudonym_get_info() to make sure that name is unique.
356 * @param cfg configuration
357 * @param pseudonym public key of the pseudonym
358 * @param name name to uniquify
359 * @param suffix if not NULL, filled with the suffix value
360 * @return NULL on failure (should never happen), name on success.
361 * Free the name with GNUNET_free().
364 GNUNET_FS_pseudonym_name_uniquify (const struct GNUNET_CONFIGURATION_Handle *cfg,
365 const struct GNUNET_FS_PseudonymIdentifier *pseudonym,
367 unsigned int *suffix)
369 struct GNUNET_HashCode nh;
370 struct GNUNET_FS_PseudonymIdentifier pi;
373 struct GNUNET_DISK_FileHandle *fh;
379 GNUNET_CRYPTO_hash (name, strlen (name), &nh);
380 fn = get_data_filename_hash (cfg, PS_NAMES_DIR, &nh);
382 if (0 == STAT (fn, &sbuf))
383 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_size (fn, &len, GNUNET_YES, GNUNET_YES));
384 fh = GNUNET_DISK_file_open (fn,
385 GNUNET_DISK_OPEN_CREATE |
386 GNUNET_DISK_OPEN_READWRITE,
387 GNUNET_DISK_PERM_USER_READ |
388 GNUNET_DISK_PERM_USER_WRITE);
391 while ((len >= sizeof (struct GNUNET_FS_PseudonymIdentifier)) &&
392 (sizeof (struct GNUNET_FS_PseudonymIdentifier) ==
393 GNUNET_DISK_file_read (fh, &pi, sizeof (struct GNUNET_FS_PseudonymIdentifier))))
395 if (0 == memcmp (&pi, pseudonym, sizeof (struct GNUNET_FS_PseudonymIdentifier)))
401 len -= sizeof (struct GNUNET_HashCode);
406 if (sizeof (struct GNUNET_FS_PseudonymIdentifier) !=
407 GNUNET_DISK_file_write (fh, pseudonym, sizeof (struct GNUNET_FS_PseudonymIdentifier)))
408 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "write", fn);
410 GNUNET_DISK_file_close (fh);
411 ret = GNUNET_malloc (strlen (name) + 32);
412 GNUNET_snprintf (ret, strlen (name) + 32, "%s-%u", name, idx);
421 * Get namespace name, metadata and rank
422 * This is a wrapper around internal read_info() call, and ensures that
423 * returned data is not invalid (not NULL).
425 * @param cfg configuration
426 * @param pseudonym public key of the pseudonym
427 * @param ret_meta a location to store metadata pointer. NULL, if metadata
428 * is not needed. Destroy with GNUNET_CONTAINER_meta_data_destroy().
429 * @param ret_rank a location to store rank. NULL, if rank not needed.
430 * @param ret_name a location to store human-readable name. Name is not unique.
431 * NULL, if name is not needed. Free with GNUNET_free().
432 * @param name_is_a_dup is set to GNUNET_YES, if ret_name was filled with
433 * a duplicate of a "no-name" placeholder
434 * @return GNUNET_OK on success. GNUENT_SYSERR if the data was
435 * unobtainable (in that case ret_* are filled with placeholders -
436 * empty metadata container, rank -1 and a "no-name" name).
439 GNUNET_FS_pseudonym_get_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
440 const struct GNUNET_FS_PseudonymIdentifier *pseudonym,
441 struct GNUNET_CONTAINER_MetaData **ret_meta,
446 struct GNUNET_CONTAINER_MetaData *meta;
452 if (GNUNET_OK == read_info (cfg, pseudonym, &meta, &rank, &name))
454 if ((meta != NULL) && (name == NULL))
456 GNUNET_CONTAINER_meta_data_get_first_by_types (meta,
457 EXTRACTOR_METATYPE_TITLE,
458 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME,
459 EXTRACTOR_METATYPE_FILENAME,
460 EXTRACTOR_METATYPE_DESCRIPTION,
461 EXTRACTOR_METATYPE_SUBJECT,
462 EXTRACTOR_METATYPE_PUBLISHER,
463 EXTRACTOR_METATYPE_AUTHOR_NAME,
464 EXTRACTOR_METATYPE_COMMENT,
465 EXTRACTOR_METATYPE_SUMMARY,
467 if (ret_name != NULL)
471 name = GNUNET_strdup (_("no-name"));
472 if (name_is_a_dup != NULL)
473 *name_is_a_dup = GNUNET_YES;
475 else if (name_is_a_dup != NULL)
476 *name_is_a_dup = GNUNET_NO;
479 else if (name != NULL)
482 if (ret_meta != NULL)
485 meta = GNUNET_CONTAINER_meta_data_create ();
488 else if (meta != NULL)
489 GNUNET_CONTAINER_meta_data_destroy (meta);
491 if (ret_rank != NULL)
496 if (ret_name != NULL)
497 *ret_name = GNUNET_strdup (_("no-name"));
498 if (ret_meta != NULL)
499 *ret_meta = GNUNET_CONTAINER_meta_data_create ();
500 if (ret_rank != NULL)
502 if (name_is_a_dup != NULL)
503 *name_is_a_dup = GNUNET_YES;
504 return GNUNET_SYSERR;
509 * Get the namespace ID belonging to the given namespace name.
511 * @param cfg configuration to use
512 * @param ns_uname unique (!) human-readable name for the namespace
513 * @param pseudonym set to public key of pseudonym based on 'ns_uname'
514 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
517 GNUNET_FS_pseudonym_name_to_id (const struct GNUNET_CONFIGURATION_Handle *cfg,
518 const char *ns_uname,
519 struct GNUNET_FS_PseudonymIdentifier *pseudonym)
525 struct GNUNET_HashCode nh;
527 struct GNUNET_DISK_FileHandle *fh;
530 slen = strlen (ns_uname);
531 while ((slen > 0) && (1 != SSCANF (&ns_uname[slen - 1], "-%u", &idx)))
534 return GNUNET_SYSERR;
535 name = GNUNET_strdup (ns_uname);
536 name[slen - 1] = '\0';
538 GNUNET_CRYPTO_hash (name, strlen (name), &nh);
540 fn = get_data_filename_hash (cfg, PS_NAMES_DIR, &nh);
542 if ((GNUNET_OK != GNUNET_DISK_file_test (fn) ||
543 (GNUNET_OK != GNUNET_DISK_file_size (fn, &len, GNUNET_YES, GNUNET_YES))) ||
544 ((idx + 1) * sizeof (struct GNUNET_FS_PseudonymIdentifier) > len))
547 return GNUNET_SYSERR;
549 fh = GNUNET_DISK_file_open (fn,
550 GNUNET_DISK_OPEN_CREATE |
551 GNUNET_DISK_OPEN_READWRITE,
552 GNUNET_DISK_PERM_USER_READ |
553 GNUNET_DISK_PERM_USER_WRITE);
556 GNUNET_DISK_file_seek (fh, idx * sizeof (struct GNUNET_FS_PseudonymIdentifier),
557 GNUNET_DISK_SEEK_SET))
559 GNUNET_DISK_file_close (fh);
560 return GNUNET_SYSERR;
562 if (sizeof (struct GNUNET_FS_PseudonymIdentifier) !=
563 GNUNET_DISK_file_read (fh, pseudonym, sizeof (struct GNUNET_FS_PseudonymIdentifier)))
565 GNUNET_DISK_file_close (fh);
566 return GNUNET_SYSERR;
568 GNUNET_DISK_file_close (fh);
575 * struct used to list the pseudonym
577 struct ListPseudonymClosure
581 * iterator over pseudonym
583 GNUNET_FS_PseudonymIterator iterator;
586 * Closure for iterator.
591 * Configuration to use.
593 const struct GNUNET_CONFIGURATION_Handle *cfg;
599 * Helper function to list all available pseudonyms
601 * @param cls point to a struct ListPseudonymClosure
602 * @param fullname name of pseudonym
605 list_pseudonym_helper (void *cls, const char *fullname)
607 struct ListPseudonymClosure *lpc = cls;
608 struct GNUNET_FS_PseudonymIdentifier pd;
610 struct GNUNET_BIO_ReadHandle *fileR;
613 struct GNUNET_CONTAINER_MetaData *meta;
617 if (NULL == (fileR = GNUNET_BIO_read_open (fullname)))
618 return GNUNET_SYSERR;
621 if ( (GNUNET_OK != GNUNET_BIO_read (fileR, "pseudonym", &pd, sizeof (pd))) ||
622 (GNUNET_OK != GNUNET_BIO_read_int32 (fileR, &rank)) ||
624 GNUNET_BIO_read_string (fileR, "Read string error!", &ns_name, 200)) ||
626 GNUNET_BIO_read_meta_data (fileR, "Read meta data error!", &meta)) )
628 (void) GNUNET_BIO_read_close (fileR, &emsg);
629 GNUNET_free_non_null (emsg);
630 GNUNET_free_non_null (ns_name);
631 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fullname));
632 return GNUNET_SYSERR;
635 ns_name = GNUNET_strdup (_("no-name"));
636 if (GNUNET_OK != GNUNET_BIO_read_close (fileR, &emsg))
638 LOG (GNUNET_ERROR_TYPE_WARNING,
639 _("Failed to parse metadata about pseudonym from file `%s': %s\n"), fullname,
641 GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fullname));
642 GNUNET_CONTAINER_meta_data_destroy (meta);
643 GNUNET_free (ns_name);
644 GNUNET_free_non_null (emsg);
645 return GNUNET_SYSERR;
648 name_unique = GNUNET_FS_pseudonym_name_uniquify (lpc->cfg, &pd, ns_name, NULL);
649 if (NULL != lpc->iterator)
650 ret = lpc->iterator (lpc->iterator_cls, &pd, ns_name, name_unique, meta, rank);
651 GNUNET_free (ns_name);
652 GNUNET_free_non_null (name_unique);
653 GNUNET_CONTAINER_meta_data_destroy (meta);
659 * List all available pseudonyms.
661 * @param cfg overall configuration
662 * @param iterator function to call for each pseudonym
663 * @param iterator_cls closure for iterator
664 * @return number of pseudonyms found
667 GNUNET_FS_pseudonym_list_all (const struct GNUNET_CONFIGURATION_Handle *cfg,
668 GNUNET_FS_PseudonymIterator iterator,
671 struct ListPseudonymClosure cls;
675 cls.iterator = iterator;
676 cls.iterator_cls = iterator_cls;
678 fn = get_data_filename (cfg, PS_METADATA_DIR, NULL);
679 GNUNET_assert (fn != NULL);
680 GNUNET_DISK_directory_create (fn);
681 ret = GNUNET_DISK_directory_scan (fn, &list_pseudonym_helper, &cls);
688 * Change the rank of a pseudonym.
690 * @param cfg overall configuration
691 * @param pseudonym the pseudonym
692 * @param delta by how much should the rating be changed?
693 * @return new rating of the pseudonym
696 GNUNET_FS_pseudonym_rank (const struct GNUNET_CONFIGURATION_Handle *cfg,
697 const struct GNUNET_FS_PseudonymIdentifier *pseudonym,
700 struct GNUNET_CONTAINER_MetaData *meta;
706 ret = read_info (cfg, pseudonym, &meta, &rank, &name);
707 if (ret == GNUNET_SYSERR)
710 meta = GNUNET_CONTAINER_meta_data_create ();
713 GNUNET_FS_pseudonym_set_info (cfg, pseudonym, name, meta, rank);
714 GNUNET_CONTAINER_meta_data_destroy (meta);
715 GNUNET_free_non_null (name);
721 * Add a pseudonym to the set of known pseudonyms.
722 * For all pseudonym advertisements that we discover
723 * FS should automatically call this function.
725 * @param cfg overall configuration
726 * @param pseudonym the pseudonym to add
727 * @param meta metadata for the pseudonym
728 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
731 GNUNET_FS_pseudonym_add (const struct GNUNET_CONFIGURATION_Handle *cfg,
732 const struct GNUNET_FS_PseudonymIdentifier *pseudonym,
733 const struct GNUNET_CONTAINER_MetaData *meta)
737 struct GNUNET_CONTAINER_MetaData *old;
743 fn = get_data_filename (cfg, PS_METADATA_DIR, pseudonym);
744 GNUNET_assert (fn != NULL);
746 if ((0 == STAT (fn, &sbuf)) &&
747 (GNUNET_OK == read_info (cfg, pseudonym, &old, &rank, &name)))
749 GNUNET_CONTAINER_meta_data_merge (old, meta);
750 ret = GNUNET_FS_pseudonym_set_info (cfg, pseudonym, name, old, rank);
751 GNUNET_CONTAINER_meta_data_destroy (old);
752 GNUNET_free_non_null (name);
756 ret = GNUNET_FS_pseudonym_set_info (cfg, pseudonym, NULL, meta, rank);
759 internal_notify (pseudonym, meta, rank);
764 /* ***************************** cryptographic operations ************************* */
767 * Handle for a pseudonym (private key).
769 struct GNUNET_FS_PseudonymHandle
772 * 256-bit 'd' secret value (mod 'n', where n is 256-bit for NIST P-256).
774 unsigned char d[256 / 8];
777 * Public key corresponding to the private key.
779 struct GNUNET_FS_PseudonymIdentifier public_key;
784 * If target != size, move target bytes to the end of the size-sized
785 * buffer and zero out the first target-size bytes.
787 * @param buf original buffer
788 * @param size number of bytes in the buffer
789 * @param target target size of the buffer
792 adjust (unsigned char *buf, size_t size, size_t target)
796 memmove (&buf[target - size], buf, size);
797 memset (buf, 0, target - size);
803 * Extract values from an S-expression.
805 * @param array where to store the result(s)
806 * @param sexp S-expression to parse
807 * @param topname top-level name in the S-expression that is of interest
808 * @param elems names of the elements to extract
809 * @return 0 on success
812 key_from_sexp (gcry_mpi_t * array, gcry_sexp_t sexp, const char *topname,
821 if (! (list = gcry_sexp_find_token (sexp, topname, 0)))
823 l2 = gcry_sexp_cadr (list);
824 gcry_sexp_release (list);
829 for (s = elems; *s; s++, idx++)
831 if (! (l2 = gcry_sexp_find_token (list, s, 1)))
833 for (i = 0; i < idx; i++)
835 gcry_free (array[i]);
838 gcry_sexp_release (list);
839 return 3; /* required parameter not found */
841 array[idx] = gcry_sexp_nth_mpi (l2, 1, GCRYMPI_FMT_USG);
842 gcry_sexp_release (l2);
845 for (i = 0; i < idx; i++)
847 gcry_free (array[i]);
850 gcry_sexp_release (list);
851 return 4; /* required parameter is invalid */
854 gcry_sexp_release (list);
860 * Create a pseudonym.
862 * @param filename name of the file to use for storage, NULL for in-memory only
863 * @return handle to the private key of the pseudonym
865 struct GNUNET_FS_PseudonymHandle *
866 GNUNET_FS_pseudonym_create (const char *filename)
868 struct GNUNET_FS_PseudonymHandle *ph;
880 ph = GNUNET_malloc (sizeof (struct GNUNET_FS_PseudonymHandle));
881 if ( (NULL != filename) &&
882 (GNUNET_YES == GNUNET_DISK_file_test (filename)) )
884 ret = GNUNET_DISK_fn_read (filename, ph,
885 sizeof (struct GNUNET_FS_PseudonymHandle));
886 /* Note: we don't do any validation here, maybe we should? */
887 if (sizeof (struct GNUNET_FS_PseudonymHandle) == ret)
890 if (0 != (rc = gcry_sexp_build (¶ms, NULL,
891 "(genkey(ecdsa(curve \"NIST P-256\")))")))
893 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
896 if (0 != (rc = gcry_pk_genkey (&r_key, params)))
898 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_genkey", rc);
899 gcry_sexp_release (r_key);
902 /* extract "d" (secret key) from r_key */
903 rc = key_from_sexp (&d, r_key, "private-key", "d");
905 rc = key_from_sexp (&d, r_key, "private-key", "d");
907 rc = key_from_sexp (&d, r_key, "ecc", "d");
910 gcry_sexp_release (r_key);
911 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "key_from_sexp", rc);
914 size = sizeof (ph->d);
916 gcry_mpi_print (GCRYMPI_FMT_USG, ph->d, size, &size,
918 gcry_mpi_release (d);
919 adjust (ph->d, size, sizeof (ph->d));
921 /* extract 'q' (public key) from r_key */
922 if (0 != (rc = gcry_mpi_ec_new (&ctx, r_key, NULL)))
924 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_ec_new", rc); /* erroff gives more info */
925 gcry_sexp_release (r_key);
928 gcry_sexp_release (r_key);
929 q = gcry_mpi_ec_get_point ("q", ctx, 0);
930 q_x = gcry_mpi_new (256);
931 q_y = gcry_mpi_new (256);
932 gcry_mpi_ec_get_affine (q_x, q_y, q, ctx);
933 gcry_mpi_point_release (q);
935 /* store q_x/q_y in public key */
936 size = sizeof (ph->public_key.q_x);
938 gcry_mpi_print (GCRYMPI_FMT_USG, ph->public_key.q_x, size, &size,
941 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_print", rc);
942 gcry_mpi_release (q_x);
943 gcry_mpi_release (q_y);
947 adjust (ph->public_key.q_x, size, sizeof (ph->public_key.q_x));
948 gcry_mpi_release (q_x);
950 size = sizeof (ph->public_key.q_y);
952 gcry_mpi_print (GCRYMPI_FMT_USG, ph->public_key.q_y, size, &size,
955 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_print", rc);
956 gcry_mpi_release (q_y);
959 adjust (ph->public_key.q_y, size, sizeof (ph->public_key.q_y));
960 gcry_mpi_release (q_y);
963 if (NULL != filename)
965 ret = GNUNET_DISK_fn_write (filename, ph, sizeof (struct GNUNET_FS_PseudonymHandle),
966 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
967 if (sizeof (struct GNUNET_FS_PseudonymHandle) != ret)
978 * Create a pseudonym, from a file that must already exist.
980 * @param filename name of the file to use for storage, NULL for in-memory only
981 * @return handle to the private key of the pseudonym
983 struct GNUNET_FS_PseudonymHandle *
984 GNUNET_FS_pseudonym_create_from_existing_file (const char *filename)
986 struct GNUNET_FS_PseudonymHandle *ph;
989 ph = GNUNET_malloc (sizeof (struct GNUNET_FS_PseudonymHandle));
990 ret = GNUNET_DISK_fn_read (filename, ph,
991 sizeof (struct GNUNET_FS_PseudonymHandle));
992 if (sizeof (struct GNUNET_FS_PseudonymHandle) != ret)
997 /* Note: we don't do any validation here; maybe we should? */
1003 * Get the handle for the 'anonymous' pseudonym shared by all users.
1004 * That pseudonym uses a fixed 'secret' for the private key; this
1005 * construction is useful to make anonymous and pseudonymous APIs
1006 * (and packets) indistinguishable on the network. See #2564.
1008 * @return handle to the (non-secret) private key of the 'anonymous' pseudonym
1010 struct GNUNET_FS_PseudonymHandle *
1011 GNUNET_FS_pseudonym_get_anonymous_pseudonym_handle ()
1013 struct GNUNET_FS_PseudonymHandle *ph;
1015 ph = GNUNET_malloc (sizeof (struct GNUNET_FS_PseudonymHandle));
1016 /* Note if we use 'd=0' for the anonymous handle (as per#2564),
1017 then I believe the public key should be also zero, as Q=0P=0;
1018 so setting everything to all-zeros (as per GNUNET_malloc)
1019 should be all that is needed here).
1026 * Destroy a pseudonym handle. Does NOT remove the private key from
1029 * @param ph pseudonym handle to destroy
1032 GNUNET_FS_pseudonym_destroy (struct GNUNET_FS_PseudonymHandle *ph)
1039 * Convert the data specified in the given purpose argument to an
1040 * S-expression suitable for signature operations.
1042 * @param purpose data to convert
1043 * @return converted s-expression
1046 data_to_pkcs1 (const struct GNUNET_FS_PseudonymSignaturePurpose *purpose)
1048 struct GNUNET_CRYPTO_ShortHashCode hc;
1052 GNUNET_CRYPTO_short_hash (purpose, ntohl (purpose->size), &hc);
1053 #define FORMATSTRING "(4:data(5:flags3:raw)(5:value32:01234567890123456789012345678901))"
1054 bufSize = strlen (FORMATSTRING) + 1;
1058 memcpy (buff, FORMATSTRING, bufSize);
1062 ("01234567890123456789012345678901))")
1063 - 1], &hc, sizeof (struct GNUNET_CRYPTO_ShortHashCode));
1064 GNUNET_assert (0 == gcry_sexp_new (&data, buff, bufSize, 0));
1074 * Cryptographically sign some data with the pseudonym.
1076 * @param ph private key 'd' 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 ('h');
1082 * see section 2.3 of #2564.
1083 * @param signature where to store the signature
1084 * @return GNUNET_SYSERR on failure
1087 GNUNET_FS_pseudonym_sign (struct GNUNET_FS_PseudonymHandle *ph,
1088 const struct GNUNET_FS_PseudonymSignaturePurpose *purpose,
1089 const struct GNUNET_HashCode *seed,
1090 const struct GNUNET_HashCode *signing_key,
1091 struct GNUNET_FS_PseudonymSignature *signature)
1099 gcry_mpi_t n; /* n from P-256 */
1106 /* get private key 'd' from pseudonym */
1107 size = sizeof (ph->d);
1108 if (0 != (rc = gcry_mpi_scan (&d, GCRYMPI_FMT_USG,
1112 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
1113 return GNUNET_SYSERR;
1115 /* get 'x' value from signing key */
1116 size = sizeof (struct GNUNET_HashCode);
1117 if (0 != (rc = gcry_mpi_scan (&h, GCRYMPI_FMT_USG,
1121 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
1122 gcry_mpi_release (d);
1123 return GNUNET_SYSERR;
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)))
1130 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
1131 gcry_mpi_release (d);
1132 gcry_mpi_release (h);
1133 return GNUNET_SYSERR;
1136 /* calculate dx = d + h mod n */
1137 dh = gcry_mpi_new (256);
1138 gcry_mpi_addm (dh, d, h, n);
1139 // gcry_mpi_release (d);
1140 // gcry_mpi_release (h);
1141 gcry_mpi_release (n);
1146 gcry_mpi_point_t hg;
1151 gcry_mpi_ec_new (&xctx, NULL, "NIST P-256");
1152 g = gcry_mpi_ec_get_point ("g", xctx, 0);
1154 hg = gcry_mpi_point_new (0);
1155 gcry_mpi_ec_mul (hg, h, g, xctx);
1156 fprintf (stderr, "\nExpected verification hG value:\n");
1157 v_x = gcry_mpi_new (256);
1158 v_y = gcry_mpi_new (256);
1159 gcry_mpi_ec_get_affine (v_x, v_y, hg, xctx);
1160 gcry_mpi_dump (v_x);
1161 gcry_mpi_dump (v_y);
1163 q = gcry_mpi_point_new (0);
1164 gcry_mpi_ec_mul (q, d, g, xctx);
1165 fprintf (stderr, "\nExpected verification q value:\n");
1166 gcry_mpi_ec_get_affine (v_x, v_y, q, xctx);
1167 gcry_mpi_dump (v_x);
1168 gcry_mpi_dump (v_y);
1170 v = gcry_mpi_point_new (0);
1171 gcry_mpi_ec_add (v, q, hg, xctx);
1172 gcry_mpi_ec_get_affine (v_x, v_y, v, xctx);
1173 fprintf (stderr, "\nExpected verification key public point value V := q + hG:\n");
1174 gcry_mpi_dump (v_x);
1175 gcry_mpi_dump (v_y);
1176 fprintf (stderr, "\n");
1181 /* now build sexpression with the signing key */
1182 if (0 != (rc = gcry_sexp_build (&spriv, &erroff,
1183 "(private-key(ecdsa(curve \"NIST P-256\")(d %m)))",
1186 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
1187 gcry_mpi_release (dh);
1188 return GNUNET_SYSERR;
1190 gcry_mpi_release (dh);
1191 /* prepare data for signing */
1192 data = data_to_pkcs1 (purpose);
1194 /* get 'k' value from seed, if available */
1197 size = sizeof (struct GNUNET_HashCode);
1198 if (0 != (rc = gcry_mpi_scan (&k, GCRYMPI_FMT_USG,
1202 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
1203 return GNUNET_SYSERR;
1207 /* actually create signature */
1208 /* FIXME: need API to pass 'k' if 'seed' was non-NULL! */
1209 if (0 != (rc = gcry_pk_sign (&result, data, spriv)))
1211 LOG (GNUNET_ERROR_TYPE_WARNING,
1212 _("ECC signing failed at %s:%d: %s\n"), __FILE__,
1213 __LINE__, gcry_strerror (rc));
1214 gcry_sexp_release (data);
1215 gcry_sexp_release (spriv);
1217 gcry_mpi_release (k);
1218 memset (signature, 0, sizeof (struct GNUNET_FS_PseudonymSignature));
1219 return GNUNET_SYSERR;
1222 gcry_mpi_release (k);
1223 gcry_sexp_release (data);
1224 gcry_sexp_release (spriv);
1227 /* extract 'r' and 's' values from sexpression 'result' and store in 'signature' */
1228 if (0 != (rc = key_from_sexp (rs, result, "sig-val", "rs")))
1231 gcry_sexp_release (result);
1232 return GNUNET_SYSERR;
1234 gcry_sexp_release (result);
1235 size = sizeof (signature->sig_r);
1236 if (0 != (rc = gcry_mpi_print (GCRYMPI_FMT_USG, signature->sig_r, size,
1239 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_print", rc);
1240 gcry_mpi_release (rs[0]);
1241 gcry_mpi_release (rs[1]);
1242 return GNUNET_SYSERR;
1244 gcry_mpi_release (rs[0]);
1245 size = sizeof (signature->sig_s);
1246 if (0 != (rc = gcry_mpi_print (GCRYMPI_FMT_USG, signature->sig_s, size,
1249 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_print", rc);
1250 gcry_mpi_release (rs[1]);
1251 return GNUNET_SYSERR;
1253 gcry_mpi_release (rs[1]);
1259 * Get an ECC context (with Q set to the respective public key) from
1262 * @param pseudonym with information on 'q'
1263 * @return curve context
1266 get_context_from_pseudonym (struct GNUNET_FS_PseudonymIdentifier *pseudonym)
1276 /* extract 'q' from pseudonym */
1277 size = sizeof (pseudonym->q_x);
1278 if (0 != (rc = gcry_mpi_scan (&q_x, GCRYMPI_FMT_USG, pseudonym->q_x, size, &size)))
1280 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
1283 size = sizeof (pseudonym->q_y);
1284 if (0 != (rc = gcry_mpi_scan (&q_y, GCRYMPI_FMT_USG, pseudonym->q_y, size, &size)))
1286 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
1287 gcry_mpi_release (q_x);
1290 q = gcry_mpi_point_new (256);
1291 ONE = gcry_mpi_new (1);
1292 gcry_mpi_set_ui (ONE, 1);
1293 gcry_mpi_point_set (q, q_x, q_y, ONE); /* FIXME: convenience function 'set_affine'? */
1294 gcry_mpi_release (ONE);
1295 gcry_mpi_release (q_x);
1296 gcry_mpi_release (q_y);
1298 /* create basic ECC context */
1299 if (0 != (rc = gcry_mpi_ec_new (&ctx, NULL, "NIST P-256")))
1301 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_ec_new", rc); /* erroff gives more info */
1302 gcry_mpi_point_release (q);
1305 /* initialize 'ctx' with 'q' */
1306 gcry_mpi_ec_set_point ("q", q, ctx);
1307 gcry_mpi_point_release (q);
1313 * Given a pseudonym and a signing key, derive the corresponding public
1314 * key that would be used to verify the resulting signature.
1316 * @param pseudonym the public key (dQ in ECDSA)
1317 * @param signing_key input to derive 'h' (see section 2.4 of #2564)
1318 * @param verification_key resulting public key to verify the signature
1319 * created from the '(d+h)' of 'pseudonym' and the 'signing_key';
1320 * the value stored here can then be given to GNUNET_FS_pseudonym_verify.
1321 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1324 GNUNET_FS_pseudonym_derive_verification_key (struct GNUNET_FS_PseudonymIdentifier *pseudonym,
1325 const struct GNUNET_HashCode *signing_key,
1326 struct GNUNET_FS_PseudonymIdentifier *verification_key)
1334 gcry_mpi_point_t hg;
1339 /* get 'h' value from signing key */
1340 size = sizeof (struct GNUNET_HashCode);
1341 if (0 != (rc = gcry_mpi_scan (&h, GCRYMPI_FMT_USG,
1345 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
1346 return GNUNET_SYSERR;
1348 /* create ECC context based on Q from pseudonym */
1349 if (NULL == (ctx = get_context_from_pseudonym (pseudonym)))
1351 gcry_mpi_release (h);
1352 return GNUNET_SYSERR;
1355 g = gcry_mpi_ec_get_point ("g", ctx, 0);
1357 /* then call the 'multiply' function, to compute the product hG */
1358 hg = gcry_mpi_point_new (0);
1359 gcry_mpi_ec_mul (hg, h, g, ctx);
1362 fprintf (stderr, "\nVerification hG value:\n");
1363 v_x = gcry_mpi_new (256);
1364 v_y = gcry_mpi_new (256);
1365 gcry_mpi_ec_get_affine (v_x, v_y, hg, ctx);
1366 gcry_mpi_dump (v_x);
1367 gcry_mpi_dump (v_y);
1369 gcry_mpi_release (h);
1371 /* get Q = dG from 'pseudonym' */
1372 q = gcry_mpi_ec_get_point ("q", ctx, 0);
1374 fprintf (stderr, "\nVerification q value:\n");
1375 v_x = gcry_mpi_new (256);
1376 v_y = gcry_mpi_new (256);
1377 gcry_mpi_ec_get_affine (v_x, v_y, q, ctx);
1378 gcry_mpi_dump (v_x);
1379 gcry_mpi_dump (v_y);
1381 /* calculate V = Q + hG = dG + hG = (d + h)G*/
1382 v = gcry_mpi_point_new (0);
1383 gcry_mpi_ec_add (v, q, hg, xctx);
1384 /* FIXME: free 'hg'? */
1386 /* store 'v' point in "verification_key" */
1387 v_x = gcry_mpi_new (256);
1388 v_y = gcry_mpi_new (256);
1389 gcry_mpi_ec_get_affine (v_x, v_y, v, xctx);
1392 fprintf (stderr, "\nVerification key public point value V := q + hG:\n");
1393 gcry_mpi_dump (v_x);
1394 gcry_mpi_dump (v_y);
1395 fprintf (stderr, " <=== WTF!?\n");
1399 gcry_mpi_point_release (v);
1400 gcry_ctx_release (ctx);
1402 size = sizeof (verification_key->q_x);
1403 if (0 != (rc = gcry_mpi_print (GCRYMPI_FMT_USG, verification_key->q_x, size,
1406 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_print", rc);
1407 gcry_mpi_release (v_x);
1408 gcry_mpi_release (v_y);
1409 return GNUNET_SYSERR;
1411 gcry_mpi_release (v_x);
1412 size = sizeof (verification_key->q_y);
1413 if (0 != (rc = gcry_mpi_print (GCRYMPI_FMT_USG, verification_key->q_y, size,
1416 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_print", rc);
1417 gcry_mpi_release (v_y);
1418 return GNUNET_SYSERR;
1420 gcry_mpi_release (v_y);
1426 * Verify a signature made with a pseudonym.
1428 * @param purpose data that was signed
1429 * @param signature signature to verify
1430 * @param verification_key public key to use for checking the signature;
1431 * corresponds to 'g^(x+h)' in section 2.4 of #2564.
1432 * @return GNUNET_OK on success (signature valid, 'pseudonym' set),
1433 * GNUNET_SYSERR if the signature is invalid
1436 GNUNET_FS_pseudonym_verify (const struct GNUNET_FS_PseudonymSignaturePurpose *purpose,
1437 const struct GNUNET_FS_PseudonymSignature *signature,
1438 const struct GNUNET_FS_PseudonymIdentifier *verification_key)
1441 gcry_sexp_t sig_sexpr;
1442 gcry_sexp_t pk_sexpr;
1454 /* build s-expression for signature */
1455 size = sizeof (signature->sig_r);
1456 if (0 != (rc = gcry_mpi_scan (&r, GCRYMPI_FMT_USG,
1457 signature->sig_r, size, &size)))
1459 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
1460 return GNUNET_SYSERR;
1462 size = sizeof (signature->sig_s);
1463 if (0 != (rc = gcry_mpi_scan (&s, GCRYMPI_FMT_USG,
1464 signature->sig_s, size, &size)))
1466 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
1467 gcry_mpi_release (r);
1468 return GNUNET_SYSERR;
1470 if (0 != (rc = gcry_sexp_build (&sig_sexpr, &erroff, "(sig-val(ecdsa(r %m)(s %m)))",
1473 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
1474 gcry_mpi_release (r);
1475 gcry_mpi_release (s);
1476 return GNUNET_SYSERR;
1478 gcry_mpi_release (r);
1479 gcry_mpi_release (s);
1481 /* build s-expression for data that was signed */
1482 data = data_to_pkcs1 (purpose);
1484 /* create context of public key and initialize Q */
1485 size = sizeof (verification_key->q_x);
1486 if (0 != (rc = gcry_mpi_scan (&q_x, GCRYMPI_FMT_USG,
1487 verification_key->q_x, size, &size)))
1489 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
1490 gcry_sexp_release (data);
1491 gcry_sexp_release (sig_sexpr);
1492 return GNUNET_SYSERR;
1494 size = sizeof (verification_key->q_y);
1495 if (0 != (rc = gcry_mpi_scan (&q_y, GCRYMPI_FMT_USG,
1496 verification_key->q_y, size, &size)))
1498 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
1499 gcry_sexp_release (data);
1500 gcry_sexp_release (sig_sexpr);
1501 gcry_mpi_release (q_x);
1502 return GNUNET_SYSERR;
1504 q = gcry_mpi_point_new (256);
1505 ONE = gcry_mpi_new (1);
1506 gcry_mpi_set_ui (ONE, 1);
1507 gcry_mpi_point_set (q, q_x, q_y, ONE); /* FIXME: convenience function 'set_affine'? */
1508 gcry_mpi_release (ONE);
1509 gcry_mpi_release (q_x);
1510 gcry_mpi_release (q_y);
1512 /* create basic ECC context */
1513 if (0 != (rc = gcry_mpi_ec_new (&ctx, NULL, "NIST P-256")))
1515 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_ec_new", rc); /* erroff gives more info */
1516 gcry_sexp_release (data);
1517 gcry_sexp_release (sig_sexpr);
1518 gcry_mpi_point_release (q);
1519 return GNUNET_SYSERR;
1521 /* initialize 'ctx' with 'q' */
1522 gcry_mpi_ec_set_point ("q", q, ctx);
1523 gcry_mpi_point_release (q);
1525 /* convert 'ctx' to 'sexp' */
1526 if (0 != (rc = gcry_pubkey_get_sexp (&pk_sexpr, GCRY_PK_GET_PUBKEY, ctx)))
1528 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_from_context", rc);
1529 gcry_ctx_release (ctx);
1530 gcry_sexp_release (data);
1531 gcry_sexp_release (sig_sexpr);
1532 return GNUNET_SYSERR;
1534 gcry_ctx_release (ctx);
1536 /* finally, verify the signature */
1537 rc = gcry_pk_verify (sig_sexpr, data, pk_sexpr);
1538 gcry_sexp_release (sig_sexpr);
1539 gcry_sexp_release (data);
1540 gcry_sexp_release (pk_sexpr);
1543 LOG (GNUNET_ERROR_TYPE_WARNING,
1544 _("ECDSA signature verification failed at %s:%d: %s\n"), __FILE__,
1545 __LINE__, gcry_strerror (rc));
1547 return GNUNET_SYSERR;
1554 * Get the identifier (public key) of a pseudonym.
1556 * @param ph pseudonym handle with the private key
1557 * @param pseudonym pseudonym identifier (set based on 'ph')
1560 GNUNET_FS_pseudonym_get_identifier (struct GNUNET_FS_PseudonymHandle *ph,
1561 struct GNUNET_FS_PseudonymIdentifier *pseudonym)
1563 memcpy (pseudonym, &ph->public_key,
1564 sizeof (struct GNUNET_FS_PseudonymIdentifier));
1569 * Remove pseudonym from the set of known pseudonyms.
1571 * @param cfg overall configuration
1572 * @param id the pseudonym identifier
1573 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1576 GNUNET_FS_pseudonym_remove (const struct GNUNET_CONFIGURATION_Handle *cfg,
1577 const struct GNUNET_FS_PseudonymIdentifier *id)
1582 fn = get_data_filename (cfg, PS_METADATA_DIR, id);
1584 return GNUNET_SYSERR;
1585 result = UNLINK (fn);
1587 return (0 == result) ? GNUNET_OK : GNUNET_SYSERR;
1590 /* end of pseudonym.c */