nsec.c 16 KB
Newer Older
1
/*  Copyright (C) 2015-2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
2 3 4 5 6 7 8 9 10 11 12 13

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
14
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
15 16 17
 */

#include <assert.h>
Vladimír Čunát's avatar
Vladimír Čunát committed
18
#include <stdlib.h>
19 20 21

#include <libknot/descriptor.h>
#include <libknot/dname.h>
22
#include <libknot/packet/wire.h>
23 24 25
#include <libknot/rrset.h>
#include <libknot/rrtype/nsec.h>
#include <libknot/rrtype/rrsig.h>
Vladimír Čunát's avatar
Vladimír Čunát committed
26
#include <libdnssec/error.h>
27
#include <libdnssec/nsec.h>
28 29 30 31

#include "lib/defines.h"
#include "lib/dnssec/nsec.h"

32

33 34 35 36 37 38
int kr_nsec_children_in_zone_check(const uint8_t *bm, uint16_t bm_size)
{
	if (!bm) {
		return kr_error(EINVAL);
	}
	const bool parent_side =
39 40 41
		dnssec_nsec_bitmap_contains(bm, bm_size, KNOT_RRTYPE_DNAME)
		|| (dnssec_nsec_bitmap_contains(bm, bm_size, KNOT_RRTYPE_NS)
		    && !dnssec_nsec_bitmap_contains(bm, bm_size, KNOT_RRTYPE_SOA)
42 43 44 45 46 47 48
		);
	return parent_side ? abs(ENOENT) : kr_ok();
	/* LATER: after refactoring, probably also check if signer name equals owner,
	 * but even without that it's not possible to attack *correctly* signed zones.
	 */
}

49 50 51 52
/**
 * Check whether the NSEC RR proves that there is no closer match for <SNAME, SCLASS>.
 * @param nsec  NSEC RRSet.
 * @param sname Searched name.
53
 * @return      0 if proves, >0 if not (abs(ENOENT)), or error code (<0).
54
 */
55
static int nsec_covers(const knot_rrset_t *nsec, const knot_dname_t *sname)
56
{
57
	assert(nsec && sname);
58 59 60
	if (knot_dname_cmp(sname, nsec->owner) <= 0) {
		return abs(ENOENT); /* 'sname' before 'owner', so can't be covered */
	}
61

62
	/* If NSEC 'owner' >= 'next', it means that there is nothing after 'owner' */
63 64
	/* We have to lower-case it with libknot >= 2.7; see also RFC 6840 5.1. */
	knot_dname_t next[KNOT_DNAME_MAXLEN];
65
	int ret = knot_dname_to_wire(next, knot_nsec_next(nsec->rrs.rdata), sizeof(next));
66 67 68 69
	if (ret < 0) {
		assert(!ret);
		return kr_error(ret);
	}
70 71
	knot_dname_to_lower(next);

72 73 74 75
	const bool is_last_nsec = knot_dname_cmp(nsec->owner, next) >= 0;
	const bool in_range = is_last_nsec || knot_dname_cmp(sname, next) < 0;
	if (!in_range) {
		return abs(ENOENT);
76
	}
77 78 79 80 81 82 83
	/* Before returning kr_ok(), we have to check a special case:
	 * sname might be under delegation from owner and thus
	 * not in the zone of this NSEC at all.
	 */
	if (!knot_dname_is_sub(sname, nsec->owner)) {
		return kr_ok();
	}
84 85 86
	const uint8_t *bm = knot_nsec_bitmap(nsec->rrs.rdata);
	uint16_t bm_size = knot_nsec_bitmap_len(nsec->rrs.rdata);

87
	return kr_nsec_children_in_zone_check(bm, bm_size);
88 89
}

90 91 92 93
#define FLG_NOEXIST_RRTYPE (1 << 0) /**< <SNAME, SCLASS> exists, <SNAME, SCLASS, STYPE> does not exist. */
#define FLG_NOEXIST_RRSET  (1 << 1) /**< <SNAME, SCLASS> does not exist. */
#define FLG_NOEXIST_WILDCARD (1 << 2) /**< No wildcard covering <SNAME, SCLASS> exists. */
#define FLG_NOEXIST_CLOSER (1 << 3) /**< Wildcard covering <SNAME, SCLASS> exists, but doesn't match STYPE. */
94

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

/**
 * According to set flags determine whether NSEC proving
 * RRset or RRType non-existense has been found.
 * @param f Flags to inspect.
 * @return  True if required NSEC exists.
 */
