Merge branch 'vendor/OPENSSL'
[dragonfly.git] / contrib / bind-9.3 / lib / dns / rdata / generic / key_25.c
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2003  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and 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: key_25.c,v 1.41.12.7 2004/03/08 09:04:41 marka Exp $ */
19
20 /*
21  * Reviewed: Wed Mar 15 16:47:10 PST 2000 by halley.
22  */
23
24 /* RFC 2535 */
25
26 #ifndef RDATA_GENERIC_KEY_25_C
27 #define RDATA_GENERIC_KEY_25_C
28
29 #include <dst/dst.h>
30
31 #define RRTYPE_KEY_ATTRIBUTES (0)
32
33 static inline isc_result_t
34 fromtext_key(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 == 25);
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_key(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 == 25);
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_key(ARGS_FROMWIRE) {
136         isc_region_t sr;
137
138         REQUIRE(type == 25);
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_key(ARGS_TOWIRE) {
155         isc_region_t sr;
156
157         REQUIRE(rdata->type == 25);
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_key(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 == 25);
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_key(ARGS_FROMSTRUCT) {
184         dns_rdata_key_t *key = source;
185
186         REQUIRE(type == 25);
187         REQUIRE(source != NULL);
188         REQUIRE(key->common.rdtype == type);
189         REQUIRE(key->common.rdclass == rdclass);
190
191         UNUSED(type);
192         UNUSED(rdclass);
193
194         /* Flags */
195         RETERR(uint16_tobuffer(key->flags, target));
196
197         /* Protocol */
198         RETERR(uint8_tobuffer(key->protocol, target));
199
200         /* Algorithm */
201         RETERR(uint8_tobuffer(key->algorithm, target));
202
203         /* Data */
204         return (mem_tobuffer(target, key->data, key->datalen));
205 }
206
207 static inline isc_result_t
208 tostruct_key(ARGS_TOSTRUCT) {
209         dns_rdata_key_t *key = target;
210         isc_region_t sr;
211
212         REQUIRE(rdata->type == 25);
213         REQUIRE(target != NULL);
214         REQUIRE(rdata->length != 0);
215
216         key->common.rdclass = rdata->rdclass;
217         key->common.rdtype = rdata->type;
218         ISC_LINK_INIT(&key->common, link);
219
220         dns_rdata_toregion(rdata, &sr);
221
222         /* Flags */
223         if (sr.length < 2)
224                 return (ISC_R_UNEXPECTEDEND);
225         key->flags = uint16_fromregion(&sr);
226         isc_region_consume(&sr, 2);
227
228         /* Protocol */
229         if (sr.length < 1)
230                 return (ISC_R_UNEXPECTEDEND);
231         key->protocol = uint8_fromregion(&sr);
232         isc_region_consume(&sr, 1);
233
234         /* Algorithm */
235         if (sr.length < 1)
236                 return (ISC_R_UNEXPECTEDEND);
237         key->algorithm = uint8_fromregion(&sr);
238         isc_region_consume(&sr, 1);
239
240         /* Data */
241         key->datalen = sr.length;
242         key->data = mem_maybedup(mctx, sr.base, key->datalen);
243         if (key->data == NULL)
244                 return (ISC_R_NOMEMORY);
245
246         key->mctx = mctx;
247         return (ISC_R_SUCCESS);
248 }
249
250 static inline void
251 freestruct_key(ARGS_FREESTRUCT) {
252         dns_rdata_key_t *key = (dns_rdata_key_t *) source;
253
254         REQUIRE(source != NULL);
255         REQUIRE(key->common.rdtype == 25);
256
257         if (key->mctx == NULL)
258                 return;
259
260         if (key->data != NULL)
261                 isc_mem_free(key->mctx, key->data);
262         key->mctx = NULL;
263 }
264
265 static inline isc_result_t
266 additionaldata_key(ARGS_ADDLDATA) {
267         REQUIRE(rdata->type == 25);
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_key(ARGS_DIGEST) {
278         isc_region_t r;
279
280         REQUIRE(rdata->type == 25);
281
282         dns_rdata_toregion(rdata, &r);
283
284         return ((digest)(arg, &r));
285 }
286
287 static inline isc_boolean_t
288 checkowner_key(ARGS_CHECKOWNER) {
289
290         REQUIRE(type == 25);
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_key(ARGS_CHECKNAMES) {
302
303         REQUIRE(rdata->type == 25);
304
305         UNUSED(rdata);
306         UNUSED(owner);
307         UNUSED(bad);
308
309         return (ISC_TRUE);
310 }
311
312 #endif  /* RDATA_GENERIC_KEY_25_C */