]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/bind9/lib/dns/rdata/generic/rrsig_46.c
Update BIND to 9.9.7.
[FreeBSD/stable/9.git] / contrib / bind9 / lib / dns / rdata / generic / rrsig_46.c
1 /*
2  * Copyright (C) 2004, 2005, 2007, 2009, 2011, 2012, 2014, 2015  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 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 /* Reviewed: Fri Mar 17 09:05:02 PST 2000 by gson */
21
22 /* RFC2535 */
23
24 #ifndef RDATA_GENERIC_RRSIG_46_C
25 #define RDATA_GENERIC_RRSIG_46_C
26
27 #define RRTYPE_RRSIG_ATTRIBUTES (DNS_RDATATYPEATTR_DNSSEC)
28
29 static inline isc_result_t
30 fromtext_rrsig(ARGS_FROMTEXT) {
31         isc_token_t token;
32         unsigned char c;
33         long i;
34         dns_rdatatype_t covered;
35         char *e;
36         isc_result_t result;
37         dns_name_t name;
38         isc_buffer_t buffer;
39         isc_uint32_t time_signed, time_expire;
40
41         REQUIRE(type == 46);
42
43         UNUSED(type);
44         UNUSED(rdclass);
45         UNUSED(callbacks);
46
47         /*
48          * Type covered.
49          */
50         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
51                                       ISC_FALSE));
52         result = dns_rdatatype_fromtext(&covered, &token.value.as_textregion);
53         if (result != ISC_R_SUCCESS && result != ISC_R_NOTIMPLEMENTED) {
54                 i = strtol(DNS_AS_STR(token), &e, 10);
55                 if (i < 0 || i > 65535)
56                         RETTOK(ISC_R_RANGE);
57                 if (*e != 0)
58                         RETTOK(result);
59                 covered = (dns_rdatatype_t)i;
60         }
61         RETERR(uint16_tobuffer(covered, target));
62
63         /*
64          * Algorithm.
65          */
66         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
67                                       ISC_FALSE));
68         RETTOK(dns_secalg_fromtext(&c, &token.value.as_textregion));
69         RETERR(mem_tobuffer(target, &c, 1));
70
71         /*
72          * Labels.
73          */
74         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
75                                       ISC_FALSE));
76         if (token.value.as_ulong > 0xffU)
77                 RETTOK(ISC_R_RANGE);
78         c = (unsigned char)token.value.as_ulong;
79         RETERR(mem_tobuffer(target, &c, 1));
80
81         /*
82          * Original ttl.
83          */
84         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
85                                       ISC_FALSE));
86         RETERR(uint32_tobuffer(token.value.as_ulong, target));
87
88         /*
89          * Signature expiration.
90          */
91         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
92                                       ISC_FALSE));
93         if (strlen(DNS_AS_STR(token)) <= 10U &&
94             *DNS_AS_STR(token) != '-' && *DNS_AS_STR(token) != '+') {
95                 char *end;
96                 unsigned long u;
97                 isc_uint64_t u64;
98
99                 u64 = u = strtoul(DNS_AS_STR(token), &end, 10);
100                 if (u == ULONG_MAX || *end != 0)
101                         RETTOK(DNS_R_SYNTAX);
102                 if (u64 > 0xffffffffUL)
103                         RETTOK(ISC_R_RANGE);
104                 time_expire = u;
105         } else
106                 RETTOK(dns_time32_fromtext(DNS_AS_STR(token), &time_expire));
107         RETERR(uint32_tobuffer(time_expire, target));
108
109         /*
110          * Time signed.
111          */
112         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
113                                       ISC_FALSE));
114         if (strlen(DNS_AS_STR(token)) <= 10U &&
115             *DNS_AS_STR(token) != '-' && *DNS_AS_STR(token) != '+') {
116                 char *end;
117                 unsigned long u;
118                 isc_uint64_t u64;
119
120                 u64 = u = strtoul(DNS_AS_STR(token), &end, 10);
121                 if (u == ULONG_MAX || *end != 0)
122                         RETTOK(DNS_R_SYNTAX);
123                 if (u64 > 0xffffffffUL)
124                         RETTOK(ISC_R_RANGE);
125                 time_signed = u;
126         } else
127                 RETTOK(dns_time32_fromtext(DNS_AS_STR(token), &time_signed));
128         RETERR(uint32_tobuffer(time_signed, target));
129
130         /*
131          * Key footprint.
132          */
133         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
134                                       ISC_FALSE));
135         RETERR(uint16_tobuffer(token.value.as_ulong, target));
136
137         /*
138          * Signer.
139          */
140         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
141                                       ISC_FALSE));
142         dns_name_init(&name, NULL);
143         buffer_fromregion(&buffer, &token.value.as_region);
144         origin = (origin != NULL) ? origin : dns_rootname;
145         RETTOK(dns_name_fromtext(&name, &buffer, origin, options, target));
146
147         /*
148          * Sig.
149          */
150         return (isc_base64_tobuffer(lexer, target, -1));
151 }
152
153 static inline isc_result_t
154 totext_rrsig(ARGS_TOTEXT) {
155         isc_region_t sr;
156         char buf[sizeof("4294967295")];
157         dns_rdatatype_t covered;
158         unsigned long ttl;
159         unsigned long when;
160         unsigned long exp;
161         unsigned long foot;
162         dns_name_t name;
163
164         REQUIRE(rdata->type == 46);
165         REQUIRE(rdata->length != 0);
166
167         dns_rdata_toregion(rdata, &sr);
168
169         /*
170          * Type covered.
171          */
172         covered = uint16_fromregion(&sr);
173         isc_region_consume(&sr, 2);
174         /*
175          * XXXAG We should have something like dns_rdatatype_isknown()
176          * that does the right thing with type 0.
177          */
178         if (dns_rdatatype_isknown(covered) && covered != 0) {
179                 RETERR(dns_rdatatype_totext(covered, target));
180         } else {
181                 sprintf(buf, "TYPE%u", covered);
182                 RETERR(str_totext(buf, target));
183         }
184         RETERR(str_totext(" ", target));
185
186         /*
187          * Algorithm.
188          */
189         sprintf(buf, "%u", sr.base[0]);
190         isc_region_consume(&sr, 1);
191         RETERR(str_totext(buf, target));
192         RETERR(str_totext(" ", target));
193
194         /*
195          * Labels.
196          */
197         sprintf(buf, "%u", sr.base[0]);
198         isc_region_consume(&sr, 1);
199         RETERR(str_totext(buf, target));
200         RETERR(str_totext(" ", target));
201
202         /*
203          * Ttl.
204          */
205         ttl = uint32_fromregion(&sr);
206         isc_region_consume(&sr, 4);
207         sprintf(buf, "%lu", ttl);
208         RETERR(str_totext(buf, target));
209
210         if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
211                 RETERR(str_totext(" (", target));
212         RETERR(str_totext(tctx->linebreak, target));
213
214         /*
215          * Sig exp.
216          */
217         exp = uint32_fromregion(&sr);
218         isc_region_consume(&sr, 4);
219         RETERR(dns_time32_totext(exp, target));
220         RETERR(str_totext(" ", target));
221
222         /*
223          * Time signed.
224          */
225         when = uint32_fromregion(&sr);
226         isc_region_consume(&sr, 4);
227         RETERR(dns_time32_totext(when, target));
228         RETERR(str_totext(" ", target));
229
230         /*
231          * Footprint.
232          */
233         foot = uint16_fromregion(&sr);
234         isc_region_consume(&sr, 2);
235         sprintf(buf, "%lu", foot);
236         RETERR(str_totext(buf, target));
237         RETERR(str_totext(" ", target));
238
239         /*
240          * Signer.
241          */
242         dns_name_init(&name, NULL);
243         dns_name_fromregion(&name, &sr);
244         isc_region_consume(&sr, name_length(&name));
245         RETERR(dns_name_totext(&name, ISC_FALSE, target));
246
247         /*
248          * Sig.
249          */
250         RETERR(str_totext(tctx->linebreak, target));
251         if (tctx->width == 0)   /* No splitting */
252                 RETERR(isc_base64_totext(&sr, 60, "", target));
253         else
254                 RETERR(isc_base64_totext(&sr, tctx->width - 2,
255                                          tctx->linebreak, target));
256         if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
257                 RETERR(str_totext(" )", target));
258
259         return (ISC_R_SUCCESS);
260 }
261
262 static inline isc_result_t
263 fromwire_rrsig(ARGS_FROMWIRE) {
264         isc_region_t sr;
265         dns_name_t name;
266
267         REQUIRE(type == 46);
268
269         UNUSED(type);
270         UNUSED(rdclass);
271
272         dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
273
274         isc_buffer_activeregion(source, &sr);
275         /*
276          * type covered: 2
277          * algorithm: 1
278          * labels: 1
279          * original ttl: 4
280          * signature expiration: 4
281          * time signed: 4
282          * key footprint: 2
283          */
284         if (sr.length < 18)
285                 return (ISC_R_UNEXPECTEDEND);
286
287         isc_buffer_forward(source, 18);
288         RETERR(mem_tobuffer(target, sr.base, 18));
289
290         /*
291          * Signer.
292          */
293         dns_name_init(&name, NULL);
294         RETERR(dns_name_fromwire(&name, source, dctx, options, target));
295
296         /*
297          * Sig.
298          */
299         isc_buffer_activeregion(source, &sr);
300         isc_buffer_forward(source, sr.length);
301         return (mem_tobuffer(target, sr.base, sr.length));
302 }
303
304 static inline isc_result_t
305 towire_rrsig(ARGS_TOWIRE) {
306         isc_region_t sr;
307         dns_name_t name;
308         dns_offsets_t offsets;
309
310         REQUIRE(rdata->type == 46);
311         REQUIRE(rdata->length != 0);
312
313         dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
314         dns_rdata_toregion(rdata, &sr);
315         /*
316          * type covered: 2
317          * algorithm: 1
318          * labels: 1
319          * original ttl: 4
320          * signature expiration: 4
321          * time signed: 4
322          * key footprint: 2
323          */
324         RETERR(mem_tobuffer(target, sr.base, 18));
325         isc_region_consume(&sr, 18);
326
327         /*
328          * Signer.
329          */
330         dns_name_init(&name, offsets);
331         dns_name_fromregion(&name, &sr);
332         isc_region_consume(&sr, name_length(&name));
333         RETERR(dns_name_towire(&name, cctx, target));
334
335         /*
336          * Signature.
337          */
338         return (mem_tobuffer(target, sr.base, sr.length));
339 }
340
341 static inline int
342 compare_rrsig(ARGS_COMPARE) {
343         isc_region_t r1;
344         isc_region_t r2;
345
346         REQUIRE(rdata1->type == rdata2->type);
347         REQUIRE(rdata1->rdclass == rdata2->rdclass);
348         REQUIRE(rdata1->type == 46);
349         REQUIRE(rdata1->length != 0);
350         REQUIRE(rdata2->length != 0);
351
352         dns_rdata_toregion(rdata1, &r1);
353         dns_rdata_toregion(rdata2, &r2);
354         return (isc_region_compare(&r1, &r2));
355 }
356
357 static inline isc_result_t
358 fromstruct_rrsig(ARGS_FROMSTRUCT) {
359         dns_rdata_rrsig_t *sig = source;
360
361         REQUIRE(type == 46);
362         REQUIRE(source != NULL);
363         REQUIRE(sig->common.rdtype == type);
364         REQUIRE(sig->common.rdclass == rdclass);
365         REQUIRE(sig->signature != NULL || sig->siglen == 0);
366
367         UNUSED(type);
368         UNUSED(rdclass);
369
370         /*
371          * Type covered.
372          */
373         RETERR(uint16_tobuffer(sig->covered, target));
374
375         /*
376          * Algorithm.
377          */
378         RETERR(uint8_tobuffer(sig->algorithm, target));
379
380         /*
381          * Labels.
382          */
383         RETERR(uint8_tobuffer(sig->labels, target));
384
385         /*
386          * Original TTL.
387          */
388         RETERR(uint32_tobuffer(sig->originalttl, target));
389
390         /*
391          * Expire time.
392          */
393         RETERR(uint32_tobuffer(sig->timeexpire, target));
394
395         /*
396          * Time signed.
397          */
398         RETERR(uint32_tobuffer(sig->timesigned, target));
399
400         /*
401          * Key ID.
402          */
403         RETERR(uint16_tobuffer(sig->keyid, target));
404
405         /*
406          * Signer name.
407          */
408         RETERR(name_tobuffer(&sig->signer, target));
409
410         /*
411          * Signature.
412          */
413         return (mem_tobuffer(target, sig->signature, sig->siglen));
414 }
415
416 static inline isc_result_t
417 tostruct_rrsig(ARGS_TOSTRUCT) {
418         isc_region_t sr;
419         dns_rdata_rrsig_t *sig = target;
420         dns_name_t signer;
421
422         REQUIRE(rdata->type == 46);
423         REQUIRE(target != NULL);
424         REQUIRE(rdata->length != 0);
425
426         sig->common.rdclass = rdata->rdclass;
427         sig->common.rdtype = rdata->type;
428         ISC_LINK_INIT(&sig->common, link);
429
430         dns_rdata_toregion(rdata, &sr);
431
432         /*
433          * Type covered.
434          */
435         sig->covered = uint16_fromregion(&sr);
436         isc_region_consume(&sr, 2);
437
438         /*
439          * Algorithm.
440          */
441         sig->algorithm = uint8_fromregion(&sr);
442         isc_region_consume(&sr, 1);
443
444         /*
445          * Labels.
446          */
447         sig->labels = uint8_fromregion(&sr);
448         isc_region_consume(&sr, 1);
449
450         /*
451          * Original TTL.
452          */
453         sig->originalttl = uint32_fromregion(&sr);
454         isc_region_consume(&sr, 4);
455
456         /*
457          * Expire time.
458          */
459         sig->timeexpire = uint32_fromregion(&sr);
460         isc_region_consume(&sr, 4);
461
462         /*
463          * Time signed.
464          */
465         sig->timesigned = uint32_fromregion(&sr);
466         isc_region_consume(&sr, 4);
467
468         /*
469          * Key ID.
470          */
471         sig->keyid = uint16_fromregion(&sr);
472         isc_region_consume(&sr, 2);
473
474         dns_name_init(&signer, NULL);
475         dns_name_fromregion(&signer, &sr);
476         dns_name_init(&sig->signer, NULL);
477         RETERR(name_duporclone(&signer, mctx, &sig->signer));
478         isc_region_consume(&sr, name_length(&sig->signer));
479
480         /*
481          * Signature.
482          */
483         sig->siglen = sr.length;
484         sig->signature = mem_maybedup(mctx, sr.base, sig->siglen);
485         if (sig->signature == NULL)
486                 goto cleanup;
487
488
489         sig->mctx = mctx;
490         return (ISC_R_SUCCESS);
491
492  cleanup:
493         if (mctx != NULL)
494                 dns_name_free(&sig->signer, mctx);
495         return (ISC_R_NOMEMORY);
496 }
497
498 static inline void
499 freestruct_rrsig(ARGS_FREESTRUCT) {
500         dns_rdata_rrsig_t *sig = (dns_rdata_rrsig_t *) source;
501
502         REQUIRE(source != NULL);
503         REQUIRE(sig->common.rdtype == 46);
504
505         if (sig->mctx == NULL)
506                 return;
507
508         dns_name_free(&sig->signer, sig->mctx);
509         if (sig->signature != NULL)
510                 isc_mem_free(sig->mctx, sig->signature);
511         sig->mctx = NULL;
512 }
513
514 static inline isc_result_t
515 additionaldata_rrsig(ARGS_ADDLDATA) {
516         REQUIRE(rdata->type == 46);
517
518         UNUSED(rdata);
519         UNUSED(add);
520         UNUSED(arg);
521
522         return (ISC_R_SUCCESS);
523 }
524
525 static inline isc_result_t
526 digest_rrsig(ARGS_DIGEST) {
527
528         REQUIRE(rdata->type == 46);
529
530         UNUSED(rdata);
531         UNUSED(digest);
532         UNUSED(arg);
533
534         return (ISC_R_NOTIMPLEMENTED);
535 }
536
537 static inline dns_rdatatype_t
538 covers_rrsig(dns_rdata_t *rdata) {
539         dns_rdatatype_t type;
540         isc_region_t r;
541
542         REQUIRE(rdata->type == 46);
543
544         dns_rdata_toregion(rdata, &r);
545         type = uint16_fromregion(&r);
546
547         return (type);
548 }
549
550 static inline isc_boolean_t
551 checkowner_rrsig(ARGS_CHECKOWNER) {
552
553         REQUIRE(type == 46);
554
555         UNUSED(name);
556         UNUSED(type);
557         UNUSED(rdclass);
558         UNUSED(wildcard);
559
560         return (ISC_TRUE);
561 }
562
563 static inline isc_boolean_t
564 checknames_rrsig(ARGS_CHECKNAMES) {
565
566         REQUIRE(rdata->type == 46);
567
568         UNUSED(rdata);
569         UNUSED(owner);
570         UNUSED(bad);
571
572         return (ISC_TRUE);
573 }
574
575 static inline int
576 casecompare_rrsig(ARGS_COMPARE) {
577         isc_region_t r1;
578         isc_region_t r2;
579         dns_name_t name1;
580         dns_name_t name2;
581         int order;
582
583         REQUIRE(rdata1->type == rdata2->type);
584         REQUIRE(rdata1->rdclass == rdata2->rdclass);
585         REQUIRE(rdata1->type == 46);
586         REQUIRE(rdata1->length != 0);
587         REQUIRE(rdata2->length != 0);
588
589         dns_rdata_toregion(rdata1, &r1);
590         dns_rdata_toregion(rdata2, &r2);
591
592         INSIST(r1.length > 18);
593         INSIST(r2.length > 18);
594         r1.length = 18;
595         r2.length = 18;
596         order = isc_region_compare(&r1, &r2);
597         if (order != 0)
598                 return (order);
599
600         dns_name_init(&name1, NULL);
601         dns_name_init(&name2, NULL);
602         dns_rdata_toregion(rdata1, &r1);
603         dns_rdata_toregion(rdata2, &r2);
604         isc_region_consume(&r1, 18);
605         isc_region_consume(&r2, 18);
606         dns_name_fromregion(&name1, &r1);
607         dns_name_fromregion(&name2, &r2);
608         order = dns_name_rdatacompare(&name1, &name2);
609         if (order != 0)
610                 return (order);
611
612         isc_region_consume(&r1, name_length(&name1));
613         isc_region_consume(&r2, name_length(&name2));
614
615         return (isc_region_compare(&r1, &r2));
616 }
617
618 #endif  /* RDATA_GENERIC_RRSIG_46_C */