Merge from vendor branch TCSH:
[dragonfly.git] / contrib / bind-9.3 / lib / dns / rdata / generic / rrsig_46.c
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 2003  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and 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: rrsig_46.c,v 1.4.2.3 2004/06/24 00:58:06 marka Exp $ */
19
20 /* Reviewed: Fri Mar 17 09:05:02 PST 2000 by gson */
21
22 /* RFC 2535 */
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         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_rrsig(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 == 46);
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("TYPE65535")];
158                 sprintf(buf, "TYPE%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         RETERR(isc_base64_totext(&sr, tctx->width - 2,
231                                     tctx->linebreak, target));
232         if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
233                 RETERR(str_totext(" )", target));
234
235         return (ISC_R_SUCCESS);
236 }
237
238 static inline isc_result_t
239 fromwire_rrsig(ARGS_FROMWIRE) {
240         isc_region_t sr;
241         dns_name_t name;
242
243         REQUIRE(type == 46);
244
245         UNUSED(type);
246         UNUSED(rdclass);
247
248         dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
249
250         isc_buffer_activeregion(source, &sr);
251         /*
252          * type covered: 2
253          * algorithm: 1
254          * labels: 1
255          * original ttl: 4
256          * signature expiration: 4
257          * time signed: 4
258          * key footprint: 2
259          */
260         if (sr.length < 18)
261                 return (ISC_R_UNEXPECTEDEND);
262
263         isc_buffer_forward(source, 18);
264         RETERR(mem_tobuffer(target, sr.base, 18));
265
266         /*
267          * Signer.
268          */
269         dns_name_init(&name, NULL);
270         RETERR(dns_name_fromwire(&name, source, dctx, options, target));
271
272         /*
273          * Sig.
274          */
275         isc_buffer_activeregion(source, &sr);
276         isc_buffer_forward(source, sr.length);
277         return (mem_tobuffer(target, sr.base, sr.length));
278 }
279
280 static inline isc_result_t
281 towire_rrsig(ARGS_TOWIRE) {
282         isc_region_t sr;
283         dns_name_t name;
284         dns_offsets_t offsets;
285
286         REQUIRE(rdata->type == 46);
287         REQUIRE(rdata->length != 0);
288
289         dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
290         dns_rdata_toregion(rdata, &sr);
291         /*
292          * type covered: 2
293          * algorithm: 1
294          * labels: 1
295          * original ttl: 4
296          * signature expiration: 4
297          * time signed: 4
298          * key footprint: 2
299          */
300         RETERR(mem_tobuffer(target, sr.base, 18));
301         isc_region_consume(&sr, 18);
302
303         /*
304          * Signer.
305          */
306         dns_name_init(&name, offsets);
307         dns_name_fromregion(&name, &sr);
308         isc_region_consume(&sr, name_length(&name));
309         RETERR(dns_name_towire(&name, cctx, target));
310
311         /*
312          * Signature.
313          */
314         return (mem_tobuffer(target, sr.base, sr.length));
315 }
316
317 static inline int
318 compare_rrsig(ARGS_COMPARE) {
319         isc_region_t r1;
320         isc_region_t r2;
321
322         REQUIRE(rdata1->type == rdata2->type);
323         REQUIRE(rdata1->rdclass == rdata2->rdclass);
324         REQUIRE(rdata1->type == 46);
325         REQUIRE(rdata1->length != 0);
326         REQUIRE(rdata2->length != 0);
327
328         dns_rdata_toregion(rdata1, &r1);
329         dns_rdata_toregion(rdata2, &r2);
330         return (isc_region_compare(&r1, &r2));
331 }
332
333 static inline isc_result_t
334 fromstruct_rrsig(ARGS_FROMSTRUCT) {
335         dns_rdata_rrsig_t *sig = source;
336
337         REQUIRE(type == 46);
338         REQUIRE(source != NULL);
339         REQUIRE(sig->common.rdtype == type);
340         REQUIRE(sig->common.rdclass == rdclass);
341         REQUIRE(sig->signature != NULL || sig->siglen == 0);
342
343         UNUSED(type);
344         UNUSED(rdclass);
345
346         /*
347          * Type covered.
348          */
349         RETERR(uint16_tobuffer(sig->covered, target));
350
351         /*
352          * Algorithm.
353          */
354         RETERR(uint8_tobuffer(sig->algorithm, target));
355
356         /*
357          * Labels.
358          */
359         RETERR(uint8_tobuffer(sig->labels, target));
360
361         /*
362          * Original TTL.
363          */
364         RETERR(uint32_tobuffer(sig->originalttl, target));
365
366         /*
367          * Expire time.
368          */
369         RETERR(uint32_tobuffer(sig->timeexpire, target));
370
371         /*
372          * Time signed.
373          */
374         RETERR(uint32_tobuffer(sig->timesigned, target));
375
376         /*
377          * Key ID.
378          */
379         RETERR(uint16_tobuffer(sig->keyid, target));
380
381         /*
382          * Signer name.
383          */
384         RETERR(name_tobuffer(&sig->signer, target));
385
386         /*
387          * Signature.
388          */
389         return (mem_tobuffer(target, sig->signature, sig->siglen));
390 }
391
392 static inline isc_result_t
393 tostruct_rrsig(ARGS_TOSTRUCT) {
394         isc_region_t sr;
395         dns_rdata_rrsig_t *sig = target;
396         dns_name_t signer;
397
398         REQUIRE(rdata->type == 46);
399         REQUIRE(target != NULL);
400         REQUIRE(rdata->length != 0);
401
402         sig->common.rdclass = rdata->rdclass;
403         sig->common.rdtype = rdata->type;
404         ISC_LINK_INIT(&sig->common, link);
405
406         dns_rdata_toregion(rdata, &sr);
407
408         /*
409          * Type covered.
410          */
411         sig->covered = uint16_fromregion(&sr);
412         isc_region_consume(&sr, 2);
413
414         /*
415          * Algorithm.
416          */
417         sig->algorithm = uint8_fromregion(&sr);
418         isc_region_consume(&sr, 1);
419
420         /*
421          * Labels.
422          */
423         sig->labels = uint8_fromregion(&sr);
424         isc_region_consume(&sr, 1);
425
426         /*
427          * Original TTL.
428          */
429         sig->originalttl = uint32_fromregion(&sr);
430         isc_region_consume(&sr, 4);
431
432         /*
433          * Expire time.
434          */
435         sig->timeexpire = uint32_fromregion(&sr);
436         isc_region_consume(&sr, 4);
437
438         /*
439          * Time signed.
440          */
441         sig->timesigned = uint32_fromregion(&sr);
442         isc_region_consume(&sr, 4);
443
444         /*
445          * Key ID.
446          */
447         sig->keyid = uint16_fromregion(&sr);
448         isc_region_consume(&sr, 2);
449
450         dns_name_init(&signer, NULL);
451         dns_name_fromregion(&signer, &sr);
452         dns_name_init(&sig->signer, NULL);
453         RETERR(name_duporclone(&signer, mctx, &sig->signer));
454         isc_region_consume(&sr, name_length(&sig->signer));
455
456         /*
457          * Signature.
458          */
459         sig->siglen = sr.length;
460         sig->signature = mem_maybedup(mctx, sr.base, sig->siglen);
461         if (sig->signature == NULL)
462                 goto cleanup;
463
464
465         sig->mctx = mctx;
466         return (ISC_R_SUCCESS);
467
468  cleanup:
469         if (mctx != NULL)
470                 dns_name_free(&sig->signer, mctx);
471         return (ISC_R_NOMEMORY);
472 }
473
474 static inline void
475 freestruct_rrsig(ARGS_FREESTRUCT) {
476         dns_rdata_rrsig_t *sig = (dns_rdata_rrsig_t *) source;
477
478         REQUIRE(source != NULL);
479         REQUIRE(sig->common.rdtype == 46);
480
481         if (sig->mctx == NULL)
482                 return;
483
484         dns_name_free(&sig->signer, sig->mctx);
485         if (sig->signature != NULL)
486                 isc_mem_free(sig->mctx, sig->signature);
487         sig->mctx = NULL;
488 }
489
490 static inline isc_result_t
491 additionaldata_rrsig(ARGS_ADDLDATA) {
492         REQUIRE(rdata->type == 46);
493
494         UNUSED(rdata);
495         UNUSED(add);
496         UNUSED(arg);
497
498         return (ISC_R_SUCCESS);
499 }
500
501 static inline isc_result_t
502 digest_rrsig(ARGS_DIGEST) {
503
504         REQUIRE(rdata->type == 46);
505
506         UNUSED(rdata);
507         UNUSED(digest);
508         UNUSED(arg);
509
510         return (ISC_R_NOTIMPLEMENTED);
511 }
512
513 static inline dns_rdatatype_t
514 covers_rrsig(dns_rdata_t *rdata) {
515         dns_rdatatype_t type;
516         isc_region_t r;
517
518         REQUIRE(rdata->type == 46);
519
520         dns_rdata_toregion(rdata, &r);
521         type = uint16_fromregion(&r);
522
523         return (type);
524 }
525
526 static inline isc_boolean_t
527 checkowner_rrsig(ARGS_CHECKOWNER) {
528
529         REQUIRE(type == 46);
530
531         UNUSED(name);
532         UNUSED(type);
533         UNUSED(rdclass);
534         UNUSED(wildcard);
535
536         return (ISC_TRUE);
537 }
538
539 static inline isc_boolean_t
540 checknames_rrsig(ARGS_CHECKNAMES) {
541
542         REQUIRE(rdata->type == 46);
543
544         UNUSED(rdata);
545         UNUSED(owner);
546         UNUSED(bad);
547
548         return (ISC_TRUE);
549 }
550
551 #endif  /* RDATA_GENERIC_RRSIG_46_C */