#define kr_nsec_rrset_noexist(f) \
        ((f) & (FLG_NOEXIST_RRTYPE | FLG_NOEXIST_RRSET))
/**
 * According to set flags determine whether wildcard non-existense
 * has been proven.
 * @param f Flags to inspect.
 * @return  True if wildcard not exists.
 */
#define kr_nsec_wcard_noexist(f) ((f) & FLG_NOEXIST_WILDCARD)

112
/**
113 114 115 116 117
 * According to set flags determine whether authenticated denial of existence has been proven.
 * @param f Flags to inspect.
 * @return  True if denial of existence proven.
 */
#define kr_nsec_existence_denied(f) \
118
	((kr_nsec_rrset_noexist(f)) && (kr_nsec_wcard_noexist(f)))
119 120 121 122 123 124 125 126 127

/**
 * Name error response check (RFC4035 3.1.3.2; RFC4035 5.4, bullet 2).
 * @note Returned flags must be checked in order to prove denial.
 * @param flags Flags to be set according to check outcome.
 * @param nsec  NSEC RR.
 * @param name  Name to be checked.
 * @param pool
 * @return      0 or error code.
128
 */
129
static int name_error_response_check_rr(int *flags, const knot_rrset_t *nsec,
130
                                        const knot_dname_t *name)
131
{
132 133
	assert(flags && nsec && name);

134
	if (nsec_covers(nsec, name) == 0) {
135
		*flags |= FLG_NOEXIST_RRSET;
136 137
	}

138 139
	/* Try to find parent wildcard that is proved by this NSEC. */ 
	uint8_t namebuf[KNOT_DNAME_MAXLEN];
140
	int ret = knot_dname_to_wire(namebuf, name, sizeof(namebuf));
Marek Vavruša's avatar
Marek Vavruša committed
141
	if (ret < 0)
142
		return ret;
143
	knot_dname_t *ptr = namebuf;
144
	while (ptr[0]) {
145
		/* Remove leftmost label and replace it with '\1*'. */
146
		ptr = (uint8_t *) knot_wire_next_label(ptr, NULL);
147 148 149
		if (!ptr) {
			return kr_error(EINVAL);
		}
150 151
		*(--ptr) = '*';
		*(--ptr) = 1;
152
		/* True if this wildcard provably doesn't exist. */
153
		if (nsec_covers(nsec, ptr) == 0) {
154 155 156 157 158 159
			*flags |= FLG_NOEXIST_WILDCARD;
			break;
		}
		/* Remove added leftmost asterisk. */
		ptr += 2;
	}
160

161 162
	return kr_ok();
}
163

164
int kr_nsec_name_error_response_check(const knot_pkt_t *pkt, knot_section_t section_id,
165
                                      const knot_dname_t *sname)
166 167 168 169
{
	const knot_pktsection_t *sec = knot_pkt_section(pkt, section_id);
	if (!sec || !sname) {
		return kr_error(EINVAL);
170 171
	}

172 173 174 175 176 177
	int flags = 0;
	for (unsigned i = 0; i < sec->count; ++i) {
		const knot_rrset_t *rrset = knot_pkt_rr(sec, i);
		if (rrset->type != KNOT_RRTYPE_NSEC) {
			continue;
		}
178
		int ret = name_error_response_check_rr(&flags, rrset, sname);
179 180 181 182 183 184
		if (ret != 0) {
			return ret;
		}
	}

	return kr_nsec_existence_denied(flags) ? kr_ok() : kr_error(ENOENT);
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
}

/**
 * Returns the labels from the covering RRSIG RRs.
 * @note The number must be the same in all covering RRSIGs.
 * @param nsec NSEC RR.
 * @param sec  Packet section.
 * @param      Number of labels or (negative) error code.
 */
static int coverign_rrsig_labels(const knot_rrset_t *nsec, const knot_pktsection_t *sec)
{
	assert(nsec && sec);

	int ret = kr_error(ENOENT);

	for (unsigned i = 0; i < sec->count; ++i) {
		const knot_rrset_t *rrset = knot_pkt_rr(sec, i);
		if ((rrset->type != KNOT_RRTYPE_RRSIG) ||
		    (!knot_dname_is_equal(rrset->owner, nsec->owner))) {
			continue;
		}

207 208 209 210
		knot_rdata_t *rdata_j = rrset->rrs.rdata;
		for (uint16_t j = 0; j < rrset->rrs.count;
				++j, rdata_j = knot_rdataset_next(rdata_j)) {
			if (knot_rrsig_type_covered(rdata_j) != KNOT_RRTYPE_NSEC) {
211 212 213 214
				continue;
			}

			if (ret < 0) {
215
				ret = knot_rrsig_labels(rdata_j);
216
			} else {
217
				if (ret != knot_rrsig_labels(rdata_j)) {
218 219 220 221 222 223 224 225 226 227
					return kr_error(EINVAL);
				}
			}
		}
	}

	return ret;
}


