Debian Codegraph @

tests/dns/tsig_test.c

raw
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/mem.h>
27 #include <isc/random.h>
28 #include <isc/result.h>
29 #include <isc/util.h>
30
31 #include <dns/rdatalist.h>
32 #include <dns/rdataset.h>
33 #include <dns/tsig.h>
34
35 #include "tsig_p.h"
36
37 #include <tests/dns.h>
38
39 #define TEST_ORIGIN "test"
40
41 #define CHECK(r) \
42 { \
43 result = (r); \
44 if (result != ISC_R_SUCCESS) { \
45 goto cleanup; \
46 } \
47 }
48
49 static int
50 setup_test(void **state) {
51 isc_result_t result;
52
53 UNUSED(state);
54
55 result = dst_lib_init(mctx, NULL);
56
57 if (result != ISC_R_SUCCESS) {
58 return 1;
59 }
60
61 return 0;
62 }
63
64 static int
65 teardown_test(void **state) {
66 UNUSED(state);
67
68 dst_lib_destroy();
69
70 return 0;
71 }
72
73 static isc_result_t
74 add_mac(dst_context_t *tsigctx, isc_buffer_t *buf) {
75 dns_rdata_any_tsig_t tsig;
76 dns_rdata_t rdata = DNS_RDATA_INIT;
77 isc_buffer_t databuf;
78 isc_region_t r;
79 isc_result_t result;
80 unsigned char tsigbuf[1024];
81
82 isc_buffer_usedregion(buf, &r);
83 dns_rdata_fromregion(&rdata, dns_rdataclass_any, dns_rdatatype_tsig,
84 &r);
85 isc_buffer_init(&databuf, tsigbuf, sizeof(tsigbuf));
86 CHECK(dns_rdata_tostruct(&rdata, &tsig, NULL));
87 isc_buffer_putuint16(&databuf, tsig.siglen);
88 isc_buffer_putmem(&databuf, tsig.signature, tsig.siglen);
89 isc_buffer_usedregion(&databuf, &r);
90 result = dst_context_adddata(tsigctx, &r);
91 dns_rdata_freestruct(&tsig);
92 cleanup:
93 return result;
94 }
95
96 static isc_result_t
97 add_tsig(dst_context_t *tsigctx, dns_tsigkey_t *key, isc_buffer_t *target,
98 isc_stdtime_t now, bool mangle_sig) {
99 dns_compress_t cctx;
100 dns_rdata_any_tsig_t tsig;
101 dns_rdata_t rdata = DNS_RDATA_INIT;
102 dns_rdatalist_t rdatalist;
103 dns_rdataset_t rdataset;
104 isc_buffer_t *dynbuf = NULL;
105 isc_buffer_t databuf;
106 isc_buffer_t sigbuf;
107 isc_region_t r;
108 isc_result_t result = ISC_R_SUCCESS;
109 unsigned char tsigbuf[1024];
110 unsigned int count;
111 unsigned int sigsize = 0;
112
113 memset(&tsig, 0, sizeof(tsig));
114
115 dns_compress_init(&cctx, mctx, 0);
116
117 tsig.common.rdclass = dns_rdataclass_any;
118 tsig.common.rdtype = dns_rdatatype_tsig;
119 ISC_LINK_INIT(&tsig.common, link);
120 dns_name_init(&tsig.algorithm, NULL);
121 dns_name_clone(key->algorithm, &tsig.algorithm);
122
123 tsig.timesigned = now;
124 tsig.fudge = DNS_TSIG_FUDGE;
125 tsig.originalid = 50;
126 tsig.error = dns_rcode_noerror;
127 tsig.otherlen = 0;
128 tsig.other = NULL;
129
130 isc_buffer_init(&databuf, tsigbuf, sizeof(tsigbuf));
131 isc_buffer_putuint48(&databuf, tsig.timesigned);
132 isc_buffer_putuint16(&databuf, tsig.fudge);
133 isc_buffer_usedregion(&databuf, &r);
134 CHECK(dst_context_adddata(tsigctx, &r));
135
136 CHECK(dst_key_sigsize(key->key, &sigsize));
137 tsig.signature = isc_mem_get(mctx, sigsize);
138 isc_buffer_init(&sigbuf, tsig.signature, sigsize);
139 CHECK(dst_context_sign(tsigctx, &sigbuf));
140 tsig.siglen = isc_buffer_usedlength(&sigbuf);
141 assert_int_equal(sigsize, tsig.siglen);
142 if (mangle_sig) {
143 isc_random_buf(tsig.signature, tsig.siglen);
144 }
145
146 isc_buffer_allocate(mctx, &dynbuf, 512);
147 CHECK(dns_rdata_fromstruct(&rdata, dns_rdataclass_any,
148 dns_rdatatype_tsig, &tsig, dynbuf));
149 dns_rdatalist_init(&rdatalist);
150 rdatalist.rdclass = dns_rdataclass_any;
151 rdatalist.type = dns_rdatatype_tsig;
152 ISC_LIST_APPEND(rdatalist.rdata, &rdata, link);
153 dns_rdataset_init(&rdataset);
154 dns_rdatalist_tordataset(&rdatalist, &rdataset);
155 CHECK(dns_rdataset_towire(&rdataset, key->name, &cctx, target, 0,
156 &count));
157
158 /*
159 * Fixup additional record count.
160 */
161 ((unsigned char *)target->base)[11]++;
162 if (((unsigned char *)target->base)[11] == 0) {
163 ((unsigned char *)target->base)[10]++;
164 }
165 cleanup:
166 if (tsig.signature != NULL) {
167 isc_mem_put(mctx, tsig.signature, sigsize);
168 }
169 if (dynbuf != NULL) {
170 isc_buffer_free(&dynbuf);
171 }
172 dns_compress_invalidate(&cctx);
173
174 return result;
175 }
176
177 static void
178 printmessage(dns_message_t *msg) {
179 isc_buffer_t b;
180 char *buf = NULL;
181 int len = 1024;
182 isc_result_t result = ISC_R_SUCCESS;
183
184 if (!debug) {
185 return;
186 }
187
188 do {
189 buf = isc_mem_get(mctx, len);
190
191 isc_buffer_init(&b, buf, len);
192 result = dns_message_totext(msg, &dns_master_style_debug, 0,
193 &b);
194 if (result == ISC_R_NOSPACE) {
195 isc_mem_put(mctx, buf, len);
196 len *= 2;
197 } else if (result == ISC_R_SUCCESS) {
198 printf("%.*s\n", (int)isc_buffer_usedlength(&b), buf);
199 }
200 } while (result == ISC_R_NOSPACE);
201
202 if (buf != NULL) {
203 isc_mem_put(mctx, buf, len);
204 }
205 }
206
207 static void
208 render(isc_buffer_t *buf, unsigned int flags, dns_tsigkey_t *key,
209 isc_buffer_t **tsigin, isc_buffer_t **tsigout, dst_context_t *tsigctx) {
210 dns_message_t *msg = NULL;
211 dns_compress_t cctx;
212 isc_result_t result;
213
214 dns_message_create(mctx, NULL, NULL, DNS_MESSAGE_INTENTRENDER, &msg);
215 assert_non_null(msg);
216
217 msg->id = 50;
218 msg->rcode = dns_rcode_noerror;
219 msg->flags = flags;
220
221 /*
222 * XXXMPA: this hack needs to be replaced with use of
223 * dns_message_reply() at some point.
224 */
225 if ((flags & DNS_MESSAGEFLAG_QR) != 0) {
226 msg->verified_sig = 1;
227 }
228
229 if (tsigin == tsigout) {
230 msg->tcp_continuation = 1;
231 }
232
233 if (tsigctx == NULL) {
234 result = dns_message_settsigkey(msg, key);
235 assert_int_equal(result, ISC_R_SUCCESS);
236
237 dns_message_setquerytsig(msg, *tsigin);
238 }
239
240 dns_compress_init(&cctx, mctx, 0);
241
242 result = dns_message_renderbegin(msg, &cctx, buf);
243 assert_int_equal(result, ISC_R_SUCCESS);
244
245 result = dns_message_renderend(msg);
246 assert_int_equal(result, ISC_R_SUCCESS);
247
248 if (tsigctx != NULL) {
249 isc_region_t r;
250
251 isc_buffer_usedregion(buf, &r);
252 result = dst_context_adddata(tsigctx, &r);
253 assert_int_equal(result, ISC_R_SUCCESS);
254 } else {
255 if (tsigin == tsigout && *tsigin != NULL) {
256 isc_buffer_free(tsigin);
257 }
258
259 result = dns_message_getquerytsig(msg, mctx, tsigout);
260 assert_int_equal(result, ISC_R_SUCCESS);
261 }
262
263 dns_compress_invalidate(&cctx);
264 dns_message_detach(&msg);
265 }
266
267 static void
268 tsig_tcp(isc_stdtime_t now, isc_result_t expected_result, bool mangle_sig) {
269 const dns_name_t *tsigowner = NULL;
270 dns_fixedname_t fkeyname;
271 dns_message_t *msg = NULL;
272 dns_name_t *keyname;
273 dns_tsigkeyring_t *ring = NULL;
274 dns_tsigkey_t *key = NULL;
275 isc_buffer_t *buf = NULL;
276 isc_buffer_t *querytsig = NULL;
277 isc_buffer_t *tsigin = NULL;
278 isc_buffer_t *tsigout = NULL;
279 isc_result_t result;
280 unsigned char secret[16] = { 0 };
281 dst_context_t *tsigctx = NULL;
282 dst_context_t *outctx = NULL;
283
284 /* isc_log_setdebuglevel(lctx, 99); */
285
286 keyname = dns_fixedname_initname(&fkeyname);
287 result = dns_name_fromstring(keyname, "test", dns_rootname, 0, NULL);
288 assert_int_equal(result, ISC_R_SUCCESS);
289
290 dns_tsigkeyring_create(mctx, &ring);
291 assert_non_null(ring);
292
293 result = dns_tsigkey_create(keyname, DST_ALG_HMACSHA256, secret,
294 sizeof(secret), mctx, &key);
295 assert_int_equal(result, ISC_R_SUCCESS);
296 result = dns_tsigkeyring_add(ring, key);
297 assert_int_equal(result, ISC_R_SUCCESS);
298 assert_non_null(key);
299
300 /*
301 * Create request.
302 */
303 isc_buffer_allocate(mctx, &buf, 65535);
304 render(buf, 0, key, &tsigout, &querytsig, NULL);
305 isc_buffer_free(&buf);
306
307 /*
308 * Create response message 1.
309 */
310 isc_buffer_allocate(mctx, &buf, 65535);
311 render(buf, DNS_MESSAGEFLAG_QR, key, &querytsig, &tsigout, NULL);
312 assert_non_null(tsigout);
313
314 /*
315 * Process response message 1.
316 */
317 dns_message_create(mctx, NULL, NULL, DNS_MESSAGE_INTENTPARSE, &msg);
318 assert_non_null(msg);
319
320 result = dns_message_settsigkey(msg, key);
321 assert_int_equal(result, ISC_R_SUCCESS);
322
323 result = dns_message_parse(msg, buf, 0);
324 assert_int_equal(result, ISC_R_SUCCESS);
325
326 printmessage(msg);
327
328 dns_message_setquerytsig(msg, querytsig);
329
330 result = dns_tsig_verify(buf, msg, NULL, NULL);
331 assert_int_equal(result, ISC_R_SUCCESS);
332 assert_int_equal(msg->verified_sig, 1);
333 assert_int_equal(msg->tsigstatus, dns_rcode_noerror);
334
335 /*
336 * Check that we have a TSIG in the first message.
337 */
338 assert_non_null(dns_message_gettsig(msg, &tsigowner));
339
340 result = dns_message_getquerytsig(msg, mctx, &tsigin);
341 assert_int_equal(result, ISC_R_SUCCESS);
342
343 tsigctx = msg->tsigctx;
344 msg->tsigctx = NULL;
345 isc_buffer_free(&buf);
346 dns_message_detach(&msg);
347
348 result = dst_context_create(key->key, mctx, DNS_LOGCATEGORY_DNSSEC,
349 false, 0, &outctx);
350 assert_int_equal(result, ISC_R_SUCCESS);
351 assert_non_null(outctx);
352
353 /*
354 * Start digesting.
355 */
356 result = add_mac(outctx, tsigout);
357 assert_int_equal(result, ISC_R_SUCCESS);
358
359 /*
360 * Create response message 2.
361 */
362 isc_buffer_allocate(mctx, &buf, 65535);
363
364 assert_int_equal(result, ISC_R_SUCCESS);
365 render(buf, DNS_MESSAGEFLAG_QR, key, &tsigout, &tsigout, outctx);
366
367 /*
368 * Process response message 2.
369 */
370 dns_message_create(mctx, NULL, NULL, DNS_MESSAGE_INTENTPARSE, &msg);
371 assert_non_null(msg);
372
373 msg->tcp_continuation = 1;
374 msg->tsigctx = tsigctx;
375 tsigctx = NULL;
376
377 result = dns_message_settsigkey(msg, key);
378 assert_int_equal(result, ISC_R_SUCCESS);
379
380 result = dns_message_parse(msg, buf, 0);
381 assert_int_equal(result, ISC_R_SUCCESS);
382
383 printmessage(msg);
384
385 dns_message_setquerytsig(msg, tsigin);
386
387 result = dns_tsig_verify(buf, msg, NULL, NULL);
388 assert_int_equal(result, ISC_R_SUCCESS);
389 assert_int_equal(msg->verified_sig, 0);
390 assert_int_equal(msg->tsigstatus, dns_rcode_noerror);
391
392 /*
393 * Check that we don't have a TSIG in the second message.
394 */
395 tsigowner = NULL;
396 assert_true(dns_message_gettsig(msg, &tsigowner) == NULL);
397
398 tsigctx = msg->tsigctx;
399 msg->tsigctx = NULL;
400 isc_buffer_free(&buf);
401 dns_message_detach(&msg);
402
403 /*
404 * Create response message 3.
405 */
406 isc_buffer_allocate(mctx, &buf, 65535);
407 render(buf, DNS_MESSAGEFLAG_QR, key, &tsigout, &tsigout, outctx);
408
409 result = add_tsig(outctx, key, buf, now, mangle_sig);
410 assert_int_equal(result, ISC_R_SUCCESS);
411
412 /*
413 * Process response message 3.
414 */
415 dns_message_create(mctx, NULL, NULL, DNS_MESSAGE_INTENTPARSE, &msg);
416 assert_non_null(msg);
417
418 msg->tcp_continuation = 1;
419 msg->tsigctx = tsigctx;
420 tsigctx = NULL;
421
422 result = dns_message_settsigkey(msg, key);
423 assert_int_equal(result, ISC_R_SUCCESS);
424
425 result = dns_message_parse(msg, buf, 0);
426 assert_int_equal(result, ISC_R_SUCCESS);
427
428 printmessage(msg);
429
430 /*
431 * Check that we had a TSIG in the third message.
432 */
433 assert_non_null(dns_message_gettsig(msg, &tsigowner));
434
435 dns_message_setquerytsig(msg, tsigin);
436
437 result = dns_tsig_verify(buf, msg, NULL, NULL);
438 switch (expected_result) {
439 case ISC_R_SUCCESS:
440 assert_int_equal(result, ISC_R_SUCCESS);
441 assert_int_equal(msg->verified_sig, 1);
442 assert_int_equal(msg->tsigstatus, dns_rcode_noerror);
443 break;
444 case DNS_R_CLOCKSKEW:
445 assert_int_equal(result, DNS_R_CLOCKSKEW);
446 assert_int_equal(msg->verified_sig, 1);
447 assert_int_equal(msg->tsigstatus, dns_tsigerror_badtime);
448 break;
449 default:
450 UNREACHABLE();
451 }
452
453 if (tsigin != NULL) {
454 isc_buffer_free(&tsigin);
455 }
456
457 result = dns_message_getquerytsig(msg, mctx, &tsigin);
458 assert_int_equal(result, ISC_R_SUCCESS);
459
460 isc_buffer_free(&buf);
461 dns_message_detach(&msg);
462
463 if (outctx != NULL) {
464 dst_context_destroy(&outctx);
465 }
466 if (querytsig != NULL) {
467 isc_buffer_free(&querytsig);
468 }
469 if (tsigin != NULL) {
470 isc_buffer_free(&tsigin);
471 }
472 if (tsigout != NULL) {
473 isc_buffer_free(&tsigout);
474 }
475 dns_tsigkey_detach(&key);
476 if (ring != NULL) {
477 dns_tsigkeyring_detach(&ring);
478 }
479 }
480
481 /*
482 * Test tsig tcp-continuation validation:
483 * Check that a simulated three message TCP sequence where the first
484 * and last messages contain TSIGs but the intermediate message doesn't
485 * correctly verifies.
486 */
487 ISC_RUN_TEST_IMPL(tsig_tcp) {
488 /* Run with correct current time */
489 tsig_tcp(isc_stdtime_now(), ISC_R_SUCCESS, false);
490 }
491
492 ISC_RUN_TEST_IMPL(tsig_badtime) {
493 /* Run with time outside of the fudge */
494 tsig_tcp(isc_stdtime_now() - 2 * DNS_TSIG_FUDGE, DNS_R_CLOCKSKEW,
495 false);
496 tsig_tcp(isc_stdtime_now() + 2 * DNS_TSIG_FUDGE, DNS_R_CLOCKSKEW,
497 false);
498 }
499
500 ISC_RUN_TEST_IMPL(tsig_badsig) {
501 tsig_tcp(isc_stdtime_now(), DNS_R_TSIGERRORSET, true);
502 }
503
504 /* Tests the dns__tsig_algvalid function */
505 ISC_RUN_TEST_IMPL(algvalid) {
506 UNUSED(state);
507
508 assert_true(dns__tsig_algvalid(DST_ALG_HMACMD5));
509
510 assert_true(dns__tsig_algvalid(DST_ALG_HMACSHA1));
511 assert_true(dns__tsig_algvalid(DST_ALG_HMACSHA224));
512 assert_true(dns__tsig_algvalid(DST_ALG_HMACSHA256));
513 assert_true(dns__tsig_algvalid(DST_ALG_HMACSHA384));
514 assert_true(dns__tsig_algvalid(DST_ALG_HMACSHA512));
515
516 assert_false(dns__tsig_algvalid(DST_ALG_GSSAPI));
517 }
518
519 ISC_TEST_LIST_START
520 ISC_TEST_ENTRY_CUSTOM(tsig_tcp, setup_test, teardown_test)
521 ISC_TEST_ENTRY_CUSTOM(tsig_badtime, setup_test, teardown_test)
522 ISC_TEST_ENTRY(algvalid)
523 ISC_TEST_LIST_END
524
525 ISC_TEST_MAIN