Commit 3a4f820d authored by Ondřej Surý's avatar Ondřej Surý Committed by Witold Krecicki
Browse files

Replace all random functions with isc_random, isc_random_buf and isc_random_uniform API.

The three functions has been modeled after the arc4random family of
functions, and they will always return random bytes.

The isc_random family of functions internally use these CSPRNG (if available):

1. getrandom() libc call (might be available on Linux and Solaris)
2. SYS_getrandom syscall (might be available on Linux, detected at runtime)
3. arc4random(), arc4random_buf() and arc4random_uniform() (available on BSDs and Mac OS X)
4. crypto library function:
4a. RAND_bytes in case OpenSSL
4b. pkcs_C_GenerateRandom() in case PKCS#11 library
parent 74dd289a
......@@ -20,7 +20,6 @@
#include <isc/commandline.h>
#include <isc/dir.h>
#include <isc/entropy.h>
#include <isc/hash.h>
#include <isc/log.h>
#include <isc/mem.h>
......@@ -538,7 +537,6 @@ main(int argc, char **argv) {
isc_mem_t *mctx = NULL;
isc_result_t result;
int exit_status = 0;
isc_entropy_t *ectx = NULL;
isc_boolean_t load_zones = ISC_FALSE;
isc_boolean_t list_zones = ISC_FALSE;
isc_boolean_t print = ISC_FALSE;
......@@ -654,7 +652,6 @@ main(int argc, char **argv) {
RUNTIME_CHECK(setup_logging(mctx, stdout, &logc) == ISC_R_SUCCESS);
RUNTIME_CHECK(isc_entropy_create(mctx, &ectx) == ISC_R_SUCCESS);
dns_result_register();
RUNTIME_CHECK(cfg_parser_create(mctx, logc, &parser) == ISC_R_SUCCESS);
......@@ -685,8 +682,6 @@ main(int argc, char **argv) {
isc_log_destroy(&logc);
isc_entropy_detach(&ectx);
isc_mem_destroy(&mctx);
#ifdef _WIN32
......
......@@ -19,7 +19,6 @@
#include <isc/app.h>
#include <isc/commandline.h>
#include <isc/dir.h>
#include <isc/entropy.h>
#include <isc/hash.h>
#include <isc/log.h>
#include <isc/mem.h>
......@@ -46,7 +45,6 @@
static int quiet = 0;
static isc_mem_t *mctx = NULL;
static isc_entropy_t *ectx = NULL;
dns_zone_t *zone = NULL;
dns_zonetype_t zonetype = dns_zone_master;
static int dumpzone = 0;
......@@ -521,7 +519,6 @@ main(int argc, char **argv) {
if (!quiet)
RUNTIME_CHECK(setup_logging(mctx, errout, &lctx)
== ISC_R_SUCCESS);
RUNTIME_CHECK(isc_entropy_create(mctx, &ectx) == ISC_R_SUCCESS);
dns_result_register();
......@@ -553,7 +550,6 @@ main(int argc, char **argv) {
destroy();
if (lctx != NULL)
isc_log_destroy(&lctx);
isc_entropy_detach(&ectx);
isc_mem_destroy(&mctx);
#ifdef _WIN32
DestroySockets();
......
......@@ -26,7 +26,6 @@
#include <isc/base64.h>
#include <isc/buffer.h>
#include <isc/commandline.h>
#include <isc/entropy.h>
#include <isc/file.h>
#include <isc/keyboard.h>
#include <isc/mem.h>
......@@ -67,10 +66,9 @@ usage(int status) {
if (progmode == progmode_confgen) {
fprintf(stderr, "\
Usage:\n\
%s [-a alg] [-k keyname] [-r randomfile] [-q] [-s name | -z zone]\n\
%s [-a alg] [-k keyname] [-q] [-s name | -z zone]\n\
-a alg: algorithm (default hmac-sha256)\n\
-k keyname: name of the key as it will be used in named.conf\n\
-r randomfile: source of random data (use \"keyboard\" for key timing)\n\
-s name: domain name to be updated using the created key\n\
-z zone: name of the zone as it will be used in named.conf\n\
-q: quiet mode: print the key, with no explanatory text\n",
......@@ -78,9 +76,8 @@ Usage:\n\
} else {
fprintf(stderr, "\
Usage:\n\
%s [-a alg] [-r randomfile] [keyname]\n\
-a alg: algorithm (default hmac-sha256)\n\
-r randomfile: source of random data (use \"keyboard\" for key timing)\n",
%s [-a alg] [keyname]\n\
-a alg: algorithm (default hmac-sha256)\n\n",
progname);
}
......@@ -95,7 +92,6 @@ main(int argc, char **argv) {
isc_buffer_t key_txtbuffer;
char key_txtsecret[256];
isc_mem_t *mctx = NULL;
const char *randomfile = NULL;
const char *keyname = NULL;
const char *zone = NULL;
const char *self_domain = NULL;
......@@ -168,7 +164,7 @@ main(int argc, char **argv) {
usage(1);
break;
case 'r':
randomfile = isc_commandline_argument;
fatal("The -r option has been deprecated.");
break;
case 's':
if (progmode == progmode_confgen)
......@@ -235,7 +231,7 @@ main(int argc, char **argv) {
isc_buffer_init(&key_txtbuffer, &key_txtsecret, sizeof(key_txtsecret));
generate_key(mctx, randomfile, alg, keysize, &key_txtbuffer);
generate_key(mctx, alg, keysize, &key_txtbuffer);
if (!quiet)
......
......@@ -19,7 +19,6 @@
#include <isc/base64.h>
#include <isc/buffer.h>
#include <isc/entropy.h>
#include <isc/file.h>
#include <isc/keyboard.h>
#include <isc/mem.h>
......@@ -113,17 +112,12 @@ alg_bits(dns_secalg_t alg) {
}
/*%
* Generate a key of size 'keysize' using entropy source 'randomfile',
* and place it in 'key_txtbuffer'
* Generate a key of size 'keysize' and place it in 'key_txtbuffer'
*/
void
generate_key(isc_mem_t *mctx, const char *randomfile, dns_secalg_t alg,
int keysize, isc_buffer_t *key_txtbuffer) {
generate_key(isc_mem_t *mctx, dns_secalg_t alg, int keysize,
isc_buffer_t *key_txtbuffer) {
isc_result_t result = ISC_R_SUCCESS;
isc_entropysource_t *entropy_source = NULL;
int open_keyboard = ISC_ENTROPY_KEYBOARDMAYBE;
int entropy_flags = 0;
isc_entropy_t *ectx = NULL;
isc_buffer_t key_rawbuffer;
isc_region_t key_rawregion;
char key_rawsecret[64];
......@@ -150,24 +144,7 @@ generate_key(isc_mem_t *mctx, const char *randomfile, dns_secalg_t alg,
fatal("unsupported algorithm %d\n", alg);
}
DO("create entropy context", isc_entropy_create(mctx, &ectx));
if (randomfile == NULL) {
isc_entropy_usehook(ectx, ISC_TRUE);
}
if (randomfile != NULL && strcmp(randomfile, "keyboard") == 0) {
randomfile = NULL;
open_keyboard = ISC_ENTROPY_KEYBOARDYES;
}
DO("start entropy source", isc_entropy_usebestsource(ectx,
&entropy_source,
randomfile,
open_keyboard));
entropy_flags = ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY;
DO("initialize dst library", dst_lib_init(mctx, ectx, NULL, entropy_flags));
DO("initialize dst library", dst_lib_init(mctx, NULL));
DO("generate key", dst_key_generate(dns_rootname, alg,
keysize, 0, 0, DNS_KEYPROTO_ANY,
......@@ -183,17 +160,9 @@ generate_key(isc_mem_t *mctx, const char *randomfile, dns_secalg_t alg,
DO("bsse64 encode secret", isc_base64_totext(&key_rawregion, -1, "",
key_txtbuffer));
/*
* Shut down the entropy source now so the "stop typing" message
* does not muck with the output.
*/
if (entropy_source != NULL)
isc_entropy_destroysource(&entropy_source);
if (key != NULL)
dst_key_free(&key);
isc_entropy_detach(&ectx);
dst_lib_destroy();
}
......
......@@ -19,8 +19,8 @@
ISC_LANG_BEGINDECLS
void generate_key(isc_mem_t *mctx, const char *randomfile, dns_secalg_t alg,
int keysize, isc_buffer_t *key_txtbuffer);
void generate_key(isc_mem_t *mctx, dns_secalg_t alg, int keysize,
isc_buffer_t *key_txtbuffer);
void write_key_file(const char *keyfile, const char *user,
const char *keyname, isc_buffer_t *secret,
......
......@@ -29,7 +29,6 @@
#include <isc/base64.h>
#include <isc/buffer.h>
#include <isc/commandline.h>
#include <isc/entropy.h>
#include <isc/file.h>
#include <isc/keyboard.h>
#include <isc/mem.h>
......@@ -70,7 +69,7 @@ usage(int status) {
fprintf(stderr, "\
Usage:\n\
%s [-a] [-b bits] [-c keyfile] [-k keyname] [-p port] [-r randomfile] \
%s [-a] [-b bits] [-c keyfile] [-k keyname] [-p port] \
[-s addr] [-t chrootdir] [-u user]\n\
-a: generate just the key clause and write it to keyfile (%s)\n\
-A alg: algorithm (default hmac-sha256)\n\
......@@ -78,7 +77,6 @@ Usage:\n\
-c keyfile: specify an alternate key file (requires -a)\n\
-k keyname: the name as it will be used in named.conf and rndc.conf\n\
-p port: the port named will listen on and rndc will connect to\n\
-r randomfile: source of random data (use \"keyboard\" for key timing)\n\
-s addr: the address to which rndc should connect\n\
-t chrootdir: write a keyfile in chrootdir as well (requires -a)\n\
-u user: set the keyfile owner to \"user\" (requires -a)\n",
......@@ -95,7 +93,6 @@ main(int argc, char **argv) {
isc_mem_t *mctx = NULL;
isc_result_t result = ISC_R_SUCCESS;
const char *keyname = NULL;
const char *randomfile = NULL;
const char *serveraddr = NULL;
dns_secalg_t alg;
const char *algname;
......@@ -165,7 +162,7 @@ main(int argc, char **argv) {
isc_commandline_argument);
break;
case 'r':
randomfile = isc_commandline_argument;
fatal("The -r option has been deprecated.");
break;
case 's':
serveraddr = isc_commandline_argument;
......@@ -217,7 +214,7 @@ main(int argc, char **argv) {
DO("create memory context", isc_mem_create(0, 0, &mctx));
isc_buffer_init(&key_txtbuffer, &key_txtsecret, sizeof(key_txtsecret));
generate_key(mctx, randomfile, alg, keysize, &key_txtbuffer);
generate_key(mctx, alg, keysize, &key_txtbuffer);
if (keyonly) {
write_key_file(keyfile, chrootdir == NULL ? user : NULL,
......
......@@ -57,7 +57,6 @@
#include <isc/app.h>
#include <isc/base64.h>
#include <isc/entropy.h>
#include <isc/file.h>
#include <isc/hex.h>
#include <isc/lang.h>
......@@ -178,7 +177,6 @@ unsigned int digestbits = 0;
isc_buffer_t *namebuf = NULL;
dns_tsigkey_t *tsigkey = NULL;
isc_boolean_t validated = ISC_TRUE;
isc_entropy_t *entp = NULL;
isc_mempool_t *commctx = NULL;
isc_boolean_t debugging = ISC_FALSE;
isc_boolean_t debugtiming = ISC_FALSE;
......@@ -1316,10 +1314,8 @@ setup_system(isc_boolean_t ipv4only, isc_boolean_t ipv6only) {
setup_file_key();
else if (keysecret[0] != 0)
setup_text_key();
result = isc_entropy_getdata(entp, cookie_secret,
sizeof(cookie_secret), NULL, 0);
if (result != ISC_R_SUCCESS)
fatal("unable to generate cookie secret");
isc_random_buf(cookie_secret, sizeof(cookie_secret));
}
/*%
......@@ -1388,10 +1384,7 @@ setup_libs(void) {
result = isc_socketmgr_create(mctx, &socketmgr);
check_result(result, "isc_socketmgr_create");
result = isc_entropy_create(mctx, &entp);
check_result(result, "isc_entropy_create");
result = dst_lib_init(mctx, entp, NULL, 0);
result = dst_lib_init(mctx, NULL);
check_result(result, "dst_lib_init");
is_dst_up = ISC_TRUE;
......@@ -1877,7 +1870,7 @@ followup_lookup(dns_message_t *msg, dig_query_t *query, dns_section_t section)
srv != NULL;
srv = ISC_LIST_HEAD(lookup->my_server_list)) {
INSIST(i > 0);
isc_random_get(&j);
j = isc_random();
j %= i;
next = ISC_LIST_NEXT(srv, link);
while (j-- > 0 && next != NULL) {
......@@ -2205,7 +2198,7 @@ setup_lookup(dig_lookup_t *lookup) {
dighost_trying(store, lookup);
INSIST(dns_name_isabsolute(lookup->name));
isc_random_get(&id);
id = isc_random();
lookup->sendmsg->id = (unsigned short)id & 0xFFFF;
lookup->sendmsg->opcode = lookup->opcode;
lookup->msgcounter = 0;
......@@ -4217,10 +4210,6 @@ destroy_libs(void) {
dst_lib_destroy();
is_dst_up = ISC_FALSE;
}
if (entp != NULL) {
debug("detach from entropy");
isc_entropy_detach(&entp);
}
UNLOCK_LOOKUP;
DESTROYLOCK(&lookup_lock);
......
......@@ -23,7 +23,6 @@
#include <isc/buffer.h>
#include <isc/commandline.h>
#include <isc/entropy.h>
#include <isc/file.h>
#include <isc/hash.h>
#include <isc/mem.h>
......@@ -72,7 +71,6 @@ int verbose;
*/
static isc_log_t *lctx = NULL;
static isc_mem_t *mctx = NULL;
static isc_entropy_t *ectx = NULL;
/*
* The domain we are working on
......@@ -1233,16 +1231,11 @@ main(int argc, char *argv[]) {
setup_logging(mctx, &lctx);
if (ectx == NULL) {
setup_entropy(mctx, NULL, &ectx);
}
result = dst_lib_init(mctx, ectx, NULL,
ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY);
result = dst_lib_init(mctx, NULL);
if (result != ISC_R_SUCCESS) {
fatal("could not initialize dst: %s",
isc_result_totext(result));
}
isc_entropy_stopcallbacksources(ectx);
if (ds_path == NULL) {
fatal("missing -d DS pathname");
......@@ -1392,7 +1385,6 @@ main(int argc, char *argv[]) {
free_all_sets();
cleanup_logging(&lctx);
dst_lib_destroy();
cleanup_entropy(&ectx);
if (verbose > 10) {
isc_mem_stats(mctx, stdout);
}
......
......@@ -17,7 +17,6 @@
#include <isc/buffer.h>
#include <isc/commandline.h>
#include <isc/entropy.h>
#include <isc/hash.h>
#include <isc/mem.h>
#include <isc/print.h>
......@@ -359,7 +358,6 @@ main(int argc, char **argv) {
isc_boolean_t showall = ISC_FALSE;
isc_result_t result;
isc_log_t *log = NULL;
isc_entropy_t *ectx = NULL;
dns_rdataset_t rdataset;
dns_rdata_t rdata;
......@@ -475,14 +473,10 @@ main(int argc, char **argv) {
if (argc > isc_commandline_index + 1)
fatal("extraneous arguments");
if (ectx == NULL)
setup_entropy(mctx, NULL, &ectx);
result = dst_lib_init(mctx, ectx, NULL,
ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY);
result = dst_lib_init(mctx, NULL);
if (result != ISC_R_SUCCESS)
fatal("could not initialize dst: %s",
isc_result_totext(result));
isc_entropy_stopcallbacksources(ectx);
setup_logging(mctx, &log);
......@@ -544,7 +538,6 @@ main(int argc, char **argv) {
dns_rdataset_disassociate(&rdataset);
cleanup_logging(&log);
dst_lib_destroy();
cleanup_entropy(&ectx);
dns_name_destroy();
if (verbose > 10)
isc_mem_stats(mctx, stdout);
......
......@@ -17,7 +17,6 @@
#include <isc/buffer.h>
#include <isc/commandline.h>
#include <isc/entropy.h>
#include <isc/hash.h>
#include <isc/mem.h>
#include <isc/print.h>
......@@ -297,7 +296,6 @@ main(int argc, char **argv) {
int ch;
isc_result_t result;
isc_log_t *log = NULL;
isc_entropy_t *ectx = NULL;
dns_rdataset_t rdataset;
dns_rdata_t rdata;
isc_stdtime_t now;
......@@ -404,14 +402,10 @@ main(int argc, char **argv) {
if (argc > isc_commandline_index + 1)
fatal("extraneous arguments");
if (ectx == NULL)
setup_entropy(mctx, NULL, &ectx);
result = dst_lib_init(mctx, ectx, NULL,
ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY);
result = dst_lib_init(mctx, NULL);
if (result != ISC_R_SUCCESS)
fatal("could not initialize dst: %s",
isc_result_totext(result));
isc_entropy_stopcallbacksources(ectx);
setup_logging(mctx, &log);
......@@ -455,7 +449,6 @@ main(int argc, char **argv) {
dns_rdataset_disassociate(&rdataset);
cleanup_logging(&log);
dst_lib_destroy();
cleanup_entropy(&ectx);
dns_name_destroy();
if (verbose > 10)
isc_mem_stats(mctx, stdout);
......
......@@ -18,7 +18,6 @@
#include <isc/buffer.h>
#include <isc/commandline.h>
#include <isc/entropy.h>
#include <isc/mem.h>
#include <isc/region.h>
#include <isc/print.h>
......@@ -146,7 +145,6 @@ main(int argc, char **argv) {
char filename[255];
isc_buffer_t buf;
isc_log_t *log = NULL;
isc_entropy_t *ectx = NULL;
dns_rdataclass_t rdclass;
int options = DST_TYPE_PRIVATE | DST_TYPE_PUBLIC;
char *label = NULL;
......@@ -347,10 +345,7 @@ main(int argc, char **argv) {
}
}
if (ectx == NULL)
setup_entropy(mctx, NULL, &ectx);
ret = dst_lib_init(mctx, ectx, engine,
ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY);
ret = dst_lib_init(mctx, engine);
if (ret != ISC_R_SUCCESS)
fatal("could not initialize dst: %s",
isc_result_totext(ret));
......@@ -618,7 +613,6 @@ main(int argc, char **argv) {
engine,
#endif
label, NULL, mctx, &key);
isc_entropy_stopcallbacksources(ectx);
if (ret != ISC_R_SUCCESS) {
char namestr[DNS_NAME_FORMATSIZE];
......@@ -737,7 +731,6 @@ main(int argc, char **argv) {
dst_key_free(&prevkey);
cleanup_logging(&log);
cleanup_entropy(&ectx);
dst_lib_destroy();
dns_name_destroy();
if (verbose > 10)
......
......@@ -33,7 +33,6 @@
#include <isc/buffer.h>
#include <isc/commandline.h>
#include <isc/entropy.h>
#include <isc/mem.h>
#include <isc/print.h>
#include <isc/region.h>
......@@ -121,7 +120,7 @@ usage(void) {
"(DH only)\n");
fprintf(stderr, " -L <ttl>: default key TTL\n");
fprintf(stderr, " -p <protocol>: (default: 3 [dnssec])\n");
fprintf(stderr, " -r <randomdev>: a file containing random data\n");
fprintf(stderr, " -r <randomdev>: DEPRECATED and ignored\n");
fprintf(stderr, " -s <strength>: strength value this key signs DNS "
"records with (default: 0)\n");
fprintf(stderr, " -T <rrtype>: DNSKEY | KEY (default: DNSKEY; "
......@@ -218,7 +217,6 @@ main(int argc, char **argv) {
dst_key_t *prevkey = NULL;
isc_buffer_t buf;
isc_log_t *log = NULL;
isc_entropy_t *ectx = NULL;
#ifdef USE_PKCS11
const char *engine = PKCS11_ENGINE;
#else
......@@ -365,7 +363,8 @@ main(int argc, char **argv) {
quiet = ISC_TRUE;
break;
case 'r':
setup_entropy(mctx, isc_commandline_argument, &ectx);
fatal("The -r option has been deprecated.\n"
"System random data is always used.\n");
break;
case 's':
signatory = strtol(isc_commandline_argument,
......@@ -494,10 +493,7 @@ main(int argc, char **argv) {
if (!isatty(0))
quiet = ISC_TRUE;
if (ectx == NULL)
setup_entropy(mctx, NULL, &ectx);
ret = dst_lib_init(mctx, ectx, engine,
ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY);
ret = dst_lib_init(mctx, engine);
if (ret != ISC_R_SUCCESS)
fatal("could not initialize dst: %s",
isc_result_totext(ret));
......@@ -872,8 +868,6 @@ main(int argc, char **argv) {
NULL);
}
isc_entropy_stopcallbacksources(ectx);
if (ret != ISC_R_SUCCESS) {
char namestr[DNS_NAME_FORMATSIZE];
char algstr[DNS_SECALG_FORMATSIZE];
......@@ -1026,7 +1020,6 @@ main(int argc, char **argv) {
dst_key_free(&prevkey);
cleanup_logging(&log);
cleanup_entropy(&ectx);
dst_lib_destroy();
dns_name_destroy();
if (verbose > 10)
......
......@@ -18,7 +18,6 @@
#include <isc/buffer.h>
#include <isc/commandline.h>
#include <isc/entropy.h>
#include <isc/file.h>
#include <isc/hash.h>
#include <isc/mem.h>
......@@ -87,7 +86,6 @@ main(int argc, char **argv) {
char keystr[DST_KEY_FORMATSIZE];
char *endp;
int ch;
isc_entropy_t *ectx = NULL;
dst_key_t *key = NULL;
isc_uint32_t flags;
isc_buffer_t buf;
......@@ -180,14 +178,10 @@ main(int argc, char **argv) {
}
}
if (ectx == NULL)
setup_entropy(mctx, NULL, &ectx);
result = dst_lib_init(mctx, ectx, engine,
ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY);
result = dst_lib_init(mctx, engine);
if (result != ISC_R_SUCCESS)
fatal("Could not initialize dst: %s",
isc_result_totext(result));
isc_entropy_stopcallbacksources(ectx);
result = dst_key_fromnamedfile(filename, dir,
DST_TYPE_PUBLIC|DST_TYPE_PRIVATE,
......@@ -269,7 +263,6 @@ main(int argc, char **argv) {
cleanup:
dst_key_free(&key);
dst_lib_destroy();
cleanup_entropy(&ectx);
if (verbose > 10)
isc_mem_stats(mctx, stdout);
if (dir != NULL)
......
......@@ -20,7 +20,6 @@
#include <isc/buffer.h>
#include <isc/commandline.h>
#include <isc/entropy.h>
#include <isc/file.h>
#include <isc/hash.h>
#include <isc/mem.h>
......@@ -139,7 +138,6 @@ main(int argc, char **argv) {
char keystr[DST_KEY_FORMATSIZE];
char *endp, *p;
int ch;
isc_entropy_t *ectx = NULL;
const char *predecessor = NULL;
dst_key_t *prevkey = NULL;
dst_key_t *key = NULL;
......@@ -378,14 +376,10 @@ main(int argc, char **argv) {
if (argc > isc_commandline_index + 1)
fatal("Extraneous arguments");
if (ectx == NULL)
setup_entropy(mctx, NULL, &ectx);
result = dst_lib_init(mctx, ectx, engine,
ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY);
result = dst_lib_init(mctx, engine);