]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/bind9/lib/dns/rdata/generic/dlv_32769.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 / dlv_32769.c
1 /*
2  * Copyright (C) 2004, 2006, 2007, 2009, 2010, 2012  Internet Systems Consortium, Inc. ("ISC")
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14  * PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 /* $Id$ */
18
19 /* draft-ietf-dnsext-delegation-signer-05.txt */
20
21 #ifndef RDATA_GENERIC_DLV_32769_C
22 #define RDATA_GENERIC_DLV_32769_C
23
24 #define RRTYPE_DLV_ATTRIBUTES 0
25
26 #include <isc/sha1.h>
27 #include <isc/sha2.h>
28
29 #include <dns/ds.h>
30
31
32 static inline isc_result_t
33 fromtext_dlv(ARGS_FROMTEXT) {
34         isc_token_t token;
35         unsigned char c;
36         int length;
37
38         REQUIRE(type == 32769);
39
40         UNUSED(type);
41         UNUSED(rdclass);
42         UNUSED(origin);
43         UNUSED(options);
44         UNUSED(callbacks);
45
46         /*
47          * Key tag.
48          */
49         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
50                                       ISC_FALSE));
51         if (token.value.as_ulong > 0xffffU)
52                 RETTOK(ISC_R_RANGE);
53         RETERR(uint16_tobuffer(token.value.as_ulong, target));
54
55         /*
56          * Algorithm.
57          */
58         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
59                                       ISC_FALSE));
60         if (token.value.as_ulong > 0xffU)
61                 RETTOK(ISC_R_RANGE);
62         RETERR(uint8_tobuffer(token.value.as_ulong, target));
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, -1));
95 }
96
97 static inline isc_result_t
98 totext_dlv(ARGS_TOTEXT) {
99         isc_region_t sr;
100         char buf[sizeof("64000 ")];
101         unsigned int n;
102
103         REQUIRE(rdata->type == 32769);
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_dlv(ARGS_FROMWIRE) {
148         isc_region_t sr;
149
150         REQUIRE(type == 32769);
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_dlv(ARGS_TOWIRE) {
193         isc_region_t sr;
194
195         REQUIRE(rdata->type == 32769);
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_dlv(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 == 32769);
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_dlv(ARGS_FROMSTRUCT) {
222         dns_rdata_dlv_t *dlv = source;
223
224         REQUIRE(type == 32769);
225         REQUIRE(source != NULL);
226         REQUIRE(dlv->common.rdtype == type);
227         REQUIRE(dlv->common.rdclass == rdclass);
228         switch (dlv->digest_type) {
229         case DNS_DSDIGEST_SHA1:
230                 REQUIRE(dlv->length == ISC_SHA1_DIGESTLENGTH);
231                 break;
232         case DNS_DSDIGEST_SHA256:
233                 REQUIRE(dlv->length == ISC_SHA256_DIGESTLENGTH);
234                 break;
235         case DNS_DSDIGEST_GOST:
236                 REQUIRE(dlv->length == ISC_GOST_DIGESTLENGTH);
237                 break;
238         case DNS_DSDIGEST_SHA384:
239                 REQUIRE(dlv->length == ISC_SHA384_DIGESTLENGTH);
240                 break;
241         }
242
243         UNUSED(type);
244         UNUSED(rdclass);
245
246         RETERR(uint16_tobuffer(dlv->key_tag, target));
247         RETERR(uint8_tobuffer(dlv->algorithm, target));
248         RETERR(uint8_tobuffer(dlv->digest_type, target));
249
250         return (mem_tobuffer(target, dlv->digest, dlv->length));
251 }
252
253 static inline isc_result_t
254 tostruct_dlv(ARGS_TOSTRUCT) {
255         dns_rdata_dlv_t *dlv = target;
256         isc_region_t region;
257
258         REQUIRE(rdata->type == 32769);
259         REQUIRE(target != NULL);
260         REQUIRE(rdata->length != 0);
261
262         dlv->common.rdclass = rdata->rdclass;
263         dlv->common.rdtype = rdata->type;
264         ISC_LINK_INIT(&dlv->common, link);
265
266         dns_rdata_toregion(rdata, &region);
267
268         dlv->key_tag = uint16_fromregion(&region);
269         isc_region_consume(&region, 2);
270         dlv->algorithm = uint8_fromregion(&region);
271         isc_region_consume(&region, 1);
272         dlv->digest_type = uint8_fromregion(&region);
273         isc_region_consume(&region, 1);
274         dlv->length = region.length;
275
276         dlv->digest = mem_maybedup(mctx, region.base, region.length);
277         if (dlv->digest == NULL)
278                 return (ISC_R_NOMEMORY);
279
280         dlv->mctx = mctx;
281         return (ISC_R_SUCCESS);
282 }
283
284 static inline void
285 freestruct_dlv(ARGS_FREESTRUCT) {
286         dns_rdata_dlv_t *dlv = source;
287
288         REQUIRE(dlv != NULL);
289         REQUIRE(dlv->common.rdtype == 32769);
290
291         if (dlv->mctx == NULL)
292                 return;
293
294         if (dlv->digest != NULL)
295                 isc_mem_free(dlv->mctx, dlv->digest);
296         dlv->mctx = NULL;
297 }
298
299 static inline isc_result_t
300 additionaldata_dlv(ARGS_ADDLDATA) {
301         REQUIRE(rdata->type == 32769);
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_dlv(ARGS_DIGEST) {
312         isc_region_t r;
313
314         REQUIRE(rdata->type == 32769);
315
316         dns_rdata_toregion(rdata, &r);
317
318         return ((digest)(arg, &r));
319 }
320
321 static inline isc_boolean_t
322 checkowner_dlv(ARGS_CHECKOWNER) {
323
324         REQUIRE(type == 32769);
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_dlv(ARGS_CHECKNAMES) {
336
337         REQUIRE(rdata->type == 32769);
338
339         UNUSED(rdata);
340         UNUSED(owner);
341         UNUSED(bad);
342
343         return (ISC_TRUE);
344 }
345
346 static inline int
347 casecompare_dlv(ARGS_COMPARE) {
348         return (compare_dlv(rdata1, rdata2));
349 }
350
351 #endif  /* RDATA_GENERIC_DLV_32769_C */