Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / dns / rdata / generic / dnskey_48.c
blobe19212ba8742dd7b4f05f2882ceec744917cd5a4
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: dnskey_48.c,v 1.10 2009/12/04 22:06:37 tbox Exp */
23 * Reviewed: Wed Mar 15 16:47:10 PST 2000 by halley.
26 /* RFC2535 */
28 #ifndef RDATA_GENERIC_DNSKEY_48_C
29 #define RDATA_GENERIC_DNSKEY_48_C
31 #include <dst/dst.h>
33 #define RRTYPE_DNSKEY_ATTRIBUTES (DNS_RDATATYPEATTR_DNSSEC)
35 static inline isc_result_t
36 fromtext_dnskey(ARGS_FROMTEXT) {
37 isc_token_t token;
38 dns_secalg_t alg;
39 dns_secproto_t proto;
40 dns_keyflags_t flags;
42 REQUIRE(type == 48);
44 UNUSED(type);
45 UNUSED(rdclass);
46 UNUSED(origin);
47 UNUSED(options);
48 UNUSED(callbacks);
50 /* flags */
51 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
52 ISC_FALSE));
53 RETTOK(dns_keyflags_fromtext(&flags, &token.value.as_textregion));
54 RETERR(uint16_tobuffer(flags, target));
56 /* protocol */
57 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
58 ISC_FALSE));
59 RETTOK(dns_secproto_fromtext(&proto, &token.value.as_textregion));
60 RETERR(mem_tobuffer(target, &proto, 1));
62 /* algorithm */
63 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
64 ISC_FALSE));
65 RETTOK(dns_secalg_fromtext(&alg, &token.value.as_textregion));
66 RETERR(mem_tobuffer(target, &alg, 1));
68 /* No Key? */
69 if ((flags & 0xc000) == 0xc000)
70 return (ISC_R_SUCCESS);
72 return (isc_base64_tobuffer(lexer, target, -1));
75 static inline isc_result_t
76 totext_dnskey(ARGS_TOTEXT) {
77 isc_region_t sr;
78 char buf[sizeof("64000")];
79 unsigned int flags;
80 unsigned char algorithm;
81 char namebuf[DNS_NAME_FORMATSIZE];
83 REQUIRE(rdata->type == 48);
84 REQUIRE(rdata->length != 0);
86 dns_rdata_toregion(rdata, &sr);
88 /* flags */
89 flags = uint16_fromregion(&sr);
90 isc_region_consume(&sr, 2);
91 sprintf(buf, "%u", flags);
92 RETERR(str_totext(buf, target));
93 RETERR(str_totext(" ", target));
95 /* protocol */
96 sprintf(buf, "%u", sr.base[0]);
97 isc_region_consume(&sr, 1);
98 RETERR(str_totext(buf, target));
99 RETERR(str_totext(" ", target));
101 /* algorithm */
102 algorithm = sr.base[0];
103 sprintf(buf, "%u", algorithm);
104 isc_region_consume(&sr, 1);
105 RETERR(str_totext(buf, target));
107 /* No Key? */
108 if ((flags & 0xc000) == 0xc000)
109 return (ISC_R_SUCCESS);
111 if ((tctx->flags & DNS_STYLEFLAG_COMMENT) != 0 &&
112 algorithm == DNS_KEYALG_PRIVATEDNS) {
113 dns_name_t name;
114 dns_name_init(&name, NULL);
115 dns_name_fromregion(&name, &sr);
116 dns_name_format(&name, namebuf, sizeof(namebuf));
117 } else
118 namebuf[0] = 0;
120 /* key */
121 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
122 RETERR(str_totext(" (", target));
123 RETERR(str_totext(tctx->linebreak, target));
124 RETERR(isc_base64_totext(&sr, tctx->width - 2,
125 tctx->linebreak, target));
127 if ((tctx->flags & DNS_STYLEFLAG_COMMENT) != 0)
128 RETERR(str_totext(tctx->linebreak, target));
129 else if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
130 RETERR(str_totext(" ", target));
132 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
133 RETERR(str_totext(")", target));
135 if ((tctx->flags & DNS_STYLEFLAG_COMMENT) != 0) {
136 isc_region_t tmpr;
138 RETERR(str_totext(" ; key id = ", target));
139 dns_rdata_toregion(rdata, &tmpr);
140 sprintf(buf, "%u", dst_region_computeid(&tmpr, algorithm));
141 RETERR(str_totext(buf, target));
142 if (algorithm == DNS_KEYALG_PRIVATEDNS) {
143 RETERR(str_totext(tctx->linebreak, target));
144 RETERR(str_totext("; alg = ", target));
145 RETERR(str_totext(namebuf, target));
148 return (ISC_R_SUCCESS);
151 static inline isc_result_t
152 fromwire_dnskey(ARGS_FROMWIRE) {
153 unsigned char algorithm;
154 isc_region_t sr;
156 REQUIRE(type == 48);
158 UNUSED(type);
159 UNUSED(rdclass);
160 UNUSED(dctx);
161 UNUSED(options);
163 isc_buffer_activeregion(source, &sr);
164 if (sr.length < 4)
165 return (ISC_R_UNEXPECTEDEND);
167 algorithm = sr.base[3];
168 RETERR(mem_tobuffer(target, sr.base, 4));
169 isc_region_consume(&sr, 4);
170 isc_buffer_forward(source, 4);
172 if (algorithm == DNS_KEYALG_PRIVATEDNS) {
173 dns_name_t name;
174 dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
175 dns_name_init(&name, NULL);
176 RETERR(dns_name_fromwire(&name, source, dctx, options, target));
178 isc_buffer_activeregion(source, &sr);
179 isc_buffer_forward(source, sr.length);
180 return (mem_tobuffer(target, sr.base, sr.length));
183 static inline isc_result_t
184 towire_dnskey(ARGS_TOWIRE) {
185 isc_region_t sr;
187 REQUIRE(rdata->type == 48);
188 REQUIRE(rdata->length != 0);
190 UNUSED(cctx);
192 dns_rdata_toregion(rdata, &sr);
193 return (mem_tobuffer(target, sr.base, sr.length));
196 static inline int
197 compare_dnskey(ARGS_COMPARE) {
198 isc_region_t r1;
199 isc_region_t r2;
201 REQUIRE(rdata1->type == rdata2->type);
202 REQUIRE(rdata1->rdclass == rdata2->rdclass);
203 REQUIRE(rdata1->type == 48);
204 REQUIRE(rdata1->length != 0);
205 REQUIRE(rdata2->length != 0);
207 dns_rdata_toregion(rdata1, &r1);
208 dns_rdata_toregion(rdata2, &r2);
209 return (isc_region_compare(&r1, &r2));
212 static inline isc_result_t
213 fromstruct_dnskey(ARGS_FROMSTRUCT) {
214 dns_rdata_dnskey_t *dnskey = source;
216 REQUIRE(type == 48);
217 REQUIRE(source != NULL);
218 REQUIRE(dnskey->common.rdtype == type);
219 REQUIRE(dnskey->common.rdclass == rdclass);
221 UNUSED(type);
222 UNUSED(rdclass);
224 /* Flags */
225 RETERR(uint16_tobuffer(dnskey->flags, target));
227 /* Protocol */
228 RETERR(uint8_tobuffer(dnskey->protocol, target));
230 /* Algorithm */
231 RETERR(uint8_tobuffer(dnskey->algorithm, target));
233 /* Data */
234 return (mem_tobuffer(target, dnskey->data, dnskey->datalen));
237 static inline isc_result_t
238 tostruct_dnskey(ARGS_TOSTRUCT) {
239 dns_rdata_dnskey_t *dnskey = target;
240 isc_region_t sr;
242 REQUIRE(rdata->type == 48);
243 REQUIRE(target != NULL);
244 REQUIRE(rdata->length != 0);
246 dnskey->common.rdclass = rdata->rdclass;
247 dnskey->common.rdtype = rdata->type;
248 ISC_LINK_INIT(&dnskey->common, link);
250 dns_rdata_toregion(rdata, &sr);
252 /* Flags */
253 if (sr.length < 2)
254 return (ISC_R_UNEXPECTEDEND);
255 dnskey->flags = uint16_fromregion(&sr);
256 isc_region_consume(&sr, 2);
258 /* Protocol */
259 if (sr.length < 1)
260 return (ISC_R_UNEXPECTEDEND);
261 dnskey->protocol = uint8_fromregion(&sr);
262 isc_region_consume(&sr, 1);
264 /* Algorithm */
265 if (sr.length < 1)
266 return (ISC_R_UNEXPECTEDEND);
267 dnskey->algorithm = uint8_fromregion(&sr);
268 isc_region_consume(&sr, 1);
270 /* Data */
271 dnskey->datalen = sr.length;
272 dnskey->data = mem_maybedup(mctx, sr.base, dnskey->datalen);
273 if (dnskey->data == NULL)
274 return (ISC_R_NOMEMORY);
276 dnskey->mctx = mctx;
277 return (ISC_R_SUCCESS);
280 static inline void
281 freestruct_dnskey(ARGS_FREESTRUCT) {
282 dns_rdata_dnskey_t *dnskey = (dns_rdata_dnskey_t *) source;
284 REQUIRE(source != NULL);
285 REQUIRE(dnskey->common.rdtype == 48);
287 if (dnskey->mctx == NULL)
288 return;
290 if (dnskey->data != NULL)
291 isc_mem_free(dnskey->mctx, dnskey->data);
292 dnskey->mctx = NULL;
295 static inline isc_result_t
296 additionaldata_dnskey(ARGS_ADDLDATA) {
297 REQUIRE(rdata->type == 48);
299 UNUSED(rdata);
300 UNUSED(add);
301 UNUSED(arg);
303 return (ISC_R_SUCCESS);
306 static inline isc_result_t
307 digest_dnskey(ARGS_DIGEST) {
308 isc_region_t r;
310 REQUIRE(rdata->type == 48);
312 dns_rdata_toregion(rdata, &r);
314 return ((digest)(arg, &r));
317 static inline isc_boolean_t
318 checkowner_dnskey(ARGS_CHECKOWNER) {
320 REQUIRE(type == 48);
322 UNUSED(name);
323 UNUSED(type);
324 UNUSED(rdclass);
325 UNUSED(wildcard);
327 return (ISC_TRUE);
330 static inline isc_boolean_t
331 checknames_dnskey(ARGS_CHECKNAMES) {
333 REQUIRE(rdata->type == 48);
335 UNUSED(rdata);
336 UNUSED(owner);
337 UNUSED(bad);
339 return (ISC_TRUE);
342 static inline int
343 casecompare_dnskey(ARGS_COMPARE) {
346 * Treat ALG 253 (private DNS) subtype name case sensistively.
348 return (compare_dnskey(rdata1, rdata2));
351 #endif /* RDATA_GENERIC_DNSKEY_48_C */