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