2 * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2002 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: ds_43.c,v 1.12 2007/06/18 23:47:43 tbox Exp $ */
20 /* draft-ietf-dnsext-delegation-signer-05.txt */
22 #ifndef RDATA_GENERIC_DS_43_C
23 #define RDATA_GENERIC_DS_43_C
25 #define RRTYPE_DS_ATTRIBUTES \
26 (DNS_RDATATYPEATTR_DNSSEC|DNS_RDATATYPEATTR_ATPARENT)
33 static inline isc_result_t
34 fromtext_ds(ARGS_FROMTEXT) {
50 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
52 if (token.value.as_ulong > 0xffffU)
54 RETERR(uint16_tobuffer(token.value.as_ulong, target));
59 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
61 RETTOK(dns_secalg_fromtext(&c, &token.value.as_textregion));
62 RETERR(mem_tobuffer(target, &c, 1));
67 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
69 if (token.value.as_ulong > 0xffU)
71 RETERR(uint8_tobuffer(token.value.as_ulong, target));
72 c = (unsigned char) token.value.as_ulong;
77 if (c == DNS_DSDIGEST_SHA1)
78 length = ISC_SHA1_DIGESTLENGTH;
79 else if (c == DNS_DSDIGEST_SHA256)
80 length = ISC_SHA256_DIGESTLENGTH;
83 return (isc_hex_tobuffer(lexer, target, length));
86 static inline isc_result_t
87 totext_ds(ARGS_TOTEXT) {
89 char buf[sizeof("64000 ")];
92 REQUIRE(rdata->type == 43);
93 REQUIRE(rdata->length != 0);
97 dns_rdata_toregion(rdata, &sr);
102 n = uint16_fromregion(&sr);
103 isc_region_consume(&sr, 2);
104 sprintf(buf, "%u ", n);
105 RETERR(str_totext(buf, target));
110 n = uint8_fromregion(&sr);
111 isc_region_consume(&sr, 1);
112 sprintf(buf, "%u ", n);
113 RETERR(str_totext(buf, target));
118 n = uint8_fromregion(&sr);
119 isc_region_consume(&sr, 1);
120 sprintf(buf, "%u", n);
121 RETERR(str_totext(buf, target));
126 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
127 RETERR(str_totext(" (", target));
128 RETERR(str_totext(tctx->linebreak, target));
129 RETERR(isc_hex_totext(&sr, tctx->width - 2, tctx->linebreak, target));
130 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
131 RETERR(str_totext(" )", target));
132 return (ISC_R_SUCCESS);
135 static inline isc_result_t
136 fromwire_ds(ARGS_FROMWIRE) {
146 isc_buffer_activeregion(source, &sr);
149 * Check digest lengths if we know them.
152 (sr.base[3] == DNS_DSDIGEST_SHA1 &&
153 sr.length < 4 + ISC_SHA1_DIGESTLENGTH) ||
154 (sr.base[3] == DNS_DSDIGEST_SHA256 &&
155 sr.length < 4 + ISC_SHA256_DIGESTLENGTH))
156 return (ISC_R_UNEXPECTEDEND);
159 * Only copy digest lengths if we know them.
160 * If there is extra data dns_rdata_fromwire() will
163 if (sr.base[3] == DNS_DSDIGEST_SHA1)
164 sr.length = 4 + ISC_SHA1_DIGESTLENGTH;
165 else if (sr.base[3] == DNS_DSDIGEST_SHA256)
166 sr.length = 4 + ISC_SHA256_DIGESTLENGTH;
168 isc_buffer_forward(source, sr.length);
169 return (mem_tobuffer(target, sr.base, sr.length));
172 static inline isc_result_t
173 towire_ds(ARGS_TOWIRE) {
176 REQUIRE(rdata->type == 43);
177 REQUIRE(rdata->length != 0);
181 dns_rdata_toregion(rdata, &sr);
182 return (mem_tobuffer(target, sr.base, sr.length));
186 compare_ds(ARGS_COMPARE) {
190 REQUIRE(rdata1->type == rdata2->type);
191 REQUIRE(rdata1->rdclass == rdata2->rdclass);
192 REQUIRE(rdata1->type == 43);
193 REQUIRE(rdata1->length != 0);
194 REQUIRE(rdata2->length != 0);
196 dns_rdata_toregion(rdata1, &r1);
197 dns_rdata_toregion(rdata2, &r2);
198 return (isc_region_compare(&r1, &r2));
201 static inline isc_result_t
202 fromstruct_ds(ARGS_FROMSTRUCT) {
203 dns_rdata_ds_t *ds = source;
206 REQUIRE(source != NULL);
207 REQUIRE(ds->common.rdtype == type);
208 REQUIRE(ds->common.rdclass == rdclass);
209 switch (ds->digest_type) {
210 case DNS_DSDIGEST_SHA1:
211 REQUIRE(ds->length == ISC_SHA1_DIGESTLENGTH);
213 case DNS_DSDIGEST_SHA256:
214 REQUIRE(ds->length == ISC_SHA256_DIGESTLENGTH);
221 RETERR(uint16_tobuffer(ds->key_tag, target));
222 RETERR(uint8_tobuffer(ds->algorithm, target));
223 RETERR(uint8_tobuffer(ds->digest_type, target));
225 return (mem_tobuffer(target, ds->digest, ds->length));
228 static inline isc_result_t
229 tostruct_ds(ARGS_TOSTRUCT) {
230 dns_rdata_ds_t *ds = target;
233 REQUIRE(rdata->type == 43);
234 REQUIRE(target != NULL);
235 REQUIRE(rdata->length != 0);
237 ds->common.rdclass = rdata->rdclass;
238 ds->common.rdtype = rdata->type;
239 ISC_LINK_INIT(&ds->common, link);
241 dns_rdata_toregion(rdata, ®ion);
243 ds->key_tag = uint16_fromregion(®ion);
244 isc_region_consume(®ion, 2);
245 ds->algorithm = uint8_fromregion(®ion);
246 isc_region_consume(®ion, 1);
247 ds->digest_type = uint8_fromregion(®ion);
248 isc_region_consume(®ion, 1);
249 ds->length = region.length;
251 ds->digest = mem_maybedup(mctx, region.base, region.length);
252 if (ds->digest == NULL)
253 return (ISC_R_NOMEMORY);
256 return (ISC_R_SUCCESS);
260 freestruct_ds(ARGS_FREESTRUCT) {
261 dns_rdata_ds_t *ds = source;
264 REQUIRE(ds->common.rdtype == 43);
266 if (ds->mctx == NULL)
269 if (ds->digest != NULL)
270 isc_mem_free(ds->mctx, ds->digest);
274 static inline isc_result_t
275 additionaldata_ds(ARGS_ADDLDATA) {
276 REQUIRE(rdata->type == 43);
282 return (ISC_R_SUCCESS);
285 static inline isc_result_t
286 digest_ds(ARGS_DIGEST) {
289 REQUIRE(rdata->type == 43);
291 dns_rdata_toregion(rdata, &r);
293 return ((digest)(arg, &r));
296 static inline isc_boolean_t
297 checkowner_ds(ARGS_CHECKOWNER) {
309 static inline isc_boolean_t
310 checknames_ds(ARGS_CHECKNAMES) {
312 REQUIRE(rdata->type == 43);
321 #endif /* RDATA_GENERIC_DS_43_C */