diff options
author | Özgür Kesim <oec@codeblau.de> | 2021-12-15 14:27:00 +0100 |
---|---|---|
committer | Özgür Kesim <oec-taler@kesim.org> | 2021-12-15 14:32:22 +0100 |
commit | f27fe484c60be69e762e038a84331a3a72111957 (patch) | |
tree | b635f3bfd5c72e0a705c78e7c7d3b62a9a976433 | |
parent | 10b9070e6db17b146784cd789cd7d4fa43b9e5ff (diff) | |
download | gnunet-f27fe484c60be69e762e038a84331a3a72111957.tar.gz gnunet-f27fe484c60be69e762e038a84331a3a72111957.zip |
GNUNET_CRYPTO_eddsa_key_from_file handles do_create correctly
The function GNUNET_CRYPTO_eddsa_key_from_file returns now
- GNUNET_OK on success,
- GNUNET_NO if do_create was set but we found an existing file,
- GNUNET_SYSERR on failure _or_ if the file didn't exist and
do_create was not set
Fixes #7106
-rw-r--r-- | src/util/crypto_ecc_setup.c | 11 | ||||
-rw-r--r-- | src/util/test_crypto_eddsa.c | 16 |
2 files changed, 17 insertions, 10 deletions
diff --git a/src/util/crypto_ecc_setup.c b/src/util/crypto_ecc_setup.c index f7cd8c6d9..21ce48eef 100644 --- a/src/util/crypto_ecc_setup.c +++ b/src/util/crypto_ecc_setup.c | |||
@@ -134,8 +134,10 @@ read_from_file (const char *filename, | |||
134 | * @param filename name of file to use to store the key | 134 | * @param filename name of file to use to store the key |
135 | * @param do_create should a file be created? | 135 | * @param do_create should a file be created? |
136 | * @param[out] pkey set to the private key from @a filename on success | 136 | * @param[out] pkey set to the private key from @a filename on success |
137 | * @return #GNUNET_OK on success, #GNUNET_NO if @a do_create was set but | 137 | * @return - #GNUNET_OK on success, |
138 | * we found an existing file, #GNUNET_SYSERR on failure | 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 | ||
139 | */ | 141 | */ |
140 | enum GNUNET_GenericReturnValue | 142 | enum GNUNET_GenericReturnValue |
141 | GNUNET_CRYPTO_eddsa_key_from_file (const char *filename, | 143 | GNUNET_CRYPTO_eddsa_key_from_file (const char *filename, |
@@ -152,6 +154,11 @@ GNUNET_CRYPTO_eddsa_key_from_file (const char *filename, | |||
152 | /* file existed, report that we didn't create it... */ | 154 | /* file existed, report that we didn't create it... */ |
153 | return (do_create) ? GNUNET_NO : GNUNET_OK; | 155 | return (do_create) ? GNUNET_NO : GNUNET_OK; |
154 | } | 156 | } |
157 | else if (! do_create) | ||
158 | { | ||
159 | return GNUNET_SYSERR; | ||
160 | } | ||
161 | |||
155 | GNUNET_CRYPTO_eddsa_key_create (pkey); | 162 | GNUNET_CRYPTO_eddsa_key_create (pkey); |
156 | ret = GNUNET_DISK_fn_write (filename, | 163 | ret = GNUNET_DISK_fn_write (filename, |
157 | pkey, | 164 | pkey, |
diff --git a/src/util/test_crypto_eddsa.c b/src/util/test_crypto_eddsa.c index 10d6a4e91..459619ff2 100644 --- a/src/util/test_crypto_eddsa.c +++ b/src/util/test_crypto_eddsa.c | |||
@@ -216,12 +216,16 @@ testCreateFromFile (void) | |||
216 | struct GNUNET_CRYPTO_EddsaPublicKey p1; | 216 | struct GNUNET_CRYPTO_EddsaPublicKey p1; |
217 | struct GNUNET_CRYPTO_EddsaPublicKey p2; | 217 | struct GNUNET_CRYPTO_EddsaPublicKey p2; |
218 | 218 | ||
219 | GNUNET_assert (0 <= | 219 | /* do_create == GNUNET_YES and non-existing file MUST return GNUNET_YES */ |
220 | GNUNET_assert (0 == unlink (KEYFILE) || ENOENT == errno); | ||
221 | GNUNET_assert (GNUNET_YES == | ||
220 | GNUNET_CRYPTO_eddsa_key_from_file (KEYFILE, | 222 | GNUNET_CRYPTO_eddsa_key_from_file (KEYFILE, |
221 | GNUNET_YES, | 223 | GNUNET_YES, |
222 | &key)); | 224 | &key)); |
223 | GNUNET_CRYPTO_eddsa_key_get_public (&key, | 225 | GNUNET_CRYPTO_eddsa_key_get_public (&key, |
224 | &p1); | 226 | &p1); |
227 | |||
228 | /* do_create == GNUNET_YES and _existing_ file MUST return GNUNET_NO */ | ||
225 | GNUNET_assert (GNUNET_NO == | 229 | GNUNET_assert (GNUNET_NO == |
226 | GNUNET_CRYPTO_eddsa_key_from_file (KEYFILE, | 230 | GNUNET_CRYPTO_eddsa_key_from_file (KEYFILE, |
227 | GNUNET_YES, | 231 | GNUNET_YES, |
@@ -231,16 +235,13 @@ testCreateFromFile (void) | |||
231 | GNUNET_assert (0 == | 235 | GNUNET_assert (0 == |
232 | GNUNET_memcmp (&p1, | 236 | GNUNET_memcmp (&p1, |
233 | &p2)); | 237 | &p2)); |
238 | |||
239 | /* do_create == GNUNET_NO and non-existing file MUST return GNUNET_SYSERR */ | ||
234 | GNUNET_assert (0 == unlink (KEYFILE)); | 240 | GNUNET_assert (0 == unlink (KEYFILE)); |
235 | GNUNET_assert (GNUNET_OK == | 241 | GNUNET_assert (GNUNET_SYSERR == |
236 | GNUNET_CRYPTO_eddsa_key_from_file (KEYFILE, | 242 | GNUNET_CRYPTO_eddsa_key_from_file (KEYFILE, |
237 | GNUNET_NO, | 243 | GNUNET_NO, |
238 | &key)); | 244 | &key)); |
239 | GNUNET_CRYPTO_eddsa_key_get_public (&key, | ||
240 | &p2); | ||
241 | GNUNET_assert (0 != | ||
242 | GNUNET_memcmp (&p1, | ||
243 | &p2)); | ||
244 | return GNUNET_OK; | 245 | return GNUNET_OK; |
245 | } | 246 | } |
246 | 247 | ||
@@ -299,7 +300,6 @@ main (int argc, char *argv[]) | |||
299 | failure_count++; | 300 | failure_count++; |
300 | if (GNUNET_OK != testCreateFromFile ()) | 301 | if (GNUNET_OK != testCreateFromFile ()) |
301 | failure_count++; | 302 | failure_count++; |
302 | GNUNET_assert (0 == unlink (KEYFILE)); | ||
303 | perf_keygen (); | 303 | perf_keygen (); |
304 | 304 | ||
305 | if (0 != failure_count) | 305 | if (0 != failure_count) |