| 1 |
/* |
| 2 |
* Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MPL-2.0 |
| 5 |
* |
| 6 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 7 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 |
* file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 9 |
* |
| 10 |
* See the COPYRIGHT file distributed with this work for additional |
| 11 |
* information regarding copyright ownership. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <inttypes.h> |
| 15 |
#include <sched.h> /* IWYU pragma: keep */ |
| 16 |
#include <setjmp.h> |
| 17 |
#include <stdarg.h> |
| 18 |
#include <stdbool.h> |
| 19 |
#include <stddef.h> |
| 20 |
#include <stdlib.h> |
| 21 |
#include <unistd.h> |
| 22 |
|
| 23 |
#define UNIT_TESTING |
| 24 |
#include <cmocka.h> |
| 25 |
|
| 26 |
#include <isc/buffer.h> |
| 27 |
#include <isc/util.h> |
| 28 |
|
| 29 |
#include <dns/nsec3.h> |
| 30 |
#include <dns/private.h> |
| 31 |
#include <dns/rdataclass.h> |
| 32 |
#include <dns/rdatatype.h> |
| 33 |
|
| 34 |
#include <dst/dst.h> |
| 35 |
|
| 36 |
#include <tests/dns.h> |
| 37 |
|
| 38 |
static dns_rdatatype_t privatetype = 65534; |
| 39 |
|
| 40 |
static int |
| 41 |
setup_test(void **state) { |
| 42 |
isc_result_t result; |
| 43 |
|
| 44 |
UNUSED(state); |
| 45 |
|
| 46 |
result = dst_lib_init(mctx, NULL); |
| 47 |
|
| 48 |
if (result != ISC_R_SUCCESS) { |
| 49 |
return 1; |
| 50 |
} |
| 51 |
|
| 52 |
return 0; |
| 53 |
} |
| 54 |
|
| 55 |
static int |
| 56 |
teardown_test(void **state) { |
| 57 |
UNUSED(state); |
| 58 |
|
| 59 |
dst_lib_destroy(); |
| 60 |
|
| 61 |
return 0; |
| 62 |
} |
| 63 |
|
| 64 |
typedef struct { |
| 65 |
unsigned char alg; |
| 66 |
dns_keytag_t keyid; |
| 67 |
bool remove; |
| 68 |
bool complete; |
| 69 |
} signing_testcase_t; |
| 70 |
|
| 71 |
typedef struct { |
| 72 |
unsigned char hash; |
| 73 |
unsigned char flags; |
| 74 |
unsigned int iterations; |
| 75 |
unsigned long salt; |
| 76 |
bool remove; |
| 77 |
bool pending; |
| 78 |
bool nonsec; |
| 79 |
} nsec3_testcase_t; |
| 80 |
|
| 81 |
static void |
| 82 |
make_signing(signing_testcase_t *testcase, dns_rdata_t *private, |
| 83 |
unsigned char *buf, size_t len) { |
| 84 |
dns_rdata_init(private); |
| 85 |
|
| 86 |
buf[0] = testcase->alg; |
| 87 |
buf[1] = (testcase->keyid & 0xff00) >> 8; |
| 88 |
buf[2] = (testcase->keyid & 0xff); |
| 89 |
buf[3] = testcase->remove; |
| 90 |
buf[4] = testcase->complete; |
| 91 |
private->data = buf; |
| 92 |
private->length = len; |
| 93 |
private->type = privatetype; |
| 94 |
private->rdclass = dns_rdataclass_in; |
| 95 |
} |
| 96 |
|
| 97 |
static void |
| 98 |
make_nsec3(nsec3_testcase_t *testcase, dns_rdata_t *private, |
| 99 |
unsigned char *pbuf) { |
| 100 |
dns_rdata_nsec3param_t params; |
| 101 |
dns_rdata_t nsec3param = DNS_RDATA_INIT; |
| 102 |
unsigned char bufdata[BUFSIZ]; |
| 103 |
isc_buffer_t buf; |
| 104 |
uint32_t salt; |
| 105 |
unsigned char *sp; |
| 106 |
int slen = 4; |
| 107 |
|
| 108 |
/* for simplicity, we're using a maximum salt length of 4 */ |
| 109 |
salt = htonl(testcase->salt); |
| 110 |
sp = (unsigned char *)&salt; |
| 111 |
while (slen > 0 && *sp == '\0') { |
| 112 |
slen--; |
| 113 |
sp++; |
| 114 |
} |
| 115 |
|
| 116 |
params.common.rdclass = dns_rdataclass_in; |
| 117 |
params.common.rdtype = dns_rdatatype_nsec3param; |
| 118 |
params.hash = testcase->hash; |
| 119 |
params.iterations = testcase->iterations; |
| 120 |
params.salt = sp; |
| 121 |
params.salt_length = slen; |
| 122 |
|
| 123 |
params.flags = testcase->flags; |
| 124 |
if (testcase->remove) { |
| 125 |
params.flags |= DNS_NSEC3FLAG_REMOVE; |
| 126 |
if (testcase->nonsec) { |
| 127 |
params.flags |= DNS_NSEC3FLAG_NONSEC; |
| 128 |
} |
| 129 |
} else { |
| 130 |
params.flags |= DNS_NSEC3FLAG_CREATE; |
| 131 |
if (testcase->pending) { |
| 132 |
params.flags |= DNS_NSEC3FLAG_INITIAL; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
isc_buffer_init(&buf, bufdata, sizeof(bufdata)); |
| 137 |
dns_rdata_fromstruct(&nsec3param, dns_rdataclass_in, |
| 138 |
dns_rdatatype_nsec3param, ¶ms, &buf); |
| 139 |
|
| 140 |
dns_rdata_init(private); |
| 141 |
|
| 142 |
dns_nsec3param_toprivate(&nsec3param, private, privatetype, pbuf, |
| 143 |
DNS_NSEC3PARAM_BUFFERSIZE + 1); |
| 144 |
} |
| 145 |
|
| 146 |
/* convert private signing records to text */ |
| 147 |
ISC_RUN_TEST_IMPL(private_signing_totext) { |
| 148 |
dns_rdata_t private; |
| 149 |
int i; |
| 150 |
|
| 151 |
signing_testcase_t testcases[] = { { DST_ALG_RSASHA512, 12345, 0, 0 }, |
| 152 |
{ DST_ALG_RSASHA256, 54321, 1, 0 }, |
| 153 |
{ DST_ALG_NSEC3RSASHA1, 22222, 0, |
| 154 |
1 }, |
| 155 |
{ DST_ALG_RSASHA1, 33333, 1, 1 } }; |
| 156 |
const char *results[] = { "Signing with key 12345/RSASHA512", |
| 157 |
"Removing signatures for key 54321/RSASHA256", |
| 158 |
"Done signing with key 22222/NSEC3RSASHA1", |
| 159 |
("Done removing signatures for key " |
| 160 |
"33333/RSASHA1") }; |
| 161 |
int ncases = 4; |
| 162 |
|
| 163 |
UNUSED(state); |
| 164 |
|
| 165 |
for (i = 0; i < ncases; i++) { |
| 166 |
unsigned char data[5]; |
| 167 |
char output[BUFSIZ]; |
| 168 |
isc_buffer_t buf; |
| 169 |
|
| 170 |
isc_buffer_init(&buf, output, sizeof(output)); |
| 171 |
|
| 172 |
make_signing(&testcases[i], &private, data, sizeof(data)); |
| 173 |
dns_private_totext(&private, &buf); |
| 174 |
assert_string_equal(output, results[i]); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
/* convert private chain records to text */ |
| 179 |
ISC_RUN_TEST_IMPL(private_nsec3_totext) { |
| 180 |
dns_rdata_t private; |
| 181 |
int i; |
| 182 |
|
| 183 |
nsec3_testcase_t testcases[] = { |
| 184 |
{ 1, 0, 1, 0xbeef, 0, 0, 0 }, |
| 185 |
{ 1, 1, 10, 0xdadd, 0, 0, 0 }, |
| 186 |
{ 1, 0, 20, 0xbead, 0, 1, 0 }, |
| 187 |
{ 1, 0, 30, 0xdeaf, 1, 0, 0 }, |
| 188 |
{ 1, 0, 100, 0xfeedabee, 1, 0, 1 }, |
| 189 |
}; |
| 190 |
const char *results[] = { "Creating NSEC3 chain 1 0 1 BEEF", |
| 191 |
"Creating NSEC3 chain 1 1 10 DADD", |
| 192 |
"Pending NSEC3 chain 1 0 20 BEAD", |
| 193 |
("Removing NSEC3 chain 1 0 30 DEAF / " |
| 194 |
"creating NSEC chain"), |
| 195 |
"Removing NSEC3 chain 1 0 100 FEEDABEE" }; |
| 196 |
int ncases = 5; |
| 197 |
|
| 198 |
UNUSED(state); |
| 199 |
|
| 200 |
for (i = 0; i < ncases; i++) { |
| 201 |
unsigned char data[DNS_NSEC3PARAM_BUFFERSIZE + 1]; |
| 202 |
char output[BUFSIZ]; |
| 203 |
isc_buffer_t buf; |
| 204 |
|
| 205 |
isc_buffer_init(&buf, output, sizeof(output)); |
| 206 |
|
| 207 |
make_nsec3(&testcases[i], &private, data); |
| 208 |
dns_private_totext(&private, &buf); |
| 209 |
assert_string_equal(output, results[i]); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
ISC_TEST_LIST_START |
| 214 |
ISC_TEST_ENTRY_CUSTOM(private_signing_totext, setup_test, teardown_test) |
| 215 |
ISC_TEST_ENTRY_CUSTOM(private_nsec3_totext, setup_test, teardown_test) |
| 216 |
ISC_TEST_LIST_END |
| 217 |
|
| 218 |
ISC_TEST_MAIN |