This commit reverts the previous change to use system provided entropy, as (SYS_)getrandom is very slow on Linux because it is a syscall. The change introduced in this commit adds a new call isc_nonce_buf that uses CSPRNG from cryptographic library provider to generate secure data that can be and must be used for generating nonces. Example usage would be DNS cookies. The isc_random() API has been changed to use fast PRNG that is not cryptographically secure, but runs entirely in user space. Two contestants have been considered xoroshiro family of the functions by Villa&Blackman and PCG by O'Neill. After a consideration the xoshiro128starstar function has been used as uint32_t random number provider because it is very fast and has good enough properties for our usage pattern. The other change introduced in the commit is the more extensive usage of isc_random_uniform in places where the usage pattern was isc_random() % n to prevent modulo bias. For usage patterns where only 16 or 8 bits are needed (DNS Message ID), the isc_random() functions has been renamed to isc_random32(), and isc_random16() and isc_random8() functions have been introduced by &-ing the isc_random32() output with 0xffff and 0xff. Please note that the functions that uses stripped down bit count doesn't pass our NIST SP 800-22 based random test.
333 lines
8.4 KiB
C
333 lines
8.4 KiB
C
/*
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
|
*
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
*
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
* information regarding copyright ownership.
|
|
*/
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include <isc/app.h>
|
|
#include <isc/base64.h>
|
|
#include <isc/hash.h>
|
|
#include <isc/log.h>
|
|
#include <isc/mem.h>
|
|
#include <isc/nonce.h>
|
|
#include <isc/print.h>
|
|
#include <isc/random.h>
|
|
#include <isc/sockaddr.h>
|
|
#include <isc/socket.h>
|
|
#include <isc/task.h>
|
|
#include <isc/timer.h>
|
|
#include <isc/util.h>
|
|
|
|
#include <pk11/site.h>
|
|
|
|
#include <dns/dispatch.h>
|
|
#include <dns/fixedname.h>
|
|
#include <dns/keyvalues.h>
|
|
#include <dns/message.h>
|
|
#include <dns/name.h>
|
|
#include <dns/request.h>
|
|
#include <dns/result.h>
|
|
#include <dns/tkey.h>
|
|
#include <dns/tsig.h>
|
|
#include <dns/view.h>
|
|
|
|
#include <dst/result.h>
|
|
|
|
#define CHECK(str, x) { \
|
|
if ((x) != ISC_R_SUCCESS) { \
|
|
fprintf(stderr, "I:%s: %s\n", (str), isc_result_totext(x)); \
|
|
exit(-1); \
|
|
} \
|
|
}
|
|
|
|
#define RUNCHECK(x) RUNTIME_CHECK((x) == ISC_R_SUCCESS)
|
|
|
|
#define PORT 5300
|
|
#define TIMEOUT 30
|
|
|
|
static dst_key_t *ourkey;
|
|
static isc_mem_t *mctx;
|
|
static dns_tsigkey_t *tsigkey, *initialkey;
|
|
static dns_tsig_keyring_t *ring;
|
|
static unsigned char noncedata[16];
|
|
static isc_buffer_t nonce;
|
|
static dns_requestmgr_t *requestmgr;
|
|
static const char *ownername_str = ".";
|
|
|
|
#ifndef PK11_MD5_DISABLE
|
|
static void
|
|
recvquery(isc_task_t *task, isc_event_t *event) {
|
|
dns_requestevent_t *reqev = (dns_requestevent_t *)event;
|
|
isc_result_t result;
|
|
dns_message_t *query, *response;
|
|
char keyname[256];
|
|
isc_buffer_t keynamebuf;
|
|
int type;
|
|
|
|
UNUSED(task);
|
|
|
|
REQUIRE(reqev != NULL);
|
|
|
|
if (reqev->result != ISC_R_SUCCESS) {
|
|
fprintf(stderr, "I:request event result: %s\n",
|
|
isc_result_totext(reqev->result));
|
|
exit(-1);
|
|
}
|
|
|
|
query = reqev->ev_arg;
|
|
|
|
response = NULL;
|
|
result = dns_message_create(mctx, DNS_MESSAGE_INTENTPARSE, &response);
|
|
CHECK("dns_message_create", result);
|
|
|
|
result = dns_request_getresponse(reqev->request, response,
|
|
DNS_MESSAGEPARSE_PRESERVEORDER);
|
|
CHECK("dns_request_getresponse", result);
|
|
|
|
if (response->rcode != dns_rcode_noerror) {
|
|
result = ISC_RESULTCLASS_DNSRCODE + response->rcode;
|
|
fprintf(stderr, "I:response rcode: %s\n",
|
|
isc_result_totext(result));
|
|
exit(-1);
|
|
}
|
|
|
|
result = dns_tkey_processdhresponse(query, response, ourkey, &nonce,
|
|
&tsigkey, ring);
|
|
CHECK("dns_tkey_processdhresponse", result);
|
|
|
|
/*
|
|
* Yes, this is a hack.
|
|
*/
|
|
isc_buffer_init(&keynamebuf, keyname, sizeof(keyname));
|
|
result = dst_key_buildfilename(tsigkey->key, 0, "", &keynamebuf);
|
|
CHECK("dst_key_buildfilename", result);
|
|
printf("%.*s\n", (int)isc_buffer_usedlength(&keynamebuf),
|
|
(char *)isc_buffer_base(&keynamebuf));
|
|
type = DST_TYPE_PRIVATE | DST_TYPE_PUBLIC | DST_TYPE_KEY;
|
|
result = dst_key_tofile(tsigkey->key, type, "");
|
|
CHECK("dst_key_tofile", result);
|
|
|
|
dns_message_destroy(&query);
|
|
dns_message_destroy(&response);
|
|
dns_request_destroy(&reqev->request);
|
|
isc_event_free(&event);
|
|
isc_app_shutdown();
|
|
return;
|
|
}
|
|
#endif
|
|
|
|
static void
|
|
sendquery(isc_task_t *task, isc_event_t *event) {
|
|
#ifndef PK11_MD5_DISABLE
|
|
struct in_addr inaddr;
|
|
isc_sockaddr_t address;
|
|
isc_region_t r;
|
|
isc_result_t result;
|
|
dns_fixedname_t keyname;
|
|
dns_fixedname_t ownername;
|
|
isc_buffer_t namestr, keybuf;
|
|
unsigned char keydata[9];
|
|
dns_message_t *query;
|
|
dns_request_t *request;
|
|
static char keystr[] = "0123456789ab";
|
|
|
|
isc_event_free(&event);
|
|
|
|
result = ISC_R_FAILURE;
|
|
if (inet_pton(AF_INET, "10.53.0.1", &inaddr) != 1)
|
|
CHECK("inet_pton", result);
|
|
isc_sockaddr_fromin(&address, &inaddr, PORT);
|
|
|
|
dns_fixedname_init(&keyname);
|
|
isc_buffer_constinit(&namestr, "tkeytest.", 9);
|
|
isc_buffer_add(&namestr, 9);
|
|
result = dns_name_fromtext(dns_fixedname_name(&keyname), &namestr,
|
|
NULL, 0, NULL);
|
|
CHECK("dns_name_fromtext", result);
|
|
|
|
dns_fixedname_init(&ownername);
|
|
isc_buffer_constinit(&namestr, ownername_str, strlen(ownername_str));
|
|
isc_buffer_add(&namestr, strlen(ownername_str));
|
|
result = dns_name_fromtext(dns_fixedname_name(&ownername), &namestr,
|
|
NULL, 0, NULL);
|
|
CHECK("dns_name_fromtext", result);
|
|
|
|
isc_buffer_init(&keybuf, keydata, 9);
|
|
result = isc_base64_decodestring(keystr, &keybuf);
|
|
CHECK("isc_base64_decodestring", result);
|
|
|
|
isc_buffer_usedregion(&keybuf, &r);
|
|
|
|
initialkey = NULL;
|
|
result = dns_tsigkey_create(dns_fixedname_name(&keyname),
|
|
DNS_TSIG_HMACMD5_NAME,
|
|
isc_buffer_base(&keybuf),
|
|
isc_buffer_usedlength(&keybuf),
|
|
ISC_FALSE, NULL, 0, 0, mctx, ring,
|
|
&initialkey);
|
|
CHECK("dns_tsigkey_create", result);
|
|
|
|
query = NULL;
|
|
result = dns_message_create(mctx, DNS_MESSAGE_INTENTRENDER, &query);
|
|
CHECK("dns_message_create", result);
|
|
|
|
result = dns_tkey_builddhquery(query, ourkey,
|
|
dns_fixedname_name(&ownername),
|
|
DNS_TSIG_HMACMD5_NAME, &nonce, 3600);
|
|
CHECK("dns_tkey_builddhquery", result);
|
|
|
|
request = NULL;
|
|
result = dns_request_create(requestmgr, query, &address,
|
|
DNS_REQUESTOPT_TCP, initialkey,
|
|
TIMEOUT, task, recvquery, query,
|
|
&request);
|
|
CHECK("dns_request_create", result);
|
|
#else
|
|
UNUSED(task);
|
|
|
|
isc_event_free(&event);
|
|
CHECK("MD5 was disabled", ISC_R_NOTIMPLEMENTED);
|
|
#endif
|
|
}
|
|
|
|
int
|
|
main(int argc, char *argv[]) {
|
|
char *ourkeyname;
|
|
isc_taskmgr_t *taskmgr;
|
|
isc_timermgr_t *timermgr;
|
|
isc_socketmgr_t *socketmgr;
|
|
isc_socket_t *sock;
|
|
unsigned int attrs, attrmask;
|
|
isc_sockaddr_t bind_any;
|
|
dns_dispatchmgr_t *dispatchmgr;
|
|
dns_dispatch_t *dispatchv4;
|
|
dns_view_t *view;
|
|
dns_tkeyctx_t *tctx;
|
|
isc_log_t *log;
|
|
isc_logconfig_t *logconfig;
|
|
isc_task_t *task;
|
|
isc_result_t result;
|
|
int type;
|
|
|
|
RUNCHECK(isc_app_start());
|
|
|
|
if (argc < 2) {
|
|
fprintf(stderr, "I:no DH key provided\n");
|
|
exit(-1);
|
|
}
|
|
if (strcmp(argv[1], "-r") == 0) {
|
|
fprintf(stderr, "I:the -r option has been deprecated\n");
|
|
exit(-1);
|
|
}
|
|
ourkeyname = argv[1];
|
|
|
|
if (argc >= 3)
|
|
ownername_str = argv[2];
|
|
|
|
dns_result_register();
|
|
|
|
mctx = NULL;
|
|
isc_mem_debugging = ISC_MEM_DEBUGRECORD;
|
|
RUNCHECK(isc_mem_create(0, 0, &mctx));
|
|
|
|
log = NULL;
|
|
logconfig = NULL;
|
|
RUNCHECK(isc_log_create(mctx, &log, &logconfig));
|
|
|
|
RUNCHECK(dst_lib_init(mctx, NULL));
|
|
|
|
taskmgr = NULL;
|
|
RUNCHECK(isc_taskmgr_create(mctx, 1, 0, &taskmgr));
|
|
task = NULL;
|
|
RUNCHECK(isc_task_create(taskmgr, 0, &task));
|
|
timermgr = NULL;
|
|
RUNCHECK(isc_timermgr_create(mctx, &timermgr));
|
|
socketmgr = NULL;
|
|
RUNCHECK(isc_socketmgr_create(mctx, &socketmgr));
|
|
dispatchmgr = NULL;
|
|
RUNCHECK(dns_dispatchmgr_create(mctx, &dispatchmgr));
|
|
isc_sockaddr_any(&bind_any);
|
|
attrs = DNS_DISPATCHATTR_UDP |
|
|
DNS_DISPATCHATTR_MAKEQUERY |
|
|
DNS_DISPATCHATTR_IPV4;
|
|
attrmask = DNS_DISPATCHATTR_UDP |
|
|
DNS_DISPATCHATTR_TCP |
|
|
DNS_DISPATCHATTR_IPV4 |
|
|
DNS_DISPATCHATTR_IPV6;
|
|
dispatchv4 = NULL;
|
|
RUNCHECK(dns_dispatch_getudp(dispatchmgr, socketmgr, taskmgr,
|
|
&bind_any, 4096, 4, 2, 3, 5,
|
|
attrs, attrmask, &dispatchv4));
|
|
requestmgr = NULL;
|
|
RUNCHECK(dns_requestmgr_create(mctx, timermgr, socketmgr, taskmgr,
|
|
dispatchmgr, dispatchv4, NULL,
|
|
&requestmgr));
|
|
|
|
ring = NULL;
|
|
RUNCHECK(dns_tsigkeyring_create(mctx, &ring));
|
|
tctx = NULL;
|
|
RUNCHECK(dns_tkeyctx_create(mctx, &tctx));
|
|
|
|
view = NULL;
|
|
RUNCHECK(dns_view_create(mctx, 0, "_test", &view));
|
|
dns_view_setkeyring(view, ring);
|
|
dns_tsigkeyring_detach(&ring);
|
|
|
|
sock = NULL;
|
|
RUNCHECK(isc_socket_create(socketmgr, PF_INET, isc_sockettype_udp,
|
|
&sock));
|
|
|
|
RUNCHECK(isc_app_onrun(mctx, task, sendquery, NULL));
|
|
|
|
ourkey = NULL;
|
|
type = DST_TYPE_PUBLIC | DST_TYPE_PRIVATE | DST_TYPE_KEY;
|
|
result = dst_key_fromnamedfile(ourkeyname, NULL, type, mctx, &ourkey);
|
|
CHECK("dst_key_fromnamedfile", result);
|
|
|
|
isc_buffer_init(&nonce, noncedata, sizeof(noncedata));
|
|
isc_nonce_buf(noncedata, sizeof(noncedata));
|
|
isc_buffer_add(&nonce, sizeof(noncedata));
|
|
|
|
(void)isc_app_run();
|
|
|
|
dns_requestmgr_shutdown(requestmgr);
|
|
dns_requestmgr_detach(&requestmgr);
|
|
dns_dispatch_detach(&dispatchv4);
|
|
dns_dispatchmgr_destroy(&dispatchmgr);
|
|
isc_task_shutdown(task);
|
|
isc_task_detach(&task);
|
|
isc_taskmgr_destroy(&taskmgr);
|
|
isc_socket_detach(&sock);
|
|
isc_socketmgr_destroy(&socketmgr);
|
|
isc_timermgr_destroy(&timermgr);
|
|
|
|
dst_key_free(&ourkey);
|
|
dns_tsigkey_detach(&initialkey);
|
|
dns_tsigkey_detach(&tsigkey);
|
|
|
|
dns_tkeyctx_destroy(&tctx);
|
|
|
|
dns_view_detach(&view);
|
|
|
|
isc_log_destroy(&log);
|
|
|
|
dst_lib_destroy();
|
|
|
|
isc_mem_destroy(&mctx);
|
|
|
|
isc_app_finish();
|
|
|
|
return (0);
|
|
}
|