1 /* $NetBSD: nsec3param_51.c,v 1.4 2014/12/10 04:37:59 christos Exp $ */
4 * Copyright (C) 2008, 2009 Internet Systems Consortium, Inc. ("ISC")
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.
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.
19 /* Id: nsec3param_51.c,v 1.7 2009/12/04 21:09:34 marka Exp */
22 * Copyright (C) 2004 Nominet, Ltd.
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.
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.
39 #ifndef RDATA_GENERIC_NSEC3PARAM_51_C
40 #define RDATA_GENERIC_NSEC3PARAM_51_C
42 #include <isc/iterated_hash.h>
43 #include <isc/base32.h>
45 #define RRTYPE_NSEC3PARAM_ATTRIBUTES (DNS_RDATATYPEATTR_DNSSEC)
47 static inline isc_result_t
48 fromtext_nsec3param(ARGS_FROMTEXT
) {
50 unsigned int flags
= 0;
51 unsigned char hashalg
;
62 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_string
,
64 RETTOK(dns_hashalg_fromtext(&hashalg
, &token
.value
.as_textregion
));
65 RETERR(uint8_tobuffer(hashalg
, target
));
68 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_number
,
70 flags
= token
.value
.as_ulong
;
73 RETERR(uint8_tobuffer(flags
, target
));
76 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_number
,
78 if (token
.value
.as_ulong
> 0xffffU
)
80 RETERR(uint16_tobuffer(token
.value
.as_ulong
, target
));
83 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_string
,
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
));
90 RETERR(uint8_tobuffer(strlen(DNS_AS_STR(token
)) / 2, target
));
91 RETERR(isc_hex_decodestring(DNS_AS_STR(token
), target
));
94 return (ISC_R_SUCCESS
);
97 static inline isc_result_t
98 totext_nsec3param(ARGS_TOTEXT
) {
103 char buf
[sizeof("65535 ")];
104 isc_uint32_t iterations
;
106 REQUIRE(rdata
->type
== 51);
107 REQUIRE(rdata
->length
!= 0);
111 dns_rdata_toregion(rdata
, &sr
);
113 hash
= uint8_fromregion(&sr
);
114 isc_region_consume(&sr
, 1);
116 flags
= uint8_fromregion(&sr
);
117 isc_region_consume(&sr
, 1);
119 iterations
= uint16_fromregion(&sr
);
120 isc_region_consume(&sr
, 2);
122 sprintf(buf
, "%u ", hash
);
123 RETERR(str_totext(buf
, target
));
125 sprintf(buf
, "%u ", flags
);
126 RETERR(str_totext(buf
, target
));
128 sprintf(buf
, "%u ", iterations
);
129 RETERR(str_totext(buf
, target
));
131 j
= uint8_fromregion(&sr
);
132 isc_region_consume(&sr
, 1);
133 INSIST(j
<= sr
.length
);
138 RETERR(isc_hex_totext(&sr
, 1, "", target
));
141 RETERR(str_totext("-", target
));
143 return (ISC_R_SUCCESS
);
146 static inline isc_result_t
147 fromwire_nsec3param(ARGS_FROMWIRE
) {
149 unsigned int saltlen
;
158 isc_buffer_activeregion(source
, &sr
);
161 /* hash(1), flags(1), iterations(2), saltlen(1) */
163 RETERR(DNS_R_FORMERR
);
164 saltlen
= sr
.base
[4];
165 isc_region_consume(&sr
, 5);
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
);
175 static inline isc_result_t
176 towire_nsec3param(ARGS_TOWIRE
) {
179 REQUIRE(rdata
->type
== 51);
180 REQUIRE(rdata
->length
!= 0);
184 dns_rdata_toregion(rdata
, &sr
);
185 return (mem_tobuffer(target
, sr
.base
, sr
.length
));
189 compare_nsec3param(ARGS_COMPARE
) {
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);
199 dns_rdata_toregion(rdata1
, &r1
);
200 dns_rdata_toregion(rdata2
, &r2
);
201 return (isc_region_compare(&r1
, &r2
));
204 static inline isc_result_t
205 fromstruct_nsec3param(ARGS_FROMSTRUCT
) {
206 dns_rdata_nsec3param_t
*nsec3param
= source
;
209 REQUIRE(source
!= NULL
);
210 REQUIRE(nsec3param
->common
.rdtype
== type
);
211 REQUIRE(nsec3param
->common
.rdclass
== rdclass
);
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
);
225 static inline isc_result_t
226 tostruct_nsec3param(ARGS_TOSTRUCT
) {
228 dns_rdata_nsec3param_t
*nsec3param
= target
;
230 REQUIRE(rdata
->type
== 51);
231 REQUIRE(target
!= NULL
);
232 REQUIRE(rdata
->length
!= 0);
234 nsec3param
->common
.rdclass
= rdata
->rdclass
;
235 nsec3param
->common
.rdtype
= rdata
->type
;
236 ISC_LINK_INIT(&nsec3param
->common
, link
);
238 region
.base
= rdata
->data
;
239 region
.length
= rdata
->length
;
240 nsec3param
->hash
= uint8_consume_fromregion(®ion
);
241 nsec3param
->flags
= uint8_consume_fromregion(®ion
);
242 nsec3param
->iterations
= uint16_consume_fromregion(®ion
);
244 nsec3param
->salt_length
= uint8_consume_fromregion(®ion
);
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(®ion
, nsec3param
->salt_length
);
251 nsec3param
->mctx
= mctx
;
252 return (ISC_R_SUCCESS
);
256 freestruct_nsec3param(ARGS_FREESTRUCT
) {
257 dns_rdata_nsec3param_t
*nsec3param
= source
;
259 REQUIRE(source
!= NULL
);
260 REQUIRE(nsec3param
->common
.rdtype
== 51);
262 if (nsec3param
->mctx
== NULL
)
265 if (nsec3param
->salt
!= NULL
)
266 isc_mem_free(nsec3param
->mctx
, nsec3param
->salt
);
267 nsec3param
->mctx
= NULL
;
270 static inline isc_result_t
271 additionaldata_nsec3param(ARGS_ADDLDATA
) {
272 REQUIRE(rdata
->type
== 51);
278 return (ISC_R_SUCCESS
);
281 static inline isc_result_t
282 digest_nsec3param(ARGS_DIGEST
) {
285 REQUIRE(rdata
->type
== 51);
287 dns_rdata_toregion(rdata
, &r
);
288 return ((digest
)(arg
, &r
));
291 static inline isc_boolean_t
292 checkowner_nsec3param(ARGS_CHECKOWNER
) {
304 static inline isc_boolean_t
305 checknames_nsec3param(ARGS_CHECKNAMES
) {
307 REQUIRE(rdata
->type
== 51);
317 casecompare_nsec3param(ARGS_COMPARE
) {
318 return (compare_nsec3param(rdata1
, rdata2
));
321 #endif /* RDATA_GENERIC_NSEC3PARAM_51_C */