| 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 <stddef.h> |
| 19 |
#include <stdlib.h> |
| 20 |
#include <string.h> |
| 21 |
#include <unistd.h> |
| 22 |
|
| 23 |
#define UNIT_TESTING |
| 24 |
#include <cmocka.h> |
| 25 |
|
| 26 |
#include <isc/hex.h> |
| 27 |
#include <isc/result.h> |
| 28 |
#include <isc/string.h> |
| 29 |
#include <isc/util.h> |
| 30 |
|
| 31 |
#include <dns/db.h> |
| 32 |
#include <dns/nsec3.h> |
| 33 |
|
| 34 |
#include "zone_p.h" |
| 35 |
|
| 36 |
#include <tests/dns.h> |
| 37 |
|
| 38 |
#define HASH 1 |
| 39 |
#define FLAGS 0 |
| 40 |
#define ITER 5 |
| 41 |
#define SALTLEN 4 |
| 42 |
#define SALT "FEDCBA98" |
| 43 |
|
| 44 |
/*% |
| 45 |
* Structures containing parameters for nsec3param_salttotext_test(). |
| 46 |
*/ |
| 47 |
typedef struct { |
| 48 |
dns_hash_t hash; |
| 49 |
unsigned char flags; |
| 50 |
dns_iterations_t iterations; |
| 51 |
unsigned char salt_length; |
| 52 |
const char *salt; |
| 53 |
} nsec3param_rdata_test_params_t; |
| 54 |
|
| 55 |
typedef struct { |
| 56 |
nsec3param_rdata_test_params_t lookup; |
| 57 |
nsec3param_rdata_test_params_t expect; |
| 58 |
bool resalt; |
| 59 |
isc_result_t expected_result; |
| 60 |
} nsec3param_change_test_params_t; |
| 61 |
|
| 62 |
static void |
| 63 |
decode_salt(const char *string, unsigned char *salt, size_t saltlen) { |
| 64 |
isc_buffer_t buf; |
| 65 |
isc_result_t result; |
| 66 |
|
| 67 |
isc_buffer_init(&buf, salt, saltlen); |
| 68 |
result = isc_hex_decodestring(string, &buf); |
| 69 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 70 |
} |
| 71 |
|
| 72 |
static void |
| 73 |
copy_params(nsec3param_rdata_test_params_t from, dns_rdata_nsec3param_t *to, |
| 74 |
unsigned char *saltbuf, size_t saltlen) { |
| 75 |
to->hash = from.hash; |
| 76 |
to->flags = from.flags; |
| 77 |
to->iterations = from.iterations; |
| 78 |
to->salt_length = from.salt_length; |
| 79 |
if (from.salt == NULL) { |
| 80 |
to->salt = NULL; |
| 81 |
} else if (strcmp(from.salt, "-") == 0) { |
| 82 |
to->salt = (unsigned char *)"-"; |
| 83 |
} else { |
| 84 |
decode_salt(from.salt, saltbuf, saltlen); |
| 85 |
to->salt = saltbuf; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
static nsec3param_rdata_test_params_t |
| 90 |
rdata_fromparams(uint8_t hash, uint8_t flags, uint16_t iter, uint8_t saltlen, |
| 91 |
const char *salt) { |
| 92 |
nsec3param_rdata_test_params_t nsec3param; |
| 93 |
nsec3param.hash = hash; |
| 94 |
nsec3param.flags = flags; |
| 95 |
nsec3param.iterations = iter; |
| 96 |
nsec3param.salt_length = saltlen; |
| 97 |
nsec3param.salt = salt; |
| 98 |
return nsec3param; |
| 99 |
} |
| 100 |
|
| 101 |
/*% |
| 102 |
* Check whether zone_lookup_nsec3param() finds the correct NSEC3PARAM |
| 103 |
* and sets the correct parameters to use in dns_zone_setnsec3param(). |
| 104 |
*/ |
| 105 |
static void |
| 106 |
nsec3param_change_test(const nsec3param_change_test_params_t *test) { |
| 107 |
dns_zone_t *zone = NULL; |
| 108 |
dns_rdata_nsec3param_t param, lookup, expect; |
| 109 |
isc_result_t result; |
| 110 |
unsigned char lookupsalt[255]; |
| 111 |
unsigned char expectsalt[255]; |
| 112 |
unsigned char saltbuf[255]; |
| 113 |
|
| 114 |
/* |
| 115 |
* Prepare a zone along with its signing keys. |
| 116 |
*/ |
| 117 |
result = dns_test_makezone("nsec3", &zone, NULL, false); |
| 118 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 119 |
|
| 120 |
result = dns_zone_setfile( |
| 121 |
zone, TESTS_DIR "/testdata/nsec3param/nsec3.db.signed", |
| 122 |
dns_masterformat_text, &dns_master_style_default); |
| 123 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 124 |
|
| 125 |
result = dns_zone_load(zone, false); |
| 126 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 127 |
|
| 128 |
/* |
| 129 |
* Copy parameters. |
| 130 |
*/ |
| 131 |
copy_params(test->lookup, &lookup, lookupsalt, sizeof(lookupsalt)); |
| 132 |
copy_params(test->expect, &expect, expectsalt, sizeof(expectsalt)); |
| 133 |
|
| 134 |
/* |
| 135 |
* Test dns__zone_lookup_nsec3param(). |
| 136 |
*/ |
| 137 |
result = dns__zone_lookup_nsec3param(zone, &lookup, ¶m, saltbuf, |
| 138 |
test->resalt); |
| 139 |
assert_int_equal(result, test->expected_result); |
| 140 |
assert_int_equal(param.hash, expect.hash); |
| 141 |
assert_int_equal(param.flags, expect.flags); |
| 142 |
assert_int_equal(param.iterations, expect.iterations); |
| 143 |
assert_int_equal(param.salt_length, expect.salt_length); |
| 144 |
assert_non_null(param.salt); |
| 145 |
if (expect.salt != NULL) { |
| 146 |
int ret = memcmp(param.salt, expect.salt, expect.salt_length); |
| 147 |
assert_true(ret == 0); |
| 148 |
} else { |
| 149 |
/* |
| 150 |
* We don't know what the new salt is, but we can compare it |
| 151 |
* to the previous salt and test that it has changed. |
| 152 |
*/ |
| 153 |
unsigned char salt[SALTLEN]; |
| 154 |
int ret; |
| 155 |
decode_salt(SALT, salt, SALTLEN); |
| 156 |
ret = memcmp(param.salt, salt, SALTLEN); |
| 157 |
assert_false(ret == 0); |
| 158 |
} |
| 159 |
|
| 160 |
/* |
| 161 |
* Detach. |
| 162 |
*/ |
| 163 |
dns_zone_detach(&zone); |
| 164 |
} |
| 165 |
|
| 166 |
ISC_RUN_TEST_IMPL(nsec3param_change) { |
| 167 |
size_t i; |
| 168 |
|
| 169 |
/* |
| 170 |
* Define tests. |
| 171 |
*/ |
| 172 |
const nsec3param_change_test_params_t tests[] = { |
| 173 |
/* |
| 174 |
* 1. Change nothing (don't care about salt). |
| 175 |
* This should return ISC_R_SUCCESS because we are already |
| 176 |
* using these NSEC3 parameters. |
| 177 |
*/ |
| 178 |
{ rdata_fromparams(HASH, FLAGS, ITER, SALTLEN, NULL), |
| 179 |
rdata_fromparams(HASH, FLAGS, ITER, SALTLEN, SALT), false, |
| 180 |
ISC_R_SUCCESS }, |
| 181 |
/* |
| 182 |
* 2. Change nothing, but force a resalt. |
| 183 |
* This should change the salt. Set 'expect.salt' to NULL to |
| 184 |
* test a new salt has been generated. |
| 185 |
*/ |
| 186 |
{ rdata_fromparams(HASH, FLAGS, ITER, SALTLEN, NULL), |
| 187 |
rdata_fromparams(HASH, FLAGS, ITER, SALTLEN, NULL), true, |
| 188 |
DNS_R_NSEC3RESALT }, |
| 189 |
/* |
| 190 |
* 3. Change iterations. |
| 191 |
* The NSEC3 paarameters are not found, and there is no |
| 192 |
* need to resalt because an explicit salt has been set, |
| 193 |
* and resalt is not enforced. |
| 194 |
*/ |
| 195 |
{ rdata_fromparams(HASH, FLAGS, 10, SALTLEN, SALT), |
| 196 |
rdata_fromparams(HASH, FLAGS, 10, SALTLEN, SALT), false, |
| 197 |
ISC_R_NOTFOUND }, |
| 198 |
/* |
| 199 |
* 4. Change iterations, don't care about the salt. |
| 200 |
* We don't care about the salt. Since we need to change the |
| 201 |
* NSEC3 parameters, we will also resalt. |
| 202 |
*/ |
| 203 |
{ rdata_fromparams(HASH, FLAGS, 10, SALTLEN, NULL), |
| 204 |
rdata_fromparams(HASH, FLAGS, 10, SALTLEN, NULL), false, |
| 205 |
DNS_R_NSEC3RESALT }, |
| 206 |
/* |
| 207 |
* 5. Change salt length. |
| 208 |
* Changing salt length means we need to resalt. |
| 209 |
*/ |
| 210 |
{ rdata_fromparams(HASH, FLAGS, ITER, 16, NULL), |
| 211 |
rdata_fromparams(HASH, FLAGS, ITER, 16, NULL), false, |
| 212 |
DNS_R_NSEC3RESALT }, |
| 213 |
/* |
| 214 |
* 6. Set explicit salt. |
| 215 |
* A different salt, so the NSEC3 parameters are not found. |
| 216 |
* No need to resalt because an explicit salt is available. |
| 217 |
*/ |
| 218 |
{ rdata_fromparams(HASH, FLAGS, ITER, 4, "12345678"), |
| 219 |
rdata_fromparams(HASH, FLAGS, ITER, 4, "12345678"), false, |
| 220 |
ISC_R_NOTFOUND }, |
| 221 |
/* |
| 222 |
* 7. Same salt. |
| 223 |
* Nothing changed, so expect ISC_R_SUCCESS as a result. |
| 224 |
*/ |
| 225 |
{ rdata_fromparams(HASH, FLAGS, ITER, SALTLEN, SALT), |
| 226 |
rdata_fromparams(HASH, FLAGS, ITER, SALTLEN, SALT), false, |
| 227 |
ISC_R_SUCCESS }, |
| 228 |
/* |
| 229 |
* 8. Same salt, and force resalt. |
| 230 |
* Nothing changed, but a resalt is enforced. |
| 231 |
*/ |
| 232 |
{ rdata_fromparams(HASH, FLAGS, ITER, SALTLEN, SALT), |
| 233 |
rdata_fromparams(HASH, FLAGS, ITER, SALTLEN, NULL), true, |
| 234 |
DNS_R_NSEC3RESALT }, |
| 235 |
/* |
| 236 |
* 9. No salt. |
| 237 |
* Change parameters to use no salt. These parameters are |
| 238 |
* not found, and no new salt needs to be generated. |
| 239 |
*/ |
| 240 |
{ rdata_fromparams(HASH, FLAGS, ITER, 0, NULL), |
| 241 |
rdata_fromparams(HASH, FLAGS, ITER, 0, "-"), true, |
| 242 |
ISC_R_NOTFOUND }, |
| 243 |
/* |
| 244 |
* 10. No salt, explicit. |
| 245 |
* Same as above, but set no salt explicitly. |
| 246 |
*/ |
| 247 |
{ rdata_fromparams(HASH, FLAGS, ITER, 0, "-"), |
| 248 |
rdata_fromparams(HASH, FLAGS, ITER, 0, "-"), true, |
| 249 |
ISC_R_NOTFOUND }, |
| 250 |
}; |
| 251 |
|
| 252 |
UNUSED(state); |
| 253 |
|
| 254 |
/* |
| 255 |
* Run tests. |
| 256 |
*/ |
| 257 |
for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) { |
| 258 |
nsec3param_change_test(&tests[i]); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
ISC_TEST_LIST_START |
| 263 |
ISC_TEST_ENTRY(nsec3param_change) |
| 264 |
ISC_TEST_LIST_END |
| 265 |
|
| 266 |
ISC_TEST_MAIN |