Marek Vavruša's avatar
Marek Vavruša committed
228
int kr_nsec_bitmap_nodata_check(const uint8_t *bm, uint16_t bm_size, uint16_t type, const knot_dname_t *owner)
229 230
{
	const int NO_PROOF = abs(ENOENT);
Marek Vavruša's avatar
Marek Vavruša committed
231
	if (!bm || !owner) {
232 233
		return kr_error(EINVAL);
	}
234
	if (dnssec_nsec_bitmap_contains(bm, bm_size, type)) {
235 236
		return NO_PROOF;
	}
237

238
	if (type != KNOT_RRTYPE_CNAME
239
	    && dnssec_nsec_bitmap_contains(bm, bm_size, KNOT_RRTYPE_CNAME)) {
240 241 242 243 244
		return NO_PROOF;
	}
	/* Special behavior around zone cuts. */
	switch (type) {
	case KNOT_RRTYPE_DS:
245 246 247
		/* Security feature: in case of DS also check for SOA
		 * non-existence to be more certain that we don't hold
		 * a child-side NSEC by some mistake (e.g. when forwarding).
Marek Vavruša's avatar
Marek Vavruša committed
248 249 250
		 * See RFC4035 5.2, next-to-last paragraph.
		 * This doesn't apply for root DS as it doesn't exist in DNS hierarchy.
		 */
251
		if (owner[0] != '\0' && dnssec_nsec_bitmap_contains(bm, bm_size, KNOT_RRTYPE_SOA)) {
252 253 254 255 256 257 258 259 260 261 262
			return NO_PROOF;
		}
		break;
	case KNOT_RRTYPE_CNAME:
		/* Exception from the `default` rule.  It's perhaps disputable,
		 * but existence of CNAME at zone apex is not allowed, so we
		 * consider a parent-side record to be enough to prove non-existence. */
		break;
	default:
		/* Parent-side delegation record isn't authoritative for non-DS;
		 * see RFC6840 4.1. */
263 264
		if (dnssec_nsec_bitmap_contains(bm, bm_size, KNOT_RRTYPE_NS)
		    && !dnssec_nsec_bitmap_contains(bm, bm_size, KNOT_RRTYPE_SOA)) {
265
			return NO_PROOF;
266
		}
267
		/* LATER(opt): perhaps short-circuit test if we repeat it here. */
268
	}
269 270 271 272

	return kr_ok();
}

273 274 275 276 277 278 279 280 281 282 283 284 285 286
/**
 * Attempt to prove NODATA given a matching NSEC.
 * @param flags Flags to be set according to check outcome.
 * @param nsec  NSEC RR.
 * @param type  Type to be checked.
 * @return      0 on success, abs(ENOENT) for no proof, or error code (<0).
 * @note        It's not a *full* proof, of course (wildcards, etc.)
 * @TODO returning result via `flags` is just ugly.
 */
static int no_data_response_check_rrtype(int *flags, const knot_rrset_t *nsec,
                                         uint16_t type)
{
	assert(flags && nsec);

287 288
	const uint8_t *bm = knot_nsec_bitmap(nsec->rrs.rdata);
	uint16_t bm_size = knot_nsec_bitmap_len(nsec->rrs.rdata);
Marek Vavruša's avatar
Marek Vavruša committed
289
	int ret = kr_nsec_bitmap_nodata_check(bm, bm_size, type, nsec->owner);
290 291 292 293 294 295
	if (ret == kr_ok()) {
		*flags |= FLG_NOEXIST_RRTYPE;
	}
	return ret <= 0 ? ret : kr_ok();
}

296 297 298 299 300 301 302 303 304 305 306
/**
 * Perform check for RR type wildcard existence denial according to RFC4035 5.4, bullet 1.
 * @param flags Flags to be set according to check outcome.
 * @param nsec  NSEC RR.
 * @param sec   Packet section to work with.
 * @return      0 or error code.
 */
