diff options
author | Sree Harsha Totakura <totakura@in.tum.de> | 2012-05-29 12:42:32 +0000 |
---|---|---|
committer | Sree Harsha Totakura <totakura@in.tum.de> | 2012-05-29 12:42:32 +0000 |
commit | c099f5faa8fcd5f7fff114857212d806c009a660 (patch) | |
tree | 013d06dbdd2c6c6ed3da53d10e6b574c570804b6 /src | |
parent | 699f6c4038829f8dc621ef9aacba27fb84037155 (diff) | |
download | gnunet-c099f5faa8fcd5f7fff114857212d806c009a660.tar.gz gnunet-c099f5faa8fcd5f7fff114857212d806c009a660.zip |
-modified hostkeys management
Diffstat (limited to 'src')
-rw-r--r-- | src/include/gnunet_testing_lib-new.h | 29 | ||||
-rw-r--r-- | src/testing/testing_new.c | 131 |
2 files changed, 113 insertions, 47 deletions
diff --git a/src/include/gnunet_testing_lib-new.h b/src/include/gnunet_testing_lib-new.h index 1fb3c1a02..9b5f4c2f5 100644 --- a/src/include/gnunet_testing_lib-new.h +++ b/src/include/gnunet_testing_lib-new.h | |||
@@ -87,6 +87,28 @@ GNUNET_TESTING_system_destroy (struct GNUNET_TESTING_System *system, | |||
87 | 87 | ||
88 | 88 | ||
89 | /** | 89 | /** |
90 | * Testing includes a number of pre-created hostkeys for faster peer | ||
91 | * startup. This function loads such keys into memory from a file. | ||
92 | * | ||
93 | * @param system the testing system handle | ||
94 | * @param filename the path of the hostkeys file | ||
95 | * @return GNUNET_OK on success; GNUNET_SYSERR on error | ||
96 | */ | ||
97 | int | ||
98 | GNUNET_TESTING_hostkeys_load (struct GNUNET_TESTING_System *system, | ||
99 | const char *filename); | ||
100 | |||
101 | |||
102 | /** | ||
103 | * Function to remove the loaded hostkeys | ||
104 | * | ||
105 | * @param system the testing system handle | ||
106 | */ | ||
107 | void | ||
108 | GNUNET_TESTING_hostkeys_unload (struct GNUNET_TESTING_System *system); | ||
109 | |||
110 | |||
111 | /** | ||
90 | * Testing includes a number of pre-created hostkeys for | 112 | * Testing includes a number of pre-created hostkeys for |
91 | * faster peer startup. This function can be used to | 113 | * faster peer startup. This function can be used to |
92 | * access the n-th key of those pre-created hostkeys; note | 114 | * access the n-th key of those pre-created hostkeys; note |
@@ -97,16 +119,15 @@ GNUNET_TESTING_system_destroy (struct GNUNET_TESTING_System *system, | |||
97 | * This is primarily a helper function used internally | 119 | * This is primarily a helper function used internally |
98 | * by 'GNUNET_TESTING_peer_configure'. | 120 | * by 'GNUNET_TESTING_peer_configure'. |
99 | * | 121 | * |
122 | * @param system the testing system handle | ||
100 | * @param key_number desired pre-created hostkey to obtain | 123 | * @param key_number desired pre-created hostkey to obtain |
101 | * @param filename where to store the hostkey (file will | ||
102 | * be created, or overwritten if it already exists) | ||
103 | * @param id set to the peer's identity (hash of the public | 124 | * @param id set to the peer's identity (hash of the public |
104 | * key; if NULL, GNUNET_SYSERR is returned immediately | 125 | * key; if NULL, GNUNET_SYSERR is returned immediately |
105 | * @return GNUNET_SYSERR on error (not enough keys) | 126 | * @return GNUNET_SYSERR on error (not enough keys) |
106 | */ | 127 | */ |
107 | int | 128 | int |
108 | GNUNET_TESTING_hostkey_get (uint32_t key_number, | 129 | GNUNET_TESTING_hostkey_get (const struct GNUNET_TESTING_System *system, |
109 | const char *filename, | 130 | uint32_t key_number, |
110 | struct GNUNET_PeerIdentity *id); | 131 | struct GNUNET_PeerIdentity *id); |
111 | 132 | ||
112 | 133 | ||
diff --git a/src/testing/testing_new.c b/src/testing/testing_new.c index 6b5413c89..887ad1180 100644 --- a/src/testing/testing_new.c +++ b/src/testing/testing_new.c | |||
@@ -68,6 +68,11 @@ struct GNUNET_TESTING_System | |||
68 | char *controller; | 68 | char *controller; |
69 | 69 | ||
70 | /** | 70 | /** |
71 | * Hostkeys data | ||
72 | */ | ||
73 | char *hostkeys_data; | ||
74 | |||
75 | /** | ||
71 | * Bitmap where each TCP port that has already been reserved for | 76 | * Bitmap where each TCP port that has already been reserved for |
72 | * some GNUnet peer is recorded. Note that we additionally need to | 77 | * some GNUnet peer is recorded. Note that we additionally need to |
73 | * test if a port is already in use by non-GNUnet components before | 78 | * test if a port is already in use by non-GNUnet components before |
@@ -97,7 +102,12 @@ struct GNUNET_TESTING_System | |||
97 | * by one for each configured peer. Even if peers are destroyed, | 102 | * by one for each configured peer. Even if peers are destroyed, |
98 | * we never re-use path counters. | 103 | * we never re-use path counters. |
99 | */ | 104 | */ |
100 | uint32_t path_counter; | 105 | uint32_t path_counter; |
106 | |||
107 | /** | ||
108 | * The number of hostkeys | ||
109 | */ | ||
110 | uint32_t total_hostkeys; | ||
101 | }; | 111 | }; |
102 | 112 | ||
103 | 113 | ||
@@ -184,6 +194,13 @@ GNUNET_TESTING_system_destroy (struct GNUNET_TESTING_System *system, | |||
184 | int remove_paths) | 194 | int remove_paths) |
185 | { | 195 | { |
186 | GNUNET_assert (NULL != system); | 196 | GNUNET_assert (NULL != system); |
197 | if (NULL != system->hostkeys_data) | ||
198 | { | ||
199 | GNUNET_break (0); /* Use GNUNET_TESTING_hostkeys_unload() */ | ||
200 | GNUNET_free (system->hostkeys_data); | ||
201 | system->hostkeys_data = NULL; | ||
202 | system->total_hostkeys = 0; | ||
203 | } | ||
187 | if (GNUNET_YES == remove_paths) | 204 | if (GNUNET_YES == remove_paths) |
188 | GNUNET_DISK_directory_remove (system->tmppath); | 205 | GNUNET_DISK_directory_remove (system->tmppath); |
189 | GNUNET_free (system->tmppath); | 206 | GNUNET_free (system->tmppath); |
@@ -315,38 +332,21 @@ reserve_path (struct GNUNET_TESTING_System *system) | |||
315 | 332 | ||
316 | 333 | ||
317 | /** | 334 | /** |
318 | * Testing includes a number of pre-created hostkeys for | 335 | * Testing includes a number of pre-created hostkeys for faster peer |
319 | * faster peer startup. This function can be used to | 336 | * startup. This function loads such keys into memory from a file. |
320 | * access the n-th key of those pre-created hostkeys; note | ||
321 | * that these keys are ONLY useful for testing and not | ||
322 | * secure as the private keys are part of the public | ||
323 | * GNUnet source code. | ||
324 | * | ||
325 | * This is primarily a helper function used internally | ||
326 | * by 'GNUNET_TESTING_peer_configure'. | ||
327 | * | 337 | * |
328 | * @param key_number desired pre-created hostkey to obtain | 338 | * @param system the testing system handle |
329 | * @param filename where to store the hostkey (file will | 339 | * @param filename the path of the hostkeys file |
330 | * be created, or overwritten if it already exists) | 340 | * @return GNUNET_OK on success; GNUNET_SYSERR on error |
331 | * @param id set to the peer's identity (hash of the public | ||
332 | * key; if NULL, GNUNET_SYSERR is returned immediately | ||
333 | * @return GNUNET_SYSERR on error (not enough keys) | ||
334 | */ | 341 | */ |
335 | int | 342 | int |
336 | GNUNET_TESTING_hostkey_get (uint32_t key_number, | 343 | GNUNET_TESTING_hostkeys_load (struct GNUNET_TESTING_System *system, |
337 | const char *filename, | 344 | const char *filename) |
338 | struct GNUNET_PeerIdentity *id) | ||
339 | { | 345 | { |
340 | struct GNUNET_DISK_FileHandle *fd; | 346 | struct GNUNET_DISK_FileHandle *fd; |
341 | struct GNUNET_CRYPTO_RsaPrivateKey *private_key; | 347 | uint64_t fs; |
342 | struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key; | 348 | |
343 | char *file_data; | 349 | if (GNUNET_YES != GNUNET_DISK_file_test (filename)) |
344 | uint64_t fs; | ||
345 | uint32_t total_hostkeys; | ||
346 | |||
347 | if (NULL == id) | ||
348 | return GNUNET_SYSERR; | ||
349 | if (GNUNET_YES != GNUNET_DISK_file_test (filename)) | ||
350 | { | 350 | { |
351 | LOG (GNUNET_ERROR_TYPE_ERROR, | 351 | LOG (GNUNET_ERROR_TYPE_ERROR, |
352 | "Hostkeys file not found: %s\n", filename); | 352 | "Hostkeys file not found: %s\n", filename); |
@@ -376,32 +376,77 @@ GNUNET_TESTING_hostkey_get (uint32_t key_number, | |||
376 | "Incorrect hostkey file format: %s\n", filename); | 376 | "Incorrect hostkey file format: %s\n", filename); |
377 | return GNUNET_SYSERR; | 377 | return GNUNET_SYSERR; |
378 | } | 378 | } |
379 | total_hostkeys = fs / HOSTKEYFILESIZE; | 379 | GNUNET_break (NULL == system->hostkeys_data); |
380 | if (key_number >= total_hostkeys) | 380 | system->total_hostkeys = fs / HOSTKEYFILESIZE; |
381 | system->hostkeys_data = GNUNET_malloc_large (fs); /* free in hostkeys_unload */ | ||
382 | GNUNET_assert (fs == GNUNET_DISK_file_read (fd, system->hostkeys_data, fs)); | ||
383 | GNUNET_DISK_file_close (fd); | ||
384 | return GNUNET_OK; | ||
385 | } | ||
386 | |||
387 | |||
388 | /** | ||
389 | * Function to remove the loaded hostkeys | ||
390 | * | ||
391 | * @param system the testing system handle | ||
392 | */ | ||
393 | void | ||
394 | GNUNET_TESTING_hostkeys_unload (struct GNUNET_TESTING_System *system) | ||
395 | { | ||
396 | GNUNET_break (NULL != system->hostkeys_data); | ||
397 | GNUNET_free_non_null (system->hostkeys_data); | ||
398 | system->hostkeys_data = NULL; | ||
399 | system->total_hostkeys = 0; | ||
400 | } | ||
401 | |||
402 | |||
403 | /** | ||
404 | * Testing includes a number of pre-created hostkeys for | ||
405 | * faster peer startup. This function can be used to | ||
406 | * access the n-th key of those pre-created hostkeys; note | ||
407 | * that these keys are ONLY useful for testing and not | ||
408 | * secure as the private keys are part of the public | ||
409 | * GNUnet source code. | ||
410 | * | ||
411 | * This is primarily a helper function used internally | ||
412 | * by 'GNUNET_TESTING_peer_configure'. | ||
413 | * | ||
414 | * @param system the testing system handle | ||
415 | * @param key_number desired pre-created hostkey to obtain | ||
416 | * @param id set to the peer's identity (hash of the public | ||
417 | * key; if NULL, GNUNET_SYSERR is returned immediately | ||
418 | * @return GNUNET_SYSERR on error (not enough keys) | ||
419 | */ | ||
420 | int | ||
421 | GNUNET_TESTING_hostkey_get (const struct GNUNET_TESTING_System *system, | ||
422 | uint32_t key_number, | ||
423 | struct GNUNET_PeerIdentity *id) | ||
424 | { | ||
425 | struct GNUNET_CRYPTO_RsaPrivateKey *private_key; | ||
426 | struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key; | ||
427 | |||
428 | if ((NULL == id) || (NULL == system->hostkeys_data)) | ||
429 | return GNUNET_SYSERR; | ||
430 | if (key_number >= system->total_hostkeys) | ||
381 | { | 431 | { |
382 | GNUNET_DISK_file_close (fd); | 432 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
383 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
384 | "Key number %u doesn't exist\n", key_number); | 433 | "Key number %u doesn't exist\n", key_number); |
385 | return GNUNET_SYSERR; | 434 | return GNUNET_SYSERR; |
386 | } | 435 | } |
387 | file_data = GNUNET_malloc_large (fs); | 436 | private_key = GNUNET_CRYPTO_rsa_decode_key (system->hostkeys_data + |
388 | GNUNET_assert (fs == GNUNET_DISK_file_read (fd, file_data, fs)); | ||
389 | GNUNET_DISK_file_close (fd); | ||
390 | private_key = GNUNET_CRYPTO_rsa_decode_key (file_data + | ||
391 | (key_number * HOSTKEYFILESIZE), | 437 | (key_number * HOSTKEYFILESIZE), |
392 | HOSTKEYFILESIZE); | 438 | HOSTKEYFILESIZE); |
393 | if (NULL == private_key) | 439 | if (NULL == private_key) |
394 | { | 440 | { |
395 | LOG (GNUNET_ERROR_TYPE_ERROR, | 441 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
396 | "Error while decoding key %u from %s\n", key_number, filename); | 442 | "Error while decoding key %u\n", key_number); |
397 | GNUNET_free (file_data); | ||
398 | return GNUNET_SYSERR; | 443 | return GNUNET_SYSERR; |
399 | } | 444 | } |
400 | GNUNET_CRYPTO_rsa_key_get_public (private_key, &public_key); | 445 | GNUNET_CRYPTO_rsa_key_get_public (private_key, &public_key); |
446 | GNUNET_CRYPTO_rsa_key_free (private_key); | ||
401 | GNUNET_CRYPTO_hash (&public_key, | 447 | GNUNET_CRYPTO_hash (&public_key, |
402 | sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), | 448 | sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), |
403 | &(id->hashPubKey)); | 449 | &(id->hashPubKey)); |
404 | GNUNET_free (file_data); | ||
405 | return GNUNET_OK; | 450 | return GNUNET_OK; |
406 | } | 451 | } |
407 | 452 | ||