Add BIND 9.2.4rc7.
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / dns / rdata / generic / sig_24.c
bloba7bb733127a9742c85bd4bba87bf1f2517390f7b
1 /*
2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2001, 2003 Internet Software Consortium.
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.
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.
18 /* $Id: sig_24.c,v 1.54.2.3 2004/03/09 06:11:34 marka Exp $ */
20 /* Reviewed: Fri Mar 17 09:05:02 PST 2000 by gson */
22 /* RFC 2535 */
24 #ifndef RDATA_GENERIC_SIG_24_C
25 #define RDATA_GENERIC_SIG_24_C
27 #define RRTYPE_SIG_ATTRIBUTES (DNS_RDATATYPEATTR_DNSSEC)
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;
41 REQUIRE(type == 24);
43 UNUSED(type);
44 UNUSED(rdclass);
45 UNUSED(callbacks);
48 * Type covered.
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(token.value.as_pointer, &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;
61 RETERR(uint16_tobuffer(covered, target));
64 * Algorithm.
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));
72 * Labels.
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));
82 * Original ttl.
84 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
85 ISC_FALSE));
86 RETERR(uint32_tobuffer(token.value.as_ulong, target));
89 * Signature expiration.
91 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
92 ISC_FALSE));
93 RETTOK(dns_time32_fromtext(token.value.as_pointer, &time_expire));
94 RETERR(uint32_tobuffer(time_expire, target));
97 * Time signed.
99 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
100 ISC_FALSE));
101 RETTOK(dns_time32_fromtext(token.value.as_pointer, &time_signed));
102 RETERR(uint32_tobuffer(time_signed, target));
105 * Key footprint.
107 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
108 ISC_FALSE));
109 RETERR(uint16_tobuffer(token.value.as_ulong, target));
112 * Signer.
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, downcase, target));
122 * Sig.
124 return (isc_base64_tobuffer(lexer, target, -1));
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;
140 REQUIRE(rdata->type == 24);
141 REQUIRE(rdata->length != 0);
143 dns_rdata_toregion(rdata, &sr);
146 * Type covered.
148 covered = uint16_fromregion(&sr);
149 isc_region_consume(&sr, 2);
151 * XXXAG We should have something like dns_rdatatype_isknown()
152 * that does the right thing with type 0.
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));
161 RETERR(str_totext(" ", target));
164 * Algorithm.
166 sprintf(buf, "%u", sr.base[0]);
167 isc_region_consume(&sr, 1);
168 RETERR(str_totext(buf, target));
169 RETERR(str_totext(" ", target));
172 * Labels.
174 sprintf(buf, "%u", sr.base[0]);
175 isc_region_consume(&sr, 1);
176 RETERR(str_totext(buf, target));
177 RETERR(str_totext(" ", target));
180 * Ttl.
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));
189 * Sig exp.
191 exp = uint32_fromregion(&sr);
192 isc_region_consume(&sr, 4);
193 RETERR(dns_time32_totext(exp, target));
195 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
196 RETERR(str_totext(" (", target));
197 RETERR(str_totext(tctx->linebreak, target));
200 * Time signed.
202 when = uint32_fromregion(&sr);
203 isc_region_consume(&sr, 4);
204 RETERR(dns_time32_totext(when, target));
205 RETERR(str_totext(" ", target));
208 * Footprint.
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));
217 * Signer.
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));
227 * Sig.
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));
235 return (ISC_R_SUCCESS);
238 static inline isc_result_t
239 fromwire_sig(ARGS_FROMWIRE) {
240 isc_region_t sr;
241 dns_name_t name;
243 REQUIRE(type == 24);
245 UNUSED(type);
246 UNUSED(rdclass);
248 dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
250 isc_buffer_activeregion(source, &sr);
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
260 if (sr.length < 18)
261 return (ISC_R_UNEXPECTEDEND);
263 isc_buffer_forward(source, 18);
264 RETERR(mem_tobuffer(target, sr.base, 18));
267 * Signer.
269 dns_name_init(&name, NULL);
270 RETERR(dns_name_fromwire(&name, source, dctx, downcase, target));
273 * Sig.
275 isc_buffer_activeregion(source, &sr);
276 isc_buffer_forward(source, sr.length);
277 return (mem_tobuffer(target, sr.base, sr.length));
280 static inline isc_result_t
281 towire_sig(ARGS_TOWIRE) {
282 isc_region_t sr;
283 dns_name_t name;
284 dns_offsets_t offsets;
286 REQUIRE(rdata->type == 24);
287 REQUIRE(rdata->length != 0);
289 dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
290 dns_rdata_toregion(rdata, &sr);
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
300 RETERR(mem_tobuffer(target, sr.base, 18));
301 isc_region_consume(&sr, 18);
304 * Signer.
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));
312 * Signature.
314 return (mem_tobuffer(target, sr.base, sr.length));
317 static inline int
318 compare_sig(ARGS_COMPARE) {
319 isc_region_t r1;
320 isc_region_t r2;
321 dns_name_t name1;
322 dns_name_t name2;
323 int order;
325 REQUIRE(rdata1->type == rdata2->type);
326 REQUIRE(rdata1->rdclass == rdata2->rdclass);
327 REQUIRE(rdata1->type == 24);
328 REQUIRE(rdata1->length != 0);
329 REQUIRE(rdata2->length != 0);
331 dns_rdata_toregion(rdata1, &r1);
332 dns_rdata_toregion(rdata2, &r2);
334 INSIST(r1.length > 18);
335 INSIST(r2.length > 18);
336 r1.length = 18;
337 r2.length = 18;
338 order = compare_region(&r1, &r2);
339 if (order != 0)
340 return (order);
342 dns_name_init(&name1, NULL);
343 dns_name_init(&name2, NULL);
344 dns_rdata_toregion(rdata1, &r1);
345 dns_rdata_toregion(rdata2, &r2);
346 isc_region_consume(&r1, 18);
347 isc_region_consume(&r2, 18);
348 dns_name_fromregion(&name1, &r1);
349 dns_name_fromregion(&name2, &r2);
350 order = dns_name_rdatacompare(&name1, &name2);
351 if (order != 0)
352 return (order);
354 isc_region_consume(&r1, name_length(&name1));
355 isc_region_consume(&r2, name_length(&name2));
357 return (compare_region(&r1, &r2));
360 static inline isc_result_t
361 fromstruct_sig(ARGS_FROMSTRUCT) {
362 dns_rdata_sig_t *sig = source;
364 REQUIRE(type == 24);
365 REQUIRE(source != NULL);
366 REQUIRE(sig->common.rdtype == type);
367 REQUIRE(sig->common.rdclass == rdclass);
368 REQUIRE(sig->signature != NULL || sig->siglen == 0);
370 UNUSED(type);
371 UNUSED(rdclass);
374 * Type covered.
376 RETERR(uint16_tobuffer(sig->covered, target));
379 * Algorithm.
381 RETERR(uint8_tobuffer(sig->algorithm, target));
384 * Labels.
386 RETERR(uint8_tobuffer(sig->labels, target));
389 * Original TTL.
391 RETERR(uint32_tobuffer(sig->originalttl, target));
394 * Expire time.
396 RETERR(uint32_tobuffer(sig->timeexpire, target));
399 * Time signed.
401 RETERR(uint32_tobuffer(sig->timesigned, target));
404 * Key ID.
406 RETERR(uint16_tobuffer(sig->keyid, target));
409 * Signer name.
411 RETERR(name_tobuffer(&sig->signer, target));
414 * Signature.
416 return (mem_tobuffer(target, sig->signature, sig->siglen));
419 static inline isc_result_t
420 tostruct_sig(ARGS_TOSTRUCT) {
421 isc_region_t sr;
422 dns_rdata_sig_t *sig = target;
423 dns_name_t signer;
425 REQUIRE(rdata->type == 24);
426 REQUIRE(target != NULL);
427 REQUIRE(rdata->length != 0);
429 sig->common.rdclass = rdata->rdclass;
430 sig->common.rdtype = rdata->type;
431 ISC_LINK_INIT(&sig->common, link);
433 dns_rdata_toregion(rdata, &sr);
436 * Type covered.
438 sig->covered = uint16_fromregion(&sr);
439 isc_region_consume(&sr, 2);
442 * Algorithm.
444 sig->algorithm = uint8_fromregion(&sr);
445 isc_region_consume(&sr, 1);
448 * Labels.
450 sig->labels = uint8_fromregion(&sr);
451 isc_region_consume(&sr, 1);
454 * Original TTL.
456 sig->originalttl = uint32_fromregion(&sr);
457 isc_region_consume(&sr, 4);
460 * Expire time.
462 sig->timeexpire = uint32_fromregion(&sr);
463 isc_region_consume(&sr, 4);
466 * Time signed.
468 sig->timesigned = uint32_fromregion(&sr);
469 isc_region_consume(&sr, 4);
472 * Key ID.
474 sig->keyid = uint16_fromregion(&sr);
475 isc_region_consume(&sr, 2);
477 dns_name_init(&signer, NULL);
478 dns_name_fromregion(&signer, &sr);
479 dns_name_init(&sig->signer, NULL);
480 RETERR(name_duporclone(&signer, mctx, &sig->signer));
481 isc_region_consume(&sr, name_length(&sig->signer));
484 * Signature.
486 sig->siglen = sr.length;
487 sig->signature = mem_maybedup(mctx, sr.base, sig->siglen);
488 if (sig->signature == NULL)
489 goto cleanup;
492 sig->mctx = mctx;
493 return (ISC_R_SUCCESS);
495 cleanup:
496 if (mctx != NULL)
497 dns_name_free(&sig->signer, mctx);
498 return (ISC_R_NOMEMORY);
501 static inline void
502 freestruct_sig(ARGS_FREESTRUCT) {
503 dns_rdata_sig_t *sig = (dns_rdata_sig_t *) source;
505 REQUIRE(source != NULL);
506 REQUIRE(sig->common.rdtype == 24);
508 if (sig->mctx == NULL)
509 return;
511 dns_name_free(&sig->signer, sig->mctx);
512 if (sig->signature != NULL)
513 isc_mem_free(sig->mctx, sig->signature);
514 sig->mctx = NULL;
517 static inline isc_result_t
518 additionaldata_sig(ARGS_ADDLDATA) {
519 REQUIRE(rdata->type == 24);
521 UNUSED(rdata);
522 UNUSED(add);
523 UNUSED(arg);
525 return (ISC_R_SUCCESS);
528 static inline isc_result_t
529 digest_sig(ARGS_DIGEST) {
531 REQUIRE(rdata->type == 24);
533 UNUSED(rdata);
534 UNUSED(digest);
535 UNUSED(arg);
537 return (ISC_R_NOTIMPLEMENTED);
540 static inline dns_rdatatype_t
541 covers_sig(dns_rdata_t *rdata) {
542 dns_rdatatype_t type;
543 isc_region_t r;
545 REQUIRE(rdata->type == 24);
547 dns_rdata_toregion(rdata, &r);
548 type = uint16_fromregion(&r);
550 return (type);
553 #endif /* RDATA_GENERIC_SIG_24_C */