1 /*	$NetBSD: nsec3param_51.c,v 1.4 2014/12/10 04:37:59 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2008, 2009  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 /* Id: nsec3param_51.c,v 1.7 2009/12/04 21:09:34 marka Exp  */
20 
21 /*
22  * Copyright (C) 2004  Nominet, Ltd.
23  *
24  * Permission to use, copy, modify, and distribute this software for any
25  * purpose with or without fee is hereby granted, provided that the above
26  * copyright notice and this permission notice appear in all copies.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS" AND NOMINET DISCLAIMS ALL WARRANTIES WITH
29  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
30  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
31  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
32  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
33  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
34  * PERFORMANCE OF THIS SOFTWARE.
35  */
36 
37 /* RFC 5155 */
38 
39 #ifndef RDATA_GENERIC_NSEC3PARAM_51_C
40 #define RDATA_GENERIC_NSEC3PARAM_51_C
41 
42 #include <isc/iterated_hash.h>
43 #include <isc/base32.h>
44 
45 #define RRTYPE_NSEC3PARAM_ATTRIBUTES (DNS_RDATATYPEATTR_DNSSEC)
46 
47 static inline isc_result_t
fromtext_nsec3param(ARGS_FROMTEXT)48 fromtext_nsec3param(ARGS_FROMTEXT) {
49 	isc_token_t token;
50 	unsigned int flags = 0;
51 	unsigned char hashalg;
52 
53 	REQUIRE(type == 51);
54 
55 	UNUSED(type);
56 	UNUSED(rdclass);
57 	UNUSED(callbacks);
58 	UNUSED(origin);
59 	UNUSED(options);
60 
61 	/* Hash. */
62 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
63 				      ISC_FALSE));
64 	RETTOK(dns_hashalg_fromtext(&hashalg, &token.value.as_textregion));
65 	RETERR(uint8_tobuffer(hashalg, target));
66 
67 	/* Flags. */
68 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
69 				      ISC_FALSE));
70 	flags = token.value.as_ulong;
71 	if (flags > 255U)
72 		RETTOK(ISC_R_RANGE);
73 	RETERR(uint8_tobuffer(flags, target));
74 
75 	/* Iterations. */
76 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
77 				      ISC_FALSE));
78 	if (token.value.as_ulong > 0xffffU)
79 		RETTOK(ISC_R_RANGE);
80 	RETERR(uint16_tobuffer(token.value.as_ulong, target));
81 
82 	/* Salt. */
83 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
84 				      ISC_FALSE));
85 	if (token.value.as_textregion.length > (255*2))
86 		RETTOK(DNS_R_TEXTTOOLONG);
87 	if (strcmp(DNS_AS_STR(token), "-") == 0) {
88 		RETERR(uint8_tobuffer(0, target));
89 	} else {
90 		RETERR(uint8_tobuffer(strlen(DNS_AS_STR(token)) / 2, target));
91 		RETERR(isc_hex_decodestring(DNS_AS_STR(token), target));
92 	}
93 
94 	return (ISC_R_SUCCESS);
95 }
96 
97 static inline isc_result_t
totext_nsec3param(ARGS_TOTEXT)98 totext_nsec3param(ARGS_TOTEXT) {
99 	isc_region_t sr;
100 	unsigned int i, j;
101 	unsigned char hash;
102 	unsigned char flags;
103 	char buf[sizeof("65535 ")];
104 	isc_uint32_t iterations;
105 
106 	REQUIRE(rdata->type == 51);
107 	REQUIRE(rdata->length != 0);
108 
109 	UNUSED(tctx);
110 
111 	dns_rdata_toregion(rdata, &sr);
112 
113 	hash = uint8_fromregion(&sr);
114 	isc_region_consume(&sr, 1);
115 
116 	flags = uint8_fromregion(&sr);
117 	isc_region_consume(&sr, 1);
118 
119 	iterations = uint16_fromregion(&sr);
120 	isc_region_consume(&sr, 2);
121 
122 	sprintf(buf, "%u ", hash);
123 	RETERR(str_totext(buf, target));
124 
125 	sprintf(buf, "%u ", flags);
126 	RETERR(str_totext(buf, target));
127 
128 	sprintf(buf, "%u ", iterations);
129 	RETERR(str_totext(buf, target));
130 
131 	j = uint8_fromregion(&sr);
132 	isc_region_consume(&sr, 1);
133 	INSIST(j <= sr.length);
134 
135 	if (j != 0) {
136 		i = sr.length;
137 		sr.length = j;
138 		RETERR(isc_hex_totext(&sr, 1, "", target));
139 		sr.length = i - j;
140 	} else
141 		RETERR(str_totext("-", target));
142 
143 	return (ISC_R_SUCCESS);
144 }
145 
146 static inline isc_result_t
fromwire_nsec3param(ARGS_FROMWIRE)147 fromwire_nsec3param(ARGS_FROMWIRE) {
148 	isc_region_t sr, rr;
149 	unsigned int saltlen;
150 
151 	REQUIRE(type == 51);
152 
153 	UNUSED(type);
154 	UNUSED(rdclass);
155 	UNUSED(options);
156 	UNUSED(dctx);
157 
158 	isc_buffer_activeregion(source, &sr);
159 	rr = sr;
160 
161 	/* hash(1), flags(1), iterations(2), saltlen(1) */
162 	if (sr.length < 5U)
163 		RETERR(DNS_R_FORMERR);
164 	saltlen = sr.base[4];
165 	isc_region_consume(&sr, 5);
166 
167 	if (sr.length < saltlen)
168 		RETERR(DNS_R_FORMERR);
169 	isc_region_consume(&sr, saltlen);
170 	RETERR(mem_tobuffer(target, rr.base, rr.length));
171 	isc_buffer_forward(source, rr.length);
172 	return (ISC_R_SUCCESS);
173 }
174 
175 static inline isc_result_t
towire_nsec3param(ARGS_TOWIRE)176 towire_nsec3param(ARGS_TOWIRE) {
177 	isc_region_t sr;
178 
179 	REQUIRE(rdata->type == 51);
180 	REQUIRE(rdata->length != 0);
181 
182 	UNUSED(cctx);
183 
184 	dns_rdata_toregion(rdata, &sr);
185 	return (mem_tobuffer(target, sr.base, sr.length));
186 }
187 
188 static inline int
compare_nsec3param(ARGS_COMPARE)189 compare_nsec3param(ARGS_COMPARE) {
190 	isc_region_t r1;
191 	isc_region_t r2;
192 
193 	REQUIRE(rdata1->type == rdata2->type);
194 	REQUIRE(rdata1->rdclass == rdata2->rdclass);
195 	REQUIRE(rdata1->type == 51);
196 	REQUIRE(rdata1->length != 0);
197 	REQUIRE(rdata2->length != 0);
198 
199 	dns_rdata_toregion(rdata1, &r1);
200 	dns_rdata_toregion(rdata2, &r2);
201 	return (isc_region_compare(&r1, &r2));
202 }
203 
204 static inline isc_result_t
fromstruct_nsec3param(ARGS_FROMSTRUCT)205 fromstruct_nsec3param(ARGS_FROMSTRUCT) {
206 	dns_rdata_nsec3param_t *nsec3param = source;
207 
208 	REQUIRE(type == 51);
209 	REQUIRE(source != NULL);
210 	REQUIRE(nsec3param->common.rdtype == type);
211 	REQUIRE(nsec3param->common.rdclass == rdclass);
212 
213 	UNUSED(type);
214 	UNUSED(rdclass);
215 
216 	RETERR(uint8_tobuffer(nsec3param->hash, target));
217 	RETERR(uint8_tobuffer(nsec3param->flags, target));
218 	RETERR(uint16_tobuffer(nsec3param->iterations, target));
219 	RETERR(uint8_tobuffer(nsec3param->salt_length, target));
220 	RETERR(mem_tobuffer(target, nsec3param->salt,
221 			    nsec3param->salt_length));
222 	return (ISC_R_SUCCESS);
223 }
224 
225 static inline isc_result_t
tostruct_nsec3param(ARGS_TOSTRUCT)226 tostruct_nsec3param(ARGS_TOSTRUCT) {
227 	isc_region_t region;
228 	dns_rdata_nsec3param_t *nsec3param = target;
229 
230 	REQUIRE(rdata->type == 51);
231 	REQUIRE(target != NULL);
232 	REQUIRE(rdata->length != 0);
233 
234 	nsec3param->common.rdclass = rdata->rdclass;
235 	nsec3param->common.rdtype = rdata->type;
236 	ISC_LINK_INIT(&nsec3param->common, link);
237 
238 	region.base = rdata->data;
239 	region.length = rdata->length;
240 	nsec3param->hash = uint8_consume_fromregion(&region);
241 	nsec3param->flags = uint8_consume_fromregion(&region);
242 	nsec3param->iterations = uint16_consume_fromregion(&region);
243 
244 	nsec3param->salt_length = uint8_consume_fromregion(&region);
245 	nsec3param->salt = mem_maybedup(mctx, region.base,
246 					nsec3param->salt_length);
247 	if (nsec3param->salt == NULL)
248 		return (ISC_R_NOMEMORY);
249 	isc_region_consume(&region, nsec3param->salt_length);
250 
251 	nsec3param->mctx = mctx;
252 	return (ISC_R_SUCCESS);
253 }
254 
255 static inline void
freestruct_nsec3param(ARGS_FREESTRUCT)256 freestruct_nsec3param(ARGS_FREESTRUCT) {
257 	dns_rdata_nsec3param_t *nsec3param = source;
258 
259 	REQUIRE(source != NULL);
260 	REQUIRE(nsec3param->common.rdtype == 51);
261 
262 	if (nsec3param->mctx == NULL)
263 		return;
264 
265 	if (nsec3param->salt != NULL)
266 		isc_mem_free(nsec3param->mctx, nsec3param->salt);
267 	nsec3param->mctx = NULL;
268 }
269 
270 static inline isc_result_t
additionaldata_nsec3param(ARGS_ADDLDATA)271 additionaldata_nsec3param(ARGS_ADDLDATA) {
272 	REQUIRE(rdata->type == 51);
273 
274 	UNUSED(rdata);
275 	UNUSED(add);
276 	UNUSED(arg);
277 
278 	return (ISC_R_SUCCESS);
279 }
280 
281 static inline isc_result_t
digest_nsec3param(ARGS_DIGEST)282 digest_nsec3param(ARGS_DIGEST) {
283 	isc_region_t r;
284 
285 	REQUIRE(rdata->type == 51);
286 
287 	dns_rdata_toregion(rdata, &r);
288 	return ((digest)(arg, &r));
289 }
290 
291 static inline isc_boolean_t
checkowner_nsec3param(ARGS_CHECKOWNER)292 checkowner_nsec3param(ARGS_CHECKOWNER) {
293 
294        REQUIRE(type == 51);
295 
296        UNUSED(name);
297        UNUSED(type);
298        UNUSED(rdclass);
299        UNUSED(wildcard);
300 
301        return (ISC_TRUE);
302 }
303 
304 static inline isc_boolean_t
checknames_nsec3param(ARGS_CHECKNAMES)305 checknames_nsec3param(ARGS_CHECKNAMES) {
306 
307 	REQUIRE(rdata->type == 51);
308 
309 	UNUSED(rdata);
310 	UNUSED(owner);
311 	UNUSED(bad);
312 
313 	return (ISC_TRUE);
314 }
315 
316 static inline int
casecompare_nsec3param(ARGS_COMPARE)317 casecompare_nsec3param(ARGS_COMPARE) {
318 	return (compare_nsec3param(rdata1, rdata2));
319 }
320 
321 #endif	/* RDATA_GENERIC_NSEC3PARAM_51_C */
322