]> CyberLeo.Net >> Repos - FreeBSD/releng/9.3.git/blob - contrib/bind9/lib/dns/rdata/generic/dlv_32769.c
Copy stable/9 to releng/9.3 as part of the 9.3-RELEASE cycle.
[FreeBSD/releng/9.3.git] / contrib / bind9 / lib / dns / rdata / generic / dlv_32769.c
1 /*
2  * Copyright (C) 2004, 2006, 2007, 2009-2013  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, length));
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         if (tctx->width == 0) /* No splitting */
141                 RETERR(isc_hex_totext(&sr, 0, "", target));
142         else
143                 RETERR(isc_hex_totext(&sr, tctx->width - 2,
144                                       tctx->linebreak, target));
145         if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
146                 RETERR(str_totext(" )", target));
147         return (ISC_R_SUCCESS);
148 }
149
150 static inline isc_result_t
151 fromwire_dlv(ARGS_FROMWIRE) {
152         isc_region_t sr;
153
154         REQUIRE(type == 32769);
155
156         UNUSED(type);
157         UNUSED(rdclass);
158         UNUSED(dctx);
159         UNUSED(options);
160
161         isc_buffer_activeregion(source, &sr);
162
163         /*
164          * Check digest lengths if we know them.
165          */
166         if (sr.length < 4 ||
167             (sr.base[3] == DNS_DSDIGEST_SHA1 &&
168              sr.length < 4 + ISC_SHA1_DIGESTLENGTH) ||
169             (sr.base[3] == DNS_DSDIGEST_SHA256 &&
170              sr.length < 4 + ISC_SHA256_DIGESTLENGTH) ||
171             (sr.base[3] == DNS_DSDIGEST_GOST &&
172              sr.length < 4 + ISC_GOST_DIGESTLENGTH) ||
173             (sr.base[3] == DNS_DSDIGEST_SHA384 &&
174              sr.length < 4 + ISC_SHA384_DIGESTLENGTH))
175                 return (ISC_R_UNEXPECTEDEND);
176
177         /*
178          * Only copy digest lengths if we know them.
179          * If there is extra data dns_rdata_fromwire() will
180          * detect that.
181          */
182         if (sr.base[3] == DNS_DSDIGEST_SHA1)
183                 sr.length = 4 + ISC_SHA1_DIGESTLENGTH;
184         else if (sr.base[3] == DNS_DSDIGEST_SHA256)
185                 sr.length = 4 + ISC_SHA256_DIGESTLENGTH;
186         else if (sr.base[3] == DNS_DSDIGEST_GOST)
187                 sr.length = 4 + ISC_GOST_DIGESTLENGTH;
188         else if (sr.base[3] == DNS_DSDIGEST_SHA384)
189                 sr.length = 4 + ISC_SHA384_DIGESTLENGTH;
190
191         isc_buffer_forward(source, sr.length);
192         return (mem_tobuffer(target, sr.base, sr.length));
193 }
194
195 static inline isc_result_t
196 towire_dlv(ARGS_TOWIRE) {
197         isc_region_t sr;
198
199         REQUIRE(rdata->type == 32769);
200         REQUIRE(rdata->length != 0);
201
202         UNUSED(cctx);
203
204         dns_rdata_toregion(rdata, &sr);
205         return (mem_tobuffer(target, sr.base, sr.length));
206 }
207
208 static inline int
209 compare_dlv(ARGS_COMPARE) {
210         isc_region_t r1;
211         isc_region_t r2;
212
213         REQUIRE(rdata1->type == rdata2->type);
214         REQUIRE(rdata1->rdclass == rdata2->rdclass);
215         REQUIRE(rdata1->type == 32769);
216         REQUIRE(rdata1->length != 0);
217         REQUIRE(rdata2->length != 0);
218
219         dns_rdata_toregion(rdata1, &r1);
220         dns_rdata_toregion(rdata2, &r2);
221         return (isc_region_compare(&r1, &r2));
222 }
223
224 static inline isc_result_t
225 fromstruct_dlv(ARGS_FROMSTRUCT) {
226         dns_rdata_dlv_t *dlv = source;
227
228         REQUIRE(type == 32769);
229         REQUIRE(source != NULL);
230         REQUIRE(dlv->common.rdtype == type);
231         REQUIRE(dlv->common.rdclass == rdclass);
232         switch (dlv->digest_type) {
233         case DNS_DSDIGEST_SHA1:
234                 REQUIRE(dlv->length == ISC_SHA1_DIGESTLENGTH);
235                 break;
236         case DNS_DSDIGEST_SHA256:
237                 REQUIRE(dlv->length == ISC_SHA256_DIGESTLENGTH);
238                 break;
239         case DNS_DSDIGEST_GOST:
240                 REQUIRE(dlv->length == ISC_GOST_DIGESTLENGTH);
241                 break;
242         case DNS_DSDIGEST_SHA384:
243                 REQUIRE(dlv->length == ISC_SHA384_DIGESTLENGTH);
244                 break;
245         }
246
247         UNUSED(type);
248         UNUSED(rdclass);
249
250         RETERR(uint16_tobuffer(dlv->key_tag, target));
251         RETERR(uint8_tobuffer(dlv->algorithm, target));
252         RETERR(uint8_tobuffer(dlv->digest_type, target));
253
254         return (mem_tobuffer(target, dlv->digest, dlv->length));
255 }
256
257 static inline isc_result_t
258 tostruct_dlv(ARGS_TOSTRUCT) {
259         dns_rdata_dlv_t *dlv = target;
260         isc_region_t region;
261
262         REQUIRE(rdata->type == 32769);
263         REQUIRE(target != NULL);
264         REQUIRE(rdata->length != 0);
265
266         dlv->common.rdclass = rdata->rdclass;
267         dlv->common.rdtype = rdata->type;
268         ISC_LINK_INIT(&dlv->common, link);
269
270         dns_rdata_toregion(rdata, &region);
271
272         dlv->key_tag = uint16_fromregion(&region);
273         isc_region_consume(&region, 2);
274         dlv->algorithm = uint8_fromregion(&region);
275         isc_region_consume(&region, 1);
276         dlv->digest_type = uint8_fromregion(&region);
277         isc_region_consume(&region, 1);
278         dlv->length = region.length;
279
280         dlv->digest = mem_maybedup(mctx, region.base, region.length);
281         if (dlv->digest == NULL)
282                 return (ISC_R_NOMEMORY);
283
284         dlv->mctx = mctx;
285         return (ISC_R_SUCCESS);
286 }
287
288 static inline void
289 freestruct_dlv(ARGS_FREESTRUCT) {
290         dns_rdata_dlv_t *dlv = source;
291
292         REQUIRE(dlv != NULL);
293         REQUIRE(dlv->common.rdtype == 32769);
294
295         if (dlv->mctx == NULL)
296                 return;
297
298         if (dlv->digest != NULL)
299                 isc_mem_free(dlv->mctx, dlv->digest);
300         dlv->mctx = NULL;
301 }
302
303 static inline isc_result_t
304 additionaldata_dlv(ARGS_ADDLDATA) {
305         REQUIRE(rdata->type == 32769);
306
307         UNUSED(rdata);
308         UNUSED(add);
309         UNUSED(arg);
310
311         return (ISC_R_SUCCESS);
312 }
313
314 static inline isc_result_t
315 digest_dlv(ARGS_DIGEST) {
316         isc_region_t r;
317
318         REQUIRE(rdata->type == 32769);
319
320         dns_rdata_toregion(rdata, &r);
321
322         return ((digest)(arg, &r));
323 }
324
325 static inline isc_boolean_t
326 checkowner_dlv(ARGS_CHECKOWNER) {
327
328         REQUIRE(type == 32769);
329
330         UNUSED(name);
331         UNUSED(type);
332         UNUSED(rdclass);
333         UNUSED(wildcard);
334
335         return (ISC_TRUE);
336 }
337
338 static inline isc_boolean_t
339 checknames_dlv(ARGS_CHECKNAMES) {
340
341         REQUIRE(rdata->type == 32769);
342
343         UNUSED(rdata);
344         UNUSED(owner);
345         UNUSED(bad);
346
347         return (ISC_TRUE);
348 }
349
350 static inline int
351 casecompare_dlv(ARGS_COMPARE) {
352         return (compare_dlv(rdata1, rdata2));
353 }
354
355 #endif  /* RDATA_GENERIC_DLV_32769_C */