1 /*
2  * Copyright (C) 2012, 2014, 2015  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 /* rfc6698.txt */
18 
19 #ifndef RDATA_GENERIC_TLSA_52_C
20 #define RDATA_GENERIC_TLSA_52_C
21 
22 #define RRTYPE_TLSA_ATTRIBUTES 0
23 
24 static inline isc_result_t
generic_fromtext_tlsa(ARGS_FROMTEXT)25 generic_fromtext_tlsa(ARGS_FROMTEXT) {
26 	isc_token_t token;
27 
28 	UNUSED(type);
29 	UNUSED(rdclass);
30 	UNUSED(origin);
31 	UNUSED(options);
32 	UNUSED(callbacks);
33 
34 	/*
35 	 * Certificate Usage.
36 	 */
37 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
38 				      ISC_FALSE));
39 	if (token.value.as_ulong > 0xffU)
40 		RETTOK(ISC_R_RANGE);
41 	RETERR(uint8_tobuffer(token.value.as_ulong, target));
42 
43 	/*
44 	 * Selector.
45 	 */
46 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
47 				      ISC_FALSE));
48 	if (token.value.as_ulong > 0xffU)
49 		RETTOK(ISC_R_RANGE);
50 	RETERR(uint8_tobuffer(token.value.as_ulong, target));
51 
52 	/*
53 	 * Matching type.
54 	 */
55 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
56 				      ISC_FALSE));
57 	if (token.value.as_ulong > 0xffU)
58 		RETTOK(ISC_R_RANGE);
59 	RETERR(uint8_tobuffer(token.value.as_ulong, target));
60 
61 	/*
62 	 * Certificate Association Data.
63 	 */
64 	return (isc_hex_tobuffer(lexer, target, -1));
65 }
66 
67 static inline isc_result_t
generic_totext_tlsa(ARGS_TOTEXT)68 generic_totext_tlsa(ARGS_TOTEXT) {
69 	isc_region_t sr;
70 	char buf[sizeof("64000 ")];
71 	unsigned int n;
72 
73 	REQUIRE(rdata->length != 0);
74 
75 	UNUSED(tctx);
76 
77 	dns_rdata_toregion(rdata, &sr);
78 
79 	/*
80 	 * Certificate Usage.
81 	 */
82 	n = uint8_fromregion(&sr);
83 	isc_region_consume(&sr, 1);
84 	sprintf(buf, "%u ", n);
85 	RETERR(str_totext(buf, target));
86 
87 	/*
88 	 * Selector.
89 	 */
90 	n = uint8_fromregion(&sr);
91 	isc_region_consume(&sr, 1);
92 	sprintf(buf, "%u ", n);
93 	RETERR(str_totext(buf, target));
94 
95 	/*
96 	 * Matching type.
97 	 */
98 	n = uint8_fromregion(&sr);
99 	isc_region_consume(&sr, 1);
100 	sprintf(buf, "%u", n);
101 	RETERR(str_totext(buf, target));
102 
103 	/*
104 	 * Certificate Association Data.
105 	 */
106 	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
107 		RETERR(str_totext(" (", target));
108 	RETERR(str_totext(tctx->linebreak, target));
109 	if (tctx->width == 0) /* No splitting */
110 		RETERR(isc_hex_totext(&sr, 0, "", target));
111 	else
112 		RETERR(isc_hex_totext(&sr, tctx->width - 2,
113 				      tctx->linebreak, target));
114 	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
115 		RETERR(str_totext(" )", target));
116 	return (ISC_R_SUCCESS);
117 }
118 
119 static inline isc_result_t
generic_fromwire_tlsa(ARGS_FROMWIRE)120 generic_fromwire_tlsa(ARGS_FROMWIRE) {
121 	isc_region_t sr;
122 
123 	UNUSED(type);
124 	UNUSED(rdclass);
125 	UNUSED(dctx);
126 	UNUSED(options);
127 
128 	isc_buffer_activeregion(source, &sr);
129 
130 	if (sr.length < 3)
131 		return (ISC_R_UNEXPECTEDEND);
132 
133 	isc_buffer_forward(source, sr.length);
134 	return (mem_tobuffer(target, sr.base, sr.length));
135 }
136 
137 static inline isc_result_t
fromtext_tlsa(ARGS_FROMTEXT)138 fromtext_tlsa(ARGS_FROMTEXT) {
139 
140 	REQUIRE(type == dns_rdatatype_tlsa);
141 
142 	return (generic_fromtext_tlsa(rdclass, type, lexer, origin, options,
143 				      target, callbacks));
144 }
145 
146 static inline isc_result_t
totext_tlsa(ARGS_TOTEXT)147 totext_tlsa(ARGS_TOTEXT) {
148 
149 	REQUIRE(rdata->type == dns_rdatatype_tlsa);
150 
151 	return (generic_totext_tlsa(rdata, tctx, target));
152 }
153 
154 static inline isc_result_t
fromwire_tlsa(ARGS_FROMWIRE)155 fromwire_tlsa(ARGS_FROMWIRE) {
156 
157 	REQUIRE(type == dns_rdatatype_tlsa);
158 
159 	return (generic_fromwire_tlsa(rdclass, type, source, dctx, options,
160 				      target));
161 }
162 
163 static inline isc_result_t
towire_tlsa(ARGS_TOWIRE)164 towire_tlsa(ARGS_TOWIRE) {
165 	isc_region_t sr;
166 
167 	REQUIRE(rdata->type == dns_rdatatype_tlsa);
168 	REQUIRE(rdata->length != 0);
169 
170 	UNUSED(cctx);
171 
172 	dns_rdata_toregion(rdata, &sr);
173 	return (mem_tobuffer(target, sr.base, sr.length));
174 }
175 
176 static inline int
compare_tlsa(ARGS_COMPARE)177 compare_tlsa(ARGS_COMPARE) {
178 	isc_region_t r1;
179 	isc_region_t r2;
180 
181 	REQUIRE(rdata1->type == rdata2->type);
182 	REQUIRE(rdata1->rdclass == rdata2->rdclass);
183 	REQUIRE(rdata1->type == dns_rdatatype_tlsa);
184 	REQUIRE(rdata1->length != 0);
185 	REQUIRE(rdata2->length != 0);
186 
187 	dns_rdata_toregion(rdata1, &r1);
188 	dns_rdata_toregion(rdata2, &r2);
189 	return (isc_region_compare(&r1, &r2));
190 }
191 
192 static inline isc_result_t
generic_fromstruct_tlsa(ARGS_FROMSTRUCT)193 generic_fromstruct_tlsa(ARGS_FROMSTRUCT) {
194 	dns_rdata_tlsa_t *tlsa = source;
195 
196 	REQUIRE(source != NULL);
197 	REQUIRE(tlsa->common.rdtype == type);
198 	REQUIRE(tlsa->common.rdclass == rdclass);
199 
200 	UNUSED(type);
201 	UNUSED(rdclass);
202 
203 	RETERR(uint8_tobuffer(tlsa->usage, target));
204 	RETERR(uint8_tobuffer(tlsa->selector, target));
205 	RETERR(uint8_tobuffer(tlsa->match, target));
206 
207 	return (mem_tobuffer(target, tlsa->data, tlsa->length));
208 }
209 
210 static inline isc_result_t
generic_tostruct_tlsa(ARGS_TOSTRUCT)211 generic_tostruct_tlsa(ARGS_TOSTRUCT) {
212 	dns_rdata_tlsa_t *tlsa = target;
213 	isc_region_t region;
214 
215 	REQUIRE(target != NULL);
216 	REQUIRE(rdata->length != 0);
217 
218 	tlsa->common.rdclass = rdata->rdclass;
219 	tlsa->common.rdtype = rdata->type;
220 	ISC_LINK_INIT(&tlsa->common, link);
221 
222 	dns_rdata_toregion(rdata, &region);
223 
224 	tlsa->usage = uint8_fromregion(&region);
225 	isc_region_consume(&region, 1);
226 	tlsa->selector = uint8_fromregion(&region);
227 	isc_region_consume(&region, 1);
228 	tlsa->match = uint8_fromregion(&region);
229 	isc_region_consume(&region, 1);
230 	tlsa->length = region.length;
231 
232 	tlsa->data = mem_maybedup(mctx, region.base, region.length);
233 	if (tlsa->data == NULL)
234 		return (ISC_R_NOMEMORY);
235 
236 	tlsa->mctx = mctx;
237 	return (ISC_R_SUCCESS);
238 }
239 
240 static inline void
generic_freestruct_tlsa(ARGS_FREESTRUCT)241 generic_freestruct_tlsa(ARGS_FREESTRUCT) {
242 	dns_rdata_tlsa_t *tlsa = source;
243 
244 	REQUIRE(tlsa != NULL);
245 
246 	if (tlsa->mctx == NULL)
247 		return;
248 
249 	if (tlsa->data != NULL)
250 		isc_mem_free(tlsa->mctx, tlsa->data);
251 	tlsa->mctx = NULL;
252 }
253 
254 static inline isc_result_t
fromstruct_tlsa(ARGS_FROMSTRUCT)255 fromstruct_tlsa(ARGS_FROMSTRUCT) {
256 
257 	REQUIRE(type == dns_rdatatype_tlsa);
258 
259 	return (generic_fromstruct_tlsa(rdclass, type, source, target));
260 }
261 
262 static inline isc_result_t
tostruct_tlsa(ARGS_TOSTRUCT)263 tostruct_tlsa(ARGS_TOSTRUCT) {
264 	dns_rdata_txt_t *txt = target;
265 
266 	REQUIRE(rdata->type == dns_rdatatype_tlsa);
267 	REQUIRE(target != NULL);
268 
269 	txt->common.rdclass = rdata->rdclass;
270 	txt->common.rdtype = rdata->type;
271 	ISC_LINK_INIT(&txt->common, link);
272 
273 	return (generic_tostruct_tlsa(rdata, target, mctx));
274 }
275 
276 static inline void
freestruct_tlsa(ARGS_FREESTRUCT)277 freestruct_tlsa(ARGS_FREESTRUCT) {
278 	dns_rdata_txt_t *txt = source;
279 
280 	REQUIRE(source != NULL);
281 	REQUIRE(txt->common.rdtype == dns_rdatatype_tlsa);
282 
283 	generic_freestruct_tlsa(source);
284 }
285 
286 static inline isc_result_t
additionaldata_tlsa(ARGS_ADDLDATA)287 additionaldata_tlsa(ARGS_ADDLDATA) {
288 	REQUIRE(rdata->type == dns_rdatatype_tlsa);
289 
290 	UNUSED(rdata);
291 	UNUSED(add);
292 	UNUSED(arg);
293 
294 	return (ISC_R_SUCCESS);
295 }
296 
297 static inline isc_result_t
digest_tlsa(ARGS_DIGEST)298 digest_tlsa(ARGS_DIGEST) {
299 	isc_region_t r;
300 
301 	REQUIRE(rdata->type == dns_rdatatype_tlsa);
302 
303 	dns_rdata_toregion(rdata, &r);
304 
305 	return ((digest)(arg, &r));
306 }
307 
308 static inline isc_boolean_t
checkowner_tlsa(ARGS_CHECKOWNER)309 checkowner_tlsa(ARGS_CHECKOWNER) {
310 
311 	REQUIRE(type == dns_rdatatype_tlsa);
312 
313 	UNUSED(name);
314 	UNUSED(type);
315 	UNUSED(rdclass);
316 	UNUSED(wildcard);
317 
318 	return (ISC_TRUE);
319 }
320 
321 static inline isc_boolean_t
checknames_tlsa(ARGS_CHECKNAMES)322 checknames_tlsa(ARGS_CHECKNAMES) {
323 
324 	REQUIRE(rdata->type == dns_rdatatype_tlsa);
325 
326 	UNUSED(rdata);
327 	UNUSED(owner);
328 	UNUSED(bad);
329 
330 	return (ISC_TRUE);
331 }
332 
333 static inline int
casecompare_tlsa(ARGS_COMPARE)334 casecompare_tlsa(ARGS_COMPARE) {
335 	return (compare_tlsa(rdata1, rdata2));
336 }
337 
338 #endif	/* RDATA_GENERIC_TLSA_52_C */
339