| 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 |
/* |
| 24 |
* As a workaround, include an OpenSSL header file before including cmocka.h, |
| 25 |
* because OpenSSL 3.1.0 uses __attribute__(malloc), conflicting with a |
| 26 |
* redefined malloc in cmocka.h. |
| 27 |
*/ |
| 28 |
#include <openssl/err.h> |
| 29 |
|
| 30 |
#define UNIT_TESTING |
| 31 |
#include <cmocka.h> |
| 32 |
|
| 33 |
#include <isc/file.h> |
| 34 |
#include <isc/hex.h> |
| 35 |
#include <isc/result.h> |
| 36 |
#include <isc/stdio.h> |
| 37 |
#include <isc/string.h> |
| 38 |
#include <isc/util.h> |
| 39 |
|
| 40 |
#include <dst/dst.h> |
| 41 |
|
| 42 |
#include "dst_internal.h" |
| 43 |
|
| 44 |
#include <tests/dns.h> |
| 45 |
|
| 46 |
static int |
| 47 |
setup_test(void **state) { |
| 48 |
UNUSED(state); |
| 49 |
|
| 50 |
dst_lib_init(mctx, NULL); |
| 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 |
/* Read sig in file at path to buf. Check signature ineffability */ |
| 65 |
static isc_result_t |
| 66 |
sig_fromfile(const char *path, isc_buffer_t *buf) { |
| 67 |
isc_result_t result; |
| 68 |
size_t rval, len; |
| 69 |
FILE *fp = NULL; |
| 70 |
unsigned char val; |
| 71 |
char *p, *data; |
| 72 |
off_t size; |
| 73 |
|
| 74 |
result = isc_stdio_open(path, "rb", &fp); |
| 75 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 76 |
|
| 77 |
result = isc_file_getsizefd(fileno(fp), &size); |
| 78 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 79 |
|
| 80 |
data = isc_mem_get(mctx, (size + 1)); |
| 81 |
assert_non_null(data); |
| 82 |
|
| 83 |
len = (size_t)size; |
| 84 |
p = data; |
| 85 |
while (len != 0U) { |
| 86 |
result = isc_stdio_read(p, 1, len, fp, &rval); |
| 87 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 88 |
len -= rval; |
| 89 |
p += rval; |
| 90 |
} |
| 91 |
isc_stdio_close(fp); |
| 92 |
|
| 93 |
p = data; |
| 94 |
len = size; |
| 95 |
while (len > 0U) { |
| 96 |
if ((*p == '\r') || (*p == '\n')) { |
| 97 |
++p; |
| 98 |
--len; |
| 99 |
continue; |
| 100 |
} else if (len < 2U) { |
| 101 |
goto err; |
| 102 |
} |
| 103 |
if (('0' <= *p) && (*p <= '9')) { |
| 104 |
val = *p - '0'; |
| 105 |
} else if (('A' <= *p) && (*p <= 'F')) { |
| 106 |
val = *p - 'A' + 10; |
| 107 |
} else { |
| 108 |
result = ISC_R_BADHEX; |
| 109 |
goto err; |
| 110 |
} |
| 111 |
++p; |
| 112 |
val <<= 4; |
| 113 |
--len; |
| 114 |
if (('0' <= *p) && (*p <= '9')) { |
| 115 |
val |= (*p - '0'); |
| 116 |
} else if (('A' <= *p) && (*p <= 'F')) { |
| 117 |
val |= (*p - 'A' + 10); |
| 118 |
} else { |
| 119 |
result = ISC_R_BADHEX; |
| 120 |
goto err; |
| 121 |
} |
| 122 |
++p; |
| 123 |
--len; |
| 124 |
isc_buffer_putuint8(buf, val); |
| 125 |
} |
| 126 |
|
| 127 |
result = ISC_R_SUCCESS; |
| 128 |
|
| 129 |
err: |
| 130 |
isc_mem_put(mctx, data, size + 1); |
| 131 |
return result; |
| 132 |
} |
| 133 |
|
| 134 |
static void |
| 135 |
check_sig(const char *datapath, const char *sigpath, const char *keyname, |
| 136 |
dns_keytag_t id, dns_secalg_t alg, int type, bool expect) { |
| 137 |
isc_result_t result; |
| 138 |
size_t rval, len; |
| 139 |
FILE *fp; |
| 140 |
dst_key_t *key = NULL; |
| 141 |
unsigned char sig[512]; |
| 142 |
unsigned char *p; |
| 143 |
unsigned char *data; |
| 144 |
off_t size; |
| 145 |
isc_buffer_t b; |
| 146 |
isc_buffer_t databuf, sigbuf; |
| 147 |
isc_region_t datareg, sigreg; |
| 148 |
dns_fixedname_t fname; |
| 149 |
dns_name_t *name; |
| 150 |
dst_context_t *ctx = NULL; |
| 151 |
|
| 152 |
/* |
| 153 |
* Read data from file in a form usable by dst_verify. |
| 154 |
*/ |
| 155 |
result = isc_stdio_open(datapath, "rb", &fp); |
| 156 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 157 |
|
| 158 |
result = isc_file_getsizefd(fileno(fp), &size); |
| 159 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 160 |
|
| 161 |
data = isc_mem_get(mctx, (size + 1)); |
| 162 |
assert_non_null(data); |
| 163 |
|
| 164 |
p = data; |
| 165 |
len = (size_t)size; |
| 166 |
do { |
| 167 |
result = isc_stdio_read(p, 1, len, fp, &rval); |
| 168 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 169 |
len -= rval; |
| 170 |
p += rval; |
| 171 |
} while (len); |
| 172 |
isc_stdio_close(fp); |
| 173 |
|
| 174 |
/* |
| 175 |
* Read key from file in a form usable by dst_verify. |
| 176 |
*/ |
| 177 |
name = dns_fixedname_initname(&fname); |
| 178 |
isc_buffer_constinit(&b, keyname, strlen(keyname)); |
| 179 |
isc_buffer_add(&b, strlen(keyname)); |
| 180 |
result = dns_name_fromtext(name, &b, dns_rootname, 0, NULL); |
| 181 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 182 |
result = dst_key_fromfile(name, id, alg, type, |
| 183 |
TESTS_DIR "/testdata/dst", mctx, &key); |
| 184 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 185 |
|
| 186 |
isc_buffer_init(&databuf, data, (unsigned int)size); |
| 187 |
isc_buffer_add(&databuf, (unsigned int)size); |
| 188 |
isc_buffer_usedregion(&databuf, &datareg); |
| 189 |
|
| 190 |
memset(sig, 0, sizeof(sig)); |
| 191 |
isc_buffer_init(&sigbuf, sig, sizeof(sig)); |
| 192 |
|
| 193 |
/* |
| 194 |
* Read precomputed signature from file in a form usable by dst_verify. |
| 195 |
*/ |
| 196 |
result = sig_fromfile(sigpath, &sigbuf); |
| 197 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 198 |
|
| 199 |
/* |
| 200 |
* Verify that the key signed the data. |
| 201 |
*/ |
| 202 |
isc_buffer_remainingregion(&sigbuf, &sigreg); |
| 203 |
|
| 204 |
result = dst_context_create(key, mctx, DNS_LOGCATEGORY_GENERAL, false, |
| 205 |
0, &ctx); |
| 206 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 207 |
|
| 208 |
result = dst_context_adddata(ctx, &datareg); |
| 209 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 210 |
result = dst_context_verify(ctx, &sigreg); |
| 211 |
|
| 212 |
/* |
| 213 |
* Compute the expected signature and emit it |
| 214 |
* so the precomputed signature can be updated. |
| 215 |
* This should only be done if the covered data |
| 216 |
* is updated. |
| 217 |
*/ |
| 218 |
if (expect && result != ISC_R_SUCCESS) { |
| 219 |
isc_result_t result2; |
| 220 |
|
| 221 |
dst_context_destroy(&ctx); |
| 222 |
result2 = dst_context_create(key, mctx, DNS_LOGCATEGORY_GENERAL, |
| 223 |
false, 0, &ctx); |
| 224 |
assert_int_equal(result2, ISC_R_SUCCESS); |
| 225 |
|
| 226 |
result2 = dst_context_adddata(ctx, &datareg); |
| 227 |
assert_int_equal(result2, ISC_R_SUCCESS); |
| 228 |
|
| 229 |
char sigbuf2[4096]; |
| 230 |
isc_buffer_t sigb; |
| 231 |
isc_buffer_init(&sigb, sigbuf2, sizeof(sigbuf2)); |
| 232 |
|
| 233 |
result2 = dst_context_sign(ctx, &sigb); |
| 234 |
assert_int_equal(result2, ISC_R_SUCCESS); |
| 235 |
|
| 236 |
isc_region_t r; |
| 237 |
isc_buffer_usedregion(&sigb, &r); |
| 238 |
|
| 239 |
char hexbuf[4096] = { 0 }; |
| 240 |
isc_buffer_t hb; |
| 241 |
isc_buffer_init(&hb, hexbuf, sizeof(hexbuf)); |
| 242 |
|
| 243 |
isc_hex_totext(&r, 0, "", &hb); |
| 244 |
|
| 245 |
fprintf(stderr, "# %s:\n# %s\n", sigpath, hexbuf); |
| 246 |
} |
| 247 |
|
| 248 |
isc_mem_put(mctx, data, size + 1); |
| 249 |
dst_context_destroy(&ctx); |
| 250 |
dst_key_free(&key); |
| 251 |
|
| 252 |
assert_true((expect && (result == ISC_R_SUCCESS)) || |
| 253 |
(!expect && (result != ISC_R_SUCCESS))); |
| 254 |
|
| 255 |
return; |
| 256 |
} |
| 257 |
|
| 258 |
ISC_RUN_TEST_IMPL(sig_test) { |
| 259 |
struct { |
| 260 |
const char *datapath; |
| 261 |
const char *sigpath; |
| 262 |
const char *keyname; |
| 263 |
dns_keytag_t keyid; |
| 264 |
dns_secalg_t alg; |
| 265 |
bool expect; |
| 266 |
} testcases[] = { |
| 267 |
{ TESTS_DIR "/testdata/dst/test1.data", |
| 268 |
TESTS_DIR "/testdata/dst/test1.ecdsa256sig", "test.", 49130, |
| 269 |
DST_ALG_ECDSA256, true }, |
| 270 |
{ TESTS_DIR "/testdata/dst/test1.data", |
| 271 |
TESTS_DIR "/testdata/dst/test1.rsasha256sig", "test.", 11349, |
| 272 |
DST_ALG_RSASHA256, true }, |
| 273 |
{ /* wrong sig */ |
| 274 |
TESTS_DIR "/testdata/dst/test1.data", |
| 275 |
TESTS_DIR "/testdata/dst/test1.ecdsa256sig", "test.", 11349, |
| 276 |
DST_ALG_RSASHA256, false }, |
| 277 |
{ /* wrong data */ |
| 278 |
TESTS_DIR "/testdata/dst/test2.data", |
| 279 |
TESTS_DIR "/testdata/dst/test1.ecdsa256sig", "test.", 49130, |
| 280 |
DST_ALG_ECDSA256, false }, |
| 281 |
}; |
| 282 |
unsigned int i; |
| 283 |
|
| 284 |
for (i = 0; i < (sizeof(testcases) / sizeof(testcases[0])); i++) { |
| 285 |
if (!dst_algorithm_supported(testcases[i].alg)) { |
| 286 |
continue; |
| 287 |
} |
| 288 |
|
| 289 |
check_sig(testcases[i].datapath, testcases[i].sigpath, |
| 290 |
testcases[i].keyname, testcases[i].keyid, |
| 291 |
testcases[i].alg, DST_TYPE_PRIVATE | DST_TYPE_PUBLIC, |
| 292 |
testcases[i].expect); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
static void |
| 297 |
check_cmp(const char *key1_name, dns_keytag_t key1_id, const char *key2_name, |
| 298 |
dns_keytag_t key2_id, dns_secalg_t alg, int type, bool expect) { |
| 299 |
isc_result_t result; |
| 300 |
dst_key_t *key1 = NULL; |
| 301 |
dst_key_t *key2 = NULL; |
| 302 |
isc_buffer_t b1; |
| 303 |
isc_buffer_t b2; |
| 304 |
dns_fixedname_t fname1; |
| 305 |
dns_fixedname_t fname2; |
| 306 |
dns_name_t *name1; |
| 307 |
dns_name_t *name2; |
| 308 |
|
| 309 |
/* |
| 310 |
* Read key1 from the file. |
| 311 |
*/ |
| 312 |
name1 = dns_fixedname_initname(&fname1); |
| 313 |
isc_buffer_constinit(&b1, key1_name, strlen(key1_name)); |
| 314 |
isc_buffer_add(&b1, strlen(key1_name)); |
| 315 |
result = dns_name_fromtext(name1, &b1, dns_rootname, 0, NULL); |
| 316 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 317 |
result = dst_key_fromfile(name1, key1_id, alg, type, |
| 318 |
TESTS_DIR "/comparekeys", mctx, &key1); |
| 319 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 320 |
|
| 321 |
/* |
| 322 |
* Read key2 from the file. |
| 323 |
*/ |
| 324 |
name2 = dns_fixedname_initname(&fname2); |
| 325 |
isc_buffer_constinit(&b2, key2_name, strlen(key2_name)); |
| 326 |
isc_buffer_add(&b2, strlen(key2_name)); |
| 327 |
result = dns_name_fromtext(name2, &b2, dns_rootname, 0, NULL); |
| 328 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 329 |
result = dst_key_fromfile(name2, key2_id, alg, type, |
| 330 |
TESTS_DIR "/comparekeys", mctx, &key2); |
| 331 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 332 |
|
| 333 |
/* |
| 334 |
* Compare the keys (for public-only keys). |
| 335 |
*/ |
| 336 |
if ((type & DST_TYPE_PRIVATE) == 0) { |
| 337 |
assert_true(dst_key_pubcompare(key1, key2, false) == expect); |
| 338 |
} |
| 339 |
|
| 340 |
/* |
| 341 |
* Compare the keys (for both public-only keys and keypairs). |
| 342 |
*/ |
| 343 |
assert_true(dst_key_compare(key1, key2) == expect); |
| 344 |
|
| 345 |
/* |
| 346 |
* Free the keys |
| 347 |
*/ |
| 348 |
dst_key_free(&key2); |
| 349 |
dst_key_free(&key1); |
| 350 |
|
| 351 |
return; |
| 352 |
} |
| 353 |
|
| 354 |
ISC_RUN_TEST_IMPL(cmp_test) { |
| 355 |
struct { |
| 356 |
const char *key1_name; |
| 357 |
dns_keytag_t key1_id; |
| 358 |
const char *key2_name; |
| 359 |
dns_keytag_t key2_id; |
| 360 |
dns_secalg_t alg; |
| 361 |
int type; |
| 362 |
bool expect; |
| 363 |
} testcases[] = { |
| 364 |
/* RSA Keypair: self */ |
| 365 |
{ "example.", 53461, "example.", 53461, DST_ALG_RSASHA256, |
| 366 |
DST_TYPE_PUBLIC | DST_TYPE_PRIVATE, true }, |
| 367 |
|
| 368 |
/* RSA Keypair: different key */ |
| 369 |
{ "example.", 53461, "example2.", 37993, DST_ALG_RSASHA256, |
| 370 |
DST_TYPE_PUBLIC | DST_TYPE_PRIVATE, false }, |
| 371 |
|
| 372 |
/* RSA Keypair: different PublicExponent (e) */ |
| 373 |
{ "example.", 53461, "example-e.", 53973, DST_ALG_RSASHA256, |
| 374 |
DST_TYPE_PUBLIC | DST_TYPE_PRIVATE, false }, |
| 375 |
|
| 376 |
/* RSA Keypair: different Modulus (n) */ |
| 377 |
{ "example.", 53461, "example-n.", 37464, DST_ALG_RSASHA256, |
| 378 |
DST_TYPE_PUBLIC | DST_TYPE_PRIVATE, false }, |
| 379 |
|
| 380 |
/* RSA Public Key: self */ |
| 381 |
{ "example.", 53461, "example.", 53461, DST_ALG_RSASHA256, |
| 382 |
DST_TYPE_PUBLIC, true }, |
| 383 |
|
| 384 |
/* RSA Public Key: different key */ |
| 385 |
{ "example.", 53461, "example2.", 37993, DST_ALG_RSASHA256, |
| 386 |
DST_TYPE_PUBLIC, false }, |
| 387 |
|
| 388 |
/* RSA Public Key: different PublicExponent (e) */ |
| 389 |
{ "example.", 53461, "example-e.", 53973, DST_ALG_RSASHA256, |
| 390 |
DST_TYPE_PUBLIC, false }, |
| 391 |
|
| 392 |
/* RSA Public Key: different Modulus (n) */ |
| 393 |
{ "example.", 53461, "example-n.", 37464, DST_ALG_RSASHA256, |
| 394 |
DST_TYPE_PUBLIC, false }, |
| 395 |
|
| 396 |
/* ECDSA Keypair: self */ |
| 397 |
{ "example.", 19786, "example.", 19786, DST_ALG_ECDSA256, |
| 398 |
DST_TYPE_PUBLIC | DST_TYPE_PRIVATE, true }, |
| 399 |
|
| 400 |
/* ECDSA Keypair: different key */ |
| 401 |
{ "example.", 19786, "example2.", 16384, DST_ALG_ECDSA256, |
| 402 |
DST_TYPE_PUBLIC | DST_TYPE_PRIVATE, false }, |
| 403 |
|
| 404 |
/* ECDSA Public Key: self */ |
| 405 |
{ "example.", 19786, "example.", 19786, DST_ALG_ECDSA256, |
| 406 |
DST_TYPE_PUBLIC, true }, |
| 407 |
|
| 408 |
/* ECDSA Public Key: different key */ |
| 409 |
{ "example.", 19786, "example2.", 16384, DST_ALG_ECDSA256, |
| 410 |
DST_TYPE_PUBLIC, false }, |
| 411 |
|
| 412 |
/* EdDSA Keypair: self */ |
| 413 |
{ "example.", 63663, "example.", 63663, DST_ALG_ED25519, |
| 414 |
DST_TYPE_PUBLIC | DST_TYPE_PRIVATE, true }, |
| 415 |
|
| 416 |
/* EdDSA Keypair: different key */ |
| 417 |
{ "example.", 63663, "example2.", 37529, DST_ALG_ED25519, |
| 418 |
DST_TYPE_PUBLIC | DST_TYPE_PRIVATE, false }, |
| 419 |
|
| 420 |
/* EdDSA Public Key: self */ |
| 421 |
{ "example.", 63663, "example.", 63663, DST_ALG_ED25519, |
| 422 |
DST_TYPE_PUBLIC, true }, |
| 423 |
|
| 424 |
/* EdDSA Public Key: different key */ |
| 425 |
{ "example.", 63663, "example2.", 37529, DST_ALG_ED25519, |
| 426 |
DST_TYPE_PUBLIC, false }, |
| 427 |
}; |
| 428 |
unsigned int i; |
| 429 |
|
| 430 |
for (i = 0; i < (sizeof(testcases) / sizeof(testcases[0])); i++) { |
| 431 |
if (!dst_algorithm_supported(testcases[i].alg)) { |
| 432 |
continue; |
| 433 |
} |
| 434 |
|
| 435 |
check_cmp(testcases[i].key1_name, testcases[i].key1_id, |
| 436 |
testcases[i].key2_name, testcases[i].key2_id, |
| 437 |
testcases[i].alg, testcases[i].type, |
| 438 |
testcases[i].expect); |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
ISC_TEST_LIST_START |
| 443 |
ISC_TEST_ENTRY_CUSTOM(sig_test, setup_test, teardown_test) |
| 444 |
ISC_TEST_ENTRY_CUSTOM(cmp_test, setup_test, teardown_test) |
| 445 |
ISC_TEST_LIST_END |
| 446 |
|
| 447 |
ISC_TEST_MAIN |