2 This file is part of GNUnet.
3 Copyright (C) 2012, 2013, 2015 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file util/crypto_ecc_setup.c
21 * @brief helper function for easy EdDSA key setup
22 * @author Christian Grothoff
26 #include "gnunet_util_lib.h"
28 #define LOG(kind,...) GNUNET_log_from (kind, "util-crypto-ecc", __VA_ARGS__)
30 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-crypto-ecc", syscall)
32 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util-crypto-ecc", syscall, filename)
35 * Log an error message at log-level 'level' that indicates
36 * a failure of the command 'cmd' with the message given
37 * by gcry_strerror(rc).
39 #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)
43 * Wait for a short time (we're trying to lock a file or want
44 * to give another process a shot at finishing a disk write, etc.).
45 * Sleeps for 100ms (as that should be long enough for virtually all
46 * modern systems to context switch and allow another process to do
52 struct GNUNET_TIME_Relative timeout;
54 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100);
55 (void) GNUNET_NETWORK_socket_select (NULL, NULL, NULL, timeout);
60 * Create a new private key by reading it from a file. If the
61 * files does not exist, create a new key and write it to the
62 * file. Caller must free return value. Note that this function
63 * can not guarantee that another process might not be trying
64 * the same operation on the same file at the same time.
65 * If the contents of the file
66 * are invalid the old file is deleted and a fresh key is
69 * @param filename name of file to use to store the key
70 * @return new private key, NULL on error (for example,
73 struct GNUNET_CRYPTO_EddsaPrivateKey *
74 GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename)
76 struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
77 struct GNUNET_DISK_FileHandle *fd;
83 if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file (filename))
85 while (GNUNET_YES != GNUNET_DISK_file_test (filename))
87 fd = GNUNET_DISK_file_open (filename,
88 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE
89 | GNUNET_DISK_OPEN_FAILIFEXISTS,
90 GNUNET_DISK_PERM_USER_READ |
91 GNUNET_DISK_PERM_USER_WRITE);
96 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
98 /* must exist but not be accessible, fail for good! */
99 if (0 != ACCESS (filename, R_OK))
100 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", filename);
102 GNUNET_break (0); /* what is going on!? */
107 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
112 GNUNET_DISK_file_lock (fd, 0,
113 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey),
120 LOG (GNUNET_ERROR_TYPE_ERROR,
121 _("Could not acquire lock on file `%s': %s...\n"),
126 LOG (GNUNET_ERROR_TYPE_INFO,
127 _("Creating a new private key. This may take a while.\n"));
128 priv = GNUNET_CRYPTO_eddsa_key_create ();
129 GNUNET_assert (NULL != priv);
130 GNUNET_assert (sizeof (*priv) ==
131 GNUNET_DISK_file_write (fd, priv, sizeof (*priv)));
132 GNUNET_DISK_file_sync (fd);
134 GNUNET_DISK_file_unlock (fd, 0,
135 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey)))
136 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
137 GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
140 /* key file exists already, read it! */
141 fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
142 GNUNET_DISK_PERM_NONE);
145 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
152 GNUNET_DISK_file_lock (fd, 0,
153 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey),
159 LOG (GNUNET_ERROR_TYPE_ERROR,
160 _("Could not acquire lock on file `%s': %s...\n"), filename,
162 LOG (GNUNET_ERROR_TYPE_ERROR,
164 ("This may be ok if someone is currently generating a private key.\n"));
169 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
171 /* eh, what!? File we opened is now gone!? */
172 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
174 GNUNET_DISK_file_unlock (fd, 0,
175 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey)))
176 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
177 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
181 if (GNUNET_OK != GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
183 if (fs < sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey))
185 /* maybe we got the read lock before the key generating
186 * process had a chance to get the write lock; give it up! */
188 GNUNET_DISK_file_unlock (fd, 0,
189 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey)))
190 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
193 LOG (GNUNET_ERROR_TYPE_ERROR,
194 _("When trying to read key file `%s' I found %u bytes but I need at least %u.\n"),
197 (unsigned int) sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey));
198 LOG (GNUNET_ERROR_TYPE_ERROR,
199 _("This may be ok if someone is currently generating a key.\n"));
201 short_wait (); /* wait a bit longer! */
206 fs = sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey);
207 priv = GNUNET_malloc (fs);
208 sret = GNUNET_DISK_file_read (fd,
211 GNUNET_assert ( (sret >= 0) &&
212 (fs == (size_t) sret) );
214 GNUNET_DISK_file_unlock (fd,
216 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey)))
217 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
220 GNUNET_assert (GNUNET_YES ==
221 GNUNET_DISK_file_close (fd));
224 check_eddsa_key (priv))
236 * Create a new private key by reading it from a file. If the
237 * files does not exist, create a new key and write it to the
238 * file. Caller must free return value. Note that this function
239 * can not guarantee that another process might not be trying
240 * the same operation on the same file at the same time.
241 * If the contents of the file
242 * are invalid the old file is deleted and a fresh key is
245 * @param filename name of file to use to store the key
246 * @return new private key, NULL on error (for example,
249 struct GNUNET_CRYPTO_EcdsaPrivateKey *
250 GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename)
252 struct GNUNET_CRYPTO_EcdsaPrivateKey *priv;
253 struct GNUNET_DISK_FileHandle *fd;
260 GNUNET_DISK_directory_create_for_file (filename))
262 while (GNUNET_YES != GNUNET_DISK_file_test (filename))
264 fd = GNUNET_DISK_file_open (filename,
265 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE
266 | GNUNET_DISK_OPEN_FAILIFEXISTS,
267 GNUNET_DISK_PERM_USER_READ |
268 GNUNET_DISK_PERM_USER_WRITE);
273 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
275 /* must exist but not be accessible, fail for good! */
276 if (0 != ACCESS (filename, R_OK))
277 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR,
281 GNUNET_break (0); /* what is going on!? */
286 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR,
293 GNUNET_DISK_file_lock (fd,
295 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey),
302 LOG (GNUNET_ERROR_TYPE_ERROR,
303 _("Could not acquire lock on file `%s': %s...\n"),
308 LOG (GNUNET_ERROR_TYPE_INFO,
309 _("Creating a new private key. This may take a while.\n"));
310 priv = GNUNET_CRYPTO_ecdsa_key_create ();
311 GNUNET_assert (NULL != priv);
312 GNUNET_assert (sizeof (*priv) ==
313 GNUNET_DISK_file_write (fd,
316 GNUNET_DISK_file_sync (fd);
318 GNUNET_DISK_file_unlock (fd, 0,
319 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
320 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
323 GNUNET_assert (GNUNET_YES ==
324 GNUNET_DISK_file_close (fd));
327 /* key file exists already, read it! */
328 fd = GNUNET_DISK_file_open (filename,
329 GNUNET_DISK_OPEN_READ,
330 GNUNET_DISK_PERM_NONE);
333 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR,
342 GNUNET_DISK_file_lock (fd, 0,
343 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey),
349 LOG (GNUNET_ERROR_TYPE_ERROR,
350 _("Could not acquire lock on file `%s': %s...\n"),
353 LOG (GNUNET_ERROR_TYPE_ERROR,
354 _("This may be ok if someone is currently generating a private key.\n"));
360 GNUNET_DISK_file_test (filename))
362 /* eh, what!? File we opened is now gone!? */
363 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
367 GNUNET_DISK_file_unlock (fd, 0,
368 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
369 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
372 GNUNET_assert (GNUNET_OK ==
373 GNUNET_DISK_file_close (fd));
378 GNUNET_DISK_file_size (filename,
383 if (fs < sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))
385 /* maybe we got the read lock before the key generating
386 * process had a chance to get the write lock; give it up! */
388 GNUNET_DISK_file_unlock (fd, 0,
389 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
390 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
395 LOG (GNUNET_ERROR_TYPE_ERROR,
396 _("When trying to read key file `%s' I found %u bytes but I need at least %u.\n"),
397 filename, (unsigned int) fs,
398 (unsigned int) sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
399 LOG (GNUNET_ERROR_TYPE_ERROR,
400 _("This may be ok if someone is currently generating a key.\n"));
402 short_wait (); /* wait a bit longer! */
407 fs = sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
408 priv = GNUNET_malloc (fs);
409 sret = GNUNET_DISK_file_read (fd,
412 GNUNET_assert ( (sret >= 0) &&
413 (fs == (size_t) sret) );
415 GNUNET_DISK_file_unlock (fd, 0,
416 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
417 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
420 GNUNET_assert (GNUNET_YES ==
421 GNUNET_DISK_file_close (fd));
427 * Create a new private key by reading our peer's key from
428 * the file specified in the configuration.
430 * @param cfg the configuration to use
431 * @return new private key, NULL on error (for example,
434 struct GNUNET_CRYPTO_EddsaPrivateKey *
435 GNUNET_CRYPTO_eddsa_key_create_from_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg)
437 struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
441 GNUNET_CONFIGURATION_get_value_filename (cfg, "PEER", "PRIVATE_KEY", &fn))
443 priv = GNUNET_CRYPTO_eddsa_key_create_from_file (fn);
450 * Retrieve the identity of the host's peer.
452 * @param cfg configuration to use
453 * @param dst pointer to where to write the peer identity
454 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the identity
455 * could not be retrieved
458 GNUNET_CRYPTO_get_peer_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
459 struct GNUNET_PeerIdentity *dst)
461 struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
463 if (NULL == (priv = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg)))
465 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
466 _("Could not load peer's private key\n"));
467 return GNUNET_SYSERR;
469 GNUNET_CRYPTO_eddsa_key_get_public (priv, &dst->public_key);
476 * Setup a key file for a peer given the name of the
477 * configuration file (!). This function is used so that
478 * at a later point code can be certain that reading a
479 * key is fast (for example in time-dependent testcases).
481 * @param cfg_name name of the configuration file to use
484 GNUNET_CRYPTO_eddsa_setup_key (const char *cfg_name)
486 struct GNUNET_CONFIGURATION_Handle *cfg;
487 struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
489 cfg = GNUNET_CONFIGURATION_create ();
490 (void) GNUNET_CONFIGURATION_load (cfg, cfg_name);
491 priv = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg);
494 GNUNET_CONFIGURATION_destroy (cfg);
497 /* end of crypto_ecc_setup.c */