From c73aafe6016ed1a7a6972681148cedf6a48a21bc Mon Sep 17 00:00:00 2001 From: Brian Wellington Date: Tue, 28 Mar 2000 03:06:38 +0000 Subject: [PATCH] dst_random_get notices when openssl complains about having no entropy, and calls isc_random_get to get some. This should possibly do something else later. --- CHANGES | 5 +++- lib/dns/sec/dst/dst_api.c | 39 ++++++++++++++++++++++++++-- lib/dns/sec/dst/dst_result.c | 3 ++- lib/dns/sec/dst/include/dst/result.h | 3 ++- 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/CHANGES b/CHANGES index 6443eac279..68000f0e4a 100644 --- a/CHANGES +++ b/CHANGES @@ -1,7 +1,10 @@ + 2. [bug] Generating cryptographic randomness failed on + systems without /dev/random. + 1. [bug] The installdirs rule in lib/isc/unix/include/isc/Makefile.in had a typo which prevented the isc directory from being created if it didn't exist. - --- 9.0.0b1 released --- \ No newline at end of file + --- 9.0.0b1 released --- diff --git a/lib/dns/sec/dst/dst_api.c b/lib/dns/sec/dst/dst_api.c index 41ac0c8988..f61bc1b40a 100644 --- a/lib/dns/sec/dst/dst_api.c +++ b/lib/dns/sec/dst/dst_api.c @@ -17,7 +17,7 @@ /* * Principal Author: Brian Wellington - * $Id: dst_api.c,v 1.23 2000/03/16 22:43:33 halley Exp $ + * $Id: dst_api.c,v 1.24 2000/03/28 03:06:36 bwelling Exp $ */ #include @@ -37,7 +37,9 @@ #include #include #include +#include #include +#include #include #include @@ -810,6 +812,8 @@ dst_secret_size(const dst_key_t *key, unsigned int *n) { isc_result_t dst_random_get(const unsigned int wanted, isc_buffer_t *target) { isc_region_t r; + isc_result_t result; + int status; RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS); REQUIRE(target != NULL); @@ -819,10 +823,41 @@ dst_random_get(const unsigned int wanted, isc_buffer_t *target) { return (ISC_R_NOSPACE); RUNTIME_CHECK(isc_mutex_lock((&random_lock)) == ISC_R_SUCCESS); - RAND_bytes(r.base, wanted); + status = RAND_bytes(r.base, wanted); + if (status == 0) { + isc_random_t rctx; + isc_uint32_t val; + isc_time_t now; + int count = 0; + + isc_random_init(&rctx); + result = isc_time_now(&now); + if (result != ISC_R_SUCCESS) + goto failure; + isc_random_seed(&rctx, isc_time_nanoseconds(&now)); + while (RAND_status() == 0 && count < 1000) { + isc_random_get(&rctx, &val); + RAND_add(&val, sizeof(isc_uint32_t), 1); + count++; + } + isc_random_invalidate(&rctx); + if (RAND_status() == 0) { + result = DST_R_NORANDOMNESS; + goto failure; + } + status = RAND_bytes(r.base, wanted); + } RUNTIME_CHECK(isc_mutex_unlock((&random_lock)) == ISC_R_SUCCESS); + if (status == 0) + return (DST_R_NORANDOMNESS); + isc_buffer_add(target, wanted); return (ISC_R_SUCCESS); + + failure: + RUNTIME_CHECK(isc_mutex_unlock((&random_lock)) == ISC_R_SUCCESS); + return (result); + } /*** diff --git a/lib/dns/sec/dst/dst_result.c b/lib/dns/sec/dst/dst_result.c index b1c88c3e84..6d9e4a8efc 100644 --- a/lib/dns/sec/dst/dst_result.c +++ b/lib/dns/sec/dst/dst_result.c @@ -17,7 +17,7 @@ /* * Principal Author: Brian Wellington - * $Id: dst_result.c,v 1.5 2000/03/23 19:48:24 halley Exp $ + * $Id: dst_result.c,v 1.6 2000/03/28 03:06:37 bwelling Exp $ */ #include @@ -51,6 +51,7 @@ static char *text[DST_R_NRESULTS] = { "not a private key", /* 16 */ "not a key that can compute a secret", /* 17 */ "failure computing a shared secret", /* 18 */ + "no randomness available", /* 19 */ }; #define DST_RESULT_RESULTSET 2 diff --git a/lib/dns/sec/dst/include/dst/result.h b/lib/dns/sec/dst/include/dst/result.h index a4ccc783ef..50bf49304d 100644 --- a/lib/dns/sec/dst/include/dst/result.h +++ b/lib/dns/sec/dst/include/dst/result.h @@ -43,8 +43,9 @@ ISC_LANG_BEGINDECLS #define DST_R_NOTPRIVATEKEY (ISC_RESULTCLASS_DST + 16) #define DST_R_KEYCANNOTCOMPUTESECRET (ISC_RESULTCLASS_DST + 17) #define DST_R_COMPUTESECRETFAILURE (ISC_RESULTCLASS_DST + 18) +#define DST_R_NORANDOMNESS (ISC_RESULTCLASS_DST + 19) -#define DST_R_NRESULTS 19 /* Number of results */ +#define DST_R_NRESULTS 20 /* Number of results */ char * dst_result_totext(isc_result_t);