Import bind 9.5.2 vendor sources.
[dragonfly.git] / contrib / bind-9.5.2 / lib / dns / rdata / generic / key_25.c
blob9acfe95f6c6b62856bbd927df8f8b6910486bb85
1 /*
2 * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003 Internet Software Consortium.
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.
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.
18 /* $Id: key_25.c,v 1.51 2007/06/19 23:47:17 tbox Exp $ */
21 * Reviewed: Wed Mar 15 16:47:10 PST 2000 by halley.
24 /* RFC2535 */
26 #ifndef RDATA_GENERIC_KEY_25_C
27 #define RDATA_GENERIC_KEY_25_C
29 #include <dst/dst.h>
31 #define RRTYPE_KEY_ATTRIBUTES (0)
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;
40 REQUIRE(type == 25);
42 UNUSED(type);
43 UNUSED(rdclass);
44 UNUSED(origin);
45 UNUSED(options);
46 UNUSED(callbacks);
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));
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));
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));
66 /* No Key? */
67 if ((flags & 0xc000) == 0xc000)
68 return (ISC_R_SUCCESS);
70 return (isc_base64_tobuffer(lexer, target, -1));
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;
80 REQUIRE(rdata->type == 25);
81 REQUIRE(rdata->length != 0);
83 dns_rdata_toregion(rdata, &sr);
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));
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));
98 /* algorithm */
99 algorithm = sr.base[0];
100 sprintf(buf, "%u", algorithm);
101 isc_region_consume(&sr, 1);
102 RETERR(str_totext(buf, target));
104 /* No Key? */
105 if ((flags & 0xc000) == 0xc000)
106 return (ISC_R_SUCCESS);
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));
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));
120 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
121 RETERR(str_totext(")", target));
123 if ((tctx->flags & DNS_STYLEFLAG_COMMENT) != 0) {
124 isc_region_t tmpr;
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));
131 return (ISC_R_SUCCESS);
134 static inline isc_result_t
135 fromwire_key(ARGS_FROMWIRE) {
136 isc_region_t sr;
138 REQUIRE(type == 25);
140 UNUSED(type);
141 UNUSED(rdclass);
142 UNUSED(dctx);
143 UNUSED(options);
145 isc_buffer_activeregion(source, &sr);
146 if (sr.length < 4)
147 return (ISC_R_UNEXPECTEDEND);
149 isc_buffer_forward(source, sr.length);
150 return (mem_tobuffer(target, sr.base, sr.length));
153 static inline isc_result_t
154 towire_key(ARGS_TOWIRE) {
155 isc_region_t sr;
157 REQUIRE(rdata->type == 25);
158 REQUIRE(rdata->length != 0);
160 UNUSED(cctx);
162 dns_rdata_toregion(rdata, &sr);
163 return (mem_tobuffer(target, sr.base, sr.length));
166 static inline int
167 compare_key(ARGS_COMPARE) {
168 isc_region_t r1;
169 isc_region_t r2;
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);
177 dns_rdata_toregion(rdata1, &r1);
178 dns_rdata_toregion(rdata2, &r2);
179 return (isc_region_compare(&r1, &r2));
182 static inline isc_result_t
183 fromstruct_key(ARGS_FROMSTRUCT) {
184 dns_rdata_key_t *key = source;
186 REQUIRE(type == 25);
187 REQUIRE(source != NULL);
188 REQUIRE(key->common.rdtype == type);
189 REQUIRE(key->common.rdclass == rdclass);
191 UNUSED(type);
192 UNUSED(rdclass);
194 /* Flags */
195 RETERR(uint16_tobuffer(key->flags, target));
197 /* Protocol */
198 RETERR(uint8_tobuffer(key->protocol, target));
200 /* Algorithm */
201 RETERR(uint8_tobuffer(key->algorithm, target));
203 /* Data */
204 return (mem_tobuffer(target, key->data, key->datalen));
207 static inline isc_result_t
208 tostruct_key(ARGS_TOSTRUCT) {
209 dns_rdata_key_t *key = target;
210 isc_region_t sr;
212 REQUIRE(rdata->type == 25);
213 REQUIRE(target != NULL);
214 REQUIRE(rdata->length != 0);
216 key->common.rdclass = rdata->rdclass;
217 key->common.rdtype = rdata->type;
218 ISC_LINK_INIT(&key->common, link);
220 dns_rdata_toregion(rdata, &sr);
222 /* Flags */
223 if (sr.length < 2)
224 return (ISC_R_UNEXPECTEDEND);
225 key->flags = uint16_fromregion(&sr);
226 isc_region_consume(&sr, 2);
228 /* Protocol */
229 if (sr.length < 1)
230 return (ISC_R_UNEXPECTEDEND);
231 key->protocol = uint8_fromregion(&sr);
232 isc_region_consume(&sr, 1);
234 /* Algorithm */
235 if (sr.length < 1)
236 return (ISC_R_UNEXPECTEDEND);
237 key->algorithm = uint8_fromregion(&sr);
238 isc_region_consume(&sr, 1);
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);
246 key->mctx = mctx;
247 return (ISC_R_SUCCESS);
250 static inline void
251 freestruct_key(ARGS_FREESTRUCT) {
252 dns_rdata_key_t *key = (dns_rdata_key_t *) source;
254 REQUIRE(source != NULL);
255 REQUIRE(key->common.rdtype == 25);
257 if (key->mctx == NULL)
258 return;
260 if (key->data != NULL)
261 isc_mem_free(key->mctx, key->data);
262 key->mctx = NULL;
265 static inline isc_result_t
266 additionaldata_key(ARGS_ADDLDATA) {
267 REQUIRE(rdata->type == 25);
269 UNUSED(rdata);
270 UNUSED(add);
271 UNUSED(arg);
273 return (ISC_R_SUCCESS);
276 static inline isc_result_t
277 digest_key(ARGS_DIGEST) {
278 isc_region_t r;
280 REQUIRE(rdata->type == 25);
282 dns_rdata_toregion(rdata, &r);
284 return ((digest)(arg, &r));
287 static inline isc_boolean_t
288 checkowner_key(ARGS_CHECKOWNER) {
290 REQUIRE(type == 25);
292 UNUSED(name);
293 UNUSED(type);
294 UNUSED(rdclass);
295 UNUSED(wildcard);
297 return (ISC_TRUE);
300 static inline isc_boolean_t
301 checknames_key(ARGS_CHECKNAMES) {
303 REQUIRE(rdata->type == 25);
305 UNUSED(rdata);
306 UNUSED(owner);
307 UNUSED(bad);
309 return (ISC_TRUE);
312 #endif /* RDATA_GENERIC_KEY_25_C */