crypto_ecc_setup.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2012, 2013, 2015, 2020 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. * Read file to @a buf. Fails if the file does not exist or
  46. * does not have precisely @a buf_size bytes.
  47. *
  48. * @param filename file to read
  49. * @param[out] buf where to write the file contents
  50. * @param buf_size number of bytes in @a buf
  51. * @return #GNUNET_OK on success
  52. */
  53. static enum GNUNET_GenericReturnValue
  54. read_from_file (const char *filename,
  55. void *buf,
  56. size_t buf_size)
  57. {
  58. int fd;
  59. struct stat sb;
  60. fd = open (filename,
  61. O_RDONLY);
  62. if (-1 == fd)
  63. {
  64. memset (buf,
  65. 0,
  66. buf_size);
  67. return GNUNET_SYSERR;
  68. }
  69. if (0 != fstat (fd,
  70. &sb))
  71. {
  72. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
  73. "stat",
  74. filename);
  75. GNUNET_assert (0 == close (fd));
  76. memset (buf,
  77. 0,
  78. buf_size);
  79. return GNUNET_SYSERR;
  80. }
  81. if (sb.st_size != buf_size)
  82. {
  83. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  84. "File `%s' has wrong size (%llu), expected %llu bytes\n",
  85. filename,
  86. (unsigned long long) sb.st_size,
  87. (unsigned long long) buf_size);
  88. GNUNET_assert (0 == close (fd));
  89. memset (buf,
  90. 0,
  91. buf_size);
  92. return GNUNET_SYSERR;
  93. }
  94. if (buf_size !=
  95. read (fd,
  96. buf,
  97. buf_size))
  98. {
  99. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
  100. "read",
  101. filename);
  102. GNUNET_assert (0 == close (fd));
  103. memset (buf,
  104. 0,
  105. buf_size);
  106. return GNUNET_SYSERR;
  107. }
  108. GNUNET_assert (0 == close (fd));
  109. return GNUNET_OK;
  110. }
  111. /**
  112. * @ingroup crypto
  113. * @brief Create a new private key by reading it from a file.
  114. *
  115. * If the files does not exist and @a do_create is set, creates a new key and
  116. * write it to the file.
  117. *
  118. * If the contents of the file are invalid, an error is returned.
  119. *
  120. * @param filename name of file to use to store the key
  121. * @param do_create should a file be created?
  122. * @param[out] pkey set to the private key from @a filename on success
  123. * @return #GNUNET_OK on success, #GNUNET_NO if @a do_create was set but
  124. * we found an existing file, #GNUNET_SYSERR on failure
  125. */
  126. enum GNUNET_GenericReturnValue
  127. GNUNET_CRYPTO_eddsa_key_from_file (const char *filename,
  128. int do_create,
  129. struct GNUNET_CRYPTO_EddsaPrivateKey *pkey)
  130. {
  131. enum GNUNET_GenericReturnValue ret;
  132. if (GNUNET_OK ==
  133. read_from_file (filename,
  134. pkey,
  135. sizeof (*pkey)))
  136. {
  137. /* file existed, report that we didn't create it... */
  138. return (do_create) ? GNUNET_NO : GNUNET_OK;
  139. }
  140. GNUNET_CRYPTO_eddsa_key_create (pkey);
  141. ret = GNUNET_DISK_fn_write (filename,
  142. pkey,
  143. sizeof (*pkey),
  144. GNUNET_DISK_PERM_USER_READ);
  145. if ( (GNUNET_OK == ret) ||
  146. (GNUNET_SYSERR == ret) )
  147. return ret;
  148. /* maybe another process succeeded in the meantime, try reading one more time */
  149. if (GNUNET_OK ==
  150. read_from_file (filename,
  151. pkey,
  152. sizeof (*pkey)))
  153. {
  154. /* file existed, report that *we* didn't create it... */
  155. return (do_create) ? GNUNET_NO : GNUNET_OK;
  156. }
  157. /* give up */
  158. return GNUNET_SYSERR;
  159. }
  160. /**
  161. * @ingroup crypto
  162. * @brief Create a new private key by reading it from a file.
  163. *
  164. * If the files does not exist and @a do_create is set, creates a new key and
  165. * write it to the file.
  166. *
  167. * If the contents of the file are invalid, an error is returned.
  168. *
  169. * @param filename name of file to use to store the key
  170. * @param do_create should a file be created?
  171. * @param[out] pkey set to the private key from @a filename on success
  172. * @return #GNUNET_OK on success, #GNUNET_NO if @a do_create was set but
  173. * we found an existing file, #GNUNET_SYSERR on failure
  174. */
  175. enum GNUNET_GenericReturnValue
  176. GNUNET_CRYPTO_ecdsa_key_from_file (const char *filename,
  177. int do_create,
  178. struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey)
  179. {
  180. if (GNUNET_OK ==
  181. read_from_file (filename,
  182. pkey,
  183. sizeof (*pkey)))
  184. {
  185. /* file existed, report that we didn't create it... */
  186. return (do_create) ? GNUNET_NO : GNUNET_OK;
  187. }
  188. GNUNET_CRYPTO_ecdsa_key_create (pkey);
  189. if (GNUNET_OK ==
  190. GNUNET_DISK_fn_write (filename,
  191. pkey,
  192. sizeof (*pkey),
  193. GNUNET_DISK_PERM_USER_READ))
  194. return GNUNET_OK;
  195. /* maybe another process succeeded in the meantime, try reading one more time */
  196. if (GNUNET_OK ==
  197. read_from_file (filename,
  198. pkey,
  199. sizeof (*pkey)))
  200. {
  201. /* file existed, report that *we* didn't create it... */
  202. return (do_create) ? GNUNET_NO : GNUNET_OK;
  203. }
  204. /* give up */
  205. return GNUNET_SYSERR;
  206. }
  207. /**
  208. * Create a new private key by reading our peer's key from
  209. * the file specified in the configuration.
  210. *
  211. * @param cfg the configuration to use
  212. * @return new private key, NULL on error (for example,
  213. * permission denied)
  214. */
  215. struct GNUNET_CRYPTO_EddsaPrivateKey *
  216. GNUNET_CRYPTO_eddsa_key_create_from_configuration (
  217. const struct GNUNET_CONFIGURATION_Handle *cfg)
  218. {
  219. struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
  220. char *fn;
  221. if (GNUNET_OK !=
  222. GNUNET_CONFIGURATION_get_value_filename (cfg,
  223. "PEER",
  224. "PRIVATE_KEY",
  225. &fn))
  226. return NULL;
  227. priv = GNUNET_new (struct GNUNET_CRYPTO_EddsaPrivateKey);
  228. GNUNET_CRYPTO_eddsa_key_from_file (fn,
  229. GNUNET_YES,
  230. priv);
  231. GNUNET_free (fn);
  232. return priv;
  233. }
  234. /**
  235. * Retrieve the identity of the host's peer.
  236. *
  237. * @param cfg configuration to use
  238. * @param dst pointer to where to write the peer identity
  239. * @return #GNUNET_OK on success, #GNUNET_SYSERR if the identity
  240. * could not be retrieved
  241. */
  242. enum GNUNET_GenericReturnValue
  243. GNUNET_CRYPTO_get_peer_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
  244. struct GNUNET_PeerIdentity *dst)
  245. {
  246. struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
  247. if (NULL == (priv = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg)))
  248. {
  249. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  250. _ ("Could not load peer's private key\n"));
  251. return GNUNET_SYSERR;
  252. }
  253. GNUNET_CRYPTO_eddsa_key_get_public (priv,
  254. &dst->public_key);
  255. GNUNET_free (priv);
  256. return GNUNET_OK;
  257. }
  258. /**
  259. * Setup a key file for a peer given the name of the
  260. * configuration file (!). This function is used so that
  261. * at a later point code can be certain that reading a
  262. * key is fast (for example in time-dependent testcases).
  263. *
  264. * @param cfg_name name of the configuration file to use
  265. */
  266. void
  267. GNUNET_CRYPTO_eddsa_setup_key (const char *cfg_name)
  268. {
  269. struct GNUNET_CONFIGURATION_Handle *cfg;
  270. struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
  271. cfg = GNUNET_CONFIGURATION_create ();
  272. (void) GNUNET_CONFIGURATION_load (cfg, cfg_name);
  273. priv = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg);
  274. if (NULL != priv)
  275. GNUNET_free (priv);
  276. GNUNET_CONFIGURATION_destroy (cfg);
  277. }
  278. /* end of crypto_ecc_setup.c */