aboutsummaryrefslogtreecommitdiff
path: root/src/util/crypto_ecc_setup.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/crypto_ecc_setup.c')
-rw-r--r--src/util/crypto_ecc_setup.c313
1 files changed, 0 insertions, 313 deletions
diff --git a/src/util/crypto_ecc_setup.c b/src/util/crypto_ecc_setup.c
deleted file mode 100644
index 21ce48eef..000000000
--- a/src/util/crypto_ecc_setup.c
+++ /dev/null
@@ -1,313 +0,0 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2012, 2013, 2015, 2020 GNUnet e.V.
4
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.
9
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.
14
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/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file util/crypto_ecc_setup.c
23 * @brief helper function for easy EdDSA key setup
24 * @author Christian Grothoff
25 */
26#include "platform.h"
27#include <gcrypt.h>
28#include "gnunet_util_lib.h"
29
30#define LOG(kind, ...) GNUNET_log_from (kind, "util-crypto-ecc", __VA_ARGS__)
31
32#define LOG_STRERROR(kind, syscall) \
33 GNUNET_log_from_strerror (kind, "util-crypto-ecc", syscall)
34
35#define LOG_STRERROR_FILE(kind, syscall, filename) \
36 GNUNET_log_from_strerror_file (kind, "util-crypto-ecc", syscall, filename)
37
38/**
39 * Log an error message at log-level 'level' that indicates
40 * a failure of the command 'cmd' with the message given
41 * by gcry_strerror(rc).
42 */
43#define LOG_GCRY(level, cmd, rc) \
44 do \
45 { \
46 LOG (level, \
47 _ ("`%s' failed at %s:%d with error: %s\n"), \
48 cmd, \
49 __FILE__, \
50 __LINE__, \
51 gcry_strerror (rc)); \
52 } while (0)
53
54
55/**
56 * Read file to @a buf. Fails if the file does not exist or
57 * does not have precisely @a buf_size bytes.
58 *
59 * @param filename file to read
60 * @param[out] buf where to write the file contents
61 * @param buf_size number of bytes in @a buf
62 * @return #GNUNET_OK on success
63 */
64static enum GNUNET_GenericReturnValue
65read_from_file (const char *filename,
66 void *buf,
67 size_t buf_size)
68{
69 int fd;
70 struct stat sb;
71
72 fd = open (filename,
73 O_RDONLY);
74 if (-1 == fd)
75 {
76 memset (buf,
77 0,
78 buf_size);
79 return GNUNET_SYSERR;
80 }
81 if (0 != fstat (fd,
82 &sb))
83 {
84 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
85 "stat",
86 filename);
87 GNUNET_assert (0 == close (fd));
88 memset (buf,
89 0,
90 buf_size);
91 return GNUNET_SYSERR;
92 }
93 if (sb.st_size != buf_size)
94 {
95 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
96 "File `%s' has wrong size (%llu), expected %llu bytes\n",
97 filename,
98 (unsigned long long) sb.st_size,
99 (unsigned long long) buf_size);
100 GNUNET_assert (0 == close (fd));
101 memset (buf,
102 0,
103 buf_size);
104 return GNUNET_SYSERR;
105 }
106 if (buf_size !=
107 read (fd,
108 buf,
109 buf_size))
110 {
111 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
112 "read",
113 filename);
114 GNUNET_assert (0 == close (fd));
115 memset (buf,
116 0,
117 buf_size);
118 return GNUNET_SYSERR;
119 }
120 GNUNET_assert (0 == close (fd));
121 return GNUNET_OK;
122}
123
124
125/**
126 * @ingroup crypto
127 * @brief Create a new private key by reading it from a file.
128 *
129 * If the files does not exist and @a do_create is set, creates a new key and
130 * write it to the file.
131 *
132 * If the contents of the file are invalid, an error is returned.
133 *
134 * @param filename name of file to use to store the key
135 * @param do_create should a file be created?
136 * @param[out] pkey set to the private key from @a filename on success
137 * @return - #GNUNET_OK on success,
138 * - #GNUNET_NO if @a do_create was set but we found an existing file,
139 * - #GNUNET_SYSERR on failure _or_ if the file didn't exist and @a
140 * do_create was not set
141 */
142enum GNUNET_GenericReturnValue
143GNUNET_CRYPTO_eddsa_key_from_file (const char *filename,
144 int do_create,
145 struct GNUNET_CRYPTO_EddsaPrivateKey *pkey)
146{
147 enum GNUNET_GenericReturnValue ret;
148
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 else if (! do_create)
158 {
159 return GNUNET_SYSERR;
160 }
161
162 GNUNET_CRYPTO_eddsa_key_create (pkey);
163 ret = GNUNET_DISK_fn_write (filename,
164 pkey,
165 sizeof (*pkey),
166 GNUNET_DISK_PERM_USER_READ);
167 if ( (GNUNET_OK == ret) ||
168 (GNUNET_SYSERR == ret) )
169 return ret;
170 /* maybe another process succeeded in the meantime, try reading one more time */
171 if (GNUNET_OK ==
172 read_from_file (filename,
173 pkey,
174 sizeof (*pkey)))
175 {
176 /* file existed, report that *we* didn't create it... */
177 return (do_create) ? GNUNET_NO : GNUNET_OK;
178 }
179 /* give up */
180 return GNUNET_SYSERR;
181}
182
183
184/**
185 * @ingroup crypto
186 * @brief Create a new private key by reading it from a file.
187 *
188 * If the files does not exist and @a do_create is set, creates a new key and
189 * write it to the file.
190 *
191 * If the contents of the file are invalid, an error is returned.
192 *
193 * @param filename name of file to use to store the key
194 * @param do_create should a file be created?
195 * @param[out] pkey set to the private key from @a filename on success
196 * @return #GNUNET_OK on success, #GNUNET_NO if @a do_create was set but
197 * we found an existing file, #GNUNET_SYSERR on failure
198 */
199enum GNUNET_GenericReturnValue
200GNUNET_CRYPTO_ecdsa_key_from_file (const char *filename,
201 int do_create,
202 struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey)
203{
204 if (GNUNET_OK ==
205 read_from_file (filename,
206 pkey,
207 sizeof (*pkey)))
208 {
209 /* file existed, report that we didn't create it... */
210 return (do_create) ? GNUNET_NO : GNUNET_OK;
211 }
212 GNUNET_CRYPTO_ecdsa_key_create (pkey);
213 if (GNUNET_OK ==
214 GNUNET_DISK_fn_write (filename,
215 pkey,
216 sizeof (*pkey),
217 GNUNET_DISK_PERM_USER_READ))
218 return GNUNET_OK;
219 /* maybe another process succeeded in the meantime, try reading one more time */
220 if (GNUNET_OK ==
221 read_from_file (filename,
222 pkey,
223 sizeof (*pkey)))
224 {
225 /* file existed, report that *we* didn't create it... */
226 return (do_create) ? GNUNET_NO : GNUNET_OK;
227 }
228 /* give up */
229 return GNUNET_SYSERR;
230}
231
232
233/**
234 * Create a new private key by reading our peer's key from
235 * the file specified in the configuration.
236 *
237 * @param cfg the configuration to use
238 * @return new private key, NULL on error (for example,
239 * permission denied)
240 */
241struct GNUNET_CRYPTO_EddsaPrivateKey *
242GNUNET_CRYPTO_eddsa_key_create_from_configuration (
243 const struct GNUNET_CONFIGURATION_Handle *cfg)
244{
245 struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
246 char *fn;
247
248 if (GNUNET_OK !=
249 GNUNET_CONFIGURATION_get_value_filename (cfg,
250 "PEER",
251 "PRIVATE_KEY",
252 &fn))
253 return NULL;
254 priv = GNUNET_new (struct GNUNET_CRYPTO_EddsaPrivateKey);
255 GNUNET_CRYPTO_eddsa_key_from_file (fn,
256 GNUNET_YES,
257 priv);
258 GNUNET_free (fn);
259 return priv;
260}
261
262
263/**
264 * Retrieve the identity of the host's peer.
265 *
266 * @param cfg configuration to use
267 * @param dst pointer to where to write the peer identity
268 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the identity
269 * could not be retrieved
270 */
271enum GNUNET_GenericReturnValue
272GNUNET_CRYPTO_get_peer_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
273 struct GNUNET_PeerIdentity *dst)
274{
275 struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
276
277 if (NULL == (priv = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg)))
278 {
279 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
280 _ ("Could not load peer's private key\n"));
281 return GNUNET_SYSERR;
282 }
283 GNUNET_CRYPTO_eddsa_key_get_public (priv,
284 &dst->public_key);
285 GNUNET_free (priv);
286 return GNUNET_OK;
287}
288
289
290/**
291 * Setup a key file for a peer given the name of the
292 * configuration file (!). This function is used so that
293 * at a later point code can be certain that reading a
294 * key is fast (for example in time-dependent testcases).
295 *
296 * @param cfg_name name of the configuration file to use
297 */
298void
299GNUNET_CRYPTO_eddsa_setup_key (const char *cfg_name)
300{
301 struct GNUNET_CONFIGURATION_Handle *cfg;
302 struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
303
304 cfg = GNUNET_CONFIGURATION_create ();
305 (void) GNUNET_CONFIGURATION_load (cfg, cfg_name);
306 priv = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg);
307 if (NULL != priv)
308 GNUNET_free (priv);
309 GNUNET_CONFIGURATION_destroy (cfg);
310}
311
312
313/* end of crypto_ecc_setup.c */