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 */
28 #include <isc/buffer.h>
29 #include <isc/region.h>
35 #include <dns/fixedname.h>
37 #include <dns/rdata.h>
38 #include <dns/rdatastruct.h>
39 #include <dns/result.h>
44 dns_ds_buildrdata(dns_name_t
*owner
, dns_rdata_t
*key
,
45 unsigned int digest_type
, unsigned char *buffer
,
48 dns_fixedname_t fname
;
50 unsigned char digest
[ISC_SHA256_DIGESTLENGTH
];
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
) {
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
);
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
);
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
;
98 return (dns_rdata_fromstruct(rdata
, key
->rdclass
, dns_rdatatype_ds
,
103 dns_ds_digest_supported(unsigned int digest_type
) {
104 return (ISC_TF(digest_type
== DNS_DSDIGEST_SHA1
||
105 digest_type
== DNS_DSDIGEST_SHA256
));