Merge branch 'vendor/SENDMAIL'
[dragonfly.git] / contrib / bind / lib / dns / rdata / generic / ds_43.c
1 /*
2  * Copyright (C) 2004, 2005, 2007  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: ds_43.c,v 1.12 2007/06/18 23:47:43 tbox Exp $ */
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         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, length));
84 }
85
86 static inline isc_result_t
87 totext_ds(ARGS_TOTEXT) {
88         isc_region_t sr;
89         char buf[sizeof("64000 ")];
90         unsigned int n;
91
92         REQUIRE(rdata->type == 43);
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_ds(ARGS_FROMWIRE) {
137         isc_region_t sr;
138
139         REQUIRE(type == 43);
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_ds(ARGS_TOWIRE) {
174         isc_region_t sr;
175
176         REQUIRE(rdata->type == 43);
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_ds(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 == 43);
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_ds(ARGS_FROMSTRUCT) {
203         dns_rdata_ds_t *ds = source;
204
205         REQUIRE(type == 43);
206         REQUIRE(source != NULL);
207         REQUIRE(ds->common.rdtype == type);
208         REQUIRE(ds->common.rdclass == rdclass);
209         switch (ds->digest_type) {
210         case DNS_DSDIGEST_SHA1:
211                 REQUIRE(ds->length == ISC_SHA1_DIGESTLENGTH);
212                 break;
213         case DNS_DSDIGEST_SHA256:
214                 REQUIRE(ds->length == ISC_SHA256_DIGESTLENGTH);
215                 break;
216         }
217
218         UNUSED(type);
219         UNUSED(rdclass);
220
221         RETERR(uint16_tobuffer(ds->key_tag, target));
222         RETERR(uint8_tobuffer(ds->algorithm, target));
223         RETERR(uint8_tobuffer(ds->digest_type, target));
224
225         return (mem_tobuffer(target, ds->digest, ds->length));
226 }
227
228 static inline isc_result_t
229 tostruct_ds(ARGS_TOSTRUCT) {
230         dns_rdata_ds_t *ds = target;
231         isc_region_t region;
232
233         REQUIRE(rdata->type == 43);
234         REQUIRE(target != NULL);
235         REQUIRE(rdata->length != 0);
236
237         ds->common.rdclass = rdata->rdclass;
238         ds->common.rdtype = rdata->type;
239         ISC_LINK_INIT(&ds->common, link);
240
241         dns_rdata_toregion(rdata, &region);
242
243         ds->key_tag = uint16_fromregion(&region);
244         isc_region_consume(&region, 2);
245         ds->algorithm = uint8_fromregion(&region);
246         isc_region_consume(&region, 1);
247         ds->digest_type = uint8_fromregion(&region);
248         isc_region_consume(&region, 1);
249         ds->length = region.length;
250
251         ds->digest = mem_maybedup(mctx, region.base, region.length);
252         if (ds->digest == NULL)
253                 return (ISC_R_NOMEMORY);
254
255         ds->mctx = mctx;
256         return (ISC_R_SUCCESS);
257 }
258
259 static inline void
260 freestruct_ds(ARGS_FREESTRUCT) {
261         dns_rdata_ds_t *ds = source;
262
263         REQUIRE(ds != NULL);
264         REQUIRE(ds->common.rdtype == 43);
265
266         if (ds->mctx == NULL)
267                 return;
268
269         if (ds->digest != NULL)
270                 isc_mem_free(ds->mctx, ds->digest);
271         ds->mctx = NULL;
272 }
273
274 static inline isc_result_t
275 additionaldata_ds(ARGS_ADDLDATA) {
276         REQUIRE(rdata->type == 43);
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_ds(ARGS_DIGEST) {
287         isc_region_t r;
288
289         REQUIRE(rdata->type == 43);
290
291         dns_rdata_toregion(rdata, &r);
292
293         return ((digest)(arg, &r));
294 }
295
296 static inline isc_boolean_t
297 checkowner_ds(ARGS_CHECKOWNER) {
298
299         REQUIRE(type == 43);
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_ds(ARGS_CHECKNAMES) {
311
312         REQUIRE(rdata->type == 43);
313
314         UNUSED(rdata);
315         UNUSED(owner);
316         UNUSED(bad);
317
318         return (ISC_TRUE);
319 }
320
321 #endif  /* RDATA_GENERIC_DS_43_C */