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