2 * Copyright (C) 2004, 2007, 2009, 2011, 2012 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.
21 * Reviewed: Thu Mar 16 17:35:30 PST 2000 by halley.
24 /* draft-ietf-dnsext-tkey-01.txt */
26 #ifndef RDATA_GENERIC_TKEY_249_C
27 #define RDATA_GENERIC_TKEY_249_C
29 #define RRTYPE_TKEY_ATTRIBUTES (DNS_RDATATYPEATTR_META)
31 static inline isc_result_t
32 fromtext_tkey(ARGS_FROMTEXT) {
49 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
51 dns_name_init(&name, NULL);
52 buffer_fromregion(&buffer, &token.value.as_region);
53 origin = (origin != NULL) ? origin : dns_rootname;
54 RETTOK(dns_name_fromtext(&name, &buffer, origin, options, target));
60 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
62 RETERR(uint32_tobuffer(token.value.as_ulong, target));
67 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
69 RETERR(uint32_tobuffer(token.value.as_ulong, target));
74 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
76 if (token.value.as_ulong > 0xffffU)
78 RETERR(uint16_tobuffer(token.value.as_ulong, target));
83 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
85 if (dns_tsigrcode_fromtext(&rcode, &token.value.as_textregion)
88 i = strtol(DNS_AS_STR(token), &e, 10);
90 RETTOK(DNS_R_UNKNOWN);
91 if (i < 0 || i > 0xffff)
93 rcode = (dns_rcode_t)i;
95 RETERR(uint16_tobuffer(rcode, target));
100 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
102 if (token.value.as_ulong > 0xffffU)
104 RETERR(uint16_tobuffer(token.value.as_ulong, target));
109 RETERR(isc_base64_tobuffer(lexer, target, (int)token.value.as_ulong));
114 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
116 if (token.value.as_ulong > 0xffffU)
118 RETERR(uint16_tobuffer(token.value.as_ulong, target));
123 return (isc_base64_tobuffer(lexer, target, (int)token.value.as_ulong));
126 static inline isc_result_t
127 totext_tkey(ARGS_TOTEXT) {
129 char buf[sizeof("4294967295 ")];
135 REQUIRE(rdata->type == 249);
136 REQUIRE(rdata->length != 0);
138 dns_rdata_toregion(rdata, &sr);
143 dns_name_init(&name, NULL);
144 dns_name_init(&prefix, NULL);
145 dns_name_fromregion(&name, &sr);
146 sub = name_prefix(&name, tctx->origin, &prefix);
147 RETERR(dns_name_totext(&prefix, sub, target));
148 RETERR(str_totext(" ", target));
149 isc_region_consume(&sr, name_length(&name));
154 n = uint32_fromregion(&sr);
155 isc_region_consume(&sr, 4);
156 sprintf(buf, "%lu ", n);
157 RETERR(str_totext(buf, target));
162 n = uint32_fromregion(&sr);
163 isc_region_consume(&sr, 4);
164 sprintf(buf, "%lu ", n);
165 RETERR(str_totext(buf, target));
170 n = uint16_fromregion(&sr);
171 isc_region_consume(&sr, 2);
172 sprintf(buf, "%lu ", n);
173 RETERR(str_totext(buf, target));
178 n = uint16_fromregion(&sr);
179 isc_region_consume(&sr, 2);
180 if (dns_tsigrcode_totext((dns_rcode_t)n, target) == ISC_R_SUCCESS)
181 RETERR(str_totext(" ", target));
183 sprintf(buf, "%lu ", n);
184 RETERR(str_totext(buf, target));
190 n = uint16_fromregion(&sr);
191 isc_region_consume(&sr, 2);
192 sprintf(buf, "%lu", n);
193 RETERR(str_totext(buf, target));
198 REQUIRE(n <= sr.length);
201 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
202 RETERR(str_totext(" (", target));
203 RETERR(str_totext(tctx->linebreak, target));
204 if (tctx->width == 0) /* No splitting */
205 RETERR(isc_base64_totext(&dr, 60, "", target));
207 RETERR(isc_base64_totext(&dr, tctx->width - 2,
208 tctx->linebreak, target));
209 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
210 RETERR(str_totext(" ) ", target));
212 RETERR(str_totext(" ", target));
213 isc_region_consume(&sr, n);
218 n = uint16_fromregion(&sr);
219 isc_region_consume(&sr, 2);
220 sprintf(buf, "%lu", n);
221 RETERR(str_totext(buf, target));
226 REQUIRE(n <= sr.length);
230 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
231 RETERR(str_totext(" (", target));
232 RETERR(str_totext(tctx->linebreak, target));
233 if (tctx->width == 0) /* No splitting */
234 RETERR(isc_base64_totext(&dr, 60, "", target));
236 RETERR(isc_base64_totext(&dr, tctx->width - 2,
237 tctx->linebreak, target));
238 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
239 RETERR(str_totext(" )", target));
241 return (ISC_R_SUCCESS);
244 static inline isc_result_t
245 fromwire_tkey(ARGS_FROMWIRE) {
250 REQUIRE(type == 249);
255 dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
260 dns_name_init(&name, NULL);
261 RETERR(dns_name_fromwire(&name, source, dctx, options, target));
269 isc_buffer_activeregion(source, &sr);
271 return (ISC_R_UNEXPECTEDEND);
272 RETERR(mem_tobuffer(target, sr.base, 12));
273 isc_region_consume(&sr, 12);
274 isc_buffer_forward(source, 12);
277 * Key Length + Key Data.
280 return (ISC_R_UNEXPECTEDEND);
281 n = uint16_fromregion(&sr);
282 if (sr.length < n + 2)
283 return (ISC_R_UNEXPECTEDEND);
284 RETERR(mem_tobuffer(target, sr.base, n + 2));
285 isc_region_consume(&sr, n + 2);
286 isc_buffer_forward(source, n + 2);
289 * Other Length + Other Data.
292 return (ISC_R_UNEXPECTEDEND);
293 n = uint16_fromregion(&sr);
294 if (sr.length < n + 2)
295 return (ISC_R_UNEXPECTEDEND);
296 isc_buffer_forward(source, n + 2);
297 return (mem_tobuffer(target, sr.base, n + 2));
300 static inline isc_result_t
301 towire_tkey(ARGS_TOWIRE) {
304 dns_offsets_t offsets;
306 REQUIRE(rdata->type == 249);
307 REQUIRE(rdata->length != 0);
309 dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
313 dns_rdata_toregion(rdata, &sr);
314 dns_name_init(&name, offsets);
315 dns_name_fromregion(&name, &sr);
316 RETERR(dns_name_towire(&name, cctx, target));
317 isc_region_consume(&sr, name_length(&name));
319 return (mem_tobuffer(target, sr.base, sr.length));
323 compare_tkey(ARGS_COMPARE) {
330 REQUIRE(rdata1->type == rdata2->type);
331 REQUIRE(rdata1->rdclass == rdata2->rdclass);
332 REQUIRE(rdata1->type == 249);
333 REQUIRE(rdata1->length != 0);
334 REQUIRE(rdata2->length != 0);
339 dns_rdata_toregion(rdata1, &r1);
340 dns_rdata_toregion(rdata2, &r2);
341 dns_name_init(&name1, NULL);
342 dns_name_init(&name2, NULL);
343 dns_name_fromregion(&name1, &r1);
344 dns_name_fromregion(&name2, &r2);
345 if ((order = dns_name_rdatacompare(&name1, &name2)) != 0)
347 isc_region_consume(&r1, name_length(&name1));
348 isc_region_consume(&r2, name_length(&name2));
349 return (isc_region_compare(&r1, &r2));
352 static inline isc_result_t
353 fromstruct_tkey(ARGS_FROMSTRUCT) {
354 dns_rdata_tkey_t *tkey = source;
356 REQUIRE(type == 249);
357 REQUIRE(source != NULL);
358 REQUIRE(tkey->common.rdtype == type);
359 REQUIRE(tkey->common.rdclass == rdclass);
367 RETERR(name_tobuffer(&tkey->algorithm, target));
370 * Inception: 32 bits.
372 RETERR(uint32_tobuffer(tkey->inception, target));
377 RETERR(uint32_tobuffer(tkey->expire, target));
382 RETERR(uint16_tobuffer(tkey->mode, target));
387 RETERR(uint16_tobuffer(tkey->error, target));
392 RETERR(uint16_tobuffer(tkey->keylen, target));
397 RETERR(mem_tobuffer(target, tkey->key, tkey->keylen));
400 * Other size: 16 bits.
402 RETERR(uint16_tobuffer(tkey->otherlen, target));
407 return (mem_tobuffer(target, tkey->other, tkey->otherlen));
410 static inline isc_result_t
411 tostruct_tkey(ARGS_TOSTRUCT) {
412 dns_rdata_tkey_t *tkey = target;
416 REQUIRE(rdata->type == 249);
417 REQUIRE(target != NULL);
418 REQUIRE(rdata->length != 0);
420 tkey->common.rdclass = rdata->rdclass;
421 tkey->common.rdtype = rdata->type;
422 ISC_LINK_INIT(&tkey->common, link);
424 dns_rdata_toregion(rdata, &sr);
429 dns_name_init(&alg, NULL);
430 dns_name_fromregion(&alg, &sr);
431 dns_name_init(&tkey->algorithm, NULL);
432 RETERR(name_duporclone(&alg, mctx, &tkey->algorithm));
433 isc_region_consume(&sr, name_length(&tkey->algorithm));
438 tkey->inception = uint32_fromregion(&sr);
439 isc_region_consume(&sr, 4);
444 tkey->expire = uint32_fromregion(&sr);
445 isc_region_consume(&sr, 4);
450 tkey->mode = uint16_fromregion(&sr);
451 isc_region_consume(&sr, 2);
456 tkey->error = uint16_fromregion(&sr);
457 isc_region_consume(&sr, 2);
462 tkey->keylen = uint16_fromregion(&sr);
463 isc_region_consume(&sr, 2);
468 tkey->key = mem_maybedup(mctx, sr.base, tkey->keylen);
469 if (tkey->key == NULL)
471 isc_region_consume(&sr, tkey->keylen);
476 tkey->otherlen = uint16_fromregion(&sr);
477 isc_region_consume(&sr, 2);
482 tkey->other = mem_maybedup(mctx, sr.base, tkey->otherlen);
483 if (tkey->other == NULL)
487 return (ISC_R_SUCCESS);
491 dns_name_free(&tkey->algorithm, mctx);
492 if (mctx != NULL && tkey->key != NULL)
493 isc_mem_free(mctx, tkey->key);
494 return (ISC_R_NOMEMORY);
498 freestruct_tkey(ARGS_FREESTRUCT) {
499 dns_rdata_tkey_t *tkey = (dns_rdata_tkey_t *) source;
501 REQUIRE(source != NULL);
503 if (tkey->mctx == NULL)
506 dns_name_free(&tkey->algorithm, tkey->mctx);
507 if (tkey->key != NULL)
508 isc_mem_free(tkey->mctx, tkey->key);
509 if (tkey->other != NULL)
510 isc_mem_free(tkey->mctx, tkey->other);
514 static inline isc_result_t
515 additionaldata_tkey(ARGS_ADDLDATA) {
520 REQUIRE(rdata->type == 249);
522 return (ISC_R_SUCCESS);
525 static inline isc_result_t
526 digest_tkey(ARGS_DIGEST) {
531 REQUIRE(rdata->type == 249);
533 return (ISC_R_NOTIMPLEMENTED);
536 static inline isc_boolean_t
537 checkowner_tkey(ARGS_CHECKOWNER) {
539 REQUIRE(type == 249);
549 static inline isc_boolean_t
550 checknames_tkey(ARGS_CHECKNAMES) {
552 REQUIRE(rdata->type == 249);
561 static inline isc_result_t
562 casecompare_tkey(ARGS_COMPARE) {
563 return (compare_tkey(rdata1, rdata2));
565 #endif /* RDATA_GENERIC_TKEY_249_C */