1 /*
2  * Copyright (C) 2014-2016  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 #define RRTYPE_CAA_ATTRIBUTES (0)
21 
22 static unsigned char const alphanumeric[256] = {
23 	/* 0x00-0x0f */ 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
24 	/* 0x10-0x1f */ 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
25 	/* 0x20-0x2f */ 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
26 	/* 0x30-0x3f */ 1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 0, 0, 0, 0, 0, 0,
27 	/* 0x40-0x4f */ 0, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
28 	/* 0x50-0x5f */ 1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 0, 0, 0, 0, 0,
29 	/* 0x60-0x6f */ 0, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
30 	/* 0x70-0x7f */ 1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 0, 0, 0, 0, 0,
31 	/* 0x80-0x8f */ 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
32 	/* 0x90-0x9f */ 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
33 	/* 0xa0-0xaf */ 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
34 	/* 0xb0-0xbf */ 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
35 	/* 0xc0-0xcf */ 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
36 	/* 0xd0-0xdf */ 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
37 	/* 0xe0-0xef */ 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
38 	/* 0xf0-0xff */ 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
39 };
40 
41 static inline isc_result_t
fromtext_caa(ARGS_FROMTEXT)42 fromtext_caa(ARGS_FROMTEXT) {
43 	isc_token_t token;
44 	isc_textregion_t tr;
45 	isc_uint8_t flags;
46 	unsigned int i;
47 
48 	REQUIRE(type == dns_rdatatype_caa);
49 
50 	UNUSED(type);
51 	UNUSED(rdclass);
52 	UNUSED(origin);
53 	UNUSED(options);
54 	UNUSED(callbacks);
55 
56 	/* Flags. */
57 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
58 				      ISC_FALSE));
59 	if (token.value.as_ulong > 255U)
60 		RETTOK(ISC_R_RANGE);
61 	flags = (isc_uint8_t)(token.value.as_ulong & 255U);
62 	RETERR(uint8_tobuffer(flags, target));
63 
64 	/*
65 	 * Tag
66 	 */
67 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
68 				      ISC_FALSE));
69 	tr = token.value.as_textregion;
70 	for (i = 0; i < tr.length; i++)
71 		if (!alphanumeric[(unsigned char) tr.base[i]])
72 			RETTOK(DNS_R_SYNTAX);
73 	RETERR(uint8_tobuffer(tr.length, target));
74 	RETERR(mem_tobuffer(target, tr.base, tr.length));
75 
76 	/*
77 	 * Value
78 	 */
79 	RETERR(isc_lex_getmastertoken(lexer, &token,
80 				      isc_tokentype_qstring, ISC_FALSE));
81 	if (token.type != isc_tokentype_qstring &&
82 	    token.type != isc_tokentype_string)
83 		RETERR(DNS_R_SYNTAX);
84 	RETERR(multitxt_fromtext(&token.value.as_textregion, target));
85 	return (ISC_R_SUCCESS);
86 }
87 
88 static inline isc_result_t
totext_caa(ARGS_TOTEXT)89 totext_caa(ARGS_TOTEXT) {
90 	isc_region_t region;
91 	isc_uint8_t flags;
92 	char buf[256];
93 
94 	UNUSED(tctx);
95 
96 	REQUIRE(rdata->type == dns_rdatatype_caa);
97 	REQUIRE(rdata->length >= 3U);
98 	REQUIRE(rdata->data != NULL);
99 
100 	dns_rdata_toregion(rdata, &region);
101 
102 	/*
103 	 * Flags
104 	 */
105 	flags = uint8_consume_fromregion(&region);
106 	sprintf(buf, "%u ", flags);
107 	RETERR(str_totext(buf, target));
108 
109 	/*
110 	 * Tag
111 	 */
112 	RETERR(txt_totext(&region, ISC_FALSE, target));
113 	RETERR(str_totext(" ", target));
114 
115 	/*
116 	 * Value
117 	 */
118 	RETERR(multitxt_totext(&region, target));
119 	return (ISC_R_SUCCESS);
120 }
121 
122 static inline isc_result_t
fromwire_caa(ARGS_FROMWIRE)123 fromwire_caa(ARGS_FROMWIRE) {
124 	isc_region_t sr;
125 	unsigned int len, i;
126 
127 	REQUIRE(type == dns_rdatatype_caa);
128 
129 	UNUSED(type);
130 	UNUSED(rdclass);
131 	UNUSED(dctx);
132 	UNUSED(options);
133 
134 	/*
135 	 * Flags
136 	 */
137 	isc_buffer_activeregion(source, &sr);
138 	if (sr.length < 2)
139 		return (ISC_R_UNEXPECTEDEND);
140 
141 	/*
142 	 * Flags, tag length
143 	 */
144 	RETERR(mem_tobuffer(target, sr.base, 2));
145 	len = sr.base[1];
146 	isc_region_consume(&sr, 2);
147 	isc_buffer_forward(source, 2);
148 
149 	/*
150 	 * Zero length tag fields are illegal.
151 	 */
152 	if (sr.length < len || len == 0)
153 		RETERR(DNS_R_FORMERR);
154 
155 	/* Check the Tag's value */
156 	for (i = 0; i < len; i++)
157 		if (!alphanumeric[sr.base[i]])
158 			RETERR(DNS_R_FORMERR);
159 	/*
160 	 * Tag + Value
161 	 */
162 	isc_buffer_forward(source, sr.length);
163 	return (mem_tobuffer(target, sr.base, sr.length));
164 }
165 
166 static inline isc_result_t
towire_caa(ARGS_TOWIRE)167 towire_caa(ARGS_TOWIRE) {
168 	isc_region_t region;
169 
170 	REQUIRE(rdata->type == dns_rdatatype_caa);
171 	REQUIRE(rdata->length >= 3U);
172 	REQUIRE(rdata->data != NULL);
173 
174 	UNUSED(cctx);
175 
176 	dns_rdata_toregion(rdata, &region);
177 	return (mem_tobuffer(target, region.base, region.length));
178 }
179 
180 static inline int
compare_caa(ARGS_COMPARE)181 compare_caa(ARGS_COMPARE) {
182 	isc_region_t r1, r2;
183 
184 	REQUIRE(rdata1->type == rdata2->type);
185 	REQUIRE(rdata1->rdclass == rdata2->rdclass);
186 	REQUIRE(rdata1->type == dns_rdatatype_caa);
187 	REQUIRE(rdata1->length >= 3U);
188 	REQUIRE(rdata2->length >= 3U);
189 	REQUIRE(rdata1->data != NULL);
190 	REQUIRE(rdata2->data != NULL);
191 
192 	dns_rdata_toregion(rdata1, &r1);
193 	dns_rdata_toregion(rdata2, &r2);
194 	return (isc_region_compare(&r1, &r2));
195 }
196 
197 static inline isc_result_t
fromstruct_caa(ARGS_FROMSTRUCT)198 fromstruct_caa(ARGS_FROMSTRUCT) {
199 	dns_rdata_caa_t *caa = source;
200 	isc_region_t region;
201 	unsigned int i;
202 
203 	REQUIRE(type == dns_rdatatype_caa);
204 	REQUIRE(source != NULL);
205 	REQUIRE(caa->common.rdtype == type);
206 	REQUIRE(caa->common.rdclass == rdclass);
207 	REQUIRE(caa->tag != NULL && caa->tag_len != 0);
208 	REQUIRE(caa->value != NULL);
209 
210 	UNUSED(type);
211 	UNUSED(rdclass);
212 
213 	/*
214 	 * Flags
215 	 */
216 	RETERR(uint8_tobuffer(caa->flags, target));
217 
218 	/*
219 	 * Tag length
220 	 */
221 	RETERR(uint8_tobuffer(caa->tag_len, target));
222 
223 	/*
224 	 * Tag
225 	 */
226 	region.base = caa->tag;
227 	region.length = caa->tag_len;
228 	for (i = 0; i < region.length; i++)
229 		if (!alphanumeric[region.base[i]])
230 			RETERR(DNS_R_SYNTAX);
231 	RETERR(isc_buffer_copyregion(target, &region));
232 
233 	/*
234 	 * Value
235 	 */
236 	region.base = caa->value;
237 	region.length = caa->value_len;
238 	return (isc_buffer_copyregion(target, &region));
239 }
240 
241 static inline isc_result_t
tostruct_caa(ARGS_TOSTRUCT)242 tostruct_caa(ARGS_TOSTRUCT) {
243 	dns_rdata_caa_t *caa = target;
244 	isc_region_t sr;
245 
246 	REQUIRE(rdata->type == dns_rdatatype_caa);
247 	REQUIRE(target != NULL);
248 	REQUIRE(rdata->length >= 3U);
249 	REQUIRE(rdata->data != NULL);
250 
251 	caa->common.rdclass = rdata->rdclass;
252 	caa->common.rdtype = rdata->type;
253 	ISC_LINK_INIT(&caa->common, link);
254 
255 	dns_rdata_toregion(rdata, &sr);
256 
257 	/*
258 	 * Flags
259 	 */
260 	if (sr.length < 1)
261 		return (ISC_R_UNEXPECTEDEND);
262 	caa->flags = uint8_fromregion(&sr);
263 	isc_region_consume(&sr, 1);
264 
265 	/*
266 	 * Tag length
267 	 */
268 	if (sr.length < 1)
269 		return (ISC_R_UNEXPECTEDEND);
270 	caa->tag_len = uint8_fromregion(&sr);
271 	isc_region_consume(&sr, 1);
272 
273 	/*
274 	 * Tag
275 	 */
276 	if (sr.length < caa->tag_len)
277 		return (ISC_R_UNEXPECTEDEND);
278 	caa->tag = mem_maybedup(mctx, sr.base, caa->tag_len);
279 	if (caa->tag == NULL)
280 		return (ISC_R_NOMEMORY);
281 	isc_region_consume(&sr, caa->tag_len);
282 
283 	/*
284 	 * Value
285 	 */
286 	caa->value_len = sr.length;
287 	caa->value = mem_maybedup(mctx, sr.base, sr.length);
288 	if (caa->value == NULL)
289 		return (ISC_R_NOMEMORY);
290 
291 	caa->mctx = mctx;
292 	return (ISC_R_SUCCESS);
293 }
294 
295 static inline void
freestruct_caa(ARGS_FREESTRUCT)296 freestruct_caa(ARGS_FREESTRUCT) {
297 	dns_rdata_caa_t *caa = (dns_rdata_caa_t *) source;
298 
299 	REQUIRE(source != NULL);
300 	REQUIRE(caa->common.rdtype == dns_rdatatype_caa);
301 
302 	if (caa->mctx == NULL)
303 		return;
304 
305 	if (caa->tag != NULL)
306 		isc_mem_free(caa->mctx, caa->tag);
307 	if (caa->value != NULL)
308 		isc_mem_free(caa->mctx, caa->value);
309 	caa->mctx = NULL;
310 }
311 
312 static inline isc_result_t
additionaldata_caa(ARGS_ADDLDATA)313 additionaldata_caa(ARGS_ADDLDATA) {
314 	REQUIRE(rdata->type == dns_rdatatype_caa);
315 	REQUIRE(rdata->data != NULL);
316 	REQUIRE(rdata->length >= 3U);
317 
318 	UNUSED(rdata);
319 	UNUSED(add);
320 	UNUSED(arg);
321 
322 	return (ISC_R_SUCCESS);
323 }
324 
325 static inline isc_result_t
digest_caa(ARGS_DIGEST)326 digest_caa(ARGS_DIGEST) {
327 	isc_region_t r;
328 
329 	REQUIRE(rdata->type == dns_rdatatype_caa);
330 	REQUIRE(rdata->data != NULL);
331 	REQUIRE(rdata->length >= 3U);
332 
333 	dns_rdata_toregion(rdata, &r);
334 
335 	return ((digest)(arg, &r));
336 }
337 
338 static inline isc_boolean_t
checkowner_caa(ARGS_CHECKOWNER)339 checkowner_caa(ARGS_CHECKOWNER) {
340 
341 	REQUIRE(type == dns_rdatatype_caa);
342 
343 	UNUSED(name);
344 	UNUSED(type);
345 	UNUSED(rdclass);
346 	UNUSED(wildcard);
347 
348 	return (ISC_TRUE);
349 }
350 
351 static inline isc_boolean_t
checknames_caa(ARGS_CHECKNAMES)352 checknames_caa(ARGS_CHECKNAMES) {
353 
354 	REQUIRE(rdata->type == dns_rdatatype_caa);
355 	REQUIRE(rdata->data != NULL);
356 	REQUIRE(rdata->length >= 3U);
357 
358 	UNUSED(rdata);
359 	UNUSED(owner);
360 	UNUSED(bad);
361 
362 	return (ISC_TRUE);
363 }
364 
365 static inline int
casecompare_caa(ARGS_COMPARE)366 casecompare_caa(ARGS_COMPARE) {
367 	return (compare_caa(rdata1, rdata2));
368 }
369 
370 #endif /* GENERIC_CAA_257_C */
371