Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / dns / ds.c
blob081fa9ee77e80a559533be5a710c4e28db9362e9
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004-2007 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2002, 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: ds.c,v 1.11 2007/06/19 23:47:16 tbox Exp */
22 /*! \file */
24 #include <config.h>
26 #include <string.h>
28 #include <isc/buffer.h>
29 #include <isc/region.h>
30 #include <isc/sha1.h>
31 #include <isc/sha2.h>
32 #include <isc/util.h>
34 #include <dns/ds.h>
35 #include <dns/fixedname.h>
36 #include <dns/name.h>
37 #include <dns/rdata.h>
38 #include <dns/rdatastruct.h>
39 #include <dns/result.h>
41 #include <dst/dst.h>
43 isc_result_t
44 dns_ds_buildrdata(dns_name_t *owner, dns_rdata_t *key,
45 unsigned int digest_type, unsigned char *buffer,
46 dns_rdata_t *rdata)
48 dns_fixedname_t fname;
49 dns_name_t *name;
50 unsigned char digest[ISC_SHA256_DIGESTLENGTH];
51 isc_region_t r;
52 isc_buffer_t b;
53 dns_rdata_ds_t ds;
55 REQUIRE(key != NULL);
56 REQUIRE(key->type == dns_rdatatype_dnskey);
58 if (!dns_ds_digest_supported(digest_type))
59 return (ISC_R_NOTIMPLEMENTED);
61 dns_fixedname_init(&fname);
62 name = dns_fixedname_name(&fname);
63 (void)dns_name_downcase(owner, name, NULL);
65 memset(buffer, 0, DNS_DS_BUFFERSIZE);
66 isc_buffer_init(&b, buffer, DNS_DS_BUFFERSIZE);
68 if (digest_type == DNS_DSDIGEST_SHA1) {
69 isc_sha1_t sha1;
70 isc_sha1_init(&sha1);
71 dns_name_toregion(name, &r);
72 isc_sha1_update(&sha1, r.base, r.length);
73 dns_rdata_toregion(key, &r);
74 INSIST(r.length >= 4);
75 isc_sha1_update(&sha1, r.base, r.length);
76 isc_sha1_final(&sha1, digest);
77 } else {
78 isc_sha256_t sha256;
79 isc_sha256_init(&sha256);
80 dns_name_toregion(name, &r);
81 isc_sha256_update(&sha256, r.base, r.length);
82 dns_rdata_toregion(key, &r);
83 INSIST(r.length >= 4);
84 isc_sha256_update(&sha256, r.base, r.length);
85 isc_sha256_final(digest, &sha256);
88 ds.mctx = NULL;
89 ds.common.rdclass = key->rdclass;
90 ds.common.rdtype = dns_rdatatype_ds;
91 ds.algorithm = r.base[3];
92 ds.key_tag = dst_region_computeid(&r, ds.algorithm);
93 ds.digest_type = digest_type;
94 ds.length = (digest_type == DNS_DSDIGEST_SHA1) ?
95 ISC_SHA1_DIGESTLENGTH : ISC_SHA256_DIGESTLENGTH;
96 ds.digest = digest;
98 return (dns_rdata_fromstruct(rdata, key->rdclass, dns_rdatatype_ds,
99 &ds, &b));
102 isc_boolean_t
103 dns_ds_digest_supported(unsigned int digest_type) {
104 return (ISC_TF(digest_type == DNS_DSDIGEST_SHA1 ||
105 digest_type == DNS_DSDIGEST_SHA256));