]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/bind9/lib/dns/rdata/generic/tkey_249.c
Update BIND to 9.9.8
[FreeBSD/stable/9.git] / contrib / bind9 / lib / dns / rdata / generic / tkey_249.c
1 /*
2  * Copyright (C) 2004, 2007, 2009, 2011, 2012, 2014, 2015  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2003  Internet Software Consortium.
4  *
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.
8  *
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.
16  */
17
18 /* $Id$ */
19
20 /*
21  * Reviewed: Thu Mar 16 17:35:30 PST 2000 by halley.
22  */
23
24 /* draft-ietf-dnsext-tkey-01.txt */
25
26 #ifndef RDATA_GENERIC_TKEY_249_C
27 #define RDATA_GENERIC_TKEY_249_C
28
29 #define RRTYPE_TKEY_ATTRIBUTES (DNS_RDATATYPEATTR_META)
30
31 static inline isc_result_t
32 fromtext_tkey(ARGS_FROMTEXT) {
33         isc_token_t token;
34         dns_rcode_t rcode;
35         dns_name_t name;
36         isc_buffer_t buffer;
37         long i;
38         char *e;
39
40         REQUIRE(type == dns_rdatatype_tkey);
41
42         UNUSED(type);
43         UNUSED(rdclass);
44         UNUSED(callbacks);
45
46         /*
47          * Algorithm.
48          */
49         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
50                                       ISC_FALSE));
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));
55
56
57         /*
58          * Inception.
59          */
60         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
61                                       ISC_FALSE));
62         RETERR(uint32_tobuffer(token.value.as_ulong, target));
63
64         /*
65          * Expiration.
66          */
67         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
68                                       ISC_FALSE));
69         RETERR(uint32_tobuffer(token.value.as_ulong, target));
70
71         /*
72          * Mode.
73          */
74         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
75                                       ISC_FALSE));
76         if (token.value.as_ulong > 0xffffU)
77                 RETTOK(ISC_R_RANGE);
78         RETERR(uint16_tobuffer(token.value.as_ulong, target));
79
80         /*
81          * Error.
82          */
83         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
84                                       ISC_FALSE));
85         if (dns_tsigrcode_fromtext(&rcode, &token.value.as_textregion)
86                                 != ISC_R_SUCCESS)
87         {
88                 i = strtol(DNS_AS_STR(token), &e, 10);
89                 if (*e != 0)
90                         RETTOK(DNS_R_UNKNOWN);
91                 if (i < 0 || i > 0xffff)
92                         RETTOK(ISC_R_RANGE);
93                 rcode = (dns_rcode_t)i;
94         }
95         RETERR(uint16_tobuffer(rcode, target));
96
97         /*
98          * Key Size.
99          */
100         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
101                                       ISC_FALSE));
102         if (token.value.as_ulong > 0xffffU)
103                 RETTOK(ISC_R_RANGE);
104         RETERR(uint16_tobuffer(token.value.as_ulong, target));
105
106         /*
107          * Key Data.
108          */
109         RETERR(isc_base64_tobuffer(lexer, target, (int)token.value.as_ulong));
110
111         /*
112          * Other Size.
113          */
114         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
115                                       ISC_FALSE));
116         if (token.value.as_ulong > 0xffffU)
117                 RETTOK(ISC_R_RANGE);
118         RETERR(uint16_tobuffer(token.value.as_ulong, target));
119
120         /*
121          * Other Data.
122          */
123         return (isc_base64_tobuffer(lexer, target, (int)token.value.as_ulong));
124 }
125
126 static inline isc_result_t
127 totext_tkey(ARGS_TOTEXT) {
128         isc_region_t sr, dr;
129         char buf[sizeof("4294967295 ")];
130         unsigned long n;
131         dns_name_t name;
132         dns_name_t prefix;
133         isc_boolean_t sub;
134
135         REQUIRE(rdata->type == dns_rdatatype_tkey);
136         REQUIRE(rdata->length != 0);
137
138         dns_rdata_toregion(rdata, &sr);
139
140         /*
141          * Algorithm.
142          */
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));
150
151         /*
152          * Inception.
153          */
154         n = uint32_fromregion(&sr);
155         isc_region_consume(&sr, 4);
156         sprintf(buf, "%lu ", n);
157         RETERR(str_totext(buf, target));
158
159         /*
160          * Expiration.
161          */
162         n = uint32_fromregion(&sr);
163         isc_region_consume(&sr, 4);
164         sprintf(buf, "%lu ", n);
165         RETERR(str_totext(buf, target));
166
167         /*
168          * Mode.
169          */
170         n = uint16_fromregion(&sr);
171         isc_region_consume(&sr, 2);
172         sprintf(buf, "%lu ", n);
173         RETERR(str_totext(buf, target));
174
175         /*
176          * Error.
177          */
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));
182         else {
183                 sprintf(buf, "%lu ", n);
184                 RETERR(str_totext(buf, target));
185         }
186
187         /*
188          * Key Size.
189          */
190         n = uint16_fromregion(&sr);
191         isc_region_consume(&sr, 2);
192         sprintf(buf, "%lu", n);
193         RETERR(str_totext(buf, target));
194
195         /*
196          * Key Data.
197          */
198         REQUIRE(n <= sr.length);
199         dr = sr;
200         dr.length = n;
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));
206         else
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));
211         else
212                 RETERR(str_totext(" ", target));
213         isc_region_consume(&sr, n);
214
215         /*
216          * Other Size.
217          */
218         n = uint16_fromregion(&sr);
219         isc_region_consume(&sr, 2);
220         sprintf(buf, "%lu", n);
221         RETERR(str_totext(buf, target));
222
223         /*
224          * Other Data.
225          */
226         REQUIRE(n <= sr.length);
227         if (n != 0U) {
228             dr = sr;
229             dr.length = n;
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));
235                 else
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));
240         }
241         return (ISC_R_SUCCESS);
242 }
243
244 static inline isc_result_t
245 fromwire_tkey(ARGS_FROMWIRE) {
246         isc_region_t sr;
247         unsigned long n;
248         dns_name_t name;
249
250         REQUIRE(type == dns_rdatatype_tkey);
251
252         UNUSED(type);
253         UNUSED(rdclass);
254
255         dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
256
257         /*
258          * Algorithm.
259          */
260         dns_name_init(&name, NULL);
261         RETERR(dns_name_fromwire(&name, source, dctx, options, target));
262
263         /*
264          * Inception: 4
265          * Expiration: 4
266          * Mode: 2
267          * Error: 2
268          */
269         isc_buffer_activeregion(source, &sr);
270         if (sr.length < 12)
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);
275
276         /*
277          * Key Length + Key Data.
278          */
279         if (sr.length < 2)
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);
287
288         /*
289          * Other Length + Other Data.
290          */
291         if (sr.length < 2)
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));
298 }
299
300 static inline isc_result_t
301 towire_tkey(ARGS_TOWIRE) {
302         isc_region_t sr;
303         dns_name_t name;
304         dns_offsets_t offsets;
305
306         REQUIRE(rdata->type == dns_rdatatype_tkey);
307         REQUIRE(rdata->length != 0);
308
309         dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
310         /*
311          * Algorithm.
312          */
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));
318
319         return (mem_tobuffer(target, sr.base, sr.length));
320 }
321
322 static inline int
323 compare_tkey(ARGS_COMPARE) {
324         isc_region_t r1;
325         isc_region_t r2;
326         dns_name_t name1;
327         dns_name_t name2;
328         int order;
329
330         REQUIRE(rdata1->type == rdata2->type);
331         REQUIRE(rdata1->rdclass == rdata2->rdclass);
332         REQUIRE(rdata1->type == dns_rdatatype_tkey);
333         REQUIRE(rdata1->length != 0);
334         REQUIRE(rdata2->length != 0);
335
336         /*
337          * Algorithm.
338          */
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)
346                 return (order);
347         isc_region_consume(&r1, name_length(&name1));
348         isc_region_consume(&r2, name_length(&name2));
349         return (isc_region_compare(&r1, &r2));
350 }
351
352 static inline isc_result_t
353 fromstruct_tkey(ARGS_FROMSTRUCT) {
354         dns_rdata_tkey_t *tkey = source;
355
356         REQUIRE(type == dns_rdatatype_tkey);
357         REQUIRE(source != NULL);
358         REQUIRE(tkey->common.rdtype == type);
359         REQUIRE(tkey->common.rdclass == rdclass);
360
361         UNUSED(type);
362         UNUSED(rdclass);
363
364         /*
365          * Algorithm Name.
366          */
367         RETERR(name_tobuffer(&tkey->algorithm, target));
368
369         /*
370          * Inception: 32 bits.
371          */
372         RETERR(uint32_tobuffer(tkey->inception, target));
373
374         /*
375          * Expire: 32 bits.
376          */
377         RETERR(uint32_tobuffer(tkey->expire, target));
378
379         /*
380          * Mode: 16 bits.
381          */
382         RETERR(uint16_tobuffer(tkey->mode, target));
383
384         /*
385          * Error: 16 bits.
386          */
387         RETERR(uint16_tobuffer(tkey->error, target));
388
389         /*
390          * Key size: 16 bits.
391          */
392         RETERR(uint16_tobuffer(tkey->keylen, target));
393
394         /*
395          * Key.
396          */
397         RETERR(mem_tobuffer(target, tkey->key, tkey->keylen));
398
399         /*
400          * Other size: 16 bits.
401          */
402         RETERR(uint16_tobuffer(tkey->otherlen, target));
403
404         /*
405          * Other data.
406          */
407         return (mem_tobuffer(target, tkey->other, tkey->otherlen));
408 }
409
410 static inline isc_result_t
411 tostruct_tkey(ARGS_TOSTRUCT) {
412         dns_rdata_tkey_t *tkey = target;
413         dns_name_t alg;
414         isc_region_t sr;
415
416         REQUIRE(rdata->type == dns_rdatatype_tkey);
417         REQUIRE(target != NULL);
418         REQUIRE(rdata->length != 0);
419
420         tkey->common.rdclass = rdata->rdclass;
421         tkey->common.rdtype = rdata->type;
422         ISC_LINK_INIT(&tkey->common, link);
423
424         dns_rdata_toregion(rdata, &sr);
425
426         /*
427          * Algorithm Name.
428          */
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));
434
435         /*
436          * Inception.
437          */
438         tkey->inception = uint32_fromregion(&sr);
439         isc_region_consume(&sr, 4);
440
441         /*
442          * Expire.
443          */
444         tkey->expire = uint32_fromregion(&sr);
445         isc_region_consume(&sr, 4);
446
447         /*
448          * Mode.
449          */
450         tkey->mode = uint16_fromregion(&sr);
451         isc_region_consume(&sr, 2);
452
453         /*
454          * Error.
455          */
456         tkey->error = uint16_fromregion(&sr);
457         isc_region_consume(&sr, 2);
458
459         /*
460          * Key size.
461          */
462         tkey->keylen = uint16_fromregion(&sr);
463         isc_region_consume(&sr, 2);
464
465         /*
466          * Key.
467          */
468         INSIST(tkey->keylen + 2U <= sr.length);
469         tkey->key = mem_maybedup(mctx, sr.base, tkey->keylen);
470         if (tkey->key == NULL)
471                 goto cleanup;
472         isc_region_consume(&sr, tkey->keylen);
473
474         /*
475          * Other size.
476          */
477         tkey->otherlen = uint16_fromregion(&sr);
478         isc_region_consume(&sr, 2);
479
480         /*
481          * Other.
482          */
483         INSIST(tkey->otherlen <= sr.length);
484         tkey->other = mem_maybedup(mctx, sr.base, tkey->otherlen);
485         if (tkey->other == NULL)
486                 goto cleanup;
487
488         tkey->mctx = mctx;
489         return (ISC_R_SUCCESS);
490
491  cleanup:
492         if (mctx != NULL)
493                 dns_name_free(&tkey->algorithm, mctx);
494         if (mctx != NULL && tkey->key != NULL)
495                 isc_mem_free(mctx, tkey->key);
496         return (ISC_R_NOMEMORY);
497 }
498
499 static inline void
500 freestruct_tkey(ARGS_FREESTRUCT) {
501         dns_rdata_tkey_t *tkey = (dns_rdata_tkey_t *) source;
502
503         REQUIRE(source != NULL);
504
505         if (tkey->mctx == NULL)
506                 return;
507
508         dns_name_free(&tkey->algorithm, tkey->mctx);
509         if (tkey->key != NULL)
510                 isc_mem_free(tkey->mctx, tkey->key);
511         if (tkey->other != NULL)
512                 isc_mem_free(tkey->mctx, tkey->other);
513         tkey->mctx = NULL;
514 }
515
516 static inline isc_result_t
517 additionaldata_tkey(ARGS_ADDLDATA) {
518         UNUSED(rdata);
519         UNUSED(add);
520         UNUSED(arg);
521
522         REQUIRE(rdata->type == dns_rdatatype_tkey);
523
524         return (ISC_R_SUCCESS);
525 }
526
527 static inline isc_result_t
528 digest_tkey(ARGS_DIGEST) {
529         UNUSED(rdata);
530         UNUSED(digest);
531         UNUSED(arg);
532
533         REQUIRE(rdata->type == dns_rdatatype_tkey);
534
535         return (ISC_R_NOTIMPLEMENTED);
536 }
537
538 static inline isc_boolean_t
539 checkowner_tkey(ARGS_CHECKOWNER) {
540
541         REQUIRE(type == dns_rdatatype_tkey);
542
543         UNUSED(name);
544         UNUSED(type);
545         UNUSED(rdclass);
546         UNUSED(wildcard);
547
548         return (ISC_TRUE);
549 }
550
551 static inline isc_boolean_t
552 checknames_tkey(ARGS_CHECKNAMES) {
553
554         REQUIRE(rdata->type == dns_rdatatype_tkey);
555
556         UNUSED(rdata);
557         UNUSED(owner);
558         UNUSED(bad);
559
560         return (ISC_TRUE);
561 }
562
563 static inline isc_result_t
564 casecompare_tkey(ARGS_COMPARE) {
565         return (compare_tkey(rdata1, rdata2));
566 }
567 #endif  /* RDATA_GENERIC_TKEY_249_C */