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.c608
1 files changed, 304 insertions, 304 deletions
diff --git a/src/util/crypto_ecc_setup.c b/src/util/crypto_ecc_setup.c
index c556d805d..af42df613 100644
--- a/src/util/crypto_ecc_setup.c
+++ b/src/util/crypto_ecc_setup.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file util/crypto_ecc_setup.c 22 * @file util/crypto_ecc_setup.c
@@ -27,13 +27,13 @@
27#include <gcrypt.h> 27#include <gcrypt.h>
28#include "gnunet_util_lib.h" 28#include "gnunet_util_lib.h"
29 29
30#define LOG(kind, ...) GNUNET_log_from (kind, "util-crypto-ecc", __VA_ARGS__) 30#define LOG(kind, ...) GNUNET_log_from(kind, "util-crypto-ecc", __VA_ARGS__)
31 31
32#define LOG_STRERROR(kind, syscall) \ 32#define LOG_STRERROR(kind, syscall) \
33 GNUNET_log_from_strerror (kind, "util-crypto-ecc", syscall) 33 GNUNET_log_from_strerror(kind, "util-crypto-ecc", syscall)
34 34
35#define LOG_STRERROR_FILE(kind, syscall, filename) \ 35#define LOG_STRERROR_FILE(kind, syscall, filename) \
36 GNUNET_log_from_strerror_file (kind, "util-crypto-ecc", syscall, filename) 36 GNUNET_log_from_strerror_file(kind, "util-crypto-ecc", syscall, filename)
37 37
38/** 38/**
39 * Log an error message at log-level 'level' that indicates 39 * Log an error message at log-level 'level' that indicates
@@ -42,14 +42,14 @@
42 */ 42 */
43#define LOG_GCRY(level, cmd, rc) \ 43#define LOG_GCRY(level, cmd, rc) \
44 do \ 44 do \
45 { \ 45 { \
46 LOG (level, \ 46 LOG(level, \
47 _ ("`%s' failed at %s:%d with error: %s\n"), \ 47 _("`%s' failed at %s:%d with error: %s\n"), \
48 cmd, \ 48 cmd, \
49 __FILE__, \ 49 __FILE__, \
50 __LINE__, \ 50 __LINE__, \
51 gcry_strerror (rc)); \ 51 gcry_strerror(rc)); \
52 } while (0) 52 } while (0)
53 53
54 54
55/** 55/**
@@ -60,12 +60,12 @@
60 * some 'real' work). 60 * some 'real' work).
61 */ 61 */
62static void 62static void
63short_wait () 63short_wait()
64{ 64{
65 struct GNUNET_TIME_Relative timeout; 65 struct GNUNET_TIME_Relative timeout;
66 66
67 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100); 67 timeout = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100);
68 (void) GNUNET_NETWORK_socket_select (NULL, NULL, NULL, timeout); 68 (void)GNUNET_NETWORK_socket_select(NULL, NULL, NULL, timeout);
69} 69}
70 70
71 71
@@ -84,7 +84,7 @@ short_wait ()
84 * permission denied) 84 * permission denied)
85 */ 85 */
86struct GNUNET_CRYPTO_EddsaPrivateKey * 86struct GNUNET_CRYPTO_EddsaPrivateKey *
87GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename) 87GNUNET_CRYPTO_eddsa_key_create_from_file(const char *filename)
88{ 88{
89 struct GNUNET_CRYPTO_EddsaPrivateKey *priv; 89 struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
90 struct GNUNET_DISK_FileHandle *fd; 90 struct GNUNET_DISK_FileHandle *fd;
@@ -93,159 +93,159 @@ GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename)
93 uint64_t fs; 93 uint64_t fs;
94 ssize_t sret; 94 ssize_t sret;
95 95
96 if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file (filename)) 96 if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file(filename))
97 return NULL; 97 return NULL;
98 while (GNUNET_YES != GNUNET_DISK_file_test (filename)) 98 while (GNUNET_YES != GNUNET_DISK_file_test(filename))
99 {
100 fd =
101 GNUNET_DISK_file_open (filename,
102 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE |
103 GNUNET_DISK_OPEN_FAILIFEXISTS,
104 GNUNET_DISK_PERM_USER_READ |
105 GNUNET_DISK_PERM_USER_WRITE);
106 if (NULL == fd)
107 { 99 {
108 if (EEXIST == errno) 100 fd =
109 { 101 GNUNET_DISK_file_open(filename,
110 if (GNUNET_YES != GNUNET_DISK_file_test (filename)) 102 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE |
103 GNUNET_DISK_OPEN_FAILIFEXISTS,
104 GNUNET_DISK_PERM_USER_READ |
105 GNUNET_DISK_PERM_USER_WRITE);
106 if (NULL == fd)
111 { 107 {
112 /* must exist but not be accessible, fail for good! */ 108 if (EEXIST == errno)
113 if (0 != access (filename, R_OK)) 109 {
114 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", filename); 110 if (GNUNET_YES != GNUNET_DISK_file_test(filename))
115 else 111 {
116 GNUNET_break (0); /* what is going on!? */ 112 /* must exist but not be accessible, fail for good! */
113 if (0 != access(filename, R_OK))
114 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_ERROR, "access", filename);
115 else
116 GNUNET_break(0); /* what is going on!? */
117 return NULL;
118 }
119 continue;
120 }
121 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_ERROR, "open", filename);
117 return NULL; 122 return NULL;
118 } 123 }
119 continue; 124 cnt = 0;
120 } 125 while (GNUNET_YES !=
121 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename); 126 GNUNET_DISK_file_lock(fd,
122 return NULL; 127 0,
123 } 128 sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey),
124 cnt = 0; 129 GNUNET_YES))
125 while (GNUNET_YES != 130 {
126 GNUNET_DISK_file_lock (fd, 131 short_wait();
132 if (0 == ++cnt % 10)
133 {
134 ec = errno;
135 LOG(GNUNET_ERROR_TYPE_ERROR,
136 _("Could not acquire lock on file `%s': %s...\n"),
137 filename,
138 strerror(ec));
139 }
140 }
141 LOG(GNUNET_ERROR_TYPE_INFO,
142 _("Creating a new private key. This may take a while.\n"));
143 priv = GNUNET_CRYPTO_eddsa_key_create();
144 GNUNET_assert(NULL != priv);
145 GNUNET_assert(sizeof(*priv) ==
146 GNUNET_DISK_file_write(fd, priv, sizeof(*priv)));
147 GNUNET_DISK_file_sync(fd);
148 if (GNUNET_YES !=
149 GNUNET_DISK_file_unlock(fd,
127 0, 150 0,
128 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey), 151 sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey)))
129 GNUNET_YES)) 152 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
130 { 153 GNUNET_assert(GNUNET_YES == GNUNET_DISK_file_close(fd));
131 short_wait (); 154 return priv;
132 if (0 == ++cnt % 10)
133 {
134 ec = errno;
135 LOG (GNUNET_ERROR_TYPE_ERROR,
136 _ ("Could not acquire lock on file `%s': %s...\n"),
137 filename,
138 strerror (ec));
139 }
140 } 155 }
141 LOG (GNUNET_ERROR_TYPE_INFO,
142 _ ("Creating a new private key. This may take a while.\n"));
143 priv = GNUNET_CRYPTO_eddsa_key_create ();
144 GNUNET_assert (NULL != priv);
145 GNUNET_assert (sizeof (*priv) ==
146 GNUNET_DISK_file_write (fd, priv, sizeof (*priv)));
147 GNUNET_DISK_file_sync (fd);
148 if (GNUNET_YES !=
149 GNUNET_DISK_file_unlock (fd,
150 0,
151 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey)))
152 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
153 GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
154 return priv;
155 }
156 /* key file exists already, read it! */ 156 /* key file exists already, read it! */
157 fd = GNUNET_DISK_file_open (filename, 157 fd = GNUNET_DISK_file_open(filename,
158 GNUNET_DISK_OPEN_READ, 158 GNUNET_DISK_OPEN_READ,
159 GNUNET_DISK_PERM_NONE); 159 GNUNET_DISK_PERM_NONE);
160 if (NULL == fd) 160 if (NULL == fd)
161 {
162 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
163 return NULL;
164 }
165 cnt = 0;
166 while (1)
167 {
168 if (GNUNET_YES !=
169 GNUNET_DISK_file_lock (fd,
170 0,
171 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey),
172 GNUNET_NO))
173 { 161 {
174 if (0 == ++cnt % 60) 162 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_ERROR, "open", filename);
175 {
176 ec = errno;
177 LOG (GNUNET_ERROR_TYPE_ERROR,
178 _ ("Could not acquire lock on file `%s': %s...\n"),
179 filename,
180 strerror (ec));
181 LOG (
182 GNUNET_ERROR_TYPE_ERROR,
183 _ (
184 "This may be ok if someone is currently generating a private key.\n"));
185 }
186 short_wait ();
187 continue;
188 }
189 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
190 {
191 /* eh, what!? File we opened is now gone!? */
192 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
193 if (GNUNET_YES !=
194 GNUNET_DISK_file_unlock (fd,
195 0,
196 sizeof (
197 struct GNUNET_CRYPTO_EddsaPrivateKey)))
198 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
199 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
200
201 return NULL; 163 return NULL;
202 } 164 }
203 if (GNUNET_OK != 165 cnt = 0;
204 GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES)) 166 while (1)
205 fs = 0;
206 if (fs < sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey))
207 { 167 {
208 /* maybe we got the read lock before the key generating
209 * process had a chance to get the write lock; give it up! */
210 if (GNUNET_YES != 168 if (GNUNET_YES !=
211 GNUNET_DISK_file_unlock (fd, 169 GNUNET_DISK_file_lock(fd,
212 0, 170 0,
213 sizeof ( 171 sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey),
214 struct GNUNET_CRYPTO_EddsaPrivateKey))) 172 GNUNET_NO))
215 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename); 173 {
216 if (0 == ++cnt % 10) 174 if (0 == ++cnt % 60)
217 { 175 {
218 LOG (GNUNET_ERROR_TYPE_ERROR, 176 ec = errno;
219 _ ( 177 LOG(GNUNET_ERROR_TYPE_ERROR,
220 "When trying to read key file `%s' I found %u bytes but I need at least %u.\n"), 178 _("Could not acquire lock on file `%s': %s...\n"),
221 filename, 179 filename,
222 (unsigned int) fs, 180 strerror(ec));
223 (unsigned int) sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey)); 181 LOG(
224 LOG (GNUNET_ERROR_TYPE_ERROR, 182 GNUNET_ERROR_TYPE_ERROR,
225 _ ("This may be ok if someone is currently generating a key.\n")); 183 _(
226 } 184 "This may be ok if someone is currently generating a private key.\n"));
227 short_wait (); /* wait a bit longer! */ 185 }
228 continue; 186 short_wait();
187 continue;
188 }
189 if (GNUNET_YES != GNUNET_DISK_file_test(filename))
190 {
191 /* eh, what!? File we opened is now gone!? */
192 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "stat", filename);
193 if (GNUNET_YES !=
194 GNUNET_DISK_file_unlock(fd,
195 0,
196 sizeof(
197 struct GNUNET_CRYPTO_EddsaPrivateKey)))
198 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
199 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fd));
200
201 return NULL;
202 }
203 if (GNUNET_OK !=
204 GNUNET_DISK_file_size(filename, &fs, GNUNET_YES, GNUNET_YES))
205 fs = 0;
206 if (fs < sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey))
207 {
208 /* maybe we got the read lock before the key generating
209 * process had a chance to get the write lock; give it up! */
210 if (GNUNET_YES !=
211 GNUNET_DISK_file_unlock(fd,
212 0,
213 sizeof(
214 struct GNUNET_CRYPTO_EddsaPrivateKey)))
215 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
216 if (0 == ++cnt % 10)
217 {
218 LOG(GNUNET_ERROR_TYPE_ERROR,
219 _(
220 "When trying to read key file `%s' I found %u bytes but I need at least %u.\n"),
221 filename,
222 (unsigned int)fs,
223 (unsigned int)sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey));
224 LOG(GNUNET_ERROR_TYPE_ERROR,
225 _("This may be ok if someone is currently generating a key.\n"));
226 }
227 short_wait(); /* wait a bit longer! */
228 continue;
229 }
230 break;
229 } 231 }
230 break; 232 fs = sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey);
231 } 233 priv = GNUNET_malloc(fs);
232 fs = sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey); 234 sret = GNUNET_DISK_file_read(fd, priv, fs);
233 priv = GNUNET_malloc (fs); 235 GNUNET_assert((sret >= 0) && (fs == (size_t)sret));
234 sret = GNUNET_DISK_file_read (fd, priv, fs);
235 GNUNET_assert ((sret >= 0) && (fs == (size_t) sret));
236 if (GNUNET_YES != 236 if (GNUNET_YES !=
237 GNUNET_DISK_file_unlock (fd, 237 GNUNET_DISK_file_unlock(fd,
238 0, 238 0,
239 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey))) 239 sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey)))
240 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename); 240 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
241 GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd)); 241 GNUNET_assert(GNUNET_YES == GNUNET_DISK_file_close(fd));
242#if CRYPTO_BUG 242#if CRYPTO_BUG
243 if (GNUNET_OK != check_eddsa_key (priv)) 243 if (GNUNET_OK != check_eddsa_key(priv))
244 { 244 {
245 GNUNET_break (0); 245 GNUNET_break(0);
246 GNUNET_free (priv); 246 GNUNET_free(priv);
247 return NULL; 247 return NULL;
248 } 248 }
249#endif 249#endif
250 return priv; 250 return priv;
251} 251}
@@ -266,7 +266,7 @@ GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename)
266 * permission denied) 266 * permission denied)
267 */ 267 */
268struct GNUNET_CRYPTO_EcdsaPrivateKey * 268struct GNUNET_CRYPTO_EcdsaPrivateKey *
269GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename) 269GNUNET_CRYPTO_ecdsa_key_create_from_file(const char *filename)
270{ 270{
271 struct GNUNET_CRYPTO_EcdsaPrivateKey *priv; 271 struct GNUNET_CRYPTO_EcdsaPrivateKey *priv;
272 struct GNUNET_DISK_FileHandle *fd; 272 struct GNUNET_DISK_FileHandle *fd;
@@ -275,152 +275,152 @@ GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename)
275 uint64_t fs; 275 uint64_t fs;
276 ssize_t sret; 276 ssize_t sret;
277 277
278 if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file (filename)) 278 if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file(filename))
279 return NULL; 279 return NULL;
280 while (GNUNET_YES != GNUNET_DISK_file_test (filename)) 280 while (GNUNET_YES != GNUNET_DISK_file_test(filename))
281 {
282 fd =
283 GNUNET_DISK_file_open (filename,
284 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE |
285 GNUNET_DISK_OPEN_FAILIFEXISTS,
286 GNUNET_DISK_PERM_USER_READ |
287 GNUNET_DISK_PERM_USER_WRITE);
288 if (NULL == fd)
289 { 281 {
290 if (EEXIST == errno) 282 fd =
291 { 283 GNUNET_DISK_file_open(filename,
292 if (GNUNET_YES != GNUNET_DISK_file_test (filename)) 284 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE |
285 GNUNET_DISK_OPEN_FAILIFEXISTS,
286 GNUNET_DISK_PERM_USER_READ |
287 GNUNET_DISK_PERM_USER_WRITE);
288 if (NULL == fd)
293 { 289 {
294 /* must exist but not be accessible, fail for good! */ 290 if (EEXIST == errno)
295 if (0 != access (filename, R_OK)) 291 {
296 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", filename); 292 if (GNUNET_YES != GNUNET_DISK_file_test(filename))
297 else 293 {
298 GNUNET_break (0); /* what is going on!? */ 294 /* must exist but not be accessible, fail for good! */
295 if (0 != access(filename, R_OK))
296 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_ERROR, "access", filename);
297 else
298 GNUNET_break(0); /* what is going on!? */
299 return NULL;
300 }
301 continue;
302 }
303 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_ERROR, "open", filename);
299 return NULL; 304 return NULL;
300 } 305 }
301 continue; 306 cnt = 0;
302 } 307 while (GNUNET_YES !=
303 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename); 308 GNUNET_DISK_file_lock(fd,
304 return NULL; 309 0,
305 } 310 sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey),
306 cnt = 0; 311 GNUNET_YES))
307 while (GNUNET_YES != 312 {
308 GNUNET_DISK_file_lock (fd, 313 short_wait();
314 if (0 == ++cnt % 10)
315 {
316 ec = errno;
317 LOG(GNUNET_ERROR_TYPE_ERROR,
318 _("Could not acquire lock on file `%s': %s...\n"),
319 filename,
320 strerror(ec));
321 }
322 }
323 LOG(GNUNET_ERROR_TYPE_INFO,
324 _("Creating a new private key. This may take a while.\n"));
325 priv = GNUNET_CRYPTO_ecdsa_key_create();
326 GNUNET_assert(NULL != priv);
327 GNUNET_assert(sizeof(*priv) ==
328 GNUNET_DISK_file_write(fd, priv, sizeof(*priv)));
329 GNUNET_DISK_file_sync(fd);
330 if (GNUNET_YES !=
331 GNUNET_DISK_file_unlock(fd,
309 0, 332 0,
310 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey), 333 sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey)))
311 GNUNET_YES)) 334 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
312 { 335 GNUNET_assert(GNUNET_YES == GNUNET_DISK_file_close(fd));
313 short_wait (); 336 return priv;
314 if (0 == ++cnt % 10)
315 {
316 ec = errno;
317 LOG (GNUNET_ERROR_TYPE_ERROR,
318 _ ("Could not acquire lock on file `%s': %s...\n"),
319 filename,
320 strerror (ec));
321 }
322 } 337 }
323 LOG (GNUNET_ERROR_TYPE_INFO,
324 _ ("Creating a new private key. This may take a while.\n"));
325 priv = GNUNET_CRYPTO_ecdsa_key_create ();
326 GNUNET_assert (NULL != priv);
327 GNUNET_assert (sizeof (*priv) ==
328 GNUNET_DISK_file_write (fd, priv, sizeof (*priv)));
329 GNUNET_DISK_file_sync (fd);
330 if (GNUNET_YES !=
331 GNUNET_DISK_file_unlock (fd,
332 0,
333 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
334 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
335 GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
336 return priv;
337 }
338 /* key file exists already, read it! */ 338 /* key file exists already, read it! */
339 fd = GNUNET_DISK_file_open (filename, 339 fd = GNUNET_DISK_file_open(filename,
340 GNUNET_DISK_OPEN_READ, 340 GNUNET_DISK_OPEN_READ,
341 GNUNET_DISK_PERM_NONE); 341 GNUNET_DISK_PERM_NONE);
342 if (NULL == fd) 342 if (NULL == fd)
343 {
344 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
345 return NULL;
346 }
347 cnt = 0;
348 while (1)
349 {
350 if (GNUNET_YES !=
351 GNUNET_DISK_file_lock (fd,
352 0,
353 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey),
354 GNUNET_NO))
355 { 343 {
356 if (0 == ++cnt % 60) 344 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_ERROR, "open", filename);
357 {
358 ec = errno;
359 LOG (GNUNET_ERROR_TYPE_ERROR,
360 _ ("Could not acquire lock on file `%s': %s...\n"),
361 filename,
362 strerror (ec));
363 LOG (
364 GNUNET_ERROR_TYPE_ERROR,
365 _ (
366 "This may be ok if someone is currently generating a private key.\n"));
367 }
368 short_wait ();
369 continue;
370 }
371 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
372 {
373 /* eh, what!? File we opened is now gone!? */
374 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
375 if (GNUNET_YES !=
376 GNUNET_DISK_file_unlock (fd,
377 0,
378 sizeof (
379 struct GNUNET_CRYPTO_EcdsaPrivateKey)))
380 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
381 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
382
383 return NULL; 345 return NULL;
384 } 346 }
385 if (GNUNET_OK != 347 cnt = 0;
386 GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES)) 348 while (1)
387 fs = 0;
388 if (fs < sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))
389 { 349 {
390 /* maybe we got the read lock before the key generating
391 * process had a chance to get the write lock; give it up! */
392 if (GNUNET_YES != 350 if (GNUNET_YES !=
393 GNUNET_DISK_file_unlock (fd, 351 GNUNET_DISK_file_lock(fd,
394 0, 352 0,
395 sizeof ( 353 sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey),
396 struct GNUNET_CRYPTO_EcdsaPrivateKey))) 354 GNUNET_NO))
397 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename); 355 {
398 if (0 == ++cnt % 10) 356 if (0 == ++cnt % 60)
399 { 357 {
400 LOG (GNUNET_ERROR_TYPE_ERROR, 358 ec = errno;
401 _ ( 359 LOG(GNUNET_ERROR_TYPE_ERROR,
402 "When trying to read key file `%s' I found %u bytes but I need at least %u.\n"), 360 _("Could not acquire lock on file `%s': %s...\n"),
403 filename, 361 filename,
404 (unsigned int) fs, 362 strerror(ec));
405 (unsigned int) sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)); 363 LOG(
406 LOG (GNUNET_ERROR_TYPE_ERROR, 364 GNUNET_ERROR_TYPE_ERROR,
407 _ ("This may be ok if someone is currently generating a key.\n")); 365 _(
408 } 366 "This may be ok if someone is currently generating a private key.\n"));
409 short_wait (); /* wait a bit longer! */ 367 }
410 continue; 368 short_wait();
369 continue;
370 }
371 if (GNUNET_YES != GNUNET_DISK_file_test(filename))
372 {
373 /* eh, what!? File we opened is now gone!? */
374 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "stat", filename);
375 if (GNUNET_YES !=
376 GNUNET_DISK_file_unlock(fd,
377 0,
378 sizeof(
379 struct GNUNET_CRYPTO_EcdsaPrivateKey)))
380 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
381 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fd));
382
383 return NULL;
384 }
385 if (GNUNET_OK !=
386 GNUNET_DISK_file_size(filename, &fs, GNUNET_YES, GNUNET_YES))
387 fs = 0;
388 if (fs < sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey))
389 {
390 /* maybe we got the read lock before the key generating
391 * process had a chance to get the write lock; give it up! */
392 if (GNUNET_YES !=
393 GNUNET_DISK_file_unlock(fd,
394 0,
395 sizeof(
396 struct GNUNET_CRYPTO_EcdsaPrivateKey)))
397 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
398 if (0 == ++cnt % 10)
399 {
400 LOG(GNUNET_ERROR_TYPE_ERROR,
401 _(
402 "When trying to read key file `%s' I found %u bytes but I need at least %u.\n"),
403 filename,
404 (unsigned int)fs,
405 (unsigned int)sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey));
406 LOG(GNUNET_ERROR_TYPE_ERROR,
407 _("This may be ok if someone is currently generating a key.\n"));
408 }
409 short_wait(); /* wait a bit longer! */
410 continue;
411 }
412 break;
411 } 413 }
412 break; 414 fs = sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey);
413 } 415 priv = GNUNET_malloc(fs);
414 fs = sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey); 416 sret = GNUNET_DISK_file_read(fd, priv, fs);
415 priv = GNUNET_malloc (fs); 417 GNUNET_assert((sret >= 0) && (fs == (size_t)sret));
416 sret = GNUNET_DISK_file_read (fd, priv, fs);
417 GNUNET_assert ((sret >= 0) && (fs == (size_t) sret));
418 if (GNUNET_YES != 418 if (GNUNET_YES !=
419 GNUNET_DISK_file_unlock (fd, 419 GNUNET_DISK_file_unlock(fd,
420 0, 420 0,
421 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) 421 sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey)))
422 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename); 422 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
423 GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd)); 423 GNUNET_assert(GNUNET_YES == GNUNET_DISK_file_close(fd));
424 return priv; 424 return priv;
425} 425}
426 426
@@ -434,17 +434,17 @@ GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename)
434 * permission denied) 434 * permission denied)
435 */ 435 */
436struct GNUNET_CRYPTO_EddsaPrivateKey * 436struct GNUNET_CRYPTO_EddsaPrivateKey *
437GNUNET_CRYPTO_eddsa_key_create_from_configuration ( 437GNUNET_CRYPTO_eddsa_key_create_from_configuration(
438 const struct GNUNET_CONFIGURATION_Handle *cfg) 438 const struct GNUNET_CONFIGURATION_Handle *cfg)
439{ 439{
440 struct GNUNET_CRYPTO_EddsaPrivateKey *priv; 440 struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
441 char *fn; 441 char *fn;
442 442
443 if (GNUNET_OK != 443 if (GNUNET_OK !=
444 GNUNET_CONFIGURATION_get_value_filename (cfg, "PEER", "PRIVATE_KEY", &fn)) 444 GNUNET_CONFIGURATION_get_value_filename(cfg, "PEER", "PRIVATE_KEY", &fn))
445 return NULL; 445 return NULL;
446 priv = GNUNET_CRYPTO_eddsa_key_create_from_file (fn); 446 priv = GNUNET_CRYPTO_eddsa_key_create_from_file(fn);
447 GNUNET_free (fn); 447 GNUNET_free(fn);
448 return priv; 448 return priv;
449} 449}
450 450
@@ -458,19 +458,19 @@ GNUNET_CRYPTO_eddsa_key_create_from_configuration (
458 * could not be retrieved 458 * could not be retrieved
459 */ 459 */
460int 460int
461GNUNET_CRYPTO_get_peer_identity (const struct GNUNET_CONFIGURATION_Handle *cfg, 461GNUNET_CRYPTO_get_peer_identity(const struct GNUNET_CONFIGURATION_Handle *cfg,
462 struct GNUNET_PeerIdentity *dst) 462 struct GNUNET_PeerIdentity *dst)
463{ 463{
464 struct GNUNET_CRYPTO_EddsaPrivateKey *priv; 464 struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
465 465
466 if (NULL == (priv = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg))) 466 if (NULL == (priv = GNUNET_CRYPTO_eddsa_key_create_from_configuration(cfg)))
467 { 467 {
468 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 468 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
469 _ ("Could not load peer's private key\n")); 469 _("Could not load peer's private key\n"));
470 return GNUNET_SYSERR; 470 return GNUNET_SYSERR;
471 } 471 }
472 GNUNET_CRYPTO_eddsa_key_get_public (priv, &dst->public_key); 472 GNUNET_CRYPTO_eddsa_key_get_public(priv, &dst->public_key);
473 GNUNET_free (priv); 473 GNUNET_free(priv);
474 return GNUNET_OK; 474 return GNUNET_OK;
475} 475}
476 476
@@ -484,17 +484,17 @@ GNUNET_CRYPTO_get_peer_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
484 * @param cfg_name name of the configuration file to use 484 * @param cfg_name name of the configuration file to use
485 */ 485 */
486void 486void
487GNUNET_CRYPTO_eddsa_setup_key (const char *cfg_name) 487GNUNET_CRYPTO_eddsa_setup_key(const char *cfg_name)
488{ 488{
489 struct GNUNET_CONFIGURATION_Handle *cfg; 489 struct GNUNET_CONFIGURATION_Handle *cfg;
490 struct GNUNET_CRYPTO_EddsaPrivateKey *priv; 490 struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
491 491
492 cfg = GNUNET_CONFIGURATION_create (); 492 cfg = GNUNET_CONFIGURATION_create();
493 (void) GNUNET_CONFIGURATION_load (cfg, cfg_name); 493 (void)GNUNET_CONFIGURATION_load(cfg, cfg_name);
494 priv = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg); 494 priv = GNUNET_CRYPTO_eddsa_key_create_from_configuration(cfg);
495 if (NULL != priv) 495 if (NULL != priv)
496 GNUNET_free (priv); 496 GNUNET_free(priv);
497 GNUNET_CONFIGURATION_destroy (cfg); 497 GNUNET_CONFIGURATION_destroy(cfg);
498} 498}
499 499
500/* end of crypto_ecc_setup.c */ 500/* end of crypto_ecc_setup.c */