Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / dns / rdata / generic / ds_43.c
blobfc2e02e926d4abdc823be4ad3a6ef64c8df54237
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2002 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: ds_43.c,v 1.14 2009/12/04 22:06:37 tbox Exp */
22 /* draft-ietf-dnsext-delegation-signer-05.txt */
24 #ifndef RDATA_GENERIC_DS_43_C
25 #define RDATA_GENERIC_DS_43_C
27 #define RRTYPE_DS_ATTRIBUTES \
28 (DNS_RDATATYPEATTR_DNSSEC|DNS_RDATATYPEATTR_ATPARENT)
30 #include <isc/sha1.h>
31 #include <isc/sha2.h>
33 #include <dns/ds.h>
35 static inline isc_result_t
36 fromtext_ds(ARGS_FROMTEXT) {
37 isc_token_t token;
38 unsigned char c;
39 int length;
41 REQUIRE(type == 43);
43 UNUSED(type);
44 UNUSED(rdclass);
45 UNUSED(origin);
46 UNUSED(options);
47 UNUSED(callbacks);
50 * Key tag.
52 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
53 ISC_FALSE));
54 if (token.value.as_ulong > 0xffffU)
55 RETTOK(ISC_R_RANGE);
56 RETERR(uint16_tobuffer(token.value.as_ulong, target));
59 * Algorithm.
61 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
62 ISC_FALSE));
63 RETTOK(dns_secalg_fromtext(&c, &token.value.as_textregion));
64 RETERR(mem_tobuffer(target, &c, 1));
67 * Digest type.
69 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
70 ISC_FALSE));
71 if (token.value.as_ulong > 0xffU)
72 RETTOK(ISC_R_RANGE);
73 RETERR(uint8_tobuffer(token.value.as_ulong, target));
74 c = (unsigned char) token.value.as_ulong;
77 * Digest.
79 if (c == DNS_DSDIGEST_SHA1)
80 length = ISC_SHA1_DIGESTLENGTH;
81 else if (c == DNS_DSDIGEST_SHA256)
82 length = ISC_SHA256_DIGESTLENGTH;
83 else
84 length = -1;
85 return (isc_hex_tobuffer(lexer, target, length));
88 static inline isc_result_t
89 totext_ds(ARGS_TOTEXT) {
90 isc_region_t sr;
91 char buf[sizeof("64000 ")];
92 unsigned int n;
94 REQUIRE(rdata->type == 43);
95 REQUIRE(rdata->length != 0);
97 UNUSED(tctx);
99 dns_rdata_toregion(rdata, &sr);
102 * Key tag.
104 n = uint16_fromregion(&sr);
105 isc_region_consume(&sr, 2);
106 sprintf(buf, "%u ", n);
107 RETERR(str_totext(buf, target));
110 * Algorithm.
112 n = uint8_fromregion(&sr);
113 isc_region_consume(&sr, 1);
114 sprintf(buf, "%u ", n);
115 RETERR(str_totext(buf, target));
118 * Digest type.
120 n = uint8_fromregion(&sr);
121 isc_region_consume(&sr, 1);
122 sprintf(buf, "%u", n);
123 RETERR(str_totext(buf, target));
126 * Digest.
128 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
129 RETERR(str_totext(" (", target));
130 RETERR(str_totext(tctx->linebreak, target));
131 RETERR(isc_hex_totext(&sr, tctx->width - 2, tctx->linebreak, target));
132 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
133 RETERR(str_totext(" )", target));
134 return (ISC_R_SUCCESS);
137 static inline isc_result_t
138 fromwire_ds(ARGS_FROMWIRE) {
139 isc_region_t sr;
141 REQUIRE(type == 43);
143 UNUSED(type);
144 UNUSED(rdclass);
145 UNUSED(dctx);
146 UNUSED(options);
148 isc_buffer_activeregion(source, &sr);
151 * Check digest lengths if we know them.
153 if (sr.length < 4 ||
154 (sr.base[3] == DNS_DSDIGEST_SHA1 &&
155 sr.length < 4 + ISC_SHA1_DIGESTLENGTH) ||
156 (sr.base[3] == DNS_DSDIGEST_SHA256 &&
157 sr.length < 4 + ISC_SHA256_DIGESTLENGTH))
158 return (ISC_R_UNEXPECTEDEND);
161 * Only copy digest lengths if we know them.
162 * If there is extra data dns_rdata_fromwire() will
163 * detect that.
165 if (sr.base[3] == DNS_DSDIGEST_SHA1)
166 sr.length = 4 + ISC_SHA1_DIGESTLENGTH;
167 else if (sr.base[3] == DNS_DSDIGEST_SHA256)
168 sr.length = 4 + ISC_SHA256_DIGESTLENGTH;
170 isc_buffer_forward(source, sr.length);
171 return (mem_tobuffer(target, sr.base, sr.length));
174 static inline isc_result_t
175 towire_ds(ARGS_TOWIRE) {
176 isc_region_t sr;
178 REQUIRE(rdata->type == 43);
179 REQUIRE(rdata->length != 0);
181 UNUSED(cctx);
183 dns_rdata_toregion(rdata, &sr);
184 return (mem_tobuffer(target, sr.base, sr.length));
187 static inline int
188 compare_ds(ARGS_COMPARE) {
189 isc_region_t r1;
190 isc_region_t r2;
192 REQUIRE(rdata1->type == rdata2->type);
193 REQUIRE(rdata1->rdclass == rdata2->rdclass);
194 REQUIRE(rdata1->type == 43);
195 REQUIRE(rdata1->length != 0);
196 REQUIRE(rdata2->length != 0);
198 dns_rdata_toregion(rdata1, &r1);
199 dns_rdata_toregion(rdata2, &r2);
200 return (isc_region_compare(&r1, &r2));
203 static inline isc_result_t
204 fromstruct_ds(ARGS_FROMSTRUCT) {
205 dns_rdata_ds_t *ds = source;
207 REQUIRE(type == 43);
208 REQUIRE(source != NULL);
209 REQUIRE(ds->common.rdtype == type);
210 REQUIRE(ds->common.rdclass == rdclass);
211 switch (ds->digest_type) {
212 case DNS_DSDIGEST_SHA1:
213 REQUIRE(ds->length == ISC_SHA1_DIGESTLENGTH);
214 break;
215 case DNS_DSDIGEST_SHA256:
216 REQUIRE(ds->length == ISC_SHA256_DIGESTLENGTH);
217 break;
220 UNUSED(type);
221 UNUSED(rdclass);
223 RETERR(uint16_tobuffer(ds->key_tag, target));
224 RETERR(uint8_tobuffer(ds->algorithm, target));
225 RETERR(uint8_tobuffer(ds->digest_type, target));
227 return (mem_tobuffer(target, ds->digest, ds->length));
230 static inline isc_result_t
231 tostruct_ds(ARGS_TOSTRUCT) {
232 dns_rdata_ds_t *ds = target;
233 isc_region_t region;
235 REQUIRE(rdata->type == 43);
236 REQUIRE(target != NULL);
237 REQUIRE(rdata->length != 0);
239 ds->common.rdclass = rdata->rdclass;
240 ds->common.rdtype = rdata->type;
241 ISC_LINK_INIT(&ds->common, link);
243 dns_rdata_toregion(rdata, &region);
245 ds->key_tag = uint16_fromregion(&region);
246 isc_region_consume(&region, 2);
247 ds->algorithm = uint8_fromregion(&region);
248 isc_region_consume(&region, 1);
249 ds->digest_type = uint8_fromregion(&region);
250 isc_region_consume(&region, 1);
251 ds->length = region.length;
253 ds->digest = mem_maybedup(mctx, region.base, region.length);
254 if (ds->digest == NULL)
255 return (ISC_R_NOMEMORY);
257 ds->mctx = mctx;
258 return (ISC_R_SUCCESS);
261 static inline void
262 freestruct_ds(ARGS_FREESTRUCT) {
263 dns_rdata_ds_t *ds = source;
265 REQUIRE(ds != NULL);
266 REQUIRE(ds->common.rdtype == 43);
268 if (ds->mctx == NULL)
269 return;
271 if (ds->digest != NULL)
272 isc_mem_free(ds->mctx, ds->digest);
273 ds->mctx = NULL;
276 static inline isc_result_t
277 additionaldata_ds(ARGS_ADDLDATA) {
278 REQUIRE(rdata->type == 43);
280 UNUSED(rdata);
281 UNUSED(add);
282 UNUSED(arg);
284 return (ISC_R_SUCCESS);
287 static inline isc_result_t
288 digest_ds(ARGS_DIGEST) {
289 isc_region_t r;
291 REQUIRE(rdata->type == 43);
293 dns_rdata_toregion(rdata, &r);
295 return ((digest)(arg, &r));
298 static inline isc_boolean_t
299 checkowner_ds(ARGS_CHECKOWNER) {
301 REQUIRE(type == 43);
303 UNUSED(name);
304 UNUSED(type);
305 UNUSED(rdclass);
306 UNUSED(wildcard);
308 return (ISC_TRUE);
311 static inline isc_boolean_t
312 checknames_ds(ARGS_CHECKNAMES) {
314 REQUIRE(rdata->type == 43);
316 UNUSED(rdata);
317 UNUSED(owner);
318 UNUSED(bad);
320 return (ISC_TRUE);
323 static inline int
324 casecompare_ds(ARGS_COMPARE) {
325 return (compare_ds(rdata1, rdata2));
328 #endif /* RDATA_GENERIC_DS_43_C */