Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / dns / rdata / generic / tkey_249.c
blob66332738e4229792affc9ddbff06a715d564b816
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-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: tkey_249.c,v 1.59 2009/12/04 22:06:37 tbox Exp */
23 * Reviewed: Thu Mar 16 17:35:30 PST 2000 by halley.
26 /* draft-ietf-dnsext-tkey-01.txt */
28 #ifndef RDATA_GENERIC_TKEY_249_C
29 #define RDATA_GENERIC_TKEY_249_C
31 #define RRTYPE_TKEY_ATTRIBUTES (DNS_RDATATYPEATTR_META)
33 static inline isc_result_t
34 fromtext_tkey(ARGS_FROMTEXT) {
35 isc_token_t token;
36 dns_rcode_t rcode;
37 dns_name_t name;
38 isc_buffer_t buffer;
39 long i;
40 char *e;
42 REQUIRE(type == 249);
44 UNUSED(type);
45 UNUSED(rdclass);
46 UNUSED(callbacks);
49 * Algorithm.
51 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
52 ISC_FALSE));
53 dns_name_init(&name, NULL);
54 buffer_fromregion(&buffer, &token.value.as_region);
55 origin = (origin != NULL) ? origin : dns_rootname;
56 RETTOK(dns_name_fromtext(&name, &buffer, origin, options, target));
60 * Inception.
62 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
63 ISC_FALSE));
64 RETERR(uint32_tobuffer(token.value.as_ulong, target));
67 * Expiration.
69 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
70 ISC_FALSE));
71 RETERR(uint32_tobuffer(token.value.as_ulong, target));
74 * Mode.
76 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
77 ISC_FALSE));
78 if (token.value.as_ulong > 0xffffU)
79 RETTOK(ISC_R_RANGE);
80 RETERR(uint16_tobuffer(token.value.as_ulong, target));
83 * Error.
85 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
86 ISC_FALSE));
87 if (dns_tsigrcode_fromtext(&rcode, &token.value.as_textregion)
88 != ISC_R_SUCCESS)
90 i = strtol(DNS_AS_STR(token), &e, 10);
91 if (*e != 0)
92 RETTOK(DNS_R_UNKNOWN);
93 if (i < 0 || i > 0xffff)
94 RETTOK(ISC_R_RANGE);
95 rcode = (dns_rcode_t)i;
97 RETERR(uint16_tobuffer(rcode, target));
100 * Key Size.
102 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
103 ISC_FALSE));
104 if (token.value.as_ulong > 0xffffU)
105 RETTOK(ISC_R_RANGE);
106 RETERR(uint16_tobuffer(token.value.as_ulong, target));
109 * Key Data.
111 RETERR(isc_base64_tobuffer(lexer, target, (int)token.value.as_ulong));
114 * Other Size.
116 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
117 ISC_FALSE));
118 if (token.value.as_ulong > 0xffffU)
119 RETTOK(ISC_R_RANGE);
120 RETERR(uint16_tobuffer(token.value.as_ulong, target));
123 * Other Data.
125 return (isc_base64_tobuffer(lexer, target, (int)token.value.as_ulong));
128 static inline isc_result_t
129 totext_tkey(ARGS_TOTEXT) {
130 isc_region_t sr, dr;
131 char buf[sizeof("4294967295 ")];
132 unsigned long n;
133 dns_name_t name;
134 dns_name_t prefix;
135 isc_boolean_t sub;
137 REQUIRE(rdata->type == 249);
138 REQUIRE(rdata->length != 0);
140 dns_rdata_toregion(rdata, &sr);
143 * Algorithm.
145 dns_name_init(&name, NULL);
146 dns_name_init(&prefix, NULL);
147 dns_name_fromregion(&name, &sr);
148 sub = name_prefix(&name, tctx->origin, &prefix);
149 RETERR(dns_name_totext(&prefix, sub, target));
150 RETERR(str_totext(" ", target));
151 isc_region_consume(&sr, name_length(&name));
154 * Inception.
156 n = uint32_fromregion(&sr);
157 isc_region_consume(&sr, 4);
158 sprintf(buf, "%lu ", n);
159 RETERR(str_totext(buf, target));
162 * Expiration.
164 n = uint32_fromregion(&sr);
165 isc_region_consume(&sr, 4);
166 sprintf(buf, "%lu ", n);
167 RETERR(str_totext(buf, target));
170 * Mode.
172 n = uint16_fromregion(&sr);
173 isc_region_consume(&sr, 2);
174 sprintf(buf, "%lu ", n);
175 RETERR(str_totext(buf, target));
178 * Error.
180 n = uint16_fromregion(&sr);
181 isc_region_consume(&sr, 2);
182 if (dns_tsigrcode_totext((dns_rcode_t)n, target) == ISC_R_SUCCESS)
183 RETERR(str_totext(" ", target));
184 else {
185 sprintf(buf, "%lu ", n);
186 RETERR(str_totext(buf, target));
190 * Key Size.
192 n = uint16_fromregion(&sr);
193 isc_region_consume(&sr, 2);
194 sprintf(buf, "%lu", n);
195 RETERR(str_totext(buf, target));
198 * Key Data.
200 REQUIRE(n <= sr.length);
201 dr = sr;
202 dr.length = n;
203 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
204 RETERR(str_totext(" (", target));
205 RETERR(str_totext(tctx->linebreak, target));
206 RETERR(isc_base64_totext(&dr, tctx->width - 2,
207 tctx->linebreak, target));
208 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
209 RETERR(str_totext(" ) ", target));
210 else
211 RETERR(str_totext(" ", target));
212 isc_region_consume(&sr, n);
215 * Other Size.
217 n = uint16_fromregion(&sr);
218 isc_region_consume(&sr, 2);
219 sprintf(buf, "%lu", n);
220 RETERR(str_totext(buf, target));
223 * Other Data.
225 REQUIRE(n <= sr.length);
226 if (n != 0U) {
227 dr = sr;
228 dr.length = n;
229 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
230 RETERR(str_totext(" (", target));
231 RETERR(str_totext(tctx->linebreak, target));
232 RETERR(isc_base64_totext(&dr, tctx->width - 2,
233 tctx->linebreak, target));
234 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
235 RETERR(str_totext(" )", target));
237 return (ISC_R_SUCCESS);
240 static inline isc_result_t
241 fromwire_tkey(ARGS_FROMWIRE) {
242 isc_region_t sr;
243 unsigned long n;
244 dns_name_t name;
246 REQUIRE(type == 249);
248 UNUSED(type);
249 UNUSED(rdclass);
251 dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
254 * Algorithm.
256 dns_name_init(&name, NULL);
257 RETERR(dns_name_fromwire(&name, source, dctx, options, target));
260 * Inception: 4
261 * Expiration: 4
262 * Mode: 2
263 * Error: 2
265 isc_buffer_activeregion(source, &sr);
266 if (sr.length < 12)
267 return (ISC_R_UNEXPECTEDEND);
268 RETERR(mem_tobuffer(target, sr.base, 12));
269 isc_region_consume(&sr, 12);
270 isc_buffer_forward(source, 12);
273 * Key Length + Key Data.
275 if (sr.length < 2)
276 return (ISC_R_UNEXPECTEDEND);
277 n = uint16_fromregion(&sr);
278 if (sr.length < n + 2)
279 return (ISC_R_UNEXPECTEDEND);
280 RETERR(mem_tobuffer(target, sr.base, n + 2));
281 isc_region_consume(&sr, n + 2);
282 isc_buffer_forward(source, n + 2);
285 * Other Length + Other Data.
287 if (sr.length < 2)
288 return (ISC_R_UNEXPECTEDEND);
289 n = uint16_fromregion(&sr);
290 if (sr.length < n + 2)
291 return (ISC_R_UNEXPECTEDEND);
292 isc_buffer_forward(source, n + 2);
293 return (mem_tobuffer(target, sr.base, n + 2));
296 static inline isc_result_t
297 towire_tkey(ARGS_TOWIRE) {
298 isc_region_t sr;
299 dns_name_t name;
300 dns_offsets_t offsets;
302 REQUIRE(rdata->type == 249);
303 REQUIRE(rdata->length != 0);
305 dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
307 * Algorithm.
309 dns_rdata_toregion(rdata, &sr);
310 dns_name_init(&name, offsets);
311 dns_name_fromregion(&name, &sr);
312 RETERR(dns_name_towire(&name, cctx, target));
313 isc_region_consume(&sr, name_length(&name));
315 return (mem_tobuffer(target, sr.base, sr.length));
318 static inline int
319 compare_tkey(ARGS_COMPARE) {
320 isc_region_t r1;
321 isc_region_t r2;
322 dns_name_t name1;
323 dns_name_t name2;
324 int order;
326 REQUIRE(rdata1->type == rdata2->type);
327 REQUIRE(rdata1->rdclass == rdata2->rdclass);
328 REQUIRE(rdata1->type == 249);
329 REQUIRE(rdata1->length != 0);
330 REQUIRE(rdata2->length != 0);
333 * Algorithm.
335 dns_rdata_toregion(rdata1, &r1);
336 dns_rdata_toregion(rdata2, &r2);
337 dns_name_init(&name1, NULL);
338 dns_name_init(&name2, NULL);
339 dns_name_fromregion(&name1, &r1);
340 dns_name_fromregion(&name2, &r2);
341 if ((order = dns_name_rdatacompare(&name1, &name2)) != 0)
342 return (order);
343 isc_region_consume(&r1, name_length(&name1));
344 isc_region_consume(&r2, name_length(&name2));
345 return (isc_region_compare(&r1, &r2));
348 static inline isc_result_t
349 fromstruct_tkey(ARGS_FROMSTRUCT) {
350 dns_rdata_tkey_t *tkey = source;
352 REQUIRE(type == 249);
353 REQUIRE(source != NULL);
354 REQUIRE(tkey->common.rdtype == type);
355 REQUIRE(tkey->common.rdclass == rdclass);
357 UNUSED(type);
358 UNUSED(rdclass);
361 * Algorithm Name.
363 RETERR(name_tobuffer(&tkey->algorithm, target));
366 * Inception: 32 bits.
368 RETERR(uint32_tobuffer(tkey->inception, target));
371 * Expire: 32 bits.
373 RETERR(uint32_tobuffer(tkey->expire, target));
376 * Mode: 16 bits.
378 RETERR(uint16_tobuffer(tkey->mode, target));
381 * Error: 16 bits.
383 RETERR(uint16_tobuffer(tkey->error, target));
386 * Key size: 16 bits.
388 RETERR(uint16_tobuffer(tkey->keylen, target));
391 * Key.
393 RETERR(mem_tobuffer(target, tkey->key, tkey->keylen));
396 * Other size: 16 bits.
398 RETERR(uint16_tobuffer(tkey->otherlen, target));
401 * Other data.
403 return (mem_tobuffer(target, tkey->other, tkey->otherlen));
406 static inline isc_result_t
407 tostruct_tkey(ARGS_TOSTRUCT) {
408 dns_rdata_tkey_t *tkey = target;
409 dns_name_t alg;
410 isc_region_t sr;
412 REQUIRE(rdata->type == 249);
413 REQUIRE(target != NULL);
414 REQUIRE(rdata->length != 0);
416 tkey->common.rdclass = rdata->rdclass;
417 tkey->common.rdtype = rdata->type;
418 ISC_LINK_INIT(&tkey->common, link);
420 dns_rdata_toregion(rdata, &sr);
423 * Algorithm Name.
425 dns_name_init(&alg, NULL);
426 dns_name_fromregion(&alg, &sr);
427 dns_name_init(&tkey->algorithm, NULL);
428 RETERR(name_duporclone(&alg, mctx, &tkey->algorithm));
429 isc_region_consume(&sr, name_length(&tkey->algorithm));
432 * Inception.
434 tkey->inception = uint32_fromregion(&sr);
435 isc_region_consume(&sr, 4);
438 * Expire.
440 tkey->expire = uint32_fromregion(&sr);
441 isc_region_consume(&sr, 4);
444 * Mode.
446 tkey->mode = uint16_fromregion(&sr);
447 isc_region_consume(&sr, 2);
450 * Error.
452 tkey->error = uint16_fromregion(&sr);
453 isc_region_consume(&sr, 2);
456 * Key size.
458 tkey->keylen = uint16_fromregion(&sr);
459 isc_region_consume(&sr, 2);
462 * Key.
464 tkey->key = mem_maybedup(mctx, sr.base, tkey->keylen);
465 if (tkey->key == NULL)
466 goto cleanup;
467 isc_region_consume(&sr, tkey->keylen);
470 * Other size.
472 tkey->otherlen = uint16_fromregion(&sr);
473 isc_region_consume(&sr, 2);
476 * Other.
478 tkey->other = mem_maybedup(mctx, sr.base, tkey->otherlen);
479 if (tkey->other == NULL)
480 goto cleanup;
482 tkey->mctx = mctx;
483 return (ISC_R_SUCCESS);
485 cleanup:
486 if (mctx != NULL)
487 dns_name_free(&tkey->algorithm, mctx);
488 if (mctx != NULL && tkey->key != NULL)
489 isc_mem_free(mctx, tkey->key);
490 return (ISC_R_NOMEMORY);
493 static inline void
494 freestruct_tkey(ARGS_FREESTRUCT) {
495 dns_rdata_tkey_t *tkey = (dns_rdata_tkey_t *) source;
497 REQUIRE(source != NULL);
499 if (tkey->mctx == NULL)
500 return;
502 dns_name_free(&tkey->algorithm, tkey->mctx);
503 if (tkey->key != NULL)
504 isc_mem_free(tkey->mctx, tkey->key);
505 if (tkey->other != NULL)
506 isc_mem_free(tkey->mctx, tkey->other);
507 tkey->mctx = NULL;
510 static inline isc_result_t
511 additionaldata_tkey(ARGS_ADDLDATA) {
512 UNUSED(rdata);
513 UNUSED(add);
514 UNUSED(arg);
516 REQUIRE(rdata->type == 249);
518 return (ISC_R_SUCCESS);
521 static inline isc_result_t
522 digest_tkey(ARGS_DIGEST) {
523 UNUSED(rdata);
524 UNUSED(digest);
525 UNUSED(arg);
527 REQUIRE(rdata->type == 249);
529 return (ISC_R_NOTIMPLEMENTED);
532 static inline isc_boolean_t
533 checkowner_tkey(ARGS_CHECKOWNER) {
535 REQUIRE(type == 249);
537 UNUSED(name);
538 UNUSED(type);
539 UNUSED(rdclass);
540 UNUSED(wildcard);
542 return (ISC_TRUE);
545 static inline isc_boolean_t
546 checknames_tkey(ARGS_CHECKNAMES) {
548 REQUIRE(rdata->type == 249);
550 UNUSED(rdata);
551 UNUSED(owner);
552 UNUSED(bad);
554 return (ISC_TRUE);
557 static inline isc_result_t
558 casecompare_tkey(ARGS_COMPARE) {
559 return (compare_tkey(rdata1, rdata2));
561 #endif /* RDATA_GENERIC_TKEY_249_C */