bind - Removed version tag from contrib directory and updated README.DRAGONFLY.
[dragonfly.git] / contrib / bind / lib / dns / rdata / generic / dnskey_48.c
1 /*
2  * Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 2003  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: dnskey_48.c,v 1.8 2007/06/19 23:47:17 tbox Exp $ */
19
20 /*
21  * Reviewed: Wed Mar 15 16:47:10 PST 2000 by halley.
22  */
23
24 /* RFC2535 */
25
26 #ifndef RDATA_GENERIC_DNSKEY_48_C
27 #define RDATA_GENERIC_DNSKEY_48_C
28
29 #include <dst/dst.h>
30
31 #define RRTYPE_DNSKEY_ATTRIBUTES (DNS_RDATATYPEATTR_DNSSEC)
32
33 static inline isc_result_t
34 fromtext_dnskey(ARGS_FROMTEXT) {
35         isc_token_t token;
36         dns_secalg_t alg;
37         dns_secproto_t proto;
38         dns_keyflags_t flags;
39
40         REQUIRE(type == 48);
41
42         UNUSED(type);
43         UNUSED(rdclass);
44         UNUSED(origin);
45         UNUSED(options);
46         UNUSED(callbacks);
47
48         /* flags */
49         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
50                                       ISC_FALSE));
51         RETTOK(dns_keyflags_fromtext(&flags, &token.value.as_textregion));
52         RETERR(uint16_tobuffer(flags, target));
53
54         /* protocol */
55         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
56                                       ISC_FALSE));
57         RETTOK(dns_secproto_fromtext(&proto, &token.value.as_textregion));
58         RETERR(mem_tobuffer(target, &proto, 1));
59
60         /* algorithm */
61         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
62                                       ISC_FALSE));
63         RETTOK(dns_secalg_fromtext(&alg, &token.value.as_textregion));
64         RETERR(mem_tobuffer(target, &alg, 1));
65
66         /* No Key? */
67         if ((flags & 0xc000) == 0xc000)
68                 return (ISC_R_SUCCESS);
69
70         return (isc_base64_tobuffer(lexer, target, -1));
71 }
72
73 static inline isc_result_t
74 totext_dnskey(ARGS_TOTEXT) {
75         isc_region_t sr;
76         char buf[sizeof("64000")];
77         unsigned int flags;
78         unsigned char algorithm;
79
80         REQUIRE(rdata->type == 48);
81         REQUIRE(rdata->length != 0);
82
83         dns_rdata_toregion(rdata, &sr);
84
85         /* flags */
86         flags = uint16_fromregion(&sr);
87         isc_region_consume(&sr, 2);
88         sprintf(buf, "%u", flags);
89         RETERR(str_totext(buf, target));
90         RETERR(str_totext(" ", target));
91
92         /* protocol */
93         sprintf(buf, "%u", sr.base[0]);
94         isc_region_consume(&sr, 1);
95         RETERR(str_totext(buf, target));
96         RETERR(str_totext(" ", target));
97
98         /* algorithm */
99         algorithm = sr.base[0];
100         sprintf(buf, "%u", algorithm);
101         isc_region_consume(&sr, 1);
102         RETERR(str_totext(buf, target));
103
104         /* No Key? */
105         if ((flags & 0xc000) == 0xc000)
106                 return (ISC_R_SUCCESS);
107
108         /* key */
109         if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
110                 RETERR(str_totext(" (", target));
111         RETERR(str_totext(tctx->linebreak, target));
112         RETERR(isc_base64_totext(&sr, tctx->width - 2,
113                                  tctx->linebreak, target));
114
115         if ((tctx->flags & DNS_STYLEFLAG_COMMENT) != 0)
116                 RETERR(str_totext(tctx->linebreak, target));
117         else if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
118                 RETERR(str_totext(" ", target));
119
120         if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
121                 RETERR(str_totext(")", target));
122
123         if ((tctx->flags & DNS_STYLEFLAG_COMMENT) != 0) {
124                 isc_region_t tmpr;
125
126                 RETERR(str_totext(" ; key id = ", target));
127                 dns_rdata_toregion(rdata, &tmpr);
128                 sprintf(buf, "%u", dst_region_computeid(&tmpr, algorithm));
129                 RETERR(str_totext(buf, target));
130         }
131         return (ISC_R_SUCCESS);
132 }
133
134 static inline isc_result_t
135 fromwire_dnskey(ARGS_FROMWIRE) {
136         isc_region_t sr;
137
138         REQUIRE(type == 48);
139
140         UNUSED(type);
141         UNUSED(rdclass);
142         UNUSED(dctx);
143         UNUSED(options);
144
145         isc_buffer_activeregion(source, &sr);
146         if (sr.length < 4)
147                 return (ISC_R_UNEXPECTEDEND);
148
149         isc_buffer_forward(source, sr.length);
150         return (mem_tobuffer(target, sr.base, sr.length));
151 }
152
153 static inline isc_result_t
154 towire_dnskey(ARGS_TOWIRE) {
155         isc_region_t sr;
156
157         REQUIRE(rdata->type == 48);
158         REQUIRE(rdata->length != 0);
159
160         UNUSED(cctx);
161
162         dns_rdata_toregion(rdata, &sr);
163         return (mem_tobuffer(target, sr.base, sr.length));
164 }
165
166 static inline int
167 compare_dnskey(ARGS_COMPARE) {
168         isc_region_t r1;
169         isc_region_t r2;
170
171         REQUIRE(rdata1->type == rdata2->type);
172         REQUIRE(rdata1->rdclass == rdata2->rdclass);
173         REQUIRE(rdata1->type == 48);
174         REQUIRE(rdata1->length != 0);
175         REQUIRE(rdata2->length != 0);
176
177         dns_rdata_toregion(rdata1, &r1);
178         dns_rdata_toregion(rdata2, &r2);
179         return (isc_region_compare(&r1, &r2));
180 }
181
182 static inline isc_result_t
183 fromstruct_dnskey(ARGS_FROMSTRUCT) {
184         dns_rdata_dnskey_t *dnskey = source;
185
186         REQUIRE(type == 48);
187         REQUIRE(source != NULL);
188         REQUIRE(dnskey->common.rdtype == type);
189         REQUIRE(dnskey->common.rdclass == rdclass);
190
191         UNUSED(type);
192         UNUSED(rdclass);
193
194         /* Flags */
195         RETERR(uint16_tobuffer(dnskey->flags, target));
196
197         /* Protocol */
198         RETERR(uint8_tobuffer(dnskey->protocol, target));
199
200         /* Algorithm */
201         RETERR(uint8_tobuffer(dnskey->algorithm, target));
202
203         /* Data */
204         return (mem_tobuffer(target, dnskey->data, dnskey->datalen));
205 }
206
207 static inline isc_result_t
208 tostruct_dnskey(ARGS_TOSTRUCT) {
209         dns_rdata_dnskey_t *dnskey = target;
210         isc_region_t sr;
211
212         REQUIRE(rdata->type == 48);
213         REQUIRE(target != NULL);
214         REQUIRE(rdata->length != 0);
215
216         dnskey->common.rdclass = rdata->rdclass;
217         dnskey->common.rdtype = rdata->type;
218         ISC_LINK_INIT(&dnskey->common, link);
219
220         dns_rdata_toregion(rdata, &sr);
221
222         /* Flags */
223         if (sr.length < 2)
224                 return (ISC_R_UNEXPECTEDEND);
225         dnskey->flags = uint16_fromregion(&sr);
226         isc_region_consume(&sr, 2);
227
228         /* Protocol */
229         if (sr.length < 1)
230                 return (ISC_R_UNEXPECTEDEND);
231         dnskey->protocol = uint8_fromregion(&sr);
232         isc_region_consume(&sr, 1);
233
234         /* Algorithm */
235         if (sr.length < 1)
236                 return (ISC_R_UNEXPECTEDEND);
237         dnskey->algorithm = uint8_fromregion(&sr);
238         isc_region_consume(&sr, 1);
239
240         /* Data */
241         dnskey->datalen = sr.length;
242         dnskey->data = mem_maybedup(mctx, sr.base, dnskey->datalen);
243         if (dnskey->data == NULL)
244                 return (ISC_R_NOMEMORY);
245
246         dnskey->mctx = mctx;
247         return (ISC_R_SUCCESS);
248 }
249
250 static inline void
251 freestruct_dnskey(ARGS_FREESTRUCT) {
252         dns_rdata_dnskey_t *dnskey = (dns_rdata_dnskey_t *) source;
253
254         REQUIRE(source != NULL);
255         REQUIRE(dnskey->common.rdtype == 48);
256
257         if (dnskey->mctx == NULL)
258                 return;
259
260         if (dnskey->data != NULL)
261                 isc_mem_free(dnskey->mctx, dnskey->data);
262         dnskey->mctx = NULL;
263 }
264
265 static inline isc_result_t
266 additionaldata_dnskey(ARGS_ADDLDATA) {
267         REQUIRE(rdata->type == 48);
268
269         UNUSED(rdata);
270         UNUSED(add);
271         UNUSED(arg);
272
273         return (ISC_R_SUCCESS);
274 }
275
276 static inline isc_result_t
277 digest_dnskey(ARGS_DIGEST) {
278         isc_region_t r;
279
280         REQUIRE(rdata->type == 48);
281
282         dns_rdata_toregion(rdata, &r);
283
284         return ((digest)(arg, &r));
285 }
286
287 static inline isc_boolean_t
288 checkowner_dnskey(ARGS_CHECKOWNER) {
289
290         REQUIRE(type == 48);
291
292         UNUSED(name);
293         UNUSED(type);
294         UNUSED(rdclass);
295         UNUSED(wildcard);
296
297         return (ISC_TRUE);
298 }
299
300 static inline isc_boolean_t
301 checknames_dnskey(ARGS_CHECKNAMES) {
302
303         REQUIRE(rdata->type == 48);
304
305         UNUSED(rdata);
306         UNUSED(owner);
307         UNUSED(bad);
308
309         return (ISC_TRUE);
310 }
311
312 #endif  /* RDATA_GENERIC_DNSKEY_48_C */