Import bind 9.5.2 vendor sources.
[dragonfly.git] / contrib / bind-9.5.2 / lib / dns / rdata / any_255 / tsig_250.c
blob3121f78238430faa7ee38b5810c1aa2dc99e5b82
1 /*
2 * Copyright (C) 2004, 2005, 2007 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.
18 /* $Id: tsig_250.c,v 1.63 2007/06/19 23:47:17 tbox Exp $ */
20 /* Reviewed: Thu Mar 16 13:39:43 PST 2000 by gson */
22 #ifndef RDATA_ANY_255_TSIG_250_C
23 #define RDATA_ANY_255_TSIG_250_C
25 #define RRTYPE_TSIG_ATTRIBUTES \
26 (DNS_RDATATYPEATTR_META | DNS_RDATATYPEATTR_NOTQUESTION)
28 static inline isc_result_t
29 fromtext_any_tsig(ARGS_FROMTEXT) {
30 isc_token_t token;
31 dns_name_t name;
32 isc_uint64_t sigtime;
33 isc_buffer_t buffer;
34 dns_rcode_t rcode;
35 long i;
36 char *e;
38 REQUIRE(type == 250);
39 REQUIRE(rdclass == 255);
41 UNUSED(type);
42 UNUSED(rdclass);
43 UNUSED(callbacks);
46 * Algorithm Name.
48 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
49 ISC_FALSE));
50 dns_name_init(&name, NULL);
51 buffer_fromregion(&buffer, &token.value.as_region);
52 origin = (origin != NULL) ? origin : dns_rootname;
53 RETTOK(dns_name_fromtext(&name, &buffer, origin, options, target));
56 * Time Signed: 48 bits.
58 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
59 ISC_FALSE));
60 sigtime = isc_string_touint64(DNS_AS_STR(token), &e, 10);
61 if (*e != 0)
62 RETTOK(DNS_R_SYNTAX);
63 if ((sigtime >> 48) != 0)
64 RETTOK(ISC_R_RANGE);
65 RETERR(uint16_tobuffer((isc_uint16_t)(sigtime >> 32), target));
66 RETERR(uint32_tobuffer((isc_uint32_t)(sigtime & 0xffffffffU), target));
69 * Fudge.
71 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
72 ISC_FALSE));
73 if (token.value.as_ulong > 0xffffU)
74 RETTOK(ISC_R_RANGE);
75 RETERR(uint16_tobuffer(token.value.as_ulong, target));
78 * Signature Size.
80 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
81 ISC_FALSE));
82 if (token.value.as_ulong > 0xffffU)
83 RETTOK(ISC_R_RANGE);
84 RETERR(uint16_tobuffer(token.value.as_ulong, target));
87 * Signature.
89 RETERR(isc_base64_tobuffer(lexer, target, (int)token.value.as_ulong));
92 * Original ID.
94 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
95 ISC_FALSE));
96 if (token.value.as_ulong > 0xffffU)
97 RETTOK(ISC_R_RANGE);
98 RETERR(uint16_tobuffer(token.value.as_ulong, target));
101 * Error.
103 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
104 ISC_FALSE));
105 if (dns_tsigrcode_fromtext(&rcode, &token.value.as_textregion)
106 != ISC_R_SUCCESS)
108 i = strtol(DNS_AS_STR(token), &e, 10);
109 if (*e != 0)
110 RETTOK(DNS_R_UNKNOWN);
111 if (i < 0 || i > 0xffff)
112 RETTOK(ISC_R_RANGE);
113 rcode = (dns_rcode_t)i;
115 RETERR(uint16_tobuffer(rcode, target));
118 * Other Len.
120 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
121 ISC_FALSE));
122 if (token.value.as_ulong > 0xffffU)
123 RETTOK(ISC_R_RANGE);
124 RETERR(uint16_tobuffer(token.value.as_ulong, target));
127 * Other Data.
129 return (isc_base64_tobuffer(lexer, target, (int)token.value.as_ulong));
132 static inline isc_result_t
133 totext_any_tsig(ARGS_TOTEXT) {
134 isc_region_t sr;
135 isc_region_t sigr;
136 char buf[sizeof("281474976710655 ")];
137 char *bufp;
138 dns_name_t name;
139 dns_name_t prefix;
140 isc_boolean_t sub;
141 isc_uint64_t sigtime;
142 unsigned short n;
144 REQUIRE(rdata->type == 250);
145 REQUIRE(rdata->rdclass == 255);
146 REQUIRE(rdata->length != 0);
148 dns_rdata_toregion(rdata, &sr);
150 * Algorithm Name.
152 dns_name_init(&name, NULL);
153 dns_name_init(&prefix, NULL);
154 dns_name_fromregion(&name, &sr);
155 sub = name_prefix(&name, tctx->origin, &prefix);
156 RETERR(dns_name_totext(&prefix, sub, target));
157 RETERR(str_totext(" ", target));
158 isc_region_consume(&sr, name_length(&name));
161 * Time Signed.
163 sigtime = ((isc_uint64_t)sr.base[0] << 40) |
164 ((isc_uint64_t)sr.base[1] << 32) |
165 ((isc_uint64_t)sr.base[2] << 24) |
166 ((isc_uint64_t)sr.base[3] << 16) |
167 ((isc_uint64_t)sr.base[4] << 8) |
168 (isc_uint64_t)sr.base[5];
169 isc_region_consume(&sr, 6);
170 bufp = &buf[sizeof(buf) - 1];
171 *bufp-- = 0;
172 *bufp-- = ' ';
173 do {
174 *bufp-- = decdigits[sigtime % 10];
175 sigtime /= 10;
176 } while (sigtime != 0);
177 bufp++;
178 RETERR(str_totext(bufp, target));
181 * Fudge.
183 n = uint16_fromregion(&sr);
184 isc_region_consume(&sr, 2);
185 sprintf(buf, "%u ", n);
186 RETERR(str_totext(buf, target));
189 * Signature Size.
191 n = uint16_fromregion(&sr);
192 isc_region_consume(&sr, 2);
193 sprintf(buf, "%u", n);
194 RETERR(str_totext(buf, target));
197 * Signature.
199 REQUIRE(n <= sr.length);
200 sigr = sr;
201 sigr.length = n;
202 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
203 RETERR(str_totext(" (", target));
204 RETERR(str_totext(tctx->linebreak, target));
205 RETERR(isc_base64_totext(&sigr, tctx->width - 2,
206 tctx->linebreak, target));
207 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
208 RETERR(str_totext(" ) ", target));
209 else
210 RETERR(str_totext(" ", target));
211 isc_region_consume(&sr, n);
214 * Original ID.
216 n = uint16_fromregion(&sr);
217 isc_region_consume(&sr, 2);
218 sprintf(buf, "%u ", n);
219 RETERR(str_totext(buf, target));
222 * Error.
224 n = uint16_fromregion(&sr);
225 isc_region_consume(&sr, 2);
226 if (dns_tsigrcode_totext((dns_rcode_t)n, target) == ISC_R_SUCCESS)
227 RETERR(str_totext(" ", target));
228 else {
229 sprintf(buf, "%u ", n);
230 RETERR(str_totext(buf, target));
234 * Other Size.
236 n = uint16_fromregion(&sr);
237 isc_region_consume(&sr, 2);
238 sprintf(buf, "%u ", n);
239 RETERR(str_totext(buf, target));
242 * Other.
244 return (isc_base64_totext(&sr, 60, " ", target));
247 static inline isc_result_t
248 fromwire_any_tsig(ARGS_FROMWIRE) {
249 isc_region_t sr;
250 dns_name_t name;
251 unsigned long n;
253 REQUIRE(type == 250);
254 REQUIRE(rdclass == 255);
256 UNUSED(type);
257 UNUSED(rdclass);
259 dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
262 * Algorithm Name.
264 dns_name_init(&name, NULL);
265 RETERR(dns_name_fromwire(&name, source, dctx, options, target));
267 isc_buffer_activeregion(source, &sr);
269 * Time Signed + Fudge.
271 if (sr.length < 8)
272 return (ISC_R_UNEXPECTEDEND);
273 RETERR(mem_tobuffer(target, sr.base, 8));
274 isc_region_consume(&sr, 8);
275 isc_buffer_forward(source, 8);
278 * Signature Length + Signature.
280 if (sr.length < 2)
281 return (ISC_R_UNEXPECTEDEND);
282 n = uint16_fromregion(&sr);
283 if (sr.length < n + 2)
284 return (ISC_R_UNEXPECTEDEND);
285 RETERR(mem_tobuffer(target, sr.base, n + 2));
286 isc_region_consume(&sr, n + 2);
287 isc_buffer_forward(source, n + 2);
290 * Original ID + Error.
292 if (sr.length < 4)
293 return (ISC_R_UNEXPECTEDEND);
294 RETERR(mem_tobuffer(target, sr.base, 4));
295 isc_region_consume(&sr, 4);
296 isc_buffer_forward(source, 4);
299 * Other Length + Other.
301 if (sr.length < 2)
302 return (ISC_R_UNEXPECTEDEND);
303 n = uint16_fromregion(&sr);
304 if (sr.length < n + 2)
305 return (ISC_R_UNEXPECTEDEND);
306 isc_buffer_forward(source, n + 2);
307 return (mem_tobuffer(target, sr.base, n + 2));
310 static inline isc_result_t
311 towire_any_tsig(ARGS_TOWIRE) {
312 isc_region_t sr;
313 dns_name_t name;
314 dns_offsets_t offsets;
316 REQUIRE(rdata->type == 250);
317 REQUIRE(rdata->rdclass == 255);
318 REQUIRE(rdata->length != 0);
320 dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
321 dns_rdata_toregion(rdata, &sr);
322 dns_name_init(&name, offsets);
323 dns_name_fromregion(&name, &sr);
324 RETERR(dns_name_towire(&name, cctx, target));
325 isc_region_consume(&sr, name_length(&name));
326 return (mem_tobuffer(target, sr.base, sr.length));
329 static inline int
330 compare_any_tsig(ARGS_COMPARE) {
331 isc_region_t r1;
332 isc_region_t r2;
333 dns_name_t name1;
334 dns_name_t name2;
335 int order;
337 REQUIRE(rdata1->type == rdata2->type);
338 REQUIRE(rdata1->rdclass == rdata2->rdclass);
339 REQUIRE(rdata1->type == 250);
340 REQUIRE(rdata1->rdclass == 255);
341 REQUIRE(rdata1->length != 0);
342 REQUIRE(rdata2->length != 0);
344 dns_rdata_toregion(rdata1, &r1);
345 dns_rdata_toregion(rdata2, &r2);
346 dns_name_init(&name1, NULL);
347 dns_name_init(&name2, NULL);
348 dns_name_fromregion(&name1, &r1);
349 dns_name_fromregion(&name2, &r2);
350 order = dns_name_rdatacompare(&name1, &name2);
351 if (order != 0)
352 return (order);
353 isc_region_consume(&r1, name_length(&name1));
354 isc_region_consume(&r2, name_length(&name2));
355 return (isc_region_compare(&r1, &r2));
358 static inline isc_result_t
359 fromstruct_any_tsig(ARGS_FROMSTRUCT) {
360 dns_rdata_any_tsig_t *tsig = source;
361 isc_region_t tr;
363 REQUIRE(type == 250);
364 REQUIRE(rdclass == 255);
365 REQUIRE(source != NULL);
366 REQUIRE(tsig->common.rdclass == rdclass);
367 REQUIRE(tsig->common.rdtype == type);
369 UNUSED(type);
370 UNUSED(rdclass);
373 * Algorithm Name.
375 RETERR(name_tobuffer(&tsig->algorithm, target));
377 isc_buffer_availableregion(target, &tr);
378 if (tr.length < 6 + 2 + 2)
379 return (ISC_R_NOSPACE);
382 * Time Signed: 48 bits.
384 RETERR(uint16_tobuffer((isc_uint16_t)(tsig->timesigned >> 32),
385 target));
386 RETERR(uint32_tobuffer((isc_uint32_t)(tsig->timesigned & 0xffffffffU),
387 target));
390 * Fudge.
392 RETERR(uint16_tobuffer(tsig->fudge, target));
395 * Signature Size.
397 RETERR(uint16_tobuffer(tsig->siglen, target));
400 * Signature.
402 RETERR(mem_tobuffer(target, tsig->signature, tsig->siglen));
404 isc_buffer_availableregion(target, &tr);
405 if (tr.length < 2 + 2 + 2)
406 return (ISC_R_NOSPACE);
409 * Original ID.
411 RETERR(uint16_tobuffer(tsig->originalid, target));
414 * Error.
416 RETERR(uint16_tobuffer(tsig->error, target));
419 * Other Len.
421 RETERR(uint16_tobuffer(tsig->otherlen, target));
424 * Other Data.
426 return (mem_tobuffer(target, tsig->other, tsig->otherlen));
429 static inline isc_result_t
430 tostruct_any_tsig(ARGS_TOSTRUCT) {
431 dns_rdata_any_tsig_t *tsig;
432 dns_name_t alg;
433 isc_region_t sr;
435 REQUIRE(rdata->type == 250);
436 REQUIRE(rdata->rdclass == 255);
437 REQUIRE(rdata->length != 0);
439 tsig = (dns_rdata_any_tsig_t *) target;
440 tsig->common.rdclass = rdata->rdclass;
441 tsig->common.rdtype = rdata->type;
442 ISC_LINK_INIT(&tsig->common, link);
444 dns_rdata_toregion(rdata, &sr);
447 * Algorithm Name.
449 dns_name_init(&alg, NULL);
450 dns_name_fromregion(&alg, &sr);
451 dns_name_init(&tsig->algorithm, NULL);
452 RETERR(name_duporclone(&alg, mctx, &tsig->algorithm));
454 isc_region_consume(&sr, name_length(&tsig->algorithm));
457 * Time Signed.
459 INSIST(sr.length >= 6);
460 tsig->timesigned = ((isc_uint64_t)sr.base[0] << 40) |
461 ((isc_uint64_t)sr.base[1] << 32) |
462 ((isc_uint64_t)sr.base[2] << 24) |
463 ((isc_uint64_t)sr.base[3] << 16) |
464 ((isc_uint64_t)sr.base[4] << 8) |
465 (isc_uint64_t)sr.base[5];
466 isc_region_consume(&sr, 6);
469 * Fudge.
471 tsig->fudge = uint16_fromregion(&sr);
472 isc_region_consume(&sr, 2);
475 * Signature Size.
477 tsig->siglen = uint16_fromregion(&sr);
478 isc_region_consume(&sr, 2);
481 * Signature.
483 INSIST(sr.length >= tsig->siglen);
484 tsig->signature = mem_maybedup(mctx, sr.base, tsig->siglen);
485 if (tsig->signature == NULL)
486 goto cleanup;
487 isc_region_consume(&sr, tsig->siglen);
490 * Original ID.
492 tsig->originalid = uint16_fromregion(&sr);
493 isc_region_consume(&sr, 2);
496 * Error.
498 tsig->error = uint16_fromregion(&sr);
499 isc_region_consume(&sr, 2);
502 * Other Size.
504 tsig->otherlen = uint16_fromregion(&sr);
505 isc_region_consume(&sr, 2);
508 * Other.
510 INSIST(sr.length == tsig->otherlen);
511 tsig->other = mem_maybedup(mctx, sr.base, tsig->otherlen);
512 if (tsig->other == NULL)
513 goto cleanup;
515 tsig->mctx = mctx;
516 return (ISC_R_SUCCESS);
518 cleanup:
519 if (mctx != NULL)
520 dns_name_free(&tsig->algorithm, tsig->mctx);
521 if (mctx != NULL && tsig->signature != NULL)
522 isc_mem_free(mctx, tsig->signature);
523 return (ISC_R_NOMEMORY);
526 static inline void
527 freestruct_any_tsig(ARGS_FREESTRUCT) {
528 dns_rdata_any_tsig_t *tsig = (dns_rdata_any_tsig_t *) source;
530 REQUIRE(source != NULL);
531 REQUIRE(tsig->common.rdclass == 255);
532 REQUIRE(tsig->common.rdtype == 250);
534 if (tsig->mctx == NULL)
535 return;
537 dns_name_free(&tsig->algorithm, tsig->mctx);
538 if (tsig->signature != NULL)
539 isc_mem_free(tsig->mctx, tsig->signature);
540 if (tsig->other != NULL)
541 isc_mem_free(tsig->mctx, tsig->other);
542 tsig->mctx = NULL;
545 static inline isc_result_t
546 additionaldata_any_tsig(ARGS_ADDLDATA) {
547 REQUIRE(rdata->type == 250);
548 REQUIRE(rdata->rdclass == 255);
550 UNUSED(rdata);
551 UNUSED(add);
552 UNUSED(arg);
554 return (ISC_R_SUCCESS);
557 static inline isc_result_t
558 digest_any_tsig(ARGS_DIGEST) {
560 REQUIRE(rdata->type == 250);
561 REQUIRE(rdata->rdclass == 255);
563 UNUSED(rdata);
564 UNUSED(digest);
565 UNUSED(arg);
567 return (ISC_R_NOTIMPLEMENTED);
570 static inline isc_boolean_t
571 checkowner_any_tsig(ARGS_CHECKOWNER) {
573 REQUIRE(type == 250);
574 REQUIRE(rdclass == 255);
576 UNUSED(name);
577 UNUSED(type);
578 UNUSED(rdclass);
579 UNUSED(wildcard);
581 return (ISC_TRUE);
584 static inline isc_boolean_t
585 checknames_any_tsig(ARGS_CHECKNAMES) {
587 REQUIRE(rdata->type == 250);
588 REQUIRE(rdata->rdclass == 250);
590 UNUSED(rdata);
591 UNUSED(owner);
592 UNUSED(bad);
594 return (ISC_TRUE);
597 #endif /* RDATA_ANY_255_TSIG_250_C */