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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file util/crypto_ecc_setup.c
23 * @brief helper function for easy EdDSA key setup
24 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
30 #define LOG(kind,...) GNUNET_log_from (kind, "util-crypto-ecc", __VA_ARGS__)
32 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-crypto-ecc", syscall)
34 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util-crypto-ecc", syscall, filename)
37 * Log an error message at log-level 'level' that indicates
38 * a failure of the command 'cmd' with the message given
39 * by gcry_strerror(rc).
41 #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)
45 * Wait for a short time (we're trying to lock a file or want
46 * to give another process a shot at finishing a disk write, etc.).
47 * Sleeps for 100ms (as that should be long enough for virtually all
48 * modern systems to context switch and allow another process to do
54 struct GNUNET_TIME_Relative timeout;
56 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100);
57 (void) GNUNET_NETWORK_socket_select (NULL, NULL, NULL, timeout);
62 * Create a new private key by reading it from a file. If the
63 * files does not exist, create a new key and write it to the
64 * file. Caller must free return value. Note that this function
65 * can not guarantee that another process might not be trying
66 * the same operation on the same file at the same time.
67 * If the contents of the file
68 * are invalid the old file is deleted and a fresh key is
71 * @param filename name of file to use to store the key
72 * @return new private key, NULL on error (for example,
75 struct GNUNET_CRYPTO_EddsaPrivateKey *
76 GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename)
78 struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
79 struct GNUNET_DISK_FileHandle *fd;
85 if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file (filename))
87 while (GNUNET_YES != GNUNET_DISK_file_test (filename))
89 fd = GNUNET_DISK_file_open (filename,
90 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE
91 | GNUNET_DISK_OPEN_FAILIFEXISTS,
92 GNUNET_DISK_PERM_USER_READ |
93 GNUNET_DISK_PERM_USER_WRITE);
98 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
100 /* must exist but not be accessible, fail for good! */
101 if (0 != ACCESS (filename, R_OK))
102 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", filename);
104 GNUNET_break (0); /* what is going on!? */
109 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
114 GNUNET_DISK_file_lock (fd, 0,
115 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey),
122 LOG (GNUNET_ERROR_TYPE_ERROR,
123 _("Could not acquire lock on file `%s': %s...\n"),
128 LOG (GNUNET_ERROR_TYPE_INFO,
129 _("Creating a new private key. This may take a while.\n"));
130 priv = GNUNET_CRYPTO_eddsa_key_create ();
131 GNUNET_assert (NULL != priv);
132 GNUNET_assert (sizeof (*priv) ==
133 GNUNET_DISK_file_write (fd, priv, sizeof (*priv)));
134 GNUNET_DISK_file_sync (fd);
136 GNUNET_DISK_file_unlock (fd, 0,
137 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey)))
138 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
139 GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
142 /* key file exists already, read it! */
143 fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
144 GNUNET_DISK_PERM_NONE);
147 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
154 GNUNET_DISK_file_lock (fd, 0,
155 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey),
161 LOG (GNUNET_ERROR_TYPE_ERROR,
162 _("Could not acquire lock on file `%s': %s...\n"), filename,
164 LOG (GNUNET_ERROR_TYPE_ERROR,
166 ("This may be ok if someone is currently generating a private key.\n"));
171 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
173 /* eh, what!? File we opened is now gone!? */
174 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
176 GNUNET_DISK_file_unlock (fd, 0,
177 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey)))
178 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
179 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
183 if (GNUNET_OK != GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
185 if (fs < sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey))
187 /* maybe we got the read lock before the key generating
188 * process had a chance to get the write lock; give it up! */
190 GNUNET_DISK_file_unlock (fd, 0,
191 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey)))
192 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
195 LOG (GNUNET_ERROR_TYPE_ERROR,
196 _("When trying to read key file `%s' I found %u bytes but I need at least %u.\n"),
199 (unsigned int) sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey));
200 LOG (GNUNET_ERROR_TYPE_ERROR,
201 _("This may be ok if someone is currently generating a key.\n"));
203 short_wait (); /* wait a bit longer! */
208 fs = sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey);
209 priv = GNUNET_malloc (fs);
210 sret = GNUNET_DISK_file_read (fd,
213 GNUNET_assert ( (sret >= 0) &&
214 (fs == (size_t) sret) );
216 GNUNET_DISK_file_unlock (fd,
218 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey)))
219 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
222 GNUNET_assert (GNUNET_YES ==
223 GNUNET_DISK_file_close (fd));
226 check_eddsa_key (priv))
238 * Create a new private key by reading it from a file. If the
239 * files does not exist, create a new key and write it to the
240 * file. Caller must free return value. Note that this function
241 * can not guarantee that another process might not be trying
242 * the same operation on the same file at the same time.
243 * If the contents of the file
244 * are invalid the old file is deleted and a fresh key is
247 * @param filename name of file to use to store the key
248 * @return new private key, NULL on error (for example,
251 struct GNUNET_CRYPTO_EcdsaPrivateKey *
252 GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename)
254 struct GNUNET_CRYPTO_EcdsaPrivateKey *priv;
255 struct GNUNET_DISK_FileHandle *fd;
262 GNUNET_DISK_directory_create_for_file (filename))
264 while (GNUNET_YES != GNUNET_DISK_file_test (filename))
266 fd = GNUNET_DISK_file_open (filename,
267 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE
268 | GNUNET_DISK_OPEN_FAILIFEXISTS,
269 GNUNET_DISK_PERM_USER_READ |
270 GNUNET_DISK_PERM_USER_WRITE);
275 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
277 /* must exist but not be accessible, fail for good! */
278 if (0 != ACCESS (filename, R_OK))
279 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR,
283 GNUNET_break (0); /* what is going on!? */
288 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR,
295 GNUNET_DISK_file_lock (fd,
297 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey),
304 LOG (GNUNET_ERROR_TYPE_ERROR,
305 _("Could not acquire lock on file `%s': %s...\n"),
310 LOG (GNUNET_ERROR_TYPE_INFO,
311 _("Creating a new private key. This may take a while.\n"));
312 priv = GNUNET_CRYPTO_ecdsa_key_create ();
313 GNUNET_assert (NULL != priv);
314 GNUNET_assert (sizeof (*priv) ==
315 GNUNET_DISK_file_write (fd,
318 GNUNET_DISK_file_sync (fd);
320 GNUNET_DISK_file_unlock (fd, 0,
321 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
322 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
325 GNUNET_assert (GNUNET_YES ==
326 GNUNET_DISK_file_close (fd));
329 /* key file exists already, read it! */
330 fd = GNUNET_DISK_file_open (filename,
331 GNUNET_DISK_OPEN_READ,
332 GNUNET_DISK_PERM_NONE);
335 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR,
344 GNUNET_DISK_file_lock (fd, 0,
345 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey),
351 LOG (GNUNET_ERROR_TYPE_ERROR,
352 _("Could not acquire lock on file `%s': %s...\n"),
355 LOG (GNUNET_ERROR_TYPE_ERROR,
356 _("This may be ok if someone is currently generating a private key.\n"));
362 GNUNET_DISK_file_test (filename))
364 /* eh, what!? File we opened is now gone!? */
365 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
369 GNUNET_DISK_file_unlock (fd, 0,
370 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
371 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
374 GNUNET_assert (GNUNET_OK ==
375 GNUNET_DISK_file_close (fd));
380 GNUNET_DISK_file_size (filename,
385 if (fs < sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))
387 /* maybe we got the read lock before the key generating
388 * process had a chance to get the write lock; give it up! */
390 GNUNET_DISK_file_unlock (fd, 0,
391 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
392 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
397 LOG (GNUNET_ERROR_TYPE_ERROR,
398 _("When trying to read key file `%s' I found %u bytes but I need at least %u.\n"),
399 filename, (unsigned int) fs,
400 (unsigned int) sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
401 LOG (GNUNET_ERROR_TYPE_ERROR,
402 _("This may be ok if someone is currently generating a key.\n"));
404 short_wait (); /* wait a bit longer! */
409 fs = sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
410 priv = GNUNET_malloc (fs);
411 sret = GNUNET_DISK_file_read (fd,
414 GNUNET_assert ( (sret >= 0) &&
415 (fs == (size_t) sret) );
417 GNUNET_DISK_file_unlock (fd, 0,
418 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
419 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
422 GNUNET_assert (GNUNET_YES ==
423 GNUNET_DISK_file_close (fd));
429 * Create a new private key by reading our peer's key from
430 * the file specified in the configuration.
432 * @param cfg the configuration to use
433 * @return new private key, NULL on error (for example,
436 struct GNUNET_CRYPTO_EddsaPrivateKey *
437 GNUNET_CRYPTO_eddsa_key_create_from_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg)
439 struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
443 GNUNET_CONFIGURATION_get_value_filename (cfg, "PEER", "PRIVATE_KEY", &fn))
445 priv = GNUNET_CRYPTO_eddsa_key_create_from_file (fn);
452 * Retrieve the identity of the host's peer.
454 * @param cfg configuration to use
455 * @param dst pointer to where to write the peer identity
456 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the identity
457 * could not be retrieved
460 GNUNET_CRYPTO_get_peer_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
461 struct GNUNET_PeerIdentity *dst)
463 struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
465 if (NULL == (priv = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg)))
467 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
468 _("Could not load peer's private key\n"));
469 return GNUNET_SYSERR;
471 GNUNET_CRYPTO_eddsa_key_get_public (priv, &dst->public_key);
478 * Setup a key file for a peer given the name of the
479 * configuration file (!). This function is used so that
480 * at a later point code can be certain that reading a
481 * key is fast (for example in time-dependent testcases).
483 * @param cfg_name name of the configuration file to use
486 GNUNET_CRYPTO_eddsa_setup_key (const char *cfg_name)
488 struct GNUNET_CONFIGURATION_Handle *cfg;
489 struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
491 cfg = GNUNET_CONFIGURATION_create ();
492 (void) GNUNET_CONFIGURATION_load (cfg, cfg_name);
493 priv = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg);
496 GNUNET_CONFIGURATION_destroy (cfg);
499 /* end of crypto_ecc_setup.c */