2017-07-07 23:19:05 +10:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2017-07-07 23:19:05 +10:00
|
|
|
*
|
|
|
|
* 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
|
2020-09-14 16:20:40 -07:00
|
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
2017-07-07 23:19:05 +10:00
|
|
|
*/
|
|
|
|
|
2018-11-14 20:29:40 +08:00
|
|
|
#if HAVE_CMOCKA
|
|
|
|
|
Include <sched.h> where necessary for musl libc
All unit tests define the UNIT_TESTING macro, which causes <cmocka.h> to
replace malloc(), calloc(), realloc(), and free() with its own functions
tracking memory allocations. In order for this not to break
compilation, the system header declaring the prototypes for these
standard functions must be included before <cmocka.h>.
Normally, these prototypes are only present in <stdlib.h>, so we make
sure it is included before <cmocka.h>. However, musl libc also defines
the prototypes for calloc() and free() in <sched.h>, which is included
by <pthread.h>, which is included e.g. by <isc/mutex.h>. Thus, unit
tests including "dnstest.h" (which includes <isc/mem.h>, which includes
<isc/mutex.h>) after <cmocka.h> will not compile with musl libc as for
these programs, <sched.h> will be included after <cmocka.h>.
Always including <cmocka.h> after all other header files is not a
feasible solution as that causes the mock assertion macros defined in
<isc/util.h> to mangle the contents of <cmocka.h>, thus breaking
compilation. We cannot really use the __noreturn__ or analyzer_noreturn
attributes with cmocka assertion functions because they do return if the
tested condition is true. The problem is that what BIND unit tests do
is incompatible with Clang Static Analyzer's assumptions: since we use
cmocka, our custom assertion handlers are present in a shared library
(i.e. it is the cmocka library that checks the assertion condition, not
a macro in unit test code). Redefining cmocka's assertion macros in
<isc/util.h> is an ugly hack to overcome that problem - unfortunately,
this is the only way we can think of to make Clang Static Analyzer
properly process unit test code. Giving up on Clang Static Analyzer
being able to properly process unit test code is not a satisfactory
solution.
Undefining _GNU_SOURCE for unit test code could work around the problem
(musl libc's <sched.h> only defines the prototypes for calloc() and
free() when _GNU_SOURCE is defined), but doing that could introduce
discrepancies for unit tests including entire *.c files, so it is also
not a good solution.
All in all, including <sched.h> before <cmocka.h> for all affected unit
tests seems to be the most benign way of working around this musl libc
quirk. While quite an ugly solution, it achieves our goals here, which
are to keep the benefit of proper static analysis of unit test code and
to fix compilation against musl libc.
2019-07-30 21:08:40 +02:00
|
|
|
#include <sched.h> /* IWYU pragma: keep */
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <setjmp.h>
|
|
|
|
#include <stdarg.h>
|
2018-04-17 08:29:14 -07:00
|
|
|
#include <stdbool.h>
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <stddef.h>
|
Include <sched.h> where necessary for musl libc
All unit tests define the UNIT_TESTING macro, which causes <cmocka.h> to
replace malloc(), calloc(), realloc(), and free() with its own functions
tracking memory allocations. In order for this not to break
compilation, the system header declaring the prototypes for these
standard functions must be included before <cmocka.h>.
Normally, these prototypes are only present in <stdlib.h>, so we make
sure it is included before <cmocka.h>. However, musl libc also defines
the prototypes for calloc() and free() in <sched.h>, which is included
by <pthread.h>, which is included e.g. by <isc/mutex.h>. Thus, unit
tests including "dnstest.h" (which includes <isc/mem.h>, which includes
<isc/mutex.h>) after <cmocka.h> will not compile with musl libc as for
these programs, <sched.h> will be included after <cmocka.h>.
Always including <cmocka.h> after all other header files is not a
feasible solution as that causes the mock assertion macros defined in
<isc/util.h> to mangle the contents of <cmocka.h>, thus breaking
compilation. We cannot really use the __noreturn__ or analyzer_noreturn
attributes with cmocka assertion functions because they do return if the
tested condition is true. The problem is that what BIND unit tests do
is incompatible with Clang Static Analyzer's assumptions: since we use
cmocka, our custom assertion handlers are present in a shared library
(i.e. it is the cmocka library that checks the assertion condition, not
a macro in unit test code). Redefining cmocka's assertion macros in
<isc/util.h> is an ugly hack to overcome that problem - unfortunately,
this is the only way we can think of to make Clang Static Analyzer
properly process unit test code. Giving up on Clang Static Analyzer
being able to properly process unit test code is not a satisfactory
solution.
Undefining _GNU_SOURCE for unit test code could work around the problem
(musl libc's <sched.h> only defines the prototypes for calloc() and
free() when _GNU_SOURCE is defined), but doing that could introduce
discrepancies for unit tests including entire *.c files, so it is also
not a good solution.
All in all, including <sched.h> before <cmocka.h> for all affected unit
tests seems to be the most benign way of working around this musl libc
quirk. While quite an ugly solution, it achieves our goals here, which
are to keep the benefit of proper static analysis of unit test code and
to fix compilation against musl libc.
2019-07-30 21:08:40 +02:00
|
|
|
#include <stdlib.h>
|
2017-09-06 10:57:40 -07:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2018-11-14 20:29:40 +08:00
|
|
|
#define UNIT_TESTING
|
|
|
|
#include <cmocka.h>
|
|
|
|
|
2017-07-07 23:19:05 +10:00
|
|
|
#include <isc/mem.h>
|
2017-07-08 00:47:59 +10:00
|
|
|
#include <isc/print.h>
|
2019-07-30 21:08:40 +02:00
|
|
|
#include <isc/util.h>
|
2017-07-07 23:19:05 +10:00
|
|
|
|
|
|
|
#include <dns/rdatalist.h>
|
|
|
|
#include <dns/rdataset.h>
|
|
|
|
#include <dns/tsig.h>
|
|
|
|
|
2017-09-06 10:57:40 -07:00
|
|
|
#include "../tsig_p.h"
|
2017-07-07 23:19:05 +10:00
|
|
|
#include "dnstest.h"
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#define CHECK(r) \
|
|
|
|
do { \
|
|
|
|
result = (r); \
|
|
|
|
if (result != ISC_R_SUCCESS) { \
|
|
|
|
goto cleanup; \
|
|
|
|
} \
|
2019-06-18 14:56:41 +02:00
|
|
|
} while (0)
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#define TEST_ORIGIN "test"
|
2017-09-06 10:57:40 -07:00
|
|
|
|
2018-11-14 20:29:40 +08:00
|
|
|
static int
|
2020-02-13 14:44:37 -08:00
|
|
|
_setup(void **state) {
|
2018-11-14 20:29:40 +08:00
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
result = dns_test_begin(NULL, false);
|
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2020-02-13 14:44:37 -08:00
|
|
|
_teardown(void **state) {
|
2018-11-14 20:29:40 +08:00
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
dns_test_end();
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
2017-09-06 10:57:40 -07:00
|
|
|
|
2017-07-07 23:19:05 +10:00
|
|
|
static int debug = 0;
|
|
|
|
|
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
add_mac(dst_context_t *tsigctx, isc_buffer_t *buf) {
|
2017-07-07 23:19:05 +10:00
|
|
|
dns_rdata_any_tsig_t tsig;
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_rdata_t rdata = DNS_RDATA_INIT;
|
|
|
|
isc_buffer_t databuf;
|
|
|
|
isc_region_t r;
|
|
|
|
isc_result_t result;
|
|
|
|
unsigned char tsigbuf[1024];
|
2017-07-07 23:19:05 +10:00
|
|
|
|
|
|
|
isc_buffer_usedregion(buf, &r);
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_rdata_fromregion(&rdata, dns_rdataclass_any, dns_rdatatype_tsig,
|
|
|
|
&r);
|
2017-07-07 23:19:05 +10:00
|
|
|
isc_buffer_init(&databuf, tsigbuf, sizeof(tsigbuf));
|
|
|
|
CHECK(dns_rdata_tostruct(&rdata, &tsig, NULL));
|
|
|
|
isc_buffer_putuint16(&databuf, tsig.siglen);
|
|
|
|
isc_buffer_putmem(&databuf, tsig.signature, tsig.siglen);
|
|
|
|
isc_buffer_usedregion(&databuf, &r);
|
|
|
|
result = dst_context_adddata(tsigctx, &r);
|
|
|
|
dns_rdata_freestruct(&tsig);
|
2020-02-12 13:59:18 +01:00
|
|
|
cleanup:
|
2017-07-07 23:19:05 +10:00
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
add_tsig(dst_context_t *tsigctx, dns_tsigkey_t *key, isc_buffer_t *target) {
|
|
|
|
dns_compress_t cctx;
|
2017-07-07 23:19:05 +10:00
|
|
|
dns_rdata_any_tsig_t tsig;
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_rdata_t rdata = DNS_RDATA_INIT;
|
|
|
|
dns_rdatalist_t rdatalist;
|
|
|
|
dns_rdataset_t rdataset;
|
|
|
|
isc_buffer_t *dynbuf = NULL;
|
|
|
|
isc_buffer_t databuf;
|
|
|
|
isc_buffer_t sigbuf;
|
|
|
|
isc_region_t r;
|
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
|
|
|
isc_stdtime_t now;
|
|
|
|
unsigned char tsigbuf[1024];
|
|
|
|
unsigned int count;
|
|
|
|
unsigned int sigsize = 0;
|
|
|
|
bool invalidate_ctx = false;
|
2017-07-07 23:19:05 +10:00
|
|
|
|
2017-09-27 10:27:09 +10:00
|
|
|
memset(&tsig, 0, sizeof(tsig));
|
|
|
|
|
2019-06-18 15:01:43 +02:00
|
|
|
CHECK(dns_compress_init(&cctx, -1, dt_mctx));
|
2018-04-17 08:29:14 -07:00
|
|
|
invalidate_ctx = true;
|
2017-07-07 23:19:05 +10:00
|
|
|
|
2017-09-27 10:27:09 +10:00
|
|
|
tsig.common.rdclass = dns_rdataclass_any;
|
2017-07-07 23:19:05 +10:00
|
|
|
tsig.common.rdtype = dns_rdatatype_tsig;
|
|
|
|
ISC_LINK_INIT(&tsig.common, link);
|
|
|
|
dns_name_init(&tsig.algorithm, NULL);
|
|
|
|
dns_name_clone(key->algorithm, &tsig.algorithm);
|
|
|
|
|
|
|
|
isc_stdtime_get(&now);
|
|
|
|
tsig.timesigned = now;
|
|
|
|
tsig.fudge = DNS_TSIG_FUDGE;
|
|
|
|
tsig.originalid = 50;
|
|
|
|
tsig.error = dns_rcode_noerror;
|
|
|
|
tsig.otherlen = 0;
|
|
|
|
tsig.other = NULL;
|
|
|
|
|
|
|
|
isc_buffer_init(&databuf, tsigbuf, sizeof(tsigbuf));
|
|
|
|
isc_buffer_putuint48(&databuf, tsig.timesigned);
|
|
|
|
isc_buffer_putuint16(&databuf, tsig.fudge);
|
|
|
|
isc_buffer_usedregion(&databuf, &r);
|
|
|
|
CHECK(dst_context_adddata(tsigctx, &r));
|
|
|
|
|
|
|
|
CHECK(dst_key_sigsize(key->key, &sigsize));
|
2019-07-16 15:52:14 +02:00
|
|
|
tsig.signature = isc_mem_get(dt_mctx, sigsize);
|
2017-07-07 23:19:05 +10:00
|
|
|
isc_buffer_init(&sigbuf, tsig.signature, sigsize);
|
|
|
|
CHECK(dst_context_sign(tsigctx, &sigbuf));
|
|
|
|
tsig.siglen = isc_buffer_usedlength(&sigbuf);
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_int_equal(sigsize, tsig.siglen);
|
2017-07-07 23:19:05 +10:00
|
|
|
|
2020-02-02 08:35:46 +01:00
|
|
|
isc_buffer_allocate(dt_mctx, &dynbuf, 512);
|
2017-07-07 23:19:05 +10:00
|
|
|
CHECK(dns_rdata_fromstruct(&rdata, dns_rdataclass_any,
|
|
|
|
dns_rdatatype_tsig, &tsig, dynbuf));
|
|
|
|
dns_rdatalist_init(&rdatalist);
|
|
|
|
rdatalist.rdclass = dns_rdataclass_any;
|
|
|
|
rdatalist.type = dns_rdatatype_tsig;
|
|
|
|
ISC_LIST_APPEND(rdatalist.rdata, &rdata, link);
|
|
|
|
dns_rdataset_init(&rdataset);
|
|
|
|
CHECK(dns_rdatalist_tordataset(&rdatalist, &rdataset));
|
2020-02-12 13:59:18 +01:00
|
|
|
CHECK(dns_rdataset_towire(&rdataset, &key->name, &cctx, target, 0,
|
|
|
|
&count));
|
2017-07-07 23:19:05 +10:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Fixup additional record count.
|
|
|
|
*/
|
2020-02-12 13:59:18 +01:00
|
|
|
((unsigned char *)target->base)[11]++;
|
|
|
|
if (((unsigned char *)target->base)[11] == 0) {
|
|
|
|
((unsigned char *)target->base)[10]++;
|
2018-11-14 20:29:40 +08:00
|
|
|
}
|
2020-02-12 13:59:18 +01:00
|
|
|
cleanup:
|
2018-11-14 20:29:40 +08:00
|
|
|
if (tsig.signature != NULL) {
|
2019-06-18 15:01:43 +02:00
|
|
|
isc_mem_put(dt_mctx, tsig.signature, sigsize);
|
2018-11-14 20:29:40 +08:00
|
|
|
}
|
|
|
|
if (dynbuf != NULL) {
|
2017-07-07 23:19:05 +10:00
|
|
|
isc_buffer_free(&dynbuf);
|
2018-11-14 20:29:40 +08:00
|
|
|
}
|
|
|
|
if (invalidate_ctx) {
|
2017-07-07 23:19:05 +10:00
|
|
|
dns_compress_invalidate(&cctx);
|
2018-11-14 20:29:40 +08:00
|
|
|
}
|
2017-07-07 23:19:05 +10:00
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
printmessage(dns_message_t *msg) {
|
2017-07-07 23:19:05 +10:00
|
|
|
isc_buffer_t b;
|
2020-02-13 14:44:37 -08:00
|
|
|
char *buf = NULL;
|
|
|
|
int len = 1024;
|
2017-07-07 23:19:05 +10:00
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
|
|
|
|
2018-11-14 20:29:40 +08:00
|
|
|
if (!debug) {
|
2017-07-07 23:19:05 +10:00
|
|
|
return;
|
2018-11-14 20:29:40 +08:00
|
|
|
}
|
2017-07-07 23:19:05 +10:00
|
|
|
|
|
|
|
do {
|
2019-06-18 15:01:43 +02:00
|
|
|
buf = isc_mem_get(dt_mctx, len);
|
2017-07-07 23:19:05 +10:00
|
|
|
|
|
|
|
isc_buffer_init(&b, buf, len);
|
2020-02-13 14:44:37 -08:00
|
|
|
result = dns_message_totext(msg, &dns_master_style_debug, 0,
|
|
|
|
&b);
|
2017-07-07 23:19:05 +10:00
|
|
|
if (result == ISC_R_NOSPACE) {
|
2019-06-18 15:01:43 +02:00
|
|
|
isc_mem_put(dt_mctx, buf, len);
|
2017-07-07 23:19:05 +10:00
|
|
|
len *= 2;
|
2018-11-14 20:29:40 +08:00
|
|
|
} else if (result == ISC_R_SUCCESS) {
|
2020-02-12 13:59:18 +01:00
|
|
|
printf("%.*s\n", (int)isc_buffer_usedlength(&b), buf);
|
2018-11-14 20:29:40 +08:00
|
|
|
}
|
2017-07-07 23:19:05 +10:00
|
|
|
} while (result == ISC_R_NOSPACE);
|
|
|
|
|
2018-11-14 20:29:40 +08:00
|
|
|
if (buf != NULL) {
|
2019-06-18 15:01:43 +02:00
|
|
|
isc_mem_put(dt_mctx, buf, len);
|
2018-11-14 20:29:40 +08:00
|
|
|
}
|
2017-07-07 23:19:05 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
render(isc_buffer_t *buf, unsigned flags, dns_tsigkey_t *key,
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_buffer_t **tsigin, isc_buffer_t **tsigout, dst_context_t *tsigctx) {
|
2017-07-07 23:19:05 +10:00
|
|
|
dns_message_t *msg = NULL;
|
|
|
|
dns_compress_t cctx;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_result_t result;
|
2017-07-07 23:19:05 +10:00
|
|
|
|
2020-09-25 11:51:36 +02:00
|
|
|
dns_message_create(dt_mctx, DNS_MESSAGE_INTENTRENDER, &msg);
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_non_null(msg);
|
2017-07-07 23:19:05 +10:00
|
|
|
|
|
|
|
msg->id = 50;
|
|
|
|
msg->rcode = dns_rcode_noerror;
|
|
|
|
msg->flags = flags;
|
|
|
|
|
2017-08-08 19:43:39 +05:30
|
|
|
/*
|
|
|
|
* XXXMPA: this hack needs to be replaced with use of
|
|
|
|
* dns_message_reply() at some point.
|
|
|
|
*/
|
2018-11-14 20:29:40 +08:00
|
|
|
if ((flags & DNS_MESSAGEFLAG_QR) != 0) {
|
2017-08-08 19:43:39 +05:30
|
|
|
msg->verified_sig = 1;
|
2018-11-14 20:29:40 +08:00
|
|
|
}
|
2017-08-08 19:43:39 +05:30
|
|
|
|
2018-11-14 20:29:40 +08:00
|
|
|
if (tsigin == tsigout) {
|
2017-07-07 23:19:05 +10:00
|
|
|
msg->tcp_continuation = 1;
|
2018-11-14 20:29:40 +08:00
|
|
|
}
|
2017-07-07 23:19:05 +10:00
|
|
|
|
|
|
|
if (tsigctx == NULL) {
|
|
|
|
result = dns_message_settsigkey(msg, key);
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-07-07 23:19:05 +10:00
|
|
|
|
|
|
|
result = dns_message_setquerytsig(msg, *tsigin);
|
2020-02-12 13:59:18 +01:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-07-07 23:19:05 +10:00
|
|
|
}
|
|
|
|
|
2019-06-18 15:01:43 +02:00
|
|
|
result = dns_compress_init(&cctx, -1, dt_mctx);
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-07-07 23:19:05 +10:00
|
|
|
|
|
|
|
result = dns_message_renderbegin(msg, &cctx, buf);
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-07-07 23:19:05 +10:00
|
|
|
|
|
|
|
result = dns_message_renderend(msg);
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-07-07 23:19:05 +10:00
|
|
|
|
|
|
|
if (tsigctx != NULL) {
|
|
|
|
isc_region_t r;
|
|
|
|
|
|
|
|
isc_buffer_usedregion(buf, &r);
|
|
|
|
result = dst_context_adddata(tsigctx, &r);
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-07-07 23:19:05 +10:00
|
|
|
} else {
|
2018-11-14 20:29:40 +08:00
|
|
|
if (tsigin == tsigout && *tsigin != NULL) {
|
2017-07-07 23:19:05 +10:00
|
|
|
isc_buffer_free(tsigin);
|
2018-11-14 20:29:40 +08:00
|
|
|
}
|
2017-07-07 23:19:05 +10:00
|
|
|
|
2019-06-18 15:01:43 +02:00
|
|
|
result = dns_message_getquerytsig(msg, dt_mctx, tsigout);
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-07-07 23:19:05 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
dns_compress_invalidate(&cctx);
|
2020-09-21 16:16:15 -03:00
|
|
|
dns_message_detach(&msg);
|
2017-07-07 23:19:05 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-11-14 20:29:40 +08:00
|
|
|
* Test tsig tcp-continuation validation:
|
2017-07-07 23:19:05 +10:00
|
|
|
* Check that a simulated three message TCP sequence where the first
|
|
|
|
* and last messages contain TSIGs but the intermediate message doesn't
|
|
|
|
* correctly verifies.
|
|
|
|
*/
|
2018-11-14 20:29:40 +08:00
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
tsig_tcp_test(void **state) {
|
|
|
|
const dns_name_t *tsigowner = NULL;
|
|
|
|
dns_fixedname_t fkeyname;
|
|
|
|
dns_message_t *msg = NULL;
|
|
|
|
dns_name_t *keyname;
|
2017-07-07 23:19:05 +10:00
|
|
|
dns_tsig_keyring_t *ring = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_tsigkey_t *key = NULL;
|
|
|
|
isc_buffer_t *buf = NULL;
|
|
|
|
isc_buffer_t *querytsig = NULL;
|
|
|
|
isc_buffer_t *tsigin = NULL;
|
|
|
|
isc_buffer_t *tsigout = NULL;
|
|
|
|
isc_result_t result;
|
|
|
|
unsigned char secret[16] = { 0 };
|
|
|
|
dst_context_t *tsigctx = NULL;
|
|
|
|
dst_context_t *outctx = NULL;
|
2017-07-07 23:19:05 +10:00
|
|
|
|
2018-11-14 20:29:40 +08:00
|
|
|
UNUSED(state);
|
2017-07-07 23:19:05 +10:00
|
|
|
|
|
|
|
/* isc_log_setdebuglevel(lctx, 99); */
|
|
|
|
|
2018-03-28 14:38:09 +02:00
|
|
|
keyname = dns_fixedname_initname(&fkeyname);
|
2017-07-07 23:19:05 +10:00
|
|
|
result = dns_name_fromstring(keyname, "test", 0, NULL);
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-07-07 23:19:05 +10:00
|
|
|
|
2019-06-18 15:01:43 +02:00
|
|
|
result = dns_tsigkeyring_create(dt_mctx, &ring);
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-07-07 23:19:05 +10:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
result = dns_tsigkey_create(keyname, dns_tsig_hmacsha256_name, secret,
|
|
|
|
sizeof(secret), false, NULL, 0, 0, dt_mctx,
|
|
|
|
ring, &key);
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
assert_non_null(key);
|
2017-07-07 23:19:05 +10:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create request.
|
|
|
|
*/
|
2020-02-02 08:35:46 +01:00
|
|
|
isc_buffer_allocate(dt_mctx, &buf, 65535);
|
2017-07-07 23:19:05 +10:00
|
|
|
render(buf, 0, key, &tsigout, &querytsig, NULL);
|
|
|
|
isc_buffer_free(&buf);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create response message 1.
|
|
|
|
*/
|
2020-02-02 08:35:46 +01:00
|
|
|
isc_buffer_allocate(dt_mctx, &buf, 65535);
|
2017-07-07 23:19:05 +10:00
|
|
|
render(buf, DNS_MESSAGEFLAG_QR, key, &querytsig, &tsigout, NULL);
|
2020-07-07 19:12:35 +10:00
|
|
|
assert_non_null(tsigout);
|
2017-07-07 23:19:05 +10:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Process response message 1.
|
|
|
|
*/
|
2020-09-25 11:51:36 +02:00
|
|
|
dns_message_create(dt_mctx, DNS_MESSAGE_INTENTPARSE, &msg);
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_non_null(msg);
|
2017-07-07 23:19:05 +10:00
|
|
|
|
|
|
|
result = dns_message_settsigkey(msg, key);
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-07-07 23:19:05 +10:00
|
|
|
|
|
|
|
result = dns_message_parse(msg, buf, 0);
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-07-07 23:19:05 +10:00
|
|
|
|
|
|
|
printmessage(msg);
|
|
|
|
|
|
|
|
result = dns_message_setquerytsig(msg, querytsig);
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-07-07 23:19:05 +10:00
|
|
|
|
|
|
|
result = dns_tsig_verify(buf, msg, NULL, NULL);
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
assert_int_equal(msg->verified_sig, 1);
|
|
|
|
assert_int_equal(msg->tsigstatus, dns_rcode_noerror);
|
2017-07-07 23:19:05 +10:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check that we have a TSIG in the first message.
|
|
|
|
*/
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_non_null(dns_message_gettsig(msg, &tsigowner));
|
2017-07-07 23:19:05 +10:00
|
|
|
|
2019-06-18 15:01:43 +02:00
|
|
|
result = dns_message_getquerytsig(msg, dt_mctx, &tsigin);
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-07-07 23:19:05 +10:00
|
|
|
|
|
|
|
tsigctx = msg->tsigctx;
|
|
|
|
msg->tsigctx = NULL;
|
|
|
|
isc_buffer_free(&buf);
|
2020-09-21 16:16:15 -03:00
|
|
|
dns_message_detach(&msg);
|
2017-07-07 23:19:05 +10:00
|
|
|
|
2019-06-18 15:01:43 +02:00
|
|
|
result = dst_context_create(key->key, dt_mctx, DNS_LOGCATEGORY_DNSSEC,
|
2018-04-17 08:29:14 -07:00
|
|
|
false, 0, &outctx);
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
assert_non_null(outctx);
|
2017-07-07 23:19:05 +10:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Start digesting.
|
|
|
|
*/
|
|
|
|
result = add_mac(outctx, tsigout);
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-07-07 23:19:05 +10:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create response message 2.
|
|
|
|
*/
|
2020-02-02 08:35:46 +01:00
|
|
|
isc_buffer_allocate(dt_mctx, &buf, 65535);
|
2017-07-07 23:19:05 +10:00
|
|
|
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-07-07 23:19:05 +10:00
|
|
|
render(buf, DNS_MESSAGEFLAG_QR, key, &tsigout, &tsigout, outctx);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process response message 2.
|
|
|
|
*/
|
2020-09-25 11:51:36 +02:00
|
|
|
dns_message_create(dt_mctx, DNS_MESSAGE_INTENTPARSE, &msg);
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_non_null(msg);
|
2017-07-07 23:19:05 +10:00
|
|
|
|
|
|
|
msg->tcp_continuation = 1;
|
|
|
|
msg->tsigctx = tsigctx;
|
|
|
|
tsigctx = NULL;
|
|
|
|
|
|
|
|
result = dns_message_settsigkey(msg, key);
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-07-07 23:19:05 +10:00
|
|
|
|
|
|
|
result = dns_message_parse(msg, buf, 0);
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-07-07 23:19:05 +10:00
|
|
|
|
|
|
|
printmessage(msg);
|
|
|
|
|
|
|
|
result = dns_message_setquerytsig(msg, tsigin);
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-07-07 23:19:05 +10:00
|
|
|
|
|
|
|
result = dns_tsig_verify(buf, msg, NULL, NULL);
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
assert_int_equal(msg->verified_sig, 0);
|
|
|
|
assert_int_equal(msg->tsigstatus, dns_rcode_noerror);
|
2017-07-07 23:19:05 +10:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check that we don't have a TSIG in the second message.
|
|
|
|
*/
|
|
|
|
tsigowner = NULL;
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_true(dns_message_gettsig(msg, &tsigowner) == NULL);
|
2017-07-07 23:19:05 +10:00
|
|
|
|
|
|
|
tsigctx = msg->tsigctx;
|
|
|
|
msg->tsigctx = NULL;
|
|
|
|
isc_buffer_free(&buf);
|
2020-09-21 16:16:15 -03:00
|
|
|
dns_message_detach(&msg);
|
2017-07-07 23:19:05 +10:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create response message 3.
|
|
|
|
*/
|
2020-02-02 08:35:46 +01:00
|
|
|
isc_buffer_allocate(dt_mctx, &buf, 65535);
|
2017-07-07 23:19:05 +10:00
|
|
|
render(buf, DNS_MESSAGEFLAG_QR, key, &tsigout, &tsigout, outctx);
|
|
|
|
|
|
|
|
result = add_tsig(outctx, key, buf);
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-07-07 23:19:05 +10:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Process response message 3.
|
|
|
|
*/
|
2020-09-25 11:51:36 +02:00
|
|
|
dns_message_create(dt_mctx, DNS_MESSAGE_INTENTPARSE, &msg);
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_non_null(msg);
|
2017-07-07 23:19:05 +10:00
|
|
|
|
|
|
|
msg->tcp_continuation = 1;
|
|
|
|
msg->tsigctx = tsigctx;
|
|
|
|
tsigctx = NULL;
|
|
|
|
|
|
|
|
result = dns_message_settsigkey(msg, key);
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-07-07 23:19:05 +10:00
|
|
|
|
|
|
|
result = dns_message_parse(msg, buf, 0);
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-07-07 23:19:05 +10:00
|
|
|
|
|
|
|
printmessage(msg);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check that we had a TSIG in the third message.
|
|
|
|
*/
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_non_null(dns_message_gettsig(msg, &tsigowner));
|
2017-07-07 23:19:05 +10:00
|
|
|
|
|
|
|
result = dns_message_setquerytsig(msg, tsigin);
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-07-07 23:19:05 +10:00
|
|
|
|
|
|
|
result = dns_tsig_verify(buf, msg, NULL, NULL);
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
assert_int_equal(msg->verified_sig, 1);
|
|
|
|
assert_int_equal(msg->tsigstatus, dns_rcode_noerror);
|
2017-07-07 23:19:05 +10:00
|
|
|
|
2018-11-14 20:29:40 +08:00
|
|
|
if (tsigin != NULL) {
|
2017-07-07 23:19:05 +10:00
|
|
|
isc_buffer_free(&tsigin);
|
2018-11-14 20:29:40 +08:00
|
|
|
}
|
2017-07-07 23:19:05 +10:00
|
|
|
|
2019-06-18 15:01:43 +02:00
|
|
|
result = dns_message_getquerytsig(msg, dt_mctx, &tsigin);
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-07-07 23:19:05 +10:00
|
|
|
|
|
|
|
isc_buffer_free(&buf);
|
2020-09-21 16:16:15 -03:00
|
|
|
dns_message_detach(&msg);
|
2017-07-07 23:19:05 +10:00
|
|
|
|
2018-11-14 20:29:40 +08:00
|
|
|
if (outctx != NULL) {
|
2017-07-07 23:19:05 +10:00
|
|
|
dst_context_destroy(&outctx);
|
2018-11-14 20:29:40 +08:00
|
|
|
}
|
|
|
|
if (querytsig != NULL) {
|
2017-07-07 23:19:05 +10:00
|
|
|
isc_buffer_free(&querytsig);
|
2018-11-14 20:29:40 +08:00
|
|
|
}
|
|
|
|
if (tsigin != NULL) {
|
2017-07-07 23:19:05 +10:00
|
|
|
isc_buffer_free(&tsigin);
|
2018-11-14 20:29:40 +08:00
|
|
|
}
|
|
|
|
if (tsigout != NULL) {
|
2017-07-07 23:19:05 +10:00
|
|
|
isc_buffer_free(&tsigout);
|
2018-11-14 20:29:40 +08:00
|
|
|
}
|
2017-12-12 13:20:56 +11:00
|
|
|
dns_tsigkey_detach(&key);
|
2018-11-14 20:29:40 +08:00
|
|
|
if (ring != NULL) {
|
2017-07-07 23:19:05 +10:00
|
|
|
dns_tsigkeyring_detach(&ring);
|
2018-11-14 20:29:40 +08:00
|
|
|
}
|
2017-07-07 23:19:05 +10:00
|
|
|
}
|
|
|
|
|
2018-11-14 20:29:40 +08:00
|
|
|
/* Tests the dns__tsig_algvalid function */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
algvalid_test(void **state) {
|
2018-11-14 20:29:40 +08:00
|
|
|
UNUSED(state);
|
2017-09-06 10:57:40 -07:00
|
|
|
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_true(dns__tsig_algvalid(DST_ALG_HMACMD5));
|
2017-09-06 10:57:40 -07:00
|
|
|
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_true(dns__tsig_algvalid(DST_ALG_HMACSHA1));
|
|
|
|
assert_true(dns__tsig_algvalid(DST_ALG_HMACSHA224));
|
|
|
|
assert_true(dns__tsig_algvalid(DST_ALG_HMACSHA256));
|
|
|
|
assert_true(dns__tsig_algvalid(DST_ALG_HMACSHA384));
|
|
|
|
assert_true(dns__tsig_algvalid(DST_ALG_HMACSHA512));
|
2017-09-06 10:57:40 -07:00
|
|
|
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_false(dns__tsig_algvalid(DST_ALG_GSSAPI));
|
2017-09-06 10:57:40 -07:00
|
|
|
}
|
|
|
|
|
2018-11-14 20:29:40 +08:00
|
|
|
/* Tests the dns__tsig_algfromname function */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
algfromname_test(void **state) {
|
2018-11-14 20:29:40 +08:00
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
assert_int_equal(dns__tsig_algfromname(DNS_TSIG_HMACMD5_NAME),
|
|
|
|
DST_ALG_HMACMD5);
|
|
|
|
assert_int_equal(dns__tsig_algfromname(DNS_TSIG_HMACSHA1_NAME),
|
|
|
|
DST_ALG_HMACSHA1);
|
|
|
|
assert_int_equal(dns__tsig_algfromname(DNS_TSIG_HMACSHA224_NAME),
|
|
|
|
DST_ALG_HMACSHA224);
|
|
|
|
assert_int_equal(dns__tsig_algfromname(DNS_TSIG_HMACSHA256_NAME),
|
|
|
|
DST_ALG_HMACSHA256);
|
|
|
|
assert_int_equal(dns__tsig_algfromname(DNS_TSIG_HMACSHA384_NAME),
|
|
|
|
DST_ALG_HMACSHA384);
|
|
|
|
assert_int_equal(dns__tsig_algfromname(DNS_TSIG_HMACSHA512_NAME),
|
|
|
|
DST_ALG_HMACSHA512);
|
|
|
|
|
|
|
|
assert_int_equal(dns__tsig_algfromname(DNS_TSIG_GSSAPI_NAME),
|
|
|
|
DST_ALG_GSSAPI);
|
|
|
|
assert_int_equal(dns__tsig_algfromname(DNS_TSIG_GSSAPIMS_NAME),
|
|
|
|
DST_ALG_GSSAPI);
|
|
|
|
|
|
|
|
assert_int_equal(dns__tsig_algfromname(dns_rootname), 0);
|
2017-09-06 10:57:40 -07:00
|
|
|
}
|
|
|
|
|
2018-11-14 20:29:40 +08:00
|
|
|
/* Tests the dns__tsig_algnamefromname function */
|
2017-09-06 10:57:40 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper function to create a dns_name_t from a string and see if
|
|
|
|
* the dns__tsig_algnamefromname function can correctly match it against the
|
|
|
|
* static table of known algorithms.
|
|
|
|
*/
|
2020-02-12 13:59:18 +01:00
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
test_name(const char *name_string, const dns_name_t *expected) {
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_name_t name;
|
2017-09-06 10:57:40 -07:00
|
|
|
dns_name_init(&name, NULL);
|
2019-06-18 15:01:43 +02:00
|
|
|
assert_int_equal(dns_name_fromstring(&name, name_string, 0, dt_mctx),
|
2018-11-14 20:29:40 +08:00
|
|
|
ISC_R_SUCCESS);
|
2020-11-26 13:10:40 +01:00
|
|
|
assert_ptr_equal(dns__tsig_algnamefromname(&name), expected);
|
2019-06-18 15:01:43 +02:00
|
|
|
dns_name_free(&name, dt_mctx);
|
2017-09-06 10:57:40 -07:00
|
|
|
}
|
|
|
|
|
2018-11-14 20:29:40 +08:00
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
algnamefromname_test(void **state) {
|
2018-11-14 20:29:40 +08:00
|
|
|
UNUSED(state);
|
2017-09-06 10:57:40 -07:00
|
|
|
|
|
|
|
/* test the standard algorithms */
|
|
|
|
test_name("hmac-md5.sig-alg.reg.int", DNS_TSIG_HMACMD5_NAME);
|
|
|
|
test_name("hmac-sha1", DNS_TSIG_HMACSHA1_NAME);
|
|
|
|
test_name("hmac-sha224", DNS_TSIG_HMACSHA224_NAME);
|
|
|
|
test_name("hmac-sha256", DNS_TSIG_HMACSHA256_NAME);
|
|
|
|
test_name("hmac-sha384", DNS_TSIG_HMACSHA384_NAME);
|
|
|
|
test_name("hmac-sha512", DNS_TSIG_HMACSHA512_NAME);
|
|
|
|
|
|
|
|
test_name("gss-tsig", DNS_TSIG_GSSAPI_NAME);
|
|
|
|
test_name("gss.microsoft.com", DNS_TSIG_GSSAPIMS_NAME);
|
|
|
|
|
|
|
|
/* try another name that isn't a standard algorithm name */
|
2020-11-26 13:10:40 +01:00
|
|
|
assert_null(dns__tsig_algnamefromname(dns_rootname));
|
2017-09-06 10:57:40 -07:00
|
|
|
}
|
|
|
|
|
2018-11-14 20:29:40 +08:00
|
|
|
/* Tests the dns__tsig_algallocated function */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
algallocated_test(void **state) {
|
2018-11-14 20:29:40 +08:00
|
|
|
UNUSED(state);
|
2017-09-06 10:57:40 -07:00
|
|
|
|
|
|
|
/* test the standard algorithms */
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_false(dns__tsig_algallocated(DNS_TSIG_HMACMD5_NAME));
|
|
|
|
assert_false(dns__tsig_algallocated(DNS_TSIG_HMACSHA1_NAME));
|
|
|
|
assert_false(dns__tsig_algallocated(DNS_TSIG_HMACSHA224_NAME));
|
|
|
|
assert_false(dns__tsig_algallocated(DNS_TSIG_HMACSHA256_NAME));
|
|
|
|
assert_false(dns__tsig_algallocated(DNS_TSIG_HMACSHA384_NAME));
|
|
|
|
assert_false(dns__tsig_algallocated(DNS_TSIG_HMACSHA512_NAME));
|
2017-09-06 10:57:40 -07:00
|
|
|
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_false(dns__tsig_algallocated(DNS_TSIG_HMACSHA512_NAME));
|
|
|
|
assert_false(dns__tsig_algallocated(DNS_TSIG_HMACSHA512_NAME));
|
2017-09-06 10:57:40 -07:00
|
|
|
|
|
|
|
/* try another name that isn't a standard algorithm name */
|
2018-11-14 20:29:40 +08:00
|
|
|
assert_true(dns__tsig_algallocated(dns_rootname));
|
2017-09-06 10:57:40 -07:00
|
|
|
}
|
|
|
|
|
2018-11-14 20:29:40 +08:00
|
|
|
int
|
2020-02-13 14:44:37 -08:00
|
|
|
main(void) {
|
2018-11-14 20:29:40 +08:00
|
|
|
const struct CMUnitTest tests[] = {
|
2020-02-12 13:59:18 +01:00
|
|
|
cmocka_unit_test_setup_teardown(tsig_tcp_test, _setup,
|
|
|
|
_teardown),
|
2018-11-14 20:29:40 +08:00
|
|
|
cmocka_unit_test(algvalid_test),
|
|
|
|
cmocka_unit_test(algfromname_test),
|
2020-02-12 13:59:18 +01:00
|
|
|
cmocka_unit_test_setup_teardown(algnamefromname_test, _setup,
|
|
|
|
_teardown),
|
2018-11-14 20:29:40 +08:00
|
|
|
cmocka_unit_test(algallocated_test),
|
|
|
|
};
|
|
|
|
|
|
|
|
return (cmocka_run_group_tests(tests, NULL, NULL));
|
2017-07-07 23:19:05 +10:00
|
|
|
}
|
2018-11-14 20:29:40 +08:00
|
|
|
|
|
|
|
#else /* HAVE_CMOCKA */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
int
|
2020-02-13 14:44:37 -08:00
|
|
|
main(void) {
|
2018-11-14 20:29:40 +08:00
|
|
|
printf("1..0 # Skipped: cmocka not available\n");
|
2021-01-18 19:15:44 +01:00
|
|
|
return (SKIPPED_TEST_EXIT_CODE);
|
2018-11-14 20:29:40 +08:00
|
|
|
}
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if HAVE_CMOCKA */
|