xref: /openbsd/usr.bin/dig/lib/dns/rdata/generic/caa_257.c (revision 097a140d)
1 /*
2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14  * PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #ifndef GENERIC_CAA_257_C
18 #define GENERIC_CAA_257_C 1
19 
20 static unsigned char const alphanumeric[256] = {
21 	/* 0x00-0x0f */ 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
22 	/* 0x10-0x1f */ 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
23 	/* 0x20-0x2f */ 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
24 	/* 0x30-0x3f */ 1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 0, 0, 0, 0, 0, 0,
25 	/* 0x40-0x4f */ 0, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
26 	/* 0x50-0x5f */ 1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 0, 0, 0, 0, 0,
27 	/* 0x60-0x6f */ 0, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
28 	/* 0x70-0x7f */ 1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 0, 0, 0, 0, 0,
29 	/* 0x80-0x8f */ 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
30 	/* 0x90-0x9f */ 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
31 	/* 0xa0-0xaf */ 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
32 	/* 0xb0-0xbf */ 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
33 	/* 0xc0-0xcf */ 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
34 	/* 0xd0-0xdf */ 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
35 	/* 0xe0-0xef */ 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
36 	/* 0xf0-0xff */ 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
37 };
38 
39 static inline isc_result_t
40 totext_caa(ARGS_TOTEXT) {
41 	isc_region_t region;
42 	uint8_t flags;
43 	char buf[256];
44 
45 	UNUSED(tctx);
46 
47 	REQUIRE(rdata->type == dns_rdatatype_caa);
48 	REQUIRE(rdata->length >= 3U);
49 	REQUIRE(rdata->data != NULL);
50 
51 	dns_rdata_toregion(rdata, &region);
52 
53 	/*
54 	 * Flags
55 	 */
56 	flags = uint8_consume_fromregion(&region);
57 	snprintf(buf, sizeof(buf), "%u ", flags);
58 	RETERR(isc_str_tobuffer(buf, target));
59 
60 	/*
61 	 * Tag
62 	 */
63 	RETERR(txt_totext(&region, 0, target));
64 	RETERR(isc_str_tobuffer(" ", target));
65 
66 	/*
67 	 * Value
68 	 */
69 	RETERR(multitxt_totext(&region, target));
70 	return (ISC_R_SUCCESS);
71 }
72 
73 static inline isc_result_t
74 fromwire_caa(ARGS_FROMWIRE) {
75 	isc_region_t sr;
76 	unsigned int len, i;
77 
78 	REQUIRE(type == dns_rdatatype_caa);
79 
80 	UNUSED(type);
81 	UNUSED(rdclass);
82 	UNUSED(dctx);
83 	UNUSED(options);
84 
85 	/*
86 	 * Flags
87 	 */
88 	isc_buffer_activeregion(source, &sr);
89 	if (sr.length < 2)
90 		return (ISC_R_UNEXPECTEDEND);
91 
92 	/*
93 	 * Flags, tag length
94 	 */
95 	RETERR(isc_mem_tobuffer(target, sr.base, 2));
96 	len = sr.base[1];
97 	isc_region_consume(&sr, 2);
98 	isc_buffer_forward(source, 2);
99 
100 	/*
101 	 * Zero length tag fields are illegal.
102 	 */
103 	if (sr.length < len || len == 0)
104 		return (DNS_R_FORMERR);
105 
106 	/* Check the Tag's value */
107 	for (i = 0; i < len; i++)
108 		if (!alphanumeric[sr.base[i]])
109 			return (DNS_R_FORMERR);
110 	/*
111 	 * Tag + Value
112 	 */
113 	isc_buffer_forward(source, sr.length);
114 	return (isc_mem_tobuffer(target, sr.base, sr.length));
115 }
116 
117 static inline isc_result_t
118 towire_caa(ARGS_TOWIRE) {
119 	isc_region_t region;
120 
121 	REQUIRE(rdata->type == dns_rdatatype_caa);
122 	REQUIRE(rdata->length >= 3U);
123 	REQUIRE(rdata->data != NULL);
124 
125 	UNUSED(cctx);
126 
127 	dns_rdata_toregion(rdata, &region);
128 	return (isc_mem_tobuffer(target, region.base, region.length));
129 }
130 
131 #endif /* GENERIC_CAA_257_C */
132