1 /* $NetBSD: rcode.c,v 1.13 2024/02/21 22:52:07 christos Exp $ */ 2 3 /* 4 * Copyright (C) Internet Systems Consortium, Inc. ("ISC") 5 * 6 * SPDX-License-Identifier: MPL-2.0 7 * 8 * This Source Code Form is subject to the terms of the Mozilla Public 9 * License, v. 2.0. If a copy of the MPL was not distributed with this 10 * file, you can obtain one at https://mozilla.org/MPL/2.0/. 11 * 12 * See the COPYRIGHT file distributed with this work for additional 13 * information regarding copyright ownership. 14 */ 15 16 #include <ctype.h> 17 #include <inttypes.h> 18 #include <stdbool.h> 19 #include <stdlib.h> 20 21 #include <isc/buffer.h> 22 #include <isc/parseint.h> 23 #include <isc/print.h> 24 #include <isc/region.h> 25 #include <isc/result.h> 26 #include <isc/stdio.h> 27 #include <isc/string.h> 28 #include <isc/types.h> 29 #include <isc/util.h> 30 31 #include <dns/cert.h> 32 #include <dns/ds.h> 33 #include <dns/dsdigest.h> 34 #include <dns/keyflags.h> 35 #include <dns/keyvalues.h> 36 #include <dns/rcode.h> 37 #include <dns/rdataclass.h> 38 #include <dns/secalg.h> 39 #include <dns/secproto.h> 40 41 #define RETERR(x) \ 42 do { \ 43 isc_result_t _r = (x); \ 44 if (_r != ISC_R_SUCCESS) \ 45 return ((_r)); \ 46 } while (0) 47 48 #define NUMBERSIZE sizeof("037777777777") /* 2^32-1 octal + NUL */ 49 50 #define TOTEXTONLY 0x01 51 52 #define RCODENAMES \ 53 /* standard rcodes */ \ 54 { dns_rcode_noerror, "NOERROR", 0 }, \ 55 { dns_rcode_formerr, "FORMERR", 0 }, \ 56 { dns_rcode_servfail, "SERVFAIL", 0 }, \ 57 { dns_rcode_nxdomain, "NXDOMAIN", 0 }, \ 58 { dns_rcode_notimp, "NOTIMP", 0 }, \ 59 { dns_rcode_refused, "REFUSED", 0 }, \ 60 { dns_rcode_yxdomain, "YXDOMAIN", 0 }, \ 61 { dns_rcode_yxrrset, "YXRRSET", 0 }, \ 62 { dns_rcode_nxrrset, "NXRRSET", 0 }, \ 63 { dns_rcode_notauth, "NOTAUTH", 0 }, \ 64 { dns_rcode_notzone, "NOTZONE", 0 }, \ 65 { 11, "RESERVED11", TOTEXTONLY }, \ 66 { 12, "RESERVED12", TOTEXTONLY }, \ 67 { 13, "RESERVED13", TOTEXTONLY }, \ 68 { 14, "RESERVED14", TOTEXTONLY }, \ 69 { 15, "RESERVED15", TOTEXTONLY }, 70 71 #define ERCODENAMES \ 72 /* extended rcodes */ \ 73 { dns_rcode_badvers, "BADVERS", 0 }, \ 74 { dns_rcode_badcookie, "BADCOOKIE", 0 }, { \ 75 0, NULL, 0 \ 76 } 77 78 #define TSIGRCODENAMES \ 79 /* extended rcodes */ \ 80 { dns_tsigerror_badsig, "BADSIG", 0 }, \ 81 { dns_tsigerror_badkey, "BADKEY", 0 }, \ 82 { dns_tsigerror_badtime, "BADTIME", 0 }, \ 83 { dns_tsigerror_badmode, "BADMODE", 0 }, \ 84 { dns_tsigerror_badname, "BADNAME", 0 }, \ 85 { dns_tsigerror_badalg, "BADALG", 0 }, \ 86 { dns_tsigerror_badtrunc, "BADTRUNC", 0 }, { \ 87 0, NULL, 0 \ 88 } 89 90 /* RFC4398 section 2.1 */ 91 92 #define CERTNAMES \ 93 { 1, "PKIX", 0 }, { 2, "SPKI", 0 }, { 3, "PGP", 0 }, \ 94 { 4, "IPKIX", 0 }, { 5, "ISPKI", 0 }, { 6, "IPGP", 0 }, \ 95 { 7, "ACPKIX", 0 }, { 8, "IACPKIX", 0 }, { 253, "URI", 0 }, \ 96 { 254, "OID", 0 }, { \ 97 0, NULL, 0 \ 98 } 99 100 /* RFC2535 section 7, RFC3110 */ 101 102 #define SECALGNAMES \ 103 { DNS_KEYALG_RSAMD5, "RSAMD5", 0 }, { DNS_KEYALG_DH, "DH", 0 }, \ 104 { DNS_KEYALG_DSA, "DSA", 0 }, \ 105 { DNS_KEYALG_RSASHA1, "RSASHA1", 0 }, \ 106 { DNS_KEYALG_NSEC3DSA, "NSEC3DSA", 0 }, \ 107 { DNS_KEYALG_NSEC3RSASHA1, "NSEC3RSASHA1", 0 }, \ 108 { DNS_KEYALG_RSASHA256, "RSASHA256", 0 }, \ 109 { DNS_KEYALG_RSASHA512, "RSASHA512", 0 }, \ 110 { DNS_KEYALG_ECCGOST, "ECCGOST", 0 }, \ 111 { DNS_KEYALG_ECDSA256, "ECDSAP256SHA256", 0 }, \ 112 { DNS_KEYALG_ECDSA256, "ECDSA256", 0 }, \ 113 { DNS_KEYALG_ECDSA384, "ECDSAP384SHA384", 0 }, \ 114 { DNS_KEYALG_ECDSA384, "ECDSA384", 0 }, \ 115 { DNS_KEYALG_ED25519, "ED25519", 0 }, \ 116 { DNS_KEYALG_ED448, "ED448", 0 }, \ 117 { DNS_KEYALG_INDIRECT, "INDIRECT", 0 }, \ 118 { DNS_KEYALG_PRIVATEDNS, "PRIVATEDNS", 0 }, \ 119 { DNS_KEYALG_PRIVATEOID, "PRIVATEOID", 0 }, { \ 120 0, NULL, 0 \ 121 } 122 123 /* RFC2535 section 7.1 */ 124 125 #define SECPROTONAMES \ 126 { 0, "NONE", 0 }, { 1, "TLS", 0 }, { 2, "EMAIL", 0 }, \ 127 { 3, "DNSSEC", 0 }, { 4, "IPSEC", 0 }, { 255, "ALL", 0 }, { \ 128 0, NULL, 0 \ 129 } 130 131 #define HASHALGNAMES \ 132 { 1, "SHA-1", 0 }, { 0, NULL, 0 } 133 134 /* RFC3658, RFC4509, RFC5933, RFC6605 */ 135 136 #define DSDIGESTNAMES \ 137 { DNS_DSDIGEST_SHA1, "SHA-1", 0 }, { DNS_DSDIGEST_SHA1, "SHA1", 0 }, \ 138 { DNS_DSDIGEST_SHA256, "SHA-256", 0 }, \ 139 { DNS_DSDIGEST_SHA256, "SHA256", 0 }, \ 140 { DNS_DSDIGEST_GOST, "GOST", 0 }, \ 141 { DNS_DSDIGEST_SHA384, "SHA-384", 0 }, \ 142 { DNS_DSDIGEST_SHA384, "SHA384", 0 }, { \ 143 0, NULL, 0 \ 144 } 145 146 struct tbl { 147 unsigned int value; 148 const char *name; 149 int flags; 150 }; 151 152 static struct tbl rcodes[] = { RCODENAMES ERCODENAMES }; 153 static struct tbl tsigrcodes[] = { RCODENAMES TSIGRCODENAMES }; 154 static struct tbl certs[] = { CERTNAMES }; 155 static struct tbl secalgs[] = { SECALGNAMES }; 156 static struct tbl secprotos[] = { SECPROTONAMES }; 157 static struct tbl hashalgs[] = { HASHALGNAMES }; 158 static struct tbl dsdigests[] = { DSDIGESTNAMES }; 159 160 static struct keyflag { 161 const char *name; 162 unsigned int value; 163 unsigned int mask; 164 } keyflags[] = { { "NOCONF", 0x4000, 0xC000 }, 165 { "NOAUTH", 0x8000, 0xC000 }, 166 { "NOKEY", 0xC000, 0xC000 }, 167 { "FLAG2", 0x2000, 0x2000 }, 168 { "EXTEND", 0x1000, 0x1000 }, 169 { "FLAG4", 0x0800, 0x0800 }, 170 { "FLAG5", 0x0400, 0x0400 }, 171 { "USER", 0x0000, 0x0300 }, 172 { "ZONE", 0x0100, 0x0300 }, 173 { "HOST", 0x0200, 0x0300 }, 174 { "NTYP3", 0x0300, 0x0300 }, 175 { "FLAG8", 0x0080, 0x0080 }, 176 { "FLAG9", 0x0040, 0x0040 }, 177 { "FLAG10", 0x0020, 0x0020 }, 178 { "FLAG11", 0x0010, 0x0010 }, 179 { "SIG0", 0x0000, 0x000F }, 180 { "SIG1", 0x0001, 0x000F }, 181 { "SIG2", 0x0002, 0x000F }, 182 { "SIG3", 0x0003, 0x000F }, 183 { "SIG4", 0x0004, 0x000F }, 184 { "SIG5", 0x0005, 0x000F }, 185 { "SIG6", 0x0006, 0x000F }, 186 { "SIG7", 0x0007, 0x000F }, 187 { "SIG8", 0x0008, 0x000F }, 188 { "SIG9", 0x0009, 0x000F }, 189 { "SIG10", 0x000A, 0x000F }, 190 { "SIG11", 0x000B, 0x000F }, 191 { "SIG12", 0x000C, 0x000F }, 192 { "SIG13", 0x000D, 0x000F }, 193 { "SIG14", 0x000E, 0x000F }, 194 { "SIG15", 0x000F, 0x000F }, 195 { "KSK", DNS_KEYFLAG_KSK, DNS_KEYFLAG_KSK }, 196 { NULL, 0, 0 } }; 197 198 static isc_result_t 199 str_totext(const char *source, isc_buffer_t *target) { 200 unsigned int l; 201 isc_region_t region; 202 203 isc_buffer_availableregion(target, ®ion); 204 l = strlen(source); 205 206 if (l > region.length) { 207 return (ISC_R_NOSPACE); 208 } 209 210 memmove(region.base, source, l); 211 isc_buffer_add(target, l); 212 return (ISC_R_SUCCESS); 213 } 214 215 static isc_result_t 216 maybe_numeric(unsigned int *valuep, isc_textregion_t *source, unsigned int max, 217 bool hex_allowed) { 218 isc_result_t result; 219 uint32_t n; 220 char buffer[NUMBERSIZE]; 221 int v; 222 223 if (!isdigit((unsigned char)source->base[0]) || 224 source->length > NUMBERSIZE - 1) 225 { 226 return (ISC_R_BADNUMBER); 227 } 228 229 /* 230 * We have a potential number. Try to parse it with 231 * isc_parse_uint32(). isc_parse_uint32() requires 232 * null termination, so we must make a copy. 233 */ 234 v = snprintf(buffer, sizeof(buffer), "%.*s", (int)source->length, 235 source->base); 236 if (v < 0 || (unsigned)v != source->length) { 237 return (ISC_R_BADNUMBER); 238 } 239 INSIST(buffer[source->length] == '\0'); 240 241 result = isc_parse_uint32(&n, buffer, 10); 242 if (result == ISC_R_BADNUMBER && hex_allowed) { 243 result = isc_parse_uint32(&n, buffer, 16); 244 } 245 if (result != ISC_R_SUCCESS) { 246 return (result); 247 } 248 if (n > max) { 249 return (ISC_R_RANGE); 250 } 251 *valuep = n; 252 return (ISC_R_SUCCESS); 253 } 254 255 static isc_result_t 256 dns_mnemonic_fromtext(unsigned int *valuep, isc_textregion_t *source, 257 struct tbl *table, unsigned int max) { 258 isc_result_t result; 259 int i; 260 261 result = maybe_numeric(valuep, source, max, false); 262 if (result != ISC_R_BADNUMBER) { 263 return (result); 264 } 265 266 for (i = 0; table[i].name != NULL; i++) { 267 unsigned int n; 268 n = strlen(table[i].name); 269 if (n == source->length && (table[i].flags & TOTEXTONLY) == 0 && 270 strncasecmp(source->base, table[i].name, n) == 0) 271 { 272 *valuep = table[i].value; 273 return (ISC_R_SUCCESS); 274 } 275 } 276 return (DNS_R_UNKNOWN); 277 } 278 279 static isc_result_t 280 dns_mnemonic_totext(unsigned int value, isc_buffer_t *target, 281 struct tbl *table) { 282 int i = 0; 283 char buf[sizeof("4294967296")]; 284 while (table[i].name != NULL) { 285 if (table[i].value == value) { 286 return (str_totext(table[i].name, target)); 287 } 288 i++; 289 } 290 snprintf(buf, sizeof(buf), "%u", value); 291 return (str_totext(buf, target)); 292 } 293 294 isc_result_t 295 dns_rcode_fromtext(dns_rcode_t *rcodep, isc_textregion_t *source) { 296 unsigned int value; 297 RETERR(dns_mnemonic_fromtext(&value, source, rcodes, 0xffff)); 298 *rcodep = value; 299 return (ISC_R_SUCCESS); 300 } 301 302 isc_result_t 303 dns_rcode_totext(dns_rcode_t rcode, isc_buffer_t *target) { 304 return (dns_mnemonic_totext(rcode, target, rcodes)); 305 } 306 307 isc_result_t 308 dns_tsigrcode_fromtext(dns_rcode_t *rcodep, isc_textregion_t *source) { 309 unsigned int value; 310 RETERR(dns_mnemonic_fromtext(&value, source, tsigrcodes, 0xffff)); 311 *rcodep = value; 312 return (ISC_R_SUCCESS); 313 } 314 315 isc_result_t 316 dns_tsigrcode_totext(dns_rcode_t rcode, isc_buffer_t *target) { 317 return (dns_mnemonic_totext(rcode, target, tsigrcodes)); 318 } 319 320 isc_result_t 321 dns_cert_fromtext(dns_cert_t *certp, isc_textregion_t *source) { 322 unsigned int value; 323 RETERR(dns_mnemonic_fromtext(&value, source, certs, 0xffff)); 324 *certp = value; 325 return (ISC_R_SUCCESS); 326 } 327 328 isc_result_t 329 dns_cert_totext(dns_cert_t cert, isc_buffer_t *target) { 330 return (dns_mnemonic_totext(cert, target, certs)); 331 } 332 333 isc_result_t 334 dns_secalg_fromtext(dns_secalg_t *secalgp, isc_textregion_t *source) { 335 unsigned int value; 336 RETERR(dns_mnemonic_fromtext(&value, source, secalgs, 0xff)); 337 *secalgp = value; 338 return (ISC_R_SUCCESS); 339 } 340 341 isc_result_t 342 dns_secalg_totext(dns_secalg_t secalg, isc_buffer_t *target) { 343 return (dns_mnemonic_totext(secalg, target, secalgs)); 344 } 345 346 void 347 dns_secalg_format(dns_secalg_t alg, char *cp, unsigned int size) { 348 isc_buffer_t b; 349 isc_region_t r; 350 isc_result_t result; 351 352 REQUIRE(cp != NULL && size > 0); 353 isc_buffer_init(&b, cp, size - 1); 354 result = dns_secalg_totext(alg, &b); 355 isc_buffer_usedregion(&b, &r); 356 r.base[r.length] = 0; 357 if (result != ISC_R_SUCCESS) { 358 r.base[0] = 0; 359 } 360 } 361 362 isc_result_t 363 dns_secproto_fromtext(dns_secproto_t *secprotop, isc_textregion_t *source) { 364 unsigned int value; 365 RETERR(dns_mnemonic_fromtext(&value, source, secprotos, 0xff)); 366 *secprotop = value; 367 return (ISC_R_SUCCESS); 368 } 369 370 isc_result_t 371 dns_secproto_totext(dns_secproto_t secproto, isc_buffer_t *target) { 372 return (dns_mnemonic_totext(secproto, target, secprotos)); 373 } 374 375 isc_result_t 376 dns_hashalg_fromtext(unsigned char *hashalg, isc_textregion_t *source) { 377 unsigned int value; 378 RETERR(dns_mnemonic_fromtext(&value, source, hashalgs, 0xff)); 379 *hashalg = value; 380 return (ISC_R_SUCCESS); 381 } 382 383 isc_result_t 384 dns_keyflags_fromtext(dns_keyflags_t *flagsp, isc_textregion_t *source) { 385 isc_result_t result; 386 char *text, *end; 387 unsigned int value = 0; 388 #ifdef notyet 389 unsigned int mask = 0; 390 #endif /* ifdef notyet */ 391 392 result = maybe_numeric(&value, source, 0xffff, true); 393 if (result == ISC_R_SUCCESS) { 394 *flagsp = value; 395 return (ISC_R_SUCCESS); 396 } 397 if (result != ISC_R_BADNUMBER) { 398 return (result); 399 } 400 401 text = source->base; 402 end = source->base + source->length; 403 404 while (text < end) { 405 struct keyflag *p; 406 unsigned int len; 407 char *delim = memchr(text, '|', end - text); 408 if (delim != NULL) { 409 len = (unsigned int)(delim - text); 410 } else { 411 len = (unsigned int)(end - text); 412 } 413 for (p = keyflags; p->name != NULL; p++) { 414 if (strncasecmp(p->name, text, len) == 0) { 415 break; 416 } 417 } 418 if (p->name == NULL) { 419 return (DNS_R_UNKNOWNFLAG); 420 } 421 value |= p->value; 422 #ifdef notyet 423 if ((mask & p->mask) != 0) { 424 warn("overlapping key flags"); 425 } 426 mask |= p->mask; 427 #endif /* ifdef notyet */ 428 text += len; 429 if (delim != NULL) { 430 text++; /* Skip "|" */ 431 } 432 } 433 *flagsp = value; 434 return (ISC_R_SUCCESS); 435 } 436 437 isc_result_t 438 dns_dsdigest_fromtext(dns_dsdigest_t *dsdigestp, isc_textregion_t *source) { 439 unsigned int value; 440 RETERR(dns_mnemonic_fromtext(&value, source, dsdigests, 0xff)); 441 *dsdigestp = value; 442 return (ISC_R_SUCCESS); 443 } 444 445 isc_result_t 446 dns_dsdigest_totext(dns_dsdigest_t dsdigest, isc_buffer_t *target) { 447 return (dns_mnemonic_totext(dsdigest, target, dsdigests)); 448 } 449 450 void 451 dns_dsdigest_format(dns_dsdigest_t typ, char *cp, unsigned int size) { 452 isc_buffer_t b; 453 isc_region_t r; 454 isc_result_t result; 455 456 REQUIRE(cp != NULL && size > 0); 457 isc_buffer_init(&b, cp, size - 1); 458 result = dns_dsdigest_totext(typ, &b); 459 isc_buffer_usedregion(&b, &r); 460 r.base[r.length] = 0; 461 if (result != ISC_R_SUCCESS) { 462 r.base[0] = 0; 463 } 464 } 465 466 /* 467 * This uses lots of hard coded values, but how often do we actually 468 * add classes? 469 */ 470 isc_result_t 471 dns_rdataclass_fromtext(dns_rdataclass_t *classp, isc_textregion_t *source) { 472 #define COMPARE(string, rdclass) \ 473 if (((sizeof(string) - 1) == source->length) && \ 474 (strncasecmp(source->base, string, source->length) == 0)) \ 475 { \ 476 *classp = rdclass; \ 477 return (ISC_R_SUCCESS); \ 478 } 479 480 switch (tolower((unsigned char)source->base[0])) { 481 case 'a': 482 COMPARE("any", dns_rdataclass_any); 483 break; 484 case 'c': 485 /* 486 * RFC1035 says the mnemonic for the CHAOS class is CH, 487 * but historical BIND practice is to call it CHAOS. 488 * We will accept both forms, but only generate CH. 489 */ 490 COMPARE("ch", dns_rdataclass_chaos); 491 COMPARE("chaos", dns_rdataclass_chaos); 492 493 if (source->length > 5 && 494 source->length < (5 + sizeof("65000")) && 495 strncasecmp("class", source->base, 5) == 0) 496 { 497 char buf[sizeof("65000")]; 498 char *endp; 499 unsigned int val; 500 501 /* 502 * source->base is not required to be NUL terminated. 503 * Copy up to remaining bytes and NUL terminate. 504 */ 505 snprintf(buf, sizeof(buf), "%.*s", 506 (int)(source->length - 5), source->base + 5); 507 val = strtoul(buf, &endp, 10); 508 if (*endp == '\0' && val <= 0xffff) { 509 *classp = (dns_rdataclass_t)val; 510 return (ISC_R_SUCCESS); 511 } 512 } 513 break; 514 case 'h': 515 COMPARE("hs", dns_rdataclass_hs); 516 COMPARE("hesiod", dns_rdataclass_hs); 517 break; 518 case 'i': 519 COMPARE("in", dns_rdataclass_in); 520 break; 521 case 'n': 522 COMPARE("none", dns_rdataclass_none); 523 break; 524 case 'r': 525 COMPARE("reserved0", dns_rdataclass_reserved0); 526 break; 527 } 528 529 #undef COMPARE 530 531 return (DNS_R_UNKNOWN); 532 } 533 534 isc_result_t 535 dns_rdataclass_totext(dns_rdataclass_t rdclass, isc_buffer_t *target) { 536 switch (rdclass) { 537 case dns_rdataclass_any: 538 return (str_totext("ANY", target)); 539 case dns_rdataclass_chaos: 540 return (str_totext("CH", target)); 541 case dns_rdataclass_hs: 542 return (str_totext("HS", target)); 543 case dns_rdataclass_in: 544 return (str_totext("IN", target)); 545 case dns_rdataclass_none: 546 return (str_totext("NONE", target)); 547 case dns_rdataclass_reserved0: 548 return (str_totext("RESERVED0", target)); 549 default: 550 return (dns_rdataclass_tounknowntext(rdclass, target)); 551 } 552 } 553 554 isc_result_t 555 dns_rdataclass_tounknowntext(dns_rdataclass_t rdclass, isc_buffer_t *target) { 556 char buf[sizeof("CLASS65535")]; 557 558 snprintf(buf, sizeof(buf), "CLASS%u", rdclass); 559 return (str_totext(buf, target)); 560 } 561 562 void 563 dns_rdataclass_format(dns_rdataclass_t rdclass, char *array, 564 unsigned int size) { 565 isc_result_t result; 566 isc_buffer_t buf; 567 568 if (size == 0U) { 569 return; 570 } 571 572 isc_buffer_init(&buf, array, size); 573 result = dns_rdataclass_totext(rdclass, &buf); 574 /* 575 * Null terminate. 576 */ 577 if (result == ISC_R_SUCCESS) { 578 if (isc_buffer_availablelength(&buf) >= 1) { 579 isc_buffer_putuint8(&buf, 0); 580 } else { 581 result = ISC_R_NOSPACE; 582 } 583 } 584 if (result != ISC_R_SUCCESS) { 585 strlcpy(array, "<unknown>", size); 586 } 587 } 588