]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/bind9/lib/dns/rdata/generic/ds_43.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / bind9 / lib / dns / rdata / generic / ds_43.c
1 /*
2  * Copyright (C) 2004, 2005, 2007, 2009, 2010, 2012  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 2002  Internet Software Consortium.
4  *
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.
8  *
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.
16  */
17
18 /* $Id$ */
19
20 /* draft-ietf-dnsext-delegation-signer-05.txt */
21
22 #ifndef RDATA_GENERIC_DS_43_C
23 #define RDATA_GENERIC_DS_43_C
24
25 #define RRTYPE_DS_ATTRIBUTES \
26         (DNS_RDATATYPEATTR_DNSSEC|DNS_RDATATYPEATTR_ATPARENT)
27
28 #include <isc/sha1.h>
29 #include <isc/sha2.h>
30
31 #include <dns/ds.h>
32
33 static inline isc_result_t
34 fromtext_ds(ARGS_FROMTEXT) {
35         isc_token_t token;
36         unsigned char c;
37         int length;
38
39         REQUIRE(type == 43);
40
41         UNUSED(type);
42         UNUSED(rdclass);
43         UNUSED(origin);
44         UNUSED(options);
45         UNUSED(callbacks);
46
47         /*
48          * Key tag.
49          */
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));
55
56         /*
57          * Algorithm.
58          */
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));
63
64         /*
65          * Digest type.
66          */
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;
73
74         /*
75          * Digest.
76          */
77         switch (c) {
78         case DNS_DSDIGEST_SHA1:
79                 length = ISC_SHA1_DIGESTLENGTH;
80                 break;
81         case DNS_DSDIGEST_SHA256:
82                 length = ISC_SHA256_DIGESTLENGTH;
83                 break;
84         case DNS_DSDIGEST_GOST:
85                 length = ISC_GOST_DIGESTLENGTH;
86                 break;
87         case DNS_DSDIGEST_SHA384:
88                 length = ISC_SHA384_DIGESTLENGTH;
89                 break;
90         default:
91                 length = -1;
92                 break;
93         }
94         return (isc_hex_tobuffer(lexer, target, length));
95 }
96
97 static inline isc_result_t
98 totext_ds(ARGS_TOTEXT) {
99         isc_region_t sr;
100         char buf[sizeof("64000 ")];
101         unsigned int n;
102
103         REQUIRE(rdata->type == 43);
104         REQUIRE(rdata->length != 0);
105
106         UNUSED(tctx);
107
108         dns_rdata_toregion(rdata, &sr);
109
110         /*
111          * Key tag.
112          */
113         n = uint16_fromregion(&sr);
114         isc_region_consume(&sr, 2);
115         sprintf(buf, "%u ", n);
116         RETERR(str_totext(buf, target));
117
118         /*
119          * Algorithm.
120          */
121         n = uint8_fromregion(&sr);
122         isc_region_consume(&sr, 1);
123         sprintf(buf, "%u ", n);
124         RETERR(str_totext(buf, target));
125
126         /*
127          * Digest type.
128          */
129         n = uint8_fromregion(&sr);
130         isc_region_consume(&sr, 1);
131         sprintf(buf, "%u", n);
132         RETERR(str_totext(buf, target));
133
134         /*
135          * Digest.
136          */
137         if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
138                 RETERR(str_totext(" (", target));
139         RETERR(str_totext(tctx->linebreak, target));
140         RETERR(isc_hex_totext(&sr, tctx->width - 2, tctx->linebreak, target));
141         if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
142                 RETERR(str_totext(" )", target));
143         return (ISC_R_SUCCESS);
144 }
145
146 static inline isc_result_t
147 fromwire_ds(ARGS_FROMWIRE) {
148         isc_region_t sr;
149
150         REQUIRE(type == 43);
151
152         UNUSED(type);
153         UNUSED(rdclass);
154         UNUSED(dctx);
155         UNUSED(options);
156
157         isc_buffer_activeregion(source, &sr);
158
159         /*
160          * Check digest lengths if we know them.
161          */
162         if (sr.length < 4 ||
163             (sr.base[3] == DNS_DSDIGEST_SHA1 &&
164              sr.length < 4 + ISC_SHA1_DIGESTLENGTH) ||
165             (sr.base[3] == DNS_DSDIGEST_SHA256 &&
166              sr.length < 4 + ISC_SHA256_DIGESTLENGTH) ||
167             (sr.base[3] == DNS_DSDIGEST_GOST &&
168              sr.length < 4 + ISC_GOST_DIGESTLENGTH) ||
169             (sr.base[3] == DNS_DSDIGEST_SHA384 &&
170              sr.length < 4 + ISC_SHA384_DIGESTLENGTH))
171                 return (ISC_R_UNEXPECTEDEND);
172
173         /*
174          * Only copy digest lengths if we know them.
175          * If there is extra data dns_rdata_fromwire() will
176          * detect that.
177          */
178         if (sr.base[3] == DNS_DSDIGEST_SHA1)
179                 sr.length = 4 + ISC_SHA1_DIGESTLENGTH;
180         else if (sr.base[3] == DNS_DSDIGEST_SHA256)
181                 sr.length = 4 + ISC_SHA256_DIGESTLENGTH;
182         else if (sr.base[3] == DNS_DSDIGEST_GOST)
183                 sr.length = 4 + ISC_GOST_DIGESTLENGTH;
184         else if (sr.base[3] == DNS_DSDIGEST_SHA384)
185                 sr.length = 4 + ISC_SHA384_DIGESTLENGTH;
186
187         isc_buffer_forward(source, sr.length);
188         return (mem_tobuffer(target, sr.base, sr.length));
189 }
190
191 static inline isc_result_t
192 towire_ds(ARGS_TOWIRE) {
193         isc_region_t sr;
194
195         REQUIRE(rdata->type == 43);
196         REQUIRE(rdata->length != 0);
197
198         UNUSED(cctx);
199
200         dns_rdata_toregion(rdata, &sr);
201         return (mem_tobuffer(target, sr.base, sr.length));
202 }
203
204 static inline int
205 compare_ds(ARGS_COMPARE) {
206         isc_region_t r1;
207         isc_region_t r2;
208
209         REQUIRE(rdata1->type == rdata2->type);
210         REQUIRE(rdata1->rdclass == rdata2->rdclass);
211         REQUIRE(rdata1->type == 43);
212         REQUIRE(rdata1->length != 0);
213         REQUIRE(rdata2->length != 0);
214
215         dns_rdata_toregion(rdata1, &r1);
216         dns_rdata_toregion(rdata2, &r2);
217         return (isc_region_compare(&r1, &r2));
218 }
219
220 static inline isc_result_t
221 fromstruct_ds(ARGS_FROMSTRUCT) {
222         dns_rdata_ds_t *ds = source;
223
224         REQUIRE(type == 43);
225         REQUIRE(source != NULL);
226         REQUIRE(ds->common.rdtype == type);
227         REQUIRE(ds->common.rdclass == rdclass);
228         switch (ds->digest_type) {
229         case DNS_DSDIGEST_SHA1:
230                 REQUIRE(ds->length == ISC_SHA1_DIGESTLENGTH);
231                 break;
232         case DNS_DSDIGEST_SHA256:
233                 REQUIRE(ds->length == ISC_SHA256_DIGESTLENGTH);
234                 break;
235         case DNS_DSDIGEST_GOST:
236                 REQUIRE(ds->length == ISC_GOST_DIGESTLENGTH);
237                 break;
238         case DNS_DSDIGEST_SHA384:
239                 REQUIRE(ds->length == ISC_SHA384_DIGESTLENGTH);
240                 break;
241         }
242
243         UNUSED(type);
244         UNUSED(rdclass);
245
246         RETERR(uint16_tobuffer(ds->key_tag, target));
247         RETERR(uint8_tobuffer(ds->algorithm, target));
248         RETERR(uint8_tobuffer(ds->digest_type, target));
249
250         return (mem_tobuffer(target, ds->digest, ds->length));
251 }
252
253 static inline isc_result_t
254 tostruct_ds(ARGS_TOSTRUCT) {
255         dns_rdata_ds_t *ds = target;
256         isc_region_t region;
257
258         REQUIRE(rdata->type == 43);
259         REQUIRE(target != NULL);
260         REQUIRE(rdata->length != 0);
261
262         ds->common.rdclass = rdata->rdclass;
263         ds->common.rdtype = rdata->type;
264         ISC_LINK_INIT(&ds->common, link);
265
266         dns_rdata_toregion(rdata, &region);
267
268         ds->key_tag = uint16_fromregion(&region);
269         isc_region_consume(&region, 2);
270         ds->algorithm = uint8_fromregion(&region);
271         isc_region_consume(&region, 1);
272         ds->digest_type = uint8_fromregion(&region);
273         isc_region_consume(&region, 1);
274         ds->length = region.length;
275
276         ds->digest = mem_maybedup(mctx, region.base, region.length);
277         if (ds->digest == NULL)
278                 return (ISC_R_NOMEMORY);
279
280         ds->mctx = mctx;
281         return (ISC_R_SUCCESS);
282 }
283
284 static inline void
285 freestruct_ds(ARGS_FREESTRUCT) {
286         dns_rdata_ds_t *ds = source;
287
288         REQUIRE(ds != NULL);
289         REQUIRE(ds->common.rdtype == 43);
290
291         if (ds->mctx == NULL)
292                 return;
293
294         if (ds->digest != NULL)
295                 isc_mem_free(ds->mctx, ds->digest);
296         ds->mctx = NULL;
297 }
298
299 static inline isc_result_t
300 additionaldata_ds(ARGS_ADDLDATA) {
301         REQUIRE(rdata->type == 43);
302
303         UNUSED(rdata);
304         UNUSED(add);
305         UNUSED(arg);
306
307         return (ISC_R_SUCCESS);
308 }
309
310 static inline isc_result_t
311 digest_ds(ARGS_DIGEST) {
312         isc_region_t r;
313
314         REQUIRE(rdata->type == 43);
315
316         dns_rdata_toregion(rdata, &r);
317
318         return ((digest)(arg, &r));
319 }
320
321 static inline isc_boolean_t
322 checkowner_ds(ARGS_CHECKOWNER) {
323
324         REQUIRE(type == 43);
325
326         UNUSED(name);
327         UNUSED(type);
328         UNUSED(rdclass);
329         UNUSED(wildcard);
330
331         return (ISC_TRUE);
332 }
333
334 static inline isc_boolean_t
335 checknames_ds(ARGS_CHECKNAMES) {
336
337         REQUIRE(rdata->type == 43);
338
339         UNUSED(rdata);
340         UNUSED(owner);
341         UNUSED(bad);
342
343         return (ISC_TRUE);
344 }
345
346 static inline int
347 casecompare_ds(ARGS_COMPARE) {
348         return (compare_ds(rdata1, rdata2));
349 }
350
351 #endif  /* RDATA_GENERIC_DS_43_C */