2 * Copyright (C) 2008, 2009 Internet Systems Consortium, Inc. ("ISC")
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.
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.
17 /* $Id: nsec3param_51.c,v 1.4.48.2 2009/01/18 23:47:41 tbox Exp $ */
20 * Copyright (C) 2004 Nominet, Ltd.
22 * Permission to use, copy, modify, and distribute this software for any
23 * purpose with or without fee is hereby granted, provided that the above
24 * copyright notice and this permission notice appear in all copies.
26 * THE SOFTWARE IS PROVIDED "AS IS" AND NOMINET DISCLAIMS ALL WARRANTIES WITH
27 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
28 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
29 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
30 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
31 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
32 * PERFORMANCE OF THIS SOFTWARE.
37 #ifndef RDATA_GENERIC_NSEC3PARAM_51_C
38 #define RDATA_GENERIC_NSEC3PARAM_51_C
40 #include <isc/iterated_hash.h>
41 #include <isc/base32.h>
43 #define RRTYPE_NSEC3PARAM_ATTRIBUTES (DNS_RDATATYPEATTR_DNSSEC)
45 static inline isc_result_t
46 fromtext_nsec3param(ARGS_FROMTEXT) {
48 unsigned int flags = 0;
49 unsigned char hashalg;
60 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
62 RETTOK(dns_hashalg_fromtext(&hashalg, &token.value.as_textregion));
63 RETERR(uint8_tobuffer(hashalg, target));
66 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
68 flags = token.value.as_ulong;
71 RETERR(uint8_tobuffer(flags, target));
74 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
76 if (token.value.as_ulong > 0xffffU)
78 RETERR(uint16_tobuffer(token.value.as_ulong, target));
81 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
83 if (token.value.as_textregion.length > (255*2))
84 RETTOK(DNS_R_TEXTTOOLONG);
85 if (strcmp(DNS_AS_STR(token), "-") == 0) {
86 RETERR(uint8_tobuffer(0, target));
88 RETERR(uint8_tobuffer(strlen(DNS_AS_STR(token)) / 2, target));
89 RETERR(isc_hex_decodestring(DNS_AS_STR(token), target));
92 return (ISC_R_SUCCESS);
95 static inline isc_result_t
96 totext_nsec3param(ARGS_TOTEXT) {
101 char buf[sizeof("65535 ")];
102 isc_uint32_t iterations;
104 REQUIRE(rdata->type == 51);
105 REQUIRE(rdata->length != 0);
109 dns_rdata_toregion(rdata, &sr);
111 hash = uint8_fromregion(&sr);
112 isc_region_consume(&sr, 1);
114 flags = uint8_fromregion(&sr);
115 isc_region_consume(&sr, 1);
117 iterations = uint16_fromregion(&sr);
118 isc_region_consume(&sr, 2);
120 sprintf(buf, "%u ", hash);
121 RETERR(str_totext(buf, target));
123 sprintf(buf, "%u ", flags);
124 RETERR(str_totext(buf, target));
126 sprintf(buf, "%u ", iterations);
127 RETERR(str_totext(buf, target));
129 j = uint8_fromregion(&sr);
130 isc_region_consume(&sr, 1);
131 INSIST(j <= sr.length);
136 RETERR(isc_hex_totext(&sr, 1, "", target));
139 RETERR(str_totext("-", target));
141 return (ISC_R_SUCCESS);
144 static inline isc_result_t
145 fromwire_nsec3param(ARGS_FROMWIRE) {
147 unsigned int saltlen;
156 isc_buffer_activeregion(source, &sr);
159 /* hash(1), flags(1), iterations(2), saltlen(1) */
161 RETERR(DNS_R_FORMERR);
162 saltlen = sr.base[4];
163 isc_region_consume(&sr, 5);
165 if (sr.length < saltlen)
166 RETERR(DNS_R_FORMERR);
167 isc_region_consume(&sr, saltlen);
168 RETERR(mem_tobuffer(target, rr.base, rr.length));
169 isc_buffer_forward(source, rr.length);
170 return (ISC_R_SUCCESS);
173 static inline isc_result_t
174 towire_nsec3param(ARGS_TOWIRE) {
177 REQUIRE(rdata->type == 51);
178 REQUIRE(rdata->length != 0);
182 dns_rdata_toregion(rdata, &sr);
183 return (mem_tobuffer(target, sr.base, sr.length));
187 compare_nsec3param(ARGS_COMPARE) {
191 REQUIRE(rdata1->type == rdata2->type);
192 REQUIRE(rdata1->rdclass == rdata2->rdclass);
193 REQUIRE(rdata1->type == 51);
194 REQUIRE(rdata1->length != 0);
195 REQUIRE(rdata2->length != 0);
197 dns_rdata_toregion(rdata1, &r1);
198 dns_rdata_toregion(rdata2, &r2);
199 return (isc_region_compare(&r1, &r2));
202 static inline isc_result_t
203 fromstruct_nsec3param(ARGS_FROMSTRUCT) {
204 dns_rdata_nsec3param_t *nsec3param = source;
207 REQUIRE(source != NULL);
208 REQUIRE(nsec3param->common.rdtype == type);
209 REQUIRE(nsec3param->common.rdclass == rdclass);
214 RETERR(uint8_tobuffer(nsec3param->hash, target));
215 RETERR(uint8_tobuffer(nsec3param->flags, target));
216 RETERR(uint16_tobuffer(nsec3param->iterations, target));
217 RETERR(uint8_tobuffer(nsec3param->salt_length, target));
218 RETERR(mem_tobuffer(target, nsec3param->salt,
219 nsec3param->salt_length));
220 return (ISC_R_SUCCESS);
223 static inline isc_result_t
224 tostruct_nsec3param(ARGS_TOSTRUCT) {
226 dns_rdata_nsec3param_t *nsec3param = target;
228 REQUIRE(rdata->type == 51);
229 REQUIRE(target != NULL);
230 REQUIRE(rdata->length != 0);
232 nsec3param->common.rdclass = rdata->rdclass;
233 nsec3param->common.rdtype = rdata->type;
234 ISC_LINK_INIT(&nsec3param->common, link);
236 region.base = rdata->data;
237 region.length = rdata->length;
238 nsec3param->hash = uint8_consume_fromregion(®ion);
239 nsec3param->flags = uint8_consume_fromregion(®ion);
240 nsec3param->iterations = uint16_consume_fromregion(®ion);
242 nsec3param->salt_length = uint8_consume_fromregion(®ion);
243 nsec3param->salt = mem_maybedup(mctx, region.base,
244 nsec3param->salt_length);
245 if (nsec3param->salt == NULL)
246 return (ISC_R_NOMEMORY);
247 isc_region_consume(®ion, nsec3param->salt_length);
249 nsec3param->mctx = mctx;
250 return (ISC_R_SUCCESS);
254 freestruct_nsec3param(ARGS_FREESTRUCT) {
255 dns_rdata_nsec3param_t *nsec3param = source;
257 REQUIRE(source != NULL);
258 REQUIRE(nsec3param->common.rdtype == 51);
260 if (nsec3param->mctx == NULL)
263 if (nsec3param->salt != NULL)
264 isc_mem_free(nsec3param->mctx, nsec3param->salt);
265 nsec3param->mctx = NULL;
268 static inline isc_result_t
269 additionaldata_nsec3param(ARGS_ADDLDATA) {
270 REQUIRE(rdata->type == 51);
276 return (ISC_R_SUCCESS);
279 static inline isc_result_t
280 digest_nsec3param(ARGS_DIGEST) {
283 REQUIRE(rdata->type == 51);
285 dns_rdata_toregion(rdata, &r);
286 return ((digest)(arg, &r));
289 static inline isc_boolean_t
290 checkowner_nsec3param(ARGS_CHECKOWNER) {
302 static inline isc_boolean_t
303 checknames_nsec3param(ARGS_CHECKNAMES) {
305 REQUIRE(rdata->type == 51);
314 #endif /* RDATA_GENERIC_NSEC3PARAM_51_C */