1 /*	$NetBSD: tkey_249.c,v 1.5 2014/12/10 04:37:59 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2007, 2009, 2011, 2012, 2014  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 1999-2003  Internet Software Consortium.
6  *
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.
10  *
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.
18  */
19 
20 /* Id */
21 
22 /*
23  * Reviewed: Thu Mar 16 17:35:30 PST 2000 by halley.
24  */
25 
26 /* draft-ietf-dnsext-tkey-01.txt */
27 
28 #ifndef RDATA_GENERIC_TKEY_249_C
29 #define RDATA_GENERIC_TKEY_249_C
30 
31 #define RRTYPE_TKEY_ATTRIBUTES (DNS_RDATATYPEATTR_META)
32 
33 static inline isc_result_t
fromtext_tkey(ARGS_FROMTEXT)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;
41 
42 	REQUIRE(type == 249);
43 
44 	UNUSED(type);
45 	UNUSED(rdclass);
46 	UNUSED(callbacks);
47 
48 	/*
49 	 * Algorithm.
50 	 */
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));
57 
58 
59 	/*
60 	 * Inception.
61 	 */
62 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
63 				      ISC_FALSE));
64 	RETERR(uint32_tobuffer(token.value.as_ulong, target));
65 
66 	/*
67 	 * Expiration.
68 	 */
69 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
70 				      ISC_FALSE));
71 	RETERR(uint32_tobuffer(token.value.as_ulong, target));
72 
73 	/*
74 	 * Mode.
75 	 */
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));
81 
82 	/*
83 	 * Error.
84 	 */
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)
89 	{
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;
96 	}
97 	RETERR(uint16_tobuffer(rcode, target));
98 
99 	/*
100 	 * Key Size.
101 	 */
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));
107 
108 	/*
109 	 * Key Data.
110 	 */
111 	RETERR(isc_base64_tobuffer(lexer, target, (int)token.value.as_ulong));
112 
113 	/*
114 	 * Other Size.
115 	 */
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));
121 
122 	/*
123 	 * Other Data.
124 	 */
125 	return (isc_base64_tobuffer(lexer, target, (int)token.value.as_ulong));
126 }
127 
128 static inline isc_result_t
totext_tkey(ARGS_TOTEXT)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;
136 
137 	REQUIRE(rdata->type == 249);
138 	REQUIRE(rdata->length != 0);
139 
140 	dns_rdata_toregion(rdata, &sr);
141 
142 	/*
143 	 * Algorithm.
144 	 */
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));
152 
153 	/*
154 	 * Inception.
155 	 */
156 	n = uint32_fromregion(&sr);
157 	isc_region_consume(&sr, 4);
158 	sprintf(buf, "%lu ", n);
159 	RETERR(str_totext(buf, target));
160 
161 	/*
162 	 * Expiration.
163 	 */
164 	n = uint32_fromregion(&sr);
165 	isc_region_consume(&sr, 4);
166 	sprintf(buf, "%lu ", n);
167 	RETERR(str_totext(buf, target));
168 
169 	/*
170 	 * Mode.
171 	 */
172 	n = uint16_fromregion(&sr);
173 	isc_region_consume(&sr, 2);
174 	sprintf(buf, "%lu ", n);
175 	RETERR(str_totext(buf, target));
176 
177 	/*
178 	 * Error.
179 	 */
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));
187 	}
188 
189 	/*
190 	 * Key Size.
191 	 */
192 	n = uint16_fromregion(&sr);
193 	isc_region_consume(&sr, 2);
194 	sprintf(buf, "%lu", n);
195 	RETERR(str_totext(buf, target));
196 
197 	/*
198 	 * Key Data.
199 	 */
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 	if (tctx->width == 0)   /* No splitting */
207 		RETERR(isc_base64_totext(&dr, 60, "", target));
208 	else
209 		RETERR(isc_base64_totext(&dr, tctx->width - 2,
210 					 tctx->linebreak, target));
211 	if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
212 		RETERR(str_totext(" ) ", target));
213 	else
214 		RETERR(str_totext(" ", target));
215 	isc_region_consume(&sr, n);
216 
217 	/*
218 	 * Other Size.
219 	 */
220 	n = uint16_fromregion(&sr);
221 	isc_region_consume(&sr, 2);
222 	sprintf(buf, "%lu", n);
223 	RETERR(str_totext(buf, target));
224 
225 	/*
226 	 * Other Data.
227 	 */
228 	REQUIRE(n <= sr.length);
229 	if (n != 0U) {
230 	    dr = sr;
231 	    dr.length = n;
232 	    if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
233 		    RETERR(str_totext(" (", target));
234 	    RETERR(str_totext(tctx->linebreak, target));
235 		if (tctx->width == 0)   /* No splitting */
236 			RETERR(isc_base64_totext(&dr, 60, "", target));
237 		else
238 			RETERR(isc_base64_totext(&dr, tctx->width - 2,
239 						 tctx->linebreak, target));
240 	    if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
241 		    RETERR(str_totext(" )", target));
242 	}
243 	return (ISC_R_SUCCESS);
244 }
245 
246 static inline isc_result_t
fromwire_tkey(ARGS_FROMWIRE)247 fromwire_tkey(ARGS_FROMWIRE) {
248 	isc_region_t sr;
249 	unsigned long n;
250 	dns_name_t name;
251 
252 	REQUIRE(type == 249);
253 
254 	UNUSED(type);
255 	UNUSED(rdclass);
256 
257 	dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
258 
259 	/*
260 	 * Algorithm.
261 	 */
262 	dns_name_init(&name, NULL);
263 	RETERR(dns_name_fromwire(&name, source, dctx, options, target));
264 
265 	/*
266 	 * Inception: 4
267 	 * Expiration: 4
268 	 * Mode: 2
269 	 * Error: 2
270 	 */
271 	isc_buffer_activeregion(source, &sr);
272 	if (sr.length < 12)
273 		return (ISC_R_UNEXPECTEDEND);
274 	RETERR(mem_tobuffer(target, sr.base, 12));
275 	isc_region_consume(&sr, 12);
276 	isc_buffer_forward(source, 12);
277 
278 	/*
279 	 * Key Length + Key Data.
280 	 */
281 	if (sr.length < 2)
282 		return (ISC_R_UNEXPECTEDEND);
283 	n = uint16_fromregion(&sr);
284 	if (sr.length < n + 2)
285 		return (ISC_R_UNEXPECTEDEND);
286 	RETERR(mem_tobuffer(target, sr.base, n + 2));
287 	isc_region_consume(&sr, n + 2);
288 	isc_buffer_forward(source, n + 2);
289 
290 	/*
291 	 * Other Length + Other Data.
292 	 */
293 	if (sr.length < 2)
294 		return (ISC_R_UNEXPECTEDEND);
295 	n = uint16_fromregion(&sr);
296 	if (sr.length < n + 2)
297 		return (ISC_R_UNEXPECTEDEND);
298 	isc_buffer_forward(source, n + 2);
299 	return (mem_tobuffer(target, sr.base, n + 2));
300 }
301 
302 static inline isc_result_t
towire_tkey(ARGS_TOWIRE)303 towire_tkey(ARGS_TOWIRE) {
304 	isc_region_t sr;
305 	dns_name_t name;
306 	dns_offsets_t offsets;
307 
308 	REQUIRE(rdata->type == 249);
309 	REQUIRE(rdata->length != 0);
310 
311 	dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
312 	/*
313 	 * Algorithm.
314 	 */
315 	dns_rdata_toregion(rdata, &sr);
316 	dns_name_init(&name, offsets);
317 	dns_name_fromregion(&name, &sr);
318 	RETERR(dns_name_towire(&name, cctx, target));
319 	isc_region_consume(&sr, name_length(&name));
320 
321 	return (mem_tobuffer(target, sr.base, sr.length));
322 }
323 
324 static inline int
compare_tkey(ARGS_COMPARE)325 compare_tkey(ARGS_COMPARE) {
326 	isc_region_t r1;
327 	isc_region_t r2;
328 	dns_name_t name1;
329 	dns_name_t name2;
330 	int order;
331 
332 	REQUIRE(rdata1->type == rdata2->type);
333 	REQUIRE(rdata1->rdclass == rdata2->rdclass);
334 	REQUIRE(rdata1->type == 249);
335 	REQUIRE(rdata1->length != 0);
336 	REQUIRE(rdata2->length != 0);
337 
338 	/*
339 	 * Algorithm.
340 	 */
341 	dns_rdata_toregion(rdata1, &r1);
342 	dns_rdata_toregion(rdata2, &r2);
343 	dns_name_init(&name1, NULL);
344 	dns_name_init(&name2, NULL);
345 	dns_name_fromregion(&name1, &r1);
346 	dns_name_fromregion(&name2, &r2);
347 	if ((order = dns_name_rdatacompare(&name1, &name2)) != 0)
348 		return (order);
349 	isc_region_consume(&r1, name_length(&name1));
350 	isc_region_consume(&r2, name_length(&name2));
351 	return (isc_region_compare(&r1, &r2));
352 }
353 
354 static inline isc_result_t
fromstruct_tkey(ARGS_FROMSTRUCT)355 fromstruct_tkey(ARGS_FROMSTRUCT) {
356 	dns_rdata_tkey_t *tkey = source;
357 
358 	REQUIRE(type == 249);
359 	REQUIRE(source != NULL);
360 	REQUIRE(tkey->common.rdtype == type);
361 	REQUIRE(tkey->common.rdclass == rdclass);
362 
363 	UNUSED(type);
364 	UNUSED(rdclass);
365 
366 	/*
367 	 * Algorithm Name.
368 	 */
369 	RETERR(name_tobuffer(&tkey->algorithm, target));
370 
371 	/*
372 	 * Inception: 32 bits.
373 	 */
374 	RETERR(uint32_tobuffer(tkey->inception, target));
375 
376 	/*
377 	 * Expire: 32 bits.
378 	 */
379 	RETERR(uint32_tobuffer(tkey->expire, target));
380 
381 	/*
382 	 * Mode: 16 bits.
383 	 */
384 	RETERR(uint16_tobuffer(tkey->mode, target));
385 
386 	/*
387 	 * Error: 16 bits.
388 	 */
389 	RETERR(uint16_tobuffer(tkey->error, target));
390 
391 	/*
392 	 * Key size: 16 bits.
393 	 */
394 	RETERR(uint16_tobuffer(tkey->keylen, target));
395 
396 	/*
397 	 * Key.
398 	 */
399 	RETERR(mem_tobuffer(target, tkey->key, tkey->keylen));
400 
401 	/*
402 	 * Other size: 16 bits.
403 	 */
404 	RETERR(uint16_tobuffer(tkey->otherlen, target));
405 
406 	/*
407 	 * Other data.
408 	 */
409 	return (mem_tobuffer(target, tkey->other, tkey->otherlen));
410 }
411 
412 static inline isc_result_t
tostruct_tkey(ARGS_TOSTRUCT)413 tostruct_tkey(ARGS_TOSTRUCT) {
414 	dns_rdata_tkey_t *tkey = target;
415 	dns_name_t alg;
416 	isc_region_t sr;
417 
418 	REQUIRE(rdata->type == 249);
419 	REQUIRE(target != NULL);
420 	REQUIRE(rdata->length != 0);
421 
422 	tkey->common.rdclass = rdata->rdclass;
423 	tkey->common.rdtype = rdata->type;
424 	ISC_LINK_INIT(&tkey->common, link);
425 
426 	dns_rdata_toregion(rdata, &sr);
427 
428 	/*
429 	 * Algorithm Name.
430 	 */
431 	dns_name_init(&alg, NULL);
432 	dns_name_fromregion(&alg, &sr);
433 	dns_name_init(&tkey->algorithm, NULL);
434 	RETERR(name_duporclone(&alg, mctx, &tkey->algorithm));
435 	isc_region_consume(&sr, name_length(&tkey->algorithm));
436 
437 	/*
438 	 * Inception.
439 	 */
440 	tkey->inception = uint32_fromregion(&sr);
441 	isc_region_consume(&sr, 4);
442 
443 	/*
444 	 * Expire.
445 	 */
446 	tkey->expire = uint32_fromregion(&sr);
447 	isc_region_consume(&sr, 4);
448 
449 	/*
450 	 * Mode.
451 	 */
452 	tkey->mode = uint16_fromregion(&sr);
453 	isc_region_consume(&sr, 2);
454 
455 	/*
456 	 * Error.
457 	 */
458 	tkey->error = uint16_fromregion(&sr);
459 	isc_region_consume(&sr, 2);
460 
461 	/*
462 	 * Key size.
463 	 */
464 	tkey->keylen = uint16_fromregion(&sr);
465 	isc_region_consume(&sr, 2);
466 
467 	/*
468 	 * Key.
469 	 */
470 	INSIST(tkey->keylen + 2U <= sr.length);
471 	tkey->key = mem_maybedup(mctx, sr.base, tkey->keylen);
472 	if (tkey->key == NULL)
473 		goto cleanup;
474 	isc_region_consume(&sr, tkey->keylen);
475 
476 	/*
477 	 * Other size.
478 	 */
479 	tkey->otherlen = uint16_fromregion(&sr);
480 	isc_region_consume(&sr, 2);
481 
482 	/*
483 	 * Other.
484 	 */
485 	INSIST(tkey->otherlen <= sr.length);
486 	tkey->other = mem_maybedup(mctx, sr.base, tkey->otherlen);
487 	if (tkey->other == NULL)
488 		goto cleanup;
489 
490 	tkey->mctx = mctx;
491 	return (ISC_R_SUCCESS);
492 
493  cleanup:
494 	if (mctx != NULL)
495 		dns_name_free(&tkey->algorithm, mctx);
496 	if (mctx != NULL && tkey->key != NULL)
497 		isc_mem_free(mctx, tkey->key);
498 	return (ISC_R_NOMEMORY);
499 }
500 
501 static inline void
freestruct_tkey(ARGS_FREESTRUCT)502 freestruct_tkey(ARGS_FREESTRUCT) {
503 	dns_rdata_tkey_t *tkey = (dns_rdata_tkey_t *) source;
504 
505 	REQUIRE(source != NULL);
506 
507 	if (tkey->mctx == NULL)
508 		return;
509 
510 	dns_name_free(&tkey->algorithm, tkey->mctx);
511 	if (tkey->key != NULL)
512 		isc_mem_free(tkey->mctx, tkey->key);
513 	if (tkey->other != NULL)
514 		isc_mem_free(tkey->mctx, tkey->other);
515 	tkey->mctx = NULL;
516 }
517 
518 static inline isc_result_t
additionaldata_tkey(ARGS_ADDLDATA)519 additionaldata_tkey(ARGS_ADDLDATA) {
520 	UNUSED(rdata);
521 	UNUSED(add);
522 	UNUSED(arg);
523 
524 	REQUIRE(rdata->type == 249);
525 
526 	return (ISC_R_SUCCESS);
527 }
528 
529 static inline isc_result_t
digest_tkey(ARGS_DIGEST)530 digest_tkey(ARGS_DIGEST) {
531 	UNUSED(rdata);
532 	UNUSED(digest);
533 	UNUSED(arg);
534 
535 	REQUIRE(rdata->type == 249);
536 
537 	return (ISC_R_NOTIMPLEMENTED);
538 }
539 
540 static inline isc_boolean_t
checkowner_tkey(ARGS_CHECKOWNER)541 checkowner_tkey(ARGS_CHECKOWNER) {
542 
543 	REQUIRE(type == 249);
544 
545 	UNUSED(name);
546 	UNUSED(type);
547 	UNUSED(rdclass);
548 	UNUSED(wildcard);
549 
550 	return (ISC_TRUE);
551 }
552 
553 static inline isc_boolean_t
checknames_tkey(ARGS_CHECKNAMES)554 checknames_tkey(ARGS_CHECKNAMES) {
555 
556 	REQUIRE(rdata->type == 249);
557 
558 	UNUSED(rdata);
559 	UNUSED(owner);
560 	UNUSED(bad);
561 
562 	return (ISC_TRUE);
563 }
564 
565 static inline isc_result_t
casecompare_tkey(ARGS_COMPARE)566 casecompare_tkey(ARGS_COMPARE) {
567 	return (compare_tkey(rdata1, rdata2));
568 }
569 #endif	/* RDATA_GENERIC_TKEY_249_C */
570