2 * Copyright (C) 2004-2007 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2002, 2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or 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: ds.c,v 1.11 2007/06/19 23:47:16 tbox Exp $ */
26 #include <isc/buffer.h>
27 #include <isc/region.h>
33 #include <dns/fixedname.h>
35 #include <dns/rdata.h>
36 #include <dns/rdatastruct.h>
37 #include <dns/result.h>
42 dns_ds_buildrdata(dns_name_t
*owner
, dns_rdata_t
*key
,
43 unsigned int digest_type
, unsigned char *buffer
,
46 dns_fixedname_t fname
;
48 unsigned char digest
[ISC_SHA256_DIGESTLENGTH
];
54 REQUIRE(key
->type
== dns_rdatatype_dnskey
);
56 if (!dns_ds_digest_supported(digest_type
))
57 return (ISC_R_NOTIMPLEMENTED
);
59 dns_fixedname_init(&fname
);
60 name
= dns_fixedname_name(&fname
);
61 (void)dns_name_downcase(owner
, name
, NULL
);
63 memset(buffer
, 0, DNS_DS_BUFFERSIZE
);
64 isc_buffer_init(&b
, buffer
, DNS_DS_BUFFERSIZE
);
66 if (digest_type
== DNS_DSDIGEST_SHA1
) {
69 dns_name_toregion(name
, &r
);
70 isc_sha1_update(&sha1
, r
.base
, r
.length
);
71 dns_rdata_toregion(key
, &r
);
72 INSIST(r
.length
>= 4);
73 isc_sha1_update(&sha1
, r
.base
, r
.length
);
74 isc_sha1_final(&sha1
, digest
);
77 isc_sha256_init(&sha256
);
78 dns_name_toregion(name
, &r
);
79 isc_sha256_update(&sha256
, r
.base
, r
.length
);
80 dns_rdata_toregion(key
, &r
);
81 INSIST(r
.length
>= 4);
82 isc_sha256_update(&sha256
, r
.base
, r
.length
);
83 isc_sha256_final(digest
, &sha256
);
87 ds
.common
.rdclass
= key
->rdclass
;
88 ds
.common
.rdtype
= dns_rdatatype_ds
;
89 ds
.algorithm
= r
.base
[3];
90 ds
.key_tag
= dst_region_computeid(&r
, ds
.algorithm
);
91 ds
.digest_type
= digest_type
;
92 ds
.length
= (digest_type
== DNS_DSDIGEST_SHA1
) ?
93 ISC_SHA1_DIGESTLENGTH
: ISC_SHA256_DIGESTLENGTH
;
96 return (dns_rdata_fromstruct(rdata
, key
->rdclass
, dns_rdatatype_ds
,
101 dns_ds_digest_supported(unsigned int digest_type
) {
102 return (ISC_TF(digest_type
== DNS_DSDIGEST_SHA1
||
103 digest_type
== DNS_DSDIGEST_SHA256
));