1 /*	$NetBSD: cds_59.c,v 1.1.1.4 2015/07/08 15:38:03 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2014, 2015  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 /* draft-ietf-dnsop-delegation-trust-maintainance-14 */
20 
21 #ifndef RDATA_GENERIC_CDS_59_C
22 #define RDATA_GENERIC_CDS_59_C
23 
24 #define RRTYPE_CDS_ATTRIBUTES 0
25 
26 #include <isc/sha1.h>
27 #include <isc/sha2.h>
28 
29 #include <dns/ds.h>
30 
31 #include "dst_gost.h"
32 
33 static inline isc_result_t
fromtext_cds(ARGS_FROMTEXT)34 fromtext_cds(ARGS_FROMTEXT) {
35 	isc_token_t token;
36 	unsigned char c;
37 	int length;
38 
39 	REQUIRE(type == 59);
40 
41 	UNUSED(type);
42 	UNUSED(rdclass);
43 	UNUSED(origin);
44 	UNUSED(options);
45 	UNUSED(callbacks);
46 
47 	/*
48 	 * Key tag.
49 	 */
50 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
51 				      ISC_FALSE));
52 	if (token.value.as_ulong > 0xffffU)
53 		RETTOK(ISC_R_RANGE);
54 	RETERR(uint16_tobuffer(token.value.as_ulong, target));
55 
56 	/*
57 	 * Algorithm.
58 	 */
59 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
60 				      ISC_FALSE));
61 	RETTOK(dns_secalg_fromtext(&c, &token.value.as_textregion));
62 	RETERR(mem_tobuffer(target, &c, 1));
63 
64 	/*
65 	 * Digest type.
66 	 */
67 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
68 				      ISC_FALSE));
69 	RETTOK(dns_dsdigest_fromtext(&c, &token.value.as_textregion));
70 	RETERR(mem_tobuffer(target, &c, 1));
71 
72 	/*
73 	 * Digest.
74 	 */
75 	switch (c) {
76 	case DNS_DSDIGEST_SHA1:
77 		length = ISC_SHA1_DIGESTLENGTH;
78 		break;
79 	case DNS_DSDIGEST_SHA256:
80 		length = ISC_SHA256_DIGESTLENGTH;
81 		break;
82 #ifdef ISC_GOST_DIGESTLENGTH
83 	case DNS_DSDIGEST_GOST:
84 		length = ISC_GOST_DIGESTLENGTH;
85 		break;
86 #endif
87 	case DNS_DSDIGEST_SHA384:
88 		length = ISC_SHA384_DIGESTLENGTH;
89 		break;
90 	default:
91 		length = -1;
92 		break;
93 	}
94 	return (isc_hex_tobuffer(lexer, target, length));
95 }
96 
97 static inline isc_result_t
totext_cds(ARGS_TOTEXT)98 totext_cds(ARGS_TOTEXT) {
99 	isc_region_t sr;
100 	char buf[sizeof("64000 ")];
101 	unsigned int n;
102 
103 	REQUIRE(rdata->type == 59);
104 	REQUIRE(rdata->length != 0);
105 
106 	UNUSED(tctx);
107 
108 	dns_rdata_toregion(rdata, &sr);
109 
110 	/*
111 	 * Key tag.
112 	 */
113 	n = uint16_fromregion(&sr);
114 	isc_region_consume(&sr, 2);
115 	sprintf(buf, "%u ", n);
116 	RETERR(str_totext(buf, target));
117 
118 	/*
119 	 * Algorithm.
120 	 */
121 	n = uint8_fromregion(&sr);
122 	isc_region_consume(&sr, 1);
123 	sprintf(buf, "%u ", n);
124 	RETERR(str_totext(buf, target));
125 
126 	/*
127 	 * Digest type.
128 	 */
129 	n = uint8_fromregion(&sr);
130 	isc_region_consume(&sr, 1);
131 	sprintf(buf, "%u", n);
132 	RETERR(str_totext(buf, target));
133 
134 	/*
135 	 * Digest.
136 	 */
137 	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
138 		RETERR(str_totext(" (", target));
139 	RETERR(str_totext(tctx->linebreak, target));
140 	if ((tctx->flags & DNS_STYLEFLAG_NOCRYPTO) == 0) {
141 		if (tctx->width == 0) /* No splitting */
142 			RETERR(isc_hex_totext(&sr, 0, "", target));
143 		else
144 			RETERR(isc_hex_totext(&sr, tctx->width - 2,
145 					      tctx->linebreak, target));
146 	} else
147 		RETERR(str_totext("[omitted]", target));
148 	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
149 		RETERR(str_totext(" )", target));
150 	return (ISC_R_SUCCESS);
151 }
152 
153 static inline isc_result_t
fromwire_cds(ARGS_FROMWIRE)154 fromwire_cds(ARGS_FROMWIRE) {
155 	isc_region_t sr;
156 
157 	REQUIRE(type == 59);
158 
159 	UNUSED(type);
160 	UNUSED(rdclass);
161 	UNUSED(dctx);
162 	UNUSED(options);
163 
164 	isc_buffer_activeregion(source, &sr);
165 
166 	/*
167 	 * Check digest lengths if we know them.
168 	 */
169 	if (sr.length < 4 ||
170 	    (sr.base[3] == DNS_DSDIGEST_SHA1 &&
171 	     sr.length < 4 + ISC_SHA1_DIGESTLENGTH) ||
172 	    (sr.base[3] == DNS_DSDIGEST_SHA256 &&
173 	     sr.length < 4 + ISC_SHA256_DIGESTLENGTH) ||
174 #ifdef ISC_GOST_DIGESTLENGTH
175 	    (sr.base[3] == DNS_DSDIGEST_GOST &&
176 	     sr.length < 4 + ISC_GOST_DIGESTLENGTH) ||
177 #endif
178 	    (sr.base[3] == DNS_DSDIGEST_SHA384 &&
179 	     sr.length < 4 + ISC_SHA384_DIGESTLENGTH))
180 		return (ISC_R_UNEXPECTEDEND);
181 
182 	/*
183 	 * Only copy digest lengths if we know them.
184 	 * If there is extra data dns_rdata_fromwire() will
185 	 * detect that.
186 	 */
187 	if (sr.base[3] == DNS_DSDIGEST_SHA1)
188 		sr.length = 4 + ISC_SHA1_DIGESTLENGTH;
189 	else if (sr.base[3] == DNS_DSDIGEST_SHA256)
190 		sr.length = 4 + ISC_SHA256_DIGESTLENGTH;
191 #ifdef ISC_GOST_DIGESTLENGTH
192 	else if (sr.base[3] == DNS_DSDIGEST_GOST)
193 		sr.length = 4 + ISC_GOST_DIGESTLENGTH;
194 #endif
195 	else if (sr.base[3] == DNS_DSDIGEST_SHA384)
196 		sr.length = 4 + ISC_SHA384_DIGESTLENGTH;
197 
198 	isc_buffer_forward(source, sr.length);
199 	return (mem_tobuffer(target, sr.base, sr.length));
200 }
201 
202 static inline isc_result_t
towire_cds(ARGS_TOWIRE)203 towire_cds(ARGS_TOWIRE) {
204 	isc_region_t sr;
205 
206 	REQUIRE(rdata->type == 59);
207 	REQUIRE(rdata->length != 0);
208 
209 	UNUSED(cctx);
210 
211 	dns_rdata_toregion(rdata, &sr);
212 	return (mem_tobuffer(target, sr.base, sr.length));
213 }
214 
215 static inline int
compare_cds(ARGS_COMPARE)216 compare_cds(ARGS_COMPARE) {
217 	isc_region_t r1;
218 	isc_region_t r2;
219 
220 	REQUIRE(rdata1->type == rdata2->type);
221 	REQUIRE(rdata1->rdclass == rdata2->rdclass);
222 	REQUIRE(rdata1->type == 59);
223 	REQUIRE(rdata1->length != 0);
224 	REQUIRE(rdata2->length != 0);
225 
226 	dns_rdata_toregion(rdata1, &r1);
227 	dns_rdata_toregion(rdata2, &r2);
228 	return (isc_region_compare(&r1, &r2));
229 }
230 
231 static inline isc_result_t
fromstruct_cds(ARGS_FROMSTRUCT)232 fromstruct_cds(ARGS_FROMSTRUCT) {
233 	dns_rdata_cds_t *ds = source;
234 
235 	REQUIRE(type == 59);
236 	REQUIRE(source != NULL);
237 	REQUIRE(ds->common.rdtype == type);
238 	REQUIRE(ds->common.rdclass == rdclass);
239 	switch (ds->digest_type) {
240 	case DNS_DSDIGEST_SHA1:
241 		REQUIRE(ds->length == ISC_SHA1_DIGESTLENGTH);
242 		break;
243 	case DNS_DSDIGEST_SHA256:
244 		REQUIRE(ds->length == ISC_SHA256_DIGESTLENGTH);
245 		break;
246 #ifdef ISC_GOST_DIGESTLENGTH
247 	case DNS_DSDIGEST_GOST:
248 		REQUIRE(ds->length == ISC_GOST_DIGESTLENGTH);
249 		break;
250 #endif
251 	case DNS_DSDIGEST_SHA384:
252 		REQUIRE(ds->length == ISC_SHA384_DIGESTLENGTH);
253 		break;
254 	}
255 
256 	UNUSED(type);
257 	UNUSED(rdclass);
258 
259 	RETERR(uint16_tobuffer(ds->key_tag, target));
260 	RETERR(uint8_tobuffer(ds->algorithm, target));
261 	RETERR(uint8_tobuffer(ds->digest_type, target));
262 
263 	return (mem_tobuffer(target, ds->digest, ds->length));
264 }
265 
266 static inline isc_result_t
tostruct_cds(ARGS_TOSTRUCT)267 tostruct_cds(ARGS_TOSTRUCT) {
268 	dns_rdata_cds_t *ds = target;
269 	isc_region_t region;
270 
271 	REQUIRE(rdata->type == 59);
272 	REQUIRE(target != NULL);
273 	REQUIRE(rdata->length != 0);
274 
275 	ds->common.rdclass = rdata->rdclass;
276 	ds->common.rdtype = rdata->type;
277 	ISC_LINK_INIT(&ds->common, link);
278 
279 	dns_rdata_toregion(rdata, &region);
280 
281 	ds->key_tag = uint16_fromregion(&region);
282 	isc_region_consume(&region, 2);
283 	ds->algorithm = uint8_fromregion(&region);
284 	isc_region_consume(&region, 1);
285 	ds->digest_type = uint8_fromregion(&region);
286 	isc_region_consume(&region, 1);
287 	ds->length = region.length;
288 
289 	ds->digest = mem_maybedup(mctx, region.base, region.length);
290 	if (ds->digest == NULL)
291 		return (ISC_R_NOMEMORY);
292 
293 	ds->mctx = mctx;
294 	return (ISC_R_SUCCESS);
295 }
296 
297 static inline void
freestruct_cds(ARGS_FREESTRUCT)298 freestruct_cds(ARGS_FREESTRUCT) {
299 	dns_rdata_cds_t *ds = source;
300 
301 	REQUIRE(ds != NULL);
302 	REQUIRE(ds->common.rdtype == 59);
303 
304 	if (ds->mctx == NULL)
305 		return;
306 
307 	if (ds->digest != NULL)
308 		isc_mem_free(ds->mctx, ds->digest);
309 	ds->mctx = NULL;
310 }
311 
312 static inline isc_result_t
additionaldata_cds(ARGS_ADDLDATA)313 additionaldata_cds(ARGS_ADDLDATA) {
314 	REQUIRE(rdata->type == 59);
315 
316 	UNUSED(rdata);
317 	UNUSED(add);
318 	UNUSED(arg);
319 
320 	return (ISC_R_SUCCESS);
321 }
322 
323 static inline isc_result_t
digest_cds(ARGS_DIGEST)324 digest_cds(ARGS_DIGEST) {
325 	isc_region_t r;
326 
327 	REQUIRE(rdata->type == 59);
328 
329 	dns_rdata_toregion(rdata, &r);
330 
331 	return ((digest)(arg, &r));
332 }
333 
334 static inline isc_boolean_t
checkowner_cds(ARGS_CHECKOWNER)335 checkowner_cds(ARGS_CHECKOWNER) {
336 
337 	REQUIRE(type == 59);
338 
339 	UNUSED(name);
340 	UNUSED(type);
341 	UNUSED(rdclass);
342 	UNUSED(wildcard);
343 
344 	return (ISC_TRUE);
345 }
346 
347 static inline isc_boolean_t
checknames_cds(ARGS_CHECKNAMES)348 checknames_cds(ARGS_CHECKNAMES) {
349 
350 	REQUIRE(rdata->type == 59);
351 
352 	UNUSED(rdata);
353 	UNUSED(owner);
354 	UNUSED(bad);
355 
356 	return (ISC_TRUE);
357 }
358 
359 static inline int
casecompare_cds(ARGS_COMPARE)360 casecompare_cds(ARGS_COMPARE) {
361 	return (compare_cds(rdata1, rdata2));
362 }
363 
364 #endif	/* RDATA_GENERIC_CDS_59_C */
365