Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / dns / rdata / generic / rrsig_46.c
blob61f6779d009ab79cd7e943587c14ebd87d9654f4
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: rrsig_46.c,v 1.12 2009/12/04 22:06:37 tbox Exp */
22 /* Reviewed: Fri Mar 17 09:05:02 PST 2000 by gson */
24 /* RFC2535 */
26 #ifndef RDATA_GENERIC_RRSIG_46_C
27 #define RDATA_GENERIC_RRSIG_46_C
29 #define RRTYPE_RRSIG_ATTRIBUTES (DNS_RDATATYPEATTR_DNSSEC)
31 static inline isc_result_t
32 fromtext_rrsig(ARGS_FROMTEXT) {
33 isc_token_t token;
34 unsigned char c;
35 long i;
36 dns_rdatatype_t covered;
37 char *e;
38 isc_result_t result;
39 dns_name_t name;
40 isc_buffer_t buffer;
41 isc_uint32_t time_signed, time_expire;
43 REQUIRE(type == 46);
45 UNUSED(type);
46 UNUSED(rdclass);
47 UNUSED(callbacks);
50 * Type covered.
52 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
53 ISC_FALSE));
54 result = dns_rdatatype_fromtext(&covered, &token.value.as_textregion);
55 if (result != ISC_R_SUCCESS && result != ISC_R_NOTIMPLEMENTED) {
56 i = strtol(DNS_AS_STR(token), &e, 10);
57 if (i < 0 || i > 65535)
58 RETTOK(ISC_R_RANGE);
59 if (*e != 0)
60 RETTOK(result);
61 covered = (dns_rdatatype_t)i;
63 RETERR(uint16_tobuffer(covered, target));
66 * Algorithm.
68 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
69 ISC_FALSE));
70 RETTOK(dns_secalg_fromtext(&c, &token.value.as_textregion));
71 RETERR(mem_tobuffer(target, &c, 1));
74 * Labels.
76 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
77 ISC_FALSE));
78 if (token.value.as_ulong > 0xffU)
79 RETTOK(ISC_R_RANGE);
80 c = (unsigned char)token.value.as_ulong;
81 RETERR(mem_tobuffer(target, &c, 1));
84 * Original ttl.
86 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
87 ISC_FALSE));
88 RETERR(uint32_tobuffer(token.value.as_ulong, target));
91 * Signature expiration.
93 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
94 ISC_FALSE));
95 RETTOK(dns_time32_fromtext(DNS_AS_STR(token), &time_expire));
96 RETERR(uint32_tobuffer(time_expire, target));
99 * Time signed.
101 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
102 ISC_FALSE));
103 RETTOK(dns_time32_fromtext(DNS_AS_STR(token), &time_signed));
104 RETERR(uint32_tobuffer(time_signed, target));
107 * Key footprint.
109 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
110 ISC_FALSE));
111 RETERR(uint16_tobuffer(token.value.as_ulong, target));
114 * Signer.
116 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
117 ISC_FALSE));
118 dns_name_init(&name, NULL);
119 buffer_fromregion(&buffer, &token.value.as_region);
120 origin = (origin != NULL) ? origin : dns_rootname;
121 RETTOK(dns_name_fromtext(&name, &buffer, origin, options, target));
124 * Sig.
126 return (isc_base64_tobuffer(lexer, target, -1));
129 static inline isc_result_t
130 totext_rrsig(ARGS_TOTEXT) {
131 isc_region_t sr;
132 char buf[sizeof("4294967295")];
133 dns_rdatatype_t covered;
134 unsigned long ttl;
135 unsigned long when;
136 unsigned long exp;
137 unsigned long foot;
138 dns_name_t name;
139 dns_name_t prefix;
140 isc_boolean_t sub;
142 REQUIRE(rdata->type == 46);
143 REQUIRE(rdata->length != 0);
145 dns_rdata_toregion(rdata, &sr);
148 * Type covered.
150 covered = uint16_fromregion(&sr);
151 isc_region_consume(&sr, 2);
153 * XXXAG We should have something like dns_rdatatype_isknown()
154 * that does the right thing with type 0.
156 if (dns_rdatatype_isknown(covered) && covered != 0) {
157 RETERR(dns_rdatatype_totext(covered, target));
158 } else {
159 char buf[sizeof("TYPE65535")];
160 sprintf(buf, "TYPE%u", covered);
161 RETERR(str_totext(buf, target));
163 RETERR(str_totext(" ", target));
166 * Algorithm.
168 sprintf(buf, "%u", sr.base[0]);
169 isc_region_consume(&sr, 1);
170 RETERR(str_totext(buf, target));
171 RETERR(str_totext(" ", target));
174 * Labels.
176 sprintf(buf, "%u", sr.base[0]);
177 isc_region_consume(&sr, 1);
178 RETERR(str_totext(buf, target));
179 RETERR(str_totext(" ", target));
182 * Ttl.
184 ttl = uint32_fromregion(&sr);
185 isc_region_consume(&sr, 4);
186 sprintf(buf, "%lu", ttl);
187 RETERR(str_totext(buf, target));
188 RETERR(str_totext(" ", target));
191 * Sig exp.
193 exp = uint32_fromregion(&sr);
194 isc_region_consume(&sr, 4);
195 RETERR(dns_time32_totext(exp, target));
197 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
198 RETERR(str_totext(" (", target));
199 RETERR(str_totext(tctx->linebreak, target));
202 * Time signed.
204 when = uint32_fromregion(&sr);
205 isc_region_consume(&sr, 4);
206 RETERR(dns_time32_totext(when, target));
207 RETERR(str_totext(" ", target));
210 * Footprint.
212 foot = uint16_fromregion(&sr);
213 isc_region_consume(&sr, 2);
214 sprintf(buf, "%lu", foot);
215 RETERR(str_totext(buf, target));
216 RETERR(str_totext(" ", target));
219 * Signer.
221 dns_name_init(&name, NULL);
222 dns_name_init(&prefix, NULL);
223 dns_name_fromregion(&name, &sr);
224 isc_region_consume(&sr, name_length(&name));
225 sub = name_prefix(&name, tctx->origin, &prefix);
226 RETERR(dns_name_totext(&prefix, sub, target));
229 * Sig.
231 RETERR(str_totext(tctx->linebreak, target));
232 RETERR(isc_base64_totext(&sr, tctx->width - 2,
233 tctx->linebreak, target));
234 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
235 RETERR(str_totext(" )", target));
237 return (ISC_R_SUCCESS);
240 static inline isc_result_t
241 fromwire_rrsig(ARGS_FROMWIRE) {
242 isc_region_t sr;
243 dns_name_t name;
245 REQUIRE(type == 46);
247 UNUSED(type);
248 UNUSED(rdclass);
250 dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
252 isc_buffer_activeregion(source, &sr);
254 * type covered: 2
255 * algorithm: 1
256 * labels: 1
257 * original ttl: 4
258 * signature expiration: 4
259 * time signed: 4
260 * key footprint: 2
262 if (sr.length < 18)
263 return (ISC_R_UNEXPECTEDEND);
265 isc_buffer_forward(source, 18);
266 RETERR(mem_tobuffer(target, sr.base, 18));
269 * Signer.
271 dns_name_init(&name, NULL);
272 RETERR(dns_name_fromwire(&name, source, dctx, options, target));
275 * Sig.
277 isc_buffer_activeregion(source, &sr);
278 isc_buffer_forward(source, sr.length);
279 return (mem_tobuffer(target, sr.base, sr.length));
282 static inline isc_result_t
283 towire_rrsig(ARGS_TOWIRE) {
284 isc_region_t sr;
285 dns_name_t name;
286 dns_offsets_t offsets;
288 REQUIRE(rdata->type == 46);
289 REQUIRE(rdata->length != 0);
291 dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
292 dns_rdata_toregion(rdata, &sr);
294 * type covered: 2
295 * algorithm: 1
296 * labels: 1
297 * original ttl: 4
298 * signature expiration: 4
299 * time signed: 4
300 * key footprint: 2
302 RETERR(mem_tobuffer(target, sr.base, 18));
303 isc_region_consume(&sr, 18);
306 * Signer.
308 dns_name_init(&name, offsets);
309 dns_name_fromregion(&name, &sr);
310 isc_region_consume(&sr, name_length(&name));
311 RETERR(dns_name_towire(&name, cctx, target));
314 * Signature.
316 return (mem_tobuffer(target, sr.base, sr.length));
319 static inline int
320 compare_rrsig(ARGS_COMPARE) {
321 isc_region_t r1;
322 isc_region_t r2;
324 REQUIRE(rdata1->type == rdata2->type);
325 REQUIRE(rdata1->rdclass == rdata2->rdclass);
326 REQUIRE(rdata1->type == 46);
327 REQUIRE(rdata1->length != 0);
328 REQUIRE(rdata2->length != 0);
330 dns_rdata_toregion(rdata1, &r1);
331 dns_rdata_toregion(rdata2, &r2);
332 return (isc_region_compare(&r1, &r2));
335 static inline isc_result_t
336 fromstruct_rrsig(ARGS_FROMSTRUCT) {
337 dns_rdata_rrsig_t *sig = source;
339 REQUIRE(type == 46);
340 REQUIRE(source != NULL);
341 REQUIRE(sig->common.rdtype == type);
342 REQUIRE(sig->common.rdclass == rdclass);
343 REQUIRE(sig->signature != NULL || sig->siglen == 0);
345 UNUSED(type);
346 UNUSED(rdclass);
349 * Type covered.
351 RETERR(uint16_tobuffer(sig->covered, target));
354 * Algorithm.
356 RETERR(uint8_tobuffer(sig->algorithm, target));
359 * Labels.
361 RETERR(uint8_tobuffer(sig->labels, target));
364 * Original TTL.
366 RETERR(uint32_tobuffer(sig->originalttl, target));
369 * Expire time.
371 RETERR(uint32_tobuffer(sig->timeexpire, target));
374 * Time signed.
376 RETERR(uint32_tobuffer(sig->timesigned, target));
379 * Key ID.
381 RETERR(uint16_tobuffer(sig->keyid, target));
384 * Signer name.
386 RETERR(name_tobuffer(&sig->signer, target));
389 * Signature.
391 return (mem_tobuffer(target, sig->signature, sig->siglen));
394 static inline isc_result_t
395 tostruct_rrsig(ARGS_TOSTRUCT) {
396 isc_region_t sr;
397 dns_rdata_rrsig_t *sig = target;
398 dns_name_t signer;
400 REQUIRE(rdata->type == 46);
401 REQUIRE(target != NULL);
402 REQUIRE(rdata->length != 0);
404 sig->common.rdclass = rdata->rdclass;
405 sig->common.rdtype = rdata->type;
406 ISC_LINK_INIT(&sig->common, link);
408 dns_rdata_toregion(rdata, &sr);
411 * Type covered.
413 sig->covered = uint16_fromregion(&sr);
414 isc_region_consume(&sr, 2);
417 * Algorithm.
419 sig->algorithm = uint8_fromregion(&sr);
420 isc_region_consume(&sr, 1);
423 * Labels.
425 sig->labels = uint8_fromregion(&sr);
426 isc_region_consume(&sr, 1);
429 * Original TTL.
431 sig->originalttl = uint32_fromregion(&sr);
432 isc_region_consume(&sr, 4);
435 * Expire time.
437 sig->timeexpire = uint32_fromregion(&sr);
438 isc_region_consume(&sr, 4);
441 * Time signed.
443 sig->timesigned = uint32_fromregion(&sr);
444 isc_region_consume(&sr, 4);
447 * Key ID.
449 sig->keyid = uint16_fromregion(&sr);
450 isc_region_consume(&sr, 2);
452 dns_name_init(&signer, NULL);
453 dns_name_fromregion(&signer, &sr);
454 dns_name_init(&sig->signer, NULL);
455 RETERR(name_duporclone(&signer, mctx, &sig->signer));
456 isc_region_consume(&sr, name_length(&sig->signer));
459 * Signature.
461 sig->siglen = sr.length;
462 sig->signature = mem_maybedup(mctx, sr.base, sig->siglen);
463 if (sig->signature == NULL)
464 goto cleanup;
467 sig->mctx = mctx;
468 return (ISC_R_SUCCESS);
470 cleanup:
471 if (mctx != NULL)
472 dns_name_free(&sig->signer, mctx);
473 return (ISC_R_NOMEMORY);
476 static inline void
477 freestruct_rrsig(ARGS_FREESTRUCT) {
478 dns_rdata_rrsig_t *sig = (dns_rdata_rrsig_t *) source;
480 REQUIRE(source != NULL);
481 REQUIRE(sig->common.rdtype == 46);
483 if (sig->mctx == NULL)
484 return;
486 dns_name_free(&sig->signer, sig->mctx);
487 if (sig->signature != NULL)
488 isc_mem_free(sig->mctx, sig->signature);
489 sig->mctx = NULL;
492 static inline isc_result_t
493 additionaldata_rrsig(ARGS_ADDLDATA) {
494 REQUIRE(rdata->type == 46);
496 UNUSED(rdata);
497 UNUSED(add);
498 UNUSED(arg);
500 return (ISC_R_SUCCESS);
503 static inline isc_result_t
504 digest_rrsig(ARGS_DIGEST) {
506 REQUIRE(rdata->type == 46);
508 UNUSED(rdata);
509 UNUSED(digest);
510 UNUSED(arg);
512 return (ISC_R_NOTIMPLEMENTED);
515 static inline dns_rdatatype_t
516 covers_rrsig(dns_rdata_t *rdata) {
517 dns_rdatatype_t type;
518 isc_region_t r;
520 REQUIRE(rdata->type == 46);
522 dns_rdata_toregion(rdata, &r);
523 type = uint16_fromregion(&r);
525 return (type);
528 static inline isc_boolean_t
529 checkowner_rrsig(ARGS_CHECKOWNER) {
531 REQUIRE(type == 46);
533 UNUSED(name);
534 UNUSED(type);
535 UNUSED(rdclass);
536 UNUSED(wildcard);
538 return (ISC_TRUE);
541 static inline isc_boolean_t
542 checknames_rrsig(ARGS_CHECKNAMES) {
544 REQUIRE(rdata->type == 46);
546 UNUSED(rdata);
547 UNUSED(owner);
548 UNUSED(bad);
550 return (ISC_TRUE);
553 static inline int
554 casecompare_rrsig(ARGS_COMPARE) {
555 isc_region_t r1;
556 isc_region_t r2;
557 dns_name_t name1;
558 dns_name_t name2;
559 int order;
561 REQUIRE(rdata1->type == rdata2->type);
562 REQUIRE(rdata1->rdclass == rdata2->rdclass);
563 REQUIRE(rdata1->type == 46);
564 REQUIRE(rdata1->length != 0);
565 REQUIRE(rdata2->length != 0);
567 dns_rdata_toregion(rdata1, &r1);
568 dns_rdata_toregion(rdata2, &r2);
570 INSIST(r1.length > 18);
571 INSIST(r2.length > 18);
572 r1.length = 18;
573 r2.length = 18;
574 order = isc_region_compare(&r1, &r2);
575 if (order != 0)
576 return (order);
578 dns_name_init(&name1, NULL);
579 dns_name_init(&name2, NULL);
580 dns_rdata_toregion(rdata1, &r1);
581 dns_rdata_toregion(rdata2, &r2);
582 isc_region_consume(&r1, 18);
583 isc_region_consume(&r2, 18);
584 dns_name_fromregion(&name1, &r1);
585 dns_name_fromregion(&name2, &r2);
586 order = dns_name_rdatacompare(&name1, &name2);
587 if (order != 0)
588 return (order);
590 isc_region_consume(&r1, name_length(&name1));
591 isc_region_consume(&r2, name_length(&name2));
593 return (isc_region_compare(&r1, &r2));
596 #endif /* RDATA_GENERIC_RRSIG_46_C */