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 <ctype.h>
15 #include <inttypes.h>
16 #include <stdbool.h>
17 #include <stdlib.h>
18 
19 #include <isc/buffer.h>
20 #include <isc/parseint.h>
21 #include <isc/print.h>
22 #include <isc/region.h>
23 #include <isc/result.h>
24 #include <isc/stdio.h>
25 #include <isc/string.h>
26 #include <isc/types.h>
27 #include <isc/util.h>
28 
29 #include <pk11/site.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/result.h>
39 #include <dns/secalg.h>
40 #include <dns/secproto.h>
41 
42 #define RETERR(x)                        \
43 	do {                             \
44 		isc_result_t _r = (x);   \
45 		if (_r != ISC_R_SUCCESS) \
46 			return ((_r));   \
47 	} while (0)
48 
49 #define NUMBERSIZE sizeof("037777777777") /* 2^32-1 octal + NUL */
50 
51 #define TOTEXTONLY 0x01
52 
53 #define RCODENAMES                                     \
54 	/* standard rcodes */                          \
55 	{ dns_rcode_noerror, "NOERROR", 0 },           \
56 		{ dns_rcode_formerr, "FORMERR", 0 },   \
57 		{ dns_rcode_servfail, "SERVFAIL", 0 }, \
58 		{ dns_rcode_nxdomain, "NXDOMAIN", 0 }, \
59 		{ dns_rcode_notimp, "NOTIMP", 0 },     \
60 		{ dns_rcode_refused, "REFUSED", 0 },   \
61 		{ dns_rcode_yxdomain, "YXDOMAIN", 0 }, \
62 		{ dns_rcode_yxrrset, "YXRRSET", 0 },   \
63 		{ dns_rcode_nxrrset, "NXRRSET", 0 },   \
64 		{ dns_rcode_notauth, "NOTAUTH", 0 },   \
65 		{ dns_rcode_notzone, "NOTZONE", 0 },   \
66 		{ 11, "RESERVED11", TOTEXTONLY },      \
67 		{ 12, "RESERVED12", TOTEXTONLY },      \
68 		{ 13, "RESERVED13", TOTEXTONLY },      \
69 		{ 14, "RESERVED14", TOTEXTONLY },      \
70 		{ 15, "RESERVED15", TOTEXTONLY },
71 
72 #define ERCODENAMES                                        \
73 	/* extended rcodes */                              \
74 	{ dns_rcode_badvers, "BADVERS", 0 },               \
75 		{ dns_rcode_badcookie, "BADCOOKIE", 0 }, { \
76 		0, NULL, 0                                 \
77 	}
78 
79 #define TSIGRCODENAMES                                       \
80 	/* extended rcodes */                                \
81 	{ dns_tsigerror_badsig, "BADSIG", 0 },               \
82 		{ dns_tsigerror_badkey, "BADKEY", 0 },       \
83 		{ dns_tsigerror_badtime, "BADTIME", 0 },     \
84 		{ dns_tsigerror_badmode, "BADMODE", 0 },     \
85 		{ dns_tsigerror_badname, "BADNAME", 0 },     \
86 		{ dns_tsigerror_badalg, "BADALG", 0 },       \
87 		{ dns_tsigerror_badtrunc, "BADTRUNC", 0 }, { \
88 		0, NULL, 0                                   \
89 	}
90 
91 /* RFC4398 section 2.1 */
92 
93 #define CERTNAMES                                                           \
94 	{ 1, "PKIX", 0 }, { 2, "SPKI", 0 }, { 3, "PGP", 0 },                \
95 		{ 4, "IPKIX", 0 }, { 5, "ISPKI", 0 }, { 6, "IPGP", 0 },     \
96 		{ 7, "ACPKIX", 0 }, { 8, "IACPKIX", 0 }, { 253, "URI", 0 }, \
97 		{ 254, "OID", 0 }, {                                        \
98 		0, NULL, 0                                                  \
99 	}
100 
101 /* RFC2535 section 7, RFC3110 */
102 
103 #define SECALGNAMES                                                     \
104 	{ DNS_KEYALG_RSAMD5, "RSAMD5", 0 }, { DNS_KEYALG_DH, "DH", 0 }, \
105 		{ DNS_KEYALG_DSA, "DSA", 0 },                           \
106 		{ DNS_KEYALG_RSASHA1, "RSASHA1", 0 },                   \
107 		{ DNS_KEYALG_NSEC3DSA, "NSEC3DSA", 0 },                 \
108 		{ DNS_KEYALG_NSEC3RSASHA1, "NSEC3RSASHA1", 0 },         \
109 		{ DNS_KEYALG_RSASHA256, "RSASHA256", 0 },               \
110 		{ DNS_KEYALG_RSASHA512, "RSASHA512", 0 },               \
111 		{ DNS_KEYALG_ECCGOST, "ECCGOST", 0 },                   \
112 		{ DNS_KEYALG_ECDSA256, "ECDSAP256SHA256", 0 },          \
113 		{ DNS_KEYALG_ECDSA256, "ECDSA256", 0 },                 \
114 		{ DNS_KEYALG_ECDSA384, "ECDSAP384SHA384", 0 },          \
115 		{ DNS_KEYALG_ECDSA384, "ECDSA384", 0 },                 \
116 		{ DNS_KEYALG_ED25519, "ED25519", 0 },                   \
117 		{ DNS_KEYALG_ED448, "ED448", 0 },                       \
118 		{ DNS_KEYALG_INDIRECT, "INDIRECT", 0 },                 \
119 		{ DNS_KEYALG_PRIVATEDNS, "PRIVATEDNS", 0 },             \
120 		{ DNS_KEYALG_PRIVATEOID, "PRIVATEOID", 0 }, {           \
121 		0, NULL, 0                                              \
122 	}
123 
124 /* RFC2535 section 7.1 */
125 
126 #define SECPROTONAMES                                                       \
127 	{ 0, "NONE", 0 }, { 1, "TLS", 0 }, { 2, "EMAIL", 0 },               \
128 		{ 3, "DNSSEC", 0 }, { 4, "IPSEC", 0 }, { 255, "ALL", 0 }, { \
129 		0, NULL, 0                                                  \
130 	}
131 
132 #define HASHALGNAMES \
133 	{ 1, "SHA-1", 0 }, { 0, NULL, 0 }
134 
135 /* RFC3658, RFC4509, RFC5933, RFC6605 */
136 
137 #define DSDIGESTNAMES                                                        \
138 	{ DNS_DSDIGEST_SHA1, "SHA-1", 0 }, { DNS_DSDIGEST_SHA1, "SHA1", 0 }, \
139 		{ DNS_DSDIGEST_SHA256, "SHA-256", 0 },                       \
140 		{ DNS_DSDIGEST_SHA256, "SHA256", 0 },                        \
141 		{ DNS_DSDIGEST_GOST, "GOST", 0 },                            \
142 		{ DNS_DSDIGEST_SHA384, "SHA-384", 0 },                       \
143 		{ DNS_DSDIGEST_SHA384, "SHA384", 0 }, {                      \
144 		0, NULL, 0                                                   \
145 	}
146 
147 struct tbl {
148 	unsigned int value;
149 	const char *name;
150 	int flags;
151 };
152 
153 static struct tbl rcodes[] = { RCODENAMES ERCODENAMES };
154 static struct tbl tsigrcodes[] = { RCODENAMES TSIGRCODENAMES };
155 static struct tbl certs[] = { CERTNAMES };
156 static struct tbl secalgs[] = { SECALGNAMES };
157 static struct tbl secprotos[] = { SECPROTONAMES };
158 static struct tbl hashalgs[] = { HASHALGNAMES };
159 static struct tbl dsdigests[] = { DSDIGESTNAMES };
160 
161 static struct keyflag {
162 	const char *name;
163 	unsigned int value;
164 	unsigned int mask;
165 } keyflags[] = { { "NOCONF", 0x4000, 0xC000 },
166 		 { "NOAUTH", 0x8000, 0xC000 },
167 		 { "NOKEY", 0xC000, 0xC000 },
168 		 { "FLAG2", 0x2000, 0x2000 },
169 		 { "EXTEND", 0x1000, 0x1000 },
170 		 { "FLAG4", 0x0800, 0x0800 },
171 		 { "FLAG5", 0x0400, 0x0400 },
172 		 { "USER", 0x0000, 0x0300 },
173 		 { "ZONE", 0x0100, 0x0300 },
174 		 { "HOST", 0x0200, 0x0300 },
175 		 { "NTYP3", 0x0300, 0x0300 },
176 		 { "FLAG8", 0x0080, 0x0080 },
177 		 { "FLAG9", 0x0040, 0x0040 },
178 		 { "FLAG10", 0x0020, 0x0020 },
179 		 { "FLAG11", 0x0010, 0x0010 },
180 		 { "SIG0", 0x0000, 0x000F },
181 		 { "SIG1", 0x0001, 0x000F },
182 		 { "SIG2", 0x0002, 0x000F },
183 		 { "SIG3", 0x0003, 0x000F },
184 		 { "SIG4", 0x0004, 0x000F },
185 		 { "SIG5", 0x0005, 0x000F },
186 		 { "SIG6", 0x0006, 0x000F },
187 		 { "SIG7", 0x0007, 0x000F },
188 		 { "SIG8", 0x0008, 0x000F },
189 		 { "SIG9", 0x0009, 0x000F },
190 		 { "SIG10", 0x000A, 0x000F },
191 		 { "SIG11", 0x000B, 0x000F },
192 		 { "SIG12", 0x000C, 0x000F },
193 		 { "SIG13", 0x000D, 0x000F },
194 		 { "SIG14", 0x000E, 0x000F },
195 		 { "SIG15", 0x000F, 0x000F },
196 		 { "KSK", DNS_KEYFLAG_KSK, DNS_KEYFLAG_KSK },
197 		 { NULL, 0, 0 } };
198 
199 static isc_result_t
str_totext(const char * source,isc_buffer_t * target)200 str_totext(const char *source, isc_buffer_t *target) {
201 	unsigned int l;
202 	isc_region_t region;
203 
204 	isc_buffer_availableregion(target, &region);
205 	l = strlen(source);
206 
207 	if (l > region.length) {
208 		return (ISC_R_NOSPACE);
209 	}
210 
211 	memmove(region.base, source, l);
212 	isc_buffer_add(target, l);
213 	return (ISC_R_SUCCESS);
214 }
215 
216 static isc_result_t
maybe_numeric(unsigned int * valuep,isc_textregion_t * source,unsigned int max,bool hex_allowed)217 maybe_numeric(unsigned int *valuep, isc_textregion_t *source, unsigned int max,
218 	      bool hex_allowed) {
219 	isc_result_t result;
220 	uint32_t n;
221 	char buffer[NUMBERSIZE];
222 	int v;
223 
224 	if (!isdigit((unsigned char)source->base[0]) ||
225 	    source->length > NUMBERSIZE - 1) {
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
dns_mnemonic_fromtext(unsigned int * valuep,isc_textregion_t * source,struct tbl * table,unsigned int max)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
dns_mnemonic_totext(unsigned int value,isc_buffer_t * target,struct tbl * table)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
dns_rcode_fromtext(dns_rcode_t * rcodep,isc_textregion_t * source)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
dns_rcode_totext(dns_rcode_t rcode,isc_buffer_t * target)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
dns_tsigrcode_fromtext(dns_rcode_t * rcodep,isc_textregion_t * source)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
dns_tsigrcode_totext(dns_rcode_t rcode,isc_buffer_t * target)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
dns_cert_fromtext(dns_cert_t * certp,isc_textregion_t * source)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
dns_cert_totext(dns_cert_t cert,isc_buffer_t * target)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
dns_secalg_fromtext(dns_secalg_t * secalgp,isc_textregion_t * source)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
dns_secalg_totext(dns_secalg_t secalg,isc_buffer_t * target)342 dns_secalg_totext(dns_secalg_t secalg, isc_buffer_t *target) {
343 	return (dns_mnemonic_totext(secalg, target, secalgs));
344 }
345 
346 void
dns_secalg_format(dns_secalg_t alg,char * cp,unsigned int size)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
dns_secproto_fromtext(dns_secproto_t * secprotop,isc_textregion_t * source)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
dns_secproto_totext(dns_secproto_t secproto,isc_buffer_t * target)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
dns_hashalg_fromtext(unsigned char * hashalg,isc_textregion_t * source)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
dns_keyflags_fromtext(dns_keyflags_t * flagsp,isc_textregion_t * source)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
dns_dsdigest_fromtext(dns_dsdigest_t * dsdigestp,isc_textregion_t * source)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
dns_dsdigest_totext(dns_dsdigest_t dsdigest,isc_buffer_t * target)446 dns_dsdigest_totext(dns_dsdigest_t dsdigest, isc_buffer_t *target) {
447 	return (dns_mnemonic_totext(dsdigest, target, dsdigests));
448 }
449 
450 void
dns_dsdigest_format(dns_dsdigest_t typ,char * cp,unsigned int size)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
dns_rdataclass_fromtext(dns_rdataclass_t * classp,isc_textregion_t * source)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
dns_rdataclass_totext(dns_rdataclass_t rdclass,isc_buffer_t * target)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
dns_rdataclass_tounknowntext(dns_rdataclass_t rdclass,isc_buffer_t * target)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
dns_rdataclass_format(dns_rdataclass_t rdclass,char * array,unsigned int size)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