crypto_ecc_setup.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2012, 2013, 2015 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file util/crypto_ecc_setup.c
  18. * @brief helper function for easy EdDSA key setup
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include <gcrypt.h>
  23. #include "gnunet_util_lib.h"
  24. #define LOG(kind, ...) GNUNET_log_from (kind, "util-crypto-ecc", __VA_ARGS__)
  25. #define LOG_STRERROR(kind, syscall) \
  26. GNUNET_log_from_strerror (kind, "util-crypto-ecc", syscall)
  27. #define LOG_STRERROR_FILE(kind, syscall, filename) \
  28. GNUNET_log_from_strerror_file (kind, "util-crypto-ecc", syscall, filename)
  29. /**
  30. * Log an error message at log-level 'level' that indicates
  31. * a failure of the command 'cmd' with the message given
  32. * by gcry_strerror(rc).
  33. */
  34. #define LOG_GCRY(level, cmd, rc) \
  35. do \
  36. { \
  37. LOG (level, \
  38. _ ("`%s' failed at %s:%d with error: %s\n"), \
  39. cmd, \
  40. __FILE__, \
  41. __LINE__, \
  42. gcry_strerror (rc)); \
  43. } while (0)
  44. /**
  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
  49. * some 'real' work).
  50. */
  51. static void
  52. short_wait ()
  53. {
  54. struct GNUNET_TIME_Relative timeout;
  55. timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100);
  56. (void) GNUNET_NETWORK_socket_select (NULL, NULL, NULL, timeout);
  57. }
  58. /**
  59. * Create a new private key by reading it from a file. If the
  60. * files does not exist, create a new key and write it to the
  61. * file. Caller must free return value. Note that this function
  62. * can not guarantee that another process might not be trying
  63. * the same operation on the same file at the same time.
  64. * If the contents of the file
  65. * are invalid the old file is deleted and a fresh key is
  66. * created.
  67. *
  68. * @param filename name of file to use to store the key
  69. * @return new private key, NULL on error (for example,
  70. * permission denied)
  71. */
  72. struct GNUNET_CRYPTO_EddsaPrivateKey *
  73. GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename)
  74. {
  75. struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
  76. struct GNUNET_DISK_FileHandle *fd;
  77. unsigned int cnt;
  78. int ec;
  79. uint64_t fs;
  80. ssize_t sret;
  81. if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file (filename))
  82. return NULL;
  83. while (GNUNET_YES != GNUNET_DISK_file_test (filename))
  84. {
  85. fd =
  86. GNUNET_DISK_file_open (filename,
  87. GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE
  88. | GNUNET_DISK_OPEN_FAILIFEXISTS,
  89. GNUNET_DISK_PERM_USER_READ
  90. | GNUNET_DISK_PERM_USER_WRITE);
  91. if (NULL == fd)
  92. {
  93. if (EEXIST == errno)
  94. {
  95. if (GNUNET_YES != GNUNET_DISK_file_test (filename))
  96. {
  97. /* must exist but not be accessible, fail for good! */
  98. if (0 != access (filename, R_OK))
  99. LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", filename);
  100. else
  101. GNUNET_break (0); /* what is going on!? */
  102. return NULL;
  103. }
  104. continue;
  105. }
  106. LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
  107. return NULL;
  108. }
  109. cnt = 0;
  110. while (GNUNET_YES !=
  111. GNUNET_DISK_file_lock (fd,
  112. 0,
  113. sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey),
  114. GNUNET_YES))
  115. {
  116. short_wait ();
  117. if (0 == ++cnt % 10)
  118. {
  119. ec = errno;
  120. LOG (GNUNET_ERROR_TYPE_ERROR,
  121. _ ("Could not acquire lock on file `%s': %s...\n"),
  122. filename,
  123. strerror (ec));
  124. }
  125. }
  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);
  133. if (GNUNET_YES !=
  134. GNUNET_DISK_file_unlock (fd,
  135. 0,
  136. sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey)))
  137. LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
  138. GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
  139. return priv;
  140. }
  141. /* key file exists already, read it! */
  142. fd = GNUNET_DISK_file_open (filename,
  143. GNUNET_DISK_OPEN_READ,
  144. GNUNET_DISK_PERM_NONE);
  145. if (NULL == fd)
  146. {
  147. LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
  148. return NULL;
  149. }
  150. cnt = 0;
  151. while (1)
  152. {
  153. if (GNUNET_YES !=
  154. GNUNET_DISK_file_lock (fd,
  155. 0,
  156. sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey),
  157. GNUNET_NO))
  158. {
  159. if (0 == ++cnt % 60)
  160. {
  161. ec = errno;
  162. LOG (GNUNET_ERROR_TYPE_ERROR,
  163. _ ("Could not acquire lock on file `%s': %s...\n"),
  164. filename,
  165. strerror (ec));
  166. LOG (
  167. GNUNET_ERROR_TYPE_ERROR,
  168. _ (
  169. "This may be ok if someone is currently generating a private key.\n"));
  170. }
  171. short_wait ();
  172. continue;
  173. }
  174. if (GNUNET_YES != GNUNET_DISK_file_test (filename))
  175. {
  176. /* eh, what!? File we opened is now gone!? */
  177. LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
  178. if (GNUNET_YES !=
  179. GNUNET_DISK_file_unlock (fd,
  180. 0,
  181. sizeof(
  182. struct GNUNET_CRYPTO_EddsaPrivateKey)))
  183. LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
  184. GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
  185. return NULL;
  186. }
  187. if (GNUNET_OK !=
  188. GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
  189. fs = 0;
  190. if (fs < sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey))
  191. {
  192. /* maybe we got the read lock before the key generating
  193. * process had a chance to get the write lock; give it up! */
  194. if (GNUNET_YES !=
  195. GNUNET_DISK_file_unlock (fd,
  196. 0,
  197. sizeof(
  198. struct GNUNET_CRYPTO_EddsaPrivateKey)))
  199. LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
  200. if (0 == ++cnt % 10)
  201. {
  202. LOG (GNUNET_ERROR_TYPE_ERROR,
  203. _ (
  204. "When trying to read key file `%s' I found %u bytes but I need at least %u.\n"),
  205. filename,
  206. (unsigned int) fs,
  207. (unsigned int) sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey));
  208. LOG (GNUNET_ERROR_TYPE_ERROR,
  209. _ ("This may be ok if someone is currently generating a key.\n"));
  210. }
  211. short_wait (); /* wait a bit longer! */
  212. continue;
  213. }
  214. break;
  215. }
  216. fs = sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey);
  217. priv = GNUNET_malloc (fs);
  218. sret = GNUNET_DISK_file_read (fd, priv, fs);
  219. GNUNET_assert ((sret >= 0) && (fs == (size_t) sret));
  220. if (GNUNET_YES !=
  221. GNUNET_DISK_file_unlock (fd,
  222. 0,
  223. sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey)))
  224. LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
  225. GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
  226. #if CRYPTO_BUG
  227. if (GNUNET_OK != check_eddsa_key (priv))
  228. {
  229. GNUNET_break (0);
  230. GNUNET_free (priv);
  231. return NULL;
  232. }
  233. #endif
  234. return priv;
  235. }
  236. /**
  237. * Create a new private key by reading it from a file. If the
  238. * files does not exist, create a new key and write it to the
  239. * file. Caller must free return value. Note that this function
  240. * can not guarantee that another process might not be trying
  241. * the same operation on the same file at the same time.
  242. * If the contents of the file
  243. * are invalid the old file is deleted and a fresh key is
  244. * created.
  245. *
  246. * @param filename name of file to use to store the key
  247. * @return new private key, NULL on error (for example,
  248. * permission denied)
  249. */
  250. struct GNUNET_CRYPTO_EcdsaPrivateKey *
  251. GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename)
  252. {
  253. struct GNUNET_CRYPTO_EcdsaPrivateKey *priv;
  254. struct GNUNET_DISK_FileHandle *fd;
  255. unsigned int cnt;
  256. int ec;
  257. uint64_t fs;
  258. ssize_t sret;
  259. if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file (filename))
  260. return NULL;
  261. while (GNUNET_YES != GNUNET_DISK_file_test (filename))
  262. {
  263. fd =
  264. 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);
  269. if (NULL == fd)
  270. {
  271. if (EEXIST == errno)
  272. {
  273. if (GNUNET_YES != GNUNET_DISK_file_test (filename))
  274. {
  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, "access", filename);
  278. else
  279. GNUNET_break (0); /* what is going on!? */
  280. return NULL;
  281. }
  282. continue;
  283. }
  284. LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
  285. return NULL;
  286. }
  287. cnt = 0;
  288. while (GNUNET_YES !=
  289. GNUNET_DISK_file_lock (fd,
  290. 0,
  291. sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey),
  292. GNUNET_YES))
  293. {
  294. short_wait ();
  295. if (0 == ++cnt % 10)
  296. {
  297. ec = errno;
  298. LOG (GNUNET_ERROR_TYPE_ERROR,
  299. _ ("Could not acquire lock on file `%s': %s...\n"),
  300. filename,
  301. strerror (ec));
  302. }
  303. }
  304. LOG (GNUNET_ERROR_TYPE_INFO,
  305. _ ("Creating a new private key. This may take a while.\n"));
  306. priv = GNUNET_CRYPTO_ecdsa_key_create ();
  307. GNUNET_assert (NULL != priv);
  308. GNUNET_assert (sizeof(*priv) ==
  309. GNUNET_DISK_file_write (fd, priv, sizeof(*priv)));
  310. GNUNET_DISK_file_sync (fd);
  311. if (GNUNET_YES !=
  312. GNUNET_DISK_file_unlock (fd,
  313. 0,
  314. sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey)))
  315. LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
  316. GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
  317. return priv;
  318. }
  319. /* key file exists already, read it! */
  320. fd = GNUNET_DISK_file_open (filename,
  321. GNUNET_DISK_OPEN_READ,
  322. GNUNET_DISK_PERM_NONE);
  323. if (NULL == fd)
  324. {
  325. LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
  326. return NULL;
  327. }
  328. cnt = 0;
  329. while (1)
  330. {
  331. if (GNUNET_YES !=
  332. GNUNET_DISK_file_lock (fd,
  333. 0,
  334. sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey),
  335. GNUNET_NO))
  336. {
  337. if (0 == ++cnt % 60)
  338. {
  339. ec = errno;
  340. LOG (GNUNET_ERROR_TYPE_ERROR,
  341. _ ("Could not acquire lock on file `%s': %s...\n"),
  342. filename,
  343. strerror (ec));
  344. LOG (
  345. GNUNET_ERROR_TYPE_ERROR,
  346. _ (
  347. "This may be ok if someone is currently generating a private key.\n"));
  348. }
  349. short_wait ();
  350. continue;
  351. }
  352. if (GNUNET_YES != GNUNET_DISK_file_test (filename))
  353. {
  354. /* eh, what!? File we opened is now gone!? */
  355. LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
  356. if (GNUNET_YES !=
  357. GNUNET_DISK_file_unlock (fd,
  358. 0,
  359. sizeof(
  360. struct GNUNET_CRYPTO_EcdsaPrivateKey)))
  361. LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
  362. GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
  363. return NULL;
  364. }
  365. if (GNUNET_OK !=
  366. GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
  367. fs = 0;
  368. if (fs < sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey))
  369. {
  370. /* maybe we got the read lock before the key generating
  371. * process had a chance to get the write lock; give it up! */
  372. if (GNUNET_YES !=
  373. GNUNET_DISK_file_unlock (fd,
  374. 0,
  375. sizeof(
  376. struct GNUNET_CRYPTO_EcdsaPrivateKey)))
  377. LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
  378. if (0 == ++cnt % 10)
  379. {
  380. LOG (GNUNET_ERROR_TYPE_ERROR,
  381. _ (
  382. "When trying to read key file `%s' I found %u bytes but I need at least %u.\n"),
  383. filename,
  384. (unsigned int) fs,
  385. (unsigned int) sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey));
  386. LOG (GNUNET_ERROR_TYPE_ERROR,
  387. _ ("This may be ok if someone is currently generating a key.\n"));
  388. }
  389. short_wait (); /* wait a bit longer! */
  390. continue;
  391. }
  392. break;
  393. }
  394. fs = sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey);
  395. priv = GNUNET_malloc (fs);
  396. sret = GNUNET_DISK_file_read (fd, priv, fs);
  397. GNUNET_assert ((sret >= 0) && (fs == (size_t) sret));
  398. if (GNUNET_YES !=
  399. GNUNET_DISK_file_unlock (fd,
  400. 0,
  401. sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey)))
  402. LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
  403. GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
  404. return priv;
  405. }
  406. /**
  407. * Create a new private key by reading our peer's key from
  408. * the file specified in the configuration.
  409. *
  410. * @param cfg the configuration to use
  411. * @return new private key, NULL on error (for example,
  412. * permission denied)
  413. */
  414. struct GNUNET_CRYPTO_EddsaPrivateKey *
  415. GNUNET_CRYPTO_eddsa_key_create_from_configuration (
  416. const struct GNUNET_CONFIGURATION_Handle *cfg)
  417. {
  418. struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
  419. char *fn;
  420. if (GNUNET_OK !=
  421. GNUNET_CONFIGURATION_get_value_filename (cfg, "PEER", "PRIVATE_KEY", &fn))
  422. return NULL;
  423. priv = GNUNET_CRYPTO_eddsa_key_create_from_file (fn);
  424. GNUNET_free (fn);
  425. return priv;
  426. }
  427. /**
  428. * Retrieve the identity of the host's peer.
  429. *
  430. * @param cfg configuration to use
  431. * @param dst pointer to where to write the peer identity
  432. * @return #GNUNET_OK on success, #GNUNET_SYSERR if the identity
  433. * could not be retrieved
  434. */
  435. int
  436. GNUNET_CRYPTO_get_peer_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
  437. struct GNUNET_PeerIdentity *dst)
  438. {
  439. struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
  440. if (NULL == (priv = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg)))
  441. {
  442. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  443. _ ("Could not load peer's private key\n"));
  444. return GNUNET_SYSERR;
  445. }
  446. GNUNET_CRYPTO_eddsa_key_get_public (priv, &dst->public_key);
  447. GNUNET_free (priv);
  448. return GNUNET_OK;
  449. }
  450. /**
  451. * Setup a key file for a peer given the name of the
  452. * configuration file (!). This function is used so that
  453. * at a later point code can be certain that reading a
  454. * key is fast (for example in time-dependent testcases).
  455. *
  456. * @param cfg_name name of the configuration file to use
  457. */
  458. void
  459. GNUNET_CRYPTO_eddsa_setup_key (const char *cfg_name)
  460. {
  461. struct GNUNET_CONFIGURATION_Handle *cfg;
  462. struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
  463. cfg = GNUNET_CONFIGURATION_create ();
  464. (void) GNUNET_CONFIGURATION_load (cfg, cfg_name);
  465. priv = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg);
  466. if (NULL != priv)
  467. GNUNET_free (priv);
  468. GNUNET_CONFIGURATION_destroy (cfg);
  469. }
  470. /* end of crypto_ecc_setup.c */