Import bind 9.5.2 vendor sources.
[dragonfly.git] / contrib / bind-9.5.2 / lib / dns / rdata / generic / dnskey_48.c
blob2e11cbaed46ae2d218b951ce7f5412a727036afc
1 /*
2 * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 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: dnskey_48.c,v 1.8 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_DNSKEY_48_C
27 #define RDATA_GENERIC_DNSKEY_48_C
29 #include <dst/dst.h>
31 #define RRTYPE_DNSKEY_ATTRIBUTES (DNS_RDATATYPEATTR_DNSSEC)
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;
40 REQUIRE(type == 48);
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_dnskey(ARGS_TOTEXT) {
75 isc_region_t sr;
76 char buf[sizeof("64000")];
77 unsigned int flags;
78 unsigned char algorithm;
80 REQUIRE(rdata->type == 48);
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_dnskey(ARGS_FROMWIRE) {
136 isc_region_t sr;
138 REQUIRE(type == 48);
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_dnskey(ARGS_TOWIRE) {
155 isc_region_t sr;
157 REQUIRE(rdata->type == 48);
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_dnskey(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 == 48);
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_dnskey(ARGS_FROMSTRUCT) {
184 dns_rdata_dnskey_t *dnskey = source;
186 REQUIRE(type == 48);
187 REQUIRE(source != NULL);
188 REQUIRE(dnskey->common.rdtype == type);
189 REQUIRE(dnskey->common.rdclass == rdclass);
191 UNUSED(type);
192 UNUSED(rdclass);
194 /* Flags */
195 RETERR(uint16_tobuffer(dnskey->flags, target));
197 /* Protocol */
198 RETERR(uint8_tobuffer(dnskey->protocol, target));
200 /* Algorithm */
201 RETERR(uint8_tobuffer(dnskey->algorithm, target));
203 /* Data */
204 return (mem_tobuffer(target, dnskey->data, dnskey->datalen));
207 static inline isc_result_t
208 tostruct_dnskey(ARGS_TOSTRUCT) {
209 dns_rdata_dnskey_t *dnskey = target;
210 isc_region_t sr;
212 REQUIRE(rdata->type == 48);
213 REQUIRE(target != NULL);
214 REQUIRE(rdata->length != 0);
216 dnskey->common.rdclass = rdata->rdclass;
217 dnskey->common.rdtype = rdata->type;
218 ISC_LINK_INIT(&dnskey->common, link);
220 dns_rdata_toregion(rdata, &sr);
222 /* Flags */
223 if (sr.length < 2)
224 return (ISC_R_UNEXPECTEDEND);
225 dnskey->flags = uint16_fromregion(&sr);
226 isc_region_consume(&sr, 2);
228 /* Protocol */
229 if (sr.length < 1)
230 return (ISC_R_UNEXPECTEDEND);
231 dnskey->protocol = uint8_fromregion(&sr);
232 isc_region_consume(&sr, 1);
234 /* Algorithm */
235 if (sr.length < 1)
236 return (ISC_R_UNEXPECTEDEND);
237 dnskey->algorithm = uint8_fromregion(&sr);
238 isc_region_consume(&sr, 1);
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);
246 dnskey->mctx = mctx;
247 return (ISC_R_SUCCESS);
250 static inline void
251 freestruct_dnskey(ARGS_FREESTRUCT) {
252 dns_rdata_dnskey_t *dnskey = (dns_rdata_dnskey_t *) source;
254 REQUIRE(source != NULL);
255 REQUIRE(dnskey->common.rdtype == 48);
257 if (dnskey->mctx == NULL)
258 return;
260 if (dnskey->data != NULL)
261 isc_mem_free(dnskey->mctx, dnskey->data);
262 dnskey->mctx = NULL;
265 static inline isc_result_t
266 additionaldata_dnskey(ARGS_ADDLDATA) {
267 REQUIRE(rdata->type == 48);
269 UNUSED(rdata);
270 UNUSED(add);
271 UNUSED(arg);
273 return (ISC_R_SUCCESS);
276 static inline isc_result_t
277 digest_dnskey(ARGS_DIGEST) {
278 isc_region_t r;
280 REQUIRE(rdata->type == 48);
282 dns_rdata_toregion(rdata, &r);
284 return ((digest)(arg, &r));
287 static inline isc_boolean_t
288 checkowner_dnskey(ARGS_CHECKOWNER) {
290 REQUIRE(type == 48);
292 UNUSED(name);
293 UNUSED(type);
294 UNUSED(rdclass);
295 UNUSED(wildcard);
297 return (ISC_TRUE);
300 static inline isc_boolean_t
301 checknames_dnskey(ARGS_CHECKNAMES) {
303 REQUIRE(rdata->type == 48);
305 UNUSED(rdata);
306 UNUSED(owner);
307 UNUSED(bad);
309 return (ISC_TRUE);
312 #endif /* RDATA_GENERIC_DNSKEY_48_C */