static int no_data_wildcard_existence_check(int *flags, const knot_rrset_t *nsec,
                                            const knot_pktsection_t *sec)
{
	assert(flags && nsec && sec);
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323

	int rrsig_labels = coverign_rrsig_labels(nsec, sec);
	if (rrsig_labels < 0) {
		return rrsig_labels;
	}
	int nsec_labels = knot_dname_labels(nsec->owner, NULL);
	if (nsec_labels < 0) {
		return nsec_labels;
	}

	if (rrsig_labels == nsec_labels) {
		*flags |= FLG_NOEXIST_WILDCARD;
	}

	return kr_ok();
}

324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
/**
 * Perform check for NSEC wildcard existence that covers sname and
 * have no stype bit set.
 * @param pkt   Packet structure to be processed.
 * @param sec   Packet section to work with.
 * @param sname Queried domain name.
 * @param stype Queried type.
 * @return      0 or error code.
 */
static int wildcard_match_check(const knot_pkt_t *pkt, const knot_pktsection_t *sec,
				const knot_dname_t *sname, uint16_t stype)
{
	if (!sec || !sname) {
		return kr_error(EINVAL);
	}

	int flags = 0;
	for (unsigned i = 0; i < sec->count; ++i) {
		const knot_rrset_t *rrset = knot_pkt_rr(sec, i);
		if (rrset->type != KNOT_RRTYPE_NSEC) {
			continue;
		}
		if (!knot_dname_is_wildcard(rrset->owner)) {
			continue;
		}
349 350 351 352 353 354 355 356 357
		if (!knot_dname_is_equal(rrset->owner, sname)) {
			int wcard_labels = knot_dname_labels(rrset->owner, NULL);
			int common_labels = knot_dname_matched_labels(rrset->owner, sname);
			int rrsig_labels = coverign_rrsig_labels(rrset, sec);
			if (wcard_labels < 1 ||
			    common_labels != wcard_labels - 1 ||
			    common_labels != rrsig_labels) {
				continue;
			}
358 359 360 361 362 363 364 365 366
		}
		int ret = no_data_response_check_rrtype(&flags, rrset, stype);
		if (ret != 0) {
			return ret;
		}
	}
	return (flags & FLG_NOEXIST_RRTYPE) ? kr_ok() : kr_error(ENOENT);
}

367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
int kr_nsec_no_data_response_check(const knot_pkt_t *pkt, knot_section_t section_id,
                                   const knot_dname_t *sname, uint16_t stype)
{
	const knot_pktsection_t *sec = knot_pkt_section(pkt, section_id);
	if (!sec || !sname) {
		return kr_error(EINVAL);
	}

	int flags = 0;
	for (unsigned i = 0; i < sec->count; ++i) {
		const knot_rrset_t *rrset = knot_pkt_rr(sec, i);
		if (rrset->type != KNOT_RRTYPE_NSEC) {
			continue;
		}
		if (knot_dname_is_equal(rrset->owner, sname)) {
382
			int ret = no_data_response_check_rrtype(&flags, rrset, stype);
383 384 385 386 387 388 389 390 391
			if (ret != 0) {
				return ret;
			}
		}
	}

	return (flags & FLG_NOEXIST_RRTYPE) ? kr_ok() : kr_error(ENOENT);
}

392 393 394 395 396 397 398 399 400 401 402 403 404
int kr_nsec_wildcard_answer_response_check(const knot_pkt_t *pkt, knot_section_t section_id,
                                           const knot_dname_t *sname)
{
	const knot_pktsection_t *sec = knot_pkt_section(pkt, section_id);
	if (!sec || !sname) {
		return kr_error(EINVAL);
	}

	for (unsigned i = 0; i < sec->count; ++i) {
		const knot_rrset_t *rrset = knot_pkt_rr(sec, i);
		if (rrset->type != KNOT_RRTYPE_NSEC) {
			continue;
		}
405
		if (nsec_covers(rrset, sname) == 0) {
406 407 408 409 410
			return kr_ok();
		}
	}

	return kr_error(ENOENT);
411 412 413
}

int kr_nsec_existence_denial(const knot_pkt_t *pkt, knot_section_t section_id,
414
                             const knot_dname_t *sname, uint16_t stype)
415 416 417 418
{
	const knot_pktsection_t *sec = knot_pkt_section(pkt, section_id);
	if (!sec || !sname) {
		return kr_error(EINVAL);
419 420
	}

421 422 423 424 425 426
	int flags = 0;
	for (unsigned i = 0; i < sec->count; ++i) {
		const knot_rrset_t *rrset = knot_pkt_rr(sec, i);
		if (rrset->type != KNOT_RRTYPE_NSEC) {
			continue;
		}
427
		/* NSEC proves that name exists, but has no data (RFC4035 4.9, 1) */
428 429 430
		if (knot_dname_is_equal(rrset->owner, sname)) {
			no_data_response_check_rrtype(&flags, rrset, stype);
		} else {
431 432
			/* NSEC proves that name doesn't exist (RFC4035, 4.9, 2) */
			name_error_response_check_rr(&flags, rrset, sname);
433
		}
434
		no_data_wildcard_existence_check(&flags, rrset, sec);
435
	}
436 437 438 439
	if (kr_nsec_existence_denied(flags)) {
		/* denial of existence proved accordignly to 4035 5.4 -
		 * NSEC proving either rrset non-existance or
		 * qtype non-existance has been found,
440
		 * and no wildcard expansion occurred.
441 442 443 444 445 446 447 448 449 450 451 452
		 */
		return kr_ok();
	} else if (kr_nsec_rrset_noexist(flags)) {
		/* NSEC proving either rrset non-existance or
		 * qtype non-existance has been found,
		 * but wildcard expansion occurs.
		 * Try to find matching wildcard and check
		 * corresponding types.
		 */
		return wildcard_match_check(pkt, sec, sname, stype);
	}
	return kr_error(ENOENT);
453
}
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486

int kr_nsec_ref_to_unsigned(const knot_pkt_t *pkt)
{
	int nsec_found = 0;
	const knot_pktsection_t *sec = knot_pkt_section(pkt, KNOT_AUTHORITY);
	if (!sec) {
		return kr_error(EINVAL);
	}
	for (unsigned i = 0; i < sec->count; ++i) {
		const knot_rrset_t *ns = knot_pkt_rr(sec, i);
		if (ns->type == KNOT_RRTYPE_DS) {
			return kr_error(EEXIST);
		}
		if (ns->type != KNOT_RRTYPE_NS) {
			continue;
		}
		nsec_found = 0;
		for (unsigned j = 0; j < sec->count; ++j) {
			const knot_rrset_t *nsec = knot_pkt_rr(sec, j);
			if (nsec->type == KNOT_RRTYPE_DS) {
				return kr_error(EEXIST);
			}
			if (nsec->type != KNOT_RRTYPE_NSEC) {
				continue;
			}
			/* nsec found
			 * check if owner name matches the delegation name
			 */
			if (!knot_dname_is_equal(nsec->owner, ns->owner)) {
				/* nsec does not match the delegation */
				continue;
			}
			nsec_found = 1;
487 488
			const uint8_t *bm = knot_nsec_bitmap(nsec->rrs.rdata);
			uint16_t bm_size = knot_nsec_bitmap_len(nsec->rrs.rdata);
489 490 491
			if (!bm) {
				return kr_error(EINVAL);
			}
492
			if (dnssec_nsec_bitmap_contains(bm, bm_size,
493
							  KNOT_RRTYPE_NS) &&
494
			    !dnssec_nsec_bitmap_contains(bm, bm_size,
495
							  KNOT_RRTYPE_DS) &&
496
			    !dnssec_nsec_bitmap_contains(bm, bm_size,
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
							  KNOT_RRTYPE_SOA)) {
				/* rfc4035, 5.2 */
				return kr_ok();
			}
		}
		if (nsec_found) {
			/* nsec which owner matches
			 * the delegation name was found,
			 * but nsec type bitmap contains wrong types
			 */
			return kr_error(EINVAL);
		} else {
			/* nsec that matches delegation was not found */
			return kr_error(DNSSEC_NOT_FOUND);
		}
	}

	return kr_error(EINVAL);
}
516 517 518 519

int kr_nsec_matches_name_and_type(const knot_rrset_t *nsec,
				   const knot_dname_t *name, uint16_t type)
{
520 521 522 523 524 525
	/* It's not secure enough to just check a single bit for (some) other types,
	 * but we don't (currently) only use this API for NS.  See RFC 6840 sec. 4.
	 */
	if (type != KNOT_RRTYPE_NS || !nsec || !name) {
		assert(!EINVAL);
		return kr_error(EINVAL);
526 527
	}
	if (!knot_dname_is_equal(nsec->owner, name)) {
528
		return kr_error(ENOENT);
529
	}
530 531
	const uint8_t *bm = knot_nsec_bitmap(nsec->rrs.rdata);
	uint16_t bm_size = knot_nsec_bitmap_len(nsec->rrs.rdata);
532 533 534
	if (!bm) {
		return kr_error(EINVAL);
	}
535
	if (dnssec_nsec_bitmap_contains(bm, bm_size, type)) {
536 537 538
		return kr_ok();
	} else {
		return kr_error(ENOENT);
539 540
	}
}