1 /* $NetBSD: rcode.c,v 1.8 2014/12/10 04:37:58 christos Exp $ */
4 * Copyright (C) 2004-2014 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1998-2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
25 #include <isc/buffer.h>
26 #include <isc/parseint.h>
27 #include <isc/print.h>
28 #include <isc/region.h>
29 #include <isc/result.h>
30 #include <isc/stdio.h>
31 #include <isc/stdlib.h>
32 #include <isc/string.h>
33 #include <isc/types.h>
38 #include <dns/dsdigest.h>
39 #include <dns/keyflags.h>
40 #include <dns/keyvalues.h>
41 #include <dns/rcode.h>
42 #include <dns/rdataclass.h>
43 #include <dns/result.h>
44 #include <dns/secalg.h>
45 #include <dns/secproto.h>
49 isc_result_t _r = (x); \
50 if (_r != ISC_R_SUCCESS) \
52 } while (/*CONSTCOND*/0)
54 #define NUMBERSIZE sizeof("037777777777") /* 2^32-1 octal + NUL */
57 /* standard rcodes */ \
58 { dns_rcode_noerror, "NOERROR", 0}, \
59 { dns_rcode_formerr, "FORMERR", 0}, \
60 { dns_rcode_servfail, "SERVFAIL", 0}, \
61 { dns_rcode_nxdomain, "NXDOMAIN", 0}, \
62 { dns_rcode_notimp, "NOTIMP", 0}, \
63 { dns_rcode_refused, "REFUSED", 0}, \
64 { dns_rcode_yxdomain, "YXDOMAIN", 0}, \
65 { dns_rcode_yxrrset, "YXRRSET", 0}, \
66 { dns_rcode_nxrrset, "NXRRSET", 0}, \
67 { dns_rcode_notauth, "NOTAUTH", 0}, \
68 { dns_rcode_notzone, "NOTZONE", 0},
71 /* extended rcodes */ \
72 { dns_rcode_badvers, "BADVERS", 0}, \
75 #define TSIGRCODENAMES \
76 /* extended rcodes */ \
77 { dns_tsigerror_badsig, "BADSIG", 0}, \
78 { dns_tsigerror_badkey, "BADKEY", 0}, \
79 { dns_tsigerror_badtime, "BADTIME", 0}, \
80 { dns_tsigerror_badmode, "BADMODE", 0}, \
81 { dns_tsigerror_badname, "BADNAME", 0}, \
82 { dns_tsigerror_badalg, "BADALG", 0}, \
83 { dns_tsigerror_badtrunc, "BADTRUNC", 0}, \
86 /* RFC4398 section 2.1 */
101 /* RFC2535 section 7, RFC3110 */
103 #define SECALGNAMES \
104 { DNS_KEYALG_RSAMD5, "RSAMD5", 0 }, \
105 { DNS_KEYALG_RSAMD5, "RSA", 0 }, \
106 { DNS_KEYALG_DH, "DH", 0 }, \
107 { DNS_KEYALG_DSA, "DSA", 0 }, \
108 { DNS_KEYALG_NSEC3DSA, "NSEC3DSA", 0 }, \
109 { DNS_KEYALG_ECC, "ECC", 0 }, \
110 { DNS_KEYALG_RSASHA1, "RSASHA1", 0 }, \
111 { DNS_KEYALG_NSEC3RSASHA1, "NSEC3RSASHA1", 0 }, \
112 { DNS_KEYALG_RSASHA256, "RSASHA256", 0 }, \
113 { DNS_KEYALG_RSASHA512, "RSASHA512", 0 }, \
114 { DNS_KEYALG_ECCGOST, "ECCGOST", 0 }, \
115 { DNS_KEYALG_ECDSA256, "ECDSAP256SHA256", 0 }, \
116 { DNS_KEYALG_ECDSA384, "ECDSAP384SHA384", 0 }, \
117 { DNS_KEYALG_INDIRECT, "INDIRECT", 0 }, \
118 { DNS_KEYALG_PRIVATEDNS, "PRIVATEDNS", 0 }, \
119 { DNS_KEYALG_PRIVATEOID, "PRIVATEOID", 0 }, \
122 /* RFC2535 section 7.1 */
124 #define SECPROTONAMES \
128 { 3, "DNSSEC", 0 }, \
133 #define HASHALGNAMES \
137 /* RFC3658, RFC4509, RFC5933, RFC6605 */
139 #define DSDIGESTNAMES \
140 { DNS_DSDIGEST_SHA1, "SHA-1", 0 }, \
141 { DNS_DSDIGEST_SHA256, "SHA-256", 0 }, \
142 { DNS_DSDIGEST_GOST, "GOST", 0 }, \
143 { DNS_DSDIGEST_SHA384, "SHA-384", 0 }, \
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
};
160 static struct keyflag
{
165 { "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
},
201 str_totext(const char *source
, isc_buffer_t
*target
) {
205 isc_buffer_availableregion(target
, ®ion
);
208 if (l
> region
.length
)
209 return (ISC_R_NOSPACE
);
211 memmove(region
.base
, source
, l
);
212 isc_buffer_add(target
, l
);
213 return (ISC_R_SUCCESS
);
217 maybe_numeric(unsigned int *valuep
, isc_textregion_t
*source
,
218 unsigned int max
, isc_boolean_t hex_allowed
)
222 char buffer
[NUMBERSIZE
];
224 if (! isdigit(source
->base
[0] & 0xff) ||
225 source
->length
> NUMBERSIZE
- 1)
226 return (ISC_R_BADNUMBER
);
229 * We have a potential number. Try to parse it with
230 * isc_parse_uint32(). isc_parse_uint32() requires
231 * null termination, so we must make a copy.
233 strncpy(buffer
, source
->base
, sizeof(buffer
));
234 buffer
[sizeof(buffer
) - 1] = '\0';
236 INSIST(buffer
[source
->length
] == '\0');
238 result
= isc_parse_uint32(&n
, buffer
, 10);
239 if (result
== ISC_R_BADNUMBER
&& hex_allowed
)
240 result
= isc_parse_uint32(&n
, buffer
, 16);
241 if (result
!= ISC_R_SUCCESS
)
244 return (ISC_R_RANGE
);
246 return (ISC_R_SUCCESS
);
250 dns_mnemonic_fromtext(unsigned int *valuep
, isc_textregion_t
*source
,
251 struct tbl
*table
, unsigned int max
)
256 result
= maybe_numeric(valuep
, source
, max
, ISC_FALSE
);
257 if (result
!= ISC_R_BADNUMBER
)
260 for (i
= 0; table
[i
].name
!= NULL
; i
++) {
262 n
= strlen(table
[i
].name
);
263 if (n
== source
->length
&&
264 strncasecmp(source
->base
, table
[i
].name
, n
) == 0) {
265 *valuep
= table
[i
].value
;
266 return (ISC_R_SUCCESS
);
269 return (DNS_R_UNKNOWN
);
273 dns_mnemonic_totext(unsigned int value
, isc_buffer_t
*target
,
277 char buf
[sizeof("4294967296")];
278 while (table
[i
].name
!= NULL
) {
279 if (table
[i
].value
== value
) {
280 return (str_totext(table
[i
].name
, target
));
284 snprintf(buf
, sizeof(buf
), "%u", value
);
285 return (str_totext(buf
, target
));
289 dns_rcode_fromtext(dns_rcode_t
*rcodep
, isc_textregion_t
*source
) {
291 RETERR(dns_mnemonic_fromtext(&value
, source
, rcodes
, 0xffff));
293 return (ISC_R_SUCCESS
);
297 dns_rcode_totext(dns_rcode_t rcode
, isc_buffer_t
*target
) {
298 return (dns_mnemonic_totext(rcode
, target
, rcodes
));
302 dns_tsigrcode_fromtext(dns_rcode_t
*rcodep
, isc_textregion_t
*source
) {
304 RETERR(dns_mnemonic_fromtext(&value
, source
, tsigrcodes
, 0xffff));
306 return (ISC_R_SUCCESS
);
310 dns_tsigrcode_totext(dns_rcode_t rcode
, isc_buffer_t
*target
) {
311 return (dns_mnemonic_totext(rcode
, target
, tsigrcodes
));
315 dns_cert_fromtext(dns_cert_t
*certp
, isc_textregion_t
*source
) {
317 RETERR(dns_mnemonic_fromtext(&value
, source
, certs
, 0xffff));
319 return (ISC_R_SUCCESS
);
323 dns_cert_totext(dns_cert_t cert
, isc_buffer_t
*target
) {
324 return (dns_mnemonic_totext(cert
, target
, certs
));
328 dns_secalg_fromtext(dns_secalg_t
*secalgp
, isc_textregion_t
*source
) {
330 RETERR(dns_mnemonic_fromtext(&value
, source
, secalgs
, 0xff));
332 return (ISC_R_SUCCESS
);
336 dns_secalg_totext(dns_secalg_t secalg
, isc_buffer_t
*target
) {
337 return (dns_mnemonic_totext(secalg
, target
, secalgs
));
341 dns_secalg_format(dns_secalg_t alg
, char *cp
, unsigned int size
) {
346 REQUIRE(cp
!= NULL
&& size
> 0);
347 isc_buffer_init(&b
, cp
, size
- 1);
348 result
= dns_secalg_totext(alg
, &b
);
349 isc_buffer_usedregion(&b
, &r
);
350 r
.base
[r
.length
] = 0;
351 if (result
!= ISC_R_SUCCESS
)
356 dns_secproto_fromtext(dns_secproto_t
*secprotop
, isc_textregion_t
*source
) {
358 RETERR(dns_mnemonic_fromtext(&value
, source
, secprotos
, 0xff));
360 return (ISC_R_SUCCESS
);
364 dns_secproto_totext(dns_secproto_t secproto
, isc_buffer_t
*target
) {
365 return (dns_mnemonic_totext(secproto
, target
, secprotos
));
369 dns_hashalg_fromtext(unsigned char *hashalg
, isc_textregion_t
*source
) {
371 RETERR(dns_mnemonic_fromtext(&value
, source
, hashalgs
, 0xff));
373 return (ISC_R_SUCCESS
);
377 dns_keyflags_fromtext(dns_keyflags_t
*flagsp
, isc_textregion_t
*source
)
381 unsigned int value
, mask
;
383 result
= maybe_numeric(&value
, source
, 0xffff, ISC_TRUE
);
384 if (result
== ISC_R_SUCCESS
) {
386 return (ISC_R_SUCCESS
);
388 if (result
!= ISC_R_BADNUMBER
)
392 end
= source
->base
+ source
->length
;
398 char *delim
= memchr(text
, '|', end
- text
);
400 len
= (unsigned int)(delim
- text
);
402 len
= (unsigned int)(end
- text
);
403 for (p
= keyflags
; p
->name
!= NULL
; p
++) {
404 if (strncasecmp(p
->name
, text
, len
) == 0)
408 return (DNS_R_UNKNOWNFLAG
);
411 if ((mask
& p
->mask
) != 0)
412 warn("overlapping key flags");
417 text
++; /* Skip "|" */
420 return (ISC_R_SUCCESS
);
424 dns_dsdigest_fromtext(dns_dsdigest_t
*dsdigestp
, isc_textregion_t
*source
) {
426 RETERR(dns_mnemonic_fromtext(&value
, source
, dsdigests
, 0xff));
428 return (ISC_R_SUCCESS
);
432 dns_dsdigest_totext(dns_dsdigest_t dsdigest
, isc_buffer_t
*target
) {
433 return (dns_mnemonic_totext(dsdigest
, target
, dsdigests
));
437 dns_dsdigest_format(dns_dsdigest_t typ
, char *cp
, unsigned int size
) {
442 REQUIRE(cp
!= NULL
&& size
> 0);
443 isc_buffer_init(&b
, cp
, size
- 1);
444 result
= dns_dsdigest_totext(typ
, &b
);
445 isc_buffer_usedregion(&b
, &r
);
446 r
.base
[r
.length
] = 0;
447 if (result
!= ISC_R_SUCCESS
)
452 * This uses lots of hard coded values, but how often do we actually
456 dns_rdataclass_fromtext(dns_rdataclass_t
*classp
, isc_textregion_t
*source
) {
457 #define COMPARE(string, rdclass) \
458 if (((sizeof(string) - 1) == source->length) \
459 && (strncasecmp(source->base, string, source->length) == 0)) { \
461 return (ISC_R_SUCCESS); \
464 switch (tolower((unsigned char)source
->base
[0])) {
466 COMPARE("any", dns_rdataclass_any
);
470 * RFC1035 says the mnemonic for the CHAOS class is CH,
471 * but historical BIND practice is to call it CHAOS.
472 * We will accept both forms, but only generate CH.
474 COMPARE("ch", dns_rdataclass_chaos
);
475 COMPARE("chaos", dns_rdataclass_chaos
);
477 if (source
->length
> 5 &&
478 source
->length
< (5 + sizeof("65000")) &&
479 strncasecmp("class", source
->base
, 5) == 0) {
480 char buf
[sizeof("65000")];
484 strncpy(buf
, source
->base
+ 5, source
->length
- 5);
485 buf
[source
->length
- 5] = '\0';
486 val
= strtoul(buf
, &endp
, 10);
487 if (*endp
== '\0' && val
<= 0xffff) {
488 *classp
= (dns_rdataclass_t
)val
;
489 return (ISC_R_SUCCESS
);
494 COMPARE("hs", dns_rdataclass_hs
);
495 COMPARE("hesiod", dns_rdataclass_hs
);
498 COMPARE("in", dns_rdataclass_in
);
501 COMPARE("none", dns_rdataclass_none
);
504 COMPARE("reserved0", dns_rdataclass_reserved0
);
510 return (DNS_R_UNKNOWN
);
514 dns_rdataclass_totext(dns_rdataclass_t rdclass
, isc_buffer_t
*target
) {
515 char buf
[sizeof("CLASS65535")];
518 case dns_rdataclass_any
:
519 return (str_totext("ANY", target
));
520 case dns_rdataclass_chaos
:
521 return (str_totext("CH", target
));
522 case dns_rdataclass_hs
:
523 return (str_totext("HS", target
));
524 case dns_rdataclass_in
:
525 return (str_totext("IN", target
));
526 case dns_rdataclass_none
:
527 return (str_totext("NONE", target
));
528 case dns_rdataclass_reserved0
:
529 return (str_totext("RESERVED0", target
));
531 snprintf(buf
, sizeof(buf
), "CLASS%u", rdclass
);
532 return (str_totext(buf
, target
));
537 dns_rdataclass_format(dns_rdataclass_t rdclass
,
538 char *array
, unsigned int size
)
546 isc_buffer_init(&buf
, array
, size
);
547 result
= dns_rdataclass_totext(rdclass
, &buf
);
551 if (result
== ISC_R_SUCCESS
) {
552 if (isc_buffer_availablelength(&buf
) >= 1)
553 isc_buffer_putuint8(&buf
, 0);
555 result
= ISC_R_NOSPACE
;
557 if (result
!= ISC_R_SUCCESS
)
558 strlcpy(array
, "<unknown>", size
);