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