Import bind 9.5.2 vendor sources.
[dragonfly.git] / contrib / bind-9.5.2 / lib / dns / rdata / generic / ds_43.c
blob08e5d5f7393b0371b2a5ce544ee9ea3bea35a87a
1 /*
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)
28 #include <isc/sha1.h>
29 #include <isc/sha2.h>
31 #include <dns/ds.h>
33 static inline isc_result_t
34 fromtext_ds(ARGS_FROMTEXT) {
35 isc_token_t token;
36 unsigned char c;
37 int length;
39 REQUIRE(type == 43);
41 UNUSED(type);
42 UNUSED(rdclass);
43 UNUSED(origin);
44 UNUSED(options);
45 UNUSED(callbacks);
48 * Key tag.
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));
57 * Algorithm.
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));
65 * Digest type.
67 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
68 ISC_FALSE));
69 if (token.value.as_ulong > 0xffU)
70 RETTOK(ISC_R_RANGE);
71 RETERR(uint8_tobuffer(token.value.as_ulong, target));
72 c = (unsigned char) token.value.as_ulong;
75 * Digest.
77 if (c == DNS_DSDIGEST_SHA1)
78 length = ISC_SHA1_DIGESTLENGTH;
79 else if (c == DNS_DSDIGEST_SHA256)
80 length = ISC_SHA256_DIGESTLENGTH;
81 else
82 length = -1;
83 return (isc_hex_tobuffer(lexer, target, length));
86 static inline isc_result_t
87 totext_ds(ARGS_TOTEXT) {
88 isc_region_t sr;
89 char buf[sizeof("64000 ")];
90 unsigned int n;
92 REQUIRE(rdata->type == 43);
93 REQUIRE(rdata->length != 0);
95 UNUSED(tctx);
97 dns_rdata_toregion(rdata, &sr);
100 * Key tag.
102 n = uint16_fromregion(&sr);
103 isc_region_consume(&sr, 2);
104 sprintf(buf, "%u ", n);
105 RETERR(str_totext(buf, target));
108 * Algorithm.
110 n = uint8_fromregion(&sr);
111 isc_region_consume(&sr, 1);
112 sprintf(buf, "%u ", n);
113 RETERR(str_totext(buf, target));
116 * Digest type.
118 n = uint8_fromregion(&sr);
119 isc_region_consume(&sr, 1);
120 sprintf(buf, "%u", n);
121 RETERR(str_totext(buf, target));
124 * Digest.
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) {
137 isc_region_t sr;
139 REQUIRE(type == 43);
141 UNUSED(type);
142 UNUSED(rdclass);
143 UNUSED(dctx);
144 UNUSED(options);
146 isc_buffer_activeregion(source, &sr);
149 * Check digest lengths if we know them.
151 if (sr.length < 4 ||
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
161 * detect that.
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) {
174 isc_region_t sr;
176 REQUIRE(rdata->type == 43);
177 REQUIRE(rdata->length != 0);
179 UNUSED(cctx);
181 dns_rdata_toregion(rdata, &sr);
182 return (mem_tobuffer(target, sr.base, sr.length));
185 static inline int
186 compare_ds(ARGS_COMPARE) {
187 isc_region_t r1;
188 isc_region_t r2;
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;
205 REQUIRE(type == 43);
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);
212 break;
213 case DNS_DSDIGEST_SHA256:
214 REQUIRE(ds->length == ISC_SHA256_DIGESTLENGTH);
215 break;
218 UNUSED(type);
219 UNUSED(rdclass);
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;
231 isc_region_t region;
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, &region);
243 ds->key_tag = uint16_fromregion(&region);
244 isc_region_consume(&region, 2);
245 ds->algorithm = uint8_fromregion(&region);
246 isc_region_consume(&region, 1);
247 ds->digest_type = uint8_fromregion(&region);
248 isc_region_consume(&region, 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);
255 ds->mctx = mctx;
256 return (ISC_R_SUCCESS);
259 static inline void
260 freestruct_ds(ARGS_FREESTRUCT) {
261 dns_rdata_ds_t *ds = source;
263 REQUIRE(ds != NULL);
264 REQUIRE(ds->common.rdtype == 43);
266 if (ds->mctx == NULL)
267 return;
269 if (ds->digest != NULL)
270 isc_mem_free(ds->mctx, ds->digest);
271 ds->mctx = NULL;
274 static inline isc_result_t
275 additionaldata_ds(ARGS_ADDLDATA) {
276 REQUIRE(rdata->type == 43);
278 UNUSED(rdata);
279 UNUSED(add);
280 UNUSED(arg);
282 return (ISC_R_SUCCESS);
285 static inline isc_result_t
286 digest_ds(ARGS_DIGEST) {
287 isc_region_t r;
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) {
299 REQUIRE(type == 43);
301 UNUSED(name);
302 UNUSED(type);
303 UNUSED(rdclass);
304 UNUSED(wildcard);
306 return (ISC_TRUE);
309 static inline isc_boolean_t
310 checknames_ds(ARGS_CHECKNAMES) {
312 REQUIRE(rdata->type == 43);
314 UNUSED(rdata);
315 UNUSED(owner);
316 UNUSED(bad);
318 return (ISC_TRUE);
321 #endif /* RDATA_GENERIC_DS_43_C */