bind - Upgraded vendor branch to 9.5.2-P1
[dragonfly.git] / contrib / bind-9.5.2 / lib / dns / rdata / generic / dlv_32769.c
1 /*
2  * Copyright (C) 2004, 2006, 2007  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: dlv_32769.c,v 1.6 2007/06/18 23:47:43 tbox Exp $ */
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         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, -1));
84 }
85
86 static inline isc_result_t
87 totext_dlv(ARGS_TOTEXT) {
88         isc_region_t sr;
89         char buf[sizeof("64000 ")];
90         unsigned int n;
91
92         REQUIRE(rdata->type == 32769);
93         REQUIRE(rdata->length != 0);
94
95         UNUSED(tctx);
96
97         dns_rdata_toregion(rdata, &sr);
98
99         /*
100          * Key tag.
101          */
102         n = uint16_fromregion(&sr);
103         isc_region_consume(&sr, 2);
104         sprintf(buf, "%u ", n);
105         RETERR(str_totext(buf, target));
106
107         /*
108          * Algorithm.
109          */
110         n = uint8_fromregion(&sr);
111         isc_region_consume(&sr, 1);
112         sprintf(buf, "%u ", n);
113         RETERR(str_totext(buf, target));
114
115         /*
116          * Digest type.
117          */
118         n = uint8_fromregion(&sr);
119         isc_region_consume(&sr, 1);
120         sprintf(buf, "%u", n);
121         RETERR(str_totext(buf, target));
122
123         /*
124          * Digest.
125          */
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);
133 }
134
135 static inline isc_result_t
136 fromwire_dlv(ARGS_FROMWIRE) {
137         isc_region_t sr;
138
139         REQUIRE(type == 32769);
140
141         UNUSED(type);
142         UNUSED(rdclass);
143         UNUSED(dctx);
144         UNUSED(options);
145
146         isc_buffer_activeregion(source, &sr);
147  
148         /*
149          * Check digest lengths if we know them.
150          */
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);
157
158         /*
159          * Only copy digest lengths if we know them.
160          * If there is extra data dns_rdata_fromwire() will
161          * detect that.
162          */
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;
167  
168         isc_buffer_forward(source, sr.length);
169         return (mem_tobuffer(target, sr.base, sr.length));
170 }
171
172 static inline isc_result_t
173 towire_dlv(ARGS_TOWIRE) {
174         isc_region_t sr;
175
176         REQUIRE(rdata->type == 32769);
177         REQUIRE(rdata->length != 0);
178
179         UNUSED(cctx);
180
181         dns_rdata_toregion(rdata, &sr);
182         return (mem_tobuffer(target, sr.base, sr.length));
183 }
184
185 static inline int
186 compare_dlv(ARGS_COMPARE) {
187         isc_region_t r1;
188         isc_region_t r2;
189
190         REQUIRE(rdata1->type == rdata2->type);
191         REQUIRE(rdata1->rdclass == rdata2->rdclass);
192         REQUIRE(rdata1->type == 32769);
193         REQUIRE(rdata1->length != 0);
194         REQUIRE(rdata2->length != 0);
195
196         dns_rdata_toregion(rdata1, &r1);
197         dns_rdata_toregion(rdata2, &r2);
198         return (isc_region_compare(&r1, &r2));
199 }
200
201 static inline isc_result_t
202 fromstruct_dlv(ARGS_FROMSTRUCT) {
203         dns_rdata_dlv_t *dlv = source;
204
205         REQUIRE(type == 32769);
206         REQUIRE(source != NULL);
207         REQUIRE(dlv->common.rdtype == type);
208         REQUIRE(dlv->common.rdclass == rdclass);
209         switch (dlv->digest_type) {
210         case DNS_DSDIGEST_SHA1:
211                 REQUIRE(dlv->length == ISC_SHA1_DIGESTLENGTH);
212                 break;
213         case DNS_DSDIGEST_SHA256:
214                 REQUIRE(dlv->length == ISC_SHA256_DIGESTLENGTH);
215                 break;
216         }
217
218         UNUSED(type);
219         UNUSED(rdclass);
220
221         RETERR(uint16_tobuffer(dlv->key_tag, target));
222         RETERR(uint8_tobuffer(dlv->algorithm, target));
223         RETERR(uint8_tobuffer(dlv->digest_type, target));
224
225         return (mem_tobuffer(target, dlv->digest, dlv->length));
226 }
227
228 static inline isc_result_t
229 tostruct_dlv(ARGS_TOSTRUCT) {
230         dns_rdata_dlv_t *dlv = target;
231         isc_region_t region;
232
233         REQUIRE(rdata->type == 32769);
234         REQUIRE(target != NULL);
235         REQUIRE(rdata->length != 0);
236
237         dlv->common.rdclass = rdata->rdclass;
238         dlv->common.rdtype = rdata->type;
239         ISC_LINK_INIT(&dlv->common, link);
240
241         dns_rdata_toregion(rdata, &region);
242
243         dlv->key_tag = uint16_fromregion(&region);
244         isc_region_consume(&region, 2);
245         dlv->algorithm = uint8_fromregion(&region);
246         isc_region_consume(&region, 1);
247         dlv->digest_type = uint8_fromregion(&region);
248         isc_region_consume(&region, 1);
249         dlv->length = region.length;
250
251         dlv->digest = mem_maybedup(mctx, region.base, region.length);
252         if (dlv->digest == NULL)
253                 return (ISC_R_NOMEMORY);
254
255         dlv->mctx = mctx;
256         return (ISC_R_SUCCESS);
257 }
258
259 static inline void
260 freestruct_dlv(ARGS_FREESTRUCT) {
261         dns_rdata_dlv_t *dlv = source;
262
263         REQUIRE(dlv != NULL);
264         REQUIRE(dlv->common.rdtype == 32769);
265
266         if (dlv->mctx == NULL)
267                 return;
268
269         if (dlv->digest != NULL)
270                 isc_mem_free(dlv->mctx, dlv->digest);
271         dlv->mctx = NULL;
272 }
273
274 static inline isc_result_t
275 additionaldata_dlv(ARGS_ADDLDATA) {
276         REQUIRE(rdata->type == 32769);
277
278         UNUSED(rdata);
279         UNUSED(add);
280         UNUSED(arg);
281
282         return (ISC_R_SUCCESS);
283 }
284
285 static inline isc_result_t
286 digest_dlv(ARGS_DIGEST) {
287         isc_region_t r;
288
289         REQUIRE(rdata->type == 32769);
290
291         dns_rdata_toregion(rdata, &r);
292
293         return ((digest)(arg, &r));
294 }
295
296 static inline isc_boolean_t
297 checkowner_dlv(ARGS_CHECKOWNER) {
298
299         REQUIRE(type == 32769);
300
301         UNUSED(name);
302         UNUSED(type);
303         UNUSED(rdclass);
304         UNUSED(wildcard);
305
306         return (ISC_TRUE);
307 }
308
309 static inline isc_boolean_t
310 checknames_dlv(ARGS_CHECKNAMES) {
311
312         REQUIRE(rdata->type == 32769);
313
314         UNUSED(rdata);
315         UNUSED(owner);
316         UNUSED(bad);
317
318         return (ISC_TRUE);
319 }
320
321 #endif  /* RDATA_GENERIC_DLV_32769_C */