]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - contrib/bind9/lib/dns/rdata/generic/rrsig_46.c
MFC r362623:
[FreeBSD/stable/8.git] / contrib / bind9 / lib / dns / rdata / generic / rrsig_46.c
1 /*
2  * Copyright (C) 2004, 2005, 2007, 2009, 2011, 2012, 2014  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                 char buf[sizeof("TYPE65535")];
182                 sprintf(buf, "TYPE%u", covered);
183                 RETERR(str_totext(buf, target));
184         }
185         RETERR(str_totext(" ", target));
186
187         /*
188          * Algorithm.
189          */
190         sprintf(buf, "%u", sr.base[0]);
191         isc_region_consume(&sr, 1);
192         RETERR(str_totext(buf, target));
193         RETERR(str_totext(" ", target));
194
195         /*
196          * Labels.
197          */
198         sprintf(buf, "%u", sr.base[0]);
199         isc_region_consume(&sr, 1);
200         RETERR(str_totext(buf, target));
201         RETERR(str_totext(" ", target));
202
203         /*
204          * Ttl.
205          */
206         ttl = uint32_fromregion(&sr);
207         isc_region_consume(&sr, 4);
208         sprintf(buf, "%lu", ttl);
209         RETERR(str_totext(buf, target));
210         RETERR(str_totext(" ", target));
211
212         /*
213          * Sig exp.
214          */
215         exp = uint32_fromregion(&sr);
216         isc_region_consume(&sr, 4);
217         RETERR(dns_time32_totext(exp, target));
218
219         if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
220                 RETERR(str_totext(" (", target));
221         RETERR(str_totext(tctx->linebreak, target));
222
223         /*
224          * Time signed.
225          */
226         when = uint32_fromregion(&sr);
227         isc_region_consume(&sr, 4);
228         RETERR(dns_time32_totext(when, target));
229         RETERR(str_totext(" ", target));
230
231         /*
232          * Footprint.
233          */
234         foot = uint16_fromregion(&sr);
235         isc_region_consume(&sr, 2);
236         sprintf(buf, "%lu", foot);
237         RETERR(str_totext(buf, target));
238         RETERR(str_totext(" ", target));
239
240         /*
241          * Signer.
242          */
243         dns_name_init(&name, NULL);
244         dns_name_fromregion(&name, &sr);
245         isc_region_consume(&sr, name_length(&name));
246         RETERR(dns_name_totext(&name, ISC_FALSE, target));
247
248         /*
249          * Sig.
250          */
251         RETERR(str_totext(tctx->linebreak, target));
252         RETERR(isc_base64_totext(&sr, tctx->width - 2,
253                                     tctx->linebreak, target));
254         if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
255                 RETERR(str_totext(" )", target));
256
257         return (ISC_R_SUCCESS);
258 }
259
260 static inline isc_result_t
261 fromwire_rrsig(ARGS_FROMWIRE) {
262         isc_region_t sr;
263         dns_name_t name;
264
265         REQUIRE(type == 46);
266
267         UNUSED(type);
268         UNUSED(rdclass);
269
270         dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
271
272         isc_buffer_activeregion(source, &sr);
273         /*
274          * type covered: 2
275          * algorithm: 1
276          * labels: 1
277          * original ttl: 4
278          * signature expiration: 4
279          * time signed: 4
280          * key footprint: 2
281          */
282         if (sr.length < 18)
283                 return (ISC_R_UNEXPECTEDEND);
284
285         isc_buffer_forward(source, 18);
286         RETERR(mem_tobuffer(target, sr.base, 18));
287
288         /*
289          * Signer.
290          */
291         dns_name_init(&name, NULL);
292         RETERR(dns_name_fromwire(&name, source, dctx, options, target));
293
294         /*
295          * Sig.
296          */
297         isc_buffer_activeregion(source, &sr);
298         isc_buffer_forward(source, sr.length);
299         return (mem_tobuffer(target, sr.base, sr.length));
300 }
301
302 static inline isc_result_t
303 towire_rrsig(ARGS_TOWIRE) {
304         isc_region_t sr;
305         dns_name_t name;
306         dns_offsets_t offsets;
307
308         REQUIRE(rdata->type == 46);
309         REQUIRE(rdata->length != 0);
310
311         dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
312         dns_rdata_toregion(rdata, &sr);
313         /*
314          * type covered: 2
315          * algorithm: 1
316          * labels: 1
317          * original ttl: 4
318          * signature expiration: 4
319          * time signed: 4
320          * key footprint: 2
321          */
322         RETERR(mem_tobuffer(target, sr.base, 18));
323         isc_region_consume(&sr, 18);
324
325         /*
326          * Signer.
327          */
328         dns_name_init(&name, offsets);
329         dns_name_fromregion(&name, &sr);
330         isc_region_consume(&sr, name_length(&name));
331         RETERR(dns_name_towire(&name, cctx, target));
332
333         /*
334          * Signature.
335          */
336         return (mem_tobuffer(target, sr.base, sr.length));
337 }
338
339 static inline int
340 compare_rrsig(ARGS_COMPARE) {
341         isc_region_t r1;
342         isc_region_t r2;
343
344         REQUIRE(rdata1->type == rdata2->type);
345         REQUIRE(rdata1->rdclass == rdata2->rdclass);
346         REQUIRE(rdata1->type == 46);
347         REQUIRE(rdata1->length != 0);
348         REQUIRE(rdata2->length != 0);
349
350         dns_rdata_toregion(rdata1, &r1);
351         dns_rdata_toregion(rdata2, &r2);
352         return (isc_region_compare(&r1, &r2));
353 }
354
355 static inline isc_result_t
356 fromstruct_rrsig(ARGS_FROMSTRUCT) {
357         dns_rdata_rrsig_t *sig = source;
358
359         REQUIRE(type == 46);
360         REQUIRE(source != NULL);
361         REQUIRE(sig->common.rdtype == type);
362         REQUIRE(sig->common.rdclass == rdclass);
363         REQUIRE(sig->signature != NULL || sig->siglen == 0);
364
365         UNUSED(type);
366         UNUSED(rdclass);
367
368         /*
369          * Type covered.
370          */
371         RETERR(uint16_tobuffer(sig->covered, target));
372
373         /*
374          * Algorithm.
375          */
376         RETERR(uint8_tobuffer(sig->algorithm, target));
377
378         /*
379          * Labels.
380          */
381         RETERR(uint8_tobuffer(sig->labels, target));
382
383         /*
384          * Original TTL.
385          */
386         RETERR(uint32_tobuffer(sig->originalttl, target));
387
388         /*
389          * Expire time.
390          */
391         RETERR(uint32_tobuffer(sig->timeexpire, target));
392
393         /*
394          * Time signed.
395          */
396         RETERR(uint32_tobuffer(sig->timesigned, target));
397
398         /*
399          * Key ID.
400          */
401         RETERR(uint16_tobuffer(sig->keyid, target));
402
403         /*
404          * Signer name.
405          */
406         RETERR(name_tobuffer(&sig->signer, target));
407
408         /*
409          * Signature.
410          */
411         return (mem_tobuffer(target, sig->signature, sig->siglen));
412 }
413
414 static inline isc_result_t
415 tostruct_rrsig(ARGS_TOSTRUCT) {
416         isc_region_t sr;
417         dns_rdata_rrsig_t *sig = target;
418         dns_name_t signer;
419
420         REQUIRE(rdata->type == 46);
421         REQUIRE(target != NULL);
422         REQUIRE(rdata->length != 0);
423
424         sig->common.rdclass = rdata->rdclass;
425         sig->common.rdtype = rdata->type;
426         ISC_LINK_INIT(&sig->common, link);
427
428         dns_rdata_toregion(rdata, &sr);
429
430         /*
431          * Type covered.
432          */
433         sig->covered = uint16_fromregion(&sr);
434         isc_region_consume(&sr, 2);
435
436         /*
437          * Algorithm.
438          */
439         sig->algorithm = uint8_fromregion(&sr);
440         isc_region_consume(&sr, 1);
441
442         /*
443          * Labels.
444          */
445         sig->labels = uint8_fromregion(&sr);
446         isc_region_consume(&sr, 1);
447
448         /*
449          * Original TTL.
450          */
451         sig->originalttl = uint32_fromregion(&sr);
452         isc_region_consume(&sr, 4);
453
454         /*
455          * Expire time.
456          */
457         sig->timeexpire = uint32_fromregion(&sr);
458         isc_region_consume(&sr, 4);
459
460         /*
461          * Time signed.
462          */
463         sig->timesigned = uint32_fromregion(&sr);
464         isc_region_consume(&sr, 4);
465
466         /*
467          * Key ID.
468          */
469         sig->keyid = uint16_fromregion(&sr);
470         isc_region_consume(&sr, 2);
471
472         dns_name_init(&signer, NULL);
473         dns_name_fromregion(&signer, &sr);
474         dns_name_init(&sig->signer, NULL);
475         RETERR(name_duporclone(&signer, mctx, &sig->signer));
476         isc_region_consume(&sr, name_length(&sig->signer));
477
478         /*
479          * Signature.
480          */
481         sig->siglen = sr.length;
482         sig->signature = mem_maybedup(mctx, sr.base, sig->siglen);
483         if (sig->signature == NULL)
484                 goto cleanup;
485
486
487         sig->mctx = mctx;
488         return (ISC_R_SUCCESS);
489
490  cleanup:
491         if (mctx != NULL)
492                 dns_name_free(&sig->signer, mctx);
493         return (ISC_R_NOMEMORY);
494 }
495
496 static inline void
497 freestruct_rrsig(ARGS_FREESTRUCT) {
498         dns_rdata_rrsig_t *sig = (dns_rdata_rrsig_t *) source;
499
500         REQUIRE(source != NULL);
501         REQUIRE(sig->common.rdtype == 46);
502
503         if (sig->mctx == NULL)
504                 return;
505
506         dns_name_free(&sig->signer, sig->mctx);
507         if (sig->signature != NULL)
508                 isc_mem_free(sig->mctx, sig->signature);
509         sig->mctx = NULL;
510 }
511
512 static inline isc_result_t
513 additionaldata_rrsig(ARGS_ADDLDATA) {
514         REQUIRE(rdata->type == 46);
515
516         UNUSED(rdata);
517         UNUSED(add);
518         UNUSED(arg);
519
520         return (ISC_R_SUCCESS);
521 }
522
523 static inline isc_result_t
524 digest_rrsig(ARGS_DIGEST) {
525
526         REQUIRE(rdata->type == 46);
527
528         UNUSED(rdata);
529         UNUSED(digest);
530         UNUSED(arg);
531
532         return (ISC_R_NOTIMPLEMENTED);
533 }
534
535 static inline dns_rdatatype_t
536 covers_rrsig(dns_rdata_t *rdata) {
537         dns_rdatatype_t type;
538         isc_region_t r;
539
540         REQUIRE(rdata->type == 46);
541
542         dns_rdata_toregion(rdata, &r);
543         type = uint16_fromregion(&r);
544
545         return (type);
546 }
547
548 static inline isc_boolean_t
549 checkowner_rrsig(ARGS_CHECKOWNER) {
550
551         REQUIRE(type == 46);
552
553         UNUSED(name);
554         UNUSED(type);
555         UNUSED(rdclass);
556         UNUSED(wildcard);
557
558         return (ISC_TRUE);
559 }
560
561 static inline isc_boolean_t
562 checknames_rrsig(ARGS_CHECKNAMES) {
563
564         REQUIRE(rdata->type == 46);
565
566         UNUSED(rdata);
567         UNUSED(owner);
568         UNUSED(bad);
569
570         return (ISC_TRUE);
571 }
572
573 static inline int
574 casecompare_rrsig(ARGS_COMPARE) {
575         isc_region_t r1;
576         isc_region_t r2;
577         dns_name_t name1;
578         dns_name_t name2;
579         int order;
580
581         REQUIRE(rdata1->type == rdata2->type);
582         REQUIRE(rdata1->rdclass == rdata2->rdclass);
583         REQUIRE(rdata1->type == 46);
584         REQUIRE(rdata1->length != 0);
585         REQUIRE(rdata2->length != 0);
586
587         dns_rdata_toregion(rdata1, &r1);
588         dns_rdata_toregion(rdata2, &r2);
589
590         INSIST(r1.length > 18);
591         INSIST(r2.length > 18);
592         r1.length = 18;
593         r2.length = 18;
594         order = isc_region_compare(&r1, &r2);
595         if (order != 0)
596                 return (order);
597
598         dns_name_init(&name1, NULL);
599         dns_name_init(&name2, NULL);
600         dns_rdata_toregion(rdata1, &r1);
601         dns_rdata_toregion(rdata2, &r2);
602         isc_region_consume(&r1, 18);
603         isc_region_consume(&r2, 18);
604         dns_name_fromregion(&name1, &r1);
605         dns_name_fromregion(&name2, &r2);
606         order = dns_name_rdatacompare(&name1, &name2);
607         if (order != 0)
608                 return (order);
609
610         isc_region_consume(&r1, name_length(&name1));
611         isc_region_consume(&r2, name_length(&name2));
612
613         return (isc_region_compare(&r1, &r2));
614 }
615
616 #endif  /* RDATA_GENERIC_RRSIG_46_C */