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