diff options
Diffstat (limited to 'src/util/crypto_ecc_setup.c')
-rw-r--r-- | src/util/crypto_ecc_setup.c | 573 |
1 files changed, 233 insertions, 340 deletions
diff --git a/src/util/crypto_ecc_setup.c b/src/util/crypto_ecc_setup.c index b3d410b7a..5167a33fb 100644 --- a/src/util/crypto_ecc_setup.c +++ b/src/util/crypto_ecc_setup.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | Copyright (C) 2012, 2013, 2015 GNUnet e.V. | 3 | Copyright (C) 2012, 2013, 2015, 2020 GNUnet e.V. |
4 | 4 | ||
5 | GNUnet is free software: you can redistribute it and/or modify it | 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 | 6 | under the terms of the GNU Affero General Public License as published |
@@ -53,375 +53,261 @@ | |||
53 | 53 | ||
54 | 54 | ||
55 | /** | 55 | /** |
56 | * Wait for a short time (we're trying to lock a file or want | 56 | * Read file to @a buf. Fails if the file does not exist or |
57 | * to give another process a shot at finishing a disk write, etc.). | 57 | * does not have precisely @a buf_size bytes. |
58 | * Sleeps for 100ms (as that should be long enough for virtually all | 58 | * |
59 | * modern systems to context switch and allow another process to do | 59 | * @param filename file to read |
60 | * some 'real' work). | 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 | ||
61 | */ | 63 | */ |
62 | static void | 64 | static int |
63 | short_wait () | 65 | read_from_file (const char *filename, |
66 | void *buf, | ||
67 | size_t buf_size) | ||
64 | { | 68 | { |
65 | struct GNUNET_TIME_Relative timeout; | 69 | int fd; |
70 | struct stat sb; | ||
66 | 71 | ||
67 | timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100); | 72 | fd = open (filename, |
68 | (void) GNUNET_NETWORK_socket_select (NULL, NULL, NULL, timeout); | 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; | ||
69 | } | 122 | } |
70 | 123 | ||
71 | 124 | ||
72 | /** | 125 | /** |
73 | * Create a new private key by reading it from a file. If the | 126 | * Write contents of @a buf atomically to @a filename. |
74 | * files does not exist, create a new key and write it to the | 127 | * Fail if @a filename already exists or if not exactly |
75 | * file. Caller must free return value. Note that this function | 128 | * @a buf with @a buf_size bytes could be written to |
76 | * can not guarantee that another process might not be trying | 129 | * @a filename. |
77 | * the same operation on the same file at the same time. | ||
78 | * If the contents of the file | ||
79 | * are invalid the old file is deleted and a fresh key is | ||
80 | * created. | ||
81 | * | 130 | * |
82 | * @param filename name of file to use to store the key | 131 | * @param filename where to write |
83 | * @return new private key, NULL on error (for example, | 132 | * @param buf buffer to write |
84 | * permission denied) | 133 | * @param buf_size number of bytes in @a buf to write |
134 | * @return #GNUNET_OK on success, | ||
135 | * #GNUNET_NO if a file existed under @a filename | ||
136 | * #GNUNET_SYSERR on failure | ||
85 | */ | 137 | */ |
86 | struct GNUNET_CRYPTO_EddsaPrivateKey * | 138 | static int |
87 | GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename) | 139 | atomic_write_to_file (const char *filename, |
140 | const void *buf, | ||
141 | size_t buf_size) | ||
88 | { | 142 | { |
89 | struct GNUNET_CRYPTO_EddsaPrivateKey *priv; | 143 | char *tmpl; |
90 | struct GNUNET_DISK_FileHandle *fd; | 144 | int fd; |
91 | unsigned int cnt; | ||
92 | int ec; | ||
93 | uint64_t fs; | ||
94 | ssize_t sret; | ||
95 | 145 | ||
96 | if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file (filename)) | ||
97 | return NULL; | ||
98 | while (GNUNET_YES != GNUNET_DISK_file_test (filename)) | ||
99 | { | 146 | { |
100 | fd = | 147 | char *dname; |
101 | GNUNET_DISK_file_open (filename, | 148 | |
102 | GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE | 149 | dname = GNUNET_strdup (filename); |
103 | | GNUNET_DISK_OPEN_FAILIFEXISTS, | 150 | GNUNET_asprintf (&tmpl, |
104 | GNUNET_DISK_PERM_USER_READ | 151 | "%s/XXXXXX", |
105 | | GNUNET_DISK_PERM_USER_WRITE); | 152 | dirname (dname)); |
106 | if (NULL == fd) | 153 | GNUNET_free (dname); |
107 | { | ||
108 | if (EEXIST == errno) | ||
109 | { | ||
110 | if (GNUNET_YES != GNUNET_DISK_file_test (filename)) | ||
111 | { | ||
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); | ||
122 | return NULL; | ||
123 | } | ||
124 | cnt = 0; | ||
125 | while (GNUNET_YES != | ||
126 | GNUNET_DISK_file_lock (fd, | ||
127 | 0, | ||
128 | sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey), | ||
129 | GNUNET_YES)) | ||
130 | { | ||
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, | ||
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 | } | 154 | } |
156 | /* key file exists already, read it! */ | 155 | fd = mkstemp (tmpl); |
157 | fd = GNUNET_DISK_file_open (filename, | 156 | if (-1 == fd) |
158 | GNUNET_DISK_OPEN_READ, | ||
159 | GNUNET_DISK_PERM_NONE); | ||
160 | if (NULL == fd) | ||
161 | { | 157 | { |
162 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename); | 158 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, |
163 | return NULL; | 159 | "mkstemp", |
160 | tmpl); | ||
161 | GNUNET_free (tmpl); | ||
162 | return GNUNET_SYSERR; | ||
164 | } | 163 | } |
165 | cnt = 0; | 164 | if (0 != fchmod (fd, |
166 | while (1) | 165 | S_IRUSR)) |
167 | { | 166 | { |
168 | if (GNUNET_YES != | 167 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, |
169 | GNUNET_DISK_file_lock (fd, | 168 | "chmod", |
170 | 0, | 169 | tmpl); |
171 | sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey), | 170 | GNUNET_assert (0 == close (fd)); |
172 | GNUNET_NO)) | 171 | if (0 != unlink (tmpl)) |
173 | { | 172 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, |
174 | if (0 == ++cnt % 60) | 173 | "unlink", |
175 | { | 174 | tmpl); |
176 | ec = errno; | 175 | GNUNET_free (tmpl); |
177 | LOG (GNUNET_ERROR_TYPE_ERROR, | 176 | return GNUNET_SYSERR; |
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; | ||
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 | } | 177 | } |
232 | fs = sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey); | 178 | if (buf_size != |
233 | priv = GNUNET_malloc (fs); | 179 | write (fd, |
234 | sret = GNUNET_DISK_file_read (fd, priv, fs); | 180 | buf, |
235 | GNUNET_assert ((sret >= 0) && (fs == (size_t) sret)); | 181 | buf_size)) |
236 | if (GNUNET_YES != | ||
237 | GNUNET_DISK_file_unlock (fd, | ||
238 | 0, | ||
239 | sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey))) | ||
240 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename); | ||
241 | GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd)); | ||
242 | #if CRYPTO_BUG | ||
243 | if (GNUNET_OK != check_eddsa_key (priv)) | ||
244 | { | 182 | { |
245 | GNUNET_break (0); | 183 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, |
246 | GNUNET_free (priv); | 184 | "write", |
247 | return NULL; | 185 | tmpl); |
186 | GNUNET_assert (0 == close (fd)); | ||
187 | if (0 != unlink (tmpl)) | ||
188 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, | ||
189 | "unlink", | ||
190 | tmpl); | ||
191 | GNUNET_free (tmpl); | ||
192 | return GNUNET_SYSERR; | ||
248 | } | 193 | } |
249 | #endif | 194 | GNUNET_assert (0 == close (fd)); |
250 | return priv; | 195 | |
196 | if (0 != link (tmpl, | ||
197 | filename)) | ||
198 | { | ||
199 | if (0 != unlink (tmpl)) | ||
200 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, | ||
201 | "unlink", | ||
202 | tmpl); | ||
203 | GNUNET_free (tmpl); | ||
204 | return GNUNET_NO; | ||
205 | } | ||
206 | if (0 != unlink (tmpl)) | ||
207 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, | ||
208 | "unlink", | ||
209 | tmpl); | ||
210 | GNUNET_free (tmpl); | ||
211 | return GNUNET_OK; | ||
251 | } | 212 | } |
252 | 213 | ||
253 | 214 | ||
254 | /** | 215 | /** |
255 | * Create a new private key by reading it from a file. If the | 216 | * @ingroup crypto |
256 | * files does not exist, create a new key and write it to the | 217 | * @brief Create a new private key by reading it from a file. |
257 | * file. Caller must free return value. Note that this function | 218 | * |
258 | * can not guarantee that another process might not be trying | 219 | * If the files does not exist and @a do_create is set, creates a new key and |
259 | * the same operation on the same file at the same time. | 220 | * write it to the file. |
260 | * If the contents of the file | 221 | * |
261 | * are invalid the old file is deleted and a fresh key is | 222 | * If the contents of the file are invalid, an error is returned. |
262 | * created. | ||
263 | * | 223 | * |
264 | * @param filename name of file to use to store the key | 224 | * @param filename name of file to use to store the key |
265 | * @return new private key, NULL on error (for example, | 225 | * @param do_create should a file be created? |
266 | * permission denied) | 226 | * @param[out] pkey set to the private key from @a filename on success |
227 | * @return #GNUNET_OK on success, #GNUNET_NO if @a do_create was set but | ||
228 | * we found an existing file, #GNUNET_SYSERR on failure | ||
267 | */ | 229 | */ |
268 | struct GNUNET_CRYPTO_EcdsaPrivateKey * | 230 | int |
269 | GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename) | 231 | GNUNET_CRYPTO_eddsa_key_from_file (const char *filename, |
232 | int do_create, | ||
233 | struct GNUNET_CRYPTO_EddsaPrivateKey *pkey) | ||
270 | { | 234 | { |
271 | struct GNUNET_CRYPTO_EcdsaPrivateKey *priv; | 235 | int ret; |
272 | struct GNUNET_DISK_FileHandle *fd; | 236 | |
273 | unsigned int cnt; | 237 | if (GNUNET_OK == |
274 | int ec; | 238 | read_from_file (filename, |
275 | uint64_t fs; | 239 | pkey, |
276 | ssize_t sret; | 240 | sizeof (*pkey))) |
277 | |||
278 | if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file (filename)) | ||
279 | return NULL; | ||
280 | while (GNUNET_YES != GNUNET_DISK_file_test (filename)) | ||
281 | { | 241 | { |
282 | fd = | 242 | /* file existed, report that we didn't create it... */ |
283 | GNUNET_DISK_file_open (filename, | 243 | return (do_create) ? GNUNET_NO : GNUNET_OK; |
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 | { | ||
290 | if (EEXIST == errno) | ||
291 | { | ||
292 | if (GNUNET_YES != GNUNET_DISK_file_test (filename)) | ||
293 | { | ||
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); | ||
304 | return NULL; | ||
305 | } | ||
306 | cnt = 0; | ||
307 | while (GNUNET_YES != | ||
308 | GNUNET_DISK_file_lock (fd, | ||
309 | 0, | ||
310 | sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey), | ||
311 | GNUNET_YES)) | ||
312 | { | ||
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, | ||
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 | } | 244 | } |
338 | /* key file exists already, read it! */ | 245 | GNUNET_CRYPTO_eddsa_key_create (pkey); |
339 | fd = GNUNET_DISK_file_open (filename, | 246 | ret = atomic_write_to_file (filename, |
340 | GNUNET_DISK_OPEN_READ, | 247 | pkey, |
341 | GNUNET_DISK_PERM_NONE); | 248 | sizeof (*pkey)); |
342 | if (NULL == fd) | 249 | if ( (GNUNET_OK == ret) || |
250 | (GNUNET_SYSERR == ret) ) | ||
251 | return ret; | ||
252 | /* maybe another process succeeded in the meantime, try reading one more time */ | ||
253 | if (GNUNET_OK == | ||
254 | read_from_file (filename, | ||
255 | pkey, | ||
256 | sizeof (*pkey))) | ||
343 | { | 257 | { |
344 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename); | 258 | /* file existed, report that *we* didn't create it... */ |
345 | return NULL; | 259 | return (do_create) ? GNUNET_NO : GNUNET_OK; |
346 | } | 260 | } |
347 | cnt = 0; | 261 | /* give up */ |
348 | while (1) | 262 | return GNUNET_SYSERR; |
263 | } | ||
264 | |||
265 | |||
266 | /** | ||
267 | * @ingroup crypto | ||
268 | * @brief Create a new private key by reading it from a file. | ||
269 | * | ||
270 | * If the files does not exist and @a do_create is set, creates a new key and | ||
271 | * write it to the file. | ||
272 | * | ||
273 | * If the contents of the file are invalid, an error is returned. | ||
274 | * | ||
275 | * @param filename name of file to use to store the key | ||
276 | * @param do_create should a file be created? | ||
277 | * @param[out] pkey set to the private key from @a filename on success | ||
278 | * @return #GNUNET_OK on success, #GNUNET_NO if @a do_create was set but | ||
279 | * we found an existing file, #GNUNET_SYSERR on failure | ||
280 | */ | ||
281 | int | ||
282 | GNUNET_CRYPTO_ecdsa_key_from_file (const char *filename, | ||
283 | int do_create, | ||
284 | struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey) | ||
285 | { | ||
286 | if (GNUNET_OK == | ||
287 | read_from_file (filename, | ||
288 | pkey, | ||
289 | sizeof (*pkey))) | ||
349 | { | 290 | { |
350 | if (GNUNET_YES != | 291 | /* file existed, report that we didn't create it... */ |
351 | GNUNET_DISK_file_lock (fd, | 292 | return (do_create) ? GNUNET_NO : GNUNET_OK; |
352 | 0, | ||
353 | sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey), | ||
354 | GNUNET_NO)) | ||
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); | ||
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; | ||
413 | } | 293 | } |
414 | fs = sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey); | 294 | GNUNET_CRYPTO_ecdsa_key_create (pkey); |
415 | priv = GNUNET_malloc (fs); | 295 | if (GNUNET_OK == |
416 | sret = GNUNET_DISK_file_read (fd, priv, fs); | 296 | atomic_write_to_file (filename, |
417 | GNUNET_assert ((sret >= 0) && (fs == (size_t) sret)); | 297 | pkey, |
418 | if (GNUNET_YES != | 298 | sizeof (*pkey))) |
419 | GNUNET_DISK_file_unlock (fd, | 299 | return GNUNET_OK; |
420 | 0, | 300 | /* maybe another process succeeded in the meantime, try reading one more time */ |
421 | sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey))) | 301 | if (GNUNET_OK == |
422 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename); | 302 | read_from_file (filename, |
423 | GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd)); | 303 | pkey, |
424 | return priv; | 304 | sizeof (*pkey))) |
305 | { | ||
306 | /* file existed, report that *we* didn't create it... */ | ||
307 | return (do_create) ? GNUNET_NO : GNUNET_OK; | ||
308 | } | ||
309 | /* give up */ | ||
310 | return GNUNET_SYSERR; | ||
425 | } | 311 | } |
426 | 312 | ||
427 | 313 | ||
@@ -441,9 +327,15 @@ GNUNET_CRYPTO_eddsa_key_create_from_configuration ( | |||
441 | char *fn; | 327 | char *fn; |
442 | 328 | ||
443 | if (GNUNET_OK != | 329 | if (GNUNET_OK != |
444 | GNUNET_CONFIGURATION_get_value_filename (cfg, "PEER", "PRIVATE_KEY", &fn)) | 330 | GNUNET_CONFIGURATION_get_value_filename (cfg, |
331 | "PEER", | ||
332 | "PRIVATE_KEY", | ||
333 | &fn)) | ||
445 | return NULL; | 334 | return NULL; |
446 | priv = GNUNET_CRYPTO_eddsa_key_create_from_file (fn); | 335 | priv = GNUNET_new (struct GNUNET_CRYPTO_EddsaPrivateKey); |
336 | GNUNET_CRYPTO_eddsa_key_from_file (fn, | ||
337 | GNUNET_YES, | ||
338 | priv); | ||
447 | GNUNET_free (fn); | 339 | GNUNET_free (fn); |
448 | return priv; | 340 | return priv; |
449 | } | 341 | } |
@@ -469,7 +361,8 @@ GNUNET_CRYPTO_get_peer_identity (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
469 | _ ("Could not load peer's private key\n")); | 361 | _ ("Could not load peer's private key\n")); |
470 | return GNUNET_SYSERR; | 362 | return GNUNET_SYSERR; |
471 | } | 363 | } |
472 | GNUNET_CRYPTO_eddsa_key_get_public (priv, &dst->public_key); | 364 | GNUNET_CRYPTO_eddsa_key_get_public (priv, |
365 | &dst->public_key); | ||
473 | GNUNET_free (priv); | 366 | GNUNET_free (priv); |
474 | return GNUNET_OK; | 367 | return GNUNET_OK; |
475 | } | 368 | } |