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.c600
1 files changed, 300 insertions, 300 deletions
diff --git a/src/util/crypto_ecc_setup.c b/src/util/crypto_ecc_setup.c
index af42df613..0ce9f9d1e 100644
--- a/src/util/crypto_ecc_setup.c
+++ b/src/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)
99 { 107 {
100 fd = 108 if (EEXIST == errno)
101 GNUNET_DISK_file_open(filename, 109 {
102 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE | 110 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
103 GNUNET_DISK_OPEN_FAILIFEXISTS,
104 GNUNET_DISK_PERM_USER_READ |
105 GNUNET_DISK_PERM_USER_WRITE);
106 if (NULL == fd)
107 { 111 {
108 if (EEXIST == errno) 112 /* must exist but not be accessible, fail for good! */
109 { 113 if (0 != access (filename, R_OK))
110 if (GNUNET_YES != GNUNET_DISK_file_test(filename)) 114 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", filename);
111 { 115 else
112 /* must exist but not be accessible, fail for good! */ 116 GNUNET_break (0); /* what is going on!? */
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);
122 return NULL; 117 return NULL;
123 } 118 }
124 cnt = 0; 119 continue;
125 while (GNUNET_YES != 120 }
126 GNUNET_DISK_file_lock(fd, 121 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
127 0, 122 return NULL;
128 sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey), 123 }
129 GNUNET_YES)) 124 cnt = 0;
130 { 125 while (GNUNET_YES !=
131 short_wait(); 126 GNUNET_DISK_file_lock (fd,
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,
150 0, 127 0,
151 sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey))) 128 sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey),
152 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "fcntl", filename); 129 GNUNET_YES))
153 GNUNET_assert(GNUNET_YES == GNUNET_DISK_file_close(fd)); 130 {
154 return priv; 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 }
155 } 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,
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 { 161 {
162 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_ERROR, "open", filename); 162 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
163 return NULL; 163 return NULL;
164 } 164 }
165 cnt = 0; 165 cnt = 0;
166 while (1) 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))
167 { 173 {
174 if (0 == ++cnt % 60)
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);
168 if (GNUNET_YES != 193 if (GNUNET_YES !=
169 GNUNET_DISK_file_lock(fd, 194 GNUNET_DISK_file_unlock (fd,
170 0, 195 0,
171 sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey), 196 sizeof(
172 GNUNET_NO)) 197 struct GNUNET_CRYPTO_EddsaPrivateKey)))
173 { 198 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
174 if (0 == ++cnt % 60) 199 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
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 200
201 return NULL; 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;
231 } 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;
231 }
232 fs = sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey); 232 fs = sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey);
233 priv = GNUNET_malloc(fs); 233 priv = GNUNET_malloc (fs);
234 sret = GNUNET_DISK_file_read(fd, priv, fs); 234 sret = GNUNET_DISK_file_read (fd, priv, fs);
235 GNUNET_assert((sret >= 0) && (fs == (size_t)sret)); 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)
281 { 289 {
282 fd = 290 if (EEXIST == errno)
283 GNUNET_DISK_file_open(filename, 291 {
284 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE | 292 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
285 GNUNET_DISK_OPEN_FAILIFEXISTS,
286 GNUNET_DISK_PERM_USER_READ |
287 GNUNET_DISK_PERM_USER_WRITE);
288 if (NULL == fd)
289 { 293 {
290 if (EEXIST == errno) 294 /* must exist but not be accessible, fail for good! */
291 { 295 if (0 != access (filename, R_OK))
292 if (GNUNET_YES != GNUNET_DISK_file_test(filename)) 296 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", filename);
293 { 297 else
294 /* must exist but not be accessible, fail for good! */ 298 GNUNET_break (0); /* what is going on!? */
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);
304 return NULL; 299 return NULL;
305 } 300 }
306 cnt = 0; 301 continue;
307 while (GNUNET_YES != 302 }
308 GNUNET_DISK_file_lock(fd, 303 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
309 0, 304 return NULL;
310 sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey), 305 }
311 GNUNET_YES)) 306 cnt = 0;
312 { 307 while (GNUNET_YES !=
313 short_wait(); 308 GNUNET_DISK_file_lock (fd,
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,
332 0, 309 0,
333 sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey))) 310 sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey),
334 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "fcntl", filename); 311 GNUNET_YES))
335 GNUNET_assert(GNUNET_YES == GNUNET_DISK_file_close(fd)); 312 {
336 return priv; 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 }
337 } 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,
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 { 343 {
344 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_ERROR, "open", filename); 344 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
345 return NULL; 345 return NULL;
346 } 346 }
347 cnt = 0; 347 cnt = 0;
348 while (1) 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))
349 { 355 {
356 if (0 == ++cnt % 60)
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);
350 if (GNUNET_YES != 375 if (GNUNET_YES !=
351 GNUNET_DISK_file_lock(fd, 376 GNUNET_DISK_file_unlock (fd,
352 0, 377 0,
353 sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey), 378 sizeof(
354 GNUNET_NO)) 379 struct GNUNET_CRYPTO_EcdsaPrivateKey)))
355 { 380 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
356 if (0 == ++cnt % 60) 381 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
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 382
383 return NULL; 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;
413 } 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;
413 }
414 fs = sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey); 414 fs = sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey);
415 priv = GNUNET_malloc(fs); 415 priv = GNUNET_malloc (fs);
416 sret = GNUNET_DISK_file_read(fd, priv, fs); 416 sret = GNUNET_DISK_file_read (fd, priv, fs);
417 GNUNET_assert((sret >= 0) && (fs == (size_t)sret)); 